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