x509_create.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * X.509 base functions for creating certificates / CSRs
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_X509_CREATE_C)
  27. #include "mbedtls/x509.h"
  28. #include "mbedtls/asn1write.h"
  29. #include "mbedtls/oid.h"
  30. #include <string.h>
  31. typedef struct {
  32. const char *name;
  33. size_t name_len;
  34. const char*oid;
  35. } x509_attr_descriptor_t;
  36. #define ADD_STRLEN( s ) s, sizeof( s ) - 1
  37. static const x509_attr_descriptor_t x509_attrs[] =
  38. {
  39. { ADD_STRLEN( "CN" ), MBEDTLS_OID_AT_CN },
  40. { ADD_STRLEN( "commonName" ), MBEDTLS_OID_AT_CN },
  41. { ADD_STRLEN( "C" ), MBEDTLS_OID_AT_COUNTRY },
  42. { ADD_STRLEN( "countryName" ), MBEDTLS_OID_AT_COUNTRY },
  43. { ADD_STRLEN( "O" ), MBEDTLS_OID_AT_ORGANIZATION },
  44. { ADD_STRLEN( "organizationName" ), MBEDTLS_OID_AT_ORGANIZATION },
  45. { ADD_STRLEN( "L" ), MBEDTLS_OID_AT_LOCALITY },
  46. { ADD_STRLEN( "locality" ), MBEDTLS_OID_AT_LOCALITY },
  47. { ADD_STRLEN( "R" ), MBEDTLS_OID_PKCS9_EMAIL },
  48. { ADD_STRLEN( "OU" ), MBEDTLS_OID_AT_ORG_UNIT },
  49. { ADD_STRLEN( "organizationalUnitName" ), MBEDTLS_OID_AT_ORG_UNIT },
  50. { ADD_STRLEN( "ST" ), MBEDTLS_OID_AT_STATE },
  51. { ADD_STRLEN( "stateOrProvinceName" ), MBEDTLS_OID_AT_STATE },
  52. { ADD_STRLEN( "emailAddress" ), MBEDTLS_OID_PKCS9_EMAIL },
  53. { ADD_STRLEN( "serialNumber" ), MBEDTLS_OID_AT_SERIAL_NUMBER },
  54. { ADD_STRLEN( "postalAddress" ), MBEDTLS_OID_AT_POSTAL_ADDRESS },
  55. { ADD_STRLEN( "postalCode" ), MBEDTLS_OID_AT_POSTAL_CODE },
  56. { ADD_STRLEN( "dnQualifier" ), MBEDTLS_OID_AT_DN_QUALIFIER },
  57. { ADD_STRLEN( "title" ), MBEDTLS_OID_AT_TITLE },
  58. { ADD_STRLEN( "surName" ), MBEDTLS_OID_AT_SUR_NAME },
  59. { ADD_STRLEN( "SN" ), MBEDTLS_OID_AT_SUR_NAME },
  60. { ADD_STRLEN( "givenName" ), MBEDTLS_OID_AT_GIVEN_NAME },
  61. { ADD_STRLEN( "GN" ), MBEDTLS_OID_AT_GIVEN_NAME },
  62. { ADD_STRLEN( "initials" ), MBEDTLS_OID_AT_INITIALS },
  63. { ADD_STRLEN( "pseudonym" ), MBEDTLS_OID_AT_PSEUDONYM },
  64. { ADD_STRLEN( "generationQualifier" ), MBEDTLS_OID_AT_GENERATION_QUALIFIER },
  65. { ADD_STRLEN( "domainComponent" ), MBEDTLS_OID_DOMAIN_COMPONENT },
  66. { ADD_STRLEN( "DC" ), MBEDTLS_OID_DOMAIN_COMPONENT },
  67. { NULL, 0, NULL }
  68. };
  69. static const char *x509_at_oid_from_name( const char *name, size_t name_len )
  70. {
  71. const x509_attr_descriptor_t *cur;
  72. for( cur = x509_attrs; cur->name != NULL; cur++ )
  73. if( cur->name_len == name_len &&
  74. strncmp( cur->name, name, name_len ) == 0 )
  75. break;
  76. return( cur->oid );
  77. }
  78. int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
  79. {
  80. int ret = 0;
  81. const char *s = name, *c = s;
  82. const char *end = s + strlen( s );
  83. const char *oid = NULL;
  84. int in_tag = 1;
  85. char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
  86. char *d = data;
  87. /* Clear existing chain if present */
  88. mbedtls_asn1_free_named_data_list( head );
  89. while( c <= end )
  90. {
  91. if( in_tag && *c == '=' )
  92. {
  93. if( ( oid = x509_at_oid_from_name( s, c - s ) ) == NULL )
  94. {
  95. ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
  96. goto exit;
  97. }
  98. s = c + 1;
  99. in_tag = 0;
  100. d = data;
  101. }
  102. if( !in_tag && *c == '\\' && c != end )
  103. {
  104. c++;
  105. /* Check for valid escaped characters */
  106. if( c == end || *c != ',' )
  107. {
  108. ret = MBEDTLS_ERR_X509_INVALID_NAME;
  109. goto exit;
  110. }
  111. }
  112. else if( !in_tag && ( *c == ',' || c == end ) )
  113. {
  114. if( mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
  115. (unsigned char *) data,
  116. d - data ) == NULL )
  117. {
  118. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  119. }
  120. while( c < end && *(c + 1) == ' ' )
  121. c++;
  122. s = c + 1;
  123. in_tag = 1;
  124. }
  125. if( !in_tag && s != c + 1 )
  126. {
  127. *(d++) = *c;
  128. if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
  129. {
  130. ret = MBEDTLS_ERR_X509_INVALID_NAME;
  131. goto exit;
  132. }
  133. }
  134. c++;
  135. }
  136. exit:
  137. return( ret );
  138. }
  139. /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
  140. * to store the critical boolean for us
  141. */
  142. int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
  143. int critical, const unsigned char *val, size_t val_len )
  144. {
  145. mbedtls_asn1_named_data *cur;
  146. if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
  147. NULL, val_len + 1 ) ) == NULL )
  148. {
  149. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  150. }
  151. cur->val.p[0] = critical;
  152. memcpy( cur->val.p + 1, val, val_len );
  153. return( 0 );
  154. }
  155. /*
  156. * RelativeDistinguishedName ::=
  157. * SET OF AttributeTypeAndValue
  158. *
  159. * AttributeTypeAndValue ::= SEQUENCE {
  160. * type AttributeType,
  161. * value AttributeValue }
  162. *
  163. * AttributeType ::= OBJECT IDENTIFIER
  164. *
  165. * AttributeValue ::= ANY DEFINED BY AttributeType
  166. */
  167. static int x509_write_name( unsigned char **p, unsigned char *start,
  168. const char *oid, size_t oid_len,
  169. const unsigned char *name, size_t name_len )
  170. {
  171. int ret;
  172. size_t len = 0;
  173. // Write PrintableString for all except MBEDTLS_OID_PKCS9_EMAIL
  174. //
  175. if( MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_EMAIL ) == oid_len &&
  176. memcmp( oid, MBEDTLS_OID_PKCS9_EMAIL, oid_len ) == 0 )
  177. {
  178. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_ia5_string( p, start,
  179. (const char *) name,
  180. name_len ) );
  181. }
  182. else
  183. {
  184. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_printable_string( p, start,
  185. (const char *) name,
  186. name_len ) );
  187. }
  188. // Write OID
  189. //
  190. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  191. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  192. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  193. MBEDTLS_ASN1_SEQUENCE ) );
  194. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  195. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  196. MBEDTLS_ASN1_SET ) );
  197. return( (int) len );
  198. }
  199. int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
  200. mbedtls_asn1_named_data *first )
  201. {
  202. int ret;
  203. size_t len = 0;
  204. mbedtls_asn1_named_data *cur = first;
  205. while( cur != NULL )
  206. {
  207. MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, (char *) cur->oid.p,
  208. cur->oid.len,
  209. cur->val.p, cur->val.len ) );
  210. cur = cur->next;
  211. }
  212. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  213. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  214. MBEDTLS_ASN1_SEQUENCE ) );
  215. return( (int) len );
  216. }
  217. int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
  218. const char *oid, size_t oid_len,
  219. unsigned char *sig, size_t size )
  220. {
  221. int ret;
  222. size_t len = 0;
  223. if( *p < start || (size_t)( *p - start ) < size )
  224. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  225. len = size;
  226. (*p) -= len;
  227. memcpy( *p, sig, len );
  228. if( *p - start < 1 )
  229. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  230. *--(*p) = 0;
  231. len += 1;
  232. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  233. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
  234. // Write OID
  235. //
  236. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
  237. oid_len, 0 ) );
  238. return( (int) len );
  239. }
  240. static int x509_write_extension( unsigned char **p, unsigned char *start,
  241. mbedtls_asn1_named_data *ext )
  242. {
  243. int ret;
  244. size_t len = 0;
  245. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
  246. ext->val.len - 1 ) );
  247. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
  248. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
  249. if( ext->val.p[0] != 0 )
  250. {
  251. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
  252. }
  253. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
  254. ext->oid.len ) );
  255. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
  256. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
  257. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  258. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  259. MBEDTLS_ASN1_SEQUENCE ) );
  260. return( (int) len );
  261. }
  262. /*
  263. * Extension ::= SEQUENCE {
  264. * extnID OBJECT IDENTIFIER,
  265. * critical BOOLEAN DEFAULT FALSE,
  266. * extnValue OCTET STRING
  267. * -- contains the DER encoding of an ASN.1 value
  268. * -- corresponding to the extension type identified
  269. * -- by extnID
  270. * }
  271. */
  272. int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
  273. mbedtls_asn1_named_data *first )
  274. {
  275. int ret;
  276. size_t len = 0;
  277. mbedtls_asn1_named_data *cur_ext = first;
  278. while( cur_ext != NULL )
  279. {
  280. MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
  281. cur_ext = cur_ext->next;
  282. }
  283. return( (int) len );
  284. }
  285. #endif /* MBEDTLS_X509_CREATE_C */