x509write_csr.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * X.509 Certificate Signing Request 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. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  26. * - attributes: PKCS#9 v2.0 aka RFC 2985
  27. */
  28. #if !defined(MBEDTLS_CONFIG_FILE)
  29. #include "mbedtls/config.h"
  30. #else
  31. #include MBEDTLS_CONFIG_FILE
  32. #endif
  33. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  34. #include "mbedtls/x509_csr.h"
  35. #include "mbedtls/oid.h"
  36. #include "mbedtls/asn1write.h"
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #if defined(MBEDTLS_PEM_WRITE_C)
  40. #include "mbedtls/pem.h"
  41. #endif
  42. /* Implementation that should never be optimized out by the compiler */
  43. static void mbedtls_zeroize( void *v, size_t n ) {
  44. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  45. }
  46. void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
  47. {
  48. memset( ctx, 0, sizeof(mbedtls_x509write_csr) );
  49. }
  50. void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
  51. {
  52. mbedtls_asn1_free_named_data_list( &ctx->subject );
  53. mbedtls_asn1_free_named_data_list( &ctx->extensions );
  54. mbedtls_zeroize( ctx, sizeof(mbedtls_x509write_csr) );
  55. }
  56. void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
  57. {
  58. ctx->md_alg = md_alg;
  59. }
  60. void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
  61. {
  62. ctx->key = key;
  63. }
  64. int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
  65. const char *subject_name )
  66. {
  67. return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
  68. }
  69. int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
  70. const char *oid, size_t oid_len,
  71. const unsigned char *val, size_t val_len )
  72. {
  73. return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
  74. 0, val, val_len );
  75. }
  76. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
  77. {
  78. unsigned char buf[4];
  79. unsigned char *c;
  80. int ret;
  81. c = buf + 4;
  82. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
  83. return( ret );
  84. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  85. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  86. buf, 4 );
  87. if( ret != 0 )
  88. return( ret );
  89. return( 0 );
  90. }
  91. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  92. unsigned char ns_cert_type )
  93. {
  94. unsigned char buf[4];
  95. unsigned char *c;
  96. int ret;
  97. c = buf + 4;
  98. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
  99. return( ret );
  100. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  101. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  102. buf, 4 );
  103. if( ret != 0 )
  104. return( ret );
  105. return( 0 );
  106. }
  107. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  108. int (*f_rng)(void *, unsigned char *, size_t),
  109. void *p_rng )
  110. {
  111. int ret;
  112. const char *sig_oid;
  113. size_t sig_oid_len = 0;
  114. unsigned char *c, *c2;
  115. unsigned char hash[64];
  116. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  117. unsigned char tmp_buf[2048];
  118. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  119. size_t len = 0;
  120. mbedtls_pk_type_t pk_alg;
  121. /*
  122. * Prepare data to be signed in tmp_buf
  123. */
  124. c = tmp_buf + sizeof( tmp_buf );
  125. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
  126. if( len )
  127. {
  128. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  129. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  130. MBEDTLS_ASN1_SEQUENCE ) );
  131. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  132. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  133. MBEDTLS_ASN1_SET ) );
  134. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  135. MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
  136. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  137. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  138. MBEDTLS_ASN1_SEQUENCE ) );
  139. }
  140. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  141. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  142. MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
  143. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
  144. tmp_buf, c - tmp_buf ) );
  145. c -= pub_len;
  146. len += pub_len;
  147. /*
  148. * Subject ::= Name
  149. */
  150. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
  151. /*
  152. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  153. */
  154. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
  155. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  156. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  157. MBEDTLS_ASN1_SEQUENCE ) );
  158. /*
  159. * Prepare signature
  160. */
  161. mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
  162. pk_alg = mbedtls_pk_get_type( ctx->key );
  163. if( pk_alg == MBEDTLS_PK_ECKEY )
  164. pk_alg = MBEDTLS_PK_ECDSA;
  165. if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
  166. f_rng, p_rng ) ) != 0 ||
  167. ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  168. &sig_oid, &sig_oid_len ) ) != 0 )
  169. {
  170. return( ret );
  171. }
  172. /*
  173. * Write data to output buffer
  174. */
  175. c2 = buf + size;
  176. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
  177. sig_oid, sig_oid_len, sig, sig_len ) );
  178. if( len > (size_t)( c2 - buf ) )
  179. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  180. c2 -= len;
  181. memcpy( c2, c, len );
  182. len += sig_and_oid_len;
  183. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  184. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
  185. MBEDTLS_ASN1_SEQUENCE ) );
  186. return( (int) len );
  187. }
  188. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  189. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  190. #if defined(MBEDTLS_PEM_WRITE_C)
  191. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  192. int (*f_rng)(void *, unsigned char *, size_t),
  193. void *p_rng )
  194. {
  195. int ret;
  196. unsigned char output_buf[4096];
  197. size_t olen = 0;
  198. if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
  199. f_rng, p_rng ) ) < 0 )
  200. {
  201. return( ret );
  202. }
  203. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
  204. output_buf + sizeof(output_buf) - ret,
  205. ret, buf, size, &olen ) ) != 0 )
  206. {
  207. return( ret );
  208. }
  209. return( 0 );
  210. }
  211. #endif /* MBEDTLS_PEM_WRITE_C */
  212. #endif /* MBEDTLS_X509_CSR_WRITE_C */