x509_create.c 13 KB

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