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 /*
8 * References:
9 * - CSRs: PKCS#10 v1.7 aka RFC 2986
10 * - attributes: PKCS#9 v2.0 aka RFC 2985
11 */
12
13 #include "common.h"
14
15 #if defined(MBEDTLS_X509_CSR_WRITE_C)
16
17 #include "x509_internal.h"
18 #include "mbedtls/x509_csr.h"
19 #include "mbedtls/asn1write.h"
20 #include "mbedtls/error.h"
21 #include "mbedtls/oid.h"
22 #include "mbedtls/platform_util.h"
23
24 #if defined(MBEDTLS_USE_PSA_CRYPTO)
25 #include "psa/crypto.h"
26 #include "psa_util_internal.h"
27 #include "mbedtls/psa_util.h"
28 #endif /* MBEDTLS_USE_PSA_CRYPTO */
29
30 #include <string.h>
31 #include <stdlib.h>
32
33 #if defined(MBEDTLS_PEM_WRITE_C)
34 #include "mbedtls/pem.h"
35 #endif
36
37 #include "mbedtls/platform.h"
38
mbedtls_x509write_csr_init(mbedtls_x509write_csr * ctx)39 void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
40 {
41 memset(ctx, 0, sizeof(mbedtls_x509write_csr));
42 }
43
mbedtls_x509write_csr_free(mbedtls_x509write_csr * ctx)44 void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
45 {
46 mbedtls_asn1_free_named_data_list(&ctx->subject);
47 mbedtls_asn1_free_named_data_list(&ctx->extensions);
48
49 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
50 }
51
mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr * ctx,mbedtls_md_type_t md_alg)52 void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
53 {
54 ctx->md_alg = md_alg;
55 }
56
mbedtls_x509write_csr_set_key(mbedtls_x509write_csr * ctx,mbedtls_pk_context * key)57 void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
58 {
59 ctx->key = key;
60 }
61
mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr * ctx,const char * subject_name)62 int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
63 const char *subject_name)
64 {
65 mbedtls_asn1_free_named_data_list(&ctx->subject);
66 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
67 }
68
mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr * ctx,const char * oid,size_t oid_len,int critical,const unsigned char * val,size_t val_len)69 int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
70 const char *oid, size_t oid_len,
71 int critical,
72 const unsigned char *val, size_t val_len)
73 {
74 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
75 critical, val, val_len);
76 }
77
mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr * ctx,const mbedtls_x509_san_list * san_list)78 int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
79 const mbedtls_x509_san_list *san_list)
80 {
81 return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list);
82 }
83
mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr * ctx,unsigned char key_usage)84 int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
85 {
86 unsigned char buf[4] = { 0 };
87 unsigned char *c;
88 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
89
90 c = buf + 4;
91
92 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
93 if (ret < 3 || ret > 4) {
94 return ret;
95 }
96
97 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
98 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
99 0, c, (size_t) ret);
100 if (ret != 0) {
101 return ret;
102 }
103
104 return 0;
105 }
106
mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr * ctx,unsigned char ns_cert_type)107 int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
108 unsigned char ns_cert_type)
109 {
110 unsigned char buf[4] = { 0 };
111 unsigned char *c;
112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
113
114 c = buf + 4;
115
116 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
117 if (ret < 3 || ret > 4) {
118 return ret;
119 }
120
121 ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
122 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
123 0, c, (size_t) ret);
124 if (ret != 0) {
125 return ret;
126 }
127
128 return 0;
129 }
130
x509write_csr_der_internal(mbedtls_x509write_csr * ctx,unsigned char * buf,size_t size,unsigned char * sig,size_t sig_size,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)131 static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
132 unsigned char *buf,
133 size_t size,
134 unsigned char *sig, size_t sig_size,
135 int (*f_rng)(void *, unsigned char *, size_t),
136 void *p_rng)
137 {
138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
139 const char *sig_oid;
140 size_t sig_oid_len = 0;
141 unsigned char *c, *c2;
142 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
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 size_t hash_len;
148 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ctx->md_alg);
149 #endif /* MBEDTLS_USE_PSA_CRYPTO */
150
151 /* Write the CSR backwards starting from the end of buf */
152 c = buf + size;
153
154 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
155 ctx->extensions));
156
157 if (len) {
158 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
159 MBEDTLS_ASN1_CHK_ADD(len,
160 mbedtls_asn1_write_tag(
161 &c, buf,
162 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
163
164 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
165 MBEDTLS_ASN1_CHK_ADD(len,
166 mbedtls_asn1_write_tag(
167 &c, buf,
168 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
169
170 MBEDTLS_ASN1_CHK_ADD(len,
171 mbedtls_asn1_write_oid(
172 &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
173 MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
174
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
182 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
183 MBEDTLS_ASN1_CHK_ADD(len,
184 mbedtls_asn1_write_tag(
185 &c, buf,
186 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
187
188 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
189 buf, (size_t) (c - buf)));
190 c -= pub_len;
191 len += pub_len;
192
193 /*
194 * Subject ::= Name
195 */
196 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
197 ctx->subject));
198
199 /*
200 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
201 */
202 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
203
204 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
205 MBEDTLS_ASN1_CHK_ADD(len,
206 mbedtls_asn1_write_tag(
207 &c, buf,
208 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
209
210 /*
211 * Sign the written CSR data into the sig buffer
212 * Note: hash errors can happen only after an internal error
213 */
214 #if defined(MBEDTLS_USE_PSA_CRYPTO)
215 if (psa_hash_compute(hash_alg,
216 c,
217 len,
218 hash,
219 sizeof(hash),
220 &hash_len) != PSA_SUCCESS) {
221 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
222 }
223 #else /* MBEDTLS_USE_PSA_CRYPTO */
224 ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
225 if (ret != 0) {
226 return ret;
227 }
228 #endif
229 if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
230 sig, sig_size, &sig_len,
231 f_rng, p_rng)) != 0) {
232 return ret;
233 }
234
235 if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
236 pk_alg = MBEDTLS_PK_RSA;
237 } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
238 pk_alg = MBEDTLS_PK_ECDSA;
239 } else {
240 return MBEDTLS_ERR_X509_INVALID_ALG;
241 }
242
243 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
244 &sig_oid, &sig_oid_len)) != 0) {
245 return ret;
246 }
247
248 /*
249 * Move the written CSR data to the start of buf to create space for
250 * writing the signature into buf.
251 */
252 memmove(buf, c, len);
253
254 /*
255 * Write sig and its OID into buf backwards from the end of buf.
256 * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
257 * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
258 */
259 c2 = buf + size;
260 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
261 mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
262 sig, sig_len, pk_alg));
263
264 /*
265 * Compact the space between the CSR data and signature by moving the
266 * CSR data to the start of the signature.
267 */
268 c2 -= len;
269 memmove(c2, buf, len);
270
271 /* ASN encode the total size and tag the CSR data with it. */
272 len += sig_and_oid_len;
273 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
274 MBEDTLS_ASN1_CHK_ADD(len,
275 mbedtls_asn1_write_tag(
276 &c2, buf,
277 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
278
279 /* Zero the unused bytes at the start of buf */
280 memset(buf, 0, (size_t) (c2 - buf));
281
282 return (int) len;
283 }
284
mbedtls_x509write_csr_der(mbedtls_x509write_csr * ctx,unsigned char * buf,size_t size,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)285 int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
286 size_t size,
287 int (*f_rng)(void *, unsigned char *, size_t),
288 void *p_rng)
289 {
290 int ret;
291 unsigned char *sig;
292
293 if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
294 return MBEDTLS_ERR_X509_ALLOC_FAILED;
295 }
296
297 ret = x509write_csr_der_internal(ctx, buf, size,
298 sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
299 f_rng, p_rng);
300
301 mbedtls_free(sig);
302
303 return ret;
304 }
305
306 #define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
307 #define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
308
309 #if defined(MBEDTLS_PEM_WRITE_C)
mbedtls_x509write_csr_pem(mbedtls_x509write_csr * ctx,unsigned char * buf,size_t size,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)310 int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
311 int (*f_rng)(void *, unsigned char *, size_t),
312 void *p_rng)
313 {
314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
315 size_t olen = 0;
316
317 if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
318 f_rng, p_rng)) < 0) {
319 return ret;
320 }
321
322 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
323 buf + size - ret,
324 ret, buf, size, &olen)) != 0) {
325 return ret;
326 }
327
328 return 0;
329 }
330 #endif /* MBEDTLS_PEM_WRITE_C */
331
332 #endif /* MBEDTLS_X509_CSR_WRITE_C */
333