x509write_csr.c 9.3 KB

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