x509_create.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * X.509 base functions for creating certificates / CSRs
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_X509_CREATE_C)
  21. #include "mbedtls/x509.h"
  22. #include "mbedtls/asn1write.h"
  23. #include "mbedtls/error.h"
  24. #include "mbedtls/oid.h"
  25. #include <string.h>
  26. /* Structure linking OIDs for X.509 DN AttributeTypes to their
  27. * string representations and default string encodings used by Mbed TLS. */
  28. typedef struct {
  29. const char *name; /* String representation of AttributeType, e.g.
  30. * "CN" or "emailAddress". */
  31. size_t name_len; /* Length of 'name', without trailing 0 byte. */
  32. const char *oid; /* String representation of OID of AttributeType,
  33. * as per RFC 5280, Appendix A.1. */
  34. int default_tag; /* The default character encoding used for the
  35. * given attribute type, e.g.
  36. * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
  37. } x509_attr_descriptor_t;
  38. #define ADD_STRLEN(s) s, sizeof(s) - 1
  39. /* X.509 DN attributes from RFC 5280, Appendix A.1. */
  40. static const x509_attr_descriptor_t x509_attrs[] =
  41. {
  42. { ADD_STRLEN("CN"),
  43. MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
  44. { ADD_STRLEN("commonName"),
  45. MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
  46. { ADD_STRLEN("C"),
  47. MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
  48. { ADD_STRLEN("countryName"),
  49. MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
  50. { ADD_STRLEN("O"),
  51. MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
  52. { ADD_STRLEN("organizationName"),
  53. MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
  54. { ADD_STRLEN("L"),
  55. MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
  56. { ADD_STRLEN("locality"),
  57. MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
  58. { ADD_STRLEN("R"),
  59. MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
  60. { ADD_STRLEN("OU"),
  61. MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
  62. { ADD_STRLEN("organizationalUnitName"),
  63. MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
  64. { ADD_STRLEN("ST"),
  65. MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
  66. { ADD_STRLEN("stateOrProvinceName"),
  67. MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
  68. { ADD_STRLEN("emailAddress"),
  69. MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
  70. { ADD_STRLEN("serialNumber"),
  71. MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
  72. { ADD_STRLEN("postalAddress"),
  73. MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
  74. { ADD_STRLEN("postalCode"),
  75. MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
  76. { ADD_STRLEN("dnQualifier"),
  77. MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
  78. { ADD_STRLEN("title"),
  79. MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
  80. { ADD_STRLEN("surName"),
  81. MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
  82. { ADD_STRLEN("SN"),
  83. MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
  84. { ADD_STRLEN("givenName"),
  85. MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
  86. { ADD_STRLEN("GN"),
  87. MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
  88. { ADD_STRLEN("initials"),
  89. MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
  90. { ADD_STRLEN("pseudonym"),
  91. MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
  92. { ADD_STRLEN("generationQualifier"),
  93. MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
  94. { ADD_STRLEN("domainComponent"),
  95. MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
  96. { ADD_STRLEN("DC"),
  97. MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
  98. { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
  99. };
  100. static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name, size_t name_len)
  101. {
  102. const x509_attr_descriptor_t *cur;
  103. for (cur = x509_attrs; cur->name != NULL; cur++) {
  104. if (cur->name_len == name_len &&
  105. strncmp(cur->name, name, name_len) == 0) {
  106. break;
  107. }
  108. }
  109. if (cur->name == NULL) {
  110. return NULL;
  111. }
  112. return cur;
  113. }
  114. int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
  115. {
  116. int ret = 0;
  117. const char *s = name, *c = s;
  118. const char *end = s + strlen(s);
  119. const char *oid = NULL;
  120. const x509_attr_descriptor_t *attr_descr = NULL;
  121. int in_tag = 1;
  122. char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
  123. char *d = data;
  124. /* Clear existing chain if present */
  125. mbedtls_asn1_free_named_data_list(head);
  126. while (c <= end) {
  127. if (in_tag && *c == '=') {
  128. if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) {
  129. ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
  130. goto exit;
  131. }
  132. oid = attr_descr->oid;
  133. s = c + 1;
  134. in_tag = 0;
  135. d = data;
  136. }
  137. if (!in_tag && *c == '\\' && c != end) {
  138. c++;
  139. /* Check for valid escaped characters */
  140. if (c == end || *c != ',') {
  141. ret = MBEDTLS_ERR_X509_INVALID_NAME;
  142. goto exit;
  143. }
  144. } else if (!in_tag && (*c == ',' || c == end)) {
  145. mbedtls_asn1_named_data *cur =
  146. mbedtls_asn1_store_named_data(head, oid, strlen(oid),
  147. (unsigned char *) data,
  148. d - data);
  149. if (cur == NULL) {
  150. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  151. }
  152. // set tagType
  153. cur->val.tag = attr_descr->default_tag;
  154. while (c < end && *(c + 1) == ' ') {
  155. c++;
  156. }
  157. s = c + 1;
  158. in_tag = 1;
  159. }
  160. if (!in_tag && s != c + 1) {
  161. *(d++) = *c;
  162. if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
  163. ret = MBEDTLS_ERR_X509_INVALID_NAME;
  164. goto exit;
  165. }
  166. }
  167. c++;
  168. }
  169. exit:
  170. return ret;
  171. }
  172. /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
  173. * to store the critical boolean for us
  174. */
  175. int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
  176. int critical, const unsigned char *val, size_t val_len)
  177. {
  178. mbedtls_asn1_named_data *cur;
  179. if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
  180. NULL, val_len + 1)) == NULL) {
  181. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  182. }
  183. cur->val.p[0] = critical;
  184. memcpy(cur->val.p + 1, val, val_len);
  185. return 0;
  186. }
  187. /*
  188. * RelativeDistinguishedName ::=
  189. * SET OF AttributeTypeAndValue
  190. *
  191. * AttributeTypeAndValue ::= SEQUENCE {
  192. * type AttributeType,
  193. * value AttributeValue }
  194. *
  195. * AttributeType ::= OBJECT IDENTIFIER
  196. *
  197. * AttributeValue ::= ANY DEFINED BY AttributeType
  198. */
  199. static int x509_write_name(unsigned char **p,
  200. unsigned char *start,
  201. mbedtls_asn1_named_data *cur_name)
  202. {
  203. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  204. size_t len = 0;
  205. const char *oid = (const char *) cur_name->oid.p;
  206. size_t oid_len = cur_name->oid.len;
  207. const unsigned char *name = cur_name->val.p;
  208. size_t name_len = cur_name->val.len;
  209. // Write correct string tag and value
  210. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
  211. cur_name->val.tag,
  212. (const char *) name,
  213. name_len));
  214. // Write OID
  215. //
  216. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
  217. oid_len));
  218. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  219. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  220. MBEDTLS_ASN1_CONSTRUCTED |
  221. MBEDTLS_ASN1_SEQUENCE));
  222. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  223. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  224. MBEDTLS_ASN1_CONSTRUCTED |
  225. MBEDTLS_ASN1_SET));
  226. return (int) len;
  227. }
  228. int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
  229. mbedtls_asn1_named_data *first)
  230. {
  231. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  232. size_t len = 0;
  233. mbedtls_asn1_named_data *cur = first;
  234. while (cur != NULL) {
  235. MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
  236. cur = cur->next;
  237. }
  238. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  239. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
  240. MBEDTLS_ASN1_SEQUENCE));
  241. return (int) len;
  242. }
  243. int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
  244. const char *oid, size_t oid_len,
  245. unsigned char *sig, size_t size)
  246. {
  247. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  248. size_t len = 0;
  249. if (*p < start || (size_t) (*p - start) < size) {
  250. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  251. }
  252. len = size;
  253. (*p) -= len;
  254. memcpy(*p, sig, len);
  255. if (*p - start < 1) {
  256. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  257. }
  258. *--(*p) = 0;
  259. len += 1;
  260. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  261. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
  262. // Write OID
  263. //
  264. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(p, start, oid,
  265. oid_len, 0));
  266. return (int) len;
  267. }
  268. static int x509_write_extension(unsigned char **p, unsigned char *start,
  269. mbedtls_asn1_named_data *ext)
  270. {
  271. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  272. size_t len = 0;
  273. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
  274. ext->val.len - 1));
  275. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
  276. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
  277. if (ext->val.p[0] != 0) {
  278. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
  279. }
  280. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
  281. ext->oid.len));
  282. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
  283. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
  284. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  285. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
  286. MBEDTLS_ASN1_SEQUENCE));
  287. return (int) len;
  288. }
  289. /*
  290. * Extension ::= SEQUENCE {
  291. * extnID OBJECT IDENTIFIER,
  292. * critical BOOLEAN DEFAULT FALSE,
  293. * extnValue OCTET STRING
  294. * -- contains the DER encoding of an ASN.1 value
  295. * -- corresponding to the extension type identified
  296. * -- by extnID
  297. * }
  298. */
  299. int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
  300. mbedtls_asn1_named_data *first)
  301. {
  302. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  303. size_t len = 0;
  304. mbedtls_asn1_named_data *cur_ext = first;
  305. while (cur_ext != NULL) {
  306. MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
  307. cur_ext = cur_ext->next;
  308. }
  309. return (int) len;
  310. }
  311. #endif /* MBEDTLS_X509_CREATE_C */