David Brown | 1314bf3 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 1 | # Copyright 2017 Linaro Limited |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 2 | # Copyright 2023 Arm Limited |
David Brown | 1314bf3 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 3 | # |
David Brown | 79c4fcf | 2021-01-26 15:04:05 -0700 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
David Brown | 1314bf3 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 18 | """ |
| 19 | Cryptographic key management for imgtool. |
| 20 | """ |
| 21 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 22 | from cryptography.hazmat.backends import default_backend |
| 23 | from cryptography.hazmat.primitives import serialization |
Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -0300 | [diff] [blame] | 24 | from cryptography.hazmat.primitives.asymmetric.rsa import ( |
| 25 | RSAPrivateKey, RSAPublicKey) |
| 26 | from cryptography.hazmat.primitives.asymmetric.ec import ( |
| 27 | EllipticCurvePrivateKey, EllipticCurvePublicKey) |
| 28 | from cryptography.hazmat.primitives.asymmetric.ed25519 import ( |
| 29 | Ed25519PrivateKey, Ed25519PublicKey) |
| 30 | from cryptography.hazmat.primitives.asymmetric.x25519 import ( |
| 31 | X25519PrivateKey, X25519PublicKey) |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 32 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 33 | from .rsa import RSA, RSAPublic, RSAUsageError, RSA_KEY_SIZES |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 34 | from .ecdsa import (ECDSA256P1, ECDSA256P1Public, |
| 35 | ECDSA384P1, ECDSA384P1Public, ECDSAUsageError) |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 36 | from .ed25519 import Ed25519, Ed25519Public, Ed25519UsageError |
Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -0300 | [diff] [blame] | 37 | from .x25519 import X25519, X25519Public, X25519UsageError |
| 38 | |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 39 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 40 | class PasswordRequired(Exception): |
| 41 | """Raised to indicate that the key is password protected, but a |
| 42 | password was not specified.""" |
| 43 | pass |
| 44 | |
Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -0300 | [diff] [blame] | 45 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 46 | def load(path, passwd=None): |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 47 | """Try loading a key from the given path. |
| 48 | Returns None if the password wasn't specified.""" |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 49 | with open(path, 'rb') as f: |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 50 | raw_pem = f.read() |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 51 | try: |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 52 | pk = serialization.load_pem_private_key( |
| 53 | raw_pem, |
| 54 | password=passwd, |
| 55 | backend=default_backend()) |
David Brown | 1d5bea1 | 2017-11-16 15:11:10 -0700 | [diff] [blame] | 56 | # Unfortunately, the crypto library raises unhelpful exceptions, |
| 57 | # so we have to look at the text. |
| 58 | except TypeError as e: |
| 59 | msg = str(e) |
| 60 | if "private key is encrypted" in msg: |
| 61 | return None |
| 62 | raise e |
David Brown | 23f91ad | 2017-05-16 11:38:17 -0600 | [diff] [blame] | 63 | except ValueError: |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 64 | # This seems to happen if the key is a public key, let's try |
| 65 | # loading it as a public key. |
| 66 | pk = serialization.load_pem_public_key( |
| 67 | raw_pem, |
| 68 | backend=default_backend()) |
| 69 | |
| 70 | if isinstance(pk, RSAPrivateKey): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 71 | if pk.key_size not in RSA_KEY_SIZES: |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 72 | raise Exception("Unsupported RSA key size: " + pk.key_size) |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 73 | return RSA(pk) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 74 | elif isinstance(pk, RSAPublicKey): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 75 | if pk.key_size not in RSA_KEY_SIZES: |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 76 | raise Exception("Unsupported RSA key size: " + pk.key_size) |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 77 | return RSAPublic(pk) |
David Brown | b6e0ae6 | 2017-11-21 15:13:04 -0700 | [diff] [blame] | 78 | elif isinstance(pk, EllipticCurvePrivateKey): |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 79 | if pk.curve.name not in ('secp256r1', 'secp384r1'): |
David Brown | b6e0ae6 | 2017-11-21 15:13:04 -0700 | [diff] [blame] | 80 | raise Exception("Unsupported EC curve: " + pk.curve.name) |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 81 | if pk.key_size not in (256, 384): |
David Brown | b6e0ae6 | 2017-11-21 15:13:04 -0700 | [diff] [blame] | 82 | raise Exception("Unsupported EC size: " + pk.key_size) |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 83 | if pk.curve.name == 'secp256r1': |
| 84 | return ECDSA256P1(pk) |
| 85 | elif pk.curve.name == 'secp384r1': |
| 86 | return ECDSA384P1(pk) |
David Brown | b6e0ae6 | 2017-11-21 15:13:04 -0700 | [diff] [blame] | 87 | elif isinstance(pk, EllipticCurvePublicKey): |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 88 | if pk.curve.name not in ('secp256r1', 'secp384r1'): |
David Brown | b6e0ae6 | 2017-11-21 15:13:04 -0700 | [diff] [blame] | 89 | raise Exception("Unsupported EC curve: " + pk.curve.name) |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 90 | if pk.key_size not in (256, 384): |
David Brown | b6e0ae6 | 2017-11-21 15:13:04 -0700 | [diff] [blame] | 91 | raise Exception("Unsupported EC size: " + pk.key_size) |
Roland Mikhel | 5704174 | 2023-02-03 14:43:13 +0100 | [diff] [blame] | 92 | if pk.curve.name == 'secp256r1': |
| 93 | return ECDSA256P1Public(pk) |
| 94 | elif pk.curve.name == 'secp384r1': |
| 95 | return ECDSA384P1Public(pk) |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 96 | elif isinstance(pk, Ed25519PrivateKey): |
| 97 | return Ed25519(pk) |
| 98 | elif isinstance(pk, Ed25519PublicKey): |
| 99 | return Ed25519Public(pk) |
Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -0300 | [diff] [blame] | 100 | elif isinstance(pk, X25519PrivateKey): |
| 101 | return X25519(pk) |
| 102 | elif isinstance(pk, X25519PublicKey): |
| 103 | return X25519Public(pk) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 104 | else: |
| 105 | raise Exception("Unknown key type: " + str(type(pk))) |