der.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/cal/private/der.h>
  6. #include <aws/cal/cal.h>
  7. #include <aws/common/byte_buf.h>
  8. #ifdef _MSC_VER
  9. # pragma warning(push)
  10. # pragma warning(disable : 4204 4221) /* non-standard aggregate initializer warnings */
  11. #endif
  12. struct aws_der_encoder {
  13. struct aws_allocator *allocator;
  14. struct aws_byte_buf storage;
  15. struct aws_byte_buf *buffer; /* buffer being written to, might be storage, might be a sequence/set buffer */
  16. struct aws_array_list stack;
  17. };
  18. struct aws_der_decoder {
  19. struct aws_allocator *allocator;
  20. struct aws_array_list tlvs; /* parsed elements */
  21. int tlv_idx; /* index to elements after parsing */
  22. struct aws_byte_cursor input; /* input buffer */
  23. uint32_t depth; /* recursion depth when expanding containers */
  24. struct der_tlv *container; /* currently expanding container */
  25. };
  26. struct der_tlv {
  27. uint8_t tag;
  28. uint32_t length; /* length of value in bytes */
  29. uint32_t count; /* SEQUENCE or SET element count */
  30. uint8_t *value;
  31. };
  32. static void s_decode_tlv(struct der_tlv *tlv) {
  33. if (tlv->tag == AWS_DER_INTEGER) {
  34. uint8_t first_byte = tlv->value[0];
  35. /* if the first byte is 0, it just denotes unsigned and should be removed */
  36. if (first_byte == 0x00) {
  37. tlv->length -= 1;
  38. tlv->value += 1;
  39. }
  40. } else if (tlv->tag == AWS_DER_BIT_STRING) {
  41. /* skip over the trailing skipped bit count */
  42. tlv->length -= 1;
  43. tlv->value += 1;
  44. }
  45. }
  46. static int s_der_read_tlv(struct aws_byte_cursor *cur, struct der_tlv *tlv) {
  47. uint8_t tag = 0;
  48. uint8_t len_bytes = 0;
  49. uint32_t len = 0;
  50. if (!aws_byte_cursor_read_u8(cur, &tag)) {
  51. return AWS_OP_ERR;
  52. }
  53. if (!aws_byte_cursor_read_u8(cur, &len_bytes)) {
  54. return AWS_OP_ERR;
  55. }
  56. /* if the sign bit is set, then the first byte is the number of bytes required to store
  57. * the length */
  58. if (len_bytes & 0x80) {
  59. len_bytes &= 0x7f;
  60. switch (len_bytes) {
  61. case 1:
  62. if (!aws_byte_cursor_read_u8(cur, (uint8_t *)&len)) {
  63. return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED);
  64. }
  65. break;
  66. case 2:
  67. if (!aws_byte_cursor_read_be16(cur, (uint16_t *)&len)) {
  68. return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED);
  69. }
  70. break;
  71. case 4:
  72. if (!aws_byte_cursor_read_be32(cur, &len)) {
  73. return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED);
  74. }
  75. break;
  76. default:
  77. return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED);
  78. }
  79. } else {
  80. len = len_bytes;
  81. }
  82. tlv->tag = tag;
  83. tlv->length = len;
  84. tlv->value = (tag == AWS_DER_NULL) ? NULL : cur->ptr;
  85. s_decode_tlv(tlv);
  86. aws_byte_cursor_advance(cur, len);
  87. return AWS_OP_SUCCESS;
  88. }
  89. static uint32_t s_encoded_len(struct der_tlv *tlv) {
  90. if (tlv->tag == AWS_DER_INTEGER) {
  91. uint8_t first_byte = tlv->value[0];
  92. /* if the first byte has the high bit set, a 0 will be prepended to denote unsigned */
  93. return tlv->length + ((first_byte & 0x80) != 0);
  94. }
  95. if (tlv->tag == AWS_DER_BIT_STRING) {
  96. return tlv->length + 1; /* needs a byte to denote how many trailing skipped bits */
  97. }
  98. return tlv->length;
  99. }
  100. static int s_der_write_tlv(struct der_tlv *tlv, struct aws_byte_buf *buf) {
  101. if (!aws_byte_buf_write_u8(buf, tlv->tag)) {
  102. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  103. }
  104. uint32_t len = s_encoded_len(tlv);
  105. if (len > UINT16_MAX) {
  106. /* write the high bit plus 4 byte length */
  107. if (!aws_byte_buf_write_u8(buf, 0x84)) {
  108. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  109. }
  110. if (!aws_byte_buf_write_be32(buf, len)) {
  111. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  112. }
  113. } else if (len > UINT8_MAX) {
  114. /* write the high bit plus 2 byte length */
  115. if (!aws_byte_buf_write_u8(buf, 0x82)) {
  116. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  117. }
  118. if (!aws_byte_buf_write_be16(buf, (uint16_t)len)) {
  119. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  120. }
  121. } else if (len > INT8_MAX) {
  122. /* Write the high bit + 1 byte length */
  123. if (!aws_byte_buf_write_u8(buf, 0x81)) {
  124. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  125. }
  126. if (!aws_byte_buf_write_u8(buf, (uint8_t)len)) {
  127. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  128. }
  129. } else {
  130. if (!aws_byte_buf_write_u8(buf, (uint8_t)len)) {
  131. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  132. }
  133. }
  134. switch (tlv->tag) {
  135. case AWS_DER_INTEGER: {
  136. /* if the first byte has the sign bit set, insert an extra 0x00 byte to indicate unsigned */
  137. uint8_t first_byte = tlv->value[0];
  138. if (first_byte & 0x80) {
  139. if (!aws_byte_buf_write_u8(buf, 0)) {
  140. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  141. }
  142. }
  143. if (!aws_byte_buf_write(buf, tlv->value, tlv->length)) {
  144. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  145. }
  146. } break;
  147. case AWS_DER_BOOLEAN:
  148. if (!aws_byte_buf_write_u8(buf, (*tlv->value) ? 0xff : 0x00)) {
  149. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  150. }
  151. break;
  152. case AWS_DER_BIT_STRING:
  153. /* Write that there are 0 skipped bits */
  154. if (!aws_byte_buf_write_u8(buf, 0)) {
  155. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  156. }
  157. /* FALLTHROUGH */
  158. case AWS_DER_BMPString:
  159. case AWS_DER_IA5String:
  160. case AWS_DER_PrintableString:
  161. case AWS_DER_UTF8_STRING:
  162. case AWS_DER_OBJECT_IDENTIFIER:
  163. case AWS_DER_OCTET_STRING:
  164. case AWS_DER_SEQUENCE:
  165. case AWS_DER_SET:
  166. if (!aws_byte_buf_write(buf, tlv->value, tlv->length)) {
  167. return aws_raise_error(AWS_ERROR_INVALID_BUFFER_SIZE);
  168. }
  169. break;
  170. case AWS_DER_NULL:
  171. /* No value bytes */
  172. break;
  173. default:
  174. return aws_raise_error(AWS_ERROR_CAL_MISMATCHED_DER_TYPE);
  175. }
  176. return AWS_OP_SUCCESS;
  177. }
  178. struct aws_der_encoder *aws_der_encoder_new(struct aws_allocator *allocator, size_t capacity) {
  179. struct aws_der_encoder *encoder = aws_mem_calloc(allocator, 1, sizeof(struct aws_der_encoder));
  180. AWS_FATAL_ASSERT(encoder);
  181. encoder->allocator = allocator;
  182. if (aws_byte_buf_init(&encoder->storage, encoder->allocator, capacity)) {
  183. goto error;
  184. }
  185. if (aws_array_list_init_dynamic(&encoder->stack, encoder->allocator, 4, sizeof(struct der_tlv))) {
  186. goto error;
  187. }
  188. encoder->buffer = &encoder->storage;
  189. return encoder;
  190. error:
  191. aws_array_list_clean_up(&encoder->stack);
  192. aws_byte_buf_clean_up(&encoder->storage);
  193. aws_mem_release(allocator, encoder);
  194. return NULL;
  195. }
  196. void aws_der_encoder_destroy(struct aws_der_encoder *encoder) {
  197. if (!encoder) {
  198. return;
  199. }
  200. aws_byte_buf_clean_up_secure(&encoder->storage);
  201. aws_array_list_clean_up(&encoder->stack);
  202. aws_mem_release(encoder->allocator, encoder);
  203. }
  204. int aws_der_encoder_write_integer(struct aws_der_encoder *encoder, struct aws_byte_cursor integer) {
  205. AWS_FATAL_ASSERT(integer.len <= UINT32_MAX);
  206. struct der_tlv tlv = {
  207. .tag = AWS_DER_INTEGER,
  208. .length = (uint32_t)integer.len,
  209. .value = integer.ptr,
  210. };
  211. return s_der_write_tlv(&tlv, encoder->buffer);
  212. }
  213. int aws_der_encoder_write_boolean(struct aws_der_encoder *encoder, bool boolean) {
  214. struct der_tlv tlv = {.tag = AWS_DER_BOOLEAN, .length = 1, .value = (uint8_t *)&boolean};
  215. return s_der_write_tlv(&tlv, encoder->buffer);
  216. }
  217. int aws_der_encoder_write_null(struct aws_der_encoder *encoder) {
  218. struct der_tlv tlv = {
  219. .tag = AWS_DER_NULL,
  220. .length = 0,
  221. .value = NULL,
  222. };
  223. return s_der_write_tlv(&tlv, encoder->buffer);
  224. }
  225. int aws_der_encoder_write_bit_string(struct aws_der_encoder *encoder, struct aws_byte_cursor bit_string) {
  226. AWS_FATAL_ASSERT(bit_string.len <= UINT32_MAX);
  227. struct der_tlv tlv = {
  228. .tag = AWS_DER_BIT_STRING,
  229. .length = (uint32_t)bit_string.len,
  230. .value = bit_string.ptr,
  231. };
  232. return s_der_write_tlv(&tlv, encoder->buffer);
  233. }
  234. int aws_der_encoder_write_octet_string(struct aws_der_encoder *encoder, struct aws_byte_cursor octet_string) {
  235. AWS_FATAL_ASSERT(octet_string.len <= UINT32_MAX);
  236. struct der_tlv tlv = {
  237. .tag = AWS_DER_OCTET_STRING,
  238. .length = (uint32_t)octet_string.len,
  239. .value = octet_string.ptr,
  240. };
  241. return s_der_write_tlv(&tlv, encoder->buffer);
  242. }
  243. static int s_der_encoder_begin_container(struct aws_der_encoder *encoder, enum aws_der_type type) {
  244. struct aws_byte_buf *seq_buf = aws_mem_acquire(encoder->allocator, sizeof(struct aws_byte_buf));
  245. AWS_FATAL_ASSERT(seq_buf);
  246. if (aws_byte_buf_init(seq_buf, encoder->allocator, encoder->storage.capacity)) {
  247. return AWS_OP_ERR;
  248. }
  249. struct der_tlv tlv_seq = {
  250. .tag = type,
  251. .length = 0, /* not known yet, will update later */
  252. .value = (void *)seq_buf,
  253. };
  254. if (aws_array_list_push_back(&encoder->stack, &tlv_seq)) {
  255. aws_byte_buf_clean_up(seq_buf);
  256. return AWS_OP_ERR;
  257. }
  258. encoder->buffer = seq_buf;
  259. return AWS_OP_SUCCESS;
  260. }
  261. static int s_der_encoder_end_container(struct aws_der_encoder *encoder) {
  262. struct der_tlv tlv;
  263. if (aws_array_list_back(&encoder->stack, &tlv)) {
  264. return AWS_OP_ERR;
  265. }
  266. aws_array_list_pop_back(&encoder->stack);
  267. /* update the buffer to point at the next container on the stack */
  268. if (encoder->stack.length > 0) {
  269. struct der_tlv outer;
  270. if (aws_array_list_back(&encoder->stack, &outer)) {
  271. return AWS_OP_ERR;
  272. }
  273. encoder->buffer = (struct aws_byte_buf *)outer.value;
  274. } else {
  275. encoder->buffer = &encoder->storage;
  276. }
  277. struct aws_byte_buf *seq_buf = (struct aws_byte_buf *)tlv.value;
  278. tlv.length = (uint32_t)seq_buf->len;
  279. tlv.value = seq_buf->buffer;
  280. int result = s_der_write_tlv(&tlv, encoder->buffer);
  281. aws_byte_buf_clean_up_secure(seq_buf);
  282. aws_mem_release(encoder->allocator, seq_buf);
  283. return result;
  284. }
  285. int aws_der_encoder_begin_sequence(struct aws_der_encoder *encoder) {
  286. return s_der_encoder_begin_container(encoder, AWS_DER_SEQUENCE);
  287. }
  288. int aws_der_encoder_end_sequence(struct aws_der_encoder *encoder) {
  289. return s_der_encoder_end_container(encoder);
  290. }
  291. int aws_der_encoder_begin_set(struct aws_der_encoder *encoder) {
  292. return s_der_encoder_begin_container(encoder, AWS_DER_SET);
  293. }
  294. int aws_der_encoder_end_set(struct aws_der_encoder *encoder) {
  295. return s_der_encoder_end_container(encoder);
  296. }
  297. int aws_der_encoder_get_contents(struct aws_der_encoder *encoder, struct aws_byte_cursor *contents) {
  298. if (encoder->storage.len == 0) {
  299. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  300. }
  301. if (encoder->buffer != &encoder->storage) {
  302. /* someone forgot to end a sequence or set */
  303. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  304. }
  305. *contents = aws_byte_cursor_from_buf(&encoder->storage);
  306. return AWS_OP_SUCCESS;
  307. }
  308. /*
  309. * DECODER
  310. */
  311. int s_decoder_parse(struct aws_der_decoder *decoder);
  312. struct aws_der_decoder *aws_der_decoder_new(struct aws_allocator *allocator, struct aws_byte_cursor input) {
  313. struct aws_der_decoder *decoder = aws_mem_calloc(allocator, 1, sizeof(struct aws_der_decoder));
  314. AWS_FATAL_ASSERT(decoder);
  315. decoder->allocator = allocator;
  316. decoder->input = input;
  317. decoder->tlv_idx = -1;
  318. decoder->depth = 0;
  319. decoder->container = NULL;
  320. if (aws_array_list_init_dynamic(&decoder->tlvs, decoder->allocator, 16, sizeof(struct der_tlv))) {
  321. goto error;
  322. }
  323. if (s_decoder_parse(decoder)) {
  324. goto error;
  325. }
  326. return decoder;
  327. error:
  328. aws_array_list_clean_up(&decoder->tlvs);
  329. aws_mem_release(allocator, decoder);
  330. return NULL;
  331. }
  332. void aws_der_decoder_destroy(struct aws_der_decoder *decoder) {
  333. if (!decoder) {
  334. return;
  335. }
  336. aws_array_list_clean_up(&decoder->tlvs);
  337. aws_mem_release(decoder->allocator, decoder);
  338. }
  339. int s_parse_cursor(struct aws_der_decoder *decoder, struct aws_byte_cursor cur) {
  340. if (++decoder->depth > 16) {
  341. /* stream contains too many nested containers, probably malformed/attack */
  342. return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED);
  343. }
  344. while (cur.len) {
  345. struct der_tlv tlv = {0};
  346. if (s_der_read_tlv(&cur, &tlv)) {
  347. return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED);
  348. }
  349. /* skip trailing newlines in the stream after any TLV */
  350. while (cur.len && *cur.ptr == '\n') {
  351. aws_byte_cursor_advance(&cur, 1);
  352. }
  353. if (aws_array_list_push_back(&decoder->tlvs, &tlv)) {
  354. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  355. }
  356. if (decoder->container) {
  357. decoder->container->count++;
  358. }
  359. /* if the last element was a container, expand it recursively to maintain order */
  360. if (tlv.tag & AWS_DER_FORM_CONSTRUCTED) {
  361. struct der_tlv *outer_container = decoder->container;
  362. struct der_tlv *container = NULL;
  363. aws_array_list_get_at_ptr(&decoder->tlvs, (void **)&container, decoder->tlvs.length - 1);
  364. decoder->container = container;
  365. if (!container) {
  366. return aws_raise_error(AWS_ERROR_INVALID_STATE);
  367. }
  368. struct aws_byte_cursor container_cur = aws_byte_cursor_from_array(container->value, container->length);
  369. if (s_parse_cursor(decoder, container_cur)) {
  370. return aws_raise_error(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED);
  371. }
  372. decoder->container = outer_container; /* restore the container stack */
  373. }
  374. }
  375. --decoder->depth;
  376. return AWS_OP_SUCCESS;
  377. }
  378. int s_decoder_parse(struct aws_der_decoder *decoder) {
  379. return s_parse_cursor(decoder, decoder->input);
  380. }
  381. bool aws_der_decoder_next(struct aws_der_decoder *decoder) {
  382. return (++decoder->tlv_idx < (int)decoder->tlvs.length);
  383. }
  384. static struct der_tlv s_decoder_tlv(struct aws_der_decoder *decoder) {
  385. AWS_FATAL_ASSERT(decoder->tlv_idx < (int)decoder->tlvs.length);
  386. struct der_tlv tlv = {0};
  387. aws_array_list_get_at(&decoder->tlvs, &tlv, decoder->tlv_idx);
  388. return tlv;
  389. }
  390. enum aws_der_type aws_der_decoder_tlv_type(struct aws_der_decoder *decoder) {
  391. struct der_tlv tlv = s_decoder_tlv(decoder);
  392. return tlv.tag;
  393. }
  394. size_t aws_der_decoder_tlv_length(struct aws_der_decoder *decoder) {
  395. struct der_tlv tlv = s_decoder_tlv(decoder);
  396. return tlv.length;
  397. }
  398. size_t aws_der_decoder_tlv_count(struct aws_der_decoder *decoder) {
  399. struct der_tlv tlv = s_decoder_tlv(decoder);
  400. AWS_FATAL_ASSERT(tlv.tag & AWS_DER_FORM_CONSTRUCTED);
  401. return tlv.count;
  402. }
  403. static void s_tlv_to_blob(struct der_tlv *tlv, struct aws_byte_cursor *blob) {
  404. AWS_FATAL_ASSERT(tlv->tag != AWS_DER_NULL);
  405. *blob = aws_byte_cursor_from_array(tlv->value, tlv->length);
  406. }
  407. int aws_der_decoder_tlv_string(struct aws_der_decoder *decoder, struct aws_byte_cursor *string) {
  408. struct der_tlv tlv = s_decoder_tlv(decoder);
  409. if (tlv.tag != AWS_DER_OCTET_STRING && tlv.tag != AWS_DER_BIT_STRING) {
  410. return aws_raise_error(AWS_ERROR_CAL_MISMATCHED_DER_TYPE);
  411. }
  412. s_tlv_to_blob(&tlv, string);
  413. return AWS_OP_SUCCESS;
  414. }
  415. int aws_der_decoder_tlv_integer(struct aws_der_decoder *decoder, struct aws_byte_cursor *integer) {
  416. struct der_tlv tlv = s_decoder_tlv(decoder);
  417. if (tlv.tag != AWS_DER_INTEGER) {
  418. return aws_raise_error(AWS_ERROR_CAL_MISMATCHED_DER_TYPE);
  419. }
  420. s_tlv_to_blob(&tlv, integer);
  421. return AWS_OP_SUCCESS;
  422. }
  423. int aws_der_decoder_tlv_boolean(struct aws_der_decoder *decoder, bool *boolean) {
  424. struct der_tlv tlv = s_decoder_tlv(decoder);
  425. if (tlv.tag != AWS_DER_BOOLEAN) {
  426. return aws_raise_error(AWS_ERROR_CAL_MISMATCHED_DER_TYPE);
  427. }
  428. *boolean = *tlv.value != 0;
  429. return AWS_OP_SUCCESS;
  430. }
  431. int aws_der_decoder_tlv_blob(struct aws_der_decoder *decoder, struct aws_byte_cursor *blob) {
  432. struct der_tlv tlv = s_decoder_tlv(decoder);
  433. s_tlv_to_blob(&tlv, blob);
  434. return AWS_OP_SUCCESS;
  435. }
  436. #ifdef _MSC_VER
  437. # pragma warning(pop)
  438. #endif