blob: de6193432256291012795660b53d4b4f0d24947a [file] [log] [blame]
Edison Aic6672fd2018-02-28 15:01:47 +08001// SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02002/*
3 * X.509 Certificate Signing Request writing
4 *
5 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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"
Jens Wiklander3d3b0592019-03-20 15:30:29 +010038#include "mbedtls/platform_util.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020039
40#include <string.h>
41#include <stdlib.h>
42
43#if defined(MBEDTLS_PEM_WRITE_C)
44#include "mbedtls/pem.h"
45#endif
46
Jerome Forissier5b25c762020-04-07 11:18:49 +020047/*
48 * For the currently used signature algorithms the buffer to store any signature
49 * must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE)
50 */
51#if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE
52#define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN
53#else
54#define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE
55#endif
56
Jens Wiklander817466c2018-05-22 13:49:31 +020057void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
58{
Jens Wiklander3d3b0592019-03-20 15:30:29 +010059 memset( ctx, 0, sizeof( mbedtls_x509write_csr ) );
Jens Wiklander817466c2018-05-22 13:49:31 +020060}
61
62void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
63{
64 mbedtls_asn1_free_named_data_list( &ctx->subject );
65 mbedtls_asn1_free_named_data_list( &ctx->extensions );
66
Jens Wiklander3d3b0592019-03-20 15:30:29 +010067 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_csr ) );
Jens Wiklander817466c2018-05-22 13:49:31 +020068}
69
70void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
71{
72 ctx->md_alg = md_alg;
73}
74
75void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
76{
77 ctx->key = key;
78}
79
80int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
81 const char *subject_name )
82{
83 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
84}
85
86int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
87 const char *oid, size_t oid_len,
88 const unsigned char *val, size_t val_len )
89{
90 return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
91 0, val, val_len );
92}
93
Jerome Forissier5b25c762020-04-07 11:18:49 +020094static size_t csr_get_unused_bits_for_named_bitstring( unsigned char bitstring,
95 size_t bit_offset )
96{
97 size_t unused_bits;
98
99 /* Count the unused bits removing trailing 0s */
100 for( unused_bits = bit_offset; unused_bits < 8; unused_bits++ )
101 if( ( ( bitstring >> unused_bits ) & 0x1 ) != 0 )
102 break;
103
104 return( unused_bits );
105}
106
Jens Wiklander817466c2018-05-22 13:49:31 +0200107int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
108{
109 unsigned char buf[4];
110 unsigned char *c;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200111 size_t unused_bits;
Jens Wiklander817466c2018-05-22 13:49:31 +0200112 int ret;
113
114 c = buf + 4;
115
Jerome Forissier5b25c762020-04-07 11:18:49 +0200116 unused_bits = csr_get_unused_bits_for_named_bitstring( key_usage, 0 );
117 ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 8 - unused_bits );
118
119 if( ret < 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200120 return( ret );
Jerome Forissier5b25c762020-04-07 11:18:49 +0200121 else if( ret < 3 || ret > 4 )
122 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Jens Wiklander817466c2018-05-22 13:49:31 +0200123
124 ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
125 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
Jerome Forissier5b25c762020-04-07 11:18:49 +0200126 c, (size_t)ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200127 if( ret != 0 )
128 return( ret );
129
130 return( 0 );
131}
132
133int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
134 unsigned char ns_cert_type )
135{
136 unsigned char buf[4];
137 unsigned char *c;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200138 size_t unused_bits;
Jens Wiklander817466c2018-05-22 13:49:31 +0200139 int ret;
140
141 c = buf + 4;
142
Jerome Forissier5b25c762020-04-07 11:18:49 +0200143 unused_bits = csr_get_unused_bits_for_named_bitstring( ns_cert_type, 0 );
144 ret = mbedtls_asn1_write_bitstring( &c,
145 buf,
146 &ns_cert_type,
147 8 - unused_bits );
148
149 if( ret < 0 )
150 return( ret );
151 else if( ret < 3 || ret > 4 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200152 return( ret );
153
154 ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
155 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
Jerome Forissier5b25c762020-04-07 11:18:49 +0200156 c, (size_t)ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200157 if( ret != 0 )
158 return( ret );
159
160 return( 0 );
161}
162
163int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
164 int (*f_rng)(void *, unsigned char *, size_t),
165 void *p_rng )
166{
167 int ret;
168 const char *sig_oid;
169 size_t sig_oid_len = 0;
170 unsigned char *c, *c2;
171 unsigned char hash[64];
Jerome Forissier5b25c762020-04-07 11:18:49 +0200172 unsigned char sig[SIGNATURE_MAX_SIZE];
Jens Wiklander817466c2018-05-22 13:49:31 +0200173 unsigned char tmp_buf[2048];
174 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
175 size_t len = 0;
176 mbedtls_pk_type_t pk_alg;
177
178 /*
179 * Prepare data to be signed in tmp_buf
180 */
181 c = tmp_buf + sizeof( tmp_buf );
182
183 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
184
185 if( len )
186 {
187 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
188 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
189 MBEDTLS_ASN1_SEQUENCE ) );
190
191 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
192 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
193 MBEDTLS_ASN1_SET ) );
194
195 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
196 MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
197
198 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
199 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
200 MBEDTLS_ASN1_SEQUENCE ) );
201 }
202
203 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
205 MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
206
207 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
208 tmp_buf, c - tmp_buf ) );
209 c -= pub_len;
210 len += pub_len;
211
212 /*
213 * Subject ::= Name
214 */
215 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
216
217 /*
218 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
219 */
220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
221
222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
223 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
224 MBEDTLS_ASN1_SEQUENCE ) );
225
226 /*
227 * Prepare signature
228 */
Jerome Forissier5b25c762020-04-07 11:18:49 +0200229 ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
230 if( ret != 0 )
231 return( ret );
Jens Wiklander817466c2018-05-22 13:49:31 +0200232
Jens Wiklander817466c2018-05-22 13:49:31 +0200233 if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100234 f_rng, p_rng ) ) != 0 )
235 {
236 return( ret );
237 }
238
239 if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
240 pk_alg = MBEDTLS_PK_RSA;
241 else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
242 pk_alg = MBEDTLS_PK_ECDSA;
243 else
244 return( MBEDTLS_ERR_X509_INVALID_ALG );
245
246 if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
247 &sig_oid, &sig_oid_len ) ) != 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200248 {
249 return( ret );
250 }
251
252 /*
253 * Write data to output buffer
254 */
255 c2 = buf + size;
256 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
257 sig_oid, sig_oid_len, sig, sig_len ) );
258
259 if( len > (size_t)( c2 - buf ) )
260 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
261
262 c2 -= len;
263 memcpy( c2, c, len );
264
265 len += sig_and_oid_len;
266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
267 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
268 MBEDTLS_ASN1_SEQUENCE ) );
269
270 return( (int) len );
271}
272
273#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
274#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
275
276#if defined(MBEDTLS_PEM_WRITE_C)
277int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
278 int (*f_rng)(void *, unsigned char *, size_t),
279 void *p_rng )
280{
281 int ret;
282 unsigned char output_buf[4096];
283 size_t olen = 0;
284
285 if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
286 f_rng, p_rng ) ) < 0 )
287 {
288 return( ret );
289 }
290
291 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
292 output_buf + sizeof(output_buf) - ret,
293 ret, buf, size, &olen ) ) != 0 )
294 {
295 return( ret );
296 }
297
298 return( 0 );
299}
300#endif /* MBEDTLS_PEM_WRITE_C */
301
302#endif /* MBEDTLS_X509_CSR_WRITE_C */