blob: e65ddb07f49f23b41e3197fba4fac72f42852efc [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 Certificate Signing Request writing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006 */
7/*
8 * References:
9 * - CSRs: PKCS#10 v1.7 aka RFC 2986
10 * - attributes: PKCS#9 v2.0 aka RFC 2985
11 */
12
Harry Ramsey0f6bc412024-10-04 10:36:54 +010013#include "x509_internal.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015#if defined(MBEDTLS_X509_CSR_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020016
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/x509_csr.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000019#include "mbedtls/error.h"
20#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050021#include "mbedtls/platform_util.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020022
Andrzej Kurekd4a65532018-10-31 06:18:39 -040023#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020024#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010025#include "mbedtls/psa_util.h"
Andrzej Kurekd4a65532018-10-31 06:18:39 -040026
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
28#include <stdlib.h>
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032#endif
33
Doru Gucea2957b352018-12-14 21:08:35 +020034#include "mbedtls/platform.h"
Doru Gucea2957b352018-12-14 21:08:35 +020035
Gilles Peskine449bd832023-01-11 14:50:10 +010036void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037{
Gilles Peskine449bd832023-01-11 14:50:10 +010038 memset(ctx, 0, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039}
40
Gilles Peskine449bd832023-01-11 14:50:10 +010041void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042{
Troy-Butler9ac3e232024-03-22 14:46:04 -040043 if (ctx == NULL) {
44 return;
45 }
46
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_asn1_free_named_data_list(&ctx->subject);
48 mbedtls_asn1_free_named_data_list(&ctx->extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051}
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054{
55 ctx->md_alg = md_alg;
56}
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059{
60 ctx->key = key;
61}
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
64 const char *subject_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065{
Gilles Peskine449bd832023-01-11 14:50:10 +010066 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067}
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069int 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)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073{
Gilles Peskine449bd832023-01-11 14:50:10 +010074 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
75 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076}
77
Hannes Tschofenig6b108602022-12-28 18:38:53 +010078int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
79 const mbedtls_x509_san_list *san_list)
80{
Andrzej Kurekc508dc22023-07-07 08:20:02 -040081 return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list);
Hannes Tschofenig6b108602022-12-28 18:38:53 +010082}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085{
Gilles Peskine449bd832023-01-11 14:50:10 +010086 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +000088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020089
90 c = buf + 4;
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
93 if (ret < 3 || ret > 4) {
94 return ret;
95 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 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 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105}
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
108 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109{
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113
114 c = buf + 4;
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
117 if (ret < 3 || ret > 4) {
118 return ret;
119 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 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 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200129}
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
132 unsigned char *buf,
133 size_t size,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000134 unsigned char *sig, size_t sig_size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135{
Janos Follath865b3eb2019-12-16 11:46:15 +0000136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200137 const char *sig_oid;
138 size_t sig_oid_len = 0;
139 unsigned char *c, *c2;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +0200140 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141 size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
142 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_pk_type_t pk_alg;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400144 size_t hash_len;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200145 psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ctx->md_alg);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146
Simon Leet40ca54a2020-06-26 21:23:32 +0000147 /* Write the CSR backwards starting from the end of buf */
Doru Gucea2957b352018-12-14 21:08:35 +0200148 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
151 ctx->extensions));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (len) {
154 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
155 MBEDTLS_ASN1_CHK_ADD(len,
156 mbedtls_asn1_write_tag(
157 &c, buf,
158 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
161 MBEDTLS_ASN1_CHK_ADD(len,
162 mbedtls_asn1_write_tag(
163 &c, buf,
164 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 MBEDTLS_ASN1_CHK_ADD(len,
167 mbedtls_asn1_write_oid(
168 &c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
169 MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
172 MBEDTLS_ASN1_CHK_ADD(len,
173 mbedtls_asn1_write_tag(
174 &c, buf,
175 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176 }
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
179 MBEDTLS_ASN1_CHK_ADD(len,
180 mbedtls_asn1_write_tag(
181 &c, buf,
182 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000185 buf, (size_t) (c - buf)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186 c -= pub_len;
187 len += pub_len;
188
189 /*
190 * Subject ::= Name
191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
193 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194
195 /*
196 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
197 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
201 MBEDTLS_ASN1_CHK_ADD(len,
202 mbedtls_asn1_write_tag(
203 &c, buf,
204 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205
206 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000207 * Sign the written CSR data into the sig buffer
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400208 * Note: hash errors can happen only after an internal error
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (psa_hash_compute(hash_alg,
211 c,
212 len,
213 hash,
214 sizeof(hash),
215 &hash_len) != PSA_SUCCESS) {
216 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Andrzej Kurekd4a65532018-10-31 06:18:39 -0400217 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000219 sig, sig_size, &sig_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return ret;
Hanno Beckerfc771442017-09-13 08:45:48 +0100221 }
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100224 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 } else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100226 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 } else {
228 return MBEDTLS_ERR_X509_INVALID_ALG;
229 }
Hanno Beckerfc771442017-09-13 08:45:48 +0100230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
232 &sig_oid, &sig_oid_len)) != 0) {
233 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200234 }
235
236 /*
Simon Leet40ca54a2020-06-26 21:23:32 +0000237 * Move the written CSR data to the start of buf to create space for
238 * writing the signature into buf.
239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 memmove(buf, c, len);
Doru Gucea2957b352018-12-14 21:08:35 +0200241
Simon Leet40ca54a2020-06-26 21:23:32 +0000242 /*
243 * Write sig and its OID into buf backwards from the end of buf.
244 * Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
245 * and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246 */
247 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
249 mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100250 sig, sig_len, pk_alg));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251
Simon Leet40ca54a2020-06-26 21:23:32 +0000252 /*
253 * Compact the space between the CSR data and signature by moving the
254 * CSR data to the start of the signature.
255 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256 c2 -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 memmove(c2, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258
Simon Leet40ca54a2020-06-26 21:23:32 +0000259 /* ASN encode the total size and tag the CSR data with it. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
262 MBEDTLS_ASN1_CHK_ADD(len,
263 mbedtls_asn1_write_tag(
264 &c2, buf,
265 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Simon Leet40ca54a2020-06-26 21:23:32 +0000266
267 /* Zero the unused bytes at the start of buf */
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000268 memset(buf, 0, (size_t) (c2 - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271}
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000274 size_t size)
Doru Gucea2957b352018-12-14 21:08:35 +0200275{
276 int ret;
277 unsigned char *sig;
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
280 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Doru Gucea2957b352018-12-14 21:08:35 +0200281 }
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 ret = x509write_csr_der_internal(ctx, buf, size,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000284 sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE);
Doru Gucea2957b352018-12-14 21:08:35 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 mbedtls_free(sig);
Doru Gucea2957b352018-12-14 21:08:35 +0200287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return ret;
Doru Gucea2957b352018-12-14 21:08:35 +0200289}
290
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
292#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_PEM_WRITE_C)
Ben Taylor440cb2a2025-03-05 09:40:08 +0000295int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296{
Janos Follath865b3eb2019-12-16 11:46:15 +0000297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 size_t olen = 0;
299
Ben Taylor440cb2a2025-03-05 09:40:08 +0000300 if ((ret = mbedtls_x509write_csr_der(ctx, buf, size)) < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302 }
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
305 buf + size - ret,
306 ret, buf, size, &olen)) != 0) {
307 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308 }
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#endif /* MBEDTLS_X509_CSR_WRITE_C */