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