asn1write.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * ASN.1 buffer writing functionality
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_ASN1_WRITE_C) || defined(MBEDTLS_X509_USE_C) || \
  9. defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
  10. #include "mbedtls/asn1write.h"
  11. #include "mbedtls/error.h"
  12. #include <string.h>
  13. #include "mbedtls/platform.h"
  14. #if defined(MBEDTLS_ASN1_PARSE_C)
  15. #include "mbedtls/asn1.h"
  16. #endif
  17. int mbedtls_asn1_write_len(unsigned char **p, const unsigned char *start, size_t len)
  18. {
  19. #if SIZE_MAX > 0xFFFFFFFF
  20. if (len > 0xFFFFFFFF) {
  21. return MBEDTLS_ERR_ASN1_INVALID_LENGTH;
  22. }
  23. #endif
  24. int required = 1;
  25. if (len >= 0x80) {
  26. for (size_t l = len; l != 0; l >>= 8) {
  27. required++;
  28. }
  29. }
  30. if (required > (*p - start)) {
  31. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  32. }
  33. do {
  34. *--(*p) = MBEDTLS_BYTE_0(len);
  35. len >>= 8;
  36. } while (len);
  37. if (required > 1) {
  38. *--(*p) = (unsigned char) (0x80 + required - 1);
  39. }
  40. return required;
  41. }
  42. int mbedtls_asn1_write_tag(unsigned char **p, const unsigned char *start, unsigned char tag)
  43. {
  44. if (*p - start < 1) {
  45. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  46. }
  47. *--(*p) = tag;
  48. return 1;
  49. }
  50. #endif /* MBEDTLS_ASN1_WRITE_C || MBEDTLS_X509_USE_C || MBEDTLS_PSA_UTIL_HAVE_ECDSA */
  51. #if defined(MBEDTLS_ASN1_WRITE_C)
  52. static int mbedtls_asn1_write_len_and_tag(unsigned char **p,
  53. const unsigned char *start,
  54. size_t len,
  55. unsigned char tag)
  56. {
  57. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  58. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  59. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, tag));
  60. return (int) len;
  61. }
  62. int mbedtls_asn1_write_raw_buffer(unsigned char **p, const unsigned char *start,
  63. const unsigned char *buf, size_t size)
  64. {
  65. size_t len = 0;
  66. if (*p < start || (size_t) (*p - start) < size) {
  67. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  68. }
  69. len = size;
  70. (*p) -= len;
  71. memcpy(*p, buf, len);
  72. return (int) len;
  73. }
  74. #if defined(MBEDTLS_BIGNUM_C)
  75. int mbedtls_asn1_write_mpi(unsigned char **p, const unsigned char *start, const mbedtls_mpi *X)
  76. {
  77. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  78. size_t len = 0;
  79. // Write the MPI
  80. //
  81. len = mbedtls_mpi_size(X);
  82. /* DER represents 0 with a sign bit (0=nonnegative) and 7 value bits, not
  83. * as 0 digits. We need to end up with 020100, not with 0200. */
  84. if (len == 0) {
  85. len = 1;
  86. }
  87. if (*p < start || (size_t) (*p - start) < len) {
  88. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  89. }
  90. (*p) -= len;
  91. MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(X, *p, len));
  92. // DER format assumes 2s complement for numbers, so the leftmost bit
  93. // should be 0 for positive numbers and 1 for negative numbers.
  94. //
  95. if (X->s == 1 && **p & 0x80) {
  96. if (*p - start < 1) {
  97. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  98. }
  99. *--(*p) = 0x00;
  100. len += 1;
  101. }
  102. ret = mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_INTEGER);
  103. cleanup:
  104. return ret;
  105. }
  106. #endif /* MBEDTLS_BIGNUM_C */
  107. int mbedtls_asn1_write_null(unsigned char **p, const unsigned char *start)
  108. {
  109. // Write NULL
  110. //
  111. return mbedtls_asn1_write_len_and_tag(p, start, 0, MBEDTLS_ASN1_NULL);
  112. }
  113. int mbedtls_asn1_write_oid(unsigned char **p, const unsigned char *start,
  114. const char *oid, size_t oid_len)
  115. {
  116. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  117. size_t len = 0;
  118. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
  119. (const unsigned char *) oid, oid_len));
  120. return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OID);
  121. }
  122. int mbedtls_asn1_write_algorithm_identifier(unsigned char **p, const unsigned char *start,
  123. const char *oid, size_t oid_len,
  124. size_t par_len)
  125. {
  126. return mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len, par_len, 1);
  127. }
  128. int mbedtls_asn1_write_algorithm_identifier_ext(unsigned char **p, const unsigned char *start,
  129. const char *oid, size_t oid_len,
  130. size_t par_len, int has_par)
  131. {
  132. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  133. size_t len = 0;
  134. if (has_par) {
  135. if (par_len == 0) {
  136. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_null(p, start));
  137. } else {
  138. len += par_len;
  139. }
  140. }
  141. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid, oid_len));
  142. return mbedtls_asn1_write_len_and_tag(p, start, len,
  143. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
  144. }
  145. int mbedtls_asn1_write_bool(unsigned char **p, const unsigned char *start, int boolean)
  146. {
  147. size_t len = 0;
  148. if (*p - start < 1) {
  149. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  150. }
  151. *--(*p) = (boolean) ? 255 : 0;
  152. len++;
  153. return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BOOLEAN);
  154. }
  155. static int asn1_write_tagged_int(unsigned char **p, const unsigned char *start, int val, int tag)
  156. {
  157. size_t len = 0;
  158. do {
  159. if (*p - start < 1) {
  160. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  161. }
  162. len += 1;
  163. *--(*p) = val & 0xff;
  164. val >>= 8;
  165. } while (val > 0);
  166. if (**p & 0x80) {
  167. if (*p - start < 1) {
  168. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  169. }
  170. *--(*p) = 0x00;
  171. len += 1;
  172. }
  173. return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
  174. }
  175. int mbedtls_asn1_write_int(unsigned char **p, const unsigned char *start, int val)
  176. {
  177. return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_INTEGER);
  178. }
  179. int mbedtls_asn1_write_enum(unsigned char **p, const unsigned char *start, int val)
  180. {
  181. return asn1_write_tagged_int(p, start, val, MBEDTLS_ASN1_ENUMERATED);
  182. }
  183. int mbedtls_asn1_write_tagged_string(unsigned char **p, const unsigned char *start, int tag,
  184. const char *text, size_t text_len)
  185. {
  186. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  187. size_t len = 0;
  188. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
  189. (const unsigned char *) text,
  190. text_len));
  191. return mbedtls_asn1_write_len_and_tag(p, start, len, tag);
  192. }
  193. int mbedtls_asn1_write_utf8_string(unsigned char **p, const unsigned char *start,
  194. const char *text, size_t text_len)
  195. {
  196. return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_UTF8_STRING, text, text_len);
  197. }
  198. int mbedtls_asn1_write_printable_string(unsigned char **p, const unsigned char *start,
  199. const char *text, size_t text_len)
  200. {
  201. return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_PRINTABLE_STRING, text,
  202. text_len);
  203. }
  204. int mbedtls_asn1_write_ia5_string(unsigned char **p, const unsigned char *start,
  205. const char *text, size_t text_len)
  206. {
  207. return mbedtls_asn1_write_tagged_string(p, start, MBEDTLS_ASN1_IA5_STRING, text, text_len);
  208. }
  209. int mbedtls_asn1_write_named_bitstring(unsigned char **p,
  210. const unsigned char *start,
  211. const unsigned char *buf,
  212. size_t bits)
  213. {
  214. size_t unused_bits, byte_len;
  215. const unsigned char *cur_byte;
  216. unsigned char cur_byte_shifted;
  217. unsigned char bit;
  218. byte_len = (bits + 7) / 8;
  219. unused_bits = (byte_len * 8) - bits;
  220. /*
  221. * Named bitstrings require that trailing 0s are excluded in the encoding
  222. * of the bitstring. Trailing 0s are considered part of the 'unused' bits
  223. * when encoding this value in the first content octet
  224. */
  225. if (bits != 0) {
  226. cur_byte = buf + byte_len - 1;
  227. cur_byte_shifted = *cur_byte >> unused_bits;
  228. for (;;) {
  229. bit = cur_byte_shifted & 0x1;
  230. cur_byte_shifted >>= 1;
  231. if (bit != 0) {
  232. break;
  233. }
  234. bits--;
  235. if (bits == 0) {
  236. break;
  237. }
  238. if (bits % 8 == 0) {
  239. cur_byte_shifted = *--cur_byte;
  240. }
  241. }
  242. }
  243. return mbedtls_asn1_write_bitstring(p, start, buf, bits);
  244. }
  245. int mbedtls_asn1_write_bitstring(unsigned char **p, const unsigned char *start,
  246. const unsigned char *buf, size_t bits)
  247. {
  248. size_t len = 0;
  249. size_t unused_bits, byte_len;
  250. byte_len = (bits + 7) / 8;
  251. unused_bits = (byte_len * 8) - bits;
  252. if (*p < start || (size_t) (*p - start) < byte_len + 1) {
  253. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  254. }
  255. len = byte_len + 1;
  256. /* Write the bitstring. Ensure the unused bits are zeroed */
  257. if (byte_len > 0) {
  258. byte_len--;
  259. *--(*p) = buf[byte_len] & ~((0x1 << unused_bits) - 1);
  260. (*p) -= byte_len;
  261. memcpy(*p, buf, byte_len);
  262. }
  263. /* Write unused bits */
  264. *--(*p) = (unsigned char) unused_bits;
  265. return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_BIT_STRING);
  266. }
  267. int mbedtls_asn1_write_octet_string(unsigned char **p, const unsigned char *start,
  268. const unsigned char *buf, size_t size)
  269. {
  270. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  271. size_t len = 0;
  272. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, buf, size));
  273. return mbedtls_asn1_write_len_and_tag(p, start, len, MBEDTLS_ASN1_OCTET_STRING);
  274. }
  275. #if !defined(MBEDTLS_ASN1_PARSE_C)
  276. /* This is a copy of the ASN.1 parsing function mbedtls_asn1_find_named_data(),
  277. * which is replicated to avoid a dependency ASN1_WRITE_C on ASN1_PARSE_C. */
  278. static mbedtls_asn1_named_data *asn1_find_named_data(
  279. mbedtls_asn1_named_data *list,
  280. const char *oid, size_t len)
  281. {
  282. while (list != NULL) {
  283. if (list->oid.len == len &&
  284. memcmp(list->oid.p, oid, len) == 0) {
  285. break;
  286. }
  287. list = list->next;
  288. }
  289. return list;
  290. }
  291. #else
  292. #define asn1_find_named_data(list, oid, len) \
  293. ((mbedtls_asn1_named_data *) mbedtls_asn1_find_named_data(list, oid, len))
  294. #endif
  295. mbedtls_asn1_named_data *mbedtls_asn1_store_named_data(
  296. mbedtls_asn1_named_data **head,
  297. const char *oid, size_t oid_len,
  298. const unsigned char *val,
  299. size_t val_len)
  300. {
  301. mbedtls_asn1_named_data *cur;
  302. if ((cur = asn1_find_named_data(*head, oid, oid_len)) == NULL) {
  303. // Add new entry if not present yet based on OID
  304. //
  305. cur = (mbedtls_asn1_named_data *) mbedtls_calloc(1,
  306. sizeof(mbedtls_asn1_named_data));
  307. if (cur == NULL) {
  308. return NULL;
  309. }
  310. cur->oid.len = oid_len;
  311. cur->oid.p = mbedtls_calloc(1, oid_len);
  312. if (cur->oid.p == NULL) {
  313. mbedtls_free(cur);
  314. return NULL;
  315. }
  316. memcpy(cur->oid.p, oid, oid_len);
  317. cur->val.len = val_len;
  318. if (val_len != 0) {
  319. cur->val.p = mbedtls_calloc(1, val_len);
  320. if (cur->val.p == NULL) {
  321. mbedtls_free(cur->oid.p);
  322. mbedtls_free(cur);
  323. return NULL;
  324. }
  325. }
  326. cur->next = *head;
  327. *head = cur;
  328. } else if (val_len == 0) {
  329. mbedtls_free(cur->val.p);
  330. cur->val.p = NULL;
  331. } else if (cur->val.len != val_len) {
  332. /*
  333. * Enlarge existing value buffer if needed
  334. * Preserve old data until the allocation succeeded, to leave list in
  335. * a consistent state in case allocation fails.
  336. */
  337. void *p = mbedtls_calloc(1, val_len);
  338. if (p == NULL) {
  339. return NULL;
  340. }
  341. mbedtls_free(cur->val.p);
  342. cur->val.p = p;
  343. cur->val.len = val_len;
  344. }
  345. if (val != NULL && val_len != 0) {
  346. memcpy(cur->val.p, val, val_len);
  347. }
  348. return cur;
  349. }
  350. #endif /* MBEDTLS_ASN1_WRITE_C */