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