|
1 | 1 | import binascii |
2 | 2 |
|
3 | 3 | import six |
4 | | -from pyasn1.codec.der import decoder, encoder |
5 | 4 | from pyasn1.error import PyAsn1Error |
6 | | -from pyasn1.type import namedtype, univ |
7 | 5 |
|
8 | 6 | import rsa as pyrsa |
9 | 7 | import rsa.pem as pyrsa_pem |
10 | | -from rsa.asn1 import OpenSSLPubKey, AsnPubKey, PubKeyHeader |
11 | 8 |
|
12 | 9 | from jose.backends.base import Key |
| 10 | +from jose.backends._asn1 import ( |
| 11 | + rsa_private_key_pkcs1_to_pkcs8, |
| 12 | + rsa_private_key_pkcs8_to_pkcs1, |
| 13 | + rsa_public_key_pkcs1_to_pkcs8, |
| 14 | +) |
13 | 15 | from jose.constants import ALGORITHMS |
14 | 16 | from jose.exceptions import JWKError |
15 | 17 | from jose.utils import base64_to_long, long_to_base64 |
@@ -114,48 +116,6 @@ def _legacy_private_key_pkcs8_to_pkcs1(pkcs8_key): |
114 | 116 | return pkcs8_key[len(LEGACY_INVALID_PKCS8_RSA_HEADER):] |
115 | 117 |
|
116 | 118 |
|
117 | | -class PKCS8RsaPrivateKeyAlgorithm(univ.Sequence): |
118 | | - """ASN1 structure for recording RSA PrivateKeyAlgorithm identifiers.""" |
119 | | - componentType = namedtype.NamedTypes( |
120 | | - namedtype.NamedType("rsaEncryption", univ.ObjectIdentifier()), |
121 | | - namedtype.NamedType("parameters", univ.Null()) |
122 | | - ) |
123 | | - |
124 | | - |
125 | | -class PKCS8PrivateKey(univ.Sequence): |
126 | | - """ASN1 structure for recording PKCS8 private keys.""" |
127 | | - componentType = namedtype.NamedTypes( |
128 | | - namedtype.NamedType("version", univ.Integer()), |
129 | | - namedtype.NamedType("privateKeyAlgorithm", PKCS8RsaPrivateKeyAlgorithm()), |
130 | | - namedtype.NamedType("privateKey", univ.OctetString()) |
131 | | - ) |
132 | | - |
133 | | - |
134 | | -def _private_key_pkcs8_to_pkcs1(pkcs8_key): |
135 | | - """Convert a PKCS8-encoded RSA private key to PKCS1.""" |
136 | | - decoded_values = decoder.decode(pkcs8_key, asn1Spec=PKCS8PrivateKey()) |
137 | | - |
138 | | - try: |
139 | | - decoded_key = decoded_values[0] |
140 | | - except IndexError: |
141 | | - raise ValueError("Invalid private key encoding") |
142 | | - |
143 | | - return decoded_key["privateKey"] |
144 | | - |
145 | | - |
146 | | -def _private_key_pkcs1_to_pkcs8(pkcs1_key): |
147 | | - """Convert a PKCS1-encoded RSA private key to PKCS8.""" |
148 | | - algorithm = PKCS8RsaPrivateKeyAlgorithm() |
149 | | - algorithm["rsaEncryption"] = RSA_ENCRYPTION_ASN1_OID |
150 | | - |
151 | | - pkcs8_key = PKCS8PrivateKey() |
152 | | - pkcs8_key["version"] = 0 |
153 | | - pkcs8_key["privateKeyAlgorithm"] = algorithm |
154 | | - pkcs8_key["privateKey"] = pkcs1_key |
155 | | - |
156 | | - return encoder.encode(pkcs8_key) |
157 | | - |
158 | | - |
159 | 119 | class RSAKey(Key): |
160 | 120 | SHA256 = 'SHA-256' |
161 | 121 | SHA384 = 'SHA-384' |
@@ -196,7 +156,7 @@ def __init__(self, key, algorithm): |
196 | 156 | try: |
197 | 157 | der = pyrsa_pem.load_pem(key, b'PRIVATE KEY') |
198 | 158 | try: |
199 | | - pkcs1_key = _private_key_pkcs8_to_pkcs1(der) |
| 159 | + pkcs1_key = rsa_private_key_pkcs8_to_pkcs1(der) |
200 | 160 | except PyAsn1Error: |
201 | 161 | # If the key was encoded using the old, invalid, |
202 | 162 | # encoding then pyasn1 will throw an error attempting |
@@ -259,27 +219,17 @@ def to_pem(self, pem_format='PKCS8'): |
259 | 219 | if isinstance(self._prepared_key, pyrsa.PrivateKey): |
260 | 220 | der = self._prepared_key.save_pkcs1(format='DER') |
261 | 221 | if pem_format == 'PKCS8': |
262 | | - pkcs8_der = _private_key_pkcs1_to_pkcs8(der) |
| 222 | + pkcs8_der = rsa_private_key_pkcs1_to_pkcs8(der) |
263 | 223 | pem = pyrsa_pem.save_pem(pkcs8_der, pem_marker='PRIVATE KEY') |
264 | 224 | elif pem_format == 'PKCS1': |
265 | 225 | pem = pyrsa_pem.save_pem(der, pem_marker='RSA PRIVATE KEY') |
266 | 226 | else: |
267 | 227 | raise ValueError("Invalid pem format specified: %r" % (pem_format,)) |
268 | 228 | else: |
269 | 229 | if pem_format == 'PKCS8': |
270 | | - asn_key = AsnPubKey() |
271 | | - asn_key.setComponentByName('modulus', self._prepared_key.n) |
272 | | - asn_key.setComponentByName('publicExponent', self._prepared_key.e) |
273 | | - der = encoder.encode(asn_key) |
274 | | - |
275 | | - header = PubKeyHeader() |
276 | | - header['oid'] = univ.ObjectIdentifier(RSA_ENCRYPTION_ASN1_OID) |
277 | | - pub_key = OpenSSLPubKey() |
278 | | - pub_key['header'] = header |
279 | | - pub_key['key'] = univ.BitString.fromOctetString(der) |
280 | | - |
281 | | - der = encoder.encode(pub_key) |
282 | | - pem = pyrsa_pem.save_pem(der, pem_marker='PUBLIC KEY') |
| 230 | + pkcs1_der = self._prepared_key.save_pkcs1(format="DER") |
| 231 | + pkcs8_der = rsa_public_key_pkcs1_to_pkcs8(pkcs1_der) |
| 232 | + pem = pyrsa_pem.save_pem(pkcs8_der, pem_marker='PUBLIC KEY') |
283 | 233 | elif pem_format == 'PKCS1': |
284 | 234 | der = self._prepared_key.save_pkcs1(format='DER') |
285 | 235 | pem = pyrsa_pem.save_pem(der, pem_marker='RSA PUBLIC KEY') |
|
0 commit comments