x509write_csr.c 11 KB

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