x509write_csr.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * X.509 Certificate Signing Request 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. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  24. * - attributes: PKCS#9 v2.0 aka RFC 2985
  25. */
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "mbedtls/config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  32. #include "mbedtls/x509_csr.h"
  33. #include "mbedtls/oid.h"
  34. #include "mbedtls/asn1write.h"
  35. #include "mbedtls/platform_util.h"
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #if defined(MBEDTLS_PEM_WRITE_C)
  39. #include "mbedtls/pem.h"
  40. #endif
  41. /*
  42. * For the currently used signature algorithms the buffer to store any signature
  43. * must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)
  44. */
  45. #if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE
  46. #define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
  47. #else
  48. #define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
  49. #endif
  50. void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
  51. {
  52. memset( ctx, 0, sizeof( mbedtls_x509write_csr ) );
  53. }
  54. void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
  55. {
  56. mbedtls_asn1_free_named_data_list( &ctx->subject );
  57. mbedtls_asn1_free_named_data_list( &ctx->extensions );
  58. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_csr ) );
  59. }
  60. void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
  61. {
  62. ctx->md_alg = md_alg;
  63. }
  64. void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
  65. {
  66. ctx->key = key;
  67. }
  68. int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
  69. const char *subject_name )
  70. {
  71. return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
  72. }
  73. int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
  74. const char *oid, size_t oid_len,
  75. const unsigned char *val, size_t val_len )
  76. {
  77. return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
  78. 0, val, val_len );
  79. }
  80. static size_t csr_get_unused_bits_for_named_bitstring( unsigned char bitstring,
  81. size_t bit_offset )
  82. {
  83. size_t unused_bits;
  84. /* Count the unused bits removing trailing 0s */
  85. for( unused_bits = bit_offset; unused_bits < 8; unused_bits++ )
  86. if( ( ( bitstring >> unused_bits ) & 0x1 ) != 0 )
  87. break;
  88. return( unused_bits );
  89. }
  90. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
  91. {
  92. unsigned char buf[4];
  93. unsigned char *c;
  94. size_t unused_bits;
  95. int ret;
  96. c = buf + 4;
  97. unused_bits = csr_get_unused_bits_for_named_bitstring( key_usage, 0 );
  98. ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 8 - unused_bits );
  99. if( ret < 0 )
  100. return( ret );
  101. else if( ret < 3 || ret > 4 )
  102. return( MBEDTLS_ERR_X509_INVALID_FORMAT );
  103. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  104. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  105. c, (size_t)ret );
  106. if( ret != 0 )
  107. return( ret );
  108. return( 0 );
  109. }
  110. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  111. unsigned char ns_cert_type )
  112. {
  113. unsigned char buf[4];
  114. unsigned char *c;
  115. size_t unused_bits;
  116. int ret;
  117. c = buf + 4;
  118. unused_bits = csr_get_unused_bits_for_named_bitstring( ns_cert_type, 0 );
  119. ret = mbedtls_asn1_write_bitstring( &c,
  120. buf,
  121. &ns_cert_type,
  122. 8 - unused_bits );
  123. if( ret < 0 )
  124. return( ret );
  125. else if( ret < 3 || ret > 4 )
  126. return( ret );
  127. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  128. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  129. c, (size_t)ret );
  130. if( ret != 0 )
  131. return( ret );
  132. return( 0 );
  133. }
  134. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  135. int (*f_rng)(void *, unsigned char *, size_t),
  136. void *p_rng )
  137. {
  138. int ret;
  139. const char *sig_oid;
  140. size_t sig_oid_len = 0;
  141. unsigned char *c, *c2;
  142. unsigned char hash[64];
  143. unsigned char sig[SIGNATURE_MAX_SIZE];
  144. unsigned char tmp_buf[2048];
  145. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  146. size_t len = 0;
  147. mbedtls_pk_type_t pk_alg;
  148. /*
  149. * Prepare data to be signed in tmp_buf
  150. */
  151. c = tmp_buf + sizeof( tmp_buf );
  152. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
  153. if( len )
  154. {
  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. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  159. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  160. MBEDTLS_ASN1_SET ) );
  161. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  162. MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
  163. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  164. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  165. MBEDTLS_ASN1_SEQUENCE ) );
  166. }
  167. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  168. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  169. MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
  170. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
  171. tmp_buf, c - tmp_buf ) );
  172. c -= pub_len;
  173. len += pub_len;
  174. /*
  175. * Subject ::= Name
  176. */
  177. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
  178. /*
  179. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  180. */
  181. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
  182. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  183. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  184. MBEDTLS_ASN1_SEQUENCE ) );
  185. /*
  186. * Prepare signature
  187. */
  188. ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
  189. if( ret != 0 )
  190. return( ret );
  191. if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
  192. f_rng, p_rng ) ) != 0 )
  193. {
  194. return( ret );
  195. }
  196. if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
  197. pk_alg = MBEDTLS_PK_RSA;
  198. else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
  199. pk_alg = MBEDTLS_PK_ECDSA;
  200. else
  201. return( MBEDTLS_ERR_X509_INVALID_ALG );
  202. if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  203. &sig_oid, &sig_oid_len ) ) != 0 )
  204. {
  205. return( ret );
  206. }
  207. /*
  208. * Write data to output buffer
  209. */
  210. c2 = buf + size;
  211. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
  212. sig_oid, sig_oid_len, sig, sig_len ) );
  213. if( len > (size_t)( c2 - buf ) )
  214. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  215. c2 -= len;
  216. memcpy( c2, c, len );
  217. len += sig_and_oid_len;
  218. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  219. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
  220. MBEDTLS_ASN1_SEQUENCE ) );
  221. return( (int) len );
  222. }
  223. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  224. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  225. #if defined(MBEDTLS_PEM_WRITE_C)
  226. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  227. int (*f_rng)(void *, unsigned char *, size_t),
  228. void *p_rng )
  229. {
  230. int ret;
  231. unsigned char output_buf[4096];
  232. size_t olen = 0;
  233. if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
  234. f_rng, p_rng ) ) < 0 )
  235. {
  236. return( ret );
  237. }
  238. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
  239. output_buf + sizeof(output_buf) - ret,
  240. ret, buf, size, &olen ) ) != 0 )
  241. {
  242. return( ret );
  243. }
  244. return( 0 );
  245. }
  246. #endif /* MBEDTLS_PEM_WRITE_C */
  247. #endif /* MBEDTLS_X509_CSR_WRITE_C */