Skip to content

Instantly share code, notes, and snippets.

@MorbosVermin
Last active August 29, 2015 14:06
Show Gist options
  • Save MorbosVermin/8a9940c90334b8ffee18 to your computer and use it in GitHub Desktop.
Save MorbosVermin/8a9940c90334b8ffee18 to your computer and use it in GitHub Desktop.
import os, sys, base64
from M2Crypto import X509
'''
Load the certificate from a file.
'''
cert = X509.load_cert(filename, X509.FORMAT_DER)
'''
Lets first save the certiticate as PEM.
'''
cert.save_pem(name +".crt")
#cert.save(name +".cer", 0) //As DER format, but we loaded from a DER file. So not needed.
'''
Convert public key to SSH-RSA format and write this to a file.
'''
name = "<Email or name here>"
pk = cert.get_pubkey().get_rsa()
content = "ssh-rsa %s %s\n" % (base64.b64encode("\0\0\0\7ssh-rsa%s%s" % pk.pub()), name)
filename = "%s.pub.asc" % name
file(filename, 'w').write(content);
'''
You can use the contents of the written file within .ssh/authorized_keys(2) for pubkeyauth. Danke.
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment