r46798 - Merge conch-regression-8208: Make Conch accept PyCrypto keys again.

mithrandi-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: mithrandi
Date: Mon Feb 15 03:24:17 2016
New Revision: 46798

Added:
   trunk/twisted/conch/topfiles/8208.misc
Modified:
   trunk/twisted/conch/ssh/keys.py
   trunk/twisted/conch/test/test_keys.py

Log:
Merge conch-regression-8208: Make Conch accept PyCrypto keys again.

Author: mithrandi
Reviewer: glyph
Fixes: #8208

`twisted.conch.ssh.keys.Key` now accepts PyCrypto keys again, although a
deprecation warning is emitted.

Modified: trunk/twisted/conch/ssh/keys.py
==============================================================================
--- trunk/twisted/conch/ssh/keys.py	(original)
+++ trunk/twisted/conch/ssh/keys.py	Mon Feb 15 03:24:17 2016
@@ -10,6 +10,7 @@
 
 import base64
 import itertools
+import warnings
 from hashlib import md5
 
 from cryptography.exceptions import InvalidSignature
@@ -34,7 +35,7 @@
 from twisted.conch.ssh.common import int_from_bytes, int_to_bytes
 from twisted.python import randbytes
 from twisted.python.compat import iterbytes, long, izip, nativeString, _PY3
-from twisted.python.deprecate import deprecated
+from twisted.python.deprecate import deprecated, getDeprecationWarningString
 from twisted.python.versions import Version
 
 
@@ -601,7 +602,16 @@
         @param keyObject: Low level key.
         @type keyObject: C{cryptography.hazmat.primitives.asymmetric} key.
         """
-        self._keyObject = keyObject
+        # Avoid importing PyCrypto if at all possible
+        if keyObject.__class__.__module__.startswith('Crypto.PublicKey'):
+            warningString = getDeprecationWarningString(
+                Key,
+                Version("Twisted", 16, 0, 0),
+                replacement='passing a cryptography key object')
+            warnings.warn(warningString, DeprecationWarning, stacklevel=2)
+            self.keyObject = keyObject
+        else:
+            self._keyObject = keyObject
 
 
     def __eq__(self, other):

Modified: trunk/twisted/conch/test/test_keys.py
==============================================================================
--- trunk/twisted/conch/test/test_keys.py	(original)
+++ trunk/twisted/conch/test/test_keys.py	Mon Feb 15 03:24:17 2016
@@ -15,6 +15,8 @@
 
 try:
     import Crypto.Cipher.DES3
+    import Crypto.PublicKey.RSA
+    import Crypto.PublicKey.DSA
 except ImportError:
     # we'll have to skip some tests without PyCypto
     Crypto = None
@@ -32,6 +34,7 @@
 from twisted.python import randbytes
 from twisted.trial import unittest
 from twisted.python.compat import long, _PY3
+from twisted.python.versions import Version
 
 
 
@@ -1136,3 +1139,23 @@
             'x': keydata.DSAData['x'],
             },
             key.data())
+
+
+    def test_constructorPyCrypto(self):
+        """
+        Passing a PyCrypto key object to L{keys.Key} is deprecated.
+        """
+        pycryptoKey = Crypto.PublicKey.RSA.construct((
+            keydata.RSAData['n'],
+            keydata.RSAData['e']))
+        key = self.callDeprecated(
+            (Version('Twisted', 16, 0, 0),
+             'passing a cryptography key object'),
+            keys.Key,
+            pycryptoKey)
+        self.assertEqual('RSA', key.type())
+        self.assertEqual({
+            'n': keydata.RSAData['n'],
+            'e': keydata.RSAData['e'],
+            },
+            key.data())
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.