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