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