|
4 | 4 | from typing import Any, Dict, Optional, Tuple, Type
|
5 | 5 |
|
6 | 6 | import securesystemslib.keys as sslib_keys
|
7 |
| -from securesystemslib import KEY_TYPE_ECDSA, exceptions |
| 7 | +from securesystemslib import exceptions |
8 | 8 | from securesystemslib.signer._signature import Signature
|
9 | 9 |
|
10 |
| -# pylint: disable=wrong-import-position |
11 |
| -CRYPTO_IMPORT_ERROR = None |
12 |
| -try: |
13 |
| - from cryptography.hazmat.primitives import serialization |
14 |
| - from cryptography.hazmat.primitives.asymmetric.ec import ( |
15 |
| - SECP256R1, |
16 |
| - SECP384R1, |
17 |
| - EllipticCurvePublicKey, |
18 |
| - ObjectIdentifier, |
19 |
| - get_curve_for_oid, |
20 |
| - ) |
21 |
| - |
22 |
| -except ImportError: # pragma: no cover |
23 |
| - CRYPTO_IMPORT_ERROR = "'cryptography' required" |
24 |
| - |
25 |
| -PYKCS11_IMPORT_ERROR = None |
26 |
| -try: |
27 |
| - from PyKCS11 import PyKCS11 |
28 |
| - |
29 |
| -except ImportError: # pragma: no cover |
30 |
| - PYKCS11_IMPORT_ERROR = "'PyKCS11' required" |
31 |
| - |
32 |
| -ASN1CRYPTO_IMPORT_ERROR = None |
33 |
| -try: |
34 |
| - from asn1crypto.keys import ECDomainParameters, ECPoint |
35 |
| - |
36 |
| -except ImportError: # pragma: no cover |
37 |
| - ASN1CRYPTO_IMPORT_ERROR = "'asn1crypto' required" |
38 |
| -# pylint: enable=wrong-import-position |
39 |
| - |
40 | 10 | logger = logging.getLogger(__name__)
|
41 | 11 |
|
42 | 12 | # NOTE dict for Key dispatch defined here, but filled at end of file when
|
@@ -213,94 +183,6 @@ def verify_signature(self, signature: Signature, data: bytes) -> None:
|
213 | 183 | ) from e
|
214 | 184 |
|
215 | 185 |
|
216 |
| -class HSMKey(SSlibKey): |
217 |
| - """Hardware Security Module (HSM) Key |
218 |
| -
|
219 |
| - HSMKey is a regular SSlibKey with an additional `from_hsm` method to |
220 |
| - export public keys from hardware security modules. |
221 |
| - """ |
222 |
| - |
223 |
| - @classmethod |
224 |
| - def from_hsm( |
225 |
| - cls, |
226 |
| - hsm_session: "PyKCS11.Session", |
227 |
| - hsm_keyid: Tuple[int, ...], |
228 |
| - keyid: str, |
229 |
| - ): |
230 |
| - """Export public key from HSM |
231 |
| -
|
232 |
| - Supports ecdsa on SECG curves secp256r1 (NIST P-256) or secp384r1 (NIST P-384). |
233 |
| -
|
234 |
| - Arguments: |
235 |
| - hsm_session: An open ``PyKCS11.Session`` to the token with the public key. |
236 |
| - hsm_keyid: Key identifier on the token. |
237 |
| - keyid: Key identifier that is unique within the metadata it is used in. |
238 |
| -
|
239 |
| - Raises: |
240 |
| - ValueError: No compatible key for ``hsm_keyid`` found on HSM. |
241 |
| - PyKCS11.PyKCS11Error: Various HSM communication errors. |
242 |
| -
|
243 |
| - """ |
244 |
| - if CRYPTO_IMPORT_ERROR: |
245 |
| - raise exceptions.UnsupportedLibraryError(CRYPTO_IMPORT_ERROR) |
246 |
| - |
247 |
| - if PYKCS11_IMPORT_ERROR: |
248 |
| - raise exceptions.UnsupportedLibraryError(PYKCS11_IMPORT_ERROR) |
249 |
| - |
250 |
| - # Search for ecdsa public keys with passed keyid on HSM |
251 |
| - keys = hsm_session.findObjects( |
252 |
| - [ |
253 |
| - (PyKCS11.CKA_CLASS, PyKCS11.CKO_PUBLIC_KEY), |
254 |
| - (PyKCS11.CKA_KEY_TYPE, PyKCS11.CKK_ECDSA), |
255 |
| - (PyKCS11.CKA_ID, hsm_keyid), |
256 |
| - ] |
257 |
| - ) |
258 |
| - |
259 |
| - if len(keys) != 1: |
260 |
| - raise ValueError( |
261 |
| - f"hsm_keyid must identify one {KEY_TYPE_ECDSA} key, found {len(keys)}" |
262 |
| - ) |
263 |
| - |
264 |
| - # Extract public key domain parameters and point from HSM |
265 |
| - hsm_params, hsm_point = hsm_session.getAttributeValue( |
266 |
| - keys[0], [PyKCS11.CKA_EC_PARAMS, PyKCS11.CKA_EC_POINT] |
267 |
| - ) |
268 |
| - |
269 |
| - params = ECDomainParameters.load(bytes(hsm_params)) |
270 |
| - |
271 |
| - # TODO: Define as module level constant and don't hardcode scheme strings |
272 |
| - scheme_for_curve = { |
273 |
| - SECP256R1: "ecdsa-sha2-nistp256", |
274 |
| - SECP384R1: "ecdsa-sha2-nistp384", |
275 |
| - } |
276 |
| - curve_names = [curve.name for curve in scheme_for_curve] |
277 |
| - |
278 |
| - if params.chosen.native not in curve_names: |
279 |
| - raise ValueError( |
280 |
| - f"found key on {params.chosen.native}, should be on one of {curve_names}" |
281 |
| - ) |
282 |
| - |
283 |
| - # Create PEM from key |
284 |
| - curve = get_curve_for_oid(ObjectIdentifier(params.chosen.dotted)) |
285 |
| - public_pem = ( |
286 |
| - EllipticCurvePublicKey.from_encoded_point( |
287 |
| - curve(), ECPoint().load(bytes(hsm_point)).native |
288 |
| - ) |
289 |
| - .public_bytes( |
290 |
| - serialization.Encoding.PEM, |
291 |
| - serialization.PublicFormat.SubjectPublicKeyInfo, |
292 |
| - ) |
293 |
| - .decode() |
294 |
| - ) |
295 |
| - |
296 |
| - return HSMKey( |
297 |
| - keyid, |
298 |
| - KEY_TYPE_ECDSA, |
299 |
| - scheme_for_curve[curve], |
300 |
| - {"public": public_pem}, |
301 |
| - ) |
302 |
| - |
303 |
| - |
304 | 186 | # Supported key types and schemes, and the Keys implementing them
|
305 | 187 | KEY_FOR_TYPE_AND_SCHEME = {
|
306 | 188 | ("ecdsa", "ecdsa-sha2-nistp256"): SSlibKey,
|
|
0 commit comments