2013-09-06 09:55:26 +02:00
|
|
|
/*
|
|
|
|
* Certificate generation and signing
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-09-06 09:55:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-09-06 09:55:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-06 09:55:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-09-06 09:55:26 +02:00
|
|
|
*/
|
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2023-04-07 09:10:28 +02:00
|
|
|
/* md.h is included this early since MD_CAN_XXX macros are defined there. */
|
2023-04-04 15:55:06 +02:00
|
|
|
#include "mbedtls/md.h"
|
2015-01-19 15:26:37 +01:00
|
|
|
|
2016-10-07 16:00:17 +02:00
|
|
|
#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
|
|
|
|
!defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
|
|
|
|
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
|
2023-03-21 17:23:08 +01:00
|
|
|
!defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
|
2016-10-07 16:00:17 +02:00
|
|
|
!defined(MBEDTLS_PEM_WRITE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2015-03-31 15:10:03 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
|
2023-03-21 17:23:08 +01:00
|
|
|
"MBEDTLS_FS_IO and/or MBEDTLS_MD_CAN_SHA256 and/or "
|
2023-01-11 14:50:10 +01:00
|
|
|
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
|
|
|
|
"MBEDTLS_ERROR_C not defined.\n");
|
|
|
|
mbedtls_exit(0);
|
2015-03-31 15:10:03 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/x509_csr.h"
|
2015-09-09 20:03:34 +02:00
|
|
|
#include "mbedtls/oid.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
|
|
|
#include "mbedtls/error.h"
|
2023-01-11 10:40:18 +01:00
|
|
|
#include "test/helpers.h"
|
2013-09-20 12:29:56 +02:00
|
|
|
|
2015-02-11 15:06:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-01-11 10:40:18 +01:00
|
|
|
#include <errno.h>
|
2015-02-11 15:06:19 +01:00
|
|
|
|
2015-09-09 20:03:34 +02:00
|
|
|
#define SET_OID(x, oid) \
|
2023-01-11 14:50:10 +01:00
|
|
|
do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0)
|
2015-09-09 20:03:34 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CSR_PARSE_C)
|
2015-02-11 15:06:19 +01:00
|
|
|
#define USAGE_CSR \
|
2017-09-13 16:39:59 +02:00
|
|
|
" request_file=%%s default: (empty)\n" \
|
|
|
|
" If request_file is specified, subject_key,\n" \
|
|
|
|
" subject_pwd and subject_name are ignored!\n"
|
2015-02-11 15:06:19 +01:00
|
|
|
#else
|
|
|
|
#define USAGE_CSR ""
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CSR_PARSE_C */
|
2015-02-11 15:06:19 +01:00
|
|
|
|
2022-10-27 17:29:38 +02:00
|
|
|
#define FORMAT_PEM 0
|
|
|
|
#define FORMAT_DER 1
|
|
|
|
|
2013-09-09 13:59:42 +02:00
|
|
|
#define DFL_ISSUER_CRT ""
|
2013-09-09 15:52:07 +02:00
|
|
|
#define DFL_REQUEST_FILE ""
|
2013-09-06 09:55:26 +02:00
|
|
|
#define DFL_SUBJECT_KEY "subject.key"
|
|
|
|
#define DFL_ISSUER_KEY "ca.key"
|
|
|
|
#define DFL_SUBJECT_PWD ""
|
|
|
|
#define DFL_ISSUER_PWD ""
|
|
|
|
#define DFL_OUTPUT_FILENAME "cert.crt"
|
2015-01-22 17:26:39 +01:00
|
|
|
#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
|
|
|
|
#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
|
2013-09-06 09:55:26 +02:00
|
|
|
#define DFL_NOT_BEFORE "20010101000000"
|
|
|
|
#define DFL_NOT_AFTER "20301231235959"
|
|
|
|
#define DFL_SERIAL "1"
|
2023-01-11 10:40:18 +01:00
|
|
|
#define DFL_SERIAL_HEX "1"
|
2023-04-01 16:38:30 +02:00
|
|
|
#define DFL_EXT_SUBJECTALTNAME ""
|
2013-09-09 16:24:18 +02:00
|
|
|
#define DFL_SELFSIGN 0
|
2013-09-06 19:27:21 +02:00
|
|
|
#define DFL_IS_CA 0
|
|
|
|
#define DFL_MAX_PATHLEN -1
|
2015-09-10 19:28:01 +02:00
|
|
|
#define DFL_SIG_ALG MBEDTLS_MD_SHA256
|
2013-09-06 09:55:26 +02:00
|
|
|
#define DFL_KEY_USAGE 0
|
2022-09-09 11:22:15 +02:00
|
|
|
#define DFL_EXT_KEY_USAGE NULL
|
2013-09-06 09:55:26 +02:00
|
|
|
#define DFL_NS_CERT_TYPE 0
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
#define DFL_VERSION 3
|
|
|
|
#define DFL_AUTH_IDENT 1
|
|
|
|
#define DFL_SUBJ_IDENT 1
|
|
|
|
#define DFL_CONSTRAINTS 1
|
|
|
|
#define DFL_DIGEST MBEDTLS_MD_SHA256
|
2022-10-27 17:29:38 +02:00
|
|
|
#define DFL_FORMAT FORMAT_PEM
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2015-02-11 15:06:19 +01:00
|
|
|
#define USAGE \
|
|
|
|
"\n usage: cert_write param=<>...\n" \
|
|
|
|
"\n acceptable parameters:\n" \
|
|
|
|
USAGE_CSR \
|
2017-09-13 16:39:59 +02:00
|
|
|
" subject_key=%%s default: subject.key\n" \
|
|
|
|
" subject_pwd=%%s default: (empty)\n" \
|
|
|
|
" subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
|
2015-02-11 15:06:19 +01:00
|
|
|
"\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" issuer_crt=%%s default: (empty)\n" \
|
|
|
|
" If issuer_crt is specified, issuer_name is\n" \
|
|
|
|
" ignored!\n" \
|
|
|
|
" issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
|
2015-02-11 15:06:19 +01:00
|
|
|
"\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" selfsign=%%d default: 0 (false)\n" \
|
|
|
|
" If selfsign is enabled, issuer_name and\n" \
|
|
|
|
" issuer_key are required (issuer_crt and\n" \
|
|
|
|
" subject_* are ignored\n" \
|
|
|
|
" issuer_key=%%s default: ca.key\n" \
|
|
|
|
" issuer_pwd=%%s default: (empty)\n" \
|
|
|
|
" output_file=%%s default: cert.crt\n" \
|
|
|
|
" serial=%%s default: 1\n" \
|
2023-01-11 10:40:18 +01:00
|
|
|
" In decimal format; it can be used as\n" \
|
|
|
|
" alternative to serial_hex, but it's\n" \
|
|
|
|
" limited in max length to\n" \
|
|
|
|
" unsigned long long int\n" \
|
|
|
|
" serial_hex=%%s default: 1\n" \
|
|
|
|
" In hex format; it can be used as\n" \
|
|
|
|
" alternative to serial\n" \
|
2023-01-11 14:50:10 +01:00
|
|
|
" not_before=%%s default: 20010101000000\n" \
|
|
|
|
" not_after=%%s default: 20301231235959\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" is_ca=%%d default: 0 (disabled)\n" \
|
|
|
|
" max_pathlen=%%d default: -1 (none)\n" \
|
|
|
|
" md=%%s default: SHA256\n" \
|
2020-08-21 20:42:32 +02:00
|
|
|
" Supported values (if enabled):\n" \
|
2021-05-31 17:58:57 +02:00
|
|
|
" MD5, RIPEMD160, SHA1,\n" \
|
2020-08-21 20:42:32 +02:00
|
|
|
" SHA224, SHA256, SHA384, SHA512\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" version=%%d default: 3\n" \
|
2023-01-11 14:50:10 +01:00
|
|
|
" Possible values: 1, 2, 3\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" subject_identifier=%%s default: 1\n" \
|
|
|
|
" Possible values: 0, 1\n" \
|
2023-01-11 14:50:10 +01:00
|
|
|
" (Considered for v3 only)\n" \
|
2023-04-01 16:38:30 +02:00
|
|
|
" san=%%s default: (none)\n" \
|
2023-04-25 00:39:53 +02:00
|
|
|
" Semicolon-separated-list of values:\n" \
|
2023-04-01 16:38:30 +02:00
|
|
|
" DNS:value\n" \
|
|
|
|
" URI:value\n" \
|
|
|
|
" RFC822:value\n" \
|
|
|
|
" IP:value (Only IPv4 is supported)\n" \
|
2023-04-25 08:21:07 +02:00
|
|
|
" DN:list of comma separated key=value pairs\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" authority_identifier=%%s default: 1\n" \
|
|
|
|
" Possible values: 0, 1\n" \
|
2023-01-11 14:50:10 +01:00
|
|
|
" (Considered for v3 only)\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" basic_constraints=%%d default: 1\n" \
|
|
|
|
" Possible values: 0, 1\n" \
|
2023-01-11 14:50:10 +01:00
|
|
|
" (Considered for v3 only)\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" key_usage=%%s default: (empty)\n" \
|
|
|
|
" Comma-separated-list of values:\n" \
|
|
|
|
" digital_signature\n" \
|
|
|
|
" non_repudiation\n" \
|
|
|
|
" key_encipherment\n" \
|
|
|
|
" data_encipherment\n" \
|
|
|
|
" key_agreement\n" \
|
|
|
|
" key_cert_sign\n" \
|
|
|
|
" crl_sign\n" \
|
2023-01-11 14:50:10 +01:00
|
|
|
" (Considered for v3 only)\n" \
|
2015-09-09 20:03:34 +02:00
|
|
|
" ext_key_usage=%%s default: (empty)\n" \
|
|
|
|
" Comma-separated-list of values:\n" \
|
|
|
|
" serverAuth\n" \
|
|
|
|
" clientAuth\n" \
|
|
|
|
" codeSigning\n" \
|
|
|
|
" emailProtection\n" \
|
|
|
|
" timeStamping\n" \
|
|
|
|
" OCSPSigning\n" \
|
2017-09-13 16:39:59 +02:00
|
|
|
" ns_cert_type=%%s default: (empty)\n" \
|
|
|
|
" Comma-separated-list of values:\n" \
|
|
|
|
" ssl_client\n" \
|
|
|
|
" ssl_server\n" \
|
|
|
|
" email\n" \
|
|
|
|
" object_signing\n" \
|
|
|
|
" ssl_ca\n" \
|
|
|
|
" email_ca\n" \
|
|
|
|
" object_signing_ca\n" \
|
2022-10-27 17:29:38 +02:00
|
|
|
" format=pem|der default: pem\n" \
|
2015-02-11 15:06:19 +01:00
|
|
|
"\n"
|
2015-02-13 15:12:07 +01:00
|
|
|
|
2023-01-11 10:40:18 +01:00
|
|
|
typedef enum {
|
|
|
|
SERIAL_FRMT_UNSPEC,
|
|
|
|
SERIAL_FRMT_DEC,
|
|
|
|
SERIAL_FRMT_HEX
|
|
|
|
} serial_format_t;
|
2018-12-06 18:43:31 +01:00
|
|
|
|
2013-09-06 09:55:26 +02:00
|
|
|
/*
|
|
|
|
* global options
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
struct options {
|
2013-11-25 13:29:43 +01:00
|
|
|
const char *issuer_crt; /* filename of the issuer certificate */
|
|
|
|
const char *request_file; /* filename of the certificate request */
|
|
|
|
const char *subject_key; /* filename of the subject key file */
|
|
|
|
const char *issuer_key; /* filename of the issuer key file */
|
|
|
|
const char *subject_pwd; /* password for the subject key file */
|
|
|
|
const char *issuer_pwd; /* password for the issuer key file */
|
2018-08-23 16:26:06 +02:00
|
|
|
const char *output_file; /* where to store the constructed CRT */
|
2013-11-25 13:29:43 +01:00
|
|
|
const char *subject_name; /* subject name for certificate */
|
2023-04-01 16:38:30 +02:00
|
|
|
mbedtls_x509_san_list *san_list; /* subjectAltName for certificate */
|
2013-11-25 13:29:43 +01:00
|
|
|
const char *issuer_name; /* issuer name for certificate */
|
|
|
|
const char *not_before; /* validity period not before */
|
|
|
|
const char *not_after; /* validity period not after */
|
2023-01-11 10:40:18 +01:00
|
|
|
const char *serial; /* serial number string (decimal) */
|
|
|
|
const char *serial_hex; /* serial number string (hex) */
|
2013-09-09 16:24:18 +02:00
|
|
|
int selfsign; /* selfsign the certificate */
|
2013-09-06 19:27:21 +02:00
|
|
|
int is_ca; /* is a CA certificate */
|
|
|
|
int max_pathlen; /* maximum CA path length */
|
2017-09-22 16:35:16 +02:00
|
|
|
int authority_identifier; /* add authority identifier to CRT */
|
|
|
|
int subject_identifier; /* add subject identifier to CRT */
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
int basic_constraints; /* add basic constraints ext to CRT */
|
|
|
|
int version; /* CRT version */
|
|
|
|
mbedtls_md_type_t md; /* Hash used for signing */
|
2013-09-06 09:55:26 +02:00
|
|
|
unsigned char key_usage; /* key usage flags */
|
2015-09-09 20:03:34 +02:00
|
|
|
mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
|
2013-09-06 09:55:26 +02:00
|
|
|
unsigned char ns_cert_type; /* NS cert type */
|
2022-10-27 17:29:38 +02:00
|
|
|
int format; /* format */
|
2013-09-06 09:55:26 +02:00
|
|
|
} opt;
|
|
|
|
|
2023-04-05 17:19:30 +02:00
|
|
|
static void ip_string_to_bytes(const char *str, uint8_t *bytes, int maxBytes)
|
2023-04-01 16:38:30 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < maxBytes; i++) {
|
2023-04-25 00:39:53 +02:00
|
|
|
bytes[i] = (uint8_t) strtoul(str, NULL, 10);
|
2023-04-01 16:38:30 +02:00
|
|
|
str = strchr(str, '.');
|
|
|
|
if (str == NULL || *str == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2013-09-06 09:55:26 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
FILE *f;
|
|
|
|
unsigned char output_buf[4096];
|
2022-10-27 17:29:38 +02:00
|
|
|
unsigned char *output_start;
|
2013-09-06 09:55:26 +02:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(output_buf, 0, 4096);
|
|
|
|
if (opt.format == FORMAT_DER) {
|
|
|
|
ret = mbedtls_x509write_crt_der(crt, output_buf, 4096,
|
|
|
|
f_rng, p_rng);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2022-10-27 17:29:38 +02:00
|
|
|
len = ret;
|
|
|
|
output_start = output_buf + 4096 - len;
|
|
|
|
} else {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
|
|
|
|
f_rng, p_rng);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-10-27 17:29:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
len = strlen((char *) output_buf);
|
2022-10-27 17:29:38 +02:00
|
|
|
output_start = output_buf;
|
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((f = fopen(output_file, "w")) == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fwrite(output_start, 1, len, f) != len) {
|
|
|
|
fclose(f);
|
|
|
|
return -1;
|
2014-04-17 16:02:36 +02:00
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
fclose(f);
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-06 09:55:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 10:40:18 +01:00
|
|
|
int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
|
|
|
|
const char *ibuf, size_t *len)
|
2023-01-09 17:19:26 +01:00
|
|
|
{
|
2023-01-11 10:40:18 +01:00
|
|
|
unsigned long long int dec;
|
|
|
|
unsigned int remaining_bytes = sizeof(dec);
|
|
|
|
unsigned char *p = obuf;
|
2023-01-09 17:19:26 +01:00
|
|
|
unsigned char val;
|
2023-01-11 10:40:18 +01:00
|
|
|
char *end_ptr = NULL;
|
2023-01-09 17:19:26 +01:00
|
|
|
|
2023-01-11 10:40:18 +01:00
|
|
|
errno = 0;
|
|
|
|
dec = strtoull(ibuf, &end_ptr, 10);
|
|
|
|
|
|
|
|
if ((errno != 0) || (end_ptr == ibuf)) {
|
2023-01-09 17:19:26 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-01-11 10:40:18 +01:00
|
|
|
*len = 0;
|
2023-01-09 17:19:26 +01:00
|
|
|
|
2023-01-11 10:40:18 +01:00
|
|
|
while (remaining_bytes > 0) {
|
|
|
|
if (obufmax < (*len + 1)) {
|
2023-01-09 17:19:26 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-01-11 10:40:18 +01:00
|
|
|
val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
|
|
|
|
|
|
|
|
/* Skip leading zeros */
|
2023-01-12 15:31:35 +01:00
|
|
|
if ((val != 0) || (*len != 0)) {
|
2023-01-11 10:40:18 +01:00
|
|
|
*p = val;
|
|
|
|
(*len)++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining_bytes--;
|
2023-01-09 17:19:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(int argc, char *argv[])
|
2013-09-06 09:55:26 +02:00
|
|
|
{
|
2018-04-29 22:42:45 +02:00
|
|
|
int ret = 1;
|
|
|
|
int exit_code = MBEDTLS_EXIT_FAILURE;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt issuer_crt;
|
|
|
|
mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
|
|
|
|
mbedtls_pk_context *issuer_key = &loaded_issuer_key,
|
2023-01-11 14:50:10 +01:00
|
|
|
*subject_key = &loaded_subject_key;
|
2013-09-06 09:55:26 +02:00
|
|
|
char buf[1024];
|
2015-10-10 21:58:07 +02:00
|
|
|
char issuer_name[256];
|
2013-11-30 15:13:02 +01:00
|
|
|
int i;
|
2023-04-27 13:50:56 +02:00
|
|
|
char *p, *q, *r;
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CSR_PARSE_C)
|
2015-10-10 21:58:07 +02:00
|
|
|
char subject_name[256];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_csr csr;
|
2013-09-17 14:44:00 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509write_cert crt;
|
2023-01-11 10:40:18 +01:00
|
|
|
serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC;
|
2023-01-09 17:19:26 +01:00
|
|
|
unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
|
|
|
|
size_t serial_len;
|
2015-09-09 20:03:34 +02:00
|
|
|
mbedtls_asn1_sequence *ext_key_usage;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
2013-09-12 05:59:05 +02:00
|
|
|
const char *pers = "crt example app";
|
2023-04-01 16:38:30 +02:00
|
|
|
mbedtls_x509_san_list *cur, *prev;
|
2023-04-05 14:30:59 +02:00
|
|
|
mbedtls_asn1_named_data *ext_san_dirname = NULL;
|
2023-04-01 16:38:30 +02:00
|
|
|
uint8_t ip[4] = { 0 };
|
2013-09-06 09:55:26 +02:00
|
|
|
/*
|
|
|
|
* Set to sane values
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509write_crt_init(&crt);
|
|
|
|
mbedtls_pk_init(&loaded_issuer_key);
|
|
|
|
mbedtls_pk_init(&loaded_subject_key);
|
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
|
|
|
mbedtls_entropy_init(&entropy);
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CSR_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_csr_init(&csr);
|
2013-09-17 14:44:00 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&issuer_crt);
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2023-01-11 10:40:18 +01:00
|
|
|
memset(serial, 0, sizeof(serial));
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-04-17 11:10:05 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status = psa_crypto_init();
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
|
|
|
|
(int) status);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-01-30 16:58:50 +01:00
|
|
|
if (argc < 2) {
|
2023-01-11 14:50:10 +01:00
|
|
|
usage:
|
|
|
|
mbedtls_printf(USAGE);
|
2013-09-06 09:55:26 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2013-09-09 13:59:42 +02:00
|
|
|
opt.issuer_crt = DFL_ISSUER_CRT;
|
2013-09-09 15:52:07 +02:00
|
|
|
opt.request_file = DFL_REQUEST_FILE;
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.subject_key = DFL_SUBJECT_KEY;
|
|
|
|
opt.issuer_key = DFL_ISSUER_KEY;
|
|
|
|
opt.subject_pwd = DFL_SUBJECT_PWD;
|
|
|
|
opt.issuer_pwd = DFL_ISSUER_PWD;
|
|
|
|
opt.output_file = DFL_OUTPUT_FILENAME;
|
|
|
|
opt.subject_name = DFL_SUBJECT_NAME;
|
|
|
|
opt.issuer_name = DFL_ISSUER_NAME;
|
|
|
|
opt.not_before = DFL_NOT_BEFORE;
|
|
|
|
opt.not_after = DFL_NOT_AFTER;
|
|
|
|
opt.serial = DFL_SERIAL;
|
2023-01-11 10:40:18 +01:00
|
|
|
opt.serial_hex = DFL_SERIAL_HEX;
|
2013-09-09 16:24:18 +02:00
|
|
|
opt.selfsign = DFL_SELFSIGN;
|
2013-09-06 19:27:21 +02:00
|
|
|
opt.is_ca = DFL_IS_CA;
|
|
|
|
opt.max_pathlen = DFL_MAX_PATHLEN;
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.key_usage = DFL_KEY_USAGE;
|
2015-09-09 20:03:34 +02:00
|
|
|
opt.ext_key_usage = DFL_EXT_KEY_USAGE;
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.ns_cert_type = DFL_NS_CERT_TYPE;
|
2017-09-22 16:38:20 +02:00
|
|
|
opt.version = DFL_VERSION - 1;
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
opt.md = DFL_DIGEST;
|
|
|
|
opt.subject_identifier = DFL_SUBJ_IDENT;
|
|
|
|
opt.authority_identifier = DFL_AUTH_IDENT;
|
|
|
|
opt.basic_constraints = DFL_CONSTRAINTS;
|
2022-10-27 17:29:38 +02:00
|
|
|
opt.format = DFL_FORMAT;
|
2023-04-01 16:38:30 +02:00
|
|
|
opt.san_list = NULL;
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 1; i < argc; i++) {
|
2013-09-06 09:55:26 +02:00
|
|
|
|
|
|
|
p = argv[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((q = strchr(p, '=')) == NULL) {
|
2013-09-06 09:55:26 +02:00
|
|
|
goto usage;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
*q++ = '\0';
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(p, "request_file") == 0) {
|
2013-09-09 15:52:07 +02:00
|
|
|
opt.request_file = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "subject_key") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.subject_key = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "issuer_key") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.issuer_key = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "subject_pwd") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.subject_pwd = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "issuer_pwd") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.issuer_pwd = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "issuer_crt") == 0) {
|
2013-09-09 13:59:42 +02:00
|
|
|
opt.issuer_crt = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "output_file") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.output_file = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "subject_name") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.subject_name = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "issuer_name") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.issuer_name = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "not_before") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.not_before = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "not_after") == 0) {
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.not_after = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "serial") == 0) {
|
2023-01-11 10:40:18 +01:00
|
|
|
if (serial_frmt != SERIAL_FRMT_UNSPEC) {
|
|
|
|
mbedtls_printf("Invalid attempt to set the serial more than once\n");
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
serial_frmt = SERIAL_FRMT_DEC;
|
2013-09-06 09:55:26 +02:00
|
|
|
opt.serial = q;
|
2023-01-11 10:40:18 +01:00
|
|
|
} else if (strcmp(p, "serial_hex") == 0) {
|
|
|
|
if (serial_frmt != SERIAL_FRMT_UNSPEC) {
|
|
|
|
mbedtls_printf("Invalid attempt to set the serial more than once\n");
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
serial_frmt = SERIAL_FRMT_HEX;
|
|
|
|
opt.serial_hex = q;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "authority_identifier") == 0) {
|
|
|
|
opt.authority_identifier = atoi(q);
|
|
|
|
if (opt.authority_identifier != 0 &&
|
|
|
|
opt.authority_identifier != 1) {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
goto usage;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "subject_identifier") == 0) {
|
|
|
|
opt.subject_identifier = atoi(q);
|
|
|
|
if (opt.subject_identifier != 0 &&
|
|
|
|
opt.subject_identifier != 1) {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
goto usage;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "basic_constraints") == 0) {
|
|
|
|
opt.basic_constraints = atoi(q);
|
|
|
|
if (opt.basic_constraints != 0 &&
|
|
|
|
opt.basic_constraints != 1) {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
goto usage;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "md") == 0) {
|
2020-08-21 20:42:32 +02:00
|
|
|
const mbedtls_md_info_t *md_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_info_from_string(q);
|
|
|
|
if (md_info == NULL) {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
goto usage;
|
2017-10-03 15:56:04 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
opt.md = mbedtls_md_get_type(md_info);
|
|
|
|
} else if (strcmp(p, "version") == 0) {
|
|
|
|
opt.version = atoi(q);
|
|
|
|
if (opt.version < 1 || opt.version > 3) {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
goto usage;
|
2017-10-03 15:56:04 +02:00
|
|
|
}
|
2017-09-22 16:38:20 +02:00
|
|
|
opt.version--;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "selfsign") == 0) {
|
|
|
|
opt.selfsign = atoi(q);
|
|
|
|
if (opt.selfsign < 0 || opt.selfsign > 1) {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
2013-09-09 16:24:18 +02:00
|
|
|
goto usage;
|
2017-10-03 15:56:04 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "is_ca") == 0) {
|
|
|
|
opt.is_ca = atoi(q);
|
|
|
|
if (opt.is_ca < 0 || opt.is_ca > 1) {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
2013-09-06 19:27:21 +02:00
|
|
|
goto usage;
|
2017-10-03 15:56:04 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "max_pathlen") == 0) {
|
|
|
|
opt.max_pathlen = atoi(q);
|
|
|
|
if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
2013-09-06 19:27:21 +02:00
|
|
|
goto usage;
|
2017-10-03 15:56:04 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "key_usage") == 0) {
|
|
|
|
while (q != NULL) {
|
|
|
|
if ((r = strchr(q, ',')) != NULL) {
|
2013-09-06 09:55:26 +02:00
|
|
|
*r++ = '\0';
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(q, "digital_signature") == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "non_repudiation") == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "key_encipherment") == 0) {
|
2015-04-20 13:19:02 +02:00
|
|
|
opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "data_encipherment") == 0) {
|
2015-04-20 13:19:02 +02:00
|
|
|
opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "key_agreement") == 0) {
|
2015-04-20 13:19:02 +02:00
|
|
|
opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "key_cert_sign") == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "crl_sign") == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
2013-09-06 09:55:26 +02:00
|
|
|
goto usage;
|
2017-10-03 15:56:04 +02:00
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
|
|
|
q = r;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "ext_key_usage") == 0) {
|
2022-08-15 15:12:25 +02:00
|
|
|
mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (q != NULL) {
|
|
|
|
if ((r = strchr(q, ',')) != NULL) {
|
2015-09-09 20:03:34 +02:00
|
|
|
*r++ = '\0';
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-09-09 20:03:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
|
2015-09-09 20:03:34 +02:00
|
|
|
ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(q, "serverAuth") == 0) {
|
|
|
|
SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
|
|
|
|
} else if (strcmp(q, "clientAuth") == 0) {
|
|
|
|
SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
|
|
|
|
} else if (strcmp(q, "codeSigning") == 0) {
|
|
|
|
SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
|
|
|
|
} else if (strcmp(q, "emailProtection") == 0) {
|
|
|
|
SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
|
|
|
|
} else if (strcmp(q, "timeStamping") == 0) {
|
|
|
|
SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
|
|
|
|
} else if (strcmp(q, "OCSPSigning") == 0) {
|
|
|
|
SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
|
|
|
|
} else {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
2015-09-09 20:03:34 +02:00
|
|
|
goto usage;
|
2022-08-15 15:24:22 +02:00
|
|
|
}
|
2022-08-15 15:12:25 +02:00
|
|
|
|
|
|
|
*tail = ext_key_usage;
|
|
|
|
tail = &ext_key_usage->next;
|
2015-09-09 20:03:34 +02:00
|
|
|
|
2023-04-01 16:38:30 +02:00
|
|
|
q = r;
|
|
|
|
}
|
|
|
|
} else if (strcmp(p, "san") == 0) {
|
2023-04-27 13:50:56 +02:00
|
|
|
char *subtype_value;
|
2023-04-01 16:38:30 +02:00
|
|
|
prev = NULL;
|
|
|
|
|
|
|
|
while (q != NULL) {
|
2023-04-27 13:50:56 +02:00
|
|
|
char *semicolon;
|
|
|
|
r = q;
|
|
|
|
|
|
|
|
/* Find the first non-escaped ; occurrence and remove escaped ones */
|
|
|
|
do {
|
|
|
|
if ((semicolon = strchr(r, ';')) != NULL) {
|
|
|
|
if (*(semicolon-1) != '\\') {
|
|
|
|
r = semicolon;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Remove the escape character */
|
|
|
|
size_t size_left = strlen(semicolon);
|
|
|
|
memmove(semicolon-1, semicolon, size_left);
|
|
|
|
*(semicolon + size_left - 1) = '\0';
|
|
|
|
/* r will now point at the character after the semicolon */
|
|
|
|
r = semicolon;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (semicolon != NULL);
|
|
|
|
|
|
|
|
if (semicolon != NULL) {
|
2023-04-01 16:38:30 +02:00
|
|
|
*r++ = '\0';
|
2023-04-27 13:50:56 +02:00
|
|
|
} else {
|
|
|
|
r = NULL;
|
2023-04-01 16:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
|
|
|
|
if (cur == NULL) {
|
|
|
|
mbedtls_printf("Not enough memory for subjectAltName list\n");
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->next = NULL;
|
|
|
|
|
2023-04-27 13:50:56 +02:00
|
|
|
if ((subtype_value = strchr(q, ':')) != NULL) {
|
|
|
|
*subtype_value++ = '\0';
|
2023-04-01 16:38:30 +02:00
|
|
|
}
|
|
|
|
if (strcmp(q, "RFC822") == 0) {
|
|
|
|
cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
|
|
|
|
} else if (strcmp(q, "URI") == 0) {
|
|
|
|
cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
|
|
|
|
} else if (strcmp(q, "DNS") == 0) {
|
|
|
|
cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
|
|
|
|
} else if (strcmp(q, "IP") == 0) {
|
|
|
|
cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
|
2023-04-27 13:50:56 +02:00
|
|
|
ip_string_to_bytes(subtype_value, ip, 4);
|
2023-04-01 16:38:30 +02:00
|
|
|
cur->node.san.unstructured_name.p = (unsigned char *) ip;
|
|
|
|
cur->node.san.unstructured_name.len = sizeof(ip);
|
|
|
|
} else if (strcmp(q, "DN") == 0) {
|
|
|
|
cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
|
|
|
|
if ((ret = mbedtls_x509_string_to_names(&ext_san_dirname,
|
2023-04-27 13:50:56 +02:00
|
|
|
subtype_value)) != 0) {
|
2023-04-01 16:38:30 +02:00
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(
|
|
|
|
" failed\n ! mbedtls_x509_string_to_names "
|
|
|
|
"returned -0x%04x - %s\n\n",
|
|
|
|
(unsigned int) -ret, buf);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
cur->node.san.directory_name = *ext_san_dirname;
|
|
|
|
} else {
|
|
|
|
mbedtls_free(cur);
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
|
2023-06-02 11:10:17 +02:00
|
|
|
if (cur->node.type == MBEDTLS_X509_SAN_RFC822_NAME ||
|
|
|
|
cur->node.type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER ||
|
|
|
|
cur->node.type == MBEDTLS_X509_SAN_DNS_NAME) {
|
2023-04-27 13:50:56 +02:00
|
|
|
cur->node.san.unstructured_name.p = (unsigned char *) subtype_value;
|
|
|
|
cur->node.san.unstructured_name.len = strlen(subtype_value);
|
2023-04-01 16:38:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prev == NULL) {
|
|
|
|
opt.san_list = cur;
|
|
|
|
} else {
|
|
|
|
prev->next = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = cur;
|
2015-09-09 20:03:34 +02:00
|
|
|
q = r;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "ns_cert_type") == 0) {
|
|
|
|
while (q != NULL) {
|
|
|
|
if ((r = strchr(q, ',')) != NULL) {
|
2013-09-06 09:55:26 +02:00
|
|
|
*r++ = '\0';
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(q, "ssl_client") == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "ssl_server") == 0) {
|
2015-04-20 13:19:02 +02:00
|
|
|
opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "email") == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "object_signing") == 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "ssl_ca") == 0) {
|
2015-04-20 13:19:02 +02:00
|
|
|
opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "email_ca") == 0) {
|
2015-04-20 13:19:02 +02:00
|
|
|
opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(q, "object_signing_ca") == 0) {
|
2015-04-20 13:19:02 +02:00
|
|
|
opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
2013-09-06 09:55:26 +02:00
|
|
|
goto usage;
|
2017-10-03 15:56:04 +02:00
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
|
|
|
q = r;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (strcmp(p, "format") == 0) {
|
|
|
|
if (strcmp(q, "der") == 0) {
|
|
|
|
opt.format = FORMAT_DER;
|
|
|
|
} else if (strcmp(q, "pem") == 0) {
|
|
|
|
opt.format = FORMAT_PEM;
|
|
|
|
} else {
|
|
|
|
mbedtls_printf("Invalid argument for option %s\n", p);
|
2022-10-27 17:29:38 +02:00
|
|
|
goto usage;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2013-09-06 09:55:26 +02:00
|
|
|
goto usage;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf("\n");
|
2013-09-09 13:59:42 +02:00
|
|
|
|
2013-09-12 05:59:05 +02:00
|
|
|
/*
|
|
|
|
* 0. Seed the PRNG
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" . Seeding the random number generator...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
|
|
|
|
(const unsigned char *) pers,
|
|
|
|
strlen(pers))) != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
|
|
|
|
ret, buf);
|
2013-09-12 05:59:05 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-12 05:59:05 +02:00
|
|
|
|
2013-09-06 09:55:26 +02:00
|
|
|
// Parse serial to MPI
|
|
|
|
//
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" . Reading serial number...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
2023-01-11 10:40:18 +01:00
|
|
|
if (serial_frmt == SERIAL_FRMT_HEX) {
|
|
|
|
ret = mbedtls_test_unhexify(serial, sizeof(serial),
|
|
|
|
opt.serial_hex, &serial_len);
|
|
|
|
} else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC
|
|
|
|
ret = parse_serial_decimal_format(serial, sizeof(serial),
|
|
|
|
opt.serial, &serial_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_printf(" failed\n ! Unable to parse serial\n");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-12 05:59:05 +02:00
|
|
|
|
2013-09-09 13:59:42 +02:00
|
|
|
// Parse issuer certificate if present
|
|
|
|
//
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!opt.selfsign && strlen(opt.issuer_crt)) {
|
2013-09-09 13:59:42 +02:00
|
|
|
/*
|
2013-09-09 15:52:07 +02:00
|
|
|
* 1.0.a. Load the certificates
|
2013-09-09 13:59:42 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" . Loading the issuer certificate ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 13:59:42 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
|
|
|
|
&issuer_crt.subject);
|
|
|
|
if (ret < 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 13:59:42 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2013-09-09 15:52:07 +02:00
|
|
|
opt.issuer_name = issuer_name;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-09 15:52:07 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CSR_PARSE_C)
|
2013-09-09 15:52:07 +02:00
|
|
|
// Parse certificate request if present
|
|
|
|
//
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!opt.selfsign && strlen(opt.request_file)) {
|
2013-09-09 15:52:07 +02:00
|
|
|
/*
|
|
|
|
* 1.0.b. Load the CSR
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" . Loading the certificate request ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 15:52:07 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
|
|
|
|
&csr.subject);
|
|
|
|
if (ret < 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 15:52:07 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.subject_name = subject_name;
|
2021-08-04 21:56:10 +02:00
|
|
|
subject_key = &csr.pk;
|
2013-09-09 13:59:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-09 13:59:42 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CSR_PARSE_C */
|
2013-09-09 13:59:42 +02:00
|
|
|
|
2013-09-06 09:55:26 +02:00
|
|
|
/*
|
|
|
|
* 1.1. Load the keys
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!opt.selfsign && !strlen(opt.request_file)) {
|
|
|
|
mbedtls_printf(" . Loading the subject key ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
|
|
|
|
opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 15:52:07 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-06 09:55:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" . Loading the issuer key ...");
|
|
|
|
fflush(stdout);
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
|
|
|
|
opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
|
|
|
|
"returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-06 09:55:26 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2013-09-09 13:59:42 +02:00
|
|
|
// Check if key and issuer certificate match
|
|
|
|
//
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strlen(opt.issuer_crt)) {
|
|
|
|
if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
|
|
|
|
mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
|
|
|
|
mbedtls_printf(" failed\n ! issuer_key does not match "
|
|
|
|
"issuer certificate\n\n");
|
2013-09-09 13:59:42 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (opt.selfsign) {
|
2013-10-28 22:28:09 +01:00
|
|
|
opt.subject_name = opt.issuer_name;
|
2013-09-12 05:21:54 +02:00
|
|
|
subject_key = issuer_key;
|
2013-09-09 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
|
|
|
|
mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
|
2013-09-09 16:24:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 1.0. Check the names for validity
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 16:24:18 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 16:24:18 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" . Setting certificate values ...");
|
|
|
|
fflush(stdout);
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509write_crt_set_version(&crt, opt.version);
|
|
|
|
mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
|
2023-01-26 17:43:09 +01:00
|
|
|
ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len);
|
2023-01-05 16:46:59 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
2023-01-26 17:43:09 +01:00
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw "
|
2023-01-05 16:46:59 +01:00
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
|
|
|
goto exit;
|
|
|
|
}
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-06 09:55:26 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
|
|
|
|
|
|
|
if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
|
|
|
|
opt.basic_constraints != 0) {
|
|
|
|
mbedtls_printf(" . Adding the Basic Constraints extension ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
|
|
|
|
opt.max_pathlen);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2013-09-06 19:27:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
}
|
2013-09-06 19:27:21 +02:00
|
|
|
|
2023-03-21 17:23:08 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA1)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
|
|
|
|
opt.subject_identifier != 0) {
|
|
|
|
mbedtls_printf(" . Adding the Subject Key Identifier ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
|
|
|
|
"_key_identifier returned -0x%04x - %s\n\n",
|
|
|
|
(unsigned int) -ret, buf);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2013-09-06 19:27:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
}
|
2013-09-06 19:27:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
|
|
|
|
opt.authority_identifier != 0) {
|
|
|
|
mbedtls_printf(" . Adding the Authority Key Identifier ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
|
|
|
|
"key_identifier returned -0x%04x - %s\n\n",
|
|
|
|
(unsigned int) -ret, buf);
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
Extend cert_write example program by multiple cmd line options
This commit adds the following command line options to programs/x509/cert_write:
- version (val 1, 2, 3): Set the certificate's version (v1, v2, v3)
- authority_identifier (val 0, 1): Enable or disable the addition of the
authority identifier extension.
- subject_identifier (val 0, 1): Enable or disable the addition of the
subject identifier extension.
- basic_constraints (val 0, 1): Enable or disable the addition of the
basic constraints extension.
- md (val MD5, SHA1, SHA256, SHA512): Set the hash function used
when creating the CRT.
2017-09-13 13:49:22 +02:00
|
|
|
}
|
2023-03-21 17:23:08 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA1 */
|
2013-09-06 19:27:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
|
|
|
|
opt.key_usage != 0) {
|
|
|
|
mbedtls_printf(" . Adding the Key Usage extension ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 12:37:54 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-09 12:37:54 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 16:38:30 +02:00
|
|
|
if (opt.san_list != NULL) {
|
|
|
|
ret = mbedtls_x509write_crt_set_subject_alternative_name(&crt, opt.san_list);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_printf(
|
2023-04-04 13:09:04 +02:00
|
|
|
" failed\n ! mbedtls_x509write_crt_set_subject_alternative_name returned %d",
|
2023-04-01 16:38:30 +02:00
|
|
|
ret);
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (opt.ext_key_usage) {
|
|
|
|
mbedtls_printf(" . Adding the Extended Key Usage extension ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(
|
|
|
|
" failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
|
|
|
|
(unsigned int) -ret,
|
|
|
|
buf);
|
2015-09-09 20:03:34 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2015-09-09 20:03:34 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
|
|
|
|
opt.ns_cert_type != 0) {
|
|
|
|
mbedtls_printf(" . Adding the NS Cert Type extension ...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
|
|
|
|
"returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
|
2013-09-09 12:37:54 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-09 12:37:54 +02:00
|
|
|
}
|
|
|
|
|
2013-09-06 09:55:26 +02:00
|
|
|
/*
|
2018-08-23 16:26:06 +02:00
|
|
|
* 1.2. Writing the certificate
|
2013-09-06 09:55:26 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" . Writing the certificate...");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if ((ret = write_certificate(&crt, opt.output_file,
|
|
|
|
mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
|
|
|
|
mbedtls_strerror(ret, buf, sizeof(buf));
|
|
|
|
mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
|
|
|
|
(unsigned int) -ret, buf);
|
2013-09-06 09:55:26 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(" ok\n");
|
2013-09-06 09:55:26 +02:00
|
|
|
|
2018-04-29 22:42:45 +02:00
|
|
|
exit_code = MBEDTLS_EXIT_SUCCESS;
|
|
|
|
|
2013-09-06 09:55:26 +02:00
|
|
|
exit:
|
2018-10-05 10:49:33 +02:00
|
|
|
#if defined(MBEDTLS_X509_CSR_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_csr_free(&csr);
|
2018-10-05 10:49:33 +02:00
|
|
|
#endif /* MBEDTLS_X509_CSR_PARSE_C */
|
2023-04-05 14:30:59 +02:00
|
|
|
mbedtls_asn1_free_named_data_list(&ext_san_dirname);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&issuer_crt);
|
|
|
|
mbedtls_x509write_crt_free(&crt);
|
|
|
|
mbedtls_pk_free(&loaded_subject_key);
|
|
|
|
mbedtls_pk_free(&loaded_issuer_key);
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
2023-04-19 13:47:43 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-04-19 10:15:26 +02:00
|
|
|
mbedtls_psa_crypto_free();
|
2023-04-19 13:47:43 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
mbedtls_exit(exit_code);
|
2013-09-06 09:55:26 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
|
|
|
|
MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
|
2016-10-07 16:00:17 +02:00
|
|
|
MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */
|