Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Certificate generation and signing |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 20 | #include "mbedtls/build_info.h" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 22 | #include "mbedtls/platform.h" |
Andrzej Kurek | 0af3248 | 2023-04-07 03:10:28 -0400 | [diff] [blame] | 23 | /* md.h is included this early since MD_CAN_XXX macros are defined there. */ |
Andrzej Kurek | 1b75e5f | 2023-04-04 09:55:06 -0400 | [diff] [blame] | 24 | #include "mbedtls/md.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 25 | |
Simon Butcher | 203a693 | 2016-10-07 15:00:17 +0100 | [diff] [blame] | 26 | #if !defined(MBEDTLS_X509_CRT_WRITE_C) || \ |
| 27 | !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \ |
| 28 | !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
Manuel Pégourié-Gonnard | 9330242 | 2023-03-21 17:23:08 +0100 | [diff] [blame] | 29 | !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \ |
Simon Butcher | 203a693 | 2016-10-07 15:00:17 +0100 | [diff] [blame] | 30 | !defined(MBEDTLS_PEM_WRITE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 31 | int main(void) |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 32 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or " |
Manuel Pégourié-Gonnard | 9330242 | 2023-03-21 17:23:08 +0100 | [diff] [blame] | 34 | "MBEDTLS_FS_IO and/or MBEDTLS_MD_CAN_SHA256 and/or " |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 35 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " |
| 36 | "MBEDTLS_ERROR_C not defined.\n"); |
| 37 | mbedtls_exit(0); |
Manuel Pégourié-Gonnard | 8d649c6 | 2015-03-31 15:10:03 +0200 | [diff] [blame] | 38 | } |
| 39 | #else |
| 40 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 41 | #include "mbedtls/x509_crt.h" |
| 42 | #include "mbedtls/x509_csr.h" |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 43 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 44 | #include "mbedtls/entropy.h" |
| 45 | #include "mbedtls/ctr_drbg.h" |
| 46 | #include "mbedtls/error.h" |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 47 | #include "test/helpers.h" |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 48 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 52 | #include <errno.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 53 | |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 54 | #define SET_OID(x, oid) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0) |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 58 | #define USAGE_CSR \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 59 | " request_file=%%s default: (empty)\n" \ |
| 60 | " If request_file is specified, subject_key,\n" \ |
| 61 | " subject_pwd and subject_name are ignored!\n" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 62 | #else |
| 63 | #define USAGE_CSR "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 65 | |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 66 | #define FORMAT_PEM 0 |
| 67 | #define FORMAT_DER 1 |
| 68 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 69 | #define DFL_ISSUER_CRT "" |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 70 | #define DFL_REQUEST_FILE "" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 71 | #define DFL_SUBJECT_KEY "subject.key" |
| 72 | #define DFL_ISSUER_KEY "ca.key" |
| 73 | #define DFL_SUBJECT_PWD "" |
| 74 | #define DFL_ISSUER_PWD "" |
| 75 | #define DFL_OUTPUT_FILENAME "cert.crt" |
Manuel Pégourié-Gonnard | 9169921 | 2015-01-22 16:26:39 +0000 | [diff] [blame] | 76 | #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK" |
| 77 | #define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK" |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 78 | #define DFL_NOT_BEFORE "20010101000000" |
| 79 | #define DFL_NOT_AFTER "20301231235959" |
| 80 | #define DFL_SERIAL "1" |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 81 | #define DFL_SERIAL_HEX "1" |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 82 | #define DFL_SELFSIGN 0 |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 83 | #define DFL_IS_CA 0 |
| 84 | #define DFL_MAX_PATHLEN -1 |
Nicholas Wilson | 99a96b1 | 2015-09-10 18:28:01 +0100 | [diff] [blame] | 85 | #define DFL_SIG_ALG MBEDTLS_MD_SHA256 |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 86 | #define DFL_KEY_USAGE 0 |
Dave Rodgman | 1577c54 | 2022-09-09 10:22:15 +0100 | [diff] [blame] | 87 | #define DFL_EXT_KEY_USAGE NULL |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 88 | #define DFL_NS_CERT_TYPE 0 |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 89 | #define DFL_VERSION 3 |
| 90 | #define DFL_AUTH_IDENT 1 |
| 91 | #define DFL_SUBJ_IDENT 1 |
| 92 | #define DFL_CONSTRAINTS 1 |
| 93 | #define DFL_DIGEST MBEDTLS_MD_SHA256 |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 94 | #define DFL_FORMAT FORMAT_PEM |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 95 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 96 | #define USAGE \ |
| 97 | "\n usage: cert_write param=<>...\n" \ |
| 98 | "\n acceptable parameters:\n" \ |
| 99 | USAGE_CSR \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 100 | " subject_key=%%s default: subject.key\n" \ |
| 101 | " subject_pwd=%%s default: (empty)\n" \ |
| 102 | " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 103 | "\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 104 | " issuer_crt=%%s default: (empty)\n" \ |
| 105 | " If issuer_crt is specified, issuer_name is\n" \ |
| 106 | " ignored!\n" \ |
| 107 | " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 108 | "\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 109 | " selfsign=%%d default: 0 (false)\n" \ |
| 110 | " If selfsign is enabled, issuer_name and\n" \ |
| 111 | " issuer_key are required (issuer_crt and\n" \ |
| 112 | " subject_* are ignored\n" \ |
| 113 | " issuer_key=%%s default: ca.key\n" \ |
| 114 | " issuer_pwd=%%s default: (empty)\n" \ |
| 115 | " output_file=%%s default: cert.crt\n" \ |
| 116 | " serial=%%s default: 1\n" \ |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 117 | " In decimal format; it can be used as\n" \ |
| 118 | " alternative to serial_hex, but it's\n" \ |
| 119 | " limited in max length to\n" \ |
| 120 | " unsigned long long int\n" \ |
| 121 | " serial_hex=%%s default: 1\n" \ |
| 122 | " In hex format; it can be used as\n" \ |
| 123 | " alternative to serial\n" \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | " not_before=%%s default: 20010101000000\n" \ |
| 125 | " not_after=%%s default: 20301231235959\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 126 | " is_ca=%%d default: 0 (disabled)\n" \ |
| 127 | " max_pathlen=%%d default: -1 (none)\n" \ |
| 128 | " md=%%s default: SHA256\n" \ |
Gilles Peskine | 18292fe | 2020-08-21 20:42:32 +0200 | [diff] [blame] | 129 | " Supported values (if enabled):\n" \ |
TRodziewicz | 10e8cf5 | 2021-05-31 17:58:57 +0200 | [diff] [blame] | 130 | " MD5, RIPEMD160, SHA1,\n" \ |
Gilles Peskine | 18292fe | 2020-08-21 20:42:32 +0200 | [diff] [blame] | 131 | " SHA224, SHA256, SHA384, SHA512\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 132 | " version=%%d default: 3\n" \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | " Possible values: 1, 2, 3\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 134 | " subject_identifier=%%s default: 1\n" \ |
| 135 | " Possible values: 0, 1\n" \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | " (Considered for v3 only)\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 137 | " authority_identifier=%%s default: 1\n" \ |
| 138 | " Possible values: 0, 1\n" \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | " (Considered for v3 only)\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 140 | " basic_constraints=%%d default: 1\n" \ |
| 141 | " Possible values: 0, 1\n" \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | " (Considered for v3 only)\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 143 | " key_usage=%%s default: (empty)\n" \ |
| 144 | " Comma-separated-list of values:\n" \ |
| 145 | " digital_signature\n" \ |
| 146 | " non_repudiation\n" \ |
| 147 | " key_encipherment\n" \ |
| 148 | " data_encipherment\n" \ |
| 149 | " key_agreement\n" \ |
| 150 | " key_cert_sign\n" \ |
| 151 | " crl_sign\n" \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | " (Considered for v3 only)\n" \ |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 153 | " ext_key_usage=%%s default: (empty)\n" \ |
| 154 | " Comma-separated-list of values:\n" \ |
| 155 | " serverAuth\n" \ |
| 156 | " clientAuth\n" \ |
| 157 | " codeSigning\n" \ |
| 158 | " emailProtection\n" \ |
| 159 | " timeStamping\n" \ |
| 160 | " OCSPSigning\n" \ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 161 | " ns_cert_type=%%s default: (empty)\n" \ |
| 162 | " Comma-separated-list of values:\n" \ |
| 163 | " ssl_client\n" \ |
| 164 | " ssl_server\n" \ |
| 165 | " email\n" \ |
| 166 | " object_signing\n" \ |
| 167 | " ssl_ca\n" \ |
| 168 | " email_ca\n" \ |
| 169 | " object_signing_ca\n" \ |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 170 | " format=pem|der default: pem\n" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 171 | "\n" |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 172 | |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 173 | typedef enum { |
| 174 | SERIAL_FRMT_UNSPEC, |
| 175 | SERIAL_FRMT_DEC, |
| 176 | SERIAL_FRMT_HEX |
| 177 | } serial_format_t; |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 178 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 179 | /* |
| 180 | * global options |
| 181 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | struct options { |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 183 | const char *issuer_crt; /* filename of the issuer certificate */ |
| 184 | const char *request_file; /* filename of the certificate request */ |
| 185 | const char *subject_key; /* filename of the subject key file */ |
| 186 | const char *issuer_key; /* filename of the issuer key file */ |
| 187 | const char *subject_pwd; /* password for the subject key file */ |
| 188 | const char *issuer_pwd; /* password for the issuer key file */ |
Hanno Becker | 25d882b | 2018-08-23 15:26:06 +0100 | [diff] [blame] | 189 | const char *output_file; /* where to store the constructed CRT */ |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 190 | const char *subject_name; /* subject name for certificate */ |
| 191 | const char *issuer_name; /* issuer name for certificate */ |
| 192 | const char *not_before; /* validity period not before */ |
| 193 | const char *not_after; /* validity period not after */ |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 194 | const char *serial; /* serial number string (decimal) */ |
| 195 | const char *serial_hex; /* serial number string (hex) */ |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 196 | int selfsign; /* selfsign the certificate */ |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 197 | int is_ca; /* is a CA certificate */ |
| 198 | int max_pathlen; /* maximum CA path length */ |
Hanno Becker | e1b1d0a | 2017-09-22 15:35:16 +0100 | [diff] [blame] | 199 | int authority_identifier; /* add authority identifier to CRT */ |
| 200 | int subject_identifier; /* add subject identifier to CRT */ |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 201 | int basic_constraints; /* add basic constraints ext to CRT */ |
| 202 | int version; /* CRT version */ |
| 203 | mbedtls_md_type_t md; /* Hash used for signing */ |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 204 | unsigned char key_usage; /* key usage flags */ |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 205 | mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */ |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 206 | unsigned char ns_cert_type; /* NS cert type */ |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 207 | int format; /* format */ |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 208 | } opt; |
| 209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | int write_certificate(mbedtls_x509write_cert *crt, const char *output_file, |
| 211 | int (*f_rng)(void *, unsigned char *, size_t), |
| 212 | void *p_rng) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 213 | { |
| 214 | int ret; |
| 215 | FILE *f; |
| 216 | unsigned char output_buf[4096]; |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 217 | unsigned char *output_start; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 218 | size_t len = 0; |
| 219 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | memset(output_buf, 0, 4096); |
| 221 | if (opt.format == FORMAT_DER) { |
| 222 | ret = mbedtls_x509write_crt_der(crt, output_buf, 4096, |
| 223 | f_rng, p_rng); |
| 224 | if (ret < 0) { |
| 225 | return ret; |
| 226 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 227 | |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 228 | len = ret; |
| 229 | output_start = output_buf + 4096 - len; |
| 230 | } else { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096, |
| 232 | f_rng, p_rng); |
| 233 | if (ret < 0) { |
| 234 | return ret; |
| 235 | } |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | len = strlen((char *) output_buf); |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 238 | output_start = output_buf; |
| 239 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | if ((f = fopen(output_file, "w")) == NULL) { |
| 242 | return -1; |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 243 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | if (fwrite(output_start, 1, len, f) != len) { |
| 246 | fclose(f); |
| 247 | return -1; |
| 248 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | fclose(f); |
| 251 | |
| 252 | return 0; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 255 | int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax, |
| 256 | const char *ibuf, size_t *len) |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 257 | { |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 258 | unsigned long long int dec; |
| 259 | unsigned int remaining_bytes = sizeof(dec); |
| 260 | unsigned char *p = obuf; |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 261 | unsigned char val; |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 262 | char *end_ptr = NULL; |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 263 | |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 264 | errno = 0; |
| 265 | dec = strtoull(ibuf, &end_ptr, 10); |
| 266 | |
| 267 | if ((errno != 0) || (end_ptr == ibuf)) { |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 268 | return -1; |
| 269 | } |
| 270 | |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 271 | *len = 0; |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 272 | |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 273 | while (remaining_bytes > 0) { |
| 274 | if (obufmax < (*len + 1)) { |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 275 | return -1; |
| 276 | } |
| 277 | |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 278 | val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF; |
| 279 | |
| 280 | /* Skip leading zeros */ |
Valerio Setti | 48fdbb3 | 2023-01-12 15:31:35 +0100 | [diff] [blame] | 281 | if ((val != 0) || (*len != 0)) { |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 282 | *p = val; |
| 283 | (*len)++; |
| 284 | p++; |
| 285 | } |
| 286 | |
| 287 | remaining_bytes--; |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | int main(int argc, char *argv[]) |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 294 | { |
Andres Amaya Garcia | f9a54d3 | 2018-04-29 21:42:45 +0100 | [diff] [blame] | 295 | int ret = 1; |
| 296 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | mbedtls_x509_crt issuer_crt; |
| 298 | mbedtls_pk_context loaded_issuer_key, loaded_subject_key; |
| 299 | mbedtls_pk_context *issuer_key = &loaded_issuer_key, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | *subject_key = &loaded_subject_key; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 301 | char buf[1024]; |
Jonathan Leroy | bbc75d9 | 2015-10-10 21:58:07 +0200 | [diff] [blame] | 302 | char issuer_name[256]; |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 303 | int i; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 304 | char *p, *q, *r; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Jonathan Leroy | bbc75d9 | 2015-10-10 21:58:07 +0200 | [diff] [blame] | 306 | char subject_name[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 307 | mbedtls_x509_csr csr; |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 308 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | mbedtls_x509write_cert crt; |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 310 | serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC; |
Valerio Setti | acf12fb | 2023-01-09 17:19:26 +0100 | [diff] [blame] | 311 | unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN]; |
| 312 | size_t serial_len; |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 313 | mbedtls_asn1_sequence *ext_key_usage; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | mbedtls_entropy_context entropy; |
| 315 | mbedtls_ctr_drbg_context ctr_drbg; |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 316 | const char *pers = "crt example app"; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * Set to sane values |
| 320 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | mbedtls_x509write_crt_init(&crt); |
| 322 | mbedtls_pk_init(&loaded_issuer_key); |
| 323 | mbedtls_pk_init(&loaded_subject_key); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 325 | mbedtls_entropy_init(&entropy); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | mbedtls_x509_csr_init(&csr); |
Paul Bakker | 7fc7fa6 | 2013-09-17 14:44:00 +0200 | [diff] [blame] | 328 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 329 | mbedtls_x509_crt_init(&issuer_crt); |
| 330 | memset(buf, 0, sizeof(buf)); |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 331 | memset(serial, 0, sizeof(serial)); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 332 | |
Przemek Stekiel | a0a1c1e | 2023-04-17 11:10:05 +0200 | [diff] [blame] | 333 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 334 | psa_status_t status = psa_crypto_init(); |
| 335 | if (status != PSA_SUCCESS) { |
| 336 | mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n", |
| 337 | (int) status); |
| 338 | goto exit; |
| 339 | } |
| 340 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 341 | |
Aditya Deshpande | 644a5c0 | 2023-01-30 15:58:50 +0000 | [diff] [blame] | 342 | if (argc < 2) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | usage: |
| 344 | mbedtls_printf(USAGE); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 345 | goto exit; |
| 346 | } |
| 347 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 348 | opt.issuer_crt = DFL_ISSUER_CRT; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 349 | opt.request_file = DFL_REQUEST_FILE; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 350 | opt.subject_key = DFL_SUBJECT_KEY; |
| 351 | opt.issuer_key = DFL_ISSUER_KEY; |
| 352 | opt.subject_pwd = DFL_SUBJECT_PWD; |
| 353 | opt.issuer_pwd = DFL_ISSUER_PWD; |
| 354 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 355 | opt.subject_name = DFL_SUBJECT_NAME; |
| 356 | opt.issuer_name = DFL_ISSUER_NAME; |
| 357 | opt.not_before = DFL_NOT_BEFORE; |
| 358 | opt.not_after = DFL_NOT_AFTER; |
| 359 | opt.serial = DFL_SERIAL; |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 360 | opt.serial_hex = DFL_SERIAL_HEX; |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 361 | opt.selfsign = DFL_SELFSIGN; |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 362 | opt.is_ca = DFL_IS_CA; |
| 363 | opt.max_pathlen = DFL_MAX_PATHLEN; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 364 | opt.key_usage = DFL_KEY_USAGE; |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 365 | opt.ext_key_usage = DFL_EXT_KEY_USAGE; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 366 | opt.ns_cert_type = DFL_NS_CERT_TYPE; |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame] | 367 | opt.version = DFL_VERSION - 1; |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 368 | opt.md = DFL_DIGEST; |
| 369 | opt.subject_identifier = DFL_SUBJ_IDENT; |
| 370 | opt.authority_identifier = DFL_AUTH_IDENT; |
| 371 | opt.basic_constraints = DFL_CONSTRAINTS; |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 372 | opt.format = DFL_FORMAT; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | for (i = 1; i < argc; i++) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 375 | |
| 376 | p = argv[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | if ((q = strchr(p, '=')) == NULL) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 378 | goto usage; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 380 | *q++ = '\0'; |
| 381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | if (strcmp(p, "request_file") == 0) { |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 383 | opt.request_file = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | } else if (strcmp(p, "subject_key") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 385 | opt.subject_key = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | } else if (strcmp(p, "issuer_key") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 387 | opt.issuer_key = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | } else if (strcmp(p, "subject_pwd") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 389 | opt.subject_pwd = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | } else if (strcmp(p, "issuer_pwd") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 391 | opt.issuer_pwd = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | } else if (strcmp(p, "issuer_crt") == 0) { |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 393 | opt.issuer_crt = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | } else if (strcmp(p, "output_file") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 395 | opt.output_file = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | } else if (strcmp(p, "subject_name") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 397 | opt.subject_name = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 398 | } else if (strcmp(p, "issuer_name") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 399 | opt.issuer_name = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | } else if (strcmp(p, "not_before") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 401 | opt.not_before = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | } else if (strcmp(p, "not_after") == 0) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 403 | opt.not_after = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | } else if (strcmp(p, "serial") == 0) { |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 405 | if (serial_frmt != SERIAL_FRMT_UNSPEC) { |
| 406 | mbedtls_printf("Invalid attempt to set the serial more than once\n"); |
| 407 | goto usage; |
| 408 | } |
| 409 | serial_frmt = SERIAL_FRMT_DEC; |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 410 | opt.serial = q; |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 411 | } else if (strcmp(p, "serial_hex") == 0) { |
| 412 | if (serial_frmt != SERIAL_FRMT_UNSPEC) { |
| 413 | mbedtls_printf("Invalid attempt to set the serial more than once\n"); |
| 414 | goto usage; |
| 415 | } |
| 416 | serial_frmt = SERIAL_FRMT_HEX; |
| 417 | opt.serial_hex = q; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | } else if (strcmp(p, "authority_identifier") == 0) { |
| 419 | opt.authority_identifier = atoi(q); |
| 420 | if (opt.authority_identifier != 0 && |
| 421 | opt.authority_identifier != 1) { |
| 422 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 423 | goto usage; |
| 424 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | } else if (strcmp(p, "subject_identifier") == 0) { |
| 426 | opt.subject_identifier = atoi(q); |
| 427 | if (opt.subject_identifier != 0 && |
| 428 | opt.subject_identifier != 1) { |
| 429 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 430 | goto usage; |
| 431 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | } else if (strcmp(p, "basic_constraints") == 0) { |
| 433 | opt.basic_constraints = atoi(q); |
| 434 | if (opt.basic_constraints != 0 && |
| 435 | opt.basic_constraints != 1) { |
| 436 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 437 | goto usage; |
| 438 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | } else if (strcmp(p, "md") == 0) { |
Gilles Peskine | 18292fe | 2020-08-21 20:42:32 +0200 | [diff] [blame] | 440 | const mbedtls_md_info_t *md_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | mbedtls_md_info_from_string(q); |
| 442 | if (md_info == NULL) { |
| 443 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 444 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 445 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | opt.md = mbedtls_md_get_type(md_info); |
| 447 | } else if (strcmp(p, "version") == 0) { |
| 448 | opt.version = atoi(q); |
| 449 | if (opt.version < 1 || opt.version > 3) { |
| 450 | mbedtls_printf("Invalid argument for option %s\n", p); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 451 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 452 | } |
Hanno Becker | 38eff43 | 2017-09-22 15:38:20 +0100 | [diff] [blame] | 453 | opt.version--; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | } else if (strcmp(p, "selfsign") == 0) { |
| 455 | opt.selfsign = atoi(q); |
| 456 | if (opt.selfsign < 0 || opt.selfsign > 1) { |
| 457 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 458 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 459 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | } else if (strcmp(p, "is_ca") == 0) { |
| 461 | opt.is_ca = atoi(q); |
| 462 | if (opt.is_ca < 0 || opt.is_ca > 1) { |
| 463 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 464 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 465 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | } else if (strcmp(p, "max_pathlen") == 0) { |
| 467 | opt.max_pathlen = atoi(q); |
| 468 | if (opt.max_pathlen < -1 || opt.max_pathlen > 127) { |
| 469 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 470 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 471 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | } else if (strcmp(p, "key_usage") == 0) { |
| 473 | while (q != NULL) { |
| 474 | if ((r = strchr(q, ',')) != NULL) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 475 | *r++ = '\0'; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 478 | if (strcmp(q, "digital_signature") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 480 | } else if (strcmp(q, "non_repudiation") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | } else if (strcmp(q, "key_encipherment") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 483 | opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | } else if (strcmp(q, "data_encipherment") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 485 | opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | } else if (strcmp(q, "key_agreement") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 487 | opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | } else if (strcmp(q, "key_cert_sign") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 489 | opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | } else if (strcmp(q, "crl_sign") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 491 | opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | } else { |
| 493 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 494 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 495 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 496 | |
| 497 | q = r; |
| 498 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 499 | } else if (strcmp(p, "ext_key_usage") == 0) { |
Dave Rodgman | 6493785 | 2022-08-15 14:12:25 +0100 | [diff] [blame] | 500 | mbedtls_asn1_sequence **tail = &opt.ext_key_usage; |
| 501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | while (q != NULL) { |
| 503 | if ((r = strchr(q, ',')) != NULL) { |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 504 | *r++ = '\0'; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | } |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence)); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 508 | ext_key_usage->buf.tag = MBEDTLS_ASN1_OID; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | if (strcmp(q, "serverAuth") == 0) { |
| 510 | SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH); |
| 511 | } else if (strcmp(q, "clientAuth") == 0) { |
| 512 | SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH); |
| 513 | } else if (strcmp(q, "codeSigning") == 0) { |
| 514 | SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING); |
| 515 | } else if (strcmp(q, "emailProtection") == 0) { |
| 516 | SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION); |
| 517 | } else if (strcmp(q, "timeStamping") == 0) { |
| 518 | SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING); |
| 519 | } else if (strcmp(q, "OCSPSigning") == 0) { |
| 520 | SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING); |
| 521 | } else { |
| 522 | mbedtls_printf("Invalid argument for option %s\n", p); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 523 | goto usage; |
Dave Rodgman | c5e0a8a | 2022-08-15 14:24:22 +0100 | [diff] [blame] | 524 | } |
Dave Rodgman | 6493785 | 2022-08-15 14:12:25 +0100 | [diff] [blame] | 525 | |
| 526 | *tail = ext_key_usage; |
| 527 | tail = &ext_key_usage->next; |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 528 | |
| 529 | q = r; |
| 530 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 531 | } else if (strcmp(p, "ns_cert_type") == 0) { |
| 532 | while (q != NULL) { |
| 533 | if ((r = strchr(q, ',')) != NULL) { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 534 | *r++ = '\0'; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | if (strcmp(q, "ssl_client") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | } else if (strcmp(q, "ssl_server") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 540 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | } else if (strcmp(q, "email") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | } else if (strcmp(q, "object_signing") == 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | } else if (strcmp(q, "ssl_ca") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 546 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | } else if (strcmp(q, "email_ca") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 548 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | } else if (strcmp(q, "object_signing_ca") == 0) { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 550 | opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 551 | } else { |
| 552 | mbedtls_printf("Invalid argument for option %s\n", p); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 553 | goto usage; |
Hanno Becker | 17c3276 | 2017-10-03 14:56:04 +0100 | [diff] [blame] | 554 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 555 | |
| 556 | q = r; |
| 557 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | } else if (strcmp(p, "format") == 0) { |
| 559 | if (strcmp(q, "der") == 0) { |
| 560 | opt.format = FORMAT_DER; |
| 561 | } else if (strcmp(q, "pem") == 0) { |
| 562 | opt.format = FORMAT_PEM; |
| 563 | } else { |
| 564 | mbedtls_printf("Invalid argument for option %s\n", p); |
Dave Rodgman | 66e0550 | 2022-10-27 16:29:38 +0100 | [diff] [blame] | 565 | goto usage; |
| 566 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | } else { |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 568 | goto usage; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 569 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | mbedtls_printf("\n"); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 573 | |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 574 | /* |
| 575 | * 0. Seed the PRNG |
| 576 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | mbedtls_printf(" . Seeding the random number generator..."); |
| 578 | fflush(stdout); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 580 | if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, |
| 581 | (const unsigned char *) pers, |
| 582 | strlen(pers))) != 0) { |
| 583 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 584 | mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n", |
| 585 | ret, buf); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 586 | goto exit; |
| 587 | } |
| 588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 590 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 591 | // Parse serial to MPI |
| 592 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | mbedtls_printf(" . Reading serial number..."); |
| 594 | fflush(stdout); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 595 | |
Valerio Setti | 791bbe6 | 2023-01-11 10:40:18 +0100 | [diff] [blame] | 596 | if (serial_frmt == SERIAL_FRMT_HEX) { |
| 597 | ret = mbedtls_test_unhexify(serial, sizeof(serial), |
| 598 | opt.serial_hex, &serial_len); |
| 599 | } else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC |
| 600 | ret = parse_serial_decimal_format(serial, sizeof(serial), |
| 601 | opt.serial, &serial_len); |
| 602 | } |
| 603 | |
| 604 | if (ret != 0) { |
| 605 | mbedtls_printf(" failed\n ! Unable to parse serial\n"); |
| 606 | goto exit; |
| 607 | } |
| 608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 609 | mbedtls_printf(" ok\n"); |
Manuel Pégourié-Gonnard | 31e5940 | 2013-09-12 05:59:05 +0200 | [diff] [blame] | 610 | |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 611 | // Parse issuer certificate if present |
| 612 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | if (!opt.selfsign && strlen(opt.issuer_crt)) { |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 614 | /* |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 615 | * 1.0.a. Load the certificates |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 616 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 617 | mbedtls_printf(" . Loading the issuer certificate ..."); |
| 618 | fflush(stdout); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 620 | if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) { |
| 621 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 622 | mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file " |
| 623 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 624 | goto exit; |
| 625 | } |
| 626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name), |
| 628 | &issuer_crt.subject); |
| 629 | if (ret < 0) { |
| 630 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 631 | mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets " |
| 632 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 633 | goto exit; |
| 634 | } |
| 635 | |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 636 | opt.issuer_name = issuer_name; |
| 637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | mbedtls_printf(" ok\n"); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 642 | // Parse certificate request if present |
| 643 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | if (!opt.selfsign && strlen(opt.request_file)) { |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 645 | /* |
| 646 | * 1.0.b. Load the CSR |
| 647 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 648 | mbedtls_printf(" . Loading the certificate request ..."); |
| 649 | fflush(stdout); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) { |
| 652 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 653 | mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file " |
| 654 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 655 | goto exit; |
| 656 | } |
| 657 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 658 | ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name), |
| 659 | &csr.subject); |
| 660 | if (ret < 0) { |
| 661 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 662 | mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets " |
| 663 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 664 | goto exit; |
| 665 | } |
| 666 | |
| 667 | opt.subject_name = subject_name; |
Gilles Peskine | 842edf4 | 2021-08-04 21:56:10 +0200 | [diff] [blame] | 668 | subject_key = &csr.pk; |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | mbedtls_printf(" ok\n"); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 671 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 672 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 673 | |
| 674 | /* |
| 675 | * 1.1. Load the keys |
| 676 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | if (!opt.selfsign && !strlen(opt.request_file)) { |
| 678 | mbedtls_printf(" . Loading the subject key ..."); |
| 679 | fflush(stdout); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 680 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 681 | ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key, |
| 682 | opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg); |
| 683 | if (ret != 0) { |
| 684 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 685 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile " |
| 686 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | e2673fb | 2013-09-09 15:52:07 +0200 | [diff] [blame] | 687 | goto exit; |
| 688 | } |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | mbedtls_printf(" ok\n"); |
Paul Bakker | 1014e95 | 2013-09-09 13:59:42 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | mbedtls_printf(" . Loading the issuer key ..."); |
| 694 | fflush(stdout); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 695 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key, |
| 697 | opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg); |
| 698 | if (ret != 0) { |
| 699 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 700 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile " |
| 701 | "returned -x%02x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 702 | goto exit; |
| 703 | } |
| 704 | |
| 705 | // Check if key and issuer certificate match |
| 706 | // |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 707 | if (strlen(opt.issuer_crt)) { |
| 708 | if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key, |
| 709 | mbedtls_ctr_drbg_random, &ctr_drbg) != 0) { |
| 710 | mbedtls_printf(" failed\n ! issuer_key does not match " |
| 711 | "issuer certificate\n\n"); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 712 | goto exit; |
| 713 | } |
| 714 | } |
| 715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 716 | mbedtls_printf(" ok\n"); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | if (opt.selfsign) { |
Paul Bakker | 93c6aa4 | 2013-10-28 22:28:09 +0100 | [diff] [blame] | 719 | opt.subject_name = opt.issuer_name; |
Manuel Pégourié-Gonnard | f38e71a | 2013-09-12 05:21:54 +0200 | [diff] [blame] | 720 | subject_key = issuer_key; |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 723 | mbedtls_x509write_crt_set_subject_key(&crt, subject_key); |
| 724 | mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key); |
Paul Bakker | b2d7f23 | 2013-09-09 16:24:18 +0200 | [diff] [blame] | 725 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 726 | /* |
| 727 | * 1.0. Check the names for validity |
| 728 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 729 | if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) { |
| 730 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 731 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name " |
| 732 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 733 | goto exit; |
| 734 | } |
| 735 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) { |
| 737 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 738 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name " |
| 739 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 740 | goto exit; |
| 741 | } |
| 742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 743 | mbedtls_printf(" . Setting certificate values ..."); |
| 744 | fflush(stdout); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 745 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 746 | mbedtls_x509write_crt_set_version(&crt, opt.version); |
| 747 | mbedtls_x509write_crt_set_md_alg(&crt, opt.md); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 748 | |
Valerio Setti | af4815c | 2023-01-26 17:43:09 +0100 | [diff] [blame] | 749 | ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len); |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 750 | if (ret != 0) { |
| 751 | mbedtls_strerror(ret, buf, sizeof(buf)); |
Valerio Setti | af4815c | 2023-01-26 17:43:09 +0100 | [diff] [blame] | 752 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw " |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 753 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
| 754 | goto exit; |
| 755 | } |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 757 | ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after); |
| 758 | if (ret != 0) { |
| 759 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 760 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity " |
| 761 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 762 | goto exit; |
| 763 | } |
| 764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 765 | mbedtls_printf(" ok\n"); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 767 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 768 | opt.basic_constraints != 0) { |
| 769 | mbedtls_printf(" . Adding the Basic Constraints extension ..."); |
| 770 | fflush(stdout); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca, |
| 773 | opt.max_pathlen); |
| 774 | if (ret != 0) { |
| 775 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 776 | mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints " |
| 777 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 778 | goto exit; |
| 779 | } |
| 780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 781 | mbedtls_printf(" ok\n"); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 782 | } |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 783 | |
Manuel Pégourié-Gonnard | 9330242 | 2023-03-21 17:23:08 +0100 | [diff] [blame] | 784 | #if defined(MBEDTLS_MD_CAN_SHA1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 786 | opt.subject_identifier != 0) { |
| 787 | mbedtls_printf(" . Adding the Subject Key Identifier ..."); |
| 788 | fflush(stdout); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt); |
| 791 | if (ret != 0) { |
| 792 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 793 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject" |
| 794 | "_key_identifier returned -0x%04x - %s\n\n", |
| 795 | (unsigned int) -ret, buf); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 796 | goto exit; |
| 797 | } |
| 798 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 799 | mbedtls_printf(" ok\n"); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 802 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 803 | opt.authority_identifier != 0) { |
| 804 | mbedtls_printf(" . Adding the Authority Key Identifier ..."); |
| 805 | fflush(stdout); |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt); |
| 808 | if (ret != 0) { |
| 809 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 810 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_" |
| 811 | "key_identifier returned -0x%04x - %s\n\n", |
| 812 | (unsigned int) -ret, buf); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 813 | goto exit; |
| 814 | } |
| 815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | mbedtls_printf(" ok\n"); |
Hanno Becker | 6c13d37 | 2017-09-13 12:49:22 +0100 | [diff] [blame] | 817 | } |
Manuel Pégourié-Gonnard | 9330242 | 2023-03-21 17:23:08 +0100 | [diff] [blame] | 818 | #endif /* MBEDTLS_MD_CAN_SHA1 */ |
Paul Bakker | 15162a0 | 2013-09-06 19:27:21 +0200 | [diff] [blame] | 819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 820 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 821 | opt.key_usage != 0) { |
| 822 | mbedtls_printf(" . Adding the Key Usage extension ..."); |
| 823 | fflush(stdout); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage); |
| 826 | if (ret != 0) { |
| 827 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 828 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage " |
| 829 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 830 | goto exit; |
| 831 | } |
| 832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | mbedtls_printf(" ok\n"); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 836 | if (opt.ext_key_usage) { |
| 837 | mbedtls_printf(" . Adding the Extended Key Usage extension ..."); |
| 838 | fflush(stdout); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 840 | ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage); |
| 841 | if (ret != 0) { |
| 842 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 843 | mbedtls_printf( |
| 844 | " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n", |
| 845 | (unsigned int) -ret, |
| 846 | buf); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 847 | goto exit; |
| 848 | } |
| 849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 850 | mbedtls_printf(" ok\n"); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 853 | if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && |
| 854 | opt.ns_cert_type != 0) { |
| 855 | mbedtls_printf(" . Adding the NS Cert Type extension ..."); |
| 856 | fflush(stdout); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 858 | ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type); |
| 859 | if (ret != 0) { |
| 860 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 861 | mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type " |
| 862 | "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 863 | goto exit; |
| 864 | } |
| 865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | mbedtls_printf(" ok\n"); |
Paul Bakker | 52be08c | 2013-09-09 12:37:54 +0200 | [diff] [blame] | 867 | } |
| 868 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 869 | /* |
Hanno Becker | 25d882b | 2018-08-23 15:26:06 +0100 | [diff] [blame] | 870 | * 1.2. Writing the certificate |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 871 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 872 | mbedtls_printf(" . Writing the certificate..."); |
| 873 | fflush(stdout); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 875 | if ((ret = write_certificate(&crt, opt.output_file, |
| 876 | mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { |
| 877 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 878 | mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n", |
| 879 | (unsigned int) -ret, buf); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 880 | goto exit; |
| 881 | } |
| 882 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | mbedtls_printf(" ok\n"); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 884 | |
Andres Amaya Garcia | f9a54d3 | 2018-04-29 21:42:45 +0100 | [diff] [blame] | 885 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 886 | |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 887 | exit: |
Hanno Becker | 30a9510 | 2018-10-05 09:49:33 +0100 | [diff] [blame] | 888 | #if defined(MBEDTLS_X509_CSR_PARSE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 889 | mbedtls_x509_csr_free(&csr); |
Hanno Becker | 30a9510 | 2018-10-05 09:49:33 +0100 | [diff] [blame] | 890 | #endif /* MBEDTLS_X509_CSR_PARSE_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 891 | mbedtls_x509_crt_free(&issuer_crt); |
| 892 | mbedtls_x509write_crt_free(&crt); |
| 893 | mbedtls_pk_free(&loaded_subject_key); |
| 894 | mbedtls_pk_free(&loaded_issuer_key); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 895 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 896 | mbedtls_entropy_free(&entropy); |
Przemek Stekiel | a8c560a | 2023-04-19 10:15:26 +0200 | [diff] [blame^] | 897 | mbedtls_psa_crypto_free(); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 898 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 899 | mbedtls_exit(exit_code); |
Paul Bakker | 9397dcb | 2013-09-06 09:55:26 +0200 | [diff] [blame] | 900 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | #endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C && |
| 902 | MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
Simon Butcher | 203a693 | 2016-10-07 15:00:17 +0100 | [diff] [blame] | 903 | MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */ |