blob: 8dc39e7a5a5765b7d1ccd95b3efba79da602b933 [file] [log] [blame]
Gilles Peskine458b8f22020-02-26 18:28:28 +01001/*
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
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "mbedtls/config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
33#if defined(MBEDTLS_X509_CSR_WRITE_C)
34
35#include "mbedtls/x509_csr.h"
36#include "mbedtls/oid.h"
37#include "mbedtls/asn1write.h"
38#include "mbedtls/platform_util.h"
39
40#if defined(MBEDTLS_USE_PSA_CRYPTO)
41#include "psa/crypto.h"
42#include "mbedtls/psa_util.h"
43#endif
44
45#include <string.h>
46#include <stdlib.h>
47
48#if defined(MBEDTLS_PEM_WRITE_C)
49#include "mbedtls/pem.h"
50#endif
51
52void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
53{
54 memset( ctx, 0, sizeof( mbedtls_x509write_csr ) );
55}
56
57void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
58{
59 mbedtls_asn1_free_named_data_list( &ctx->subject );
60 mbedtls_asn1_free_named_data_list( &ctx->extensions );
61
62 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_csr ) );
63}
64
65void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
66{
67 ctx->md_alg = md_alg;
68}
69
70void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
71{
72 ctx->key = key;
73}
74
75int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
76 const char *subject_name )
77{
78 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
79}
80
81int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
82 const char *oid, size_t oid_len,
83 const unsigned char *val, size_t val_len )
84{
85 return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
86 0, val, val_len );
87}
88
89int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
90{
91 unsigned char buf[4];
92 unsigned char *c;
93 int ret;
94
95 c = buf + 4;
96
97 ret = mbedtls_asn1_write_named_bitstring( &c, buf, &key_usage, 8 );
98 if( ret < 3 || ret > 4 )
99 return( ret );
100
101 ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
102 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
103 c, (size_t)ret );
104 if( ret != 0 )
105 return( ret );
106
107 return( 0 );
108}
109
110int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
111 unsigned char ns_cert_type )
112{
113 unsigned char buf[4];
114 unsigned char *c;
115 int ret;
116
117 c = buf + 4;
118
119 ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
120 if( ret < 3 || ret > 4 )
121 return( ret );
122
123 ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
124 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
125 c, (size_t)ret );
126 if( ret != 0 )
127 return( ret );
128
129 return( 0 );
130}
131
132int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
133 int (*f_rng)(void *, unsigned char *, size_t),
134 void *p_rng )
135{
136 int ret;
137 const char *sig_oid;
138 size_t sig_oid_len = 0;
139 unsigned char *c, *c2;
140 unsigned char hash[64];
141 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
142 unsigned char tmp_buf[2048];
143 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
144 size_t len = 0;
145 mbedtls_pk_type_t pk_alg;
146#if defined(MBEDTLS_USE_PSA_CRYPTO)
147 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
148 size_t hash_len;
149 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( ctx->md_alg );
150#endif /* MBEDTLS_USE_PSA_CRYPTO */
151 /*
152 * Prepare data to be signed in tmp_buf
153 */
154 c = tmp_buf + sizeof( tmp_buf );
155
156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
157
158 if( len )
159 {
160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
161 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
162 MBEDTLS_ASN1_SEQUENCE ) );
163
164 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
165 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
166 MBEDTLS_ASN1_SET ) );
167
168 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
169 MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
170
171 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
172 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
173 MBEDTLS_ASN1_SEQUENCE ) );
174 }
175
176 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
177 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
178 MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
179
180 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
181 tmp_buf, c - tmp_buf ) );
182 c -= pub_len;
183 len += pub_len;
184
185 /*
186 * Subject ::= Name
187 */
188 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
189
190 /*
191 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
192 */
193 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
194
195 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
196 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
197 MBEDTLS_ASN1_SEQUENCE ) );
198
199 /*
200 * Prepare signature
201 * Note: hash errors can happen only after an internal error
202 */
203#if defined(MBEDTLS_USE_PSA_CRYPTO)
204 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
205 return( MBEDTLS_ERR_X509_FATAL_ERROR );
206
207 if( psa_hash_update( &hash_operation, c, len ) != PSA_SUCCESS )
208 return( MBEDTLS_ERR_X509_FATAL_ERROR );
209
210 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
211 != PSA_SUCCESS )
212 {
213 return( MBEDTLS_ERR_X509_FATAL_ERROR );
214 }
215#else /* MBEDTLS_USE_PSA_CRYPTO */
216 mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
217#endif
218 if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
219 f_rng, p_rng ) ) != 0 )
220 {
221 return( ret );
222 }
223
224 if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
225 pk_alg = MBEDTLS_PK_RSA;
226 else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
227 pk_alg = MBEDTLS_PK_ECDSA;
228 else
229 return( MBEDTLS_ERR_X509_INVALID_ALG );
230
231 if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
232 &sig_oid, &sig_oid_len ) ) != 0 )
233 {
234 return( ret );
235 }
236
237 /*
238 * Write data to output buffer
239 */
240 c2 = buf + size;
241 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
242 sig_oid, sig_oid_len, sig, sig_len ) );
243
244 if( len > (size_t)( c2 - buf ) )
245 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
246
247 c2 -= len;
248 memcpy( c2, c, len );
249
250 len += sig_and_oid_len;
251 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
252 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
253 MBEDTLS_ASN1_SEQUENCE ) );
254
255 return( (int) len );
256}
257
258#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
259#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
260
261#if defined(MBEDTLS_PEM_WRITE_C)
262int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
263 int (*f_rng)(void *, unsigned char *, size_t),
264 void *p_rng )
265{
266 int ret;
267 unsigned char output_buf[4096];
268 size_t olen = 0;
269
270 if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
271 f_rng, p_rng ) ) < 0 )
272 {
273 return( ret );
274 }
275
276 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
277 output_buf + sizeof(output_buf) - ret,
278 ret, buf, size, &olen ) ) != 0 )
279 {
280 return( ret );
281 }
282
283 return( 0 );
284}
285#endif /* MBEDTLS_PEM_WRITE_C */
286
287#endif /* MBEDTLS_X509_CSR_WRITE_C */