x509write_csr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #if defined(MBEDTLS_PLATFORM_C)
  41. #include "mbedtls/platform.h"
  42. #else
  43. #include <stdlib.h>
  44. #define mbedtls_calloc calloc
  45. #define mbedtls_free free
  46. #endif
  47. void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
  48. {
  49. memset( ctx, 0, sizeof( mbedtls_x509write_csr ) );
  50. }
  51. void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
  52. {
  53. mbedtls_asn1_free_named_data_list( &ctx->subject );
  54. mbedtls_asn1_free_named_data_list( &ctx->extensions );
  55. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_csr ) );
  56. }
  57. void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
  58. {
  59. ctx->md_alg = md_alg;
  60. }
  61. void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
  62. {
  63. ctx->key = key;
  64. }
  65. int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
  66. const char *subject_name )
  67. {
  68. return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
  69. }
  70. int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
  71. const char *oid, size_t oid_len,
  72. const unsigned char *val, size_t val_len )
  73. {
  74. return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
  75. 0, val, val_len );
  76. }
  77. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
  78. {
  79. unsigned char buf[4] = {0};
  80. unsigned char *c;
  81. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  82. c = buf + 4;
  83. ret = mbedtls_asn1_write_named_bitstring( &c, buf, &key_usage, 8 );
  84. if( ret < 3 || ret > 4 )
  85. return( ret );
  86. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  87. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  88. c, (size_t)ret );
  89. if( ret != 0 )
  90. return( ret );
  91. return( 0 );
  92. }
  93. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  94. unsigned char ns_cert_type )
  95. {
  96. unsigned char buf[4] = {0};
  97. unsigned char *c;
  98. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  99. c = buf + 4;
  100. ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
  101. if( ret < 3 || ret > 4 )
  102. return( ret );
  103. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  104. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  105. c, (size_t)ret );
  106. if( ret != 0 )
  107. return( ret );
  108. return( 0 );
  109. }
  110. static int x509write_csr_der_internal( mbedtls_x509write_csr *ctx,
  111. unsigned char *buf,
  112. size_t size,
  113. unsigned char *sig,
  114. int (*f_rng)(void *, unsigned char *, size_t),
  115. void *p_rng )
  116. {
  117. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  118. const char *sig_oid;
  119. size_t sig_oid_len = 0;
  120. unsigned char *c, *c2;
  121. unsigned char hash[64];
  122. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  123. size_t len = 0;
  124. mbedtls_pk_type_t pk_alg;
  125. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  126. psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
  127. size_t hash_len;
  128. psa_algorithm_t hash_alg = mbedtls_psa_translate_md( ctx->md_alg );
  129. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  130. /* Write the CSR backwards starting from the end of buf */
  131. c = buf + size;
  132. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, buf,
  133. ctx->extensions ) );
  134. if( len )
  135. {
  136. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  137. MBEDTLS_ASN1_CHK_ADD( len,
  138. mbedtls_asn1_write_tag(
  139. &c, buf,
  140. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  141. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  142. MBEDTLS_ASN1_CHK_ADD( len,
  143. mbedtls_asn1_write_tag(
  144. &c, buf,
  145. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) );
  146. MBEDTLS_ASN1_CHK_ADD( len,
  147. mbedtls_asn1_write_oid(
  148. &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  149. MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
  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_SEQUENCE ) );
  155. }
  156. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  157. MBEDTLS_ASN1_CHK_ADD( len,
  158. mbedtls_asn1_write_tag(
  159. &c, buf,
  160. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
  161. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
  162. buf, c - 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, buf,
  169. ctx->subject ) );
  170. /*
  171. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  172. */
  173. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
  174. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  175. MBEDTLS_ASN1_CHK_ADD( len,
  176. mbedtls_asn1_write_tag(
  177. &c, buf,
  178. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  179. /*
  180. * Sign the written CSR data into the sig buffer
  181. * Note: hash errors can happen only after an internal error
  182. */
  183. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  184. if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
  185. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  186. if( psa_hash_update( &hash_operation, c, len ) != PSA_SUCCESS )
  187. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  188. if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
  189. != PSA_SUCCESS )
  190. {
  191. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  192. }
  193. #else /* MBEDTLS_USE_PSA_CRYPTO */
  194. ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
  195. if( ret != 0 )
  196. return( ret );
  197. #endif
  198. if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
  199. f_rng, p_rng ) ) != 0 )
  200. {
  201. return( ret );
  202. }
  203. if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
  204. pk_alg = MBEDTLS_PK_RSA;
  205. else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
  206. pk_alg = MBEDTLS_PK_ECDSA;
  207. else
  208. return( MBEDTLS_ERR_X509_INVALID_ALG );
  209. if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  210. &sig_oid, &sig_oid_len ) ) != 0 )
  211. {
  212. return( ret );
  213. }
  214. /*
  215. * Move the written CSR data to the start of buf to create space for
  216. * writing the signature into buf.
  217. */
  218. memmove( buf, c, len );
  219. /*
  220. * Write sig and its OID into buf backwards from the end of buf.
  221. * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
  222. * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
  223. */
  224. c2 = buf + size;
  225. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len,
  226. mbedtls_x509_write_sig( &c2, buf + len, sig_oid, sig_oid_len,
  227. sig, sig_len ) );
  228. /*
  229. * Compact the space between the CSR data and signature by moving the
  230. * CSR data to the start of the signature.
  231. */
  232. c2 -= len;
  233. memmove( c2, buf, len );
  234. /* ASN encode the total size and tag the CSR data with it. */
  235. len += sig_and_oid_len;
  236. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  237. MBEDTLS_ASN1_CHK_ADD( len,
  238. mbedtls_asn1_write_tag(
  239. &c2, buf,
  240. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  241. /* Zero the unused bytes at the start of buf */
  242. memset( buf, 0, c2 - buf);
  243. return( (int) len );
  244. }
  245. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf,
  246. size_t size,
  247. int (*f_rng)(void *, unsigned char *, size_t),
  248. void *p_rng )
  249. {
  250. int ret;
  251. unsigned char *sig;
  252. if( ( sig = mbedtls_calloc( 1, MBEDTLS_PK_SIGNATURE_MAX_SIZE ) ) == NULL )
  253. {
  254. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  255. }
  256. ret = x509write_csr_der_internal( ctx, buf, size, sig, f_rng, p_rng );
  257. mbedtls_free( sig );
  258. return( ret );
  259. }
  260. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  261. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  262. #if defined(MBEDTLS_PEM_WRITE_C)
  263. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  264. int (*f_rng)(void *, unsigned char *, size_t),
  265. void *p_rng )
  266. {
  267. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  268. size_t olen = 0;
  269. if( ( ret = mbedtls_x509write_csr_der( ctx, buf, size,
  270. f_rng, p_rng ) ) < 0 )
  271. {
  272. return( ret );
  273. }
  274. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
  275. buf + size - ret,
  276. ret, buf, size, &olen ) ) != 0 )
  277. {
  278. return( ret );
  279. }
  280. return( 0 );
  281. }
  282. #endif /* MBEDTLS_PEM_WRITE_C */
  283. #endif /* MBEDTLS_X509_CSR_WRITE_C */