David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 1 | """ |
| 2 | RSA Key management |
| 3 | """ |
| 4 | |
| 5 | from cryptography.hazmat.backends import default_backend |
| 6 | from cryptography.hazmat.primitives import serialization |
| 7 | from cryptography.hazmat.primitives.asymmetric import rsa |
| 8 | from cryptography.hazmat.primitives.asymmetric.padding import PSS, MGF1 |
| 9 | from cryptography.hazmat.primitives.hashes import SHA256 |
| 10 | |
| 11 | from .general import KeyClass |
| 12 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 13 | |
| 14 | # Sizes that bootutil will recognize |
| 15 | RSA_KEY_SIZES = [2048, 3072] |
| 16 | |
| 17 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 18 | class RSAUsageError(Exception): |
| 19 | pass |
| 20 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 21 | |
| 22 | class RSAPublic(KeyClass): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 23 | """The public key can only do a few operations""" |
| 24 | def __init__(self, key): |
| 25 | self.key = key |
| 26 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 27 | def key_size(self): |
| 28 | return self.key.key_size |
| 29 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 30 | def shortname(self): |
| 31 | return "rsa" |
| 32 | |
| 33 | def _unsupported(self, name): |
| 34 | raise RSAUsageError("Operation {} requires private key".format(name)) |
| 35 | |
| 36 | def _get_public(self): |
| 37 | return self.key |
| 38 | |
| 39 | def get_public_bytes(self): |
| 40 | # The key embedded into MCUboot is in PKCS1 format. |
| 41 | return self._get_public().public_bytes( |
| 42 | encoding=serialization.Encoding.DER, |
| 43 | format=serialization.PublicFormat.PKCS1) |
| 44 | |
| 45 | def export_private(self, path, passwd=None): |
| 46 | self._unsupported('export_private') |
| 47 | |
| 48 | def export_public(self, path): |
| 49 | """Write the public key to the given file.""" |
| 50 | pem = self._get_public().public_bytes( |
| 51 | encoding=serialization.Encoding.PEM, |
| 52 | format=serialization.PublicFormat.SubjectPublicKeyInfo) |
| 53 | with open(path, 'wb') as f: |
| 54 | f.write(pem) |
| 55 | |
| 56 | def sig_type(self): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 57 | return "PKCS1_PSS_RSA{}_SHA256".format(self.key_size()) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 58 | |
| 59 | def sig_tlv(self): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 60 | return"RSA{}".format(self.key_size()) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 61 | |
David Brown | 1d5bea1 | 2017-11-16 15:11:10 -0700 | [diff] [blame] | 62 | def sig_len(self): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 63 | return self.key_size() / 8 |
David Brown | 1d5bea1 | 2017-11-16 15:11:10 -0700 | [diff] [blame] | 64 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 65 | |
| 66 | class RSA(RSAPublic): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 67 | """ |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 68 | Wrapper around an RSA key, with imgtool support. |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 69 | """ |
| 70 | |
| 71 | def __init__(self, key): |
| 72 | """The key should be a private key from cryptography""" |
| 73 | self.key = key |
| 74 | |
| 75 | @staticmethod |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 76 | def generate(key_size=2048): |
| 77 | if key_size not in RSA_KEY_SIZES: |
| 78 | raise RSAUsageError("Key size {} is not supported by MCUboot" |
| 79 | .format(key_size)) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 80 | pk = rsa.generate_private_key( |
| 81 | public_exponent=65537, |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 82 | key_size=key_size, |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 83 | backend=default_backend()) |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 84 | return RSA(pk) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 85 | |
| 86 | def _get_public(self): |
| 87 | return self.key.public_key() |
| 88 | |
| 89 | def export_private(self, path, passwd=None): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame^] | 90 | """Write the private key to the given file, protecting it with the |
| 91 | optional password.""" |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 92 | if passwd is None: |
| 93 | enc = serialization.NoEncryption() |
| 94 | else: |
| 95 | enc = serialization.BestAvailableEncryption(passwd) |
| 96 | pem = self.key.private_bytes( |
| 97 | encoding=serialization.Encoding.PEM, |
| 98 | format=serialization.PrivateFormat.PKCS8, |
| 99 | encryption_algorithm=enc) |
| 100 | with open(path, 'wb') as f: |
| 101 | f.write(pem) |
| 102 | |
| 103 | def sign(self, payload): |
David Brown | 20462a7 | 2017-11-21 14:28:51 -0700 | [diff] [blame] | 104 | # The verification code only allows the salt length to be the |
| 105 | # same as the hash length, 32. |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 106 | return self.key.sign( |
| 107 | data=payload, |
David Brown | 20462a7 | 2017-11-21 14:28:51 -0700 | [diff] [blame] | 108 | padding=PSS(mgf=MGF1(SHA256()), salt_length=32), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 109 | algorithm=SHA256()) |