x509_create.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 = MBEDTLS_ERR_X509_INVALID_NAME;
  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. /* Successfully parsed one name, update ret to success */
  160. ret = 0;
  161. }
  162. if (!in_tag && s != c + 1) {
  163. *(d++) = *c;
  164. if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
  165. ret = MBEDTLS_ERR_X509_INVALID_NAME;
  166. goto exit;
  167. }
  168. }
  169. c++;
  170. }
  171. exit:
  172. return ret;
  173. }
  174. /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
  175. * to store the critical boolean for us
  176. */
  177. int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
  178. int critical, const unsigned char *val, size_t val_len)
  179. {
  180. mbedtls_asn1_named_data *cur;
  181. if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
  182. NULL, val_len + 1)) == NULL) {
  183. return MBEDTLS_ERR_X509_ALLOC_FAILED;
  184. }
  185. cur->val.p[0] = critical;
  186. memcpy(cur->val.p + 1, val, val_len);
  187. return 0;
  188. }
  189. /*
  190. * RelativeDistinguishedName ::=
  191. * SET OF AttributeTypeAndValue
  192. *
  193. * AttributeTypeAndValue ::= SEQUENCE {
  194. * type AttributeType,
  195. * value AttributeValue }
  196. *
  197. * AttributeType ::= OBJECT IDENTIFIER
  198. *
  199. * AttributeValue ::= ANY DEFINED BY AttributeType
  200. */
  201. static int x509_write_name(unsigned char **p,
  202. unsigned char *start,
  203. mbedtls_asn1_named_data *cur_name)
  204. {
  205. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  206. size_t len = 0;
  207. const char *oid = (const char *) cur_name->oid.p;
  208. size_t oid_len = cur_name->oid.len;
  209. const unsigned char *name = cur_name->val.p;
  210. size_t name_len = cur_name->val.len;
  211. // Write correct string tag and value
  212. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
  213. cur_name->val.tag,
  214. (const char *) name,
  215. name_len));
  216. // Write OID
  217. //
  218. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
  219. oid_len));
  220. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  221. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  222. MBEDTLS_ASN1_CONSTRUCTED |
  223. MBEDTLS_ASN1_SEQUENCE));
  224. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  225. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
  226. MBEDTLS_ASN1_CONSTRUCTED |
  227. MBEDTLS_ASN1_SET));
  228. return (int) len;
  229. }
  230. int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
  231. mbedtls_asn1_named_data *first)
  232. {
  233. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  234. size_t len = 0;
  235. mbedtls_asn1_named_data *cur = first;
  236. while (cur != NULL) {
  237. MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
  238. cur = cur->next;
  239. }
  240. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  241. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
  242. MBEDTLS_ASN1_SEQUENCE));
  243. return (int) len;
  244. }
  245. int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
  246. const char *oid, size_t oid_len,
  247. unsigned char *sig, size_t size)
  248. {
  249. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  250. size_t len = 0;
  251. if (*p < start || (size_t) (*p - start) < size) {
  252. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  253. }
  254. len = size;
  255. (*p) -= len;
  256. memcpy(*p, sig, len);
  257. if (*p - start < 1) {
  258. return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
  259. }
  260. *--(*p) = 0;
  261. len += 1;
  262. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  263. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
  264. // Write OID
  265. //
  266. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(p, start, oid,
  267. oid_len, 0));
  268. return (int) len;
  269. }
  270. static int x509_write_extension(unsigned char **p, unsigned char *start,
  271. mbedtls_asn1_named_data *ext)
  272. {
  273. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  274. size_t len = 0;
  275. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
  276. ext->val.len - 1));
  277. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
  278. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
  279. if (ext->val.p[0] != 0) {
  280. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
  281. }
  282. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
  283. ext->oid.len));
  284. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
  285. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
  286. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
  287. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
  288. MBEDTLS_ASN1_SEQUENCE));
  289. return (int) len;
  290. }
  291. /*
  292. * Extension ::= SEQUENCE {
  293. * extnID OBJECT IDENTIFIER,
  294. * critical BOOLEAN DEFAULT FALSE,
  295. * extnValue OCTET STRING
  296. * -- contains the DER encoding of an ASN.1 value
  297. * -- corresponding to the extension type identified
  298. * -- by extnID
  299. * }
  300. */
  301. int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
  302. mbedtls_asn1_named_data *first)
  303. {
  304. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  305. size_t len = 0;
  306. mbedtls_asn1_named_data *cur_ext = first;
  307. while (cur_ext != NULL) {
  308. MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
  309. cur_ext = cur_ext->next;
  310. }
  311. return (int) len;
  312. }
  313. #endif /* MBEDTLS_X509_CREATE_C */