David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 1 | """ |
| 2 | RSA Key management |
| 3 | """ |
| 4 | |
David Brown | 79c4fcf | 2021-01-26 15:04:05 -0700 | [diff] [blame] | 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 7 | from cryptography.hazmat.backends import default_backend |
| 8 | from cryptography.hazmat.primitives import serialization |
| 9 | from cryptography.hazmat.primitives.asymmetric import rsa |
| 10 | from cryptography.hazmat.primitives.asymmetric.padding import PSS, MGF1 |
| 11 | from cryptography.hazmat.primitives.hashes import SHA256 |
| 12 | |
| 13 | from .general import KeyClass |
| 14 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 15 | |
| 16 | # Sizes that bootutil will recognize |
| 17 | RSA_KEY_SIZES = [2048, 3072] |
| 18 | |
| 19 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 20 | class RSAUsageError(Exception): |
| 21 | pass |
| 22 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 23 | |
| 24 | class RSAPublic(KeyClass): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 25 | """The public key can only do a few operations""" |
| 26 | def __init__(self, key): |
| 27 | self.key = key |
| 28 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 29 | def key_size(self): |
| 30 | return self.key.key_size |
| 31 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 32 | def shortname(self): |
| 33 | return "rsa" |
| 34 | |
| 35 | def _unsupported(self, name): |
| 36 | raise RSAUsageError("Operation {} requires private key".format(name)) |
| 37 | |
| 38 | def _get_public(self): |
| 39 | return self.key |
| 40 | |
| 41 | def get_public_bytes(self): |
| 42 | # The key embedded into MCUboot is in PKCS1 format. |
| 43 | return self._get_public().public_bytes( |
| 44 | encoding=serialization.Encoding.DER, |
| 45 | format=serialization.PublicFormat.PKCS1) |
| 46 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 47 | def get_private_bytes(self, minimal): |
| 48 | self._unsupported('get_private_bytes') |
| 49 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 50 | def export_private(self, path, passwd=None): |
| 51 | self._unsupported('export_private') |
| 52 | |
| 53 | def export_public(self, path): |
| 54 | """Write the public key to the given file.""" |
| 55 | pem = self._get_public().public_bytes( |
| 56 | encoding=serialization.Encoding.PEM, |
| 57 | format=serialization.PublicFormat.SubjectPublicKeyInfo) |
| 58 | with open(path, 'wb') as f: |
| 59 | f.write(pem) |
| 60 | |
| 61 | def sig_type(self): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 62 | return "PKCS1_PSS_RSA{}_SHA256".format(self.key_size()) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 63 | |
| 64 | def sig_tlv(self): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 65 | return"RSA{}".format(self.key_size()) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 66 | |
David Brown | 1d5bea1 | 2017-11-16 15:11:10 -0700 | [diff] [blame] | 67 | def sig_len(self): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 68 | return self.key_size() / 8 |
David Brown | 1d5bea1 | 2017-11-16 15:11:10 -0700 | [diff] [blame] | 69 | |
Fabio Utzig | 4a5477a | 2019-05-27 15:45:08 -0300 | [diff] [blame] | 70 | def verify(self, signature, payload): |
| 71 | k = self.key |
| 72 | if isinstance(self.key, rsa.RSAPrivateKey): |
| 73 | k = self.key.public_key() |
| 74 | return k.verify(signature=signature, data=payload, |
| 75 | padding=PSS(mgf=MGF1(SHA256()), salt_length=32), |
| 76 | algorithm=SHA256()) |
| 77 | |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 78 | |
| 79 | class RSA(RSAPublic): |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 80 | """ |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 81 | Wrapper around an RSA key, with imgtool support. |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 82 | """ |
| 83 | |
| 84 | def __init__(self, key): |
| 85 | """The key should be a private key from cryptography""" |
| 86 | self.key = key |
| 87 | |
| 88 | @staticmethod |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 89 | def generate(key_size=2048): |
| 90 | if key_size not in RSA_KEY_SIZES: |
| 91 | raise RSAUsageError("Key size {} is not supported by MCUboot" |
| 92 | .format(key_size)) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 93 | pk = rsa.generate_private_key( |
| 94 | public_exponent=65537, |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 95 | key_size=key_size, |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 96 | backend=default_backend()) |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 97 | return RSA(pk) |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 98 | |
| 99 | def _get_public(self): |
| 100 | return self.key.public_key() |
| 101 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 102 | def _build_minimal_rsa_privkey(self, der): |
| 103 | ''' |
| 104 | Builds a new DER that only includes N/E/D/P/Q RSA parameters; |
| 105 | standard DER private bytes provided by OpenSSL also includes |
| 106 | CRT params (DP/DQ/QP) which can be removed. |
| 107 | ''' |
| 108 | OFFSET_N = 7 # N is always located at this offset |
| 109 | b = bytearray(der) |
| 110 | off = OFFSET_N |
| 111 | if b[off + 1] != 0x82: |
| 112 | raise RSAUsageError("Error parsing N while minimizing") |
| 113 | len_N = (b[off + 2] << 8) + b[off + 3] + 4 |
| 114 | off += len_N |
| 115 | if b[off + 1] != 0x03: |
| 116 | raise RSAUsageError("Error parsing E while minimizing") |
| 117 | len_E = b[off + 2] + 4 |
| 118 | off += len_E |
| 119 | if b[off + 1] != 0x82: |
| 120 | raise RSAUsageError("Error parsing D while minimizing") |
| 121 | len_D = (b[off + 2] << 8) + b[off + 3] + 4 |
| 122 | off += len_D |
| 123 | if b[off + 1] != 0x81: |
| 124 | raise RSAUsageError("Error parsing P while minimizing") |
| 125 | len_P = b[off + 2] + 3 |
| 126 | off += len_P |
| 127 | if b[off + 1] != 0x81: |
| 128 | raise RSAUsageError("Error parsing Q while minimizing") |
| 129 | len_Q = b[off + 2] + 3 |
| 130 | off += len_Q |
| 131 | # adjust DER size for removed elements |
| 132 | b[2] = (off - 4) >> 8 |
| 133 | b[3] = (off - 4) & 0xff |
| 134 | return b[:off] |
| 135 | |
| 136 | def get_private_bytes(self, minimal): |
| 137 | priv = self.key.private_bytes( |
| 138 | encoding=serialization.Encoding.DER, |
| 139 | format=serialization.PrivateFormat.TraditionalOpenSSL, |
| 140 | encryption_algorithm=serialization.NoEncryption()) |
| 141 | if minimal: |
| 142 | priv = self._build_minimal_rsa_privkey(priv) |
| 143 | return priv |
| 144 | |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 145 | def export_private(self, path, passwd=None): |
Fabio Utzig | 19fd79a | 2019-05-08 18:20:39 -0300 | [diff] [blame] | 146 | """Write the private key to the given file, protecting it with the |
| 147 | optional password.""" |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 148 | if passwd is None: |
| 149 | enc = serialization.NoEncryption() |
| 150 | else: |
| 151 | enc = serialization.BestAvailableEncryption(passwd) |
| 152 | pem = self.key.private_bytes( |
| 153 | encoding=serialization.Encoding.PEM, |
| 154 | format=serialization.PrivateFormat.PKCS8, |
| 155 | encryption_algorithm=enc) |
| 156 | with open(path, 'wb') as f: |
| 157 | f.write(pem) |
| 158 | |
| 159 | def sign(self, payload): |
David Brown | 20462a7 | 2017-11-21 14:28:51 -0700 | [diff] [blame] | 160 | # The verification code only allows the salt length to be the |
| 161 | # same as the hash length, 32. |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 162 | return self.key.sign( |
| 163 | data=payload, |
David Brown | 20462a7 | 2017-11-21 14:28:51 -0700 | [diff] [blame] | 164 | padding=PSS(mgf=MGF1(SHA256()), salt_length=32), |
David Brown | 5e7c6dd | 2017-11-16 14:47:16 -0700 | [diff] [blame] | 165 | algorithm=SHA256()) |