x509write_crt.c 16 KB

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