def _fromString_PRIVATE_LSH(Class, data): """ Return a private key corresponding to this LSH private key string. The LSH private key string format is:: <s-expression: ('private-key', (<key type>, (<name>, <value>)+))> The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e, d, p, q. The names for a DSA (key type 'dsa') key are: y, g, p, q, x. @type data: C{str} @return: a {Crypto.PublicKey.pubkey.pubkey} object @raises BadKeyError: if the key type is unknown """ sexp = sexpy.parse(data) assert sexp[0] == 'private-key' kd = {} for name, data in sexp[1][1:]: kd[name] = common.getMP(common.NS(data))[0] if sexp[1][0] == 'dsa': assert len(kd) == 5, len(kd) return Class( DSA.construct((kd['y'], kd['g'], kd['p'], kd['q'], kd['x']))) elif sexp[1][0] == 'rsa-pkcs1': assert len(kd) == 8, len(kd) if kd['p'] > kd['q']: # make p smaller than q kd['p'], kd['q'] = kd['q'], kd['p'] return Class( RSA.construct((kd['n'], kd['e'], kd['d'], kd['p'], kd['q']))) else: raise BadKeyError('unknown lsh key type %s' % sexp[1][0])
def _fromString_PRIVATE_LSH(cls, data): """ Return a private key corresponding to this LSH private key string. The LSH private key string format is:: <s-expression: ('private-key', (<key type>, (<name>, <value>)+))> The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e, d, p, q. The names for a DSA (key type 'dsa') key are: y, g, p, q, x. @type data: L{bytes} @param data: The key data. @return: A new key. @rtype: L{twisted.conch.ssh.keys.Key} @raises BadKeyError: if the key type is unknown """ sexp = sexpy.parse(data) assert sexp[0] == b'private-key' kd = {} for name, data in sexp[1][1:]: kd[name] = common.getMP(common.NS(data))[0] if sexp[1][0] == b'dsa': assert len(kd) == 5, len(kd) return cls._fromDSAComponents( y=kd[b'y'], g=kd[b'g'], p=kd[b'p'], q=kd[b'q'], x=kd[b'x']) elif sexp[1][0] == b'rsa-pkcs1': assert len(kd) == 8, len(kd) if kd[b'p'] > kd[b'q']: # Make p smaller than q kd[b'p'], kd[b'q'] = kd[b'q'], kd[b'p'] return cls._fromRSAComponents( n=kd[b'n'], e=kd[b'e'], d=kd[b'd'], p=kd[b'p'], q=kd[b'q']) else: raise BadKeyError('unknown lsh key type %s' % (sexp[1][0],))
def _fromString_PRIVATE_LSH(Class, data): """ Return a private key corresponding to this LSH private key string. The LSH private key string format is:: <s-expression: ('private-key', (<key type>, (<name>, <value>)+))> The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e, d, p, q. The names for a DSA (key type 'dsa') key are: y, g, p, q, x. @type data: C{str} @return: a {Crypto.PublicKey.pubkey.pubkey} object @raises BadKeyError: if the key type is unknown """ sexp = sexpy.parse(data) assert sexp[0] == 'private-key' kd = {} for name, data in sexp[1][1:]: kd[name] = common.getMP(common.NS(data))[0] if sexp[1][0] == 'dsa': assert len(kd) == 5, len(kd) return Class(DSA.construct((kd['y'], kd['g'], kd['p'], kd['q'], kd['x']))) elif sexp[1][0] == 'rsa-pkcs1': assert len(kd) == 8, len(kd) if kd['p'] > kd['q']: # make p smaller than q kd['p'], kd['q'] = kd['q'], kd['p'] return Class(RSA.construct((kd['n'], kd['e'], kd['d'], kd['p'], kd['q']))) else: raise BadKeyError('unknown lsh key type %s' % sexp[1][0])
def _fromString_PUBLIC_LSH(cls, data): """ Return a public key corresponding to this LSH public key string. The LSH public key string format is:: <s-expression: ('public-key', (<key type>, (<name, <value>)+))> The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e. The names for a DSA (key type 'dsa') key are: y, g, p, q. @type data: L{bytes} @param data: The key data. @return: A new key. @rtype: L{twisted.conch.ssh.keys.Key} @raises BadKeyError: if the key type is unknown """ sexp = sexpy.parse(decodebytes(data[1:-1])) assert sexp[0] == b'public-key' kd = {} for name, data in sexp[1][1:]: kd[name] = common.getMP(common.NS(data))[0] if sexp[1][0] == b'dsa': return cls._fromDSAComponents( y=kd[b'y'], g=kd[b'g'], p=kd[b'p'], q=kd[b'q']) elif sexp[1][0] == b'rsa-pkcs1-sha1': return cls._fromRSAComponents(n=kd[b'n'], e=kd[b'e']) else: raise BadKeyError('unknown lsh key type %s' % (sexp[1][0],))
def _fromString_PUBLIC_LSH(Class, data): """ Return a public key corresponding to this LSH public key string. The LSH public key string format is:: <s-expression: ('public-key', (<key type>, (<name, <value>)+))> The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e. The names for a DSA (key type 'dsa') key are: y, g, p, q. @type data: C{bytes} @return: a C{Crypto.PublicKey.pubkey.pubkey} object @raises BadKeyError: if the key type is unknown """ sexp = sexpy.parse(base64.decodestring(data[1:-1])) assert sexp[0] == b'public-key' kd = {} for name, data in sexp[1][1:]: kd[name] = common.getMP(common.NS(data))[0] if sexp[1][0] == b'dsa': return Class( DSA.construct((kd[b'y'], kd[b'g'], kd[b'p'], kd[b'q']))) elif sexp[1][0] == b'rsa-pkcs1-sha1': return Class(RSA.construct((kd[b'n'], kd[b'e']))) else: raise BadKeyError('unknown lsh key type %s' % (sexp[1][0], ))
def _fromString_PUBLIC_LSH(cls, data): """ Return a public key corresponding to this LSH public key string. The LSH public key string format is:: <s-expression: ('public-key', (<key type>, (<name, <value>)+))> The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e. The names for a DSA (key type 'dsa') key are: y, g, p, q. @type data: L{bytes} @param data: The key data. @return: A new key. @rtype: L{twisted.conch.ssh.keys.Key} @raises BadKeyError: if the key type is unknown """ sexp = sexpy.parse(base64.decodestring(data[1:-1])) assert sexp[0] == b'public-key' kd = {} for name, data in sexp[1][1:]: kd[name] = common.getMP(common.NS(data))[0] if sexp[1][0] == b'dsa': return cls._fromDSAComponents( y=kd[b'y'], g=kd[b'g'], p=kd[b'p'], q=kd[b'q']) elif sexp[1][0] == b'rsa-pkcs1-sha1': return cls._fromRSAComponents(n=kd[b'n'], e=kd[b'e']) else: raise BadKeyError('unknown lsh key type %s' % (sexp[1][0],))
def _fromString_PUBLIC_LSH(Class, data): """ Return a public key corresponding to this LSH public key string. The LSH public key string format is:: <s-expression: ('public-key', (<key type>, (<name, <value>)+))> The names for a RSA (key type 'rsa-pkcs1-sha1') key are: n, e. The names for a DSA (key type 'dsa') key are: y, g, p, q. @type data: C{str} @return: a C{Crypto.PublicKey.pubkey.pubkey} object @raises BadKeyError: if the key type is unknown """ sexp = sexpy.parse(base64.decodestring(data[1:-1])) assert sexp[0] == 'public-key' kd = {} for name, data in sexp[1][1:]: kd[name] = common.getMP(common.NS(data))[0] if sexp[1][0] == 'dsa': return Class(DSA.construct((kd['y'], kd['g'], kd['p'], kd['q']))) elif sexp[1][0] == 'rsa-pkcs1-sha1': return Class(RSA.construct((kd['n'], kd['e']))) else: raise BadKeyError('unknown lsh key type %s' % sexp[1][0])