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