1 // Copyright 2021 Google LLC
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 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 use super::*;
18 use crate::{iana, util::expect_err, CborSerializable, HeaderBuilder};
19 use alloc::vec;
20 
21 #[test]
test_context_encode()22 fn test_context_encode() {
23     let tests = vec![
24         (
25             CoseKdfContext::default(),
26             concat!(
27                 "84", // 4-tuple
28                 "00", // int : reserved
29                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
30                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
31                 "82", "0040", // 2-tuple: [0, 0-bstr]
32             ),
33         ),
34         (
35             CoseKdfContextBuilder::new()
36                 .algorithm(iana::Algorithm::A128GCM)
37                 .build(),
38             concat!(
39                 "84", // 4-tuple
40                 "01", // int : AES-128-GCM
41                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
42                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
43                 "82", "0040", // 2-tuple: [0, 0-bstr]
44             ),
45         ),
46         (
47             CoseKdfContextBuilder::new()
48                 .algorithm(iana::Algorithm::A128GCM)
49                 .party_u_info(PartyInfoBuilder::new().identity(vec![]).build())
50                 .build(),
51             concat!(
52                 "84", // 4-tuple
53                 "01", // int : AES-128-GCM
54                 "83", "40f6f6", // 3-tuple: [0-bstr, nil, nil]
55                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
56                 "82", "0040", // 2-tuple: [0, 0-bstr]
57             ),
58         ),
59         (
60             CoseKdfContextBuilder::new()
61                 .algorithm(iana::Algorithm::A128GCM)
62                 .party_u_info(
63                     PartyInfoBuilder::new()
64                         .identity(vec![3, 6])
65                         .nonce(Nonce::Integer(7))
66                         .build(),
67                 )
68                 .build(),
69             concat!(
70                 "84", // 4-tuple
71                 "01", // int : AES-128-GCM
72                 "83", "420306", "07f6", // 3-tuple: [2-bstr, int, nil]
73                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
74                 "82", "0040", // 2-tuple: [0, 0-bstr]
75             ),
76         ),
77         (
78             CoseKdfContextBuilder::new()
79                 .algorithm(iana::Algorithm::A128GCM)
80                 .party_u_info(
81                     PartyInfoBuilder::new()
82                         .identity(vec![3, 6])
83                         .nonce(Nonce::Integer(-2))
84                         .build(),
85                 )
86                 .build(),
87             concat!(
88                 "84", // 4-tuple
89                 "01", // int : AES-128-GCM
90                 "83", "420306", "21f6", // 3-tuple: [2-bstr, nint, nil]
91                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
92                 "82", "0040", // 2-tuple: [0, 0-bstr]
93             ),
94         ),
95         (
96             CoseKdfContextBuilder::new()
97                 .algorithm(iana::Algorithm::A128GCM)
98                 .party_v_info(
99                     PartyInfoBuilder::new()
100                         .identity(vec![3, 6])
101                         .nonce(Nonce::Bytes(vec![7, 3]))
102                         .build(),
103                 )
104                 .build(),
105             concat!(
106                 "84", // 4-tuple
107                 "01", // int : AES-128-GCM
108                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
109                 "83", "420306", "420703", "f6", // 3-tuple: [2-bstr, 2-bstr, nil]
110                 "82", "0040", // 2-tuple: [0, 0-bstr]
111             ),
112         ),
113         (
114             CoseKdfContextBuilder::new()
115                 .algorithm(iana::Algorithm::A128GCM)
116                 .party_v_info(
117                     PartyInfoBuilder::new()
118                         .identity(vec![3, 6])
119                         .other(vec![7, 3])
120                         .build(),
121                 )
122                 .build(),
123             concat!(
124                 "84", // 4-tuple
125                 "01", // int : AES-128-GCM
126                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
127                 "83", "420306", "f6", "420703", // 3-tuple: [2-bstr, nil, 2-bstr]
128                 "82", "0040", // 2-tuple: [0, 0-bstr]
129             ),
130         ),
131         (
132             CoseKdfContextBuilder::new()
133                 .supp_pub_info(
134                     SuppPubInfoBuilder::new()
135                         .key_data_length(10)
136                         .protected(
137                             HeaderBuilder::new()
138                                 .algorithm(iana::Algorithm::A128GCM)
139                                 .build(),
140                         )
141                         .build(),
142                 )
143                 .build(),
144             concat!(
145                 "84", // 4-tuple
146                 "00", // int : reserved
147                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
148                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
149                 "82", "0a43", "a10101" // 2-tuple: [10, 3-bstr]
150             ),
151         ),
152         (
153             CoseKdfContextBuilder::new()
154                 .supp_pub_info(
155                     SuppPubInfoBuilder::new()
156                         .key_data_length(10)
157                         .other(vec![1, 3, 5])
158                         .build(),
159                 )
160                 .build(),
161             concat!(
162                 "84", // 4-tuple
163                 "00", // int : reserved
164                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
165                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
166                 "83", "0a40", "43010305", // 3-tuple: [10, 0-bstr, 3-bstr]
167             ),
168         ),
169         (
170             CoseKdfContextBuilder::new()
171                 .add_supp_priv_info(vec![1, 2, 3])
172                 .build(),
173             concat!(
174                 "85", // 5-tuple
175                 "00", // int : reserved
176                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
177                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
178                 "82", "0040", // 2-tuple: [0, 0-bstr]
179                 "43", "010203", // 3-bstr
180             ),
181         ),
182         (
183             CoseKdfContextBuilder::new()
184                 .add_supp_priv_info(vec![1, 2, 3])
185                 .add_supp_priv_info(vec![2, 3])
186                 .add_supp_priv_info(vec![3])
187                 .build(),
188             concat!(
189                 "87", // 7-tuple
190                 "00", // int : reserved
191                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
192                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
193                 "82", "0040", // 2-tuple: [0, 0-bstr]
194                 "43", "010203", // 3-bstr
195                 "42", "0203", // 2-bstr
196                 "41", "03", // 1-bstr
197             ),
198         ),
199     ];
200     for (i, (key, key_data)) in tests.iter().enumerate() {
201         let got = key.clone().to_vec().unwrap();
202         assert_eq!(*key_data, hex::encode(&got), "case {}", i);
203 
204         let mut got = CoseKdfContext::from_slice(&got).unwrap();
205         got.supp_pub_info.protected.original_data = None;
206         assert_eq!(*key, got);
207     }
208 }
209 
210 #[test]
test_context_decode_fail()211 fn test_context_decode_fail() {
212     let tests = vec![
213         (
214             concat!(
215                 "a2", // 2-map
216                 "00", // int : reserved
217                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
218                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
219                 "82", "0040", // 2-tuple: [0, 0-bstr]
220             ),
221             "expected array",
222         ),
223         (
224             concat!(
225                 "83", // 3-tuple
226                 "00", // int : reserved
227                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
228                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
229             ),
230             "expected array with at least 4 items",
231         ),
232         (
233             concat!(
234                 "84", // 4-tuple
235                 "00", // int : reserved
236                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
237                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
238             ),
239             "decode CBOR failure: Io(EndOfFile",
240         ),
241         (
242             concat!(
243                 "84", // 4-tuple
244                 "08", // int : unassigned value
245                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
246                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
247                 "82", "0040", // 2-tuple: [0, 0-bstr]
248             ),
249             "expected value in IANA or private use range",
250         ),
251         (
252             concat!(
253                 "84", // 4-tuple
254                 "40", // 0-bstr : invalid
255                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
256                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
257                 "82", "0040", // 2-tuple: [0, 0-bstr]
258             ),
259             "expected int/tstr",
260         ),
261         (
262             concat!(
263                 "84", // 4-tuple
264                 "00", // int : reserved
265                 "a1", "f6f6", // 1-map
266                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
267                 "82", "0040", // 2-tuple: [0, 0-bstr]
268             ),
269             "expected array",
270         ),
271         (
272             concat!(
273                 "84", // 4-tuple
274                 "00", // int : reserved
275                 "84", "f6f6f6f6", // 4-tuple: [nil, nil, nil, nil]
276                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
277                 "82", "0040", // 2-tuple: [0, 0-bstr]
278             ),
279             "expected array with 3 items",
280         ),
281         (
282             concat!(
283                 "84", // 4-tuple
284                 "00", // int : reserved
285                 "83", "f660f6", // 3-tuple: [nil, 0-tstr, nil]
286                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
287                 "82", "0040", // 2-tuple: [0, 0-bstr]
288             ),
289             "expected bstr / int / nil",
290         ),
291         (
292             concat!(
293                 "84", // 4-tuple
294                 "00", // int : reserved
295                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
296                 "83", "f6f660", // 3-tuple: [nil, nil, 0-tstr]
297                 "82", "0040", // 2-tuple: [0, 0-bstr]
298             ),
299             "expected bstr / nil",
300         ),
301         (
302             concat!(
303                 "84", // 4-tuple
304                 "00", // int : reserved
305                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
306                 "83", "60f6f6", // 3-tuple: [0-tstr, nil, nil]
307                 "82", "0040", // 2-tuple: [0, 0-bstr]
308             ),
309             "expected bstr / nil",
310         ),
311         (
312             concat!(
313                 "84", // 4-tuple
314                 "00", // int : reserved
315                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
316                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
317                 "a1", "0040", // 1-map: {0: 0-bstr}
318             ),
319             "expected array",
320         ),
321         (
322             concat!(
323                 "84", // 4-tuple
324                 "00", // int : reserved
325                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
326                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
327                 "81", "00", // 2-tuple: [0]
328             ),
329             "expected array with 2 or 3 items",
330         ),
331         (
332             concat!(
333                 "84", // 4-tuple
334                 "00", // int : reserved
335                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
336                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
337                 "82", "4040", // 2-tuple: [0-bstr, 0-bstr]
338             ),
339             "expected int",
340         ),
341         (
342             concat!(
343                 "84", // 4-tuple
344                 "00", // int : reserved
345                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
346                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
347                 "82", "0060", // 2-tuple: [0, 0-tstr]
348             ),
349             "expected bstr",
350         ),
351         (
352             concat!(
353                 "84", // 4-tuple
354                 "00", // int : reserved
355                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
356                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
357                 "83", "004060", // 3-tuple: [0, 0-bstr, 0-tstr]
358             ),
359             "expected bstr",
360         ),
361         (
362             concat!(
363                 "85", // 5-tuple
364                 "00", // int : reserved
365                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
366                 "83", "f6f6f6", // 3-tuple: [nil, nil, nil]
367                 "82", "0040", // 2-tuple: [0, 0-bstr]
368                 "60",   // 0-tstr
369             ),
370             "expected bstr",
371         ),
372         (
373             concat!(
374                 "84", // 4-tuple
375                 "01", // int : AES-128-GCM
376                 "83", // 3-tuple: [0-bstr, out-of-range int, nil]
377                 "401b8000000000000000f6",
378                 "83", // 3-tuple: [nil, nil, nil]
379                 "f6f6f6",
380                 "82", // 2-tuple: [0, 0-bstr]
381                 "0040",
382             ),
383             "out of range integer value",
384         ),
385     ];
386     for (context_data, err_msg) in tests.iter() {
387         let data = hex::decode(context_data).unwrap();
388         let result = CoseKdfContext::from_slice(&data);
389         expect_err(result, err_msg);
390     }
391 }
392