x509write_csr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * X.509 Certificate Signing Request writing
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. /*
  20. * References:
  21. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  22. * - attributes: PKCS#9 v2.0 aka RFC 2985
  23. */
  24. #include "common.h"
  25. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  26. #include "mbedtls/x509_csr.h"
  27. #include "mbedtls/asn1write.h"
  28. #include "mbedtls/error.h"
  29. #include "mbedtls/oid.h"
  30. #include "mbedtls/platform_util.h"
  31. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  32. #include "psa/crypto.h"
  33. #include "mbedtls/psa_util.h"
  34. #endif
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #if defined(MBEDTLS_PEM_WRITE_C)
  38. #include "mbedtls/pem.h"
  39. #endif
  40. #include "mbedtls/platform.h"
  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] = {0};
  74. unsigned char *c;
  75. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  76. c = buf + 4;
  77. ret = mbedtls_asn1_write_named_bitstring( &c, buf, &key_usage, 8 );
  78. if( ret < 3 || ret > 4 )
  79. return( ret );
  80. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  81. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  82. c, (size_t)ret );
  83. if( ret != 0 )
  84. return( ret );
  85. return( 0 );
  86. }
  87. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  88. unsigned char ns_cert_type )
  89. {
  90. unsigned char buf[4] = {0};
  91. unsigned char *c;
  92. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  93. c = buf + 4;
  94. ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
  95. if( ret < 3 || ret > 4 )
  96. return( ret );
  97. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  98. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  99. c, (size_t)ret );
  100. if( ret != 0 )
  101. return( ret );
  102. return( 0 );
  103. }
  104. static int x509write_csr_der_internal( mbedtls_x509write_csr *ctx,
  105. unsigned char *buf,
  106. size_t size,
  107. unsigned char *sig,
  108. int (*f_rng)(void *, unsigned char *, size_t),
  109. void *p_rng )
  110. {
  111. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  112. const char *sig_oid;
  113. size_t sig_oid_len = 0;
  114. unsigned char *c, *c2;
  115. unsigned char hash[64];
  116. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  117. size_t len = 0;
  118. mbedtls_pk_type_t pk_alg;
  119. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  120. psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
  121. size_t hash_len;
  122. psa_algorithm_t hash_alg = mbedtls_psa_translate_md( ctx->md_alg );
  123. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  124. /* Write the CSR backwards starting from the end of buf */
  125. c = buf + size;
  126. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, buf,
  127. ctx->extensions ) );
  128. if( len )
  129. {
  130. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  131. MBEDTLS_ASN1_CHK_ADD( len,
  132. mbedtls_asn1_write_tag(
  133. &c, buf,
  134. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  135. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  136. MBEDTLS_ASN1_CHK_ADD( len,
  137. mbedtls_asn1_write_tag(
  138. &c, buf,
  139. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) );
  140. MBEDTLS_ASN1_CHK_ADD( len,
  141. mbedtls_asn1_write_oid(
  142. &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  143. MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
  144. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  145. MBEDTLS_ASN1_CHK_ADD( len,
  146. mbedtls_asn1_write_tag(
  147. &c, buf,
  148. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  149. }
  150. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  151. MBEDTLS_ASN1_CHK_ADD( len,
  152. mbedtls_asn1_write_tag(
  153. &c, buf,
  154. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
  155. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
  156. buf, c - buf ) );
  157. c -= pub_len;
  158. len += pub_len;
  159. /*
  160. * Subject ::= Name
  161. */
  162. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf,
  163. ctx->subject ) );
  164. /*
  165. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  166. */
  167. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
  168. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  169. MBEDTLS_ASN1_CHK_ADD( len,
  170. mbedtls_asn1_write_tag(
  171. &c, buf,
  172. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  173. /*
  174. * Sign the written CSR data into the sig buffer
  175. * Note: hash errors can happen only after an internal error
  176. */
  177. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  178. if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
  179. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  180. if( psa_hash_update( &hash_operation, c, len ) != PSA_SUCCESS )
  181. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  182. if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
  183. != PSA_SUCCESS )
  184. {
  185. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  186. }
  187. #else /* MBEDTLS_USE_PSA_CRYPTO */
  188. ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
  189. if( ret != 0 )
  190. return( ret );
  191. #endif
  192. if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
  193. f_rng, p_rng ) ) != 0 )
  194. {
  195. return( ret );
  196. }
  197. if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
  198. pk_alg = MBEDTLS_PK_RSA;
  199. else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
  200. pk_alg = MBEDTLS_PK_ECDSA;
  201. else
  202. return( MBEDTLS_ERR_X509_INVALID_ALG );
  203. if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  204. &sig_oid, &sig_oid_len ) ) != 0 )
  205. {
  206. return( ret );
  207. }
  208. /*
  209. * Move the written CSR data to the start of buf to create space for
  210. * writing the signature into buf.
  211. */
  212. memmove( buf, c, len );
  213. /*
  214. * Write sig and its OID into buf backwards from the end of buf.
  215. * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
  216. * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
  217. */
  218. c2 = buf + size;
  219. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len,
  220. mbedtls_x509_write_sig( &c2, buf + len, sig_oid, sig_oid_len,
  221. sig, sig_len ) );
  222. /*
  223. * Compact the space between the CSR data and signature by moving the
  224. * CSR data to the start of the signature.
  225. */
  226. c2 -= len;
  227. memmove( c2, buf, len );
  228. /* ASN encode the total size and tag the CSR data with it. */
  229. len += sig_and_oid_len;
  230. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  231. MBEDTLS_ASN1_CHK_ADD( len,
  232. mbedtls_asn1_write_tag(
  233. &c2, buf,
  234. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  235. /* Zero the unused bytes at the start of buf */
  236. memset( buf, 0, c2 - buf);
  237. return( (int) len );
  238. }
  239. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf,
  240. size_t size,
  241. int (*f_rng)(void *, unsigned char *, size_t),
  242. void *p_rng )
  243. {
  244. int ret;
  245. unsigned char *sig;
  246. if( ( sig = mbedtls_calloc( 1, MBEDTLS_PK_SIGNATURE_MAX_SIZE ) ) == NULL )
  247. {
  248. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  249. }
  250. ret = x509write_csr_der_internal( ctx, buf, size, sig, f_rng, p_rng );
  251. mbedtls_free( sig );
  252. return( ret );
  253. }
  254. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  255. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  256. #if defined(MBEDTLS_PEM_WRITE_C)
  257. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  258. int (*f_rng)(void *, unsigned char *, size_t),
  259. void *p_rng )
  260. {
  261. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  262. size_t olen = 0;
  263. if( ( ret = mbedtls_x509write_csr_der( ctx, buf, size,
  264. f_rng, p_rng ) ) < 0 )
  265. {
  266. return( ret );
  267. }
  268. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
  269. buf + size - ret,
  270. ret, buf, size, &olen ) ) != 0 )
  271. {
  272. return( ret );
  273. }
  274. return( 0 );
  275. }
  276. #endif /* MBEDTLS_PEM_WRITE_C */
  277. #endif /* MBEDTLS_X509_CSR_WRITE_C */