1 /*
2 * lws-api-test-jose - RFC8152 cose_sign tests
3 *
4 * Written in 2010-2021 by Andy Green <andy@warmcat.com>
5 *
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
8 *
9 * Raw key CBOR created from descriptions at
10 *
11 * https://github.com/cose-wg/Examples/blob/master/KeySet.txt
12 */
13
14 #include <libwebsockets.h>
15 #include <stdlib.h>
16 #include <fcntl.h>
17
18 static const uint8_t
19 sign1_pass_01[] = {
20 /*
21 * https://github.com/cose-wg/Examples/blob/master/
22 * sign1-tests/sign-pass-01.json
23 */
24 0xd2, 0x84, 0x41, 0xa0, 0xa2,
25 0x01, 0x26, 0x04, 0x42, 0x31,
26 0x31, 0x54, 0x54, 0x68, 0x69,
27 0x73, 0x20, 0x69, 0x73, 0x20,
28 0x74, 0x68, 0x65, 0x20, 0x63,
29 0x6f, 0x6e, 0x74, 0x65, 0x6e,
30 0x74, 0x2e, 0x58, 0x40, 0x87,
31 0xdb, 0x0d, 0x2e, 0x55, 0x71,
32 0x84, 0x3b, 0x78, 0xac, 0x33,
33 0xec, 0xb2, 0x83, 0x0d, 0xf7,
34 0xb6, 0xe0, 0xa4, 0xd5, 0xb7,
35 0x37, 0x6d, 0xe3, 0x36, 0xb2,
36 0x3c, 0x59, 0x1c, 0x90, 0xc4,
37 0x25, 0x31, 0x7e, 0x56, 0x12,
38 0x7f, 0xbe, 0x04, 0x37, 0x00,
39 0x97, 0xce, 0x34, 0x70, 0x87,
40 0xb2, 0x33, 0xbf, 0x72, 0x2b,
41 0x64, 0x07, 0x2b, 0xeb, 0x44,
42 0x86, 0xbd, 0xa4, 0x03, 0x1d,
43 0x27, 0x24, 0x4f },
44 sign1_pass_02[] = {
45 0xd2, 0x84, 0x43, 0xa1, 0x01,
46 0x26, 0xa1, 0x04, 0x42, 0x31,
47 0x31, 0x54, 0x54, 0x68, 0x69,
48 0x73, 0x20, 0x69, 0x73, 0x20,
49 0x74, 0x68, 0x65, 0x20, 0x63,
50 0x6f, 0x6e, 0x74, 0x65, 0x6e,
51 0x74, 0x2e, 0x58, 0x40, 0x10,
52 0x72, 0x9c, 0xd7, 0x11, 0xcb,
53 0x38, 0x13, 0xd8, 0xd8, 0xe9,
54 0x44, 0xa8, 0xda, 0x71, 0x11,
55 0xe7, 0xb2, 0x58, 0xc9, 0xbd,
56 0xca, 0x61, 0x35, 0xf7, 0xae,
57 0x1a, 0xdb, 0xee, 0x95, 0x09,
58 0x89, 0x12, 0x67, 0x83, 0x7e,
59 0x1e, 0x33, 0xbd, 0x36, 0xc1,
60 0x50, 0x32, 0x6a, 0xe6, 0x27,
61 0x55, 0xc6, 0xbd, 0x8e, 0x54,
62 0x0c, 0x3e, 0x8f, 0x92, 0xd7,
63 0xd2, 0x25, 0xe8, 0xdb, 0x72,
64 0xb8, 0x82, 0x0b },
65
66 sign1_pass_02_ext[] = {
67 0x11, 0xaa, 0x22, 0xbb, 0x33,
68 0xcc, 0x44, 0xdd, 0x55, 0x00,
69 0x66, 0x99 },
70
71 sign1_pass_03[] = {
72 0x84, 0x43, 0xa1, 0x01, 0x26,
73 0xa1, 0x04, 0x42, 0x31, 0x31,
74 0x54, 0x54, 0x68, 0x69, 0x73,
75 0x20, 0x69, 0x73, 0x20, 0x74,
76 0x68, 0x65, 0x20, 0x63, 0x6f,
77 0x6e, 0x74, 0x65, 0x6e, 0x74,
78 0x2e, 0x58, 0x40, 0x8e, 0xb3,
79 0x3e, 0x4c, 0xa3, 0x1d, 0x1c,
80 0x46, 0x5a, 0xb0, 0x5a, 0xac,
81 0x34, 0xcc, 0x6b, 0x23, 0xd5,
82 0x8f, 0xef, 0x5c, 0x08, 0x31,
83 0x06, 0xc4, 0xd2, 0x5a, 0x91,
84 0xae, 0xf0, 0xb0, 0x11, 0x7e,
85 0x2a, 0xf9, 0xa2, 0x91, 0xaa,
86 0x32, 0xe1, 0x4a, 0xb8, 0x34,
87 0xdc, 0x56, 0xed, 0x2a, 0x22,
88 0x34, 0x44, 0x54, 0x7e, 0x01,
89 0xf1, 0x1d, 0x3b, 0x09, 0x16,
90 0xe5, 0xa4, 0xc3, 0x45, 0xca,
91 0xcb, 0x36 },
92 sign1_fail_01[] = {
93 0xd9, 0x03, 0xe6, 0x84, 0x43,
94 0xa1, 0x01, 0x26, 0xa1, 0x04,
95 0x42, 0x31, 0x31, 0x54, 0x54,
96 0x68, 0x69, 0x73, 0x20, 0x69,
97 0x73, 0x20, 0x74, 0x68, 0x65,
98 0x20, 0x63, 0x6f, 0x6e, 0x74,
99 0x65, 0x6e, 0x74, 0x2e, 0x58,
100 0x40, 0x8e, 0xb3, 0x3e, 0x4c,
101 0xa3, 0x1d, 0x1c, 0x46, 0x5a,
102 0xb0, 0x5a, 0xac, 0x34, 0xcc,
103 0x6b, 0x23, 0xd5, 0x8f, 0xef,
104 0x5c, 0x08, 0x31, 0x06, 0xc4,
105 0xd2, 0x5a, 0x91, 0xae, 0xf0,
106 0xb0, 0x11, 0x7e, 0x2a, 0xf9,
107 0xa2, 0x91, 0xaa, 0x32, 0xe1,
108 0x4a, 0xb8, 0x34, 0xdc, 0x56,
109 0xed, 0x2a, 0x22, 0x34, 0x44,
110 0x54, 0x7e, 0x01, 0xf1, 0x1d,
111 0x3b, 0x09, 0x16, 0xe5, 0xa4,
112 0xc3, 0x45, 0xca, 0xcb, 0x36 },
113 sign1_fail_02[] = {
114 0xd2, 0x84, 0x43, 0xa1, 0x01,
115 0x26, 0xa1, 0x04, 0x42, 0x31,
116 0x31, 0x54, 0x54, 0x68, 0x69,
117 0x73, 0x20, 0x69, 0x73, 0x20,
118 0x74, 0x68, 0x65, 0x20, 0x63,
119 0x6f, 0x6e, 0x74, 0x65, 0x6e,
120 0x74, 0x2f, 0x58, 0x40, 0x8e,
121 0xb3, 0x3e, 0x4c, 0xa3, 0x1d,
122 0x1c, 0x46, 0x5a, 0xb0, 0x5a,
123 0xac, 0x34, 0xcc, 0x6b, 0x23,
124 0xd5, 0x8f, 0xef, 0x5c, 0x08,
125 0x31, 0x06, 0xc4, 0xd2, 0x5a,
126 0x91, 0xae, 0xf0, 0xb0, 0x11,
127 0x7e, 0x2a, 0xf9, 0xa2, 0x91,
128 0xaa, 0x32, 0xe1, 0x4a, 0xb8,
129 0x34, 0xdc, 0x56, 0xed, 0x2a,
130 0x22, 0x34, 0x44, 0x54, 0x7e,
131 0x01, 0xf1, 0x1d, 0x3b, 0x09,
132 0x16, 0xe5, 0xa4, 0xc3, 0x45,
133 0xca, 0xcb, 0x36 },
134 sign1_fail_03[] = {
135 0xd2, 0x84, 0x45, 0xa1, 0x01,
136 0x39, 0x03, 0xe6, 0xa1, 0x04,
137 0x42, 0x31, 0x31, 0x54, 0x54,
138 0x68, 0x69, 0x73, 0x20, 0x69,
139 0x73, 0x20, 0x74, 0x68, 0x65,
140 0x20, 0x63, 0x6f, 0x6e, 0x74,
141 0x65, 0x6e, 0x74, 0x2e, 0x58,
142 0x40, 0x8e, 0xb3, 0x3e, 0x4c,
143 0xa3, 0x1d, 0x1c, 0x46, 0x5a,
144 0xb0, 0x5a, 0xac, 0x34, 0xcc,
145 0x6b, 0x23, 0xd5, 0x8f, 0xef,
146 0x5c, 0x08, 0x31, 0x06, 0xc4,
147 0xd2, 0x5a, 0x91, 0xae, 0xf0,
148 0xb0, 0x11, 0x7e, 0x2a, 0xf9,
149 0xa2, 0x91, 0xaa, 0x32, 0xe1,
150 0x4a, 0xb8, 0x34, 0xdc, 0x56,
151 0xed, 0x2a, 0x22, 0x34, 0x44,
152 0x54, 0x7e, 0x01, 0xf1, 0x1d,
153 0x3b, 0x09, 0x16, 0xe5, 0xa4,
154 0xc3, 0x45, 0xca, 0xcb, 0x36 },
155 sign1_fail_04[] = {
156 0xd2, 0x84, 0x4a, 0xa1, 0x01,
157 0x67, 0x75, 0x6e, 0x6b, 0x6e,
158 0x6f, 0x77, 0x6e, 0xa1, 0x04,
159 0x42, 0x31, 0x31, 0x54, 0x54,
160 0x68, 0x69, 0x73, 0x20, 0x69,
161 0x73, 0x20, 0x74, 0x68, 0x65,
162 0x20, 0x63, 0x6f, 0x6e, 0x74,
163 0x65, 0x6e, 0x74, 0x2e, 0x58,
164 0x40, 0x8e, 0xb3, 0x3e, 0x4c,
165 0xa3, 0x1d, 0x1c, 0x46, 0x5a,
166 0xb0, 0x5a, 0xac, 0x34, 0xcc,
167 0x6b, 0x23, 0xd5, 0x8f, 0xef,
168 0x5c, 0x08, 0x31, 0x06, 0xc4,
169 0xd2, 0x5a, 0x91, 0xae, 0xf0,
170 0xb0, 0x11, 0x7e, 0x2a, 0xf9,
171 0xa2, 0x91, 0xaa, 0x32, 0xe1,
172 0x4a, 0xb8, 0x34, 0xdc, 0x56,
173 0xed, 0x2a, 0x22, 0x34, 0x44,
174 0x54, 0x7e, 0x01, 0xf1, 0x1d,
175 0x3b, 0x09, 0x16, 0xe5, 0xa4,
176 0xc3, 0x45, 0xca, 0xcb, 0x36 },
177
178 /* sign1/fail05 is missing upstream */
179
180 sign1_fail_06[] = {
181 0xd2, 0x84, 0x45, 0xa2, 0x01,
182 0x26, 0x03, 0x00, 0xa1, 0x04,
183 0x42, 0x31, 0x31, 0x54, 0x54,
184 0x68, 0x69, 0x73, 0x20, 0x69,
185 0x73, 0x20, 0x74, 0x68, 0x65,
186 0x20, 0x63, 0x6f, 0x6e, 0x74,
187 0x65, 0x6e, 0x74, 0x2e, 0x58,
188 0x40, 0x8e, 0xb3, 0x3e, 0x4c,
189 0xa3, 0x1d, 0x1c, 0x46, 0x5a,
190 0xb0, 0x5a, 0xac, 0x34, 0xcc,
191 0x6b, 0x23, 0xd5, 0x8f, 0xef,
192 0x5c, 0x08, 0x31, 0x06, 0xc4,
193 0xd2, 0x5a, 0x91, 0xae, 0xf0,
194 0xb0, 0x11, 0x7e, 0x2a, 0xf9,
195 0xa2, 0x91, 0xaa, 0x32, 0xe1,
196 0x4a, 0xb8, 0x34, 0xdc, 0x56,
197 0xed, 0x2a, 0x22, 0x34, 0x44,
198 0x54, 0x7e, 0x01, 0xf1, 0x1d,
199 0x3b, 0x09, 0x16, 0xe5, 0xa4,
200 0xc3, 0x45, 0xca, 0xcb, 0x36 },
201
202 sign1_fail_07[] = {
203 0xd2, 0x84, 0x43, 0xa1, 0x01,
204 0x26, 0xa1, 0x04, 0x42, 0x31,
205 0x31, 0x54, 0x54, 0x68, 0x69,
206 0x73, 0x20, 0x69, 0x73, 0x20,
207 0x74, 0x68, 0x65, 0x20, 0x63,
208 0x6f, 0x6e, 0x74, 0x65, 0x6e,
209 0x74, 0x2e, 0x58, 0x40, 0x65,
210 0x20, 0xbb, 0xaf, 0x20, 0x81,
211 0xd7, 0xe0, 0xed, 0x0f, 0x95,
212 0xf7, 0x6e, 0xb0, 0x73, 0x3d,
213 0x66, 0x70, 0x05, 0xf7, 0x46,
214 0x7c, 0xec, 0x4b, 0x87, 0xb9,
215 0x38, 0x1a, 0x6b, 0xa1, 0xed,
216 0xe8, 0xe0, 0x0d, 0xf2, 0x9f,
217 0x32, 0xa3, 0x72, 0x30, 0xf3,
218 0x9a, 0x84, 0x2a, 0x54, 0x82,
219 0x1f, 0xdd, 0x22, 0x30, 0x92,
220 0x81, 0x9d, 0x77, 0x28, 0xef,
221 0xb9, 0xd3, 0xa0, 0x08, 0x0b,
222 0x75, 0x38, 0x0b },
223
224 sign_pass_01[] = {
225 0xd8, 0x62, 0x84, 0x41, 0xa0,
226 0xa0, 0x54, 0x54, 0x68, 0x69,
227 0x73, 0x20, 0x69, 0x73, 0x20,
228 0x74, 0x68, 0x65, 0x20, 0x63,
229 0x6f, 0x6e, 0x74, 0x65, 0x6e,
230 0x74, 0x2e, 0x81, 0x83, 0x43,
231 0xa1, 0x01, 0x26, 0xa1, 0x04,
232 0x42, 0x31, 0x31, 0x58, 0x40,
233 0xe2, 0xae, 0xaf, 0xd4, 0x0d,
234 0x69, 0xd1, 0x9d, 0xfe, 0x6e,
235 0x52, 0x07, 0x7c, 0x5d, 0x7f,
236 0xf4, 0xe4, 0x08, 0x28, 0x2c,
237 0xbe, 0xfb, 0x5d, 0x06, 0xcb,
238 0xf4, 0x14, 0xaf, 0x2e, 0x19,
239 0xd9, 0x82, 0xac, 0x45, 0xac,
240 0x98, 0xb8, 0x54, 0x4c, 0x90,
241 0x8b, 0x45, 0x07, 0xde, 0x1e,
242 0x90, 0xb7, 0x17, 0xc3, 0xd3,
243 0x48, 0x16, 0xfe, 0x92, 0x6a,
244 0x2b, 0x98, 0xf5, 0x3a, 0xfd,
245 0x2f, 0xa0, 0xf3, 0x0a },
246
247 sign_pass_02[] = {
248 0xd8, 0x62, 0x84, 0x40, 0xa0,
249 0x54, 0x54, 0x68, 0x69, 0x73,
250 0x20, 0x69, 0x73, 0x20, 0x74,
251 0x68, 0x65, 0x20, 0x63, 0x6f,
252 0x6e, 0x74, 0x65, 0x6e, 0x74,
253 0x2e, 0x81, 0x83, 0x43, 0xa1,
254 0x01, 0x26, 0xa1, 0x04, 0x42,
255 0x31, 0x31, 0x58, 0x40, 0xcb,
256 0xb8, 0xda, 0xd9, 0xbe, 0xaf,
257 0xb8, 0x90, 0xe1, 0xa4, 0x14,
258 0x12, 0x4d, 0x8b, 0xfb, 0xc2,
259 0x6b, 0xed, 0xf2, 0xa9, 0x4f,
260 0xcb, 0x5a, 0x88, 0x24, 0x32,
261 0xbf, 0xf6, 0xd6, 0x3e, 0x15,
262 0xf5, 0x74, 0xee, 0xb2, 0xab,
263 0x51, 0xd8, 0x3f, 0xa2, 0xcb,
264 0xf6, 0x26, 0x72, 0xeb, 0xf4,
265 0xc7, 0xd9, 0x93, 0xb0, 0xf4,
266 0xc2, 0x44, 0x76, 0x47, 0xd8,
267 0x31, 0xba, 0x57, 0xcc, 0xa8,
268 0x6b, 0x93, 0x0a },
269
270 sign_pass_03[] = {
271 0x84, 0x40, 0xa0, 0x54, 0x54,
272 0x68, 0x69, 0x73, 0x20, 0x69,
273 0x73, 0x20, 0x74, 0x68, 0x65,
274 0x20, 0x63, 0x6f, 0x6e, 0x74,
275 0x65, 0x6e, 0x74, 0x2e, 0x81,
276 0x83, 0x43, 0xa1, 0x01, 0x26,
277 0xa1, 0x04, 0x42, 0x31, 0x31,
278 0x58, 0x40, 0xe2, 0xae, 0xaf,
279 0xd4, 0x0d, 0x69, 0xd1, 0x9d,
280 0xfe, 0x6e, 0x52, 0x07, 0x7c,
281 0x5d, 0x7f, 0xf4, 0xe4, 0x08,
282 0x28, 0x2c, 0xbe, 0xfb, 0x5d,
283 0x06, 0xcb, 0xf4, 0x14, 0xaf,
284 0x2e, 0x19, 0xd9, 0x82, 0xac,
285 0x45, 0xac, 0x98, 0xb8, 0x54,
286 0x4c, 0x90, 0x8b, 0x45, 0x07,
287 0xde, 0x1e, 0x90, 0xb7, 0x17,
288 0xc3, 0xd3, 0x48, 0x16, 0xfe,
289 0x92, 0x6a, 0x2b, 0x98, 0xf5,
290 0x3a, 0xfd, 0x2f, 0xa0, 0xf3,
291 0x0a },
292
293 sign_fail_01[] = {
294 0xd9, 0x03, 0xe6, 0x84, 0x40,
295 0xa0, 0x54, 0x54, 0x68, 0x69,
296 0x73, 0x20, 0x69, 0x73, 0x20,
297 0x74, 0x68, 0x65, 0x20, 0x63,
298 0x6f, 0x6e, 0x74, 0x65, 0x6e,
299 0x74, 0x2e, 0x81, 0x83, 0x43,
300 0xa1, 0x01, 0x26, 0xa1, 0x04,
301 0x42, 0x31, 0x31, 0x58, 0x40,
302 0xe2, 0xae, 0xaf, 0xd4, 0x0d,
303 0x69, 0xd1, 0x9d, 0xfe, 0x6e,
304 0x52, 0x07, 0x7c, 0x5d, 0x7f,
305 0xf4, 0xe4, 0x08, 0x28, 0x2c,
306 0xbe, 0xfb, 0x5d, 0x06, 0xcb,
307 0xf4, 0x14, 0xaf, 0x2e, 0x19,
308 0xd9, 0x82, 0xac, 0x45, 0xac,
309 0x98, 0xb8, 0x54, 0x4c, 0x90,
310 0x8b, 0x45, 0x07, 0xde, 0x1e,
311 0x90, 0xb7, 0x17, 0xc3, 0xd3,
312 0x48, 0x16, 0xfe, 0x92, 0x6a,
313 0x2b, 0x98, 0xf5, 0x3a, 0xfd,
314 0x2f, 0xa0, 0xf3, 0x0a },
315
316 sign_fail_02[] = {
317 0xd8, 0x62, 0x84, 0x40, 0xa0,
318 0x54, 0x54, 0x68, 0x69, 0x73,
319 0x20, 0x69, 0x73, 0x20, 0x74,
320 0x68, 0x65, 0x20, 0x63, 0x6f,
321 0x6e, 0x74, 0x65, 0x6e, 0x74,
322 0x2e, 0x81, 0x83, 0x43, 0xa1,
323 0x01, 0x26, 0xa1, 0x04, 0x42,
324 0x31, 0x31, 0x58, 0x40, 0xe2,
325 0xae, 0xaf, 0xd4, 0x0d, 0x69,
326 0xd1, 0x9d, 0xfe, 0x6e, 0x52,
327 0x07, 0x7c, 0x5d, 0x7f, 0xf4,
328 0xe4, 0x08, 0x28, 0x2c, 0xbe,
329 0xfb, 0x5d, 0x06, 0xcb, 0xf4,
330 0x14, 0xaf, 0x2e, 0x19, 0xd9,
331 0x82, 0xac, 0x45, 0xac, 0x98,
332 0xb8, 0x54, 0x4c, 0x90, 0x8b,
333 0x45, 0x07, 0xde, 0x1e, 0x90,
334 0xb7, 0x17, 0xc3, 0xd3, 0x48,
335 0x16, 0xfe, 0x92, 0x6a, 0x2b,
336 0x98, 0xf5, 0x3a, 0xfd, 0x2f,
337 0xa0, 0xf3, 0x0b },
338
339 sign_fail_03[] = {
340 0xd8, 0x62, 0x84, 0x40, 0xa0,
341 0x54, 0x54, 0x68, 0x69, 0x73,
342 0x20, 0x69, 0x73, 0x20, 0x74,
343 0x68, 0x65, 0x20, 0x63, 0x6f,
344 0x6e, 0x74, 0x65, 0x6e, 0x74,
345 0x2e, 0x81, 0x83, 0x45, 0xa1,
346 0x01, 0x39, 0x03, 0xe6, 0xa1,
347 0x04, 0x42, 0x31, 0x31, 0x58,
348 0x40, 0xe2, 0xae, 0xaf, 0xd4,
349 0x0d, 0x69, 0xd1, 0x9d, 0xfe,
350 0x6e, 0x52, 0x07, 0x7c, 0x5d,
351 0x7f, 0xf4, 0xe4, 0x08, 0x28,
352 0x2c, 0xbe, 0xfb, 0x5d, 0x06,
353 0xcb, 0xf4, 0x14, 0xaf, 0x2e,
354 0x19, 0xd9, 0x82, 0xac, 0x45,
355 0xac, 0x98, 0xb8, 0x54, 0x4c,
356 0x90, 0x8b, 0x45, 0x07, 0xde,
357 0x1e, 0x90, 0xb7, 0x17, 0xc3,
358 0xd3, 0x48, 0x16, 0xfe, 0x92,
359 0x6a, 0x2b, 0x98, 0xf5, 0x3a,
360 0xfd, 0x2f, 0xa0, 0xf3, 0x0a },
361
362 sign_fail_04[] = {
363 0xd8, 0x62, 0x84, 0x40, 0xa0,
364 0x54, 0x54, 0x68, 0x69, 0x73,
365 0x20, 0x69, 0x73, 0x20, 0x74,
366 0x68, 0x65, 0x20, 0x63, 0x6f,
367 0x6e, 0x74, 0x65, 0x6e, 0x74,
368 0x2e, 0x81, 0x83, 0x4a, 0xa1,
369 0x01, 0x67, 0x75, 0x6e, 0x6b,
370 0x6e, 0x6f, 0x77, 0x6e, 0xa1,
371 0x04, 0x42, 0x31, 0x31, 0x58,
372 0x40, 0xe2, 0xae, 0xaf, 0xd4,
373 0x0d, 0x69, 0xd1, 0x9d, 0xfe,
374 0x6e, 0x52, 0x07, 0x7c, 0x5d,
375 0x7f, 0xf4, 0xe4, 0x08, 0x28,
376 0x2c, 0xbe, 0xfb, 0x5d, 0x06,
377 0xcb, 0xf4, 0x14, 0xaf, 0x2e,
378 0x19, 0xd9, 0x82, 0xac, 0x45,
379 0xac, 0x98, 0xb8, 0x54, 0x4c,
380 0x90, 0x8b, 0x45, 0x07, 0xde,
381 0x1e, 0x90, 0xb7, 0x17, 0xc3,
382 0xd3, 0x48, 0x16, 0xfe, 0x92,
383 0x6a, 0x2b, 0x98, 0xf5, 0x3a,
384 0xfd, 0x2f, 0xa0, 0xf3, 0x0a },
385
386 /* fail 5 missing upstream */
387
388 sign_fail_06[] = {
389 0xd8, 0x62, 0x84, 0x43, 0xa1,
390 0x03, 0x00, 0xa0, 0x54, 0x54,
391 0x68, 0x69, 0x73, 0x20, 0x69,
392 0x73, 0x20, 0x74, 0x68, 0x65,
393 0x20, 0x63, 0x6f, 0x6e, 0x74,
394 0x65, 0x6e, 0x74, 0x2e, 0x81,
395 0x83, 0x43, 0xa1, 0x01, 0x26,
396 0xa1, 0x04, 0x42, 0x31, 0x31,
397 0x58, 0x40, 0xe2, 0xae, 0xaf,
398 0xd4, 0x0d, 0x69, 0xd1, 0x9d,
399 0xfe, 0x6e, 0x52, 0x07, 0x7c,
400 0x5d, 0x7f, 0xf4, 0xe4, 0x08,
401 0x28, 0x2c, 0xbe, 0xfb, 0x5d,
402 0x06, 0xcb, 0xf4, 0x14, 0xaf,
403 0x2e, 0x19, 0xd9, 0x82, 0xac,
404 0x45, 0xac, 0x98, 0xb8, 0x54,
405 0x4c, 0x90, 0x8b, 0x45, 0x07,
406 0xde, 0x1e, 0x90, 0xb7, 0x17,
407 0xc3, 0xd3, 0x48, 0x16, 0xfe,
408 0x92, 0x6a, 0x2b, 0x98, 0xf5,
409 0x3a, 0xfd, 0x2f, 0xa0, 0xf3,
410 0x0a },
411
412 sign_fail_07[] = {
413 0xd8, 0x62, 0x84, 0x41, 0xa0,
414 0xa0, 0x54, 0x54, 0x68, 0x69,
415 0x73, 0x20, 0x69, 0x73, 0x20,
416 0x74, 0x68, 0x65, 0x20, 0x63,
417 0x6f, 0x6e, 0x74, 0x65, 0x6e,
418 0x74, 0x2e, 0x81, 0x83, 0x43,
419 0xa1, 0x01, 0x26, 0xa1, 0x04,
420 0x42, 0x31, 0x31, 0x58, 0x40,
421 0xd7, 0x1c, 0x05, 0xdb, 0x52,
422 0xc9, 0xce, 0x7f, 0x1b, 0xf5,
423 0xaa, 0xc0, 0x13, 0x34, 0xbb,
424 0xea, 0xca, 0xc1, 0xd8, 0x6a,
425 0x23, 0x03, 0xe6, 0xee, 0xaa,
426 0x89, 0x26, 0x6f, 0x45, 0xc0,
427 0x1e, 0xd6, 0x02, 0xca, 0x64,
428 0x9e, 0xaf, 0x79, 0x0d, 0x8b,
429 0xc9, 0x9d, 0x24, 0x58, 0x45,
430 0x7c, 0xa6, 0xa8, 0x72, 0x06,
431 0x19, 0x40, 0xe7, 0xaf, 0xbe,
432 0x48, 0xe2, 0x89, 0xdf, 0xac,
433 0x14, 0x6a, 0xe2, 0x58 },
434
435 sign_hmac_01[] = {
436 0xd8, 0x61, 0x85, 0x43, 0xa1,
437 0x01, 0x05, 0xa0, 0x54, 0x54,
438 0x68, 0x69, 0x73, 0x20, 0x69,
439 0x73, 0x20, 0x74, 0x68, 0x65,
440 0x20, 0x63, 0x6f, 0x6e, 0x74,
441 0x65, 0x6e, 0x74, 0x2e, 0x58,
442 0x20, 0x2b, 0xdc, 0xc8, 0x9f,
443 0x05, 0x82, 0x16, 0xb8, 0xa2,
444 0x08, 0xdd, 0xc6, 0xd8, 0xb5,
445 0x4a, 0xa9, 0x1f, 0x48, 0xbd,
446 0x63, 0x48, 0x49, 0x86, 0x56,
447 0x51, 0x05, 0xc9, 0xad, 0x5a,
448 0x66, 0x82, 0xf6, 0x81, 0x83,
449 0x40, 0xa2, 0x01, 0x25, 0x04,
450 0x4a, 0x6f, 0x75, 0x72, 0x2d,
451 0x73, 0x65, 0x63, 0x72, 0x65,
452 0x74, 0x40 },
453
454 sign_hmac_02[] = {
455 0xd8, 0x61, 0x85, 0x43, 0xa1,
456 0x01, 0x06, 0xa0, 0x54, 0x54,
457 0x68, 0x69, 0x73, 0x20, 0x69,
458 0x73, 0x20, 0x74, 0x68, 0x65,
459 0x20, 0x63, 0x6f, 0x6e, 0x74,
460 0x65, 0x6e, 0x74, 0x2e, 0x58,
461 0x30, 0xb3, 0x09, 0x7f, 0x70,
462 0x00, 0x9a, 0x11, 0x50, 0x74,
463 0x09, 0x59, 0x8a, 0x83, 0xe1,
464 0x5b, 0xbb, 0xbf, 0x19, 0x82,
465 0xdc, 0xe2, 0x8e, 0x5a, 0xb6,
466 0xd5, 0xa6, 0xaf, 0xf6, 0x89,
467 0x7b, 0xd2, 0x4b, 0xb8, 0xb7,
468 0x47, 0x96, 0x22, 0xc9, 0x40,
469 0x1b, 0x24, 0x09, 0x0d, 0x45,
470 0x82, 0x06, 0xd5, 0x87, 0x81,
471 0x83, 0x40, 0xa2, 0x01, 0x25,
472 0x04, 0x46, 0x73, 0x65, 0x63,
473 0x2d, 0x34, 0x38, 0x40 },
474
475 sign_hmac_03[] = {
476 0xd8, 0x61, 0x85, 0x43, 0xa1,
477 0x01, 0x07, 0xa0, 0x54, 0x54,
478 0x68, 0x69, 0x73, 0x20, 0x69,
479 0x73, 0x20, 0x74, 0x68, 0x65,
480 0x20, 0x63, 0x6f, 0x6e, 0x74,
481 0x65, 0x6e, 0x74, 0x2e, 0x58,
482 0x40, 0xcd, 0x28, 0xa6, 0xb3,
483 0xcf, 0xbb, 0xbf, 0x21, 0x48,
484 0x51, 0xb9, 0x06, 0xe0, 0x50,
485 0x05, 0x6c, 0xb4, 0x38, 0xa8,
486 0xb8, 0x89, 0x05, 0xb8, 0xb7,
487 0x46, 0x19, 0x77, 0x02, 0x27,
488 0x11, 0xa9, 0xd8, 0xac, 0x5d,
489 0xbc, 0x54, 0xe2, 0x9a, 0x56,
490 0xd9, 0x26, 0x04, 0x6b, 0x40,
491 0xfc, 0x26, 0x07, 0xc2, 0x5b,
492 0x34, 0x44, 0x54, 0xaa, 0x5f,
493 0x68, 0xde, 0x09, 0xa3, 0xe5,
494 0x25, 0xd3, 0x86, 0x5a, 0x05,
495 0x81, 0x83, 0x40, 0xa2, 0x01,
496 0x25, 0x04, 0x46, 0x73, 0x65,
497 0x63, 0x2d, 0x36, 0x34, 0x40 },
498
499 sign_hmac_04[] = {
500 0xd8, 0x61, 0x85, 0x43, 0xa1,
501 0x01, 0x05, 0xa0, 0x54, 0x54,
502 0x68, 0x69, 0x73, 0x20, 0x69,
503 0x73, 0x20, 0x74, 0x68, 0x65,
504 0x20, 0x63, 0x6f, 0x6e, 0x74,
505 0x65, 0x6e, 0x74, 0x2e, 0x58,
506 0x20, 0x2b, 0xdc, 0xc8, 0x9f,
507 0x05, 0x82, 0x16, 0xb8, 0xa2,
508 0x08, 0xdd, 0xc6, 0xd8, 0xb5,
509 0x4a, 0xa9, 0x1f, 0x48, 0xbd,
510 0x63, 0x48, 0x49, 0x86, 0x56,
511 0x51, 0x05, 0xc9, 0xad, 0x5a,
512 0x66, 0x82, 0xf7, 0x81, 0x83,
513 0x40, 0xa2, 0x01, 0x25, 0x04,
514 0x4a, 0x6f, 0x75, 0x72, 0x2d,
515 0x73, 0x65, 0x63, 0x72, 0x65,
516 0x74, 0x40 },
517
518 sign_hmac_05[] = {
519 0xd8, 0x61, 0x85, 0x43, 0xa1,
520 0x01, 0x04, 0xa0, 0x54, 0x54,
521 0x68, 0x69, 0x73, 0x20, 0x69,
522 0x73, 0x20, 0x74, 0x68, 0x65,
523 0x20, 0x63, 0x6f, 0x6e, 0x74,
524 0x65, 0x6e, 0x74, 0x2e, 0x48,
525 0x6f, 0x35, 0xca, 0xb7, 0x79,
526 0xf7, 0x78, 0x33, 0x81, 0x83,
527 0x40, 0xa2, 0x01, 0x25, 0x04,
528 0x4a, 0x6f, 0x75, 0x72, 0x2d,
529 0x73, 0x65, 0x63, 0x72, 0x65,
530 0x74, 0x40 },
531
532 enc_hmac_01[] = {
533 0xd1, 0x84, 0x43, 0xa1, 0x01,
534 0x05, 0xa0, 0x54, 0x54, 0x68,
535 0x69, 0x73, 0x20, 0x69, 0x73,
536 0x20, 0x74, 0x68, 0x65, 0x20,
537 0x63, 0x6f, 0x6e, 0x74, 0x65,
538 0x6e, 0x74, 0x2e, 0x58, 0x20,
539 0xa1, 0xa8, 0x48, 0xd3, 0x47,
540 0x1f, 0x9d, 0x61, 0xee, 0x49,
541 0x01, 0x8d, 0x24, 0x4c, 0x82,
542 0x47, 0x72, 0xf2, 0x23, 0xad,
543 0x4f, 0x93, 0x52, 0x93, 0xf1,
544 0x78, 0x9f, 0xc3, 0xa0, 0x8d,
545 0x8c, 0x58 },
546
547 enc_hmac_02[] = {
548 0xd1, 0x84, 0x43, 0xa1, 0x01,
549 0x06, 0xa0, 0x54, 0x54, 0x68,
550 0x69, 0x73, 0x20, 0x69, 0x73,
551 0x20, 0x74, 0x68, 0x65, 0x20,
552 0x63, 0x6f, 0x6e, 0x74, 0x65,
553 0x6e, 0x74, 0x2e, 0x58, 0x30,
554 0x99, 0x8d, 0x26, 0xc6, 0x45,
555 0x9a, 0xae, 0xec, 0xf4, 0x4e,
556 0xd2, 0x0c, 0xe0, 0x0c, 0x8c,
557 0xce, 0xdf, 0x0a, 0x1f, 0x3d,
558 0x22, 0xa9, 0x2f, 0xc0, 0x5d,
559 0xb0, 0x8c, 0x5a, 0xeb, 0x1c,
560 0xb5, 0x94, 0xca, 0xaf, 0x5a,
561 0x5c, 0x5e, 0x2e, 0x9d, 0x01,
562 0xcc, 0xe7, 0xe7, 0x7a, 0x93,
563 0xaa, 0x8c, 0x62 },
564
565 enc_hmac_03[] = {
566 0xd1, 0x84, 0x43, 0xa1, 0x01,
567 0x07, 0xa0, 0x54, 0x54, 0x68,
568 0x69, 0x73, 0x20, 0x69, 0x73,
569 0x20, 0x74, 0x68, 0x65, 0x20,
570 0x63, 0x6f, 0x6e, 0x74, 0x65,
571 0x6e, 0x74, 0x2e, 0x58, 0x40,
572 0x4a, 0x55, 0x5b, 0xf9, 0x71,
573 0xf7, 0xc1, 0x89, 0x1d, 0x9d,
574 0xdf, 0x30, 0x4a, 0x1a, 0x13,
575 0x2e, 0x2d, 0x6f, 0x81, 0x74,
576 0x49, 0x47, 0x4d, 0x81, 0x3e,
577 0x6d, 0x04, 0xd6, 0x59, 0x62,
578 0xbe, 0xd8, 0xbb, 0xa7, 0x0c,
579 0x17, 0xe1, 0xf5, 0x30, 0x8f,
580 0xa3, 0x99, 0x62, 0x95, 0x9a,
581 0x4b, 0x9b, 0x8d, 0x7d, 0xa8,
582 0xe6, 0xd8, 0x49, 0xb2, 0x09,
583 0xdc, 0xd3, 0xe9, 0x8c, 0xc0,
584 0xf1, 0x1e, 0xdd, 0xf2 },
585
586 enc_hmac_04[] = {
587 0xd1, 0x84, 0x43, 0xa1, 0x01,
588 0x05, 0xa0, 0x54, 0x54, 0x68,
589 0x69, 0x73, 0x20, 0x69, 0x73,
590 0x20, 0x74, 0x68, 0x65, 0x20,
591 0x63, 0x6f, 0x6e, 0x74, 0x65,
592 0x6e, 0x74, 0x2e, 0x58, 0x20,
593 0xa1, 0xa8, 0x48, 0xd3, 0x47,
594 0x1f, 0x9d, 0x61, 0xee, 0x49,
595 0x01, 0x8d, 0x24, 0x4c, 0x82,
596 0x47, 0x72, 0xf2, 0x23, 0xad,
597 0x4f, 0x93, 0x52, 0x93, 0xf1,
598 0x78, 0x9f, 0xc3, 0xa0, 0x8d,
599 0x8c, 0x59 },
600
601 enc_hmac_05[] = {
602 0xd1, 0x84, 0x43, 0xa1, 0x01,
603 0x04, 0xa0, 0x54, 0x54, 0x68,
604 0x69, 0x73, 0x20, 0x69, 0x73,
605 0x20, 0x74, 0x68, 0x65, 0x20,
606 0x63, 0x6f, 0x6e, 0x74, 0x65,
607 0x6e, 0x74, 0x2e, 0x48, 0x11,
608 0xf9, 0xe3, 0x57, 0x97, 0x5f,
609 0xb8, 0x49 }
610 #if 0
611 ,
612
613 countersign_sign_01[] = {
614 0xd8, 0x62, 0x84, 0x43, 0xa1,
615 0x03, 0x00, 0xa0, 0x54, 0x54,
616 0x68, 0x69, 0x73, 0x20, 0x69,
617 0x73, 0x20, 0x74, 0x68, 0x65,
618 0x20, 0x63, 0x6f, 0x6e, 0x74,
619 0x65, 0x6e, 0x74, 0x2e, 0x81,
620 0x83, 0x43, 0xa1, 0x01, 0x27,
621 0xa2, 0x07, 0x83, 0x43, 0xa1,
622 0x01, 0x27, 0xa1, 0x04, 0x42,
623 0x31, 0x31, 0x58, 0x40, 0x8e,
624 0x1b, 0xe2, 0xf9, 0x45, 0x3d,
625 0x26, 0x48, 0x12, 0xe5, 0x90,
626 0x49, 0x91, 0x32, 0xbe, 0xf3,
627 0xfb, 0xf9, 0xee, 0x9d, 0xb2,
628 0x7c, 0x2c, 0x16, 0x87, 0x88,
629 0xe3, 0xb7, 0xeb, 0xe5, 0x06,
630 0xc0, 0x4f, 0xd3, 0xd1, 0x9f,
631 0xaa, 0x9f, 0x51, 0x23, 0x2a,
632 0xf5, 0xc9, 0x59, 0xe4, 0xef,
633 0x47, 0x92, 0x88, 0x34, 0x64,
634 0x7f, 0x56, 0xdf, 0xbe, 0x93,
635 0x91, 0x12, 0x88, 0x4d, 0x08,
636 0xef, 0x25, 0x05, 0x04, 0x42,
637 0x31, 0x31, 0x58, 0x40, 0x77,
638 0xf3, 0xea, 0xcd, 0x11, 0x85,
639 0x2c, 0x4b, 0xf9, 0xcb, 0x1d,
640 0x72, 0xfa, 0xbe, 0x6b, 0x26,
641 0xfb, 0xa1, 0xd7, 0x60, 0x92,
642 0xb2, 0xb5, 0xb7, 0xec, 0x83,
643 0xb8, 0x35, 0x57, 0x65, 0x22,
644 0x64, 0xe6, 0x96, 0x90, 0xdb,
645 0xc1, 0x17, 0x2d, 0xdc, 0x0b,
646 0xf8, 0x84, 0x11, 0xc0, 0xd2,
647 0x5a, 0x50, 0x7f, 0xdb, 0x24,
648 0x7a, 0x20, 0xc4, 0x0d, 0x5e,
649 0x24, 0x5f, 0xab, 0xd3, 0xfc,
650 0x9e, 0xc1, 0x06 }
651 #endif
652 ;
653
654 extern const struct {
655 const uint8_t *set;
656 size_t len;
657 } keyset1, key3, key8, key9, key10;
658
659 static int
xcb(lws_cose_sig_ext_pay_t * x)660 xcb(lws_cose_sig_ext_pay_t *x)
661 {
662 x->ext = sign1_pass_02_ext;
663 x->xl = sizeof(sign1_pass_02_ext);
664
665 return LCOSESIGEXTCB_RET_FINISHED;
666 }
667
668
669
670 int
test_cose_sign(struct lws_context * context)671 test_cose_sign(struct lws_context *context)
672 {
673 struct lws_cose_validate_context *cps;
674 lws_cose_validate_create_info_t info;
675 lws_cose_validate_res_t *res;
676 lws_dll2_owner_t set;
677 lws_dll2_owner_t *o;
678 int n;
679
680 memset(&info, 0, sizeof(info));
681 info.cx = context;
682 info.keyset = &set;
683
684 #if 1
685 {
686 int fd = open("sign_hmac01.sig",
687 LWS_O_CREAT | LWS_O_TRUNC | LWS_O_WRONLY, 0600);
688
689 if (fd >= 0) {
690 write(fd, sign_hmac_01, sizeof(sign_hmac_01));
691 close(fd);
692 }
693 }
694 #endif
695
696 /*
697 * valid sign1 we have key for
698 */
699
700 lwsl_user("%s: sign1/sign-pass-01\n", __func__);
701
702 lws_dll2_owner_clear(&set);
703 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
704 lwsl_notice("%s: key import fail\n", __func__);
705 return 1;
706 }
707
708 info.sigtype = SIGTYPE_SINGLE;
709 cps = lws_cose_validate_create(&info);
710 if (!cps) {
711 lwsl_notice("%s: sign_val_create fail\n", __func__);
712 goto bail;
713 }
714
715 n = lws_cose_validate_chunk(cps, sign1_pass_01, sizeof(sign1_pass_01),
716 NULL);
717 if (n) {
718 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
719 goto bail1;
720 }
721
722 o = lws_cose_validate_results(cps);
723 if (o->count != 1)
724 goto bail1;
725
726 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
727 if (res->result)
728 goto bail1;
729
730 lws_cose_validate_destroy(&cps);
731 lws_cose_key_set_destroy(&set);
732
733 /*
734 * valid sign1 but empty key set, so can't judge it
735 */
736
737 lwsl_user("%s: sign1/sign-pass-01 - no key\n", __func__);
738
739 lws_dll2_owner_clear(&set);
740
741 info.sigtype = SIGTYPE_SINGLE;
742 cps = lws_cose_validate_create(&info);
743 if (!cps) {
744 lwsl_notice("%s: sign_val_create fail\n", __func__);
745 goto bail;
746 }
747
748 n = lws_cose_validate_chunk(cps, sign1_pass_01, sizeof(sign1_pass_01),
749 NULL);
750 if (n) {
751 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
752 goto bail1;
753 }
754
755 o = lws_cose_validate_results(cps);
756 if (o->count != 1)
757 goto bail1;
758
759 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
760 if (!res->result)
761 goto bail1;
762
763 lws_cose_validate_destroy(&cps);
764 lws_cose_key_set_destroy(&set);
765
766 /*
767 * valid sign1
768 */
769
770 lwsl_user("%s: sign1/sign-pass-02\n", __func__);
771
772 lws_dll2_owner_clear(&set);
773 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
774 lwsl_notice("%s: key import fail\n", __func__);
775 return 1;
776 }
777
778 info.sigtype = SIGTYPE_SINGLE;
779 info.ext_cb = xcb;
780 info.ext_len = sizeof(sign1_pass_02_ext);
781 cps = lws_cose_validate_create(&info);
782 if (!cps) {
783 lwsl_notice("%s: sign_val_create fail\n", __func__);
784 goto bail;
785 }
786
787 n = lws_cose_validate_chunk(cps, sign1_pass_02, sizeof(sign1_pass_02),
788 NULL);
789 if (n) {
790 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
791 goto bail1;
792 }
793
794 o = lws_cose_validate_results(cps);
795 if (o->count != 1)
796 goto bail1;
797
798 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
799 if (res->result)
800 goto bail1;
801
802 lws_cose_validate_destroy(&cps);
803 lws_cose_key_set_destroy(&set);
804
805 /*
806 * valid sign1 without enclosing tag
807 */
808
809 lwsl_user("%s: sign1/sign-pass-03\n", __func__);
810
811 lws_dll2_owner_clear(&set);
812 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
813 lwsl_notice("%s: key import fail\n", __func__);
814 return 1;
815 }
816
817 info.sigtype = SIGTYPE_SINGLE;
818 info.ext_cb = NULL;
819 info.ext_len = 0;
820 cps = lws_cose_validate_create(&info);
821 if (!cps) {
822 lwsl_notice("%s: sign_val_create fail\n", __func__);
823 goto bail;
824 }
825
826 n = lws_cose_validate_chunk(cps, sign1_pass_03, sizeof(sign1_pass_03),
827 NULL);
828 if (n) {
829 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
830 goto bail1;
831 }
832
833 o = lws_cose_validate_results(cps);
834 if (o->count != 1)
835 goto bail1;
836
837 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
838 if (res->result)
839 goto bail1;
840
841 lws_cose_validate_destroy(&cps);
842 lws_cose_key_set_destroy(&set);
843
844 /*
845 * sign1 with wrong tag
846 */
847
848 lwsl_user("%s: sign1/sign-fail-01\n", __func__);
849
850 lws_dll2_owner_clear(&set);
851 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
852 lwsl_notice("%s: key import fail\n", __func__);
853 return 1;
854 }
855
856 info.sigtype = SIGTYPE_SINGLE;
857 cps = lws_cose_validate_create(&info);
858 if (!cps) {
859 lwsl_notice("%s: sign_val_create fail\n", __func__);
860 goto bail;
861 }
862
863 n = lws_cose_validate_chunk(cps, sign1_fail_01, sizeof(sign1_fail_01),
864 NULL);
865 if (!n) {
866 lwsl_notice("%s: sign_val_chunk should have failed\n", __func__);
867 goto bail1;
868 }
869
870 lws_cose_validate_destroy(&cps);
871 lws_cose_key_set_destroy(&set);
872
873 /*
874 * invalid sign1, signature tampered
875 */
876
877 lwsl_user("%s: sign1/sign-fail-02\n", __func__);
878
879 lws_dll2_owner_clear(&set);
880 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
881 lwsl_notice("%s: key import fail\n", __func__);
882 return 1;
883 }
884
885 info.sigtype = SIGTYPE_SINGLE;
886 cps = lws_cose_validate_create(&info);
887 if (!cps) {
888 lwsl_notice("%s: sign_val_create fail\n", __func__);
889 goto bail;
890 }
891
892 n = lws_cose_validate_chunk(cps, sign1_fail_02, sizeof(sign1_fail_02),
893 NULL);
894 if (n) {
895 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
896 goto bail1;
897 }
898
899 o = lws_cose_validate_results(cps);
900 if (o->count != 1)
901 goto bail1;
902
903 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
904 if (!res->result)
905 /* validation result must be fail */
906 goto bail1;
907
908 lws_cose_validate_destroy(&cps);
909 lws_cose_key_set_destroy(&set);
910
911 /*
912 * invalid sign1, alg tampered
913 */
914
915 lwsl_user("%s: sign1/sign-fail-03\n", __func__);
916
917 lws_dll2_owner_clear(&set);
918 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
919 lwsl_notice("%s: key import fail\n", __func__);
920 return 1;
921 }
922
923 info.sigtype = SIGTYPE_SINGLE;
924 cps = lws_cose_validate_create(&info);
925 if (!cps) {
926 lwsl_notice("%s: sign_val_create fail\n", __func__);
927 goto bail;
928 }
929
930 n = lws_cose_validate_chunk(cps, sign1_fail_03, sizeof(sign1_fail_03),
931 NULL);
932 if (n) {
933 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
934 goto bail1;
935 }
936
937 o = lws_cose_validate_results(cps);
938 if (o->count != 1)
939 goto bail1;
940
941 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
942 if (!res->result)
943 /* validation result must be fail */
944 goto bail1;
945
946 lws_cose_validate_destroy(&cps);
947 lws_cose_key_set_destroy(&set);
948
949 /*
950 * invalid sign1, alg sign tampered
951 */
952
953 lwsl_user("%s: sign1/sign-fail-04\n", __func__);
954
955 lws_dll2_owner_clear(&set);
956 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
957 lwsl_notice("%s: key import fail\n", __func__);
958 return 1;
959 }
960
961 info.sigtype = SIGTYPE_SINGLE;
962 cps = lws_cose_validate_create(&info);
963 if (!cps) {
964 lwsl_notice("%s: sign_val_create fail\n", __func__);
965 goto bail;
966 }
967
968 n = lws_cose_validate_chunk(cps, sign1_fail_04, sizeof(sign1_fail_04),
969 NULL);
970 if (n) {
971 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
972 goto bail1;
973 }
974
975 o = lws_cose_validate_results(cps);
976 if (o->count != 1)
977 goto bail1;
978
979 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
980 if (!res->result)
981 /* validation result must be fail */
982 goto bail1;
983
984 lws_cose_validate_destroy(&cps);
985 lws_cose_key_set_destroy(&set);
986
987 /*
988 * invalid sign1, protected attributes tampered
989 */
990
991 lwsl_user("%s: sign1/sign-fail-06\n", __func__);
992
993 lws_dll2_owner_clear(&set);
994 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
995 lwsl_notice("%s: key import fail\n", __func__);
996 return 1;
997 }
998
999 info.sigtype = SIGTYPE_SINGLE;
1000 cps = lws_cose_validate_create(&info);
1001 if (!cps) {
1002 lwsl_notice("%s: sign_val_create fail\n", __func__);
1003 goto bail;
1004 }
1005
1006 n = lws_cose_validate_chunk(cps, sign1_fail_06, sizeof(sign1_fail_06),
1007 NULL);
1008 if (n) {
1009 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1010 goto bail1;
1011 }
1012
1013 o = lws_cose_validate_results(cps);
1014 if (o->count != 1)
1015 goto bail1;
1016
1017 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1018 if (!res->result)
1019 /* validation result must be fail */
1020 goto bail1;
1021
1022 lws_cose_validate_destroy(&cps);
1023 lws_cose_key_set_destroy(&set);
1024
1025 /*
1026 * invalid sign1, protected attribute removed
1027 */
1028
1029 lwsl_user("%s: sign1/sign-fail-07\n", __func__);
1030
1031 lws_dll2_owner_clear(&set);
1032 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1033 lwsl_notice("%s: key import fail\n", __func__);
1034 return 1;
1035 }
1036
1037 info.sigtype = SIGTYPE_SINGLE;
1038 cps = lws_cose_validate_create(&info);
1039 if (!cps) {
1040 lwsl_notice("%s: sign_val_create fail\n", __func__);
1041 goto bail;
1042 }
1043
1044 n = lws_cose_validate_chunk(cps, sign1_fail_07, sizeof(sign1_fail_07),
1045 NULL);
1046 if (n) {
1047 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1048 goto bail1;
1049 }
1050
1051 o = lws_cose_validate_results(cps);
1052 if (o->count != 1)
1053 goto bail1;
1054
1055 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1056 if (!res->result)
1057 /* validation result must be fail */
1058 goto bail1;
1059
1060 lws_cose_validate_destroy(&cps);
1061 lws_cose_key_set_destroy(&set);
1062
1063 /*
1064 * valid sign we have key for
1065 */
1066
1067 lwsl_user("%s: sign/sign-pass-01\n", __func__);
1068
1069 lws_dll2_owner_clear(&set);
1070 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1071 lwsl_notice("%s: key import fail\n", __func__);
1072 return 1;
1073 }
1074
1075 info.sigtype = SIGTYPE_MULTI;
1076 cps = lws_cose_validate_create(&info);
1077 if (!cps) {
1078 lwsl_notice("%s: sign_val_create fail\n", __func__);
1079 goto bail;
1080 }
1081
1082 n = lws_cose_validate_chunk(cps, sign_pass_01, sizeof(sign_pass_01),
1083 NULL);
1084 if (n) {
1085 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1086 goto bail1;
1087 }
1088
1089 o = lws_cose_validate_results(cps);
1090 if (o->count != 1) {
1091 lwsl_notice("%s: results: %d\n", __func__, o->count);
1092 goto bail1;
1093 }
1094
1095 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1096 if (res->result)
1097 goto bail1;
1098
1099 lws_cose_validate_destroy(&cps);
1100 lws_cose_key_set_destroy(&set);
1101
1102 /*
1103 * valid sign we have key for
1104 */
1105
1106 lwsl_user("%s: sign/sign-pass-02\n", __func__);
1107
1108 lws_dll2_owner_clear(&set);
1109 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1110 lwsl_notice("%s: key import fail\n", __func__);
1111 return 1;
1112 }
1113
1114 info.sigtype = SIGTYPE_MULTI;
1115 info.ext_cb = xcb;
1116 info.ext_len = sizeof(sign1_pass_02_ext);
1117 cps = lws_cose_validate_create(&info);
1118 if (!cps) {
1119 lwsl_notice("%s: sign_val_create fail\n", __func__);
1120 goto bail;
1121 }
1122
1123 n = lws_cose_validate_chunk(cps, sign_pass_02, sizeof(sign_pass_02),
1124 NULL);
1125 if (n) {
1126 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1127 goto bail1;
1128 }
1129
1130 o = lws_cose_validate_results(cps);
1131 if (o->count != 1) {
1132 lwsl_notice("%s: results: %d\n", __func__, o->count);
1133 goto bail1;
1134 }
1135
1136 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1137 if (res->result)
1138 goto bail1;
1139
1140 lws_cose_validate_destroy(&cps);
1141 lws_cose_key_set_destroy(&set);
1142
1143 /*
1144 * valid sign we have key for
1145 */
1146
1147 lwsl_user("%s: sign/sign-pass-03\n", __func__);
1148
1149 lws_dll2_owner_clear(&set);
1150 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1151 lwsl_notice("%s: key import fail\n", __func__);
1152 return 1;
1153 }
1154
1155 info.sigtype = SIGTYPE_MULTI;
1156 info.ext_cb = NULL;
1157 info.ext_len = 0;
1158 cps = lws_cose_validate_create(&info);
1159 if (!cps) {
1160 lwsl_notice("%s: sign_val_create fail\n", __func__);
1161 goto bail;
1162 }
1163
1164 n = lws_cose_validate_chunk(cps, sign_pass_03, sizeof(sign_pass_03),
1165 NULL);
1166 if (n) {
1167 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1168 goto bail1;
1169 }
1170
1171 o = lws_cose_validate_results(cps);
1172 if (o->count != 1) {
1173 lwsl_notice("%s: results: %d\n", __func__, o->count);
1174 goto bail1;
1175 }
1176
1177 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1178 if (res->result)
1179 goto bail1;
1180
1181 lws_cose_validate_destroy(&cps);
1182 lws_cose_key_set_destroy(&set);
1183
1184 /*
1185 * wrong cbor tag
1186 */
1187
1188 lwsl_user("%s: sign/sign-fail-01\n", __func__);
1189
1190 lws_dll2_owner_clear(&set);
1191 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1192 lwsl_notice("%s: key import fail\n", __func__);
1193 return 1;
1194 }
1195
1196 info.sigtype = SIGTYPE_MULTI;
1197 cps = lws_cose_validate_create(&info);
1198 if (!cps) {
1199 lwsl_notice("%s: sign_val_create fail\n", __func__);
1200 goto bail;
1201 }
1202
1203 n = lws_cose_validate_chunk(cps, sign_fail_01, sizeof(sign_fail_01),
1204 NULL);
1205 if (!n) {
1206 lwsl_notice("%s: sign_val_chunk should fail\n", __func__);
1207 goto bail1;
1208 }
1209
1210 lws_cose_validate_destroy(&cps);
1211 lws_cose_key_set_destroy(&set);
1212
1213 /*
1214 * tampered signature
1215 */
1216
1217 lwsl_user("%s: sign/sign-fail-02\n", __func__);
1218
1219 lws_dll2_owner_clear(&set);
1220 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1221 lwsl_notice("%s: key import fail\n", __func__);
1222 return 1;
1223 }
1224
1225 info.sigtype = SIGTYPE_MULTI;
1226 cps = lws_cose_validate_create(&info);
1227 if (!cps) {
1228 lwsl_notice("%s: sign_val_create fail\n", __func__);
1229 goto bail;
1230 }
1231
1232 n = lws_cose_validate_chunk(cps, sign_fail_02, sizeof(sign_fail_02),
1233 NULL);
1234 if (n) {
1235 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1236 goto bail1;
1237 }
1238
1239 o = lws_cose_validate_results(cps);
1240 if (o->count != 1) {
1241 lwsl_notice("%s: results: %d\n", __func__, o->count);
1242 goto bail1;
1243 }
1244
1245 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1246 if (!res->result)
1247 goto bail1;
1248
1249 lws_cose_validate_destroy(&cps);
1250 lws_cose_key_set_destroy(&set);
1251
1252 /*
1253 * tampered sign alg -999
1254 */
1255
1256 lwsl_user("%s: sign/sign-fail-03\n", __func__);
1257
1258 lws_dll2_owner_clear(&set);
1259 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1260 lwsl_notice("%s: key import fail\n", __func__);
1261 return 1;
1262 }
1263
1264 info.sigtype = SIGTYPE_MULTI;
1265 cps = lws_cose_validate_create(&info);
1266 if (!cps) {
1267 lwsl_notice("%s: sign_val_create fail\n", __func__);
1268 goto bail;
1269 }
1270
1271 n = lws_cose_validate_chunk(cps, sign_fail_03, sizeof(sign_fail_03),
1272 NULL);
1273 if (n) {
1274 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1275 goto bail1;
1276 }
1277
1278 o = lws_cose_validate_results(cps);
1279 if (o->count != 1) {
1280 lwsl_notice("%s: results: %d\n", __func__, o->count);
1281 goto bail1;
1282 }
1283
1284 lws_cose_validate_destroy(&cps);
1285 lws_cose_key_set_destroy(&set);
1286
1287 /*
1288 * tampered sign alg 0
1289 */
1290
1291 lwsl_user("%s: sign/sign-fail-04\n", __func__);
1292
1293 lws_dll2_owner_clear(&set);
1294 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1295 lwsl_notice("%s: key import fail\n", __func__);
1296 return 1;
1297 }
1298
1299 info.sigtype = SIGTYPE_MULTI;
1300 cps = lws_cose_validate_create(&info);
1301 if (!cps) {
1302 lwsl_notice("%s: sign_val_create fail\n", __func__);
1303 goto bail;
1304 }
1305
1306 n = lws_cose_validate_chunk(cps, sign_fail_04, sizeof(sign_fail_04),
1307 NULL);
1308 if (n) {
1309 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1310 goto bail1;
1311 }
1312
1313 o = lws_cose_validate_results(cps);
1314 if (o->count != 1) {
1315 lwsl_notice("%s: results: %d\n", __func__, o->count);
1316 goto bail1;
1317 }
1318
1319 lws_cose_validate_destroy(&cps);
1320 lws_cose_key_set_destroy(&set);
1321
1322 /*
1323 * add protected attribute
1324 */
1325
1326 lwsl_user("%s: sign/sign-fail-06\n", __func__);
1327
1328 lws_dll2_owner_clear(&set);
1329 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1330 lwsl_notice("%s: key import fail\n", __func__);
1331 return 1;
1332 }
1333
1334 info.sigtype = SIGTYPE_MULTI;
1335 cps = lws_cose_validate_create(&info);
1336 if (!cps) {
1337 lwsl_notice("%s: sign_val_create fail\n", __func__);
1338 goto bail;
1339 }
1340
1341 n = lws_cose_validate_chunk(cps, sign_fail_06, sizeof(sign_fail_06),
1342 NULL);
1343 if (n) {
1344 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1345 goto bail1;
1346 }
1347
1348 o = lws_cose_validate_results(cps);
1349 if (o->count != 1) {
1350 lwsl_notice("%s: results: %d\n", __func__, o->count);
1351 goto bail1;
1352 }
1353
1354 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1355 if (!res->result)
1356 goto bail1;
1357
1358 lws_cose_validate_destroy(&cps);
1359 lws_cose_key_set_destroy(&set);
1360
1361 /*
1362 * remove protected attribute
1363 */
1364
1365 lwsl_user("%s: sign/sign-fail-07\n", __func__);
1366
1367 lws_dll2_owner_clear(&set);
1368 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1369 lwsl_notice("%s: key import fail\n", __func__);
1370 return 1;
1371 }
1372
1373 info.sigtype = SIGTYPE_MULTI;
1374 cps = lws_cose_validate_create(&info);
1375 if (!cps) {
1376 lwsl_notice("%s: sign_val_create fail\n", __func__);
1377 goto bail;
1378 }
1379
1380 n = lws_cose_validate_chunk(cps, sign_fail_07, sizeof(sign_fail_07),
1381 NULL);
1382 if (n) {
1383 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1384 goto bail1;
1385 }
1386
1387 o = lws_cose_validate_results(cps);
1388 if (o->count != 1) {
1389 lwsl_notice("%s: results: %d\n", __func__, o->count);
1390 goto bail1;
1391 }
1392
1393 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1394 if (!res->result)
1395 goto bail1;
1396
1397 lws_cose_validate_destroy(&cps);
1398 lws_cose_key_set_destroy(&set);
1399
1400
1401 /*
1402 * valid HMAC sign we have key for
1403 */
1404
1405 lwsl_user("%s: hmac-examples/hmac-01\n", __func__);
1406
1407 lws_dll2_owner_clear(&set);
1408 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1409 lwsl_notice("%s: key import fail\n", __func__);
1410 return 1;
1411 }
1412
1413 info.sigtype = SIGTYPE_MAC;
1414 cps = lws_cose_validate_create(&info);
1415 if (!cps) {
1416 lwsl_notice("%s: sign_val_create fail\n", __func__);
1417 goto bail;
1418 }
1419
1420 n = lws_cose_validate_chunk(cps, sign_hmac_01, sizeof(sign_hmac_01),
1421 NULL);
1422 if (n) {
1423 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1424 goto bail1;
1425 }
1426
1427 o = lws_cose_validate_results(cps);
1428 if (o->count != 1) {
1429 lwsl_err("%s: count %d\n", __func__, o->count);
1430 goto bail1;
1431 }
1432
1433 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1434 if (res->result) {
1435 lwsl_err("%s: result is fail\n", __func__);
1436 goto bail1;
1437 }
1438
1439 lws_cose_validate_destroy(&cps);
1440 lws_cose_key_set_destroy(&set);
1441
1442 /*
1443 * valid HMAC sign we have key for
1444 */
1445
1446 lwsl_user("%s: hmac-examples/hmac-02\n", __func__);
1447
1448 lws_dll2_owner_clear(&set);
1449 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1450 lwsl_notice("%s: key import fail\n", __func__);
1451 return 1;
1452 }
1453
1454 info.sigtype = SIGTYPE_MAC;
1455 cps = lws_cose_validate_create(&info);
1456 if (!cps) {
1457 lwsl_notice("%s: sign_val_create fail\n", __func__);
1458 goto bail;
1459 }
1460
1461 n = lws_cose_validate_chunk(cps, sign_hmac_02, sizeof(sign_hmac_02),
1462 NULL);
1463 if (n) {
1464 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1465 goto bail1;
1466 }
1467
1468 o = lws_cose_validate_results(cps);
1469 if (o->count != 1) {
1470 lwsl_err("%s: count %d\n", __func__, o->count);
1471 goto bail1;
1472 }
1473
1474 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1475 if (res->result) {
1476 lwsl_err("%s: result is fail\n", __func__);
1477 goto bail1;
1478 }
1479
1480 lws_cose_validate_destroy(&cps);
1481 lws_cose_key_set_destroy(&set);
1482
1483
1484 /*
1485 * valid HMAC sign we have key for
1486 */
1487
1488 lwsl_user("%s: hmac-examples/hmac-03\n", __func__);
1489
1490 lws_dll2_owner_clear(&set);
1491 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1492 lwsl_notice("%s: key import fail\n", __func__);
1493 return 1;
1494 }
1495
1496 info.sigtype = SIGTYPE_MAC;
1497 cps = lws_cose_validate_create(&info);
1498 if (!cps) {
1499 lwsl_notice("%s: sign_val_create fail\n", __func__);
1500 goto bail;
1501 }
1502
1503 n = lws_cose_validate_chunk(cps, sign_hmac_03, sizeof(sign_hmac_03),
1504 NULL);
1505 if (n) {
1506 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1507 goto bail1;
1508 }
1509
1510 o = lws_cose_validate_results(cps);
1511 if (o->count != 1) {
1512 lwsl_err("%s: count %d\n", __func__, o->count);
1513 goto bail1;
1514 }
1515
1516 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1517 if (res->result) {
1518 lwsl_err("%s: result is fail\n", __func__);
1519 goto bail1;
1520 }
1521
1522 lws_cose_validate_destroy(&cps);
1523 lws_cose_key_set_destroy(&set);
1524
1525 /*
1526 * invalid HMAC sign we have key for
1527 */
1528
1529 lwsl_user("%s: hmac-examples/hmac-04 fail mac tag\n", __func__);
1530
1531 lws_dll2_owner_clear(&set);
1532 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1533 lwsl_notice("%s: key import fail\n", __func__);
1534 return 1;
1535 }
1536
1537 info.sigtype = SIGTYPE_MAC;
1538 cps = lws_cose_validate_create(&info);
1539 if (!cps) {
1540 lwsl_notice("%s: sign_val_create fail\n", __func__);
1541 goto bail;
1542 }
1543
1544 n = lws_cose_validate_chunk(cps, sign_hmac_04, sizeof(sign_hmac_04),
1545 NULL);
1546 if (n) {
1547 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1548 goto bail1;
1549 }
1550
1551 o = lws_cose_validate_results(cps);
1552 if (o->count != 1) {
1553 lwsl_err("%s: count %d\n", __func__, o->count);
1554 goto bail1;
1555 }
1556
1557 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1558 if (!res->result) {
1559 lwsl_err("%s: result is wrongly succeeding\n", __func__);
1560 goto bail1;
1561 }
1562
1563 lws_cose_validate_destroy(&cps);
1564 lws_cose_key_set_destroy(&set);
1565
1566 /*
1567 * valid HMAC sign we have key for HS256/64
1568 */
1569
1570 lwsl_user("%s: hmac-examples/hmac-05\n", __func__);
1571
1572 lws_dll2_owner_clear(&set);
1573 if (!lws_cose_key_import(&set, NULL, NULL, keyset1.set, keyset1.len)) {
1574 lwsl_notice("%s: key import fail\n", __func__);
1575 return 1;
1576 }
1577
1578 info.sigtype = SIGTYPE_MAC;
1579 cps = lws_cose_validate_create(&info);
1580 if (!cps) {
1581 lwsl_notice("%s: sign_val_create fail\n", __func__);
1582 goto bail;
1583 }
1584
1585 n = lws_cose_validate_chunk(cps, sign_hmac_05, sizeof(sign_hmac_05),
1586 NULL);
1587 if (n) {
1588 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1589 goto bail1;
1590 }
1591
1592 o = lws_cose_validate_results(cps);
1593 if (o->count != 1) {
1594 lwsl_err("%s: count %d\n", __func__, o->count);
1595 goto bail1;
1596 }
1597
1598 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1599 if (res->result) {
1600 lwsl_err("%s: result is fail\n", __func__);
1601 goto bail1;
1602 }
1603
1604 lws_cose_validate_destroy(&cps);
1605 lws_cose_key_set_destroy(&set);
1606
1607 /*
1608 * valid HMAC sign with implicit HS256 key
1609 */
1610
1611 lwsl_user("%s: hmac-examples/enc-01\n", __func__);
1612
1613 lws_dll2_owner_clear(&set);
1614 if (!lws_cose_key_import(&set, NULL, NULL, key3.set, key3.len)) {
1615 lwsl_notice("%s: key import fail\n", __func__);
1616 return 1;
1617 }
1618
1619 info.sigtype = SIGTYPE_MAC0;
1620 cps = lws_cose_validate_create(&info);
1621 if (!cps) {
1622 lwsl_notice("%s: sign_val_create fail\n", __func__);
1623 goto bail;
1624 }
1625
1626 n = lws_cose_validate_chunk(cps, enc_hmac_01, sizeof(enc_hmac_01),
1627 NULL);
1628 if (n) {
1629 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1630 goto bail1;
1631 }
1632
1633 o = lws_cose_validate_results(cps);
1634 if (o->count != 1) {
1635 lwsl_err("%s: count %d\n", __func__, o->count);
1636 goto bail1;
1637 }
1638
1639 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1640 if (res->result) {
1641 lwsl_err("%s: result is fail\n", __func__);
1642 goto bail1;
1643 }
1644
1645 lws_cose_validate_destroy(&cps);
1646 lws_cose_key_set_destroy(&set);
1647
1648 /*
1649 * valid HMAC sign with implicit HS384 key
1650 */
1651
1652 lwsl_user("%s: hmac-examples/enc-02\n", __func__);
1653
1654 lws_dll2_owner_clear(&set);
1655 if (!lws_cose_key_import(&set, NULL, NULL, key8.set, key8.len)) {
1656 lwsl_notice("%s: key import fail\n", __func__);
1657 return 1;
1658 }
1659
1660 info.sigtype = SIGTYPE_MAC0;
1661 cps = lws_cose_validate_create(&info);
1662 if (!cps) {
1663 lwsl_notice("%s: sign_val_create fail\n", __func__);
1664 goto bail;
1665 }
1666
1667 n = lws_cose_validate_chunk(cps, enc_hmac_02, sizeof(enc_hmac_02),
1668 NULL);
1669 if (n) {
1670 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1671 goto bail1;
1672 }
1673
1674 o = lws_cose_validate_results(cps);
1675 if (o->count != 1) {
1676 lwsl_err("%s: count %d\n", __func__, o->count);
1677 goto bail1;
1678 }
1679
1680 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1681 if (res->result) {
1682 lwsl_err("%s: result is fail\n", __func__);
1683 goto bail1;
1684 }
1685
1686 lws_cose_validate_destroy(&cps);
1687 lws_cose_key_set_destroy(&set);
1688
1689 /*
1690 * valid HMAC sign with implicit HS512 key
1691 */
1692
1693 lwsl_user("%s: hmac-examples/enc-03\n", __func__);
1694
1695 lws_dll2_owner_clear(&set);
1696 if (!lws_cose_key_import(&set, NULL, NULL, key9.set, key9.len)) {
1697 lwsl_notice("%s: key import fail\n", __func__);
1698 return 1;
1699 }
1700
1701 info.sigtype = SIGTYPE_MAC0;
1702 cps = lws_cose_validate_create(&info);
1703 if (!cps) {
1704 lwsl_notice("%s: sign_val_create fail\n", __func__);
1705 goto bail;
1706 }
1707
1708 n = lws_cose_validate_chunk(cps, enc_hmac_03, sizeof(enc_hmac_03),
1709 NULL);
1710 if (n) {
1711 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1712 goto bail1;
1713 }
1714
1715 o = lws_cose_validate_results(cps);
1716 if (o->count != 1) {
1717 lwsl_err("%s: count %d\n", __func__, o->count);
1718 goto bail1;
1719 }
1720
1721 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1722 if (res->result) {
1723 lwsl_err("%s: result is fail\n", __func__);
1724 goto bail1;
1725 }
1726
1727 lws_cose_validate_destroy(&cps);
1728 lws_cose_key_set_destroy(&set);
1729
1730 /*
1731 * invalid HMAC sign with implicit HS256 key, tampered hmac tag
1732 */
1733
1734 lwsl_user("%s: hmac-examples/enc-04\n", __func__);
1735
1736 lws_dll2_owner_clear(&set);
1737 if (!lws_cose_key_import(&set, NULL, NULL, key3.set, key3.len)) {
1738 lwsl_notice("%s: key import fail\n", __func__);
1739 return 1;
1740 }
1741
1742 info.sigtype = SIGTYPE_MAC0;
1743 cps = lws_cose_validate_create(&info);
1744 if (!cps) {
1745 lwsl_notice("%s: sign_val_create fail\n", __func__);
1746 goto bail;
1747 }
1748
1749 n = lws_cose_validate_chunk(cps, enc_hmac_04, sizeof(enc_hmac_04),
1750 NULL);
1751 if (n) {
1752 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1753 goto bail1;
1754 }
1755
1756 o = lws_cose_validate_results(cps);
1757 if (o->count != 1) {
1758 lwsl_err("%s: count %d\n", __func__, o->count);
1759 goto bail1;
1760 }
1761
1762 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1763 if (!res->result) {
1764 lwsl_err("%s: result wrongly succeeds\n", __func__);
1765 goto bail1;
1766 }
1767
1768 lws_cose_validate_destroy(&cps);
1769 lws_cose_key_set_destroy(&set);
1770
1771 /*
1772 * valid HMAC sign with implicit HS256 key, HS256/64
1773 */
1774
1775 lwsl_user("%s: hmac-examples/enc-05\n", __func__);
1776
1777 lws_dll2_owner_clear(&set);
1778 if (!lws_cose_key_import(&set, NULL, NULL, key3.set, key3.len)) {
1779 lwsl_notice("%s: key import fail\n", __func__);
1780 return 1;
1781 }
1782
1783 info.sigtype = SIGTYPE_MAC0;
1784 cps = lws_cose_validate_create(&info);
1785 if (!cps) {
1786 lwsl_notice("%s: sign_val_create fail\n", __func__);
1787 goto bail;
1788 }
1789
1790 n = lws_cose_validate_chunk(cps, enc_hmac_05, sizeof(enc_hmac_05),
1791 NULL);
1792 if (n) {
1793 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1794 goto bail1;
1795 }
1796
1797 o = lws_cose_validate_results(cps);
1798 if (o->count != 1) {
1799 lwsl_err("%s: count %d\n", __func__, o->count);
1800 goto bail1;
1801 }
1802
1803 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1804 if (res->result) {
1805 lwsl_err("%s: result is fail\n", __func__);
1806 goto bail1;
1807 }
1808
1809 lws_cose_validate_destroy(&cps);
1810 lws_cose_key_set_destroy(&set);
1811 #if 0
1812 /*
1813 * valid Ed25519 signature with countersignature from same key + alg
1814 */
1815
1816 lwsl_user("%s: countersign/sign-01\n", __func__);
1817
1818 lws_dll2_owner_clear(&set);
1819 if (!lws_cose_key_import(&set, NULL, NULL, key10.set, key10.len)) {
1820 lwsl_notice("%s: key import fail\n", __func__);
1821 return 1;
1822 }
1823
1824 info.sigtype = SIGTYPE_COUNTERSIGNED;
1825 cps = lws_cose_validate_create(&info);
1826 if (!cps) {
1827 lwsl_notice("%s: sign_val_create fail\n", __func__);
1828 goto bail;
1829 }
1830
1831 n = lws_cose_validate_chunk(cps, countersign_sign_01,
1832 sizeof(countersign_sign_01), NULL);
1833 if (n) {
1834 lwsl_notice("%s: sign_val_chunk failed\n", __func__);
1835 goto bail1;
1836 }
1837
1838 o = lws_cose_validate_results(cps);
1839 if (o->count != 1) {
1840 lwsl_err("%s: result count %d\n", __func__, o->count);
1841 goto bail1;
1842 }
1843
1844 res = lws_container_of(o->head, lws_cose_validate_res_t, list);
1845 if (res->result) {
1846 lwsl_err("%s: result is fail\n", __func__);
1847 goto bail1;
1848 }
1849
1850 lws_cose_validate_destroy(&cps);
1851 lws_cose_key_set_destroy(&set);
1852 #endif
1853
1854 return 0;
1855
1856 bail1:
1857 lws_cose_validate_destroy(&cps);
1858 bail:
1859 lws_cose_key_set_destroy(&set);
1860
1861 return 1;
1862 }
1863