x509write_crt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * X.509 certificate writing
  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. /*
  22. * References:
  23. * - certificates: RFC 5280, updated by RFC 6818
  24. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  25. * - attributes: PKCS#9 v2.0 aka RFC 2985
  26. */
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "mbedtls/config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #if defined(MBEDTLS_X509_CRT_WRITE_C)
  33. #include "mbedtls/x509_crt.h"
  34. #include "mbedtls/oid.h"
  35. #include "mbedtls/asn1write.h"
  36. #include "mbedtls/sha1.h"
  37. #include <string.h>
  38. #if defined(MBEDTLS_PEM_WRITE_C)
  39. #include "mbedtls/pem.h"
  40. #endif /* MBEDTLS_PEM_WRITE_C */
  41. /* Implementation that should never be optimized out by the compiler */
  42. static void mbedtls_zeroize( void *v, size_t n ) {
  43. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  44. }
  45. void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
  46. {
  47. memset( ctx, 0, sizeof(mbedtls_x509write_cert) );
  48. mbedtls_mpi_init( &ctx->serial );
  49. ctx->version = MBEDTLS_X509_CRT_VERSION_3;
  50. }
  51. void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
  52. {
  53. mbedtls_mpi_free( &ctx->serial );
  54. mbedtls_asn1_free_named_data_list( &ctx->subject );
  55. mbedtls_asn1_free_named_data_list( &ctx->issuer );
  56. mbedtls_asn1_free_named_data_list( &ctx->extensions );
  57. mbedtls_zeroize( ctx, sizeof(mbedtls_x509write_cert) );
  58. }
  59. void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version )
  60. {
  61. ctx->version = version;
  62. }
  63. void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg )
  64. {
  65. ctx->md_alg = md_alg;
  66. }
  67. void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key )
  68. {
  69. ctx->subject_key = key;
  70. }
  71. void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key )
  72. {
  73. ctx->issuer_key = key;
  74. }
  75. int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
  76. const char *subject_name )
  77. {
  78. return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
  79. }
  80. int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
  81. const char *issuer_name )
  82. {
  83. return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
  84. }
  85. int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial )
  86. {
  87. int ret;
  88. if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
  89. return( ret );
  90. return( 0 );
  91. }
  92. int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
  93. const char *not_after )
  94. {
  95. if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
  96. strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
  97. {
  98. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  99. }
  100. strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
  101. strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
  102. ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
  103. ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
  104. return( 0 );
  105. }
  106. int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
  107. const char *oid, size_t oid_len,
  108. int critical,
  109. const unsigned char *val, size_t val_len )
  110. {
  111. return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
  112. critical, val, val_len );
  113. }
  114. int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
  115. int is_ca, int max_pathlen )
  116. {
  117. int ret;
  118. unsigned char buf[9];
  119. unsigned char *c = buf + sizeof(buf);
  120. size_t len = 0;
  121. memset( buf, 0, sizeof(buf) );
  122. if( is_ca && max_pathlen > 127 )
  123. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  124. if( is_ca )
  125. {
  126. if( max_pathlen >= 0 )
  127. {
  128. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, max_pathlen ) );
  129. }
  130. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
  131. }
  132. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  133. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  134. MBEDTLS_ASN1_SEQUENCE ) );
  135. return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
  136. MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
  137. 0, buf + sizeof(buf) - len, len );
  138. }
  139. #if defined(MBEDTLS_SHA1_C)
  140. int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
  141. {
  142. int ret;
  143. unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
  144. unsigned char *c = buf + sizeof(buf);
  145. size_t len = 0;
  146. memset( buf, 0, sizeof(buf) );
  147. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) );
  148. mbedtls_sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
  149. c = buf + sizeof(buf) - 20;
  150. len = 20;
  151. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  152. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) );
  153. return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
  154. MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
  155. 0, buf + sizeof(buf) - len, len );
  156. }
  157. int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
  158. {
  159. int ret;
  160. unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
  161. unsigned char *c = buf + sizeof(buf);
  162. size_t len = 0;
  163. memset( buf, 0, sizeof(buf) );
  164. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) );
  165. mbedtls_sha1( buf + sizeof(buf) - len, len, buf + sizeof(buf) - 20 );
  166. c = buf + sizeof(buf) - 20;
  167. len = 20;
  168. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  169. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) );
  170. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  171. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  172. MBEDTLS_ASN1_SEQUENCE ) );
  173. return mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
  174. MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
  175. 0, buf + sizeof(buf) - len, len );
  176. }
  177. #endif /* MBEDTLS_SHA1_C */
  178. int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
  179. unsigned int key_usage )
  180. {
  181. unsigned char buf[4], ku;
  182. unsigned char *c;
  183. int ret;
  184. /* We currently only support 7 bits, from 0x80 to 0x02 */
  185. if( ( key_usage & ~0xfe ) != 0 )
  186. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
  187. c = buf + 4;
  188. ku = (unsigned char) key_usage;
  189. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ku, 7 ) ) != 4 )
  190. return( ret );
  191. ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  192. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  193. 1, buf, 4 );
  194. if( ret != 0 )
  195. return( ret );
  196. return( 0 );
  197. }
  198. int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
  199. unsigned char ns_cert_type )
  200. {
  201. unsigned char buf[4];
  202. unsigned char *c;
  203. int ret;
  204. c = buf + 4;
  205. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
  206. return( ret );
  207. ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  208. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  209. 0, buf, 4 );
  210. if( ret != 0 )
  211. return( ret );
  212. return( 0 );
  213. }
  214. static int x509_write_time( unsigned char **p, unsigned char *start,
  215. const char *time, size_t size )
  216. {
  217. int ret;
  218. size_t len = 0;
  219. /*
  220. * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
  221. */
  222. if( time[0] == '2' && time[1] == '0' && time [2] < '5' )
  223. {
  224. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  225. (const unsigned char *) time + 2,
  226. size - 2 ) );
  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, MBEDTLS_ASN1_UTC_TIME ) );
  229. }
  230. else
  231. {
  232. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
  233. (const unsigned char *) time,
  234. size ) );
  235. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  236. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_GENERALIZED_TIME ) );
  237. }
  238. return( (int) len );
  239. }
  240. int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
  241. int (*f_rng)(void *, unsigned char *, size_t),
  242. void *p_rng )
  243. {
  244. int ret;
  245. const char *sig_oid;
  246. size_t sig_oid_len = 0;
  247. unsigned char *c, *c2;
  248. unsigned char hash[64];
  249. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  250. unsigned char tmp_buf[2048];
  251. size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
  252. size_t len = 0;
  253. mbedtls_pk_type_t pk_alg;
  254. /*
  255. * Prepare data to be signed in tmp_buf
  256. */
  257. c = tmp_buf + sizeof( tmp_buf );
  258. /* Signature algorithm needed in TBS, and later for actual signature */
  259. pk_alg = mbedtls_pk_get_type( ctx->issuer_key );
  260. if( pk_alg == MBEDTLS_PK_ECKEY )
  261. pk_alg = MBEDTLS_PK_ECDSA;
  262. if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  263. &sig_oid, &sig_oid_len ) ) != 0 )
  264. {
  265. return( ret );
  266. }
  267. /*
  268. * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
  269. */
  270. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
  271. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  272. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  273. MBEDTLS_ASN1_SEQUENCE ) );
  274. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  275. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  276. MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
  277. /*
  278. * SubjectPublicKeyInfo
  279. */
  280. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->subject_key,
  281. tmp_buf, c - tmp_buf ) );
  282. c -= pub_len;
  283. len += pub_len;
  284. /*
  285. * Subject ::= Name
  286. */
  287. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
  288. /*
  289. * Validity ::= SEQUENCE {
  290. * notBefore Time,
  291. * notAfter Time }
  292. */
  293. sub_len = 0;
  294. MBEDTLS_ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_after,
  295. MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
  296. MBEDTLS_ASN1_CHK_ADD( sub_len, x509_write_time( &c, tmp_buf, ctx->not_before,
  297. MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
  298. len += sub_len;
  299. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, sub_len ) );
  300. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  301. MBEDTLS_ASN1_SEQUENCE ) );
  302. /*
  303. * Issuer ::= Name
  304. */
  305. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->issuer ) );
  306. /*
  307. * Signature ::= AlgorithmIdentifier
  308. */
  309. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, tmp_buf,
  310. sig_oid, strlen( sig_oid ), 0 ) );
  311. /*
  312. * Serial ::= INTEGER
  313. */
  314. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, tmp_buf, &ctx->serial ) );
  315. /*
  316. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  317. */
  318. sub_len = 0;
  319. MBEDTLS_ASN1_CHK_ADD( sub_len, mbedtls_asn1_write_int( &c, tmp_buf, ctx->version ) );
  320. len += sub_len;
  321. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, sub_len ) );
  322. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC |
  323. MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
  324. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  325. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  326. MBEDTLS_ASN1_SEQUENCE ) );
  327. /*
  328. * Make signature
  329. */
  330. mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
  331. if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg, hash, 0, sig, &sig_len,
  332. f_rng, p_rng ) ) != 0 )
  333. {
  334. return( ret );
  335. }
  336. /*
  337. * Write data to output buffer
  338. */
  339. c2 = buf + size;
  340. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
  341. sig_oid, sig_oid_len, sig, sig_len ) );
  342. if( len > (size_t)( c2 - buf ) )
  343. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  344. c2 -= len;
  345. memcpy( c2, c, len );
  346. len += sig_and_oid_len;
  347. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  348. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
  349. MBEDTLS_ASN1_SEQUENCE ) );
  350. return( (int) len );
  351. }
  352. #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
  353. #define PEM_END_CRT "-----END CERTIFICATE-----\n"
  354. #if defined(MBEDTLS_PEM_WRITE_C)
  355. int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt, unsigned char *buf, size_t size,
  356. int (*f_rng)(void *, unsigned char *, size_t),
  357. void *p_rng )
  358. {
  359. int ret;
  360. unsigned char output_buf[4096];
  361. size_t olen = 0;
  362. if( ( ret = mbedtls_x509write_crt_der( crt, output_buf, sizeof(output_buf),
  363. f_rng, p_rng ) ) < 0 )
  364. {
  365. return( ret );
  366. }
  367. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
  368. output_buf + sizeof(output_buf) - ret,
  369. ret, buf, size, &olen ) ) != 0 )
  370. {
  371. return( ret );
  372. }
  373. return( 0 );
  374. }
  375. #endif /* MBEDTLS_PEM_WRITE_C */
  376. #endif /* MBEDTLS_X509_CRT_WRITE_C */