x509write_csr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * X.509 Certificate Signing Request writing
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. /*
  47. * References:
  48. * - CSRs: PKCS#10 v1.7 aka RFC 2986
  49. * - attributes: PKCS#9 v2.0 aka RFC 2985
  50. */
  51. #if !defined(MBEDTLS_CONFIG_FILE)
  52. #include "mbedtls/config.h"
  53. #else
  54. #include MBEDTLS_CONFIG_FILE
  55. #endif
  56. #if defined(MBEDTLS_X509_CSR_WRITE_C)
  57. #include "mbedtls/x509_csr.h"
  58. #include "mbedtls/oid.h"
  59. #include "mbedtls/asn1write.h"
  60. #include "mbedtls/platform_util.h"
  61. #include <string.h>
  62. #include <stdlib.h>
  63. #if defined(MBEDTLS_PEM_WRITE_C)
  64. #include "mbedtls/pem.h"
  65. #endif
  66. /*
  67. * For the currently used signature algorithms the buffer to store any signature
  68. * must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)
  69. */
  70. #if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE
  71. #define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
  72. #else
  73. #define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
  74. #endif
  75. #if defined(MBEDTLS_PLATFORM_C)
  76. #include "mbedtls/platform.h"
  77. #else
  78. #include <stdlib.h>
  79. #define mbedtls_calloc calloc
  80. #define mbedtls_free free
  81. #endif
  82. void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
  83. {
  84. memset( ctx, 0, sizeof( mbedtls_x509write_csr ) );
  85. }
  86. void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
  87. {
  88. mbedtls_asn1_free_named_data_list( &ctx->subject );
  89. mbedtls_asn1_free_named_data_list( &ctx->extensions );
  90. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_csr ) );
  91. }
  92. void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
  93. {
  94. ctx->md_alg = md_alg;
  95. }
  96. void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
  97. {
  98. ctx->key = key;
  99. }
  100. int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
  101. const char *subject_name )
  102. {
  103. return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
  104. }
  105. int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
  106. const char *oid, size_t oid_len,
  107. const unsigned char *val, size_t val_len )
  108. {
  109. return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
  110. 0, val, val_len );
  111. }
  112. static size_t csr_get_unused_bits_for_named_bitstring( unsigned char bitstring,
  113. size_t bit_offset )
  114. {
  115. size_t unused_bits;
  116. /* Count the unused bits removing trailing 0s */
  117. for( unused_bits = bit_offset; unused_bits < 8; unused_bits++ )
  118. if( ( ( bitstring >> unused_bits ) & 0x1 ) != 0 )
  119. break;
  120. return( unused_bits );
  121. }
  122. int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
  123. {
  124. unsigned char buf[4];
  125. unsigned char *c;
  126. size_t unused_bits;
  127. int ret;
  128. c = buf + 4;
  129. unused_bits = csr_get_unused_bits_for_named_bitstring( key_usage, 0 );
  130. ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 8 - unused_bits );
  131. if( ret < 0 )
  132. return( ret );
  133. else if( ret < 3 || ret > 4 )
  134. return( MBEDTLS_ERR_X509_INVALID_FORMAT );
  135. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
  136. MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
  137. c, (size_t)ret );
  138. if( ret != 0 )
  139. return( ret );
  140. return( 0 );
  141. }
  142. int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
  143. unsigned char ns_cert_type )
  144. {
  145. unsigned char buf[4];
  146. unsigned char *c;
  147. size_t unused_bits;
  148. int ret;
  149. c = buf + 4;
  150. unused_bits = csr_get_unused_bits_for_named_bitstring( ns_cert_type, 0 );
  151. ret = mbedtls_asn1_write_bitstring( &c,
  152. buf,
  153. &ns_cert_type,
  154. 8 - unused_bits );
  155. if( ret < 0 )
  156. return( ret );
  157. else if( ret < 3 || ret > 4 )
  158. return( ret );
  159. ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
  160. MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
  161. c, (size_t)ret );
  162. if( ret != 0 )
  163. return( ret );
  164. return( 0 );
  165. }
  166. static int x509write_csr_der_internal( mbedtls_x509write_csr *ctx,
  167. unsigned char *buf,
  168. size_t size,
  169. unsigned char *sig,
  170. int (*f_rng)(void *, unsigned char *, size_t),
  171. void *p_rng )
  172. {
  173. int ret;
  174. const char *sig_oid;
  175. size_t sig_oid_len = 0;
  176. unsigned char *c, *c2;
  177. unsigned char hash[64];
  178. size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
  179. size_t len = 0;
  180. mbedtls_pk_type_t pk_alg;
  181. /* Write the CSR backwards starting from the end of buf */
  182. c = buf + size;
  183. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, buf,
  184. ctx->extensions ) );
  185. if( len )
  186. {
  187. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  188. MBEDTLS_ASN1_CHK_ADD( len,
  189. mbedtls_asn1_write_tag(
  190. &c, buf,
  191. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  192. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  193. MBEDTLS_ASN1_CHK_ADD( len,
  194. mbedtls_asn1_write_tag(
  195. &c, buf,
  196. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET ) );
  197. MBEDTLS_ASN1_CHK_ADD( len,
  198. mbedtls_asn1_write_oid(
  199. &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
  200. MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
  201. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  202. MBEDTLS_ASN1_CHK_ADD( len,
  203. mbedtls_asn1_write_tag(
  204. &c, buf,
  205. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  206. }
  207. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  208. MBEDTLS_ASN1_CHK_ADD( len,
  209. mbedtls_asn1_write_tag(
  210. &c, buf,
  211. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
  212. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
  213. buf, c - buf ) );
  214. c -= pub_len;
  215. len += pub_len;
  216. /*
  217. * Subject ::= Name
  218. */
  219. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf,
  220. ctx->subject ) );
  221. /*
  222. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  223. */
  224. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
  225. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  226. MBEDTLS_ASN1_CHK_ADD( len,
  227. mbedtls_asn1_write_tag(
  228. &c, buf,
  229. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  230. /*
  231. * Sign the written CSR data into the sig buffer
  232. * Note: hash errors can happen only after an internal error
  233. */
  234. ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
  235. if( ret != 0 )
  236. return( ret );
  237. if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
  238. f_rng, p_rng ) ) != 0 )
  239. {
  240. return( ret );
  241. }
  242. if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
  243. pk_alg = MBEDTLS_PK_RSA;
  244. else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
  245. pk_alg = MBEDTLS_PK_ECDSA;
  246. else
  247. return( MBEDTLS_ERR_X509_INVALID_ALG );
  248. if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
  249. &sig_oid, &sig_oid_len ) ) != 0 )
  250. {
  251. return( ret );
  252. }
  253. /*
  254. * Move the written CSR data to the start of buf to create space for
  255. * writing the signature into buf.
  256. */
  257. memmove( buf, c, len );
  258. /*
  259. * Write sig and its OID into buf backwards from the end of buf.
  260. * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
  261. * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
  262. */
  263. c2 = buf + size;
  264. MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len,
  265. mbedtls_x509_write_sig( &c2, buf + len, sig_oid, sig_oid_len,
  266. sig, sig_len ) );
  267. /*
  268. * Compact the space between the CSR data and signature by moving the
  269. * CSR data to the start of the signature.
  270. */
  271. c2 -= len;
  272. memmove( c2, buf, len );
  273. /* ASN encode the total size and tag the CSR data with it. */
  274. len += sig_and_oid_len;
  275. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
  276. MBEDTLS_ASN1_CHK_ADD( len,
  277. mbedtls_asn1_write_tag(
  278. &c2, buf,
  279. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
  280. /* Zero the unused bytes at the start of buf */
  281. memset( buf, 0, c2 - buf);
  282. return( (int) len );
  283. }
  284. int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf,
  285. size_t size,
  286. int (*f_rng)(void *, unsigned char *, size_t),
  287. void *p_rng )
  288. {
  289. int ret;
  290. unsigned char *sig;
  291. if( ( sig = mbedtls_calloc( 1, SIGNATURE_MAX_SIZE ) ) == NULL )
  292. {
  293. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  294. }
  295. ret = x509write_csr_der_internal( ctx, buf, size, sig, f_rng, p_rng );
  296. mbedtls_free( sig );
  297. return( ret );
  298. }
  299. #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
  300. #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
  301. #if defined(MBEDTLS_PEM_WRITE_C)
  302. int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
  303. int (*f_rng)(void *, unsigned char *, size_t),
  304. void *p_rng )
  305. {
  306. int ret;
  307. unsigned char output_buf[4096];
  308. size_t olen = 0;
  309. if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
  310. f_rng, p_rng ) ) < 0 )
  311. {
  312. return( ret );
  313. }
  314. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
  315. output_buf + sizeof(output_buf) - ret,
  316. ret, buf, size, &olen ) ) != 0 )
  317. {
  318. return( ret );
  319. }
  320. return( 0 );
  321. }
  322. #endif /* MBEDTLS_PEM_WRITE_C */
  323. #endif /* MBEDTLS_X509_CSR_WRITE_C */