x509write_csr.c 8.4 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: 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. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
  72. {
  73. unsigned char buf[4];
  74. unsigned char *c;
  75. int ret;
  76. c = buf + 4;
  77. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 7 ) ) != 4 )
  78. return( ret );
  79. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  80. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  81. buf, 4 );
  82. if( ret != 0 )
  83. return( ret );
  84. return( 0 );
  85. }
  86. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  87. unsigned char ns_cert_type )
  88. {
  89. unsigned char buf[4];
  90. unsigned char *c;
  91. int ret;
  92. c = buf + 4;
  93. if( ( ret = mbedtls_asn1_write_bitstring( &c, buf, &ns_cert_type, 8 ) ) != 4 )
  94. return( ret );
  95. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  96. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  97. buf, 4 );
  98. if( ret != 0 )
  99. return( ret );
  100. return( 0 );
  101. }
  102. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  103. int (*f_rng)(void *, unsigned char *, size_t),
  104. void *p_rng )
  105. {
  106. int ret;
  107. const char *sig_oid;
  108. size_t sig_oid_len = 0;
  109. unsigned char *c, *c2;
  110. unsigned char hash[64];
  111. unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
  112. unsigned char tmp_buf[2048];
  113. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  114. size_t len = 0;
  115. mbedtls_pk_type_t pk_alg;
  116. /*
  117. * Prepare data to be signed in tmp_buf
  118. */
  119. c = tmp_buf + sizeof( tmp_buf );
  120. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
  121. if( len )
  122. {
  123. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  124. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  125. MBEDTLS_ASN1_SEQUENCE ) );
  126. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  127. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  128. MBEDTLS_ASN1_SET ) );
  129. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  130. MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
  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_SEQUENCE ) );
  134. }
  135. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  136. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  137. MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
  138. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
  139. tmp_buf, c - tmp_buf ) );
  140. c -= pub_len;
  141. len += pub_len;
  142. /*
  143. * Subject ::= Name
  144. */
  145. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
  146. /*
  147. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  148. */
  149. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
  150. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
  151. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
  152. MBEDTLS_ASN1_SEQUENCE ) );
  153. /*
  154. * Prepare signature
  155. */
  156. mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
  157. if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
  158. f_rng, p_rng ) ) != 0 )
  159. {
  160. return( ret );
  161. }
  162. if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
  163. pk_alg = MBEDTLS_PK_RSA;
  164. else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
  165. pk_alg = MBEDTLS_PK_ECDSA;
  166. else
  167. return( MBEDTLS_ERR_X509_INVALID_ALG );
  168. if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  169. &sig_oid, &sig_oid_len ) ) != 0 )
  170. {
  171. return( ret );
  172. }
  173. /*
  174. * Write data to output buffer
  175. */
  176. c2 = buf + size;
  177. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
  178. sig_oid, sig_oid_len, sig, sig_len ) );
  179. if( len > (size_t)( c2 - buf ) )
  180. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  181. c2 -= len;
  182. memcpy( c2, c, len );
  183. len += sig_and_oid_len;
  184. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  185. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
  186. MBEDTLS_ASN1_SEQUENCE ) );
  187. return( (int) len );
  188. }
  189. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  190. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  191. #if defined(MBEDTLS_PEM_WRITE_C)
  192. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  193. int (*f_rng)(void *, unsigned char *, size_t),
  194. void *p_rng )
  195. {
  196. int ret;
  197. unsigned char output_buf[4096];
  198. size_t olen = 0;
  199. if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
  200. f_rng, p_rng ) ) < 0 )
  201. {
  202. return( ret );
  203. }
  204. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
  205. output_buf + sizeof(output_buf) - ret,
  206. ret, buf, size, &olen ) ) != 0 )
  207. {
  208. return( ret );
  209. }
  210. return( 0 );
  211. }
  212. #endif /* MBEDTLS_PEM_WRITE_C */
  213. #endif /* MBEDTLS_X509_CSR_WRITE_C */