| 1 |
/* Copyright (c) 2014, Google Inc. |
| 2 |
* |
| 3 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
* purpose with or without fee is hereby granted, provided that the above |
| 5 |
* copyright notice and this permission notice appear in all copies. |
| 6 |
* |
| 7 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 |
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 |
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 |
|
| 15 |
#if !defined(__STDC_CONSTANT_MACROS) |
| 16 |
#define __STDC_CONSTANT_MACROS |
| 17 |
#endif |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include <openssl/crypto.h> |
| 26 |
#include <openssl/bytestring.h> |
| 27 |
|
| 28 |
#include "internal.h" |
| 29 |
#include "../test/scoped_types.h" |
| 30 |
|
| 31 |
|
| 32 |
static bool TestSkip() { |
| 33 |
static const uint8_t kData[] = {1, 2, 3}; |
| 34 |
CBS data; |
| 35 |
|
| 36 |
CBS_init(&data, kData, sizeof(kData)); |
| 37 |
return CBS_len(&data) == 3 && |
| 38 |
CBS_skip(&data, 1) && |
| 39 |
CBS_len(&data) == 2 && |
| 40 |
CBS_skip(&data, 2) && |
| 41 |
CBS_len(&data) == 0 && |
| 42 |
!CBS_skip(&data, 1); |
| 43 |
} |
| 44 |
|
| 45 |
static bool TestGetUint() { |
| 46 |
static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; |
| 47 |
uint8_t u8; |
| 48 |
uint16_t u16; |
| 49 |
uint32_t u32; |
| 50 |
CBS data; |
| 51 |
|
| 52 |
CBS_init(&data, kData, sizeof(kData)); |
| 53 |
return CBS_get_u8(&data, &u8) && |
| 54 |
u8 == 1 && |
| 55 |
CBS_get_u16(&data, &u16) && |
| 56 |
u16 == 0x203 && |
| 57 |
CBS_get_u24(&data, &u32) && |
| 58 |
u32 == 0x40506 && |
| 59 |
CBS_get_u32(&data, &u32) && |
| 60 |
u32 == 0x708090a && |
| 61 |
CBS_get_last_u8(&data, &u8) && |
| 62 |
u8 == 0xb && |
| 63 |
!CBS_get_u8(&data, &u8) && |
| 64 |
!CBS_get_last_u8(&data, &u8); |
| 65 |
} |
| 66 |
|
| 67 |
static bool TestGetPrefixed() { |
| 68 |
static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1}; |
| 69 |
uint8_t u8; |
| 70 |
uint16_t u16; |
| 71 |
uint32_t u32; |
| 72 |
CBS data, prefixed; |
| 73 |
|
| 74 |
CBS_init(&data, kData, sizeof(kData)); |
| 75 |
return CBS_get_u8_length_prefixed(&data, &prefixed) && |
| 76 |
CBS_len(&prefixed) == 1 && |
| 77 |
CBS_get_u8(&prefixed, &u8) && |
| 78 |
u8 == 2 && |
| 79 |
CBS_get_u16_length_prefixed(&data, &prefixed) && |
| 80 |
CBS_len(&prefixed) == 2 && |
| 81 |
CBS_get_u16(&prefixed, &u16) && |
| 82 |
u16 == 0x304 && |
| 83 |
CBS_get_u24_length_prefixed(&data, &prefixed) && |
| 84 |
CBS_len(&prefixed) == 3 && |
| 85 |
CBS_get_u24(&prefixed, &u32) && |
| 86 |
u32 == 0x30201; |
| 87 |
} |
| 88 |
|
| 89 |
static bool TestGetPrefixedBad() { |
| 90 |
static const uint8_t kData1[] = {2, 1}; |
| 91 |
static const uint8_t kData2[] = {0, 2, 1}; |
| 92 |
static const uint8_t kData3[] = {0, 0, 2, 1}; |
| 93 |
CBS data, prefixed; |
| 94 |
|
| 95 |
CBS_init(&data, kData1, sizeof(kData1)); |
| 96 |
if (CBS_get_u8_length_prefixed(&data, &prefixed)) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
CBS_init(&data, kData2, sizeof(kData2)); |
| 101 |
if (CBS_get_u16_length_prefixed(&data, &prefixed)) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
CBS_init(&data, kData3, sizeof(kData3)); |
| 106 |
if (CBS_get_u24_length_prefixed(&data, &prefixed)) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
static bool TestGetASN1() { |
| 114 |
static const uint8_t kData1[] = {0x30, 2, 1, 2}; |
| 115 |
static const uint8_t kData2[] = {0x30, 3, 1, 2}; |
| 116 |
static const uint8_t kData3[] = {0x30, 0x80}; |
| 117 |
static const uint8_t kData4[] = {0x30, 0x81, 1, 1}; |
| 118 |
static const uint8_t kData5[4 + 0x80] = {0x30, 0x82, 0, 0x80}; |
| 119 |
static const uint8_t kData6[] = {0xa1, 3, 0x4, 1, 1}; |
| 120 |
static const uint8_t kData7[] = {0xa1, 3, 0x4, 2, 1}; |
| 121 |
static const uint8_t kData8[] = {0xa1, 3, 0x2, 1, 1}; |
| 122 |
static const uint8_t kData9[] = {0xa1, 3, 0x2, 1, 0xff}; |
| 123 |
|
| 124 |
CBS data, contents; |
| 125 |
int present; |
| 126 |
uint64_t value; |
| 127 |
|
| 128 |
CBS_init(&data, kData1, sizeof(kData1)); |
| 129 |
if (CBS_peek_asn1_tag(&data, 0x1) || |
| 130 |
!CBS_peek_asn1_tag(&data, 0x30)) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
if (!CBS_get_asn1(&data, &contents, 0x30) || |
| 134 |
CBS_len(&contents) != 2 || |
| 135 |
memcmp(CBS_data(&contents), "\x01\x02", 2) != 0) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
CBS_init(&data, kData2, sizeof(kData2)); |
| 140 |
// data is truncated |
| 141 |
if (CBS_get_asn1(&data, &contents, 0x30)) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
CBS_init(&data, kData3, sizeof(kData3)); |
| 146 |
// zero byte length of length |
| 147 |
if (CBS_get_asn1(&data, &contents, 0x30)) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
CBS_init(&data, kData4, sizeof(kData4)); |
| 152 |
// long form mistakenly used. |
| 153 |
if (CBS_get_asn1(&data, &contents, 0x30)) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
CBS_init(&data, kData5, sizeof(kData5)); |
| 158 |
// length takes too many bytes. |
| 159 |
if (CBS_get_asn1(&data, &contents, 0x30)) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
CBS_init(&data, kData1, sizeof(kData1)); |
| 164 |
// wrong tag. |
| 165 |
if (CBS_get_asn1(&data, &contents, 0x31)) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
CBS_init(&data, NULL, 0); |
| 170 |
// peek at empty data. |
| 171 |
if (CBS_peek_asn1_tag(&data, 0x30)) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
CBS_init(&data, NULL, 0); |
| 176 |
// optional elements at empty data. |
| 177 |
if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) || |
| 178 |
present || |
| 179 |
!CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) || |
| 180 |
present || |
| 181 |
CBS_len(&contents) != 0 || |
| 182 |
!CBS_get_optional_asn1_octet_string(&data, &contents, NULL, 0xa0) || |
| 183 |
CBS_len(&contents) != 0 || |
| 184 |
!CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) || |
| 185 |
value != 42) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
CBS_init(&data, kData6, sizeof(kData6)); |
| 190 |
// optional element. |
| 191 |
if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) || |
| 192 |
present || |
| 193 |
!CBS_get_optional_asn1(&data, &contents, &present, 0xa1) || |
| 194 |
!present || |
| 195 |
CBS_len(&contents) != 3 || |
| 196 |
memcmp(CBS_data(&contents), "\x04\x01\x01", 3) != 0) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
CBS_init(&data, kData6, sizeof(kData6)); |
| 201 |
// optional octet string. |
| 202 |
if (!CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa0) || |
| 203 |
present || |
| 204 |
CBS_len(&contents) != 0 || |
| 205 |
!CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1) || |
| 206 |
!present || |
| 207 |
CBS_len(&contents) != 1 || |
| 208 |
CBS_data(&contents)[0] != 1) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
CBS_init(&data, kData7, sizeof(kData7)); |
| 213 |
// invalid optional octet string. |
| 214 |
if (CBS_get_optional_asn1_octet_string(&data, &contents, &present, 0xa1)) { |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
CBS_init(&data, kData8, sizeof(kData8)); |
| 219 |
// optional octet string. |
| 220 |
if (!CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) || |
| 221 |
value != 42 || |
| 222 |
!CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42) || |
| 223 |
value != 1) { |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
CBS_init(&data, kData9, sizeof(kData9)); |
| 228 |
// invalid optional integer. |
| 229 |
if (CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42)) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
return true; |
| 234 |
} |
| 235 |
|
| 236 |
static bool TestGetOptionalASN1Bool() { |
| 237 |
static const uint8_t kTrue[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0xff}; |
| 238 |
static const uint8_t kFalse[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x00}; |
| 239 |
static const uint8_t kInvalid[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x01}; |
| 240 |
|
| 241 |
CBS data; |
| 242 |
CBS_init(&data, NULL, 0); |
| 243 |
int val = 2; |
| 244 |
if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) || |
| 245 |
val != 0) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
CBS_init(&data, kTrue, sizeof(kTrue)); |
| 250 |
val = 2; |
| 251 |
if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) || |
| 252 |
val != 1) { |
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
CBS_init(&data, kFalse, sizeof(kFalse)); |
| 257 |
val = 2; |
| 258 |
if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1) || |
| 259 |
val != 0) { |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
CBS_init(&data, kInvalid, sizeof(kInvalid)); |
| 264 |
if (CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1)) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
static bool TestCBBBasic() { |
| 272 |
static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 273 |
uint8_t *buf; |
| 274 |
size_t buf_len; |
| 275 |
CBB cbb; |
| 276 |
|
| 277 |
if (!CBB_init(&cbb, 100)) { |
| 278 |
return false; |
| 279 |
} |
| 280 |
CBB_cleanup(&cbb); |
| 281 |
|
| 282 |
if (!CBB_init(&cbb, 0)) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
if (!CBB_add_u8(&cbb, 1) || |
| 286 |
!CBB_add_u16(&cbb, 0x203) || |
| 287 |
!CBB_add_u24(&cbb, 0x40506) || |
| 288 |
!CBB_add_bytes(&cbb, (const uint8_t*) "\x07\x08", 2) || |
| 289 |
!CBB_finish(&cbb, &buf, &buf_len)) { |
| 290 |
CBB_cleanup(&cbb); |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
ScopedOpenSSLBytes scoper(buf); |
| 295 |
return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0; |
| 296 |
} |
| 297 |
|
| 298 |
static bool TestCBBFixed() { |
| 299 |
CBB cbb; |
| 300 |
uint8_t buf[1]; |
| 301 |
uint8_t *out_buf; |
| 302 |
size_t out_size; |
| 303 |
|
| 304 |
if (!CBB_init_fixed(&cbb, NULL, 0) || |
| 305 |
CBB_add_u8(&cbb, 1) || |
| 306 |
!CBB_finish(&cbb, &out_buf, &out_size) || |
| 307 |
out_buf != NULL || |
| 308 |
out_size != 0) { |
| 309 |
return false; |
| 310 |
} |
| 311 |
|
| 312 |
if (!CBB_init_fixed(&cbb, buf, 1) || |
| 313 |
!CBB_add_u8(&cbb, 1) || |
| 314 |
CBB_add_u8(&cbb, 2) || |
| 315 |
!CBB_finish(&cbb, &out_buf, &out_size) || |
| 316 |
out_buf != buf || |
| 317 |
out_size != 1 || |
| 318 |
buf[0] != 1) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
static bool TestCBBFinishChild() { |
| 326 |
CBB cbb, child; |
| 327 |
uint8_t *out_buf; |
| 328 |
size_t out_size; |
| 329 |
|
| 330 |
if (!CBB_init(&cbb, 16)) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
if (!CBB_add_u8_length_prefixed(&cbb, &child) || |
| 334 |
CBB_finish(&child, &out_buf, &out_size) || |
| 335 |
!CBB_finish(&cbb, &out_buf, &out_size)) { |
| 336 |
CBB_cleanup(&cbb); |
| 337 |
return false; |
| 338 |
} |
| 339 |
ScopedOpenSSLBytes scoper(out_buf); |
| 340 |
return out_size == 1 && out_buf[0] == 0; |
| 341 |
} |
| 342 |
|
| 343 |
static bool TestCBBPrefixed() { |
| 344 |
static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3, |
| 345 |
4, 5, 6, 5, 4, 1, 0, 1, 2}; |
| 346 |
uint8_t *buf; |
| 347 |
size_t buf_len; |
| 348 |
CBB cbb, contents, inner_contents, inner_inner_contents; |
| 349 |
|
| 350 |
if (!CBB_init(&cbb, 0) || |
| 351 |
CBB_len(&cbb) != 0 || |
| 352 |
!CBB_add_u8_length_prefixed(&cbb, &contents) || |
| 353 |
!CBB_add_u8_length_prefixed(&cbb, &contents) || |
| 354 |
!CBB_add_u8(&contents, 1) || |
| 355 |
CBB_len(&contents) != 1 || |
| 356 |
!CBB_flush(&cbb) || |
| 357 |
CBB_len(&cbb) != 3 || |
| 358 |
!CBB_add_u16_length_prefixed(&cbb, &contents) || |
| 359 |
!CBB_add_u16(&contents, 0x203) || |
| 360 |
!CBB_add_u24_length_prefixed(&cbb, &contents) || |
| 361 |
!CBB_add_u24(&contents, 0x40506) || |
| 362 |
!CBB_add_u8_length_prefixed(&cbb, &contents) || |
| 363 |
!CBB_add_u8_length_prefixed(&contents, &inner_contents) || |
| 364 |
!CBB_add_u8(&inner_contents, 1) || |
| 365 |
!CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents) || |
| 366 |
!CBB_add_u8(&inner_inner_contents, 2) || |
| 367 |
!CBB_finish(&cbb, &buf, &buf_len)) { |
| 368 |
CBB_cleanup(&cbb); |
| 369 |
return false; |
| 370 |
} |
| 371 |
|
| 372 |
ScopedOpenSSLBytes scoper(buf); |
| 373 |
return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0; |
| 374 |
} |
| 375 |
|
| 376 |
static bool TestCBBDiscardChild() { |
| 377 |
ScopedCBB cbb; |
| 378 |
CBB contents, inner_contents, inner_inner_contents; |
| 379 |
|
| 380 |
if (!CBB_init(cbb.get(), 0) || |
| 381 |
!CBB_add_u8(cbb.get(), 0xaa)) { |
| 382 |
return false; |
| 383 |
} |
| 384 |
|
| 385 |
// Discarding |cbb|'s children preserves the byte written. |
| 386 |
CBB_discard_child(cbb.get()); |
| 387 |
|
| 388 |
if (!CBB_add_u8_length_prefixed(cbb.get(), &contents) || |
| 389 |
!CBB_add_u8_length_prefixed(cbb.get(), &contents) || |
| 390 |
!CBB_add_u8(&contents, 0xbb) || |
| 391 |
!CBB_add_u16_length_prefixed(cbb.get(), &contents) || |
| 392 |
!CBB_add_u16(&contents, 0xcccc) || |
| 393 |
!CBB_add_u24_length_prefixed(cbb.get(), &contents) || |
| 394 |
!CBB_add_u24(&contents, 0xdddddd) || |
| 395 |
!CBB_add_u8_length_prefixed(cbb.get(), &contents) || |
| 396 |
!CBB_add_u8(&contents, 0xff) || |
| 397 |
!CBB_add_u8_length_prefixed(&contents, &inner_contents) || |
| 398 |
!CBB_add_u8(&inner_contents, 0x42) || |
| 399 |
!CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents) || |
| 400 |
!CBB_add_u8(&inner_inner_contents, 0x99)) { |
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
// Discard everything from |inner_contents| down. |
| 405 |
CBB_discard_child(&contents); |
| 406 |
|
| 407 |
uint8_t *buf; |
| 408 |
size_t buf_len; |
| 409 |
if (!CBB_finish(cbb.get(), &buf, &buf_len)) { |
| 410 |
return false; |
| 411 |
} |
| 412 |
ScopedOpenSSLBytes scoper(buf); |
| 413 |
|
| 414 |
static const uint8_t kExpected[] = { |
| 415 |
0xaa, |
| 416 |
0, |
| 417 |
1, 0xbb, |
| 418 |
0, 2, 0xcc, 0xcc, |
| 419 |
0, 0, 3, 0xdd, 0xdd, 0xdd, |
| 420 |
1, 0xff, |
| 421 |
}; |
| 422 |
return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0; |
| 423 |
} |
| 424 |
|
| 425 |
static bool TestCBBMisuse() { |
| 426 |
CBB cbb, child, contents; |
| 427 |
uint8_t *buf; |
| 428 |
size_t buf_len; |
| 429 |
|
| 430 |
if (!CBB_init(&cbb, 0)) { |
| 431 |
return false; |
| 432 |
} |
| 433 |
if (!CBB_add_u8_length_prefixed(&cbb, &child) || |
| 434 |
!CBB_add_u8(&child, 1) || |
| 435 |
!CBB_add_u8(&cbb, 2)) { |
| 436 |
CBB_cleanup(&cbb); |
| 437 |
return false; |
| 438 |
} |
| 439 |
|
| 440 |
// Since we wrote to |cbb|, |child| is now invalid and attempts to write to |
| 441 |
// it should fail. |
| 442 |
if (CBB_add_u8(&child, 1) || |
| 443 |
CBB_add_u16(&child, 1) || |
| 444 |
CBB_add_u24(&child, 1) || |
| 445 |
CBB_add_u8_length_prefixed(&child, &contents) || |
| 446 |
CBB_add_u16_length_prefixed(&child, &contents) || |
| 447 |
CBB_add_asn1(&child, &contents, 1) || |
| 448 |
CBB_add_bytes(&child, (const uint8_t*) "a", 1)) { |
| 449 |
fprintf(stderr, "CBB operation on invalid CBB did not fail.\n"); |
| 450 |
CBB_cleanup(&cbb); |
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
if (!CBB_finish(&cbb, &buf, &buf_len)) { |
| 455 |
CBB_cleanup(&cbb); |
| 456 |
return false; |
| 457 |
} |
| 458 |
ScopedOpenSSLBytes scoper(buf); |
| 459 |
|
| 460 |
if (buf_len != 3 || |
| 461 |
memcmp(buf, "\x01\x01\x02", 3) != 0) { |
| 462 |
return false; |
| 463 |
} |
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
static bool TestCBBASN1() { |
| 468 |
static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3}; |
| 469 |
uint8_t *buf; |
| 470 |
size_t buf_len; |
| 471 |
CBB cbb, contents, inner_contents; |
| 472 |
|
| 473 |
if (!CBB_init(&cbb, 0)) { |
| 474 |
return false; |
| 475 |
} |
| 476 |
if (!CBB_add_asn1(&cbb, &contents, 0x30) || |
| 477 |
!CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03", 3) || |
| 478 |
!CBB_finish(&cbb, &buf, &buf_len)) { |
| 479 |
CBB_cleanup(&cbb); |
| 480 |
return false; |
| 481 |
} |
| 482 |
ScopedOpenSSLBytes scoper(buf); |
| 483 |
|
| 484 |
if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) != 0) { |
| 485 |
return false; |
| 486 |
} |
| 487 |
|
| 488 |
std::vector<uint8_t> test_data(100000, 0x42); |
| 489 |
|
| 490 |
if (!CBB_init(&cbb, 0)) { |
| 491 |
return false; |
| 492 |
} |
| 493 |
if (!CBB_add_asn1(&cbb, &contents, 0x30) || |
| 494 |
!CBB_add_bytes(&contents, test_data.data(), 130) || |
| 495 |
!CBB_finish(&cbb, &buf, &buf_len)) { |
| 496 |
CBB_cleanup(&cbb); |
| 497 |
return false; |
| 498 |
} |
| 499 |
scoper.reset(buf); |
| 500 |
|
| 501 |
if (buf_len != 3 + 130 || |
| 502 |
memcmp(buf, "\x30\x81\x82", 3) != 0 || |
| 503 |
memcmp(buf + 3, test_data.data(), 130) != 0) { |
| 504 |
return false; |
| 505 |
} |
| 506 |
|
| 507 |
if (!CBB_init(&cbb, 0)) { |
| 508 |
return false; |
| 509 |
} |
| 510 |
if (!CBB_add_asn1(&cbb, &contents, 0x30) || |
| 511 |
!CBB_add_bytes(&contents, test_data.data(), 1000) || |
| 512 |
!CBB_finish(&cbb, &buf, &buf_len)) { |
| 513 |
CBB_cleanup(&cbb); |
| 514 |
return false; |
| 515 |
} |
| 516 |
scoper.reset(buf); |
| 517 |
|
| 518 |
if (buf_len != 4 + 1000 || |
| 519 |
memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 || |
| 520 |
memcmp(buf + 4, test_data.data(), 1000)) { |
| 521 |
return false; |
| 522 |
} |
| 523 |
|
| 524 |
if (!CBB_init(&cbb, 0)) { |
| 525 |
return false; |
| 526 |
} |
| 527 |
if (!CBB_add_asn1(&cbb, &contents, 0x30) || |
| 528 |
!CBB_add_asn1(&contents, &inner_contents, 0x30) || |
| 529 |
!CBB_add_bytes(&inner_contents, test_data.data(), 100000) || |
| 530 |
!CBB_finish(&cbb, &buf, &buf_len)) { |
| 531 |
CBB_cleanup(&cbb); |
| 532 |
return false; |
| 533 |
} |
| 534 |
scoper.reset(buf); |
| 535 |
|
| 536 |
if (buf_len != 5 + 5 + 100000 || |
| 537 |
memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 || |
| 538 |
memcmp(buf + 10, test_data.data(), 100000)) { |
| 539 |
return false; |
| 540 |
} |
| 541 |
|
| 542 |
return true; |
| 543 |
} |
| 544 |
|
| 545 |
static bool DoBerConvert(const char *name, |
| 546 |
const uint8_t *der_expected, size_t der_len, |
| 547 |
const uint8_t *ber, size_t ber_len) { |
| 548 |
CBS in; |
| 549 |
uint8_t *out; |
| 550 |
size_t out_len; |
| 551 |
|
| 552 |
CBS_init(&in, ber, ber_len); |
| 553 |
if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) { |
| 554 |
fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name); |
| 555 |
return false; |
| 556 |
} |
| 557 |
ScopedOpenSSLBytes scoper(out); |
| 558 |
|
| 559 |
if (out == NULL) { |
| 560 |
if (ber_len != der_len || |
| 561 |
memcmp(der_expected, ber, ber_len) != 0) { |
| 562 |
fprintf(stderr, "%s: incorrect unconverted result.\n", name); |
| 563 |
return false; |
| 564 |
} |
| 565 |
|
| 566 |
return true; |
| 567 |
} |
| 568 |
|
| 569 |
if (out_len != der_len || |
| 570 |
memcmp(out, der_expected, der_len) != 0) { |
| 571 |
fprintf(stderr, "%s: incorrect converted result.\n", name); |
| 572 |
return false; |
| 573 |
} |
| 574 |
|
| 575 |
return true; |
| 576 |
} |
| 577 |
|
| 578 |
static bool TestBerConvert() { |
| 579 |
static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00}; |
| 580 |
|
| 581 |
// kIndefBER contains a SEQUENCE with an indefinite length. |
| 582 |
static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00}; |
| 583 |
static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02}; |
| 584 |
|
| 585 |
// kOctetStringBER contains an indefinite length OCTET STRING with two parts. |
| 586 |
// These parts need to be concatenated in DER form. |
| 587 |
static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0, 1, |
| 588 |
0x04, 0x02, 2, 3, 0x00, 0x00}; |
| 589 |
static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3}; |
| 590 |
|
| 591 |
// kNSSBER is part of a PKCS#12 message generated by NSS that uses indefinite |
| 592 |
// length elements extensively. |
| 593 |
static const uint8_t kNSSBER[] = { |
| 594 |
0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48, |
| 595 |
0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04, |
| 596 |
0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39, |
| 597 |
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, |
| 598 |
0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, |
| 599 |
0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, |
| 600 |
0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, |
| 601 |
0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00, |
| 602 |
}; |
| 603 |
|
| 604 |
static const uint8_t kNSSDER[] = { |
| 605 |
0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86, |
| 606 |
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04, |
| 607 |
0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06, |
| 608 |
0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84, |
| 609 |
0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8, |
| 610 |
0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38, |
| 611 |
0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0, |
| 612 |
0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, |
| 613 |
}; |
| 614 |
|
| 615 |
// kConstructedStringBER contains a deeply-nested constructed OCTET STRING. |
| 616 |
// The BER conversion collapses this to one level deep, but not completely. |
| 617 |
static const uint8_t kConstructedStringBER[] = { |
| 618 |
0xa0, 0x10, 0x24, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, |
| 619 |
0x01, 0x24, 0x06, 0x04, 0x01, 0x02, 0x04, 0x01, 0x03, |
| 620 |
}; |
| 621 |
static const uint8_t kConstructedStringDER[] = { |
| 622 |
0xa0, 0x08, 0x04, 0x02, 0x00, 0x01, 0x04, 0x02, 0x02, 0x03, |
| 623 |
}; |
| 624 |
|
| 625 |
return DoBerConvert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER), |
| 626 |
kSimpleBER, sizeof(kSimpleBER)) && |
| 627 |
DoBerConvert("kIndefBER", kIndefDER, sizeof(kIndefDER), kIndefBER, |
| 628 |
sizeof(kIndefBER)) && |
| 629 |
DoBerConvert("kOctetStringBER", kOctetStringDER, |
| 630 |
sizeof(kOctetStringDER), kOctetStringBER, |
| 631 |
sizeof(kOctetStringBER)) && |
| 632 |
DoBerConvert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER, |
| 633 |
sizeof(kNSSBER)) && |
| 634 |
DoBerConvert("kConstructedStringBER", kConstructedStringDER, |
| 635 |
sizeof(kConstructedStringDER), kConstructedStringBER, |
| 636 |
sizeof(kConstructedStringBER)); |
| 637 |
} |
| 638 |
|
| 639 |
struct ImplicitStringTest { |
| 640 |
const char *in; |
| 641 |
size_t in_len; |
| 642 |
bool ok; |
| 643 |
const char *out; |
| 644 |
size_t out_len; |
| 645 |
}; |
| 646 |
|
| 647 |
static const ImplicitStringTest kImplicitStringTests[] = { |
| 648 |
// A properly-encoded string. |
| 649 |
{"\x80\x03\x61\x61\x61", 5, true, "aaa", 3}, |
| 650 |
// An implicit-tagged string. |
| 651 |
{"\xa0\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11, true, "aaa", 3}, |
| 652 |
// |CBS_get_asn1_implicit_string| only accepts one level deep of nesting. |
| 653 |
{"\xa0\x0b\x24\x06\x04\x01\x61\x04\x01\x61\x04\x01\x61", 13, false, nullptr, |
| 654 |
0}, |
| 655 |
// The outer tag must match. |
| 656 |
{"\x81\x03\x61\x61\x61", 5, false, nullptr, 0}, |
| 657 |
{"\xa1\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11, false, nullptr, 0}, |
| 658 |
// The inner tag must match. |
| 659 |
{"\xa1\x09\x0c\x01\x61\x0c\x01\x61\x0c\x01\x61", 11, false, nullptr, 0}, |
| 660 |
}; |
| 661 |
|
| 662 |
static bool TestImplicitString() { |
| 663 |
for (const auto &test : kImplicitStringTests) { |
| 664 |
uint8_t *storage = nullptr; |
| 665 |
CBS in, out; |
| 666 |
CBS_init(&in, reinterpret_cast<const uint8_t *>(test.in), test.in_len); |
| 667 |
int ok = CBS_get_asn1_implicit_string(&in, &out, &storage, |
| 668 |
CBS_ASN1_CONTEXT_SPECIFIC | 0, |
| 669 |
CBS_ASN1_OCTETSTRING); |
| 670 |
ScopedOpenSSLBytes scoper(storage); |
| 671 |
|
| 672 |
if (static_cast<bool>(ok) != test.ok) { |
| 673 |
fprintf(stderr, "CBS_get_asn1_implicit_string unexpectedly %s\n", |
| 674 |
ok ? "succeeded" : "failed"); |
| 675 |
return false; |
| 676 |
} |
| 677 |
|
| 678 |
if (ok && (CBS_len(&out) != test.out_len || |
| 679 |
memcmp(CBS_data(&out), test.out, test.out_len) != 0)) { |
| 680 |
fprintf(stderr, "CBS_get_asn1_implicit_string gave the wrong output\n"); |
| 681 |
return false; |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
return true; |
| 686 |
} |
| 687 |
|
| 688 |
struct ASN1Uint64Test { |
| 689 |
uint64_t value; |
| 690 |
const char *encoding; |
| 691 |
size_t encoding_len; |
| 692 |
}; |
| 693 |
|
| 694 |
static const ASN1Uint64Test kASN1Uint64Tests[] = { |
| 695 |
{0, "\x02\x01\x00", 3}, |
| 696 |
{1, "\x02\x01\x01", 3}, |
| 697 |
{127, "\x02\x01\x7f", 3}, |
| 698 |
{128, "\x02\x02\x00\x80", 4}, |
| 699 |
{0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7}, |
| 700 |
{UINT64_C(0x0102030405060708), |
| 701 |
"\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10}, |
| 702 |
{UINT64_C(0xffffffffffffffff), |
| 703 |
"\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11}, |
| 704 |
}; |
| 705 |
|
| 706 |
struct ASN1InvalidUint64Test { |
| 707 |
const char *encoding; |
| 708 |
size_t encoding_len; |
| 709 |
}; |
| 710 |
|
| 711 |
static const ASN1InvalidUint64Test kASN1InvalidUint64Tests[] = { |
| 712 |
// Bad tag. |
| 713 |
{"\x03\x01\x00", 3}, |
| 714 |
// Empty contents. |
| 715 |
{"\x02\x00", 2}, |
| 716 |
// Negative number. |
| 717 |
{"\x02\x01\x80", 3}, |
| 718 |
// Overflow. |
| 719 |
{"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11}, |
| 720 |
// Leading zeros. |
| 721 |
{"\x02\x02\x00\x01", 4}, |
| 722 |
}; |
| 723 |
|
| 724 |
static bool TestASN1Uint64() { |
| 725 |
for (size_t i = 0; i < sizeof(kASN1Uint64Tests) / sizeof(kASN1Uint64Tests[0]); |
| 726 |
i++) { |
| 727 |
const ASN1Uint64Test *test = &kASN1Uint64Tests[i]; |
| 728 |
CBS cbs; |
| 729 |
uint64_t value; |
| 730 |
CBB cbb; |
| 731 |
uint8_t *out; |
| 732 |
size_t len; |
| 733 |
|
| 734 |
CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len); |
| 735 |
if (!CBS_get_asn1_uint64(&cbs, &value) || |
| 736 |
CBS_len(&cbs) != 0 || |
| 737 |
value != test->value) { |
| 738 |
return false; |
| 739 |
} |
| 740 |
|
| 741 |
if (!CBB_init(&cbb, 0)) { |
| 742 |
return false; |
| 743 |
} |
| 744 |
if (!CBB_add_asn1_uint64(&cbb, test->value) || |
| 745 |
!CBB_finish(&cbb, &out, &len)) { |
| 746 |
CBB_cleanup(&cbb); |
| 747 |
return false; |
| 748 |
} |
| 749 |
ScopedOpenSSLBytes scoper(out); |
| 750 |
if (len != test->encoding_len || memcmp(out, test->encoding, len) != 0) { |
| 751 |
return false; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
for (size_t i = 0; |
| 756 |
i < sizeof(kASN1InvalidUint64Tests) / sizeof(kASN1InvalidUint64Tests[0]); |
| 757 |
i++) { |
| 758 |
const ASN1InvalidUint64Test *test = &kASN1InvalidUint64Tests[i]; |
| 759 |
CBS cbs; |
| 760 |
uint64_t value; |
| 761 |
|
| 762 |
CBS_init(&cbs, (const uint8_t *)test->encoding, test->encoding_len); |
| 763 |
if (CBS_get_asn1_uint64(&cbs, &value)) { |
| 764 |
return false; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
return true; |
| 769 |
} |
| 770 |
|
| 771 |
static bool TestZero() { |
| 772 |
CBB cbb; |
| 773 |
CBB_zero(&cbb); |
| 774 |
// Calling |CBB_cleanup| on a zero-state |CBB| must not crash. |
| 775 |
CBB_cleanup(&cbb); |
| 776 |
return true; |
| 777 |
} |
| 778 |
|
| 779 |
static bool TestCBBReserve() { |
| 780 |
uint8_t buf[10]; |
| 781 |
uint8_t *ptr; |
| 782 |
size_t len; |
| 783 |
ScopedCBB cbb; |
| 784 |
if (!CBB_init_fixed(cbb.get(), buf, sizeof(buf)) || |
| 785 |
// Too large. |
| 786 |
CBB_reserve(cbb.get(), &ptr, 11) || |
| 787 |
// Successfully reserve the entire space. |
| 788 |
!CBB_reserve(cbb.get(), &ptr, 10) || |
| 789 |
ptr != buf || |
| 790 |
// Advancing under the maximum bytes is legal. |
| 791 |
!CBB_did_write(cbb.get(), 5) || |
| 792 |
!CBB_finish(cbb.get(), NULL, &len) || |
| 793 |
len != 5) { |
| 794 |
return false; |
| 795 |
} |
| 796 |
return true; |
| 797 |
} |
| 798 |
|
| 799 |
int main(void) { |
| 800 |
CRYPTO_library_init(); |
| 801 |
|
| 802 |
if (!TestSkip() || |
| 803 |
!TestGetUint() || |
| 804 |
!TestGetPrefixed() || |
| 805 |
!TestGetPrefixedBad() || |
| 806 |
!TestGetASN1() || |
| 807 |
!TestCBBBasic() || |
| 808 |
!TestCBBFixed() || |
| 809 |
!TestCBBFinishChild() || |
| 810 |
!TestCBBMisuse() || |
| 811 |
!TestCBBPrefixed() || |
| 812 |
!TestCBBDiscardChild() || |
| 813 |
!TestCBBASN1() || |
| 814 |
!TestBerConvert() || |
| 815 |
!TestImplicitString() || |
| 816 |
!TestASN1Uint64() || |
| 817 |
!TestGetOptionalASN1Bool() || |
| 818 |
!TestZero() || |
| 819 |
!TestCBBReserve()) { |
| 820 |
return 1; |
| 821 |
} |
| 822 |
|
| 823 |
printf("PASS\n"); |
| 824 |
return 0; |
| 825 |
} |