David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 1 | //! TLV Support |
| 2 | //! |
| 3 | //! mcuboot images are followed immediately by a list of TLV items that contain integrity |
| 4 | //! information about the image. Their generation is made a little complicated because the size of |
| 5 | //! the TLV block is in the image header, which is included in the hash. Since some signatures can |
| 6 | //! vary in size, we just make them the largest size possible. |
| 7 | //! |
| 8 | //! Because of this header, we have to make two passes. The first pass will compute the size of |
| 9 | //! the TLV, and the second pass will build the data for the TLV. |
| 10 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 11 | use std::sync::Arc; |
| 12 | use pem; |
| 13 | use ring::{digest, rand, signature}; |
| 14 | use untrusted; |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 15 | use mcuboot_sys::c; |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 16 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 17 | #[repr(u8)] |
| 18 | #[derive(Copy, Clone, PartialEq, Eq)] |
| 19 | #[allow(dead_code)] // TODO: For now |
| 20 | pub enum TlvKinds { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 21 | KEYHASH = 0x01, |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 22 | SHA256 = 0x10, |
| 23 | RSA2048 = 0x20, |
| 24 | ECDSA224 = 0x21, |
| 25 | ECDSA256 = 0x22, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | pub struct TlvGen { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 29 | flags: u32, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 30 | kinds: Vec<TlvKinds>, |
| 31 | size: u16, |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 32 | payload: Vec<u8>, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | impl TlvGen { |
| 36 | /// Construct a new tlv generator that will only contain a hash of the data. |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 37 | #[allow(dead_code)] |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 38 | pub fn new_hash_only() -> TlvGen { |
| 39 | TlvGen { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 40 | flags: 0, |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 41 | kinds: vec![TlvKinds::SHA256], |
| 42 | size: 4 + 32, |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 43 | payload: vec![], |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 47 | #[allow(dead_code)] |
| 48 | pub fn new_rsa_pss() -> TlvGen { |
| 49 | TlvGen { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 50 | flags: 0, |
| 51 | kinds: vec![TlvKinds::SHA256, TlvKinds::KEYHASH, TlvKinds::RSA2048], |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 52 | size: 4 + 32 + 4 + 256, |
| 53 | payload: vec![], |
| 54 | } |
| 55 | } |
| 56 | |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 57 | #[allow(dead_code)] |
| 58 | pub fn new_ecdsa() -> TlvGen { |
| 59 | TlvGen { |
| 60 | flags: 0, |
| 61 | kinds: vec![TlvKinds::SHA256, TlvKinds::KEYHASH, TlvKinds::ECDSA256], |
| 62 | size: 4 + 32 + 4 + 72, |
| 63 | payload: vec![], |
| 64 | } |
| 65 | } |
| 66 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 67 | /// Retrieve the header flags for this configuration. This can be called at any time. |
| 68 | pub fn get_flags(&self) -> u32 { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 69 | self.flags |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /// Retrieve the size that the TLV will occupy. This can be called at any time. |
| 73 | pub fn get_size(&self) -> u16 { |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 74 | 4 + self.size |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /// Add bytes to the covered hash. |
| 78 | pub fn add_bytes(&mut self, bytes: &[u8]) { |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 79 | self.payload.extend_from_slice(bytes); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 80 | } |
| 81 | |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 82 | /// Create a DER representation of one ec curve point |
| 83 | fn _make_der_int(&self, x: &[u8]) -> Vec<u8> { |
| 84 | assert!(x.len() == 32); |
| 85 | |
| 86 | let mut i: Vec<u8> = vec![0x02]; |
| 87 | if x[0] >= 0x7f { |
| 88 | i.push(33); |
| 89 | i.push(0); |
| 90 | } else { |
| 91 | i.push(32); |
| 92 | } |
| 93 | i.extend(x); |
| 94 | i |
| 95 | } |
| 96 | |
| 97 | /// Create an ecdsa256 TLV |
| 98 | fn _make_der_sequence(&self, r: Vec<u8>, s: Vec<u8>) -> Vec<u8> { |
| 99 | let mut der: Vec<u8> = vec![0x30]; |
| 100 | der.push(r.len() as u8 + s.len() as u8); |
| 101 | der.extend(r); |
| 102 | der.extend(s); |
| 103 | let mut size = der.len(); |
| 104 | // must pad up to 72 bytes... |
| 105 | while size <= 72 { |
| 106 | der.push(0); |
| 107 | der[1] += 1; |
| 108 | size += 1; |
| 109 | } |
| 110 | der |
| 111 | } |
| 112 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 113 | /// Compute the TLV given the specified block of data. |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 114 | pub fn make_tlv(self) -> Vec<u8> { |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 115 | let mut result: Vec<u8> = vec![]; |
| 116 | |
David Brown | f5b33d8 | 2017-09-01 10:58:27 -0600 | [diff] [blame] | 117 | let size = self.get_size(); |
| 118 | result.push(0x07); |
| 119 | result.push(0x69); |
| 120 | result.push((size & 0xFF) as u8); |
| 121 | result.push(((size >> 8) & 0xFF) as u8); |
| 122 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 123 | if self.kinds.contains(&TlvKinds::SHA256) { |
David Brown | 4243ab0 | 2017-07-11 12:24:23 -0600 | [diff] [blame] | 124 | let hash = digest::digest(&digest::SHA256, &self.payload); |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 125 | let hash = hash.as_ref(); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 126 | |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 127 | assert!(hash.len() == 32); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 128 | result.push(TlvKinds::SHA256 as u8); |
| 129 | result.push(0); |
| 130 | result.push(32); |
| 131 | result.push(0); |
David Brown | 8054ce2 | 2017-07-11 12:12:09 -0600 | [diff] [blame] | 132 | result.extend_from_slice(hash); |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 133 | } |
| 134 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 135 | if self.kinds.contains(&TlvKinds::RSA2048) { |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 136 | // Output the hash of the public key. |
| 137 | let hash = digest::digest(&digest::SHA256, RSA_PUB_KEY); |
| 138 | let hash = hash.as_ref(); |
| 139 | |
| 140 | assert!(hash.len() == 32); |
| 141 | result.push(TlvKinds::KEYHASH as u8); |
| 142 | result.push(0); |
| 143 | result.push(32); |
| 144 | result.push(0); |
| 145 | result.extend_from_slice(hash); |
| 146 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 147 | // For now assume PSS. |
| 148 | let key_bytes = pem::parse(include_bytes!("../../root-rsa-2048.pem").as_ref()).unwrap(); |
| 149 | assert_eq!(key_bytes.tag, "RSA PRIVATE KEY"); |
| 150 | let key_bytes = untrusted::Input::from(&key_bytes.contents); |
| 151 | let key = signature::RSAKeyPair::from_der(key_bytes).unwrap(); |
| 152 | let mut signer = signature::RSASigningState::new(Arc::new(key)).unwrap(); |
| 153 | let rng = rand::SystemRandom::new(); |
| 154 | let mut signature = vec![0; signer.key_pair().public_modulus_len()]; |
| 155 | assert_eq!(signature.len(), 256); |
| 156 | signer.sign(&signature::RSA_PSS_SHA256, &rng, &self.payload, &mut signature).unwrap(); |
| 157 | |
| 158 | result.push(TlvKinds::RSA2048 as u8); |
| 159 | result.push(0); |
| 160 | result.push((signature.len() & 0xFF) as u8); |
| 161 | result.push(((signature.len() >> 8) & 0xFF) as u8); |
| 162 | result.extend_from_slice(&signature); |
| 163 | } |
| 164 | |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 165 | if self.kinds.contains(&TlvKinds::ECDSA256) { |
| 166 | let keyhash = digest::digest(&digest::SHA256, ECDSA256_PUB_KEY); |
| 167 | let keyhash = keyhash.as_ref(); |
| 168 | |
| 169 | assert!(keyhash.len() == 32); |
| 170 | result.push(TlvKinds::KEYHASH as u8); |
| 171 | result.push(0); |
| 172 | result.push(32); |
| 173 | result.push(0); |
| 174 | result.extend_from_slice(keyhash); |
| 175 | |
| 176 | let key_bytes = pem::parse(include_bytes!("../../root-ec-p256.pem").as_ref()).unwrap(); |
| 177 | assert_eq!(key_bytes.tag, "EC PRIVATE KEY"); |
| 178 | |
| 179 | let hash = digest::digest(&digest::SHA256, &self.payload); |
| 180 | let hash = hash.as_ref(); |
| 181 | assert!(hash.len() == 32); |
| 182 | |
| 183 | /* FIXME |
| 184 | * |
| 185 | * Although `ring` has an ASN1 parser, it hides access |
| 186 | * to its low-level data, which was designed to be used |
| 187 | * by its internal signing/verifying functions. Since it does |
| 188 | * not yet support ecdsa signing, for the time being I am |
| 189 | * manually loading the key from its index in the PEM and |
| 190 | * building the TLV DER manually. |
| 191 | * |
| 192 | * Once ring gets ecdsa signing (hopefully soon!) this code |
| 193 | * should be updated to leverage its functionality... |
| 194 | */ |
| 195 | |
| 196 | /* Load key directly from PEM */ |
| 197 | let key = &key_bytes.contents[7..39]; |
| 198 | |
| 199 | let signature = match c::ecdsa256_sign(&key, &hash) { |
| 200 | Ok(sign) => sign, |
| 201 | Err(_) => panic!("Failed signature generation"), |
| 202 | }; |
| 203 | |
| 204 | let r = self._make_der_int(&signature.to_vec()[..32]); |
| 205 | let s = self._make_der_int(&signature.to_vec()[32..64]); |
| 206 | let der = self._make_der_sequence(r, s); |
| 207 | |
| 208 | result.push(TlvKinds::ECDSA256 as u8); |
| 209 | result.push(0); |
| 210 | result.push(der.len() as u8); |
| 211 | result.push(0); |
| 212 | result.extend(der); |
| 213 | } |
| 214 | |
David Brown | 187dd88 | 2017-07-11 11:15:23 -0600 | [diff] [blame] | 215 | result |
| 216 | } |
| 217 | } |
David Brown | 43cda33 | 2017-09-01 09:53:23 -0600 | [diff] [blame] | 218 | |
| 219 | include!("rsa_pub_key-rs.txt"); |
Fabio Utzig | 80fde2f | 2017-12-05 09:25:31 -0200 | [diff] [blame] | 220 | include!("ecdsa_pub_key-rs.txt"); |