| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bytestring.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 |
| 24 | #include "../internal.h" |
| 25 |
| 26 |
| 27 | void CBB_zero(CBB *cbb) { OPENSSL_memset(cbb, 0, sizeof(CBB)); } |
| 28 |
| 29 | static void cbb_init(CBB *cbb, uint8_t *buf, size_t cap, int can_resize) { |
| 30 | cbb->is_child = 0; |
| 31 | cbb->child = NULL; |
| 32 | cbb->u.base.buf = buf; |
| 33 | cbb->u.base.len = 0; |
| 34 | cbb->u.base.cap = cap; |
| 35 | cbb->u.base.can_resize = can_resize; |
| 36 | cbb->u.base.error = 0; |
| 37 | } |
| 38 |
| 39 | int CBB_init(CBB *cbb, size_t initial_capacity) { |
| 40 | CBB_zero(cbb); |
| 41 |
| 42 | uint8_t *buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(initial_capacity)); |
| 43 | if (initial_capacity > 0 && buf == NULL) { |
| 44 | return 0; |
| 45 | } |
| 46 |
| 47 | cbb_init(cbb, buf, initial_capacity, /*can_resize=*/1); |
| 48 | return 1; |
| 49 | } |
| 50 |
| 51 | int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) { |
| 52 | CBB_zero(cbb); |
| 53 | cbb_init(cbb, buf, len, /*can_resize=*/0); |
| 54 | return 1; |
| 55 | } |
| 56 |
| 57 | void CBB_cleanup(CBB *cbb) { |
| 58 | // Child |CBB|s are non-owning. They are implicitly discarded and should not |
| 59 | // be used with |CBB_cleanup| or |ScopedCBB|. |
| 60 | assert(!cbb->is_child); |
| 61 | if (cbb->is_child) { |
| 62 | return; |
| 63 | } |
| 64 |
| 65 | if (cbb->u.base.can_resize) { |
| 66 | OPENSSL_free(cbb->u.base.buf); |
| 67 | } |
| 68 | } |
| 69 |
| 70 | static int cbb_buffer_reserve(struct cbb_buffer_st *base, uint8_t **out, |
| 71 | size_t len) { |
| 72 | if (base == NULL) { |
| 73 | return 0; |
| 74 | } |
| 75 |
| 76 | size_t newlen = base->len + len; |
| 77 | if (newlen < base->len) { |
| 78 | // Overflow |
| 79 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
| 80 | goto err; |
| 81 | } |
| 82 |
| 83 | if (newlen > base->cap) { |
| 84 | if (!base->can_resize) { |
| 85 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
| 86 | goto err; |
| 87 | } |
| 88 |
| 89 | size_t newcap = base->cap * 2; |
| 90 | if (newcap < base->cap || newcap < newlen) { |
| 91 | newcap = newlen; |
| 92 | } |
| 93 | uint8_t *newbuf = |
| 94 | reinterpret_cast<uint8_t *>(OPENSSL_realloc(base->buf, newcap)); |
| 95 | if (newbuf == NULL) { |
| 96 | goto err; |
| 97 | } |
| 98 |
| 99 | base->buf = newbuf; |
| 100 | base->cap = newcap; |
| 101 | } |
| 102 |
| 103 | if (out) { |
| 104 | *out = base->buf + base->len; |
| 105 | } |
| 106 |
| 107 | return 1; |
| 108 |
| 109 | err: |
| 110 | base->error = 1; |
| 111 | return 0; |
| 112 | } |
| 113 |
| 114 | static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out, |
| 115 | size_t len) { |
| 116 | if (!cbb_buffer_reserve(base, out, len)) { |
| 117 | return 0; |
| 118 | } |
| 119 | // This will not overflow or |cbb_buffer_reserve| would have failed. |
| 120 | base->len += len; |
| 121 | return 1; |
| 122 | } |
| 123 |
| 124 | int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) { |
| 125 | if (cbb->is_child) { |
| 126 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 127 | return 0; |
| 128 | } |
| 129 |
| 130 | if (!CBB_flush(cbb)) { |
| 131 | return 0; |
| 132 | } |
| 133 |
| 134 | if (cbb->u.base.can_resize && (out_data == NULL || out_len == NULL)) { |
| 135 | // |out_data| and |out_len| can only be NULL if the CBB is fixed. |
| 136 | return 0; |
| 137 | } |
| 138 |
| 139 | if (out_data != NULL) { |
| 140 | *out_data = cbb->u.base.buf; |
| 141 | } |
| 142 | if (out_len != NULL) { |
| 143 | *out_len = cbb->u.base.len; |
| 144 | } |
| 145 | cbb->u.base.buf = NULL; |
| 146 | CBB_cleanup(cbb); |
| 147 | return 1; |
| 148 | } |
| 149 |
| 150 | static struct cbb_buffer_st *cbb_get_base(CBB *cbb) { |
| 151 | if (cbb->is_child) { |
| 152 | return cbb->u.child.base; |
| 153 | } |
| 154 | return &cbb->u.base; |
| 155 | } |
| 156 |
| 157 | static void cbb_on_error(CBB *cbb) { |
| 158 | // Due to C's lack of destructors and |CBB|'s auto-flushing API, a failing |
| 159 | // |CBB|-taking function may leave a dangling pointer to a child |CBB|. As a |
| 160 | // result, the convention is callers may not write to |CBB|s that have failed. |
| 161 | // But, as a safety measure, we lock the |CBB| into an error state. Once the |
| 162 | // error bit is set, |cbb->child| will not be read. |
| 163 | // |
| 164 | // TODO(davidben): This still isn't quite ideal. A |CBB| function *outside* |
| 165 | // this file may originate an error while the |CBB| points to a local child. |
| 166 | // In that case we don't set the error bit and are reliant on the error |
| 167 | // convention. Perhaps we allow |CBB_cleanup| on child |CBB|s and make every |
| 168 | // child's |CBB_cleanup| set the error bit if unflushed. That will be |
| 169 | // convenient for C++ callers, but very tedious for C callers. So C callers |
| 170 | // perhaps should get a |CBB_on_error| function that can be, less tediously, |
| 171 | // stuck in a |goto err| block. |
| 172 | cbb_get_base(cbb)->error = 1; |
| 173 |
| 174 | // Clearing the pointer is not strictly necessary, but GCC's dangling pointer |
| 175 | // warning does not know |cbb->child| will not be read once |error| is set |
| 176 | // above. |
| 177 | cbb->child = NULL; |
| 178 | } |
| 179 |
| 180 | // CBB_flush recurses and then writes out any pending length prefix. The |
| 181 | // current length of the underlying base is taken to be the length of the |
| 182 | // length-prefixed data. |
| 183 | int CBB_flush(CBB *cbb) { |
| 184 | // If |base| has hit an error, the buffer is in an undefined state, so |
| 185 | // fail all following calls. In particular, |cbb->child| may point to invalid |
| 186 | // memory. |
| 187 | struct cbb_buffer_st *base = cbb_get_base(cbb); |
| 188 | if (base == NULL || base->error) { |
| 189 | return 0; |
| 190 | } |
| 191 |
| 192 | if (cbb->child == NULL) { |
| 193 | // Nothing to flush. |
| 194 | return 1; |
| 195 | } |
| 196 |
| 197 | assert(cbb->child->is_child); |
| 198 | struct cbb_child_st *child = &cbb->child->u.child; |
| 199 | assert(child->base == base); |
| 200 | size_t child_start = child->offset + child->pending_len_len; |
| 201 |
| 202 | size_t len; |
| 203 | if (!CBB_flush(cbb->child) || child_start < child->offset || |
| 204 | base->len < child_start) { |
| 205 | goto err; |
| 206 | } |
| 207 |
| 208 | len = base->len - child_start; |
| 209 |
| 210 | if (child->pending_is_asn1) { |
| 211 | // For ASN.1 we assume that we'll only need a single byte for the length. |
| 212 | // If that turned out to be incorrect, we have to move the contents along |
| 213 | // in order to make space. |
| 214 | uint8_t len_len; |
| 215 | uint8_t initial_length_byte; |
| 216 |
| 217 | assert(child->pending_len_len == 1); |
| 218 |
| 219 | if (len > 0xfffffffe) { |
| 220 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
| 221 | // Too large. |
| 222 | goto err; |
| 223 | } else if (len > 0xffffff) { |
| 224 | len_len = 5; |
| 225 | initial_length_byte = 0x80 | 4; |
| 226 | } else if (len > 0xffff) { |
| 227 | len_len = 4; |
| 228 | initial_length_byte = 0x80 | 3; |
| 229 | } else if (len > 0xff) { |
| 230 | len_len = 3; |
| 231 | initial_length_byte = 0x80 | 2; |
| 232 | } else if (len > 0x7f) { |
| 233 | len_len = 2; |
| 234 | initial_length_byte = 0x80 | 1; |
| 235 | } else { |
| 236 | len_len = 1; |
| 237 | initial_length_byte = (uint8_t)len; |
| 238 | len = 0; |
| 239 | } |
| 240 |
| 241 | if (len_len != 1) { |
| 242 | // We need to move the contents along in order to make space. |
| 243 | size_t extra_bytes = len_len - 1; |
| 244 | if (!cbb_buffer_add(base, NULL, extra_bytes)) { |
| 245 | goto err; |
| 246 | } |
| 247 | OPENSSL_memmove(base->buf + child_start + extra_bytes, |
| 248 | base->buf + child_start, len); |
| 249 | } |
| 250 | base->buf[child->offset++] = initial_length_byte; |
| 251 | child->pending_len_len = len_len - 1; |
| 252 | } |
| 253 |
| 254 | for (size_t i = child->pending_len_len - 1; i < child->pending_len_len; i--) { |
| 255 | base->buf[child->offset + i] = (uint8_t)len; |
| 256 | len >>= 8; |
| 257 | } |
| 258 | if (len != 0) { |
| 259 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_OVERFLOW); |
| 260 | goto err; |
| 261 | } |
| 262 |
| 263 | child->base = NULL; |
| 264 | cbb->child = NULL; |
| 265 |
| 266 | return 1; |
| 267 |
| 268 | err: |
| 269 | cbb_on_error(cbb); |
| 270 | return 0; |
| 271 | } |
| 272 |
| 273 | const uint8_t *CBB_data(const CBB *cbb) { |
| 274 | assert(cbb->child == NULL); |
| 275 | if (cbb->is_child) { |
| 276 | return cbb->u.child.base->buf + cbb->u.child.offset + |
| 277 | cbb->u.child.pending_len_len; |
| 278 | } |
| 279 | return cbb->u.base.buf; |
| 280 | } |
| 281 |
| 282 | size_t CBB_len(const CBB *cbb) { |
| 283 | assert(cbb->child == NULL); |
| 284 | if (cbb->is_child) { |
| 285 | assert(cbb->u.child.offset + cbb->u.child.pending_len_len <= |
| 286 | cbb->u.child.base->len); |
| 287 | return cbb->u.child.base->len - cbb->u.child.offset - |
| 288 | cbb->u.child.pending_len_len; |
| 289 | } |
| 290 | return cbb->u.base.len; |
| 291 | } |
| 292 |
| 293 | static int cbb_add_child(CBB *cbb, CBB *out_child, uint8_t len_len, |
| 294 | int is_asn1) { |
| 295 | assert(cbb->child == NULL); |
| 296 | assert(!is_asn1 || len_len == 1); |
| 297 | struct cbb_buffer_st *base = cbb_get_base(cbb); |
| 298 | size_t offset = base->len; |
| 299 |
| 300 | // Reserve space for the length prefix. |
| 301 | uint8_t *prefix_bytes; |
| 302 | if (!cbb_buffer_add(base, &prefix_bytes, len_len)) { |
| 303 | return 0; |
| 304 | } |
| 305 | OPENSSL_memset(prefix_bytes, 0, len_len); |
| 306 |
| 307 | CBB_zero(out_child); |
| 308 | out_child->is_child = 1; |
| 309 | out_child->u.child.base = base; |
| 310 | out_child->u.child.offset = offset; |
| 311 | out_child->u.child.pending_len_len = len_len; |
| 312 | out_child->u.child.pending_is_asn1 = is_asn1; |
| 313 | cbb->child = out_child; |
| 314 | return 1; |
| 315 | } |
| 316 |
| 317 | static int cbb_add_length_prefixed(CBB *cbb, CBB *out_contents, |
| 318 | uint8_t len_len) { |
| 319 | if (!CBB_flush(cbb)) { |
| 320 | return 0; |
| 321 | } |
| 322 |
| 323 | return cbb_add_child(cbb, out_contents, len_len, /*is_asn1=*/0); |
| 324 | } |
| 325 |
| 326 | int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) { |
| 327 | return cbb_add_length_prefixed(cbb, out_contents, 1); |
| 328 | } |
| 329 |
| 330 | int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) { |
| 331 | return cbb_add_length_prefixed(cbb, out_contents, 2); |
| 332 | } |
| 333 |
| 334 | int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) { |
| 335 | return cbb_add_length_prefixed(cbb, out_contents, 3); |
| 336 | } |
| 337 |
| 338 | // add_base128_integer encodes |v| as a big-endian base-128 integer where the |
| 339 | // high bit of each byte indicates where there is more data. This is the |
| 340 | // encoding used in DER for both high tag number form and OID components. |
| 341 | static int add_base128_integer(CBB *cbb, uint64_t v) { |
| 342 | unsigned len_len = 0; |
| 343 | uint64_t copy = v; |
| 344 | while (copy > 0) { |
| 345 | len_len++; |
| 346 | copy >>= 7; |
| 347 | } |
| 348 | if (len_len == 0) { |
| 349 | len_len = 1; // Zero is encoded with one byte. |
| 350 | } |
| 351 | for (unsigned i = len_len - 1; i < len_len; i--) { |
| 352 | uint8_t byte = (v >> (7 * i)) & 0x7f; |
| 353 | if (i != 0) { |
| 354 | // The high bit denotes whether there is more data. |
| 355 | byte |= 0x80; |
| 356 | } |
| 357 | if (!CBB_add_u8(cbb, byte)) { |
| 358 | return 0; |
| 359 | } |
| 360 | } |
| 361 | return 1; |
| 362 | } |
| 363 |
| 364 | int CBB_add_asn1(CBB *cbb, CBB *out_contents, CBS_ASN1_TAG tag) { |
| 365 | if (!CBB_flush(cbb)) { |
| 366 | return 0; |
| 367 | } |
| 368 |
| 369 | // Split the tag into leading bits and tag number. |
| 370 | uint8_t tag_bits = (tag >> CBS_ASN1_TAG_SHIFT) & 0xe0; |
| 371 | CBS_ASN1_TAG tag_number = tag & CBS_ASN1_TAG_NUMBER_MASK; |
| 372 | if (tag_number >= 0x1f) { |
| 373 | // Set all the bits in the tag number to signal high tag number form. |
| 374 | if (!CBB_add_u8(cbb, tag_bits | 0x1f) || |
| 375 | !add_base128_integer(cbb, tag_number)) { |
| 376 | return 0; |
| 377 | } |
| 378 | } else if (!CBB_add_u8(cbb, tag_bits | tag_number)) { |
| 379 | return 0; |
| 380 | } |
| 381 |
| 382 | // Reserve one byte of length prefix. |CBB_flush| will finish it later. |
| 383 | return cbb_add_child(cbb, out_contents, /*len_len=*/1, /*is_asn1=*/1); |
| 384 | } |
| 385 |
| 386 | int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) { |
| 387 | uint8_t *out; |
| 388 | if (!CBB_add_space(cbb, &out, len)) { |
| 389 | return 0; |
| 390 | } |
| 391 | OPENSSL_memcpy(out, data, len); |
| 392 | return 1; |
| 393 | } |
| 394 |
| 395 | int CBB_add_zeros(CBB *cbb, size_t len) { |
| 396 | uint8_t *out; |
| 397 | if (!CBB_add_space(cbb, &out, len)) { |
| 398 | return 0; |
| 399 | } |
| 400 | OPENSSL_memset(out, 0, len); |
| 401 | return 1; |
| 402 | } |
| 403 |
| 404 | int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) { |
| 405 | if (!CBB_flush(cbb) || !cbb_buffer_add(cbb_get_base(cbb), out_data, len)) { |
| 406 | return 0; |
| 407 | } |
| 408 | return 1; |
| 409 | } |
| 410 |
| 411 | int CBB_reserve(CBB *cbb, uint8_t **out_data, size_t len) { |
| 412 | if (!CBB_flush(cbb) || |
| 413 | !cbb_buffer_reserve(cbb_get_base(cbb), out_data, len)) { |
| 414 | return 0; |
| 415 | } |
| 416 | return 1; |
| 417 | } |
| 418 |
| 419 | int CBB_did_write(CBB *cbb, size_t len) { |
| 420 | struct cbb_buffer_st *base = cbb_get_base(cbb); |
| 421 | size_t newlen = base->len + len; |
| 422 | if (cbb->child != NULL || newlen < base->len || newlen > base->cap) { |
| 423 | return 0; |
| 424 | } |
| 425 | base->len = newlen; |
| 426 | return 1; |
| 427 | } |
| 428 |
| 429 | static int cbb_add_u(CBB *cbb, uint64_t v, size_t len_len) { |
| 430 | uint8_t *buf; |
| 431 | if (!CBB_add_space(cbb, &buf, len_len)) { |
| 432 | return 0; |
| 433 | } |
| 434 |
| 435 | for (size_t i = len_len - 1; i < len_len; i--) { |
| 436 | buf[i] = v; |
| 437 | v >>= 8; |
| 438 | } |
| 439 |
| 440 | // |v| must fit in |len_len| bytes. |
| 441 | if (v != 0) { |
| 442 | cbb_on_error(cbb); |
| 443 | return 0; |
| 444 | } |
| 445 |
| 446 | return 1; |
| 447 | } |
| 448 |
| 449 | int CBB_add_u8(CBB *cbb, uint8_t value) { return cbb_add_u(cbb, value, 1); } |
| 450 |
| 451 | int CBB_add_u16(CBB *cbb, uint16_t value) { return cbb_add_u(cbb, value, 2); } |
| 452 |
| 453 | int CBB_add_u16le(CBB *cbb, uint16_t value) { |
| 454 | return CBB_add_u16(cbb, CRYPTO_bswap2(value)); |
| 455 | } |
| 456 |
| 457 | int CBB_add_u24(CBB *cbb, uint32_t value) { return cbb_add_u(cbb, value, 3); } |
| 458 |
| 459 | int CBB_add_u32(CBB *cbb, uint32_t value) { return cbb_add_u(cbb, value, 4); } |
| 460 |
| 461 | int CBB_add_u32le(CBB *cbb, uint32_t value) { |
| 462 | return CBB_add_u32(cbb, CRYPTO_bswap4(value)); |
| 463 | } |
| 464 |
| 465 | int CBB_add_u64(CBB *cbb, uint64_t value) { return cbb_add_u(cbb, value, 8); } |
| 466 |
| 467 | int CBB_add_u64le(CBB *cbb, uint64_t value) { |
| 468 | return CBB_add_u64(cbb, CRYPTO_bswap8(value)); |
| 469 | } |
| 470 |
| 471 | void CBB_discard_child(CBB *cbb) { |
| 472 | if (cbb->child == NULL) { |
| 473 | return; |
| 474 | } |
| 475 |
| 476 | struct cbb_buffer_st *base = cbb_get_base(cbb); |
| 477 | assert(cbb->child->is_child); |
| 478 | base->len = cbb->child->u.child.offset; |
| 479 |
| 480 | cbb->child->u.child.base = NULL; |
| 481 | cbb->child = NULL; |
| 482 | } |
| 483 |
| 484 | int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) { |
| 485 | return CBB_add_asn1_uint64_with_tag(cbb, value, CBS_ASN1_INTEGER); |
| 486 | } |
| 487 |
| 488 | int CBB_add_asn1_uint64_with_tag(CBB *cbb, uint64_t value, CBS_ASN1_TAG tag) { |
| 489 | CBB child; |
| 490 | int started = 0; |
| 491 | if (!CBB_add_asn1(cbb, &child, tag)) { |
| 492 | goto err; |
| 493 | } |
| 494 |
| 495 | for (size_t i = 0; i < 8; i++) { |
| 496 | uint8_t byte = (value >> 8 * (7 - i)) & 0xff; |
| 497 | if (!started) { |
| 498 | if (byte == 0) { |
| 499 | // Don't encode leading zeros. |
| 500 | continue; |
| 501 | } |
| 502 | // If the high bit is set, add a padding byte to make it |
| 503 | // unsigned. |
| 504 | if ((byte & 0x80) && !CBB_add_u8(&child, 0)) { |
| 505 | goto err; |
| 506 | } |
| 507 | started = 1; |
| 508 | } |
| 509 | if (!CBB_add_u8(&child, byte)) { |
| 510 | goto err; |
| 511 | } |
| 512 | } |
| 513 |
| 514 | // 0 is encoded as a single 0, not the empty string. |
| 515 | if (!started && !CBB_add_u8(&child, 0)) { |
| 516 | goto err; |
| 517 | } |
| 518 |
| 519 | return CBB_flush(cbb); |
| 520 |
| 521 | err: |
| 522 | cbb_on_error(cbb); |
| 523 | return 0; |
| 524 | } |
| 525 |
| 526 | int CBB_add_asn1_int64(CBB *cbb, int64_t value) { |
| 527 | return CBB_add_asn1_int64_with_tag(cbb, value, CBS_ASN1_INTEGER); |
| 528 | } |
| 529 |
| 530 | int CBB_add_asn1_int64_with_tag(CBB *cbb, int64_t value, CBS_ASN1_TAG tag) { |
| 531 | if (value >= 0) { |
| 532 | return CBB_add_asn1_uint64_with_tag(cbb, (uint64_t)value, tag); |
| 533 | } |
| 534 |
| 535 | uint8_t bytes[sizeof(int64_t)]; |
| 536 | memcpy(bytes, &value, sizeof(value)); |
| 537 | int start = 7; |
| 538 | // Skip leading sign-extension bytes unless they are necessary. |
| 539 | while (start > 0 && (bytes[start] == 0xff && (bytes[start - 1] & 0x80))) { |
| 540 | start--; |
| 541 | } |
| 542 |
| 543 | CBB child; |
| 544 | if (!CBB_add_asn1(cbb, &child, tag)) { |
| 545 | goto err; |
| 546 | } |
| 547 | for (int i = start; i >= 0; i--) { |
| 548 | if (!CBB_add_u8(&child, bytes[i])) { |
| 549 | goto err; |
| 550 | } |
| 551 | } |
| 552 | return CBB_flush(cbb); |
| 553 |
| 554 | err: |
| 555 | cbb_on_error(cbb); |
| 556 | return 0; |
| 557 | } |
| 558 |
| 559 | int CBB_add_asn1_octet_string(CBB *cbb, const uint8_t *data, size_t data_len) { |
| 560 | CBB child; |
| 561 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_OCTETSTRING) || |
| 562 | !CBB_add_bytes(&child, data, data_len) || !CBB_flush(cbb)) { |
| 563 | cbb_on_error(cbb); |
| 564 | return 0; |
| 565 | } |
| 566 |
| 567 | return 1; |
| 568 | } |
| 569 |
| 570 | int CBB_add_asn1_bool(CBB *cbb, int value) { |
| 571 | CBB child; |
| 572 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_BOOLEAN) || |
| 573 | !CBB_add_u8(&child, value != 0 ? 0xff : 0) || !CBB_flush(cbb)) { |
| 574 | cbb_on_error(cbb); |
| 575 | return 0; |
| 576 | } |
| 577 |
| 578 | return 1; |
| 579 | } |
| 580 |
| 581 | // parse_dotted_decimal parses one decimal component from |cbs|, where |cbs| is |
| 582 | // an OID literal, e.g., "1.2.840.113554.4.1.72585". It consumes both the |
| 583 | // component and the dot, so |cbs| may be passed into the function again for the |
| 584 | // next value. |
| 585 | static int parse_dotted_decimal(CBS *cbs, uint64_t *out) { |
| 586 | if (!CBS_get_u64_decimal(cbs, out)) { |
| 587 | return 0; |
| 588 | } |
| 589 |
| 590 | // The integer must have either ended at the end of the string, or a |
| 591 | // non-terminal dot, which should be consumed. If the string ends with a dot, |
| 592 | // this is not a valid OID string. |
| 593 | uint8_t dot; |
| 594 | return !CBS_get_u8(cbs, &dot) || (dot == '.' && CBS_len(cbs) > 0); |
| 595 | } |
| 596 |
| 597 | int CBB_add_asn1_oid_from_text(CBB *cbb, const char *text, size_t len) { |
| 598 | if (!CBB_flush(cbb)) { |
| 599 | return 0; |
| 600 | } |
| 601 |
| 602 | CBS cbs; |
| 603 | CBS_init(&cbs, (const uint8_t *)text, len); |
| 604 |
| 605 | // OIDs must have at least two components. |
| 606 | uint64_t a, b; |
| 607 | if (!parse_dotted_decimal(&cbs, &a) || !parse_dotted_decimal(&cbs, &b)) { |
| 608 | return 0; |
| 609 | } |
| 610 |
| 611 | // The first component is encoded as 40 * |a| + |b|. This assumes that |a| is |
| 612 | // 0, 1, or 2 and that, when it is 0 or 1, |b| is at most 39. |
| 613 | if (a > 2 || (a < 2 && b > 39) || b > UINT64_MAX - 80 || |
| 614 | !add_base128_integer(cbb, 40u * a + b)) { |
| 615 | return 0; |
| 616 | } |
| 617 |
| 618 | // The remaining components are encoded unmodified. |
| 619 | while (CBS_len(&cbs) > 0) { |
| 620 | if (!parse_dotted_decimal(&cbs, &a) || !add_base128_integer(cbb, a)) { |
| 621 | return 0; |
| 622 | } |
| 623 | } |
| 624 |
| 625 | return 1; |
| 626 | } |
| 627 |
| 628 | static int compare_set_of_element(const void *a_ptr, const void *b_ptr) { |
| 629 | // See X.690, section 11.6 for the ordering. They are sorted in ascending |
| 630 | // order by their DER encoding. |
| 631 | const CBS *a = reinterpret_cast<const CBS *>(a_ptr), |
| 632 | *b = reinterpret_cast<const CBS *>(b_ptr); |
| 633 | size_t a_len = CBS_len(a), b_len = CBS_len(b); |
| 634 | size_t min_len = a_len < b_len ? a_len : b_len; |
| 635 | int ret = OPENSSL_memcmp(CBS_data(a), CBS_data(b), min_len); |
| 636 | if (ret != 0) { |
| 637 | return ret; |
| 638 | } |
| 639 | if (a_len == b_len) { |
| 640 | return 0; |
| 641 | } |
| 642 | // If one is a prefix of the other, the shorter one sorts first. (This is not |
| 643 | // actually reachable. No DER encoding is a prefix of another DER encoding.) |
| 644 | return a_len < b_len ? -1 : 1; |
| 645 | } |
| 646 |
| 647 | int CBB_flush_asn1_set_of(CBB *cbb) { |
| 648 | if (!CBB_flush(cbb)) { |
| 649 | return 0; |
| 650 | } |
| 651 |
| 652 | CBS cbs; |
| 653 | size_t num_children = 0; |
| 654 | CBS_init(&cbs, CBB_data(cbb), CBB_len(cbb)); |
| 655 | while (CBS_len(&cbs) != 0) { |
| 656 | if (!CBS_get_any_asn1_element(&cbs, NULL, NULL, NULL)) { |
| 657 | OPENSSL_PUT_ERROR(CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 658 | return 0; |
| 659 | } |
| 660 | num_children++; |
| 661 | } |
| 662 |
| 663 | if (num_children < 2) { |
| 664 | return 1; // Nothing to do. This is the common case for X.509. |
| 665 | } |
| 666 |
| 667 | // Parse out the children and sort. We alias them into a copy of so they |
| 668 | // remain valid as we rewrite |cbb|. |
| 669 | int ret = 0; |
| 670 | size_t buf_len = CBB_len(cbb); |
| 671 | uint8_t *buf = |
| 672 | reinterpret_cast<uint8_t *>(OPENSSL_memdup(CBB_data(cbb), buf_len)); |
| 673 | CBS *children = |
| 674 | reinterpret_cast<CBS *>(OPENSSL_calloc(num_children, sizeof(CBS))); |
| 675 | uint8_t *out; |
| 676 | size_t offset = 0; |
| 677 | if (buf == NULL || children == NULL) { |
| 678 | goto err; |
| 679 | } |
| 680 | CBS_init(&cbs, buf, buf_len); |
| 681 | for (size_t i = 0; i < num_children; i++) { |
| 682 | if (!CBS_get_any_asn1_element(&cbs, &children[i], NULL, NULL)) { |
| 683 | goto err; |
| 684 | } |
| 685 | } |
| 686 | qsort(children, num_children, sizeof(CBS), compare_set_of_element); |
| 687 |
| 688 | // Write the contents back in the new order. |
| 689 | out = (uint8_t *)CBB_data(cbb); |
| 690 | for (size_t i = 0; i < num_children; i++) { |
| 691 | OPENSSL_memcpy(out + offset, CBS_data(&children[i]), CBS_len(&children[i])); |
| 692 | offset += CBS_len(&children[i]); |
| 693 | } |
| 694 | assert(offset == buf_len); |
| 695 |
| 696 | ret = 1; |
| 697 |
| 698 | err: |
| 699 | OPENSSL_free(buf); |
| 700 | OPENSSL_free(children); |
| 701 | return ret; |
| 702 | } |
| 703 |