blob: 79e89fd9e30e11780295d884f6358a42798a22f2 [file] [log] [blame]
David Brown187dd882017-07-11 11:15:23 -06001//! 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 Brown7e701d82017-07-11 13:24:25 -060011use std::sync::Arc;
12use pem;
13use ring::{digest, rand, signature};
14use untrusted;
Fabio Utzig80fde2f2017-12-05 09:25:31 -020015use mcuboot_sys::c;
David Brown187dd882017-07-11 11:15:23 -060016
David Brown187dd882017-07-11 11:15:23 -060017#[repr(u8)]
18#[derive(Copy, Clone, PartialEq, Eq)]
19#[allow(dead_code)] // TODO: For now
20pub enum TlvKinds {
David Brown43cda332017-09-01 09:53:23 -060021 KEYHASH = 0x01,
David Brown27648b82017-08-31 10:40:29 -060022 SHA256 = 0x10,
23 RSA2048 = 0x20,
24 ECDSA224 = 0x21,
25 ECDSA256 = 0x22,
David Brown187dd882017-07-11 11:15:23 -060026}
27
28pub struct TlvGen {
David Brown43cda332017-09-01 09:53:23 -060029 flags: u32,
David Brown187dd882017-07-11 11:15:23 -060030 kinds: Vec<TlvKinds>,
31 size: u16,
David Brown4243ab02017-07-11 12:24:23 -060032 payload: Vec<u8>,
David Brown187dd882017-07-11 11:15:23 -060033}
34
35impl TlvGen {
36 /// Construct a new tlv generator that will only contain a hash of the data.
David Brown7e701d82017-07-11 13:24:25 -060037 #[allow(dead_code)]
David Brown187dd882017-07-11 11:15:23 -060038 pub fn new_hash_only() -> TlvGen {
39 TlvGen {
David Brown43cda332017-09-01 09:53:23 -060040 flags: 0,
David Brown187dd882017-07-11 11:15:23 -060041 kinds: vec![TlvKinds::SHA256],
42 size: 4 + 32,
David Brown4243ab02017-07-11 12:24:23 -060043 payload: vec![],
David Brown187dd882017-07-11 11:15:23 -060044 }
45 }
46
David Brown7e701d82017-07-11 13:24:25 -060047 #[allow(dead_code)]
48 pub fn new_rsa_pss() -> TlvGen {
49 TlvGen {
David Brown43cda332017-09-01 09:53:23 -060050 flags: 0,
51 kinds: vec![TlvKinds::SHA256, TlvKinds::KEYHASH, TlvKinds::RSA2048],
David Brown7e701d82017-07-11 13:24:25 -060052 size: 4 + 32 + 4 + 256,
53 payload: vec![],
54 }
55 }
56
Fabio Utzig80fde2f2017-12-05 09:25:31 -020057 #[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 Brown187dd882017-07-11 11:15:23 -060067 /// Retrieve the header flags for this configuration. This can be called at any time.
68 pub fn get_flags(&self) -> u32 {
David Brown43cda332017-09-01 09:53:23 -060069 self.flags
David Brown187dd882017-07-11 11:15:23 -060070 }
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 Brownf5b33d82017-09-01 10:58:27 -060074 4 + self.size
David Brown187dd882017-07-11 11:15:23 -060075 }
76
77 /// Add bytes to the covered hash.
78 pub fn add_bytes(&mut self, bytes: &[u8]) {
David Brown4243ab02017-07-11 12:24:23 -060079 self.payload.extend_from_slice(bytes);
David Brown187dd882017-07-11 11:15:23 -060080 }
81
Fabio Utzig80fde2f2017-12-05 09:25:31 -020082 /// 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 Brown187dd882017-07-11 11:15:23 -0600113 /// Compute the TLV given the specified block of data.
David Brown8054ce22017-07-11 12:12:09 -0600114 pub fn make_tlv(self) -> Vec<u8> {
David Brown187dd882017-07-11 11:15:23 -0600115 let mut result: Vec<u8> = vec![];
116
David Brownf5b33d82017-09-01 10:58:27 -0600117 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 Brown187dd882017-07-11 11:15:23 -0600123 if self.kinds.contains(&TlvKinds::SHA256) {
David Brown4243ab02017-07-11 12:24:23 -0600124 let hash = digest::digest(&digest::SHA256, &self.payload);
David Brown8054ce22017-07-11 12:12:09 -0600125 let hash = hash.as_ref();
David Brown187dd882017-07-11 11:15:23 -0600126
David Brown8054ce22017-07-11 12:12:09 -0600127 assert!(hash.len() == 32);
David Brown187dd882017-07-11 11:15:23 -0600128 result.push(TlvKinds::SHA256 as u8);
129 result.push(0);
130 result.push(32);
131 result.push(0);
David Brown8054ce22017-07-11 12:12:09 -0600132 result.extend_from_slice(hash);
David Brown187dd882017-07-11 11:15:23 -0600133 }
134
David Brown7e701d82017-07-11 13:24:25 -0600135 if self.kinds.contains(&TlvKinds::RSA2048) {
David Brown43cda332017-09-01 09:53:23 -0600136 // 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 Brown7e701d82017-07-11 13:24:25 -0600147 // 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 Utzig80fde2f2017-12-05 09:25:31 -0200165 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 Brown187dd882017-07-11 11:15:23 -0600215 result
216 }
217}
David Brown43cda332017-09-01 09:53:23 -0600218
219include!("rsa_pub_key-rs.txt");
Fabio Utzig80fde2f2017-12-05 09:25:31 -0200220include!("ecdsa_pub_key-rs.txt");