def _toString_LSH(self): """ Return a public or private LSH key. See _fromString_PUBLIC_LSH and _fromString_PRIVATE_LSH for the key formats. @rtype: C{str} """ data = self.data() if self.isPublic(): if self.type() == 'RSA': keyData = sexpy.pack([[ 'public-key', [ 'rsa-pkcs1-sha1', ['n', common.MP(data['n'])[4:]], ['e', common.MP(data['e'])[4:]] ] ]]) elif self.type() == 'DSA': keyData = sexpy.pack([[ 'public-key', [ 'dsa', ['p', common.MP(data['p'])[4:]], ['q', common.MP(data['q'])[4:]], ['g', common.MP(data['g'])[4:]], ['y', common.MP(data['y'])[4:]] ] ]]) return '{' + base64.encodestring(keyData).replace('\n', '') + '}' else: if self.type() == 'RSA': p, q = data['p'], data['q'] return sexpy.pack([[ 'private-key', [ 'rsa-pkcs1', ['n', common.MP(data['n'])[4:]], ['e', common.MP(data['e'])[4:]], ['d', common.MP(data['d'])[4:]], ['p', common.MP(q)[4:]], ['q', common.MP(p)[4:]], ['a', common.MP(data['d'] % (q - 1))[4:]], ['b', common.MP(data['d'] % (p - 1))[4:]], ['c', common.MP(data['u'])[4:]] ] ]]) elif self.type() == 'DSA': return sexpy.pack([[ 'private-key', [ 'dsa', ['p', common.MP(data['p'])[4:]], ['q', common.MP(data['q'])[4:]], ['g', common.MP(data['g'])[4:]], ['y', common.MP(data['y'])[4:]], ['x', common.MP(data['x'])[4:]] ] ]])
def _toString_LSH(self): """ Return a public or private LSH key. See _fromString_PUBLIC_LSH and _fromString_PRIVATE_LSH for the key formats. @rtype: L{bytes} """ data = self.data() type = self.type() if self.isPublic(): if type == 'RSA': keyData = sexpy.pack([[b'public-key', [b'rsa-pkcs1-sha1', [b'n', common.MP(data['n'])[4:]], [b'e', common.MP(data['e'])[4:]]]]]) elif type == 'DSA': keyData = sexpy.pack([[b'public-key', [b'dsa', [b'p', common.MP(data['p'])[4:]], [b'q', common.MP(data['q'])[4:]], [b'g', common.MP(data['g'])[4:]], [b'y', common.MP(data['y'])[4:]]]]]) else: raise BadKeyError("unknown key type %s" % (type,)) return (b'{' + encodebytes(keyData).replace(b'\n', b'') + b'}') else: if type == 'RSA': p, q = data['p'], data['q'] return sexpy.pack([[b'private-key', [b'rsa-pkcs1', [b'n', common.MP(data['n'])[4:]], [b'e', common.MP(data['e'])[4:]], [b'd', common.MP(data['d'])[4:]], [b'p', common.MP(q)[4:]], [b'q', common.MP(p)[4:]], [b'a', common.MP( data['d'] % (q - 1))[4:]], [b'b', common.MP( data['d'] % (p - 1))[4:]], [b'c', common.MP(data['u'])[4:]]]]]) elif type == 'DSA': return sexpy.pack([[b'private-key', [b'dsa', [b'p', common.MP(data['p'])[4:]], [b'q', common.MP(data['q'])[4:]], [b'g', common.MP(data['g'])[4:]], [b'y', common.MP(data['y'])[4:]], [b'x', common.MP(data['x'])[4:]]]]]) else: raise BadKeyError("unknown key type %s'" % (type,))
def _toString_LSH(self): """ Return a public or private LSH key. See _fromString_PUBLIC_LSH and _fromString_PRIVATE_LSH for the key formats. @rtype: L{bytes} """ data = self.data() type = self.type() if self.isPublic(): if type == 'RSA': keyData = sexpy.pack([[b'public-key', [b'rsa-pkcs1-sha1', [b'n', common.MP(data['n'])[4:]], [b'e', common.MP(data['e'])[4:]]]]]) elif type == 'DSA': keyData = sexpy.pack([[b'public-key', [b'dsa', [b'p', common.MP(data['p'])[4:]], [b'q', common.MP(data['q'])[4:]], [b'g', common.MP(data['g'])[4:]], [b'y', common.MP(data['y'])[4:]]]]]) else: raise BadKeyError("unknown key type %s" % (type,)) return (b'{' + base64.encodestring(keyData).replace(b'\n', b'') + b'}') else: if type == 'RSA': p, q = data['p'], data['q'] return sexpy.pack([[b'private-key', [b'rsa-pkcs1', [b'n', common.MP(data['n'])[4:]], [b'e', common.MP(data['e'])[4:]], [b'd', common.MP(data['d'])[4:]], [b'p', common.MP(q)[4:]], [b'q', common.MP(p)[4:]], [b'a', common.MP( data['d'] % (q - 1))[4:]], [b'b', common.MP( data['d'] % (p - 1))[4:]], [b'c', common.MP(data['u'])[4:]]]]]) elif type == 'DSA': return sexpy.pack([[b'private-key', [b'dsa', [b'p', common.MP(data['p'])[4:]], [b'q', common.MP(data['q'])[4:]], [b'g', common.MP(data['g'])[4:]], [b'y', common.MP(data['y'])[4:]], [b'x', common.MP(data['x'])[4:]]]]]) else: raise BadKeyError("unknown key type %s'" % (type,))
def test_fromLSH(self): """ Test that keys are correctly generated from LSH strings. """ self._testPublicPrivateFromString(keydata.publicRSA_lsh, keydata.privateRSA_lsh, 'RSA', keydata.RSAData) self._testPublicPrivateFromString(keydata.publicDSA_lsh, keydata.privateDSA_lsh, 'DSA', keydata.DSAData) sexp = sexpy.pack([['public-key', ['bad-key', ['p', '2']]]]) self.assertRaises(keys.BadKeyError, keys.Key.fromString, data='{'+base64.encodestring(sexp)+'}') sexp = sexpy.pack([['private-key', ['bad-key', ['p', '2']]]]) self.assertRaises(keys.BadKeyError, keys.Key.fromString, sexp)
def _toString_LSH(self): """ Return a public or private LSH key. See _fromString_PUBLIC_LSH and _fromString_PRIVATE_LSH for the key formats. @rtype: C{str} """ data = self.data() if self.isPublic(): if self.type() == 'RSA': keyData = sexpy.pack([['public-key', ['rsa-pkcs1-sha1', ['n', common.MP(data['n'])[4:]], ['e', common.MP(data['e'])[4:]]]]]) elif self.type() == 'DSA': keyData = sexpy.pack([['public-key', ['dsa', ['p', common.MP(data['p'])[4:]], ['q', common.MP(data['q'])[4:]], ['g', common.MP(data['g'])[4:]], ['y', common.MP(data['y'])[4:]]]]]) return '{' + base64.encodestring(keyData).replace('\n', '') + '}' else: if self.type() == 'RSA': p, q = data['p'], data['q'] return sexpy.pack([['private-key', ['rsa-pkcs1', ['n', common.MP(data['n'])[4:]], ['e', common.MP(data['e'])[4:]], ['d', common.MP(data['d'])[4:]], ['p', common.MP(q)[4:]], ['q', common.MP(p)[4:]], ['a', common.MP(data['d'] % (q - 1))[4:]], ['b', common.MP(data['d'] % (p - 1))[4:]], ['c', common.MP(data['u'])[4:]]]]]) elif self.type() == 'DSA': return sexpy.pack([['private-key', ['dsa', ['p', common.MP(data['p'])[4:]], ['q', common.MP(data['q'])[4:]], ['g', common.MP(data['g'])[4:]], ['y', common.MP(data['y'])[4:]], ['x', common.MP(data['x'])[4:]]]]])
def test_fromLSHPrivateUnsupportedType(self): """ C{BadKeyError} exception is raised when private key has an unknown type. """ sexp = sexpy.pack([[b'private-key', [b'bad-key', [b'p', b'2']]]]) self.assertRaises( keys.BadKeyError, keys.Key.fromString, sexp, )
def test_fromLSHPublicUnsupportedType(self): """ C{BadKeyError} exception is raised when public key has an unknown type. """ sexp = sexpy.pack([[b'public-key', [b'bad-key', [b'p', b'2']]]]) self.assertRaises( keys.BadKeyError, keys.Key.fromString, data=b'{' + base64.encodestring(sexp) + b'}', )
def test_getPublicKeyStringErrors(self): """ Test that getPublicKeyString raises errors in appropriate cases. """ self.assertWarns(DeprecationWarning, "getPublicKeyString is deprecated" " since Twisted Conch 0.9. Use Key.fromString().", unittest.__file__, self.assertRaises, keys.BadKeyError, keys.getPublicKeyString, self.publicKeyFile, 1, data=keydata.publicRSA_openssh) self.assertWarns(DeprecationWarning, "getPublicKeyString is deprecated" " since Twisted Conch 0.9. Use Key.fromString().", unittest.__file__, self.assertRaises, keys.BadKeyError, keys.getPublicKeyString, data = 'invalid key') sexp = sexpy.pack([['public-key', ['bad-key', ['p', '2']]]]) self.assertWarns(DeprecationWarning, "getPublicKeyString is deprecated" " since Twisted Conch 0.9. Use Key.fromString().", unittest.__file__, self.assertRaises, keys.BadKeyError, keys.getPublicKeyString, data='{'+base64.encodestring(sexp)+'}')
def test_getPrivateKeyObjectErrors(self): """ Test that getPrivateKeyObject raises errors in appropriate cases. """ self.assertWarns(DeprecationWarning, "getPrivateKeyObject is deprecated" " since Twisted Conch 0.9. Use Key.fromString().", unittest.__file__, self.assertRaises, keys.BadKeyError, keys.getPrivateKeyObject, self.privateKeyFile, keydata.privateRSA_openssh) self.assertWarns(DeprecationWarning, "getPrivateKeyObject is deprecated" " since Twisted Conch 0.9. Use Key.fromString().", unittest.__file__, self.assertRaises, keys.BadKeyError, keys.getPrivateKeyObject, data = 'invalid key') sexp = sexpy.pack([['private-key', ['bad-key', ['p', '2']]]]) self.assertWarns(DeprecationWarning, "getPrivateKeyObject is deprecated" " since Twisted Conch 0.9. Use Key.fromString().", unittest.__file__, self.assertRaises, keys.BadKeyError, keys.getPrivateKeyObject, data=sexp) self.assertWarns(DeprecationWarning, "getPrivateKeyObject is deprecated" " since Twisted Conch 0.9. Use Key.fromString().", unittest.__file__, self.assertRaises, keys.BadKeyError, keys.getPrivateKeyObject, data='\x00\x00\x00\x07ssh-foo'+'\x00\x00\x00\x01\x01'*5)