x509write_crt.c 16 KB

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