summaryrefslogtreecommitdiff
path: root/src/regress/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib')
-rw-r--r--src/regress/lib/libssl/bytestring/bytestringtest.c1178
1 files changed, 596 insertions, 582 deletions
diff --git a/src/regress/lib/libssl/bytestring/bytestringtest.c b/src/regress/lib/libssl/bytestring/bytestringtest.c
index 92e33c02e7..ba768e4bb0 100644
--- a/src/regress/lib/libssl/bytestring/bytestringtest.c
+++ b/src/regress/lib/libssl/bytestring/bytestringtest.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bytestringtest.c,v 1.1 2015/02/06 09:36:16 doug Exp $ */ 1/* $OpenBSD: bytestringtest.c,v 1.2 2015/02/06 22:22:33 doug Exp $ */
2/* 2/*
3 * Copyright (c) 2014, Google Inc. 3 * Copyright (c) 2014, Google Inc.
4 * 4 *
@@ -25,631 +25,645 @@
25/* This is from <openssl/base.h> in boringssl */ 25/* This is from <openssl/base.h> in boringssl */
26#define OPENSSL_U64(x) x##ULL 26#define OPENSSL_U64(x) x##ULL
27 27
28static int test_skip(void) { 28static int
29 static const uint8_t kData[] = {1, 2, 3}; 29test_skip(void)
30 CBS data; 30{
31 31 static const uint8_t kData[] = {1, 2, 3};
32 CBS_init(&data, kData, sizeof(kData)); 32 CBS data;
33 return CBS_len(&data) == 3 && 33
34 CBS_skip(&data, 1) && 34 CBS_init(&data, kData, sizeof(kData));
35 CBS_len(&data) == 2 && 35 return CBS_len(&data) == 3 &&
36 CBS_skip(&data, 2) && 36 CBS_skip(&data, 1) &&
37 CBS_len(&data) == 0 && 37 CBS_len(&data) == 2 &&
38 !CBS_skip(&data, 1); 38 CBS_skip(&data, 2) &&
39 CBS_len(&data) == 0 &&
40 !CBS_skip(&data, 1);
39} 41}
40 42
41static int test_get_u(void) { 43static int
42 static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 44test_get_u(void)
43 uint8_t u8; 45{
44 uint16_t u16; 46 static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
45 uint32_t u32; 47 uint8_t u8;
46 CBS data; 48 uint16_t u16;
47 49 uint32_t u32;
48 CBS_init(&data, kData, sizeof(kData)); 50 CBS data;
49 return CBS_get_u8(&data, &u8) && 51
50 u8 == 1 && 52 CBS_init(&data, kData, sizeof(kData));
51 CBS_get_u16(&data, &u16) && 53 return CBS_get_u8(&data, &u8) &&
52 u16 == 0x203 && 54 u8 == 1 &&
53 CBS_get_u24(&data, &u32) && 55 CBS_get_u16(&data, &u16) &&
54 u32 == 0x40506 && 56 u16 == 0x203 &&
55 CBS_get_u32(&data, &u32) && 57 CBS_get_u24(&data, &u32) &&
56 u32 == 0x708090a && 58 u32 == 0x40506 &&
57 !CBS_get_u8(&data, &u8); 59 CBS_get_u32(&data, &u32) &&
60 u32 == 0x708090a &&
61 !CBS_get_u8(&data, &u8);
58} 62}
59 63
60static int test_get_prefixed(void) { 64static int
61 static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1}; 65test_get_prefixed(void)
62 uint8_t u8; 66{
63 uint16_t u16; 67 static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1};
64 uint32_t u32; 68 uint8_t u8;
65 CBS data, prefixed; 69 uint16_t u16;
66 70 uint32_t u32;
67 CBS_init(&data, kData, sizeof(kData)); 71 CBS data, prefixed;
68 return CBS_get_u8_length_prefixed(&data, &prefixed) && 72
69 CBS_len(&prefixed) == 1 && 73 CBS_init(&data, kData, sizeof(kData));
70 CBS_get_u8(&prefixed, &u8) && 74 return CBS_get_u8_length_prefixed(&data, &prefixed) &&
71 u8 == 2 && 75 CBS_len(&prefixed) == 1 &&
72 CBS_get_u16_length_prefixed(&data, &prefixed) && 76 CBS_get_u8(&prefixed, &u8) &&
73 CBS_len(&prefixed) == 2 && 77 u8 == 2 &&
74 CBS_get_u16(&prefixed, &u16) && 78 CBS_get_u16_length_prefixed(&data, &prefixed) &&
75 u16 == 0x304 && 79 CBS_len(&prefixed) == 2 &&
76 CBS_get_u24_length_prefixed(&data, &prefixed) && 80 CBS_get_u16(&prefixed, &u16) &&
77 CBS_len(&prefixed) == 3 && 81 u16 == 0x304 &&
78 CBS_get_u24(&prefixed, &u32) && 82 CBS_get_u24_length_prefixed(&data, &prefixed) &&
79 u32 == 0x30201; 83 CBS_len(&prefixed) == 3 &&
84 CBS_get_u24(&prefixed, &u32) &&
85 u32 == 0x30201;
80} 86}
81 87
82static int test_get_prefixed_bad(void) { 88static int
83 static const uint8_t kData1[] = {2, 1}; 89test_get_prefixed_bad(void)
84 static const uint8_t kData2[] = {0, 2, 1}; 90{
85 static const uint8_t kData3[] = {0, 0, 2, 1}; 91 static const uint8_t kData1[] = {2, 1};
86 CBS data, prefixed; 92 static const uint8_t kData2[] = {0, 2, 1};
93 static const uint8_t kData3[] = {0, 0, 2, 1};
94 CBS data, prefixed;
87 95
88 CBS_init(&data, kData1, sizeof(kData1)); 96 CBS_init(&data, kData1, sizeof(kData1));
89 if (CBS_get_u8_length_prefixed(&data, &prefixed)) { 97 if (CBS_get_u8_length_prefixed(&data, &prefixed))
90 return 0; 98 return 0;
91 }
92 99
93 CBS_init(&data, kData2, sizeof(kData2)); 100 CBS_init(&data, kData2, sizeof(kData2));
94 if (CBS_get_u16_length_prefixed(&data, &prefixed)) { 101 if (CBS_get_u16_length_prefixed(&data, &prefixed))
95 return 0; 102 return 0;
96 }
97 103
98 CBS_init(&data, kData3, sizeof(kData3)); 104 CBS_init(&data, kData3, sizeof(kData3));
99 if (CBS_get_u24_length_prefixed(&data, &prefixed)) { 105 if (CBS_get_u24_length_prefixed(&data, &prefixed))
100 return 0; 106 return 0;
101 }
102 107
103 return 1; 108 return 1;
104} 109}
105 110
106static int test_get_asn1(void) { 111static int
107 static const uint8_t kData1[] = {0x30, 2, 1, 2}; 112test_get_asn1(void)
108 static const uint8_t kData2[] = {0x30, 3, 1, 2}; 113{
109 static const uint8_t kData3[] = {0x30, 0x80}; 114 static const uint8_t kData1[] = {0x30, 2, 1, 2};
110 static const uint8_t kData4[] = {0x30, 0x81, 1, 1}; 115 static const uint8_t kData2[] = {0x30, 3, 1, 2};
111 static const uint8_t kData5[] = {0x30, 0x82, 0, 1, 1}; 116 static const uint8_t kData3[] = {0x30, 0x80};
112 static const uint8_t kData6[] = {0xa1, 3, 0x4, 1, 1}; 117 static const uint8_t kData4[] = {0x30, 0x81, 1, 1};
113 static const uint8_t kData7[] = {0xa1, 3, 0x4, 2, 1}; 118 static const uint8_t kData5[] = {0x30, 0x82, 0, 1, 1};
114 static const uint8_t kData8[] = {0xa1, 3, 0x2, 1, 1}; 119 static const uint8_t kData6[] = {0xa1, 3, 0x4, 1, 1};
115 static const uint8_t kData9[] = {0xa1, 3, 0x2, 1, 0xff}; 120 static const uint8_t kData7[] = {0xa1, 3, 0x4, 2, 1};
116 121 static const uint8_t kData8[] = {0xa1, 3, 0x2, 1, 1};
117 CBS data, contents; 122 static const uint8_t kData9[] = {0xa1, 3, 0x2, 1, 0xff};
118 int present; 123
119 uint64_t value; 124 CBS data, contents;
120 125 int present;
121 CBS_init(&data, kData1, sizeof(kData1)); 126 uint64_t value;
122 if (CBS_peek_asn1_tag(&data, 0x1) || 127
123 !CBS_peek_asn1_tag(&data, 0x30)) { 128 CBS_init(&data, kData1, sizeof(kData1));
124 return 0; 129 if (CBS_peek_asn1_tag(&data, 0x1) || !CBS_peek_asn1_tag(&data, 0x30))
125 } 130 return 0;
126 if (!CBS_get_asn1(&data, &contents, 0x30) || 131
127 CBS_len(&contents) != 2 || 132 if (!CBS_get_asn1(&data, &contents, 0x30) ||
128 memcmp(CBS_data(&contents), "\x01\x02", 2) != 0) { 133 CBS_len(&contents) != 2 ||
129 return 0; 134 memcmp(CBS_data(&contents), "\x01\x02", 2) != 0)
130 } 135 return 0;
131 136
132 CBS_init(&data, kData2, sizeof(kData2)); 137 CBS_init(&data, kData2, sizeof(kData2));
133 /* data is truncated */ 138 /* data is truncated */
134 if (CBS_get_asn1(&data, &contents, 0x30)) { 139 if (CBS_get_asn1(&data, &contents, 0x30))
135 return 0; 140 return 0;
136 } 141
137 142 CBS_init(&data, kData3, sizeof(kData3));
138 CBS_init(&data, kData3, sizeof(kData3)); 143 /* zero byte length of length */
139 /* zero byte length of length */ 144 if (CBS_get_asn1(&data, &contents, 0x30))
140 if (CBS_get_asn1(&data, &contents, 0x30)) { 145 return 0;
141 return 0; 146
142 } 147 CBS_init(&data, kData4, sizeof(kData4));
143 148 /* long form mistakenly used. */
144 CBS_init(&data, kData4, sizeof(kData4)); 149 if (CBS_get_asn1(&data, &contents, 0x30))
145 /* long form mistakenly used. */ 150 return 0;
146 if (CBS_get_asn1(&data, &contents, 0x30)) { 151
147 return 0; 152 CBS_init(&data, kData5, sizeof(kData5));
148 } 153 /* length takes too many bytes. */
149 154 if (CBS_get_asn1(&data, &contents, 0x30))
150 CBS_init(&data, kData5, sizeof(kData5)); 155 return 0;
151 /* length takes too many bytes. */ 156
152 if (CBS_get_asn1(&data, &contents, 0x30)) { 157 CBS_init(&data, kData1, sizeof(kData1));
153 return 0; 158 /* wrong tag. */
154 } 159 if (CBS_get_asn1(&data, &contents, 0x31))
155 160 return 0;
156 CBS_init(&data, kData1, sizeof(kData1)); 161
157 /* wrong tag. */ 162 CBS_init(&data, NULL, 0);
158 if (CBS_get_asn1(&data, &contents, 0x31)) { 163 /* peek at empty data. */
159 return 0; 164 if (CBS_peek_asn1_tag(&data, 0x30))
160 } 165 return 0;
161 166
162 CBS_init(&data, NULL, 0); 167 CBS_init(&data, NULL, 0);
163 /* peek at empty data. */ 168 /* optional elements at empty data. */
164 if (CBS_peek_asn1_tag(&data, 0x30)) { 169 if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) ||
165 return 0; 170 present ||
166 } 171 !CBS_get_optional_asn1_octet_string(&data, &contents, &present,
167 172 0xa0) ||
168 CBS_init(&data, NULL, 0); 173 present ||
169 /* optional elements at empty data. */ 174 CBS_len(&contents) != 0 ||
170 if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) || 175 !CBS_get_optional_asn1_octet_string(&data, &contents, NULL, 0xa0) ||
171 present || 176 CBS_len(&contents) != 0 ||
172 !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) || 177 !CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) ||
173 present || 178 value != 42)
174 CBS_len(&contents) != 0 || 179 return 0;
175 !CBS_get_optional_asn1_octet_string(&data, &contents, NULL, 0xa0) || 180
176 CBS_len(&contents) != 0 || 181 CBS_init(&data, kData6, sizeof(kData6));
177 !CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) || 182 /* optional element. */
178 value != 42) { 183 if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) ||
179 return 0; 184 present ||
180 } 185 !CBS_get_optional_asn1(&data, &contents, &present, 0xa1) ||
181 186 !present ||
182 CBS_init(&data, kData6, sizeof(kData6)); 187 CBS_len(&contents) != 3 ||
183 /* optional element. */ 188 memcmp(CBS_data(&contents), "\x04\x01\x01", 3) != 0)
184 if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) || 189 return 0;
185 present || 190
186 !CBS_get_optional_asn1(&data, &contents, &present, 0xa1) || 191 CBS_init(&data, kData6, sizeof(kData6));
187 !present || 192 /* optional octet string. */
188 CBS_len(&contents) != 3 || 193 if (!CBS_get_optional_asn1_octet_string(&data, &contents, &present,
189 memcmp(CBS_data(&contents), "\x04\x01\x01", 3) != 0) { 194 0xa0) ||
190 return 0; 195 present ||
191 } 196 CBS_len(&contents) != 0 ||
192 197 !CBS_get_optional_asn1_octet_string(&data, &contents, &present,
193 CBS_init(&data, kData6, sizeof(kData6)); 198 0xa1) ||
194 /* optional octet string. */ 199 !present ||
195 if (!CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) || 200 CBS_len(&contents) != 1 ||
196 present || 201 CBS_data(&contents)[0] != 1)
197 CBS_len(&contents) != 0 || 202 return 0;
198 !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1) || 203
199 !present || 204 CBS_init(&data, kData7, sizeof(kData7));
200 CBS_len(&contents) != 1 || 205 /* invalid optional octet string. */
201 CBS_data(&contents)[0] != 1) { 206 if (CBS_get_optional_asn1_octet_string(&data, &contents, &present,
202 return 0; 207 0xa1))
203 } 208 return 0;
204 209
205 CBS_init(&data, kData7, sizeof(kData7)); 210 CBS_init(&data, kData8, sizeof(kData8));
206 /* invalid optional octet string. */ 211 /* optional octet string. */
207 if (CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1)) { 212 if (!CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) ||
208 return 0; 213 value != 42 ||
209 } 214 !CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42) ||
210 215 value != 1)
211 CBS_init(&data, kData8, sizeof(kData8)); 216 return 0;
212 /* optional octet string. */ 217
213 if (!CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) || 218 CBS_init(&data, kData9, sizeof(kData9));
214 value != 42 || 219 /* invalid optional integer. */
215 !CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42) || 220 if (CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42))
216 value != 1) { 221 return 0;
217 return 0; 222
218 } 223 return 1;
219
220 CBS_init(&data, kData9, sizeof(kData9));
221 /* invalid optional integer. */
222 if (CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42)) {
223 return 0;
224 }
225
226 return 1;
227} 224}
228 225
229static int test_get_optional_asn1_bool(void) { 226static int
230 CBS data; 227test_get_optional_asn1_bool(void)
231 int val; 228{
232 229 CBS data;
233 static const uint8_t kTrue[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0xff}; 230 int val;
234 static const uint8_t kFalse[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x00}; 231
235 static const uint8_t kInvalid[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x01}; 232 static const uint8_t kTrue[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0xff};
236 233 static const uint8_t kFalse[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x00};
237 CBS_init(&data, NULL, 0); 234 static const uint8_t kInvalid[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x01};
238 val = 2; 235
239 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) || 236 CBS_init(&data, NULL, 0);
240 val != 0) { 237 val = 2;
241 return 0; 238 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) || val != 0)
242 } 239 return 0;
243 240
244 CBS_init(&data, kTrue, sizeof(kTrue)); 241 CBS_init(&data, kTrue, sizeof(kTrue));
245 val = 2; 242 val = 2;
246 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) || 243 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) || val != 1)
247 val != 1) { 244 return 0;
248 return 0; 245
249 } 246 CBS_init(&data, kFalse, sizeof(kFalse));
250 247 val = 2;
251 CBS_init(&data, kFalse, sizeof(kFalse)); 248 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1) || val != 0)
252 val = 2; 249 return 0;
253 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1) || 250
254 val != 0) { 251 CBS_init(&data, kInvalid, sizeof(kInvalid));
255 return 0; 252 if (CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1))
256 } 253 return 0;
257 254
258 CBS_init(&data, kInvalid, sizeof(kInvalid)); 255 return 1;
259 if (CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1)) {
260 return 0;
261 }
262
263 return 1;
264} 256}
265 257
266static int test_cbb_basic(void) { 258static int
267 static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8}; 259test_cbb_basic(void)
268 uint8_t *buf; 260{
269 size_t buf_len; 261 static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8};
270 int ok; 262 uint8_t *buf;
271 CBB cbb; 263 size_t buf_len;
272 264 int ok;
273 if (!CBB_init(&cbb, 100)) { 265 CBB cbb;
274 return 0; 266
275 } 267 if (!CBB_init(&cbb, 100))
276 CBB_cleanup(&cbb); 268 return 0;
277 269
278 if (!CBB_init(&cbb, 0) || 270 CBB_cleanup(&cbb);
279 !CBB_add_u8(&cbb, 1) || 271
280 !CBB_add_u16(&cbb, 0x203) || 272 if (!CBB_init(&cbb, 0) ||
281 !CBB_add_u24(&cbb, 0x40506) || 273 !CBB_add_u8(&cbb, 1) ||
282 !CBB_add_bytes(&cbb, (const uint8_t*) "\x07\x08", 2) || 274 !CBB_add_u16(&cbb, 0x203) ||
283 !CBB_finish(&cbb, &buf, &buf_len)) { 275 !CBB_add_u24(&cbb, 0x40506) ||
284 return 0; 276 !CBB_add_bytes(&cbb, (const uint8_t*) "\x07\x08", 2) ||
285 } 277 !CBB_finish(&cbb, &buf, &buf_len))
286 278 return 0;
287 ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0; 279
288 free(buf); 280 ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len)
289 return ok; 281 == 0;
282 free(buf);
283 return ok;
290} 284}
291 285
292static int test_cbb_fixed(void) { 286static int
293 CBB cbb; 287test_cbb_fixed(void)
294 uint8_t buf[1]; 288{
295 uint8_t *out_buf; 289 CBB cbb;
296 size_t out_size; 290 uint8_t buf[1];
297 291 uint8_t *out_buf;
298 if (!CBB_init_fixed(&cbb, NULL, 0) || 292 size_t out_size;
299 CBB_add_u8(&cbb, 1) || 293
300 !CBB_finish(&cbb, &out_buf, &out_size) || 294 if (!CBB_init_fixed(&cbb, NULL, 0) ||
301 out_buf != NULL || 295 CBB_add_u8(&cbb, 1) ||
302 out_size != 0) { 296 !CBB_finish(&cbb, &out_buf, &out_size) ||
303 return 0; 297 out_buf != NULL ||
304 } 298 out_size != 0)
305 299 return 0;
306 if (!CBB_init_fixed(&cbb, buf, 1) || 300
307 !CBB_add_u8(&cbb, 1) || 301 if (!CBB_init_fixed(&cbb, buf, 1) ||
308 CBB_add_u8(&cbb, 2) || 302 !CBB_add_u8(&cbb, 1) ||
309 !CBB_finish(&cbb, &out_buf, &out_size) || 303 CBB_add_u8(&cbb, 2) ||
310 out_buf != buf || 304 !CBB_finish(&cbb, &out_buf, &out_size) ||
311 out_size != 1 || 305 out_buf != buf ||
312 buf[0] != 1) { 306 out_size != 1 ||
313 return 0; 307 buf[0] != 1)
314 } 308 return 0;
315 309
316 return 1; 310 return 1;
317} 311}
318 312
319static int test_cbb_finish_child(void) { 313static int
320 CBB cbb, child; 314test_cbb_finish_child(void)
321 uint8_t *out_buf; 315{
322 size_t out_size; 316 CBB cbb, child;
323 317 uint8_t *out_buf;
324 if (!CBB_init(&cbb, 16) || 318 size_t out_size;
325 !CBB_add_u8_length_prefixed(&cbb, &child) || 319
326 CBB_finish(&child, &out_buf, &out_size) || 320 if (!CBB_init(&cbb, 16) ||
327 !CBB_finish(&cbb, &out_buf, &out_size) || 321 !CBB_add_u8_length_prefixed(&cbb, &child) ||
328 out_size != 1 || 322 CBB_finish(&child, &out_buf, &out_size) ||
329 out_buf[0] != 0) { 323 !CBB_finish(&cbb, &out_buf, &out_size) ||
330 return 0; 324 out_size != 1 ||
331 } 325 out_buf[0] != 0)
332 326 return 0;
333 free(out_buf); 327
334 return 1; 328 free(out_buf);
329 return 1;
335} 330}
336 331
337static int test_cbb_prefixed(void) { 332static int
338 static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3, 333test_cbb_prefixed(void)
339 4, 5, 6, 5, 4, 1, 0, 1, 2}; 334{
340 uint8_t *buf; 335 static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3,
341 size_t buf_len; 336 4, 5, 6, 5, 4, 1, 0, 1, 2};
342 CBB cbb, contents, inner_contents, inner_inner_contents; 337 uint8_t *buf;
343 int ok; 338 size_t buf_len;
344 339 CBB cbb, contents, inner_contents, inner_inner_contents;
345 if (!CBB_init(&cbb, 0) || 340 int ok;
346 !CBB_add_u8_length_prefixed(&cbb, &contents) || 341
347 !CBB_add_u8_length_prefixed(&cbb, &contents) || 342 if (!CBB_init(&cbb, 0) ||
348 !CBB_add_u8(&contents, 1) || 343 !CBB_add_u8_length_prefixed(&cbb, &contents) ||
349 !CBB_add_u16_length_prefixed(&cbb, &contents) || 344 !CBB_add_u8_length_prefixed(&cbb, &contents) ||
350 !CBB_add_u16(&contents, 0x203) || 345 !CBB_add_u8(&contents, 1) ||
351 !CBB_add_u24_length_prefixed(&cbb, &contents) || 346 !CBB_add_u16_length_prefixed(&cbb, &contents) ||
352 !CBB_add_u24(&contents, 0x40506) || 347 !CBB_add_u16(&contents, 0x203) ||
353 !CBB_add_u8_length_prefixed(&cbb, &contents) || 348 !CBB_add_u24_length_prefixed(&cbb, &contents) ||
354 !CBB_add_u8_length_prefixed(&contents, &inner_contents) || 349 !CBB_add_u24(&contents, 0x40506) ||
355 !CBB_add_u8(&inner_contents, 1) || 350 !CBB_add_u8_length_prefixed(&cbb, &contents) ||
356 !CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents) || 351 !CBB_add_u8_length_prefixed(&contents, &inner_contents) ||
357 !CBB_add_u8(&inner_inner_contents, 2) || 352 !CBB_add_u8(&inner_contents, 1) ||
358 !CBB_finish(&cbb, &buf, &buf_len)) { 353 !CBB_add_u16_length_prefixed(&inner_contents,
359 return 0; 354 &inner_inner_contents) ||
360 } 355 !CBB_add_u8(&inner_inner_contents, 2) ||
361 356 !CBB_finish(&cbb, &buf, &buf_len))
362 ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0; 357 return 0;
363 free(buf); 358
364 return ok; 359 ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len)
360 == 0;
361 free(buf);
362 return ok;
365} 363}
366 364
367static int test_cbb_misuse(void) { 365static int
368 CBB cbb, child, contents; 366test_cbb_misuse(void)
369 uint8_t *buf; 367{
370 size_t buf_len; 368 CBB cbb, child, contents;
371 369 uint8_t *buf;
372 if (!CBB_init(&cbb, 0) || 370 size_t buf_len;
373 !CBB_add_u8_length_prefixed(&cbb, &child) || 371
374 !CBB_add_u8(&child, 1) || 372 if (!CBB_init(&cbb, 0) ||
375 !CBB_add_u8(&cbb, 2)) { 373 !CBB_add_u8_length_prefixed(&cbb, &child) ||
376 return 0; 374 !CBB_add_u8(&child, 1) ||
377 } 375 !CBB_add_u8(&cbb, 2))
378 376 return 0;
379 /* Since we wrote to |cbb|, |child| is now invalid and attempts to write to 377
380 * it should fail. */ 378 /*
381 if (CBB_add_u8(&child, 1) || 379 * Since we wrote to |cbb|, |child| is now invalid and attempts to write
382 CBB_add_u16(&child, 1) || 380 * to it should fail.
383 CBB_add_u24(&child, 1) || 381 */
384 CBB_add_u8_length_prefixed(&child, &contents) || 382 if (CBB_add_u8(&child, 1) ||
385 CBB_add_u16_length_prefixed(&child, &contents) || 383 CBB_add_u16(&child, 1) ||
386 CBB_add_asn1(&child, &contents, 1) || 384 CBB_add_u24(&child, 1) ||
387 CBB_add_bytes(&child, (const uint8_t*) "a", 1)) { 385 CBB_add_u8_length_prefixed(&child, &contents) ||
388 fprintf(stderr, "CBB operation on invalid CBB did not fail.\n"); 386 CBB_add_u16_length_prefixed(&child, &contents) ||
389 return 0; 387 CBB_add_asn1(&child, &contents, 1) ||
390 } 388 CBB_add_bytes(&child, (const uint8_t*) "a", 1)) {
391 389 fprintf(stderr, "CBB operation on invalid CBB did not fail.\n");
392 if (!CBB_finish(&cbb, &buf, &buf_len) || 390 return 0;
393 buf_len != 3 || 391 }
394 memcmp(buf, "\x01\x01\x02", 3) != 0) { 392
395 return 0; 393 if (!CBB_finish(&cbb, &buf, &buf_len) || buf_len != 3 ||
396 } 394 memcmp(buf, "\x01\x01\x02", 3) != 0)
397 395 return 0;
398 free(buf); 396
399 397 free(buf);
400 return 1; 398
399 return 1;
401} 400}
402 401
403static int test_cbb_asn1(void) { 402static int
404 static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3}; 403test_cbb_asn1(void)
405 uint8_t *buf, *test_data; 404{
406 size_t buf_len; 405 static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3};
407 CBB cbb, contents, inner_contents; 406 uint8_t *buf, *test_data;
408 407 size_t buf_len;
409 if (!CBB_init(&cbb, 0) || 408 CBB cbb, contents, inner_contents;
410 !CBB_add_asn1(&cbb, &contents, 0x30) || 409
411 !CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03", 3) || 410 if (!CBB_init(&cbb, 0) ||
412 !CBB_finish(&cbb, &buf, &buf_len)) { 411 !CBB_add_asn1(&cbb, &contents, 0x30) ||
413 return 0; 412 !CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03", 3) ||
414 } 413 !CBB_finish(&cbb, &buf, &buf_len))
415 414 return 0;
416 if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) != 0) { 415
417 return 0; 416 if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len)
418 } 417 != 0)
419 free(buf); 418 return 0;
420 419
421 test_data = malloc(100000); 420 free(buf);
422 memset(test_data, 0x42, 100000); 421
423 422 test_data = malloc(100000);
424 if (!CBB_init(&cbb, 0) || 423 memset(test_data, 0x42, 100000);
425 !CBB_add_asn1(&cbb, &contents, 0x30) || 424
426 !CBB_add_bytes(&contents, test_data, 130) || 425 if (!CBB_init(&cbb, 0) ||
427 !CBB_finish(&cbb, &buf, &buf_len)) { 426 !CBB_add_asn1(&cbb, &contents, 0x30) ||
428 return 0; 427 !CBB_add_bytes(&contents, test_data, 130) ||
429 } 428 !CBB_finish(&cbb, &buf, &buf_len))
430 429 return 0;
431 if (buf_len != 3 + 130 || 430
432 memcmp(buf, "\x30\x81\x82", 3) != 0 || 431 if (buf_len != 3 + 130 ||
433 memcmp(buf + 3, test_data, 130) != 0) { 432 memcmp(buf, "\x30\x81\x82", 3) != 0 ||
434 return 0; 433 memcmp(buf + 3, test_data, 130) != 0) {
435 } 434 return 0;
436 free(buf); 435 }
437 436 free(buf);
438 if (!CBB_init(&cbb, 0) || 437
439 !CBB_add_asn1(&cbb, &contents, 0x30) || 438 if (!CBB_init(&cbb, 0) ||
440 !CBB_add_bytes(&contents, test_data, 1000) || 439 !CBB_add_asn1(&cbb, &contents, 0x30) ||
441 !CBB_finish(&cbb, &buf, &buf_len)) { 440 !CBB_add_bytes(&contents, test_data, 1000) ||
442 return 0; 441 !CBB_finish(&cbb, &buf, &buf_len))
443 } 442 return 0;
444 443
445 if (buf_len != 4 + 1000 || 444 if (buf_len != 4 + 1000 ||
446 memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 || 445 memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 ||
447 memcmp(buf + 4, test_data, 1000)) { 446 memcmp(buf + 4, test_data, 1000)) {
448 return 0; 447 return 0;
449 } 448 }
450 free(buf); 449 free(buf);
451 450
452 if (!CBB_init(&cbb, 0) || 451 if (!CBB_init(&cbb, 0) ||
453 !CBB_add_asn1(&cbb, &contents, 0x30) || 452 !CBB_add_asn1(&cbb, &contents, 0x30) ||
454 !CBB_add_asn1(&contents, &inner_contents, 0x30) || 453 !CBB_add_asn1(&contents, &inner_contents, 0x30) ||
455 !CBB_add_bytes(&inner_contents, test_data, 100000) || 454 !CBB_add_bytes(&inner_contents, test_data, 100000) ||
456 !CBB_finish(&cbb, &buf, &buf_len)) { 455 !CBB_finish(&cbb, &buf, &buf_len))
457 return 0; 456 return 0;
458 } 457
459 458 if (buf_len != 5 + 5 + 100000 ||
460 if (buf_len != 5 + 5 + 100000 || 459 memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 ||
461 memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 || 460 memcmp(buf + 10, test_data, 100000))
462 memcmp(buf + 10, test_data, 100000)) { 461 return 0;
463 return 0; 462
464 } 463 free(buf);
465 free(buf); 464 free(test_data);
466 465
467 free(test_data); 466 return 1;
468 return 1;
469} 467}
470 468
471static int do_ber_convert(const char *name, 469static int
472 const uint8_t *der_expected, size_t der_len, 470do_ber_convert(const char *name, const uint8_t *der_expected, size_t der_len,
473 const uint8_t *ber, size_t ber_len) { 471 const uint8_t *ber, size_t ber_len)
474 CBS in; 472{
475 uint8_t *out; 473 CBS in;
476 size_t out_len; 474 uint8_t *out;
477 475 size_t out_len;
478 CBS_init(&in, ber, ber_len); 476
479 if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) { 477 CBS_init(&in, ber, ber_len);
480 fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name); 478 if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) {
481 return 0; 479 fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name);
482 } 480 return 0;
483 481 }
484 if (out == NULL) { 482
485 if (ber_len != der_len || 483 if (out == NULL) {
486 memcmp(der_expected, ber, ber_len) != 0) { 484 if (ber_len != der_len ||
487 fprintf(stderr, "%s: incorrect unconverted result.\n", name); 485 memcmp(der_expected, ber, ber_len) != 0) {
488 return 0; 486 fprintf(stderr, "%s: incorrect unconverted result.\n",
489 } 487 name);
490 488 return 0;
491 return 1; 489 }
492 } 490
493 491 return 1;
494 if (out_len != der_len || 492 }
495 memcmp(out, der_expected, der_len) != 0) { 493
496 fprintf(stderr, "%s: incorrect converted result.\n", name); 494 if (out_len != der_len || memcmp(out, der_expected, der_len) != 0) {
497 return 0; 495 fprintf(stderr, "%s: incorrect converted result.\n", name);
498 } 496 return 0;
499 497 }
500 free(out); 498
501 return 1; 499 free(out);
500 return 1;
502} 501}
503 502
504static int test_ber_convert(void) { 503static int
505 static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00}; 504test_ber_convert(void)
506 505{
507 /* kIndefBER contains a SEQUENCE with an indefinite length. */ 506 static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00};
508 static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00}; 507
509 static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02}; 508 /* kIndefBER contains a SEQUENCE with an indefinite length. */
510 509 static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00,
511 /* kOctetStringBER contains an indefinite length OCTETSTRING with two parts. 510 0x00};
512 * These parts need to be concatenated in DER form. */ 511 static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02};
513 static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0, 1, 512
514 0x04, 0x02, 2, 3, 0x00, 0x00}; 513 /*
515 static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3}; 514 * kOctetStringBER contains an indefinite length OCTETSTRING with two
516 515 * parts. These parts need to be concatenated in DER form.
517 /* kNSSBER is part of a PKCS#12 message generated by NSS that uses indefinite 516 */
518 * length elements extensively. */ 517 static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0,
519 static const uint8_t kNSSBER[] = { 518 1, 0x04, 0x02, 2, 3, 0x00, 0x00};
520 0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48, 519 static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3};
521 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04, 520
522 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39, 521 /*
523 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 522 * kNSSBER is part of a PKCS#12 message generated by NSS that uses
524 0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 523 * indefinite length elements extensively.
525 0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 524 */
526 0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 525 static const uint8_t kNSSBER[] = {
527 0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00, 526 0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86,
528 }; 527 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80,
529 528 0x04, 0x04, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
530 static const uint8_t kNSSDER[] = { 529 0x00, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
531 0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86, 530 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66,
532 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04, 531 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d,
533 0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06, 532 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38, 0x62, 0xc6, 0x44,
534 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84, 533 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0, 0x6e, 0x10, 0x9b,
535 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8, 534 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00,
536 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38, 535 };
537 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0, 536
538 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 537 static const uint8_t kNSSDER[] = {
539 }; 538 0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86,
540 539 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04,
541 return do_ber_convert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER), 540 0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06,
542 kSimpleBER, sizeof(kSimpleBER)) && 541 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84,
543 do_ber_convert("kIndefBER", kIndefDER, sizeof(kIndefDER), kIndefBER, 542 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8,
544 sizeof(kIndefBER)) && 543 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38,
545 do_ber_convert("kOctetStringBER", kOctetStringDER, 544 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0,
546 sizeof(kOctetStringDER), kOctetStringBER, 545 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0,
547 sizeof(kOctetStringBER)) && 546 };
548 do_ber_convert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER, 547
549 sizeof(kNSSBER)); 548 return do_ber_convert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER),
549 kSimpleBER, sizeof(kSimpleBER)) &&
550 do_ber_convert("kIndefBER", kIndefDER, sizeof(kIndefDER), kIndefBER,
551 sizeof(kIndefBER)) &&
552 do_ber_convert("kOctetStringBER", kOctetStringDER,
553 sizeof(kOctetStringDER), kOctetStringBER,
554 sizeof(kOctetStringBER)) &&
555 do_ber_convert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER,
556 sizeof(kNSSBER));
550} 557}
551 558
552typedef struct { 559typedef struct {
553 uint64_t value; 560 uint64_t value;
554 const char *encoding; 561 const char *encoding;
555 size_t encoding_len; 562 size_t encoding_len;
556} ASN1_UINT64_TEST; 563} ASN1_UINT64_TEST;
557 564
558static const ASN1_UINT64_TEST kAsn1Uint64Tests[] = { 565static const ASN1_UINT64_TEST kAsn1Uint64Tests[] = {
559 {0, "\x02\x01\x00", 3}, 566 {0, "\x02\x01\x00", 3},
560 {1, "\x02\x01\x01", 3}, 567 {1, "\x02\x01\x01", 3},
561 {127, "\x02\x01\x7f", 3}, 568 {127, "\x02\x01\x7f", 3},
562 {128, "\x02\x02\x00\x80", 4}, 569 {128, "\x02\x02\x00\x80", 4},
563 {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7}, 570 {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7},
564 {OPENSSL_U64(0x0102030405060708), 571 {OPENSSL_U64(0x0102030405060708),
565 "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10}, 572 "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
566 {OPENSSL_U64(0xffffffffffffffff), 573 {OPENSSL_U64(0xffffffffffffffff),
567 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11}, 574 "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
568}; 575};
569 576
570typedef struct { 577typedef struct {
571 const char *encoding; 578 const char *encoding;
572 size_t encoding_len; 579 size_t encoding_len;
573} ASN1_INVALID_UINT64_TEST; 580} ASN1_INVALID_UINT64_TEST;
574 581
575static const ASN1_INVALID_UINT64_TEST kAsn1InvalidUint64Tests[] = { 582static const ASN1_INVALID_UINT64_TEST kAsn1InvalidUint64Tests[] = {
576 /* Bad tag. */ 583 /* Bad tag. */
577 {"\x03\x01\x00", 3}, 584 {"\x03\x01\x00", 3},
578 /* Empty contents. */ 585 /* Empty contents. */
579 {"\x02\x00", 2}, 586 {"\x02\x00", 2},
580 /* Negative number. */ 587 /* Negative number. */
581 {"\x02\x01\x80", 3}, 588 {"\x02\x01\x80", 3},
582 /* Overflow */ 589 /* Overflow */
583 {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11}, 590 {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11},
584}; 591};
585 592
586static int test_asn1_uint64(void) { 593static int
587 size_t i; 594test_asn1_uint64(void)
588 595{
589 for (i = 0; i < sizeof(kAsn1Uint64Tests) / sizeof(kAsn1Uint64Tests[0]); i++) { 596 size_t i;
590 const ASN1_UINT64_TEST *test = &kAsn1Uint64Tests[i]; 597
591 CBS cbs; 598 for (i = 0; i < sizeof(kAsn1Uint64Tests) / sizeof(kAsn1Uint64Tests[0]);
592 uint64_t value; 599 i++) {
593 CBB cbb; 600 const ASN1_UINT64_TEST *test = &kAsn1Uint64Tests[i];
594 uint8_t *out; 601 CBS cbs;
595 size_t len; 602 uint64_t value;
596 603 CBB cbb;
597 CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len); 604 uint8_t *out;
598 if (!CBS_get_asn1_uint64(&cbs, &value) || 605 size_t len;
599 CBS_len(&cbs) != 0 || 606
600 value != test->value) { 607 CBS_init(&cbs, (const uint8_t *)test->encoding,
601 return 0; 608 test->encoding_len);
602 } 609
603 610 if (!CBS_get_asn1_uint64(&cbs, &value) ||
604 if (!CBB_init(&cbb, 0)) { 611 CBS_len(&cbs) != 0 ||
605 return 0; 612 value != test->value)
606 } 613 return 0;
607 if (!CBB_add_asn1_uint64(&cbb, test->value) || 614
608 !CBB_finish(&cbb, &out, &len)) { 615 if (!CBB_init(&cbb, 0))
609 CBB_cleanup(&cbb); 616 return 0;
610 return 0; 617
611 } 618 if (!CBB_add_asn1_uint64(&cbb, test->value) ||
612 if (len != test->encoding_len || memcmp(out, test->encoding, len) != 0) { 619 !CBB_finish(&cbb, &out, &len)) {
613 free(out); 620 CBB_cleanup(&cbb);
614 return 0; 621 return 0;
615 } 622 }
616 free(out); 623
617 } 624 if (len != test->encoding_len || memcmp(out, test->encoding,
618 625 len) != 0) {
619 for (i = 0; 626 free(out);
620 i < sizeof(kAsn1InvalidUint64Tests) / sizeof(kAsn1InvalidUint64Tests[0]); 627 return 0;
621 i++) { 628 }
622 const ASN1_INVALID_UINT64_TEST *test = &kAsn1InvalidUint64Tests[i]; 629 free(out);
623 CBS cbs; 630 }
624 uint64_t value; 631
625 632 for (i = 0; i < sizeof(kAsn1InvalidUint64Tests)
626 CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len); 633 / sizeof(kAsn1InvalidUint64Tests[0]); i++) {
627 if (CBS_get_asn1_uint64(&cbs, &value)) { 634 const ASN1_INVALID_UINT64_TEST *test =
628 return 0; 635 &kAsn1InvalidUint64Tests[i];
629 } 636 CBS cbs;
630 } 637 uint64_t value;
631 638
632 return 1; 639 CBS_init(&cbs, (const uint8_t *)test->encoding,
640 test->encoding_len);
641 if (CBS_get_asn1_uint64(&cbs, &value))
642 return 0;
643 }
644
645 return 1;
633} 646}
634 647
635int main(void) { 648int
636 if (!test_skip() || 649main(void)
637 !test_get_u() || 650{
638 !test_get_prefixed() || 651 if (!test_skip() ||
639 !test_get_prefixed_bad() || 652 !test_get_u() ||
640 !test_get_asn1() || 653 !test_get_prefixed() ||
641 !test_cbb_basic() || 654 !test_get_prefixed_bad() ||
642 !test_cbb_fixed() || 655 !test_get_asn1() ||
643 !test_cbb_finish_child() || 656 !test_cbb_basic() ||
644 !test_cbb_misuse() || 657 !test_cbb_fixed() ||
645 !test_cbb_prefixed() || 658 !test_cbb_finish_child() ||
646 !test_cbb_asn1() || 659 !test_cbb_misuse() ||
647 !test_ber_convert() || 660 !test_cbb_prefixed() ||
648 !test_asn1_uint64() || 661 !test_cbb_asn1() ||
649 !test_get_optional_asn1_bool()) { 662 !test_ber_convert() ||
650 return 1; 663 !test_asn1_uint64() ||
651 } 664 !test_get_optional_asn1_bool())
652 665 return 1;
653 printf("PASS\n"); 666
654 return 0; 667 printf("PASS\n");
668 return 0;
655} 669}