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