blob: d59354dc4fcd660a8c0cd5893a9ec0fa76480551 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 Certificate Signing Request writing
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * References:
23 * - CSRs: PKCS#10 v1.7 aka RFC 2986
24 * - attributes: PKCS#9 v2.0 aka RFC 2985
25 */
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/x509_csr.h"
36#include "mbedtls/oid.h"
37#include "mbedtls/asn1write.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40#include <stdlib.h>
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044#endif
45
Paul Bakker34617722014-06-13 17:20:13 +020046/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020048 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
49}
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051void mbedtls_x509write_csr_init( mbedtls_x509write_csr *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052{
Hanno Becker81535d02017-09-13 15:39:59 +010053 memset( ctx, 0, sizeof( mbedtls_x509write_csr ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054}
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056void mbedtls_x509write_csr_free( mbedtls_x509write_csr *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_asn1_free_named_data_list( &ctx->subject );
59 mbedtls_asn1_free_named_data_list( &ctx->extensions );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Hanno Becker81535d02017-09-13 15:39:59 +010061 mbedtls_zeroize( ctx, sizeof( mbedtls_x509write_csr ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062}
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064void mbedtls_x509write_csr_set_md_alg( mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065{
66 ctx->md_alg = md_alg;
67}
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069void mbedtls_x509write_csr_set_key( mbedtls_x509write_csr *ctx, mbedtls_pk_context *key )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070{
71 ctx->key = key;
72}
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074int mbedtls_x509write_csr_set_subject_name( mbedtls_x509write_csr *ctx,
Paul Bakker50dc8502013-10-28 21:19:10 +010075 const char *subject_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078}
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080int mbedtls_x509write_csr_set_extension( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081 const char *oid, size_t oid_len,
82 const unsigned char *val, size_t val_len )
83{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 return mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085 0, val, val_len );
86}
87
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +010088static size_t csr_get_unused_bits_for_named_bitstring( unsigned char bitstring,
89 size_t bit_offset )
90{
91 size_t unused_bits;
92
93 /* Count the unused bits removing trailing 0s */
94 for( unused_bits = bit_offset; unused_bits < 8; unused_bits++ )
95 if( ( ( bitstring >> unused_bits ) & 0x1 ) != 0 )
96 break;
97
98 return( unused_bits );
99}
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101int mbedtls_x509write_csr_set_key_usage( mbedtls_x509write_csr *ctx, unsigned char key_usage )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102{
103 unsigned char buf[4];
104 unsigned char *c;
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100105 size_t unused_bits;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106 int ret;
107
108 c = buf + 4;
109
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100110 unused_bits = csr_get_unused_bits_for_named_bitstring( key_usage, 0 );
111 ret = mbedtls_asn1_write_bitstring( &c, buf, &key_usage, 8 - unused_bits );
112
113 if( ret < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114 return( ret );
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100115 else if( ret < 3 || ret > 4 )
116 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
119 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100120 c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121 if( ret != 0 )
122 return( ret );
123
124 return( 0 );
125}
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_x509write_csr_set_ns_cert_type( mbedtls_x509write_csr *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200128 unsigned char ns_cert_type )
129{
130 unsigned char buf[4];
131 unsigned char *c;
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100132 size_t unused_bits;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200133 int ret;
134
135 c = buf + 4;
136
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100137 unused_bits = csr_get_unused_bits_for_named_bitstring( ns_cert_type, 0 );
138 ret = mbedtls_asn1_write_bitstring( &c,
139 buf,
140 &ns_cert_type,
141 8 - unused_bits );
142
143 if( ret < 0 )
144 return( ret );
145 else if( ret < 3 || ret > 4 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146 return( ret );
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 ret = mbedtls_x509write_csr_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
149 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
Andres Amaya Garcia04ee5e02018-09-26 10:48:24 +0100150 c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200151 if( ret != 0 )
152 return( ret );
153
154 return( 0 );
155}
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_x509write_csr_der( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200158 int (*f_rng)(void *, unsigned char *, size_t),
159 void *p_rng )
160{
161 int ret;
162 const char *sig_oid;
163 size_t sig_oid_len = 0;
164 unsigned char *c, *c2;
165 unsigned char hash[64];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167 unsigned char tmp_buf[2048];
168 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
169 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171
172 /*
173 * Prepare data to be signed in tmp_buf
174 */
175 c = tmp_buf + sizeof( tmp_buf );
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_extensions( &c, tmp_buf, ctx->extensions ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178
179 if( len )
180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
182 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
183 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
186 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
187 MBEDTLS_ASN1_SET ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( &c, tmp_buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
190 MBEDTLS_OID_SIZE( MBEDTLS_OID_PKCS9_CSR_EXT_REQ ) ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
193 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
194 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195 }
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
198 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
199 MBEDTLS_ASN1_CONTEXT_SPECIFIC ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_pk_write_pubkey_der( ctx->key,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202 tmp_buf, c - tmp_buf ) );
203 c -= pub_len;
204 len += pub_len;
205
206 /*
207 * Subject ::= Name
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, tmp_buf, ctx->subject ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210
211 /*
212 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, tmp_buf, 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, tmp_buf, len ) );
217 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, tmp_buf, MBEDTLS_ASN1_CONSTRUCTED |
218 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219
220 /*
221 * Prepare signature
222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
Hanno Beckerfc771442017-09-13 08:45:48 +0100226 f_rng, p_rng ) ) != 0 )
227 {
228 return( ret );
229 }
230
231 if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_RSA ) )
232 pk_alg = MBEDTLS_PK_RSA;
233 else if( mbedtls_pk_can_do( ctx->key, MBEDTLS_PK_ECDSA ) )
234 pk_alg = MBEDTLS_PK_ECDSA;
235 else
Hanno Beckerd8a6f7c2017-09-22 16:05:43 +0100236 return( MBEDTLS_ERR_X509_INVALID_ALG );
Hanno Beckerfc771442017-09-13 08:45:48 +0100237
238 if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
239 &sig_oid, &sig_oid_len ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240 {
241 return( ret );
242 }
243
244 /*
245 * Write data to output buffer
246 */
247 c2 = buf + size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, buf,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249 sig_oid, sig_oid_len, sig, sig_len ) );
250
Andres AG60dbc932016-09-02 15:23:48 +0100251 if( len > (size_t)( c2 - buf ) )
252 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
253
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254 c2 -= len;
255 memcpy( c2, c, len );
256
257 len += sig_and_oid_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c2, buf, len ) );
259 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c2, buf, MBEDTLS_ASN1_CONSTRUCTED |
260 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200262 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263}
264
265#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
266#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_PEM_WRITE_C)
269int mbedtls_x509write_csr_pem( mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270 int (*f_rng)(void *, unsigned char *, size_t),
271 void *p_rng )
272{
273 int ret;
274 unsigned char output_buf[4096];
275 size_t olen = 0;
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( ( ret = mbedtls_x509write_csr_der( ctx, output_buf, sizeof(output_buf),
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278 f_rng, p_rng ) ) < 0 )
279 {
280 return( ret );
281 }
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CSR, PEM_END_CSR,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 output_buf + sizeof(output_buf) - ret,
285 ret, buf, size, &olen ) ) != 0 )
286 {
287 return( ret );
288 }
289
290 return( 0 );
291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#endif /* MBEDTLS_X509_CSR_WRITE_C */