def makePublicKeyBlob(obj): keyType = objectType(obj) if keyType == 'ssh-rsa': keyData = common.MP(obj.e) + common.MP(obj.n) elif keyType == 'ssh-dss': keyData = common.MP(obj.p) keyData += common.MP(obj.q) keyData += common.MP(obj.g) keyData += common.MP(obj.y) return common.NS(keyType) + keyData
def makePublicKeyString_openssh(obj, comment): keyType = objectType(obj) if keyType == 'ssh-rsa': keyData = common.MP(obj.e) + common.MP(obj.n) elif keyType == 'ssh-dss': keyData = common.MP(obj.p) keyData += common.MP(obj.q) keyData += common.MP(obj.g) keyData += common.MP(obj.y) else: raise BadKeyError('unknown key type %s' % keyType) b64Data = base64.encodestring(common.NS(keyType) + keyData).replace( '\n', '') return '%s %s %s' % (keyType, b64Data, comment)
def printKey(obj): """ Pretty print a C{Crypto.PublicKey.pubkey.pubkey} object. @type obj: C{Crypto.PublicKey.pubkey.pubkey} """ print '%s %s (%s bits)' % (objectType(obj), obj.hasprivate() and 'Private Key' or 'Public Key', obj.size()) for k in obj.keydata: if hasattr(obj, k): print 'attr', k by = common.MP(getattr(obj, k))[4:] while by: m = by[:15] by = by[15:] o = '' for c in m: o = o + '%02x:' % ord(c) if len(m) < 15: o = o[:-1] print '\t' + o
def makePublicKeyString_lsh(obj): keyType = objectType(obj) if keyType == 'ssh-rsa': keyData = sexpy.pack([[ 'public-key', [ 'rsa-pkcs1-sha1', ['n', common.MP(obj.n)[4:]], ['e', common.MP(obj.e)[4:]] ] ]]) elif keyType == 'ssh-dss': keyData = sexpy.pack([[ 'public-key', [ 'dsa', ['p', common.MP(obj.p)[4:]], ['q', common.MP(obj.q)[4:]], ['g', common.MP(obj.g)[4:]], ['y', common.MP(obj.y)[4:]] ] ]]) else: raise BadKeyError('bad keyType %s' % keyType) return '{' + base64.encodestring(keyData).replace('\n', '') + '}'
def makePrivateKeyBlob(obj): keyType = objectType(obj) if keyType == 'ssh-rsa': return common.NS(keyType) + common.MP(obj.n) + common.MP(obj.e) + \ common.MP(obj.d) + common.MP(obj.u) + common.MP(obj.q) + \ common.MP(obj.p) elif keyType == 'ssh-dss': return common.NS(keyType) + common.MP(obj.p) + common.MP(obj.q) + \ common.MP(obj.g) + common.MP(obj.y) + common.MP(obj.x) else: raise ValueError('trying to get blob for invalid key type: %s' % keyType)
def makePrivateKeyString_lsh(obj, passphrase): if passphrase: raise BadKeyError("cannot encrypt to lsh format") keyType = objectType(obj) if keyType == 'ssh-rsa': p, q = obj.p, obj.q if p > q: (p, q) = (q, p) return sexpy.pack([[ 'private-key', [ 'rsa-pkcs1', ['n', common.MP(obj.n)[4:]], ['e', common.MP(obj.e)[4:]], ['d', common.MP(obj.d)[4:]], ['p', common.MP(q)[4:]], ['q', common.MP(p)[4:]], ['a', common.MP(obj.d % (q - 1))[4:]], ['b', common.MP(obj.d % (p - 1))[4:]], ['c', common.MP(Util.number.inverse(p, q))[4:]] ] ]]) elif keyType == 'ssh-dss': return sexpy.pack([[ 'private-key', [ 'dsa', ['p', common.MP(obj.p)[4:]], ['q', common.MP(obj.q)[4:]], ['g', common.MP(obj.g)[4:]], ['y', common.MP(obj.y)[4:]], ['x', common.MP(obj.x)[4:]] ] ]]) else: raise BadKeyError('bad keyType %s' % keyType)