OLD | NEW |
| 1 # Author: Trevor Perrin |
| 2 # See the LICENSE file for legal information regarding use of this file. |
| 3 |
1 """Factory functions for asymmetric cryptography. | 4 """Factory functions for asymmetric cryptography. |
2 @sort: generateRSAKey, parseXMLKey, parsePEMKey, parseAsPublicKey, | 5 @sort: generateRSAKey, parsePEMKey, parseAsPublicKey |
3 parseAsPrivateKey | |
4 """ | 6 """ |
5 | 7 |
6 from compat import * | 8 from .compat import * |
7 | 9 |
8 from rsakey import RSAKey | 10 from .rsakey import RSAKey |
9 from python_rsakey import Python_RSAKey | 11 from .python_rsakey import Python_RSAKey |
10 import cryptomath | 12 from tlslite.utils import cryptomath |
11 | 13 |
12 if cryptomath.m2cryptoLoaded: | 14 if cryptomath.m2cryptoLoaded: |
13 from openssl_rsakey import OpenSSL_RSAKey | 15 from .openssl_rsakey import OpenSSL_RSAKey |
14 | 16 |
15 if cryptomath.pycryptoLoaded: | 17 if cryptomath.pycryptoLoaded: |
16 from pycrypto_rsakey import PyCrypto_RSAKey | 18 from .pycrypto_rsakey import PyCrypto_RSAKey |
17 | 19 |
18 # ************************************************************************** | 20 # ************************************************************************** |
19 # Factory Functions for RSA Keys | 21 # Factory Functions for RSA Keys |
20 # ************************************************************************** | 22 # ************************************************************************** |
21 | 23 |
22 def generateRSAKey(bits, implementations=["openssl", "python"]): | 24 def generateRSAKey(bits, implementations=["openssl", "python"]): |
23 """Generate an RSA key with the specified bit length. | 25 """Generate an RSA key with the specified bit length. |
24 | 26 |
25 @type bits: int | 27 @type bits: int |
26 @param bits: Desired bit length of the new key's modulus. | 28 @param bits: Desired bit length of the new key's modulus. |
27 | 29 |
28 @rtype: L{tlslite.utils.RSAKey.RSAKey} | 30 @rtype: L{tlslite.utils.rsakey.RSAKey} |
29 @return: A new RSA private key. | 31 @return: A new RSA private key. |
30 """ | 32 """ |
31 for implementation in implementations: | 33 for implementation in implementations: |
32 if implementation == "openssl" and cryptomath.m2cryptoLoaded: | 34 if implementation == "openssl" and cryptomath.m2cryptoLoaded: |
33 return OpenSSL_RSAKey.generate(bits) | 35 return OpenSSL_RSAKey.generate(bits) |
34 elif implementation == "python": | 36 elif implementation == "python": |
35 return Python_RSAKey.generate(bits) | 37 return Python_RSAKey.generate(bits) |
36 raise ValueError("No acceptable implementations") | 38 raise ValueError("No acceptable implementations") |
37 | 39 |
38 def parseXMLKey(s, private=False, public=False, implementations=["python"]): | |
39 """Parse an XML-format key. | |
40 | |
41 The XML format used here is specific to tlslite and cryptoIDlib. The | |
42 format can store the public component of a key, or the public and | |
43 private components. For example:: | |
44 | |
45 <publicKey xmlns="http://trevp.net/rsa"> | |
46 <n>4a5yzB8oGNlHo866CAspAC47M4Fvx58zwK8pou... | |
47 <e>Aw==</e> | |
48 </publicKey> | |
49 | |
50 <privateKey xmlns="http://trevp.net/rsa"> | |
51 <n>4a5yzB8oGNlHo866CAspAC47M4Fvx58zwK8pou... | |
52 <e>Aw==</e> | |
53 <d>JZ0TIgUxWXmL8KJ0VqyG1V0J3ern9pqIoB0xmy... | |
54 <p>5PreIj6z6ldIGL1V4+1C36dQFHNCQHJvW52GXc... | |
55 <q>/E/wDit8YXPCxx126zTq2ilQ3IcW54NJYyNjiZ... | |
56 <dP>mKc+wX8inDowEH45Qp4slRo1YveBgExKPROu6... | |
57 <dQ>qDVKtBz9lk0shL5PR3ickXDgkwS576zbl2ztB... | |
58 <qInv>j6E8EA7dNsTImaXexAmLA1DoeArsYeFAInr... | |
59 </privateKey> | |
60 | |
61 @type s: str | |
62 @param s: A string containing an XML public or private key. | |
63 | |
64 @type private: bool | |
65 @param private: If True, a L{SyntaxError} will be raised if the private | |
66 key component is not present. | |
67 | |
68 @type public: bool | |
69 @param public: If True, the private key component (if present) will be | |
70 discarded, so this function will always return a public key. | |
71 | |
72 @rtype: L{tlslite.utils.RSAKey.RSAKey} | |
73 @return: An RSA key. | |
74 | |
75 @raise SyntaxError: If the key is not properly formatted. | |
76 """ | |
77 for implementation in implementations: | |
78 if implementation == "python": | |
79 key = Python_RSAKey.parseXML(s) | |
80 break | |
81 else: | |
82 raise ValueError("No acceptable implementations") | |
83 | |
84 return _parseKeyHelper(key, private, public) | |
85 | |
86 #Parse as an OpenSSL or Python key | 40 #Parse as an OpenSSL or Python key |
87 def parsePEMKey(s, private=False, public=False, passwordCallback=None, | 41 def parsePEMKey(s, private=False, public=False, passwordCallback=None, |
88 implementations=["openssl", "python"]): | 42 implementations=["openssl", "python"]): |
89 """Parse a PEM-format key. | 43 """Parse a PEM-format key. |
90 | 44 |
91 The PEM format is used by OpenSSL and other tools. The | 45 The PEM format is used by OpenSSL and other tools. The |
92 format is typically used to store both the public and private | 46 format is typically used to store both the public and private |
93 components of a key. For example:: | 47 components of a key. For example:: |
94 | 48 |
95 -----BEGIN RSA PRIVATE KEY----- | 49 -----BEGIN RSA PRIVATE KEY----- |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 | 118 |
165 if private: | 119 if private: |
166 if hasattr(key, "d"): | 120 if hasattr(key, "d"): |
167 return _createPrivateKey(key) | 121 return _createPrivateKey(key) |
168 else: | 122 else: |
169 return key | 123 return key |
170 | 124 |
171 return key | 125 return key |
172 | 126 |
173 def parseAsPublicKey(s): | 127 def parseAsPublicKey(s): |
174 """Parse an XML or PEM-formatted public key. | 128 """Parse a PEM-formatted public key. |
175 | 129 |
176 @type s: str | 130 @type s: str |
177 @param s: A string containing an XML or PEM-encoded public or private key. | 131 @param s: A string containing a PEM-encoded public or private key. |
178 | 132 |
179 @rtype: L{tlslite.utils.RSAKey.RSAKey} | 133 @rtype: L{tlslite.utils.rsakey.RSAKey} |
180 @return: An RSA public key. | 134 @return: An RSA public key. |
181 | 135 |
182 @raise SyntaxError: If the key is not properly formatted. | 136 @raise SyntaxError: If the key is not properly formatted. |
183 """ | 137 """ |
184 try: | 138 return parsePEMKey(s, public=True) |
185 return parsePEMKey(s, public=True) | |
186 except: | |
187 return parseXMLKey(s, public=True) | |
188 | 139 |
189 def parsePrivateKey(s): | 140 def parsePrivateKey(s): |
190 """Parse an XML or PEM-formatted private key. | 141 """Parse a PEM-formatted private key. |
191 | 142 |
192 @type s: str | 143 @type s: str |
193 @param s: A string containing an XML or PEM-encoded private key. | 144 @param s: A string containing a PEM-encoded private key. |
194 | 145 |
195 @rtype: L{tlslite.utils.RSAKey.RSAKey} | 146 @rtype: L{tlslite.utils.rsakey.RSAKey} |
196 @return: An RSA private key. | 147 @return: An RSA private key. |
197 | 148 |
198 @raise SyntaxError: If the key is not properly formatted. | 149 @raise SyntaxError: If the key is not properly formatted. |
199 """ | 150 """ |
200 try: | 151 return parsePEMKey(s, private=True) |
201 return parsePEMKey(s, private=True) | |
202 except: | |
203 return parseXMLKey(s, private=True) | |
204 | 152 |
205 def _createPublicKey(key): | 153 def _createPublicKey(key): |
206 """ | 154 """ |
207 Create a new public key. Discard any private component, | 155 Create a new public key. Discard any private component, |
208 and return the most efficient key possible. | 156 and return the most efficient key possible. |
209 """ | 157 """ |
210 if not isinstance(key, RSAKey): | 158 if not isinstance(key, RSAKey): |
211 raise AssertionError() | 159 raise AssertionError() |
212 return _createPublicRSAKey(key.n, key.e) | 160 return _createPublicRSAKey(key.n, key.e) |
213 | 161 |
(...skipping 20 matching lines...) Expand all Loading... |
234 raise ValueError("No acceptable implementations") | 182 raise ValueError("No acceptable implementations") |
235 | 183 |
236 def _createPrivateRSAKey(n, e, d, p, q, dP, dQ, qInv, | 184 def _createPrivateRSAKey(n, e, d, p, q, dP, dQ, qInv, |
237 implementations = ["pycrypto", "python"]): | 185 implementations = ["pycrypto", "python"]): |
238 for implementation in implementations: | 186 for implementation in implementations: |
239 if implementation == "pycrypto" and cryptomath.pycryptoLoaded: | 187 if implementation == "pycrypto" and cryptomath.pycryptoLoaded: |
240 return PyCrypto_RSAKey(n, e, d, p, q, dP, dQ, qInv) | 188 return PyCrypto_RSAKey(n, e, d, p, q, dP, dQ, qInv) |
241 elif implementation == "python": | 189 elif implementation == "python": |
242 return Python_RSAKey(n, e, d, p, q, dP, dQ, qInv) | 190 return Python_RSAKey(n, e, d, p, q, dP, dQ, qInv) |
243 raise ValueError("No acceptable implementations") | 191 raise ValueError("No acceptable implementations") |
OLD | NEW |