aboutsummaryrefslogtreecommitdiff
path: root/apps/cmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/cmp.c')
-rw-r--r--apps/cmp.c1572
1 files changed, 1211 insertions, 361 deletions
diff --git a/apps/cmp.c b/apps/cmp.c
index c479b1549660..291d62bfcb80 100644
--- a/apps/cmp.c
+++ b/apps/cmp.c
@@ -1,5 +1,5 @@
/*
- * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved.
* Copyright Nokia 2007-2019
* Copyright Siemens AG 2015-2019
*
@@ -10,6 +10,7 @@
*/
/* This app is disabled when OPENSSL_NO_CMP is defined. */
+#include "internal/e_os.h"
#include <string.h>
#include <ctype.h>
@@ -68,7 +69,7 @@ typedef enum {
} cmp_cmd_t;
/* message transfer */
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
static char *opt_server = NULL;
static char *opt_proxy = NULL;
static char *opt_no_proxy = NULL;
@@ -86,8 +87,19 @@ static char *opt_srvcert = NULL;
static char *opt_expect_sender = NULL;
static int opt_ignore_keyusage = 0;
static int opt_unprotected_errors = 0;
+static int opt_no_cache_extracerts = 0;
+static char *opt_srvcertout = NULL;
static char *opt_extracertsout = NULL;
static char *opt_cacertsout = NULL;
+static char *opt_oldwithold = NULL;
+static char *opt_newwithnew = NULL;
+static char *opt_newwithold = NULL;
+static char *opt_oldwithnew = NULL;
+static char *opt_crlcert = NULL;
+static char *opt_oldcrl = NULL;
+static char *opt_crlout = NULL;
+static char *opt_template = NULL;
+static char *opt_keyspec = NULL;
/* client authentication */
static char *opt_ref = NULL;
@@ -107,12 +119,14 @@ static int opt_cmd = -1;
static char *opt_geninfo = NULL;
static char *opt_infotype_s = NULL;
static int opt_infotype = NID_undef;
+static char *opt_profile = NULL;
/* certificate enrollment */
static char *opt_newkey = NULL;
static char *opt_newkeypass = NULL;
+static int opt_centralkeygen = 0;
+static char *opt_newkeyout = NULL;
static char *opt_subject = NULL;
-static char *opt_issuer = NULL;
static int opt_days = 0;
static char *opt_reqexts = NULL;
static char *opt_sans = NULL;
@@ -130,17 +144,25 @@ static char *opt_chainout = NULL;
/* certificate enrollment and revocation */
static char *opt_oldcert = NULL;
+static char *opt_issuer = NULL;
+static char *opt_serial = NULL;
static int opt_revreason = CRL_REASON_NONE;
/* credentials format */
static char *opt_certform_s = "PEM";
static int opt_certform = FORMAT_PEM;
+/*
+ * DER format is the preferred choice for saving a CRL because it allows for
+ * more efficient storage, especially when dealing with large CRLs.
+ */
+static char *opt_crlform_s = "DER";
+static int opt_crlform = FORMAT_ASN1;
static char *opt_keyform_s = NULL;
static int opt_keyform = FORMAT_UNDEF;
static char *opt_otherpass = NULL;
static char *opt_engine = NULL;
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
/* TLS connection */
static int opt_tls_used = 0;
static char *opt_tls_cert = NULL;
@@ -157,13 +179,15 @@ static int opt_repeat = 1;
static char *opt_reqin = NULL;
static int opt_reqin_new_tid = 0;
static char *opt_reqout = NULL;
+static char *opt_reqout_only = NULL;
+static int reqout_only_done = 0;
static char *opt_rspin = NULL;
static int rspin_in_use = 0;
static char *opt_rspout = NULL;
static int opt_use_mock_srv = 0;
/* mock server */
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
static char *opt_port = NULL;
static int opt_max_msgs = 0;
#endif
@@ -175,9 +199,17 @@ static char *opt_srv_keypass = NULL;
static char *opt_srv_trusted = NULL;
static char *opt_srv_untrusted = NULL;
+static char *opt_ref_cert = NULL;
static char *opt_rsp_cert = NULL;
+static char *opt_rsp_key = NULL;
+static char *opt_rsp_keypass = NULL;
+static char *opt_rsp_crl = NULL;
static char *opt_rsp_extracerts = NULL;
static char *opt_rsp_capubs = NULL;
+static char *opt_rsp_newwithnew = NULL;
+static char *opt_rsp_newwithold = NULL;
+static char *opt_rsp_oldwithnew = NULL;
+
static int opt_poll_count = 0;
static int opt_check_after = 1;
static int opt_grant_implicitconf = 0;
@@ -199,9 +231,11 @@ typedef enum OPTION_choice {
OPT_COMMON,
OPT_CONFIG, OPT_SECTION, OPT_VERBOSITY,
- OPT_CMD, OPT_INFOTYPE, OPT_GENINFO,
+ OPT_CMD, OPT_INFOTYPE, OPT_PROFILE, OPT_GENINFO,
+ OPT_TEMPLATE, OPT_KEYSPEC,
- OPT_NEWKEY, OPT_NEWKEYPASS, OPT_SUBJECT, OPT_ISSUER,
+ OPT_NEWKEY, OPT_NEWKEYPASS, OPT_CENTRALKEYGEN,
+ OPT_NEWKEYOUT, OPT_SUBJECT,
OPT_DAYS, OPT_REQEXTS,
OPT_SANS, OPT_SAN_NODEFAULT,
OPT_POLICIES, OPT_POLICY_OIDS, OPT_POLICY_OIDS_CRITICAL,
@@ -209,9 +243,9 @@ typedef enum OPTION_choice {
OPT_OUT_TRUSTED, OPT_IMPLICIT_CONFIRM, OPT_DISABLE_CONFIRM,
OPT_CERTOUT, OPT_CHAINOUT,
- OPT_OLDCERT, OPT_REVREASON,
+ OPT_OLDCERT, OPT_ISSUER, OPT_SERIAL, OPT_REVREASON,
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
OPT_SERVER, OPT_PROXY, OPT_NO_PROXY,
#endif
OPT_RECIPIENT, OPT_PATH,
@@ -219,14 +253,16 @@ typedef enum OPTION_choice {
OPT_TRUSTED, OPT_UNTRUSTED, OPT_SRVCERT,
OPT_EXPECT_SENDER,
- OPT_IGNORE_KEYUSAGE, OPT_UNPROTECTED_ERRORS,
- OPT_EXTRACERTSOUT, OPT_CACERTSOUT,
+ OPT_IGNORE_KEYUSAGE, OPT_UNPROTECTED_ERRORS, OPT_NO_CACHE_EXTRACERTS,
+ OPT_SRVCERTOUT, OPT_EXTRACERTSOUT, OPT_CACERTSOUT,
+ OPT_OLDWITHOLD, OPT_NEWWITHNEW, OPT_NEWWITHOLD, OPT_OLDWITHNEW,
+ OPT_CRLCERT, OPT_OLDCRL, OPT_CRLOUT,
OPT_REF, OPT_SECRET, OPT_CERT, OPT_OWN_TRUSTED, OPT_KEY, OPT_KEYPASS,
OPT_DIGEST, OPT_MAC, OPT_EXTRACERTS,
OPT_UNPROTECTED_REQUESTS,
- OPT_CERTFORM, OPT_KEYFORM,
+ OPT_CERTFORM, OPT_CRLFORM, OPT_KEYFORM,
OPT_OTHERPASS,
#ifndef OPENSSL_NO_ENGINE
OPT_ENGINE,
@@ -234,23 +270,26 @@ typedef enum OPTION_choice {
OPT_PROV_ENUM,
OPT_R_ENUM,
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
OPT_TLS_USED, OPT_TLS_CERT, OPT_TLS_KEY,
OPT_TLS_KEYPASS,
OPT_TLS_EXTRA, OPT_TLS_TRUSTED, OPT_TLS_HOST,
#endif
OPT_BATCH, OPT_REPEAT,
- OPT_REQIN, OPT_REQIN_NEW_TID, OPT_REQOUT, OPT_RSPIN, OPT_RSPOUT,
+ OPT_REQIN, OPT_REQIN_NEW_TID, OPT_REQOUT, OPT_REQOUT_ONLY,
+ OPT_RSPIN, OPT_RSPOUT,
OPT_USE_MOCK_SRV,
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
OPT_PORT, OPT_MAX_MSGS,
#endif
OPT_SRV_REF, OPT_SRV_SECRET,
OPT_SRV_CERT, OPT_SRV_KEY, OPT_SRV_KEYPASS,
OPT_SRV_TRUSTED, OPT_SRV_UNTRUSTED,
- OPT_RSP_CERT, OPT_RSP_EXTRACERTS, OPT_RSP_CAPUBS,
+ OPT_REF_CERT, OPT_RSP_CERT, OPT_RSP_KEY, OPT_RSP_KEYPASS,
+ OPT_RSP_CRL, OPT_RSP_EXTRACERTS, OPT_RSP_CAPUBS,
+ OPT_RSP_NEWWITHNEW, OPT_RSP_NEWWITHOLD, OPT_RSP_OLDWITHNEW,
OPT_POLL_COUNT, OPT_CHECK_AFTER,
OPT_GRANT_IMPLICITCONF,
OPT_PKISTATUS, OPT_FAILURE,
@@ -275,26 +314,34 @@ const OPTIONS cmp_options[] = {
OPT_SECTION("Generic message"),
{"cmd", OPT_CMD, 's', "CMP request to send: ir/cr/kur/p10cr/rr/genm"},
{"infotype", OPT_INFOTYPE, 's',
- "InfoType name for requesting specific info in genm, e.g. 'signKeyPairTypes'"},
+ "InfoType name for requesting specific info in genm, with specific support"},
+ {OPT_MORE_STR, 0, 0,
+ "for 'caCerts' and 'rootCaCert'"},
+ {"profile", OPT_PROFILE, 's',
+ "Certificate profile name to place in generalInfo field of request PKIHeader"},
{"geninfo", OPT_GENINFO, 's',
- "generalInfo integer values to place in request PKIHeader with given OID"},
+ "Comma-separated list of OID and value to place in generalInfo PKIHeader"},
{OPT_MORE_STR, 0, 0,
- "specified in the form <OID>:int:<n>, e.g. \"1.2.3.4:int:56789\""},
+ "of form <OID>:int:<n> or <OID>:str:<s>, e.g. \'1.2.3.4:int:56789, id-kp:str:name'"},
+ { "template", OPT_TEMPLATE, 's',
+ "File to save certTemplate received in genp of type certReqTemplate"},
+ { "keyspec", OPT_KEYSPEC, 's',
+ "Optional file to save Key specification received in genp of type certReqTemplate"},
OPT_SECTION("Certificate enrollment"),
{"newkey", OPT_NEWKEY, 's',
"Private or public key for the requested cert. Default: CSR key or client key"},
{"newkeypass", OPT_NEWKEYPASS, 's', "New private key pass phrase source"},
+ {"centralkeygen", OPT_CENTRALKEYGEN, '-',
+ "Request central (server-side) key generation. Default is local generation"},
+ {"newkeyout", OPT_NEWKEYOUT, 's',
+ "File to save centrally generated key, in PEM format"},
{"subject", OPT_SUBJECT, 's',
"Distinguished Name (DN) of subject to use in the requested cert template"},
{OPT_MORE_STR, 0, 0,
"For kur, default is subject of -csr arg or reference cert (see -oldcert)"},
{OPT_MORE_STR, 0, 0,
"this default is used for ir and cr only if no Subject Alt Names are set"},
- {"issuer", OPT_ISSUER, 's',
- "DN of the issuer to place in the requested certificate template"},
- {OPT_MORE_STR, 0, 0,
- "also used as recipient if neither -recipient nor -srvcert are given"},
{"days", OPT_DAYS, 'N',
"Requested validity time of the new certificate in number of days"},
{"reqexts", OPT_REQEXTS, 's',
@@ -338,15 +385,21 @@ const OPTIONS cmp_options[] = {
"also used as reference (defaulting to -cert) for subject DN and SANs."},
{OPT_MORE_STR, 0, 0,
"Issuer is used as recipient unless -recipient, -srvcert, or -issuer given"},
+ {"issuer", OPT_ISSUER, 's',
+ "DN of the issuer to place in the certificate template of ir/cr/kur/rr;"},
+ {OPT_MORE_STR, 0, 0,
+ "also used as recipient if neither -recipient nor -srvcert are given"},
+ {"serial", OPT_SERIAL, 's',
+ "Serial number of certificate to be revoked in revocation request (rr)"},
{"revreason", OPT_REVREASON, 'n',
"Reason code to include in revocation request (rr); possible values:"},
{OPT_MORE_STR, 0, 0,
"0..6, 8..10 (see RFC5280, 5.3.1) or -1. Default -1 = none included"},
OPT_SECTION("Message transfer"),
-#ifdef OPENSSL_NO_SOCK
+#if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
{OPT_MORE_STR, 0, 0,
- "NOTE: -server, -proxy, and -no_proxy not supported due to no-sock build"},
+ "NOTE: -server, -proxy, and -no_proxy not supported due to no-sock/no-http build"},
#else
{"server", OPT_SERVER, 's',
"[http[s]://]address[:port][/path] of CMP server. Default port 80 or 443."},
@@ -388,10 +441,28 @@ const OPTIONS cmp_options[] = {
"certificate responses (ip/cp/kup), revocation responses (rp), and PKIConf"},
{OPT_MORE_STR, 0, 0,
"WARNING: This setting leads to behavior allowing violation of RFC 4210"},
+ {"no_cache_extracerts", OPT_NO_CACHE_EXTRACERTS, '-',
+ "Do not keep certificates received in the extraCerts CMP message field"},
+ { "srvcertout", OPT_SRVCERTOUT, 's',
+ "File to save the server cert used and validated for CMP response protection"},
{"extracertsout", OPT_EXTRACERTSOUT, 's',
"File to save extra certificates received in the extraCerts field"},
{"cacertsout", OPT_CACERTSOUT, 's',
- "File to save CA certificates received in the caPubs field of 'ip' messages"},
+ "File to save CA certs received in caPubs field or genp with id-it-caCerts"},
+ { "oldwithold", OPT_OLDWITHOLD, 's',
+ "Root CA certificate to request update for in genm of type rootCaCert"},
+ { "newwithnew", OPT_NEWWITHNEW, 's',
+ "File to save NewWithNew cert received in genp of type rootCaKeyUpdate"},
+ { "newwithold", OPT_NEWWITHOLD, 's',
+ "File to save NewWithOld cert received in genp of type rootCaKeyUpdate"},
+ { "oldwithnew", OPT_OLDWITHNEW, 's',
+ "File to save OldWithNew cert received in genp of type rootCaKeyUpdate"},
+ { "crlcert", OPT_CRLCERT, 's',
+ "certificate to request a CRL for in genm of type crlStatusList"},
+ { "oldcrl", OPT_OLDCRL, 's',
+ "CRL to request update for in genm of type crlStatusList"},
+ { "crlout", OPT_CRLOUT, 's',
+ "File to save new CRL received in genp of type 'crls'"},
OPT_SECTION("Client authentication"),
{"ref", OPT_REF, 's',
@@ -423,6 +494,8 @@ const OPTIONS cmp_options[] = {
OPT_SECTION("Credentials format"),
{"certform", OPT_CERTFORM, 's',
"Format (PEM or DER) to use when saving a certificate to a file. Default PEM"},
+ {"crlform", OPT_CRLFORM, 's',
+ "Format (PEM or DER) to use when saving a CRL to a file. Default DER"},
{"keyform", OPT_KEYFORM, 's',
"Format of the key input (ENGINE, other values ignored)"},
{"otherpass", OPT_OTHERPASS, 's',
@@ -437,9 +510,9 @@ const OPTIONS cmp_options[] = {
OPT_R_OPTIONS,
OPT_SECTION("TLS connection"),
-#ifdef OPENSSL_NO_SOCK
+#if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
{OPT_MORE_STR, 0, 0,
- "NOTE: -tls_used and all other TLS options not supported due to no-sock build"},
+ "NOTE: -tls_used and all other TLS options not supported due to no-sock/no-http build"},
#else
{"tls_used", OPT_TLS_USED, '-',
"Enable using TLS (also when other TLS options are not set)"},
@@ -453,9 +526,9 @@ const OPTIONS cmp_options[] = {
"Extra certificates to provide to TLS server during TLS handshake"},
{"tls_trusted", OPT_TLS_TRUSTED, 's',
"Trusted certificates to use for verifying the TLS server certificate;"},
- {OPT_MORE_STR, 0, 0, "this implies host name validation"},
+ {OPT_MORE_STR, 0, 0, "this implies hostname validation"},
{"tls_host", OPT_TLS_HOST, 's',
- "Address to be checked (rather than -server) during TLS host name validation"},
+ "Address to be checked (rather than -server) during TLS hostname validation"},
#endif
OPT_SECTION("Client-side debugging"),
@@ -469,6 +542,8 @@ const OPTIONS cmp_options[] = {
"Use fresh transactionID for CMP requests read from -reqin"},
{"reqout", OPT_REQOUT, 's',
"Save sequence of CMP requests created by the client to file(s)"},
+ {"reqout_only", OPT_REQOUT_ONLY, 's',
+ "Save first CMP request created by the client to file and exit"},
{"rspin", OPT_RSPIN, 's',
"Process sequence of CMP responses provided in file(s), skipping server"},
{"rspout", OPT_RSPOUT, 's',
@@ -478,9 +553,9 @@ const OPTIONS cmp_options[] = {
"Use internal mock server at API level, bypassing socket-based HTTP"},
OPT_SECTION("Mock server"),
-#ifdef OPENSSL_NO_SOCK
+#if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
{OPT_MORE_STR, 0, 0,
- "NOTE: -port and -max_msgs not supported due to no-sock build"},
+ "NOTE: -port and -max_msgs not supported due to no-sock/no-http build"},
#else
{"port", OPT_PORT, 's',
"Act as HTTP-based mock server listening on given port"},
@@ -502,12 +577,28 @@ const OPTIONS cmp_options[] = {
"Trusted certificates for client authentication"},
{"srv_untrusted", OPT_SRV_UNTRUSTED, 's',
"Intermediate certs that may be useful for verifying CMP protection"},
+ {"ref_cert", OPT_RSP_CERT, 's',
+ "Certificate to be expected for rr and any oldCertID in kur messages"},
{"rsp_cert", OPT_RSP_CERT, 's',
"Certificate to be returned as mock enrollment result"},
+ {"rsp_key", OPT_RSP_KEY, 's',
+ "Private key for the certificate to be returned as mock enrollment result"},
+ {OPT_MORE_STR, 0, 0,
+ "Key to be returned for central key pair generation"},
+ {"rsp_keypass", OPT_RSP_KEYPASS, 's',
+ "Response private key (and cert) pass phrase source"},
+ {"rsp_crl", OPT_RSP_CRL, 's',
+ "CRL to be returned in genp of type crls"},
{"rsp_extracerts", OPT_RSP_EXTRACERTS, 's',
"Extra certificates to be included in mock certification responses"},
{"rsp_capubs", OPT_RSP_CAPUBS, 's',
"CA certificates to be included in mock ip response"},
+ {"rsp_newwithnew", OPT_RSP_NEWWITHNEW, 's',
+ "New root CA certificate to include in genp of type rootCaKeyUpdate"},
+ {"rsp_newwithold", OPT_RSP_NEWWITHOLD, 's',
+ "NewWithOld transition cert to include in genp of type rootCaKeyUpdate"},
+ {"rsp_oldwithnew", OPT_RSP_OLDWITHNEW, 's',
+ "OldWithNew transition cert to include in genp of type rootCaKeyUpdate"},
{"poll_count", OPT_POLL_COUNT, 'N',
"Number of times the client must poll before receiving a certificate"},
{"check_after", OPT_CHECK_AFTER, 'N',
@@ -552,10 +643,11 @@ typedef union {
static varref cmp_vars[] = { /* must be in same order as enumerated above! */
{&opt_config}, {&opt_section}, {(char **)&opt_verbosity},
- {&opt_cmd_s}, {&opt_infotype_s}, {&opt_geninfo},
+ {&opt_cmd_s}, {&opt_infotype_s}, {&opt_profile}, {&opt_geninfo},
+ {&opt_template}, {&opt_keyspec},
- {&opt_newkey}, {&opt_newkeypass}, {&opt_subject}, {&opt_issuer},
- {(char **)&opt_days}, {&opt_reqexts},
+ {&opt_newkey}, {&opt_newkeypass}, {(char **)&opt_centralkeygen},
+ {&opt_newkeyout}, {&opt_subject}, {(char **)&opt_days}, {&opt_reqexts},
{&opt_sans}, {(char **)&opt_san_nodefault},
{&opt_policies}, {&opt_policy_oids}, {(char **)&opt_policy_oids_critical},
{(char **)&opt_popo}, {&opt_csr},
@@ -563,9 +655,9 @@ static varref cmp_vars[] = { /* must be in same order as enumerated above! */
{(char **)&opt_implicit_confirm}, {(char **)&opt_disable_confirm},
{&opt_certout}, {&opt_chainout},
- {&opt_oldcert}, {(char **)&opt_revreason},
+ {&opt_oldcert}, {&opt_issuer}, {&opt_serial}, {(char **)&opt_revreason},
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
{&opt_server}, {&opt_proxy}, {&opt_no_proxy},
#endif
{&opt_recipient}, {&opt_path}, {(char **)&opt_keep_alive},
@@ -574,20 +666,23 @@ static varref cmp_vars[] = { /* must be in same order as enumerated above! */
{&opt_trusted}, {&opt_untrusted}, {&opt_srvcert},
{&opt_expect_sender},
{(char **)&opt_ignore_keyusage}, {(char **)&opt_unprotected_errors},
- {&opt_extracertsout}, {&opt_cacertsout},
+ {(char **)&opt_no_cache_extracerts},
+ {&opt_srvcertout}, {&opt_extracertsout}, {&opt_cacertsout},
+ {&opt_oldwithold}, {&opt_newwithnew}, {&opt_newwithold}, {&opt_oldwithnew},
+ {&opt_crlcert}, {&opt_oldcrl}, {&opt_crlout},
{&opt_ref}, {&opt_secret},
{&opt_cert}, {&opt_own_trusted}, {&opt_key}, {&opt_keypass},
{&opt_digest}, {&opt_mac}, {&opt_extracerts},
{(char **)&opt_unprotected_requests},
- {&opt_certform_s}, {&opt_keyform_s},
+ {&opt_certform_s}, {&opt_crlform_s}, {&opt_keyform_s},
{&opt_otherpass},
#ifndef OPENSSL_NO_ENGINE
{&opt_engine},
#endif
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
{(char **)&opt_tls_used}, {&opt_tls_cert}, {&opt_tls_key},
{&opt_tls_keypass},
{&opt_tls_extra}, {&opt_tls_trusted}, {&opt_tls_host},
@@ -595,16 +690,19 @@ static varref cmp_vars[] = { /* must be in same order as enumerated above! */
{(char **)&opt_batch}, {(char **)&opt_repeat},
{&opt_reqin}, {(char **)&opt_reqin_new_tid},
- {&opt_reqout}, {&opt_rspin}, {&opt_rspout},
+ {&opt_reqout}, {&opt_reqout_only}, {&opt_rspin}, {&opt_rspout},
{(char **)&opt_use_mock_srv},
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
{&opt_port}, {(char **)&opt_max_msgs},
#endif
{&opt_srv_ref}, {&opt_srv_secret},
{&opt_srv_cert}, {&opt_srv_key}, {&opt_srv_keypass},
{&opt_srv_trusted}, {&opt_srv_untrusted},
- {&opt_rsp_cert}, {&opt_rsp_extracerts}, {&opt_rsp_capubs},
+ {&opt_ref_cert}, {&opt_rsp_cert}, {&opt_rsp_key}, {&opt_rsp_keypass},
+ {&opt_rsp_crl}, {&opt_rsp_extracerts}, {&opt_rsp_capubs},
+ {&opt_rsp_newwithnew}, {&opt_rsp_newwithold}, {&opt_rsp_oldwithnew},
+
{(char **)&opt_poll_count}, {(char **)&opt_check_after},
{(char **)&opt_grant_implicitconf},
{(char **)&opt_pkistatus}, {(char **)&opt_failure},
@@ -689,40 +787,12 @@ static X509 *load_cert_pwd(const char *uri, const char *pass, const char *desc)
return cert;
}
-static X509_REQ *load_csr_autofmt(const char *infile, const char *desc)
-{
- X509_REQ *csr;
- BIO *bio_bak = bio_err;
-
- bio_err = NULL; /* do not show errors on more than one try */
- csr = load_csr(infile, FORMAT_PEM, desc);
- bio_err = bio_bak;
- if (csr == NULL) {
- ERR_clear_error();
- csr = load_csr(infile, FORMAT_ASN1, desc);
- }
- if (csr == NULL) {
- ERR_print_errors(bio_err);
- BIO_printf(bio_err, "error: unable to load %s from file '%s'\n", desc,
- infile);
- } else {
- EVP_PKEY *pkey = X509_REQ_get0_pubkey(csr);
- int ret = do_X509_REQ_verify(csr, pkey, NULL /* vfyopts */);
-
- if (pkey == NULL || ret < 0)
- CMP_warn("error while verifying CSR self-signature");
- else if (ret == 0)
- CMP_warn("CSR self-signature does not match the contents");
- }
- return csr;
-}
-
-/* set expected host name/IP addr and clears the email addr in the given ts */
+/* set expected hostname/IP addr and clears the email addr in the given ts */
static int truststore_set_host_etc(X509_STORE *ts, const char *host)
{
X509_VERIFY_PARAM *ts_vpm = X509_STORE_get0_param(ts);
- /* first clear any host names, IP, and email addresses */
+ /* first clear any hostnames, IP, and email addresses */
if (!X509_VERIFY_PARAM_set1_host(ts_vpm, NULL, 0)
|| !X509_VERIFY_PARAM_set1_ip(ts_vpm, NULL, 0)
|| !X509_VERIFY_PARAM_set1_email(ts_vpm, NULL, 0))
@@ -797,8 +867,15 @@ static OSSL_CMP_MSG *read_write_req_resp(OSSL_CMP_CTX *ctx,
OSSL_CMP_PKIHEADER *hdr;
const char *prev_opt_rspin = opt_rspin;
- if (req != NULL && opt_reqout != NULL
- && !write_PKIMESSAGE(req, &opt_reqout))
+ if (opt_reqout_only != NULL) {
+ if (OSSL_CMP_MSG_write(opt_reqout_only, req) < 0)
+ CMP_err1("cannot write request PKIMessage to file '%s'",
+ opt_reqout_only);
+ else
+ reqout_only_done = 1;
+ return NULL; /* stop at this point, not contacting any server */
+ }
+ if (opt_reqout != NULL && !write_PKIMESSAGE(req, &opt_reqout))
goto err;
if (opt_reqin != NULL && opt_rspin == NULL) {
if ((req_new = read_PKIMESSAGE("actually sending", &opt_reqin)) == NULL)
@@ -830,7 +907,7 @@ static OSSL_CMP_MSG *read_write_req_resp(OSSL_CMP_CTX *ctx,
CMP_warn("too few -rspin filename arguments; resorting to using mock server");
res = OSSL_CMP_CTX_server_perform(ctx, actual_req);
} else {
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
if (opt_server == NULL) {
CMP_err("missing -server or -use_mock_srv option, or too few -rspin filename arguments");
goto err;
@@ -839,7 +916,7 @@ static OSSL_CMP_MSG *read_write_req_resp(OSSL_CMP_CTX *ctx,
CMP_warn("too few -rspin filename arguments; resorting to contacting server");
res = OSSL_CMP_MSG_http_perform(ctx, actual_req);
#else
- CMP_err("-server not supported on no-sock build; missing -use_mock_srv option or too few -rspin filename arguments");
+ CMP_err("-server not supported on no-sock/no-http build; missing -use_mock_srv option or too few -rspin filename arguments");
#endif
}
rspin_in_use = 0;
@@ -907,11 +984,12 @@ static int set_gennames(OSSL_CMP_CTX *ctx, char *names, const char *desc)
continue;
}
- /* try IP address first, then URI or domain name */
+ /* try IP address first, then email/URI/domain name */
(void)ERR_set_mark();
n = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_IPADD, names, 0);
if (n == NULL)
n = a2i_GENERAL_NAME(NULL, NULL, NULL,
+ strchr(names, '@') != NULL ? GEN_EMAIL :
strchr(names, ':') != NULL ? GEN_URI : GEN_DNS,
names, 0);
(void)ERR_pop_to_mark();
@@ -948,8 +1026,23 @@ static X509_STORE *load_trusted(char *input, int for_new_cert, const char *desc)
return NULL;
}
-typedef int (*add_X509_stack_fn_t)(void *ctx, const STACK_OF(X509) *certs);
+typedef int (*add_X509_fn_t)(void *ctx, const X509 *cert);
+static int setup_cert(void *ctx, const char *file, const char *pass,
+ const char *desc, add_X509_fn_t set1_fn)
+{
+ X509 *cert;
+ int ok;
+
+ if (file == NULL)
+ return 1;
+ if ((cert = load_cert_pwd(file, pass, desc)) == NULL)
+ return 0;
+ ok = (*set1_fn)(ctx, cert);
+ X509_free(cert);
+ return ok;
+}
+typedef int (*add_X509_stack_fn_t)(void *ctx, const STACK_OF(X509) *certs);
static int setup_certs(char *files, const char *desc, void *ctx,
add_X509_stack_fn_t set1_fn)
{
@@ -961,11 +1054,23 @@ static int setup_certs(char *files, const char *desc, void *ctx,
if ((certs = load_certs_multifile(files, opt_otherpass, desc, vpm)) == NULL)
return 0;
ok = (*set1_fn)(ctx, certs);
- sk_X509_pop_free(certs, X509_free);
+ OSSL_STACK_OF_X509_free(certs);
return ok;
}
+static int setup_mock_crlout(void *ctx, const char *file, const char *desc)
+{
+ X509_CRL *crl;
+ int ok;
+ if (file == NULL)
+ return 1;
+ if ((crl = load_crl(file, FORMAT_UNDEF, 0, desc)) == NULL)
+ return 0;
+ ok = ossl_cmp_mock_srv_set1_crlOut(ctx, crl);
+ X509_CRL_free(crl);
+ return ok;
+}
/*
* parse and transform some options, checking their syntax.
* Returns 1 on success, 0 on error
@@ -1013,6 +1118,11 @@ static int transform_opts(void)
CMP_err("unknown option given for certificate storing format");
return 0;
}
+ if (opt_crlform_s != NULL
+ && !opt_format(opt_crlform_s, OPT_FMT_PEMDER, &opt_crlform)) {
+ CMP_err("unknown option given for CRL storing format");
+ return 0;
+ }
return 1;
}
@@ -1063,16 +1173,10 @@ static OSSL_CMP_SRV_CTX *setup_srv_ctx(ENGINE *engine)
CMP_err("must give both -srv_cert and -srv_key options or neither");
goto err;
}
- if (opt_srv_cert != NULL) {
- X509 *srv_cert = load_cert_pwd(opt_srv_cert, opt_srv_keypass,
- "certificate of the mock server");
-
- if (srv_cert == NULL || !OSSL_CMP_CTX_set1_cert(ctx, srv_cert)) {
- X509_free(srv_cert);
- goto err;
- }
- X509_free(srv_cert);
- }
+ if (!setup_cert(ctx, opt_srv_cert, opt_srv_keypass,
+ "signer certificate of the mock server",
+ (add_X509_fn_t)OSSL_CMP_CTX_set1_cert))
+ goto err;
if (opt_srv_key != NULL) {
EVP_PKEY *pkey = load_key_pwd(opt_srv_key, opt_keyform,
opt_srv_keypass,
@@ -1090,7 +1194,7 @@ static OSSL_CMP_SRV_CTX *setup_srv_ctx(ENGINE *engine)
X509_STORE *ts =
load_trusted(opt_srv_trusted, 0, "certs trusted by mock server");
- if (ts == NULL || !OSSL_CMP_CTX_set0_trustedStore(ctx, ts)) {
+ if (ts == NULL || !OSSL_CMP_CTX_set0_trusted(ctx, ts)) {
X509_STORE_free(ts);
goto err;
}
@@ -1102,21 +1206,35 @@ static OSSL_CMP_SRV_CTX *setup_srv_ctx(ENGINE *engine)
(add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
goto err;
+ if (!setup_cert(srv_ctx, opt_ref_cert, opt_otherpass,
+ "reference cert to be expected by the mock server",
+ (add_X509_fn_t)ossl_cmp_mock_srv_set1_refCert))
+ goto err;
if (opt_rsp_cert == NULL) {
CMP_warn("no -rsp_cert given for mock server");
} else {
- X509 *cert = load_cert_pwd(opt_rsp_cert, opt_keypass,
- "cert to be returned by the mock server");
-
- if (cert == NULL)
+ if (!setup_cert(srv_ctx, opt_rsp_cert, opt_rsp_keypass,
+ "cert the mock server returns on certificate requests",
+ (add_X509_fn_t)ossl_cmp_mock_srv_set1_certOut))
goto err;
- /* from server perspective the server is the client */
- if (!ossl_cmp_mock_srv_set1_certOut(srv_ctx, cert)) {
- X509_free(cert);
+ }
+ if (opt_rsp_key != NULL) {
+ EVP_PKEY *pkey = load_key_pwd(opt_rsp_key, opt_keyform,
+ opt_rsp_keypass, engine,
+ "private key for enrollment cert");
+
+ if (pkey == NULL
+ || !ossl_cmp_mock_srv_set1_keyOut(srv_ctx, pkey)) {
+ EVP_PKEY_free(pkey);
goto err;
}
- X509_free(cert);
+ EVP_PKEY_free(pkey);
}
+ cleanse(opt_rsp_keypass);
+
+ if (!setup_mock_crlout(srv_ctx, opt_rsp_crl,
+ "CRL to be returned by the mock server"))
+ goto err;
if (!setup_certs(opt_rsp_extracerts,
"CMP extra certificates for mock server", srv_ctx,
(add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_chainOut))
@@ -1124,12 +1242,22 @@ static OSSL_CMP_SRV_CTX *setup_srv_ctx(ENGINE *engine)
if (!setup_certs(opt_rsp_capubs, "caPubs for mock server", srv_ctx,
(add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_caPubsOut))
goto err;
+ if (!setup_cert(srv_ctx, opt_rsp_newwithnew, opt_otherpass,
+ "NewWithNew cert the mock server returns in rootCaKeyUpdate",
+ (add_X509_fn_t)ossl_cmp_mock_srv_set1_newWithNew)
+ || !setup_cert(srv_ctx, opt_rsp_newwithold, opt_otherpass,
+ "NewWithOld cert the mock server returns in rootCaKeyUpdate",
+ (add_X509_fn_t)ossl_cmp_mock_srv_set1_newWithOld)
+ || !setup_cert(srv_ctx, opt_rsp_oldwithnew, opt_otherpass,
+ "OldWithNew cert the mock server returns in rootCaKeyUpdate",
+ (add_X509_fn_t)ossl_cmp_mock_srv_set1_oldWithNew))
+ goto err;
(void)ossl_cmp_mock_srv_set_pollCount(srv_ctx, opt_poll_count);
(void)ossl_cmp_mock_srv_set_checkAfterTime(srv_ctx, opt_check_after);
if (opt_grant_implicitconf)
(void)OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(srv_ctx, 1);
- if (opt_failure != INT_MIN) { /* option has been set explicity */
+ if (opt_failure != INT_MIN) { /* option has been set explicitly */
if (opt_failure < 0 || OSSL_CMP_PKIFAILUREINFO_MAX < opt_failure) {
CMP_err1("-failure out of range, should be >= 0 and <= %d",
OSSL_CMP_PKIFAILUREINFO_MAX);
@@ -1180,10 +1308,6 @@ static int setup_verification_ctx(OSSL_CMP_CTX *ctx)
return 0;
if (opt_srvcert != NULL || opt_trusted != NULL) {
- X509 *srvcert;
- X509_STORE *ts;
- int ok;
-
if (opt_srvcert != NULL) {
if (opt_trusted != NULL) {
CMP_warn("-trusted option is ignored since -srvcert option is present");
@@ -1193,30 +1317,27 @@ static int setup_verification_ctx(OSSL_CMP_CTX *ctx)
CMP_warn("-recipient option is ignored since -srvcert option is present");
opt_recipient = NULL;
}
- srvcert = load_cert_pwd(opt_srvcert, opt_otherpass,
- "directly trusted CMP server certificate");
- ok = srvcert != NULL && OSSL_CMP_CTX_set1_srvCert(ctx, srvcert);
- X509_free(srvcert);
- if (!ok)
+ if (!setup_cert(ctx, opt_srvcert, opt_otherpass,
+ "directly trusted CMP server certificate",
+ (add_X509_fn_t)OSSL_CMP_CTX_set1_srvCert))
return 0;
}
if (opt_trusted != NULL) {
+ X509_STORE *ts;
+
/*
* the 0 arg below clears any expected host/ip/email address;
* opt_expect_sender is used instead
*/
ts = load_trusted(opt_trusted, 0, "certs trusted by client");
- if (ts == NULL || !OSSL_CMP_CTX_set0_trustedStore(ctx, ts)) {
+ if (ts == NULL || !OSSL_CMP_CTX_set0_trusted(ctx, ts)) {
X509_STORE_free(ts);
return 0;
}
}
}
- if (opt_ignore_keyusage)
- (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_IGNORE_KEYUSAGE, 1);
-
if (opt_unprotected_errors)
(void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
@@ -1244,7 +1365,7 @@ static int setup_verification_ctx(OSSL_CMP_CTX *ctx)
return 1;
}
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
/*
* set up ssl_ctx for the OSSL_CMP_CTX based on options from config file/CLI.
* Returns pointer on success, NULL on error
@@ -1267,6 +1388,9 @@ static SSL_CTX *setup_ssl_ctx(OSSL_CMP_CTX *ctx, const char *host,
if (trust_store == NULL)
goto err;
SSL_CTX_set_cert_store(ssl_ctx, trust_store);
+ SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
+ } else {
+ CMP_warn("-tls_used given without -tls_trusted; will not authenticate the TLS server");
}
if (opt_tls_cert != NULL && opt_tls_key != NULL) {
@@ -1290,7 +1414,7 @@ static SSL_CTX *setup_ssl_ctx(OSSL_CMP_CTX *ctx, const char *host,
if (!ok || !SSL_CTX_set0_chain(ssl_ctx, certs)) {
CMP_err1("unable to use client TLS certificate file '%s'",
opt_tls_cert);
- sk_X509_pop_free(certs, X509_free);
+ OSSL_STACK_OF_X509_free(certs);
goto err;
}
for (i = 0; i < sk_X509_num(untrusted); i++) {
@@ -1377,13 +1501,18 @@ static SSL_CTX *setup_ssl_ctx(OSSL_CMP_CTX *ctx, const char *host,
goto err;
}
EVP_PKEY_free(pkey); /* we do not need the handle any more */
+ } else {
+ CMP_warn("-tls_used given without -tls_key; cannot authenticate to the TLS server");
}
- if (opt_tls_trusted != NULL) {
- /* enable and parameterize server hostname/IP address check */
+ if (trust_store != NULL) {
+ /*
+ * Enable and parameterize server hostname/IP address check.
+ * If we did this before checking our own TLS cert
+ * the expected hostname would mislead the check.
+ */
if (!truststore_set_host_etc(trust_store,
opt_tls_host != NULL ? opt_tls_host : host))
goto err;
- SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
}
return ssl_ctx;
err:
@@ -1409,7 +1538,7 @@ static int setup_protection_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
CMP_err("must give -ref if no -cert and no -subject given");
return 0;
}
- if (!opt_secret && ((opt_cert == NULL) != (opt_key == NULL))) {
+ if (opt_secret == NULL && ((opt_cert == NULL) != (opt_key == NULL))) {
CMP_err("must give both -cert and -key options or neither");
return 0;
}
@@ -1471,7 +1600,7 @@ static int setup_protection_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
ok = ok && OSSL_CMP_CTX_build_cert_chain(ctx, own_trusted, certs);
}
X509_STORE_free(own_trusted);
- sk_X509_pop_free(certs, X509_free);
+ OSSL_STACK_OF_X509_free(certs);
if (!ok)
return 0;
} else if (opt_own_trusted != NULL) {
@@ -1502,6 +1631,7 @@ static int setup_protection_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
if (opt_mac != NULL) {
int mac = OBJ_ln2nid(opt_mac);
+
if (mac == NID_undef) {
CMP_err1("MAC algorithm name not recognized: '%s'", opt_mac);
return 0;
@@ -1511,10 +1641,52 @@ static int setup_protection_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
return 1;
}
+static int set_fallback_pubkey(OSSL_CMP_CTX *ctx)
+{
+ char *file = opt_reqin, *end = file, bak;
+ OSSL_CMP_MSG *req;
+ const X509_PUBKEY *pubkey;
+ EVP_PKEY *pkey;
+ EVP_PKEY *pkey1;
+ int res = 0;
+
+ /* temporarily separate first file name in opt_reqin */
+ while (*end != ',' && !isspace(_UC(*end)) && *end != '\0')
+ end++;
+ bak = *end;
+ *end = '\0';
+ req = OSSL_CMP_MSG_read(file, app_get0_libctx(), app_get0_propq());
+ *end = bak;
+
+ if (req == NULL) {
+ CMP_err1("failed to load ir/cr/kur file '%s' attempting to get fallback public key",
+ file);
+ return 0;
+ }
+ if ((pubkey = OSSL_CMP_MSG_get0_certreq_publickey(req)) == NULL
+ || (pkey = X509_PUBKEY_get0(pubkey)) == NULL) {
+ CMP_err1("failed to get fallback public key from ir/cr/kur file '%s'",
+ file);
+ goto err;
+ }
+ pkey1 = EVP_PKEY_dup(pkey);
+ if (pkey == NULL || !OSSL_CMP_CTX_set0_newPkey(ctx, 0 /* priv */, pkey1)) {
+ EVP_PKEY_free(pkey1);
+ CMP_err1("failed to get fallback public key obtained from ir/cr/kur file '%s'",
+ file);
+ goto err;
+ }
+ res = 1;
+
+ err:
+ OSSL_CMP_MSG_free(req);
+ return res;
+}
+
/*
- * set up IR/CR/KUR/CertConf/RR specific parts of the OSSL_CMP_CTX
- * based on options from config file/CLI.
- * Returns pointer on success, NULL on error
+ * Set up IR/CR/P10CR/KUR/CertConf/RR/GENM specific parts of the OSSL_CMP_CTX
+ * based on options from CLI and/or config file.
+ * Returns 1 on success, 0 on error
*/
static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
{
@@ -1527,47 +1699,67 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
&& opt_cmd != CMP_RR && opt_cmd != CMP_GENM)
CMP_warn("no -subject given; no -csr or -oldcert or -cert available for fallback");
+ if (!set_name(opt_issuer, OSSL_CMP_CTX_set1_issuer, ctx, "issuer"))
+ return 0;
if (opt_cmd == CMP_IR || opt_cmd == CMP_CR || opt_cmd == CMP_KUR) {
- if (opt_newkey == NULL
+ if (opt_reqin == NULL && opt_newkey == NULL && !opt_centralkeygen
&& opt_key == NULL && opt_csr == NULL && opt_oldcert == NULL) {
- CMP_err("missing -newkey (or -key) to be certified and no -csr, -oldcert, or -cert given for fallback public key");
+ CMP_err("missing -newkey (or -key) to be certified and no -csr, -oldcert, -cert, or -reqin option given, which could provide fallback public key."
+ " Neither central key generation is requested.");
return 0;
}
+ if (opt_popo == OSSL_CRMF_POPO_NONE && !opt_centralkeygen) {
+ CMP_info("POPO is disabled, which implies -centralkeygen");
+ opt_centralkeygen = 1;
+ }
+ if (opt_centralkeygen) {
+ if (opt_popo > OSSL_CRMF_POPO_NONE) {
+ CMP_err1("-popo value %d is inconsistent with -centralkeygen", opt_popo);
+ return 0;
+ }
+ if (opt_newkeyout == NULL) {
+ CMP_err("-newkeyout not given, nowhere to save centrally generated key");
+ return 0;
+ }
+ opt_popo = OSSL_CRMF_POPO_NONE;
+ }
if (opt_newkey == NULL
&& opt_popo != OSSL_CRMF_POPO_NONE
&& opt_popo != OSSL_CRMF_POPO_RAVERIFIED) {
if (opt_csr != NULL) {
- CMP_err1("no -newkey option given with private key for POPO, -csr option only provides public key%s",
- opt_key == NULL ? "" :
- ", and -key option superseded by by -csr");
+ CMP_err1("no -newkey option given with private key for POPO, -csr option provides just public key%s",
+ opt_key == NULL ? "" :
+ ", and -key option superseded by -csr");
+ if (opt_reqin != NULL)
+ CMP_info("since -reqin is used, may use -popo -1 or -popo 0 to disable the needless generation of a POPO");
return 0;
}
if (opt_key == NULL) {
- CMP_err("missing -newkey (or -key) option for POPO");
+ CMP_err("missing -newkey (or -key) option for key to be certified and for POPO");
return 0;
}
}
- if (opt_certout == NULL) {
+ if (opt_certout == NULL && opt_reqout_only == NULL) {
CMP_err("-certout not given, nowhere to save newly enrolled certificate");
return 0;
}
- if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject")
- || !set_name(opt_issuer, OSSL_CMP_CTX_set1_issuer, ctx, "issuer"))
+ if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject"))
return 0;
} else {
const char *msg = "option is ignored for commands other than 'ir', 'cr', and 'kur'";
if (opt_subject != NULL) {
if (opt_ref == NULL && opt_cert == NULL) {
- /* use subject as default sender unless oldcert subject is used */
+ /* will use subject as sender unless oldcert subject is used */
if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject"))
return 0;
} else {
- CMP_warn1("-subject %s since -ref or -cert is given", msg);
+ CMP_warn1("-subject %s since sender is taken from -ref or -cert",
+ msg);
}
}
- if (opt_issuer != NULL)
- CMP_warn1("-issuer %s", msg);
+ if (opt_issuer != NULL && opt_cmd != CMP_RR)
+ CMP_warn1("-issuer %s and 'rr'", msg);
if (opt_reqexts != NULL)
CMP_warn1("-reqexts %s", msg);
if (opt_san_nodefault)
@@ -1578,6 +1770,22 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
CMP_warn1("-policies %s", msg);
if (opt_policy_oids != NULL)
CMP_warn1("-policy_oids %s", msg);
+ if (opt_popo != OSSL_CRMF_POPO_NONE - 1)
+ CMP_warn1("-popo %s", msg);
+ if (opt_centralkeygen)
+ CMP_warn1("-popo -1 or -centralkeygen %s", msg);
+ if (opt_newkeyout != NULL)
+ CMP_warn1("-newkeyout %s", msg);
+ if (opt_cmd != CMP_P10CR) {
+ if (opt_implicit_confirm)
+ CMP_warn1("-implicit_confirm %s, and 'p10cr'", msg);
+ if (opt_disable_confirm)
+ CMP_warn1("-disable_confirm %s, and 'p10cr'", msg);
+ if (opt_certout != NULL)
+ CMP_warn1("-certout %s, and 'p10cr'", msg);
+ if (opt_chainout != NULL)
+ CMP_warn1("-chainout %s, and 'p10cr'", msg);
+ }
}
if (opt_cmd == CMP_KUR) {
char *ref_cert = opt_oldcert != NULL ? opt_oldcert : opt_cert;
@@ -1591,12 +1799,44 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
opt_subject, ref_cert != NULL ? ref_cert : opt_csr);
}
if (opt_cmd == CMP_RR) {
- if (opt_oldcert == NULL && opt_csr == NULL) {
- CMP_err("missing -oldcert for certificate to be revoked and no -csr given");
- return 0;
+ if (opt_issuer == NULL && opt_serial == NULL) {
+ if (opt_oldcert == NULL && opt_csr == NULL) {
+ CMP_err("missing -oldcert or -issuer and -serial for certificate to be revoked and no -csr given");
+ return 0;
+ }
+ if (opt_oldcert != NULL && opt_csr != NULL)
+ CMP_warn("ignoring -csr since certificate to be revoked is given");
+ } else {
+#define OSSL_CMP_RR_MSG "since -issuer and -serial is given for command 'rr'"
+ if (opt_issuer == NULL || opt_serial == NULL) {
+ CMP_err("Must give both -issuer and -serial options or neither");
+ return 0;
+ }
+ if (opt_oldcert != NULL)
+ CMP_warn("Ignoring -oldcert " OSSL_CMP_RR_MSG);
+ if (opt_csr != NULL)
+ CMP_warn("Ignoring -csr " OSSL_CMP_RR_MSG);
}
- if (opt_oldcert != NULL && opt_csr != NULL)
- CMP_warn("ignoring -csr since certificate to be revoked is given");
+ if (opt_serial != NULL) {
+ ASN1_INTEGER *sno;
+
+ if ((sno = s2i_ASN1_INTEGER(NULL, opt_serial)) == NULL) {
+ CMP_err1("cannot read serial number: '%s'", opt_serial);
+ return 0;
+ }
+ if (!OSSL_CMP_CTX_set1_serialNumber(ctx, sno)) {
+ ASN1_INTEGER_free(sno);
+ CMP_err("out of memory");
+ return 0;
+ }
+ ASN1_INTEGER_free(sno);
+ }
+ if (opt_revreason > CRL_REASON_NONE)
+ (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_REVOCATION_REASON,
+ opt_revreason);
+ } else {
+ if (opt_serial != NULL)
+ CMP_warn("Ignoring -serial for command other than 'rr'");
}
if (opt_cmd == CMP_P10CR && opt_csr == NULL) {
CMP_err("missing PKCS#10 CSR for p10cr");
@@ -1605,19 +1845,21 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
if (opt_recipient == NULL && opt_srvcert == NULL && opt_issuer == NULL
&& opt_oldcert == NULL && opt_cert == NULL)
- CMP_warn("missing -recipient, -srvcert, -issuer, -oldcert or -cert; recipient will be set to \"NULL-DN\"");
+ CMP_warn("missing -recipient, -srvcert, -issuer, -oldcert or -cert; recipient for any requests not covered by -reqin will be set to \"NULL-DN\"");
- if (opt_cmd == CMP_P10CR || opt_cmd == CMP_RR) {
- const char *msg = "option is ignored for 'p10cr' and 'rr' commands";
+ if (opt_cmd == CMP_P10CR || opt_cmd == CMP_RR || opt_cmd == CMP_GENM) {
+ const char *msg = "option is ignored for 'p10cr', 'rr', and 'genm' commands";
if (opt_newkeypass != NULL)
- CMP_warn1("-newkeytype %s", msg);
+ CMP_warn1("-newkeypass %s", msg);
if (opt_newkey != NULL)
CMP_warn1("-newkey %s", msg);
if (opt_days != 0)
CMP_warn1("-days %s", msg);
if (opt_popo != OSSL_CRMF_POPO_NONE - 1)
CMP_warn1("-popo %s", msg);
+ if (opt_out_trusted != NULL)
+ CMP_warn1("-out_trusted %s", msg);
} else if (opt_newkey != NULL) {
const char *file = opt_newkey;
const int format = opt_keyform;
@@ -1633,16 +1875,21 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
if (pkey == NULL) {
ERR_clear_error();
desc = opt_csr == NULL
- ? "fallback public key for cert to be enrolled"
- : "public key for checking cert resulting from p10cr";
+ ? "fallback public key for cert to be enrolled"
+ : "public key for checking cert resulting from p10cr";
pkey = load_pubkey(file, format, 0, pass, engine, desc);
priv = 0;
}
- cleanse(opt_newkeypass);
+
if (pkey == NULL || !OSSL_CMP_CTX_set0_newPkey(ctx, priv, pkey)) {
EVP_PKEY_free(pkey);
return 0;
}
+ } else if (opt_reqin != NULL
+ && opt_key == NULL && opt_csr == NULL && opt_oldcert == NULL
+ && !opt_centralkeygen) {
+ if (!set_fallback_pubkey(ctx))
+ return 0;
}
if (opt_days > 0
@@ -1659,9 +1906,10 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
if (opt_csr != NULL) {
if (opt_cmd == CMP_GENM) {
- CMP_warn("-csr option is ignored for command 'genm'");
+ CMP_warn("-csr option is ignored for 'genm' command");
} else {
- if ((csr = load_csr_autofmt(opt_csr, "PKCS#10 CSR")) == NULL)
+ csr = load_csr_autofmt(opt_csr, FORMAT_UNDEF, NULL, "PKCS#10 CSR");
+ if (csr == NULL)
return 0;
if (!OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
goto oom;
@@ -1715,9 +1963,11 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
char *next = next_item(opt_policy_oids);
if ((policy = OBJ_txt2obj(opt_policy_oids, 1)) == 0) {
- CMP_err1("unknown policy OID '%s'", opt_policy_oids);
+ CMP_err1("Invalid -policy_oids arg '%s'", opt_policy_oids);
return 0;
}
+ if (OBJ_obj2nid(policy) == NID_undef)
+ CMP_warn1("Unknown -policy_oids arg: %.40s", opt_policy_oids);
if ((pinfo = POLICYINFO_new()) == NULL) {
ASN1_OBJECT_free(policy);
@@ -1732,36 +1982,23 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
}
opt_policy_oids = next;
}
-
if (opt_popo >= OSSL_CRMF_POPO_NONE)
(void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POPO_METHOD, opt_popo);
if (opt_oldcert != NULL) {
if (opt_cmd == CMP_GENM) {
- CMP_warn("-oldcert option is ignored for command 'genm'");
+ CMP_warn("-oldcert option is ignored for 'genm' command");
} else {
- X509 *oldcert = load_cert_pwd(opt_oldcert, opt_keypass,
- opt_cmd == CMP_KUR ?
- "certificate to be updated" :
- opt_cmd == CMP_RR ?
- "certificate to be revoked" :
- "reference certificate (oldcert)");
- /* opt_keypass needed if opt_oldcert is an encrypted PKCS#12 file */
-
- if (oldcert == NULL)
- return 0;
- if (!OSSL_CMP_CTX_set1_oldCert(ctx, oldcert)) {
- X509_free(oldcert);
- CMP_err("out of memory");
+ if (!setup_cert(ctx, opt_oldcert, opt_keypass,
+ /* needed if opt_oldcert is encrypted PKCS12 file */
+ opt_cmd == CMP_KUR ? "certificate to be updated" :
+ opt_cmd == CMP_RR ? "certificate to be revoked" :
+ "reference certificate (oldcert)",
+ (add_X509_fn_t)OSSL_CMP_CTX_set1_oldCert))
return 0;
- }
- X509_free(oldcert);
}
}
cleanse(opt_keypass);
- if (opt_revreason > CRL_REASON_NONE)
- (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_REVOCATION_REASON,
- opt_revreason);
return 1;
@@ -1773,69 +2010,130 @@ static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
return 0;
}
-static int handle_opt_geninfo(OSSL_CMP_CTX *ctx)
+static int add_certProfile(OSSL_CMP_CTX *ctx, const char *name)
{
- long value;
- ASN1_OBJECT *type;
- ASN1_INTEGER *aint;
- ASN1_TYPE *val;
- OSSL_CMP_ITAV *itav;
- char *endstr;
- char *valptr = strchr(opt_geninfo, ':');
+ OSSL_CMP_ITAV *itav = NULL;
+ STACK_OF(ASN1_UTF8STRING) *sk;
+ ASN1_UTF8STRING *utf8string;
- if (valptr == NULL) {
- CMP_err("missing ':' in -geninfo option");
+ if (ctx == NULL || name == NULL)
return 0;
- }
- valptr[0] = '\0';
- valptr++;
- if (OPENSSL_strncasecmp(valptr, "int:", 4) != 0) {
- CMP_err("missing 'int:' in -geninfo option");
+ if ((sk = sk_ASN1_UTF8STRING_new_reserve(NULL, 1)) == NULL)
return 0;
+ if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
+ goto err;
+ if (!ASN1_STRING_set(utf8string, name, (int)strlen(name))) {
+ ASN1_STRING_free(utf8string);
+ goto err;
}
- valptr += 4;
+ /* Due to sk_ASN1_UTF8STRING_new_reserve(NULL, 1), this surely succeeds: */
+ (void)sk_ASN1_UTF8STRING_push(sk, utf8string);
+ if ((itav = OSSL_CMP_ITAV_new0_certProfile(sk)) == NULL)
+ goto err;
+ if (OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav))
+ return 1;
+ OSSL_CMP_ITAV_free(itav);
+ return 0;
- value = strtol(valptr, &endstr, 10);
- if (endstr == valptr || *endstr != '\0') {
- CMP_err("cannot parse int in -geninfo option");
- return 0;
- }
+ err:
+ sk_ASN1_UTF8STRING_pop_free(sk, ASN1_UTF8STRING_free);
+ return 0;
+}
- type = OBJ_txt2obj(opt_geninfo, 1);
- if (type == NULL) {
- CMP_err("cannot parse OID in -geninfo option");
- return 0;
- }
+static int handle_opt_geninfo(OSSL_CMP_CTX *ctx)
+{
+ ASN1_OBJECT *obj = NULL;
+ ASN1_TYPE *type = NULL;
+ long value;
+ ASN1_INTEGER *aint = NULL;
+ ASN1_UTF8STRING *text = NULL;
+ OSSL_CMP_ITAV *itav;
+ char *ptr = opt_geninfo, *oid, *end;
+
+ do {
+ while (isspace(_UC(*ptr)))
+ ptr++;
+ oid = ptr;
+ if ((ptr = strchr(oid, ':')) == NULL) {
+ CMP_err1("Missing ':' in -geninfo arg %.40s", oid);
+ return 0;
+ }
+ *ptr++ = '\0';
+ if ((obj = OBJ_txt2obj(oid, 0)) == NULL) {
+ CMP_err1("Invalid OID in -geninfo arg %.40s", oid);
+ return 0;
+ }
+ if (OBJ_obj2nid(obj) == NID_undef)
+ CMP_warn1("Unknown OID in -geninfo arg: %.40s", oid);
+ if ((type = ASN1_TYPE_new()) == NULL)
+ goto oom;
- if ((aint = ASN1_INTEGER_new()) == NULL)
- goto oom;
+ if (CHECK_AND_SKIP_CASE_PREFIX(ptr, "int:")) {
+ value = strtol(ptr, &end, 10);
+ if (end == ptr) {
+ CMP_err1("Cannot parse int in -geninfo arg %.40s", ptr);
+ goto err;
+ }
+ ptr = end;
+ if (*ptr != '\0') {
+ if (*ptr != ',') {
+ CMP_err1("Missing ',' or end of -geninfo arg after int at %.40s",
+ ptr);
+ goto err;
+ }
+ ptr++;
+ }
- val = ASN1_TYPE_new();
- if (!ASN1_INTEGER_set(aint, value) || val == NULL) {
- ASN1_INTEGER_free(aint);
- goto oom;
- }
- ASN1_TYPE_set(val, V_ASN1_INTEGER, aint);
- itav = OSSL_CMP_ITAV_create(type, val);
- if (itav == NULL) {
- ASN1_TYPE_free(val);
- goto oom;
- }
+ if ((aint = ASN1_INTEGER_new()) == NULL
+ || !ASN1_INTEGER_set(aint, value))
+ goto oom;
+ ASN1_TYPE_set(type, V_ASN1_INTEGER, aint);
+ aint = NULL;
+
+ } else if (CHECK_AND_SKIP_CASE_PREFIX(ptr, "str:")) {
+ end = strchr(ptr, ',');
+ if (end == NULL)
+ end = ptr + strlen(ptr);
+ else
+ *end++ = '\0';
+ if ((text = ASN1_UTF8STRING_new()) == NULL
+ || !ASN1_STRING_set(text, ptr, -1))
+ goto oom;
+ ptr = end;
+ ASN1_TYPE_set(type, V_ASN1_UTF8STRING, text);
+ text = NULL;
- if (!OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav)) {
- OSSL_CMP_ITAV_free(itav);
- return 0;
- }
+ } else {
+ CMP_err1("Missing 'int:' or 'str:' in -geninfo arg %.40s", ptr);
+ goto err;
+ }
+
+ if ((itav = OSSL_CMP_ITAV_create(obj, type)) == NULL) {
+ CMP_err("Unable to create 'OSSL_CMP_ITAV' structure");
+ goto err;
+ }
+ obj = NULL;
+ type = NULL;
+
+ if (!OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav)) {
+ CMP_err("Failed to add ITAV for geninfo of the PKI message header");
+ OSSL_CMP_ITAV_free(itav);
+ return 0;
+ }
+ } while (*ptr != '\0');
return 1;
oom:
- ASN1_OBJECT_free(type);
CMP_err("out of memory");
+ err:
+ ASN1_OBJECT_free(obj);
+ ASN1_TYPE_free(type);
+ ASN1_INTEGER_free(aint);
+ ASN1_UTF8STRING_free(text);
return 0;
}
-
/*
* set up the client-side OSSL_CMP_CTX based on options from config file/CLI
* while parsing options and checking their consistency.
@@ -1846,17 +2144,19 @@ static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
{
int ret = 0;
char *host = NULL, *port = NULL, *path = NULL, *used_path = opt_path;
-#ifndef OPENSSL_NO_SOCK
- int portnum, ssl;
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
+ int portnum, use_ssl;
static char server_port[32] = { '\0' };
const char *proxy_host = NULL;
#endif
char server_buf[200] = "mock server";
char proxy_buf[200] = "";
+ if (!opt_use_mock_srv)
+ strcpy(server_buf, "no server");
if (!opt_use_mock_srv && opt_rspin == NULL) { /* note: -port is not given */
-#ifndef OPENSSL_NO_SOCK
- if (opt_server == NULL) {
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
+ if (opt_server == NULL && opt_reqout_only == NULL) {
CMP_err("missing -server or -use_mock_srv or -rspin option");
goto err;
}
@@ -1865,27 +2165,26 @@ static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
goto err;
#endif
}
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
if (opt_server == NULL) {
if (opt_proxy != NULL)
CMP_warn("ignoring -proxy option since -server is not given");
if (opt_no_proxy != NULL)
CMP_warn("ignoring -no_proxy option since -server is not given");
- if (opt_tls_used) {
- CMP_warn("ignoring -tls_used option since -server is not given");
- opt_tls_used = 0;
- }
goto set_path;
}
- if (!OSSL_HTTP_parse_url(opt_server, &ssl, NULL /* user */, &host, &port,
- &portnum, &path, NULL /* q */, NULL /* frag */)) {
+ if (!OSSL_HTTP_parse_url(opt_server, &use_ssl, NULL /* user */,
+ &host, &port, &portnum,
+ &path, NULL /* q */, NULL /* frag */)) {
CMP_err1("cannot parse -server URL: %s", opt_server);
goto err;
}
- if (ssl && !opt_tls_used) {
- CMP_err("missing -tls_used option since -server URL indicates https");
- goto err;
+ if (use_ssl && !opt_tls_used) {
+ CMP_warn("assuming -tls_used since -server URL indicates HTTPS");
+ opt_tls_used = 1;
}
+ if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_USE_TLS, opt_tls_used))
+ goto err;
BIO_snprintf(server_port, sizeof(server_port), "%s", port);
if (opt_path == NULL)
@@ -1901,7 +2200,7 @@ static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
opt_tls_used ? "s" : "", host, port,
*used_path == '/' ? used_path + 1 : used_path);
- proxy_host = OSSL_HTTP_adapt_proxy(opt_proxy, opt_no_proxy, host, ssl);
+ proxy_host = OSSL_HTTP_adapt_proxy(opt_proxy, opt_no_proxy, host, use_ssl);
if (proxy_host != NULL)
(void)BIO_snprintf(proxy_buf, sizeof(proxy_buf), " via %s", proxy_host);
@@ -1913,7 +2212,12 @@ static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
if (!transform_opts())
goto err;
- if (opt_infotype_s != NULL) {
+ if (opt_infotype_s == NULL) {
+ if (opt_cmd == CMP_GENM)
+ CMP_warn("no -infotype option given for genm");
+ } else if (opt_cmd != CMP_GENM) {
+ CMP_warn("-infotype option is ignored for commands other than 'genm'");
+ } else {
char id_buf[100] = "id-it-";
strncat(id_buf, opt_infotype_s, sizeof(id_buf) - strlen(id_buf) - 1);
@@ -1922,6 +2226,29 @@ static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
goto err;
}
}
+ if (opt_cmd != CMP_GENM || opt_infotype != NID_id_it_rootCaCert) {
+ const char *msg = "option is ignored unless -cmd 'genm' and -infotype rootCaCert is given";
+
+ if (opt_oldwithold != NULL)
+ CMP_warn1("-oldwithold %s", msg);
+ if (opt_newwithnew != NULL)
+ CMP_warn1("-newwithnew %s", msg);
+ if (opt_newwithold != NULL)
+ CMP_warn1("-newwithold %s", msg);
+ if (opt_oldwithnew != NULL)
+ CMP_warn1("-oldwithnew %s", msg);
+ }
+ if (opt_cmd != CMP_GENM || opt_infotype != NID_id_it_certReqTemplate) {
+ const char *msg = "option is ignored unless -cmd 'genm' and -infotype 'certReqTemplate' is given";
+
+ if (opt_template != NULL)
+ CMP_warn1("-template %s", msg);
+ if (opt_keyspec != NULL)
+ CMP_warn1("-keyspec %s", msg);
+ } else {
+ if (opt_template == NULL)
+ CMP_err("missing -template option for genm with infotype certReqTemplate");
+ }
if (!setup_verification_ctx(ctx))
goto err;
@@ -1953,7 +2280,7 @@ static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
|| opt_rspin != NULL || opt_rspout != NULL || opt_use_mock_srv)
(void)OSSL_CMP_CTX_set_transfer_cb(ctx, read_write_req_resp);
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
if (opt_tls_used) {
APP_HTTP_TLS_INFO *info;
@@ -1970,6 +2297,7 @@ static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
if ((info = OPENSSL_zalloc(sizeof(*info))) == NULL)
goto err;
+ APP_HTTP_TLS_INFO_free(OSSL_CMP_CTX_get_http_cb_arg(ctx));
(void)OSSL_CMP_CTX_set_http_cb_arg(ctx, info);
info->ssl_ctx = setup_ssl_ctx(ctx, host, engine);
info->server = host;
@@ -1999,14 +2327,14 @@ static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
if (opt_geninfo != NULL && !handle_opt_geninfo(ctx))
goto err;
+ if (opt_profile != NULL && !add_certProfile(ctx, opt_profile))
+ goto err;
/* not printing earlier, to minimize confusion in case setup fails before */
- if (opt_rspin != NULL)
- CMP_info2("will contact %s%s "
- "only if -rspin argument gives too few filenames",
- server_buf, proxy_buf);
- else
- CMP_info2("will contact %s%s", server_buf, proxy_buf);
+ if (opt_reqout_only == NULL)
+ CMP_info3("will contact %s%s%s ", server_buf, proxy_buf,
+ opt_rspin == NULL ? "" :
+ " only if -rspin argument gives too few filenames");
ret = 1;
@@ -2037,37 +2365,54 @@ static int write_cert(BIO *bio, X509 *cert)
return 0;
}
+static int write_crl(BIO *bio, X509_CRL *crl)
+{
+ if (opt_crlform != FORMAT_PEM && opt_crlform != FORMAT_ASN1) {
+ BIO_printf(bio_err, "error: unsupported type '%s' for writing CRLs\n",
+ opt_crlform_s);
+ return 0;
+ }
+
+ return opt_crlform == FORMAT_PEM ? PEM_write_bio_X509_CRL(bio, crl)
+ : i2d_X509_CRL_bio(bio, crl);
+}
+
/*
- * If destFile != NULL writes out a stack of certs to the given file.
- * In any case frees the certs.
+ * If file != NULL writes out a stack of certs to the given file.
+ * If certs is NULL, the file is emptied.
+ * Frees the certs if present.
* Depending on options use either PEM or DER format,
* where DER does not make much sense for writing more than one cert!
* Returns number of written certificates on success, -1 on error.
*/
-static int save_free_certs(OSSL_CMP_CTX *ctx,
- STACK_OF(X509) *certs, char *destFile, char *desc)
+static int save_free_certs(STACK_OF(X509) *certs,
+ const char *file, const char *desc)
{
BIO *bio = NULL;
int i;
- int n = sk_X509_num(certs);
+ int n = sk_X509_num(certs /* may be NULL */);
- if (destFile == NULL)
+ if (n < 0)
+ n = 0;
+ if (file == NULL)
goto end;
- CMP_info3("received %d %s certificate(s), saving to file '%s'",
- n, desc, destFile);
+ if (certs != NULL)
+ CMP_info3("received %d %s certificate(s), saving to file '%s'",
+ n, desc, file);
if (n > 1 && opt_certform != FORMAT_PEM)
CMP_warn("saving more than one certificate in non-PEM format");
- if (destFile == NULL || (bio = BIO_new(BIO_s_file())) == NULL
- || !BIO_write_filename(bio, (char *)destFile)) {
- CMP_err1("could not open file '%s' for writing", destFile);
+ if ((bio = BIO_new(BIO_s_file())) == NULL
+ || !BIO_write_filename(bio, (char *)file)) {
+ CMP_err3("could not open file '%s' for %s %s certificate(s)",
+ file, certs == NULL ? "deleting" : "writing", desc);
n = -1;
goto end;
}
for (i = 0; i < n; i++) {
if (!write_cert(bio, sk_X509_value(certs, i))) {
- CMP_err1("cannot write certificate to file '%s'", destFile);
+ CMP_err2("cannot write %s certificate to file '%s'", desc, file);
n = -1;
goto end;
}
@@ -2075,32 +2420,158 @@ static int save_free_certs(OSSL_CMP_CTX *ctx,
end:
BIO_free(bio);
- sk_X509_pop_free(certs, X509_free);
+ OSSL_STACK_OF_X509_free(certs);
return n;
}
-static void print_itavs(STACK_OF(OSSL_CMP_ITAV) *itavs)
+static int save_crl(X509_CRL *crl,
+ const char *file, const char *desc)
{
- OSSL_CMP_ITAV *itav = NULL;
- char buf[128];
- int i, r;
- int n = sk_OSSL_CMP_ITAV_num(itavs); /* itavs == NULL leads to 0 */
+ BIO *bio = NULL;
+ int res = 0;
- if (n == 0) {
- CMP_info("genp contains no ITAV");
- return;
+ if (file == NULL)
+ return 1;
+ if (crl != NULL)
+ CMP_info2("received %s, saving to file '%s'", desc, file);
+
+ if ((bio = BIO_new(BIO_s_file())) == NULL
+ || !BIO_write_filename(bio, (char *)file)) {
+ CMP_err2("could not open file '%s' for writing %s",
+ file, desc);
+ goto end;
}
- for (i = 0; i < n; i++) {
- itav = sk_OSSL_CMP_ITAV_value(itavs, i);
- r = OBJ_obj2txt(buf, 128, OSSL_CMP_ITAV_get0_type(itav), 0);
- if (r < 0)
- CMP_err("could not get ITAV details");
- else if (r == 0)
- CMP_info("genp contains empty ITAV");
- else
- CMP_info1("genp contains ITAV of type: %s", buf);
+ if (!write_crl(bio, crl)) {
+ CMP_err2("cannot write %s to file '%s'", desc, file);
+ goto end;
}
+ res = 1;
+
+ end:
+ BIO_free(bio);
+ return res;
+}
+
+static int delete_file(const char *file, const char *desc)
+{
+ if (file == NULL)
+ return 1;
+
+ if (unlink(file) != 0 && errno != ENOENT) {
+ CMP_err2("Failed to delete %s, which should be done to indicate there is no %s",
+ file, desc);
+ return 0;
+ }
+ return 1;
+}
+
+static int save_cert_or_delete(X509 *cert, const char *file, const char *desc)
+{
+ if (file == NULL)
+ return 1;
+ if (cert == NULL) {
+ char desc_cert[80];
+
+ BIO_snprintf(desc_cert, sizeof(desc_cert), "%s certificate", desc);
+ return delete_file(file, desc_cert);
+ } else {
+ STACK_OF(X509) *certs = sk_X509_new_null();
+
+ if (!X509_add_cert(certs, cert, X509_ADD_FLAG_UP_REF)) {
+ sk_X509_free(certs);
+ return 0;
+ }
+ return save_free_certs(certs, file, desc) >= 0;
+ }
+}
+
+static int save_crl_or_delete(X509_CRL *crl, const char *file, const char *desc)
+{
+ if (file == NULL)
+ return 1;
+ return (crl == NULL) ? delete_file(file, desc) : save_crl(crl, file, desc);
+}
+
+static int save_template(const char *file, const OSSL_CRMF_CERTTEMPLATE *tmpl)
+{
+ BIO *bio = BIO_new_file(file, "wb");
+
+ if (bio == NULL) {
+ CMP_err1("error saving certTemplate from genp: cannot open file %s",
+ file);
+ return 0;
+ }
+ if (!ASN1_i2d_bio_of(OSSL_CRMF_CERTTEMPLATE, i2d_OSSL_CRMF_CERTTEMPLATE,
+ bio, tmpl)) {
+ CMP_err1("error saving certTemplate from genp: cannot write file %s",
+ file);
+ return 0;
+ } else {
+ CMP_info1("stored certTemplate from genp to file '%s'", file);
+ }
+ BIO_free(bio);
+ return 1;
+}
+
+static int save_keyspec(const char *file, const OSSL_CMP_ATAVS *keyspec)
+{
+ BIO *bio = BIO_new_file(file, "wb");
+
+ if (bio == NULL) {
+ CMP_err1("error saving keySpec from genp: cannot open file %s", file);
+ return 0;
+ }
+
+ if (!ASN1_i2d_bio_of(OSSL_CMP_ATAVS, i2d_OSSL_CMP_ATAVS, bio, keyspec)) {
+ CMP_err1("error saving keySpec from genp: cannot write file %s", file);
+ return 0;
+ } else {
+ CMP_info1("stored keySpec from genp to file '%s'", file);
+ }
+ BIO_free(bio);
+ return 1;
+}
+
+static const char *nid_name(int nid)
+{
+ const char *name = OBJ_nid2ln(nid);
+
+ if (name == NULL)
+ name = OBJ_nid2sn(nid);
+ if (name == NULL)
+ name = "<unknown OID>";
+ return name;
+}
+
+static int print_itavs(const STACK_OF(OSSL_CMP_ITAV) *itavs)
+{
+ int i, ret = 1;
+ int n = sk_OSSL_CMP_ITAV_num(itavs);
+
+ if (n <= 0) { /* also in case itavs == NULL */
+ CMP_info("genp does not contain any ITAV");
+ return ret;
+ }
+
+ for (i = 1; i <= n; i++) {
+ OSSL_CMP_ITAV *itav = sk_OSSL_CMP_ITAV_value(itavs, i - 1);
+ ASN1_OBJECT *type = OSSL_CMP_ITAV_get0_type(itav);
+ char name[80];
+
+ if (itav == NULL) {
+ CMP_err1("could not get ITAV #%d from genp", i);
+ ret = 0;
+ continue;
+ }
+ if (i2t_ASN1_OBJECT(name, sizeof(name), type) <= 0) {
+ CMP_err1("error parsing type of ITAV #%d from genp", i);
+ ret = 0;
+ } else {
+ CMP_info2("ITAV #%d from genp infoType=%s", i, name);
+ }
+ }
+ return ret;
}
static char opt_item[SECTION_NAME_MAX + 1];
@@ -2146,9 +2617,8 @@ static char *conf_get_string(const CONF *src_conf, const char *groups,
const char *end = groups + strlen(groups);
while ((end = prev_item(groups, end)) != NULL) {
- if ((res = NCONF_get_string(src_conf, opt_item, name)) != NULL)
+ if ((res = app_conf_try_string(src_conf, opt_item, name)) != NULL)
return res;
- ERR_clear_error();
}
return res;
}
@@ -2197,9 +2667,9 @@ static int read_config(void)
|| !strcmp(opt->name, OPT_MORE_STR))
n_options--;
OPENSSL_assert(OSSL_NELEM(cmp_vars) == n_options
- + OPT_PROV__FIRST + 1 - OPT_PROV__LAST
- + OPT_R__FIRST + 1 - OPT_R__LAST
- + OPT_V__FIRST + 1 - OPT_V__LAST);
+ + OPT_PROV__FIRST + 1 - OPT_PROV__LAST
+ + OPT_R__FIRST + 1 - OPT_R__LAST
+ + OPT_V__FIRST + 1 - OPT_V__LAST);
for (opt = &cmp_options[start_opt], i = start_idx;
opt->name != NULL; i++, opt++) {
int provider_option = (OPT_PROV__FIRST <= opt->retval
@@ -2346,7 +2816,7 @@ static int get_opts(int argc, char **argv)
if (!set_verbosity(opt_int_arg()))
goto opthelp;
break;
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
case OPT_SERVER:
opt_server = opt_str();
break;
@@ -2376,7 +2846,7 @@ static int get_opts(int argc, char **argv)
case OPT_TOTAL_TIMEOUT:
opt_total_timeout = opt_int_arg();
break;
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
case OPT_TLS_USED:
opt_tls_used = 1;
break;
@@ -2449,12 +2919,39 @@ static int get_opts(int argc, char **argv)
case OPT_UNPROTECTED_ERRORS:
opt_unprotected_errors = 1;
break;
+ case OPT_NO_CACHE_EXTRACERTS:
+ opt_no_cache_extracerts = 1;
+ break;
+ case OPT_SRVCERTOUT:
+ opt_srvcertout = opt_str();
+ break;
case OPT_EXTRACERTSOUT:
opt_extracertsout = opt_str();
break;
case OPT_CACERTSOUT:
opt_cacertsout = opt_str();
break;
+ case OPT_OLDWITHOLD:
+ opt_oldwithold = opt_str();
+ break;
+ case OPT_NEWWITHNEW:
+ opt_newwithnew = opt_str();
+ break;
+ case OPT_NEWWITHOLD:
+ opt_newwithold = opt_str();
+ break;
+ case OPT_OLDWITHNEW:
+ opt_oldwithnew = opt_str();
+ break;
+ case OPT_CRLCERT:
+ opt_crlcert = opt_str();
+ break;
+ case OPT_OLDCRL:
+ opt_oldcrl = opt_str();
+ break;
+ case OPT_CRLOUT:
+ opt_crlout = opt_str();
+ break;
case OPT_V_CASES:
if (!opt_verify(o, vpm))
@@ -2466,22 +2963,33 @@ static int get_opts(int argc, char **argv)
case OPT_INFOTYPE:
opt_infotype_s = opt_str();
break;
+ case OPT_PROFILE:
+ opt_profile = opt_str();
+ break;
case OPT_GENINFO:
opt_geninfo = opt_str();
break;
-
+ case OPT_TEMPLATE:
+ opt_template = opt_str();
+ break;
+ case OPT_KEYSPEC:
+ opt_keyspec = opt_str();
+ break;
case OPT_NEWKEY:
opt_newkey = opt_str();
break;
case OPT_NEWKEYPASS:
opt_newkeypass = opt_str();
break;
+ case OPT_CENTRALKEYGEN:
+ opt_centralkeygen = 1;
+ break;
+ case OPT_NEWKEYOUT:
+ opt_newkeyout = opt_str();
+ break;
case OPT_SUBJECT:
opt_subject = opt_str();
break;
- case OPT_ISSUER:
- opt_issuer = opt_str();
- break;
case OPT_DAYS:
opt_days = opt_int_arg();
break;
@@ -2534,16 +3042,25 @@ static int get_opts(int argc, char **argv)
break;
case OPT_REVREASON:
opt_revreason = opt_int_arg();
- if (opt_revreason < CRL_REASON_NONE
+ if (opt_revreason < CRL_REASON_NONE
|| opt_revreason > CRL_REASON_AA_COMPROMISE
|| opt_revreason == 7) {
CMP_err("invalid revreason. Valid values are -1 .. 6, 8 .. 10");
goto opthelp;
}
break;
+ case OPT_ISSUER:
+ opt_issuer = opt_str();
+ break;
+ case OPT_SERIAL:
+ opt_serial = opt_str();
+ break;
case OPT_CERTFORM:
opt_certform_s = opt_str();
break;
+ case OPT_CRLFORM:
+ opt_crlform_s = opt_str();
+ break;
case OPT_KEYFORM:
opt_keyform_s = opt_str();
break;
@@ -2579,6 +3096,9 @@ static int get_opts(int argc, char **argv)
case OPT_REQOUT:
opt_reqout = opt_str();
break;
+ case OPT_REQOUT_ONLY:
+ opt_reqout_only = opt_str();
+ break;
case OPT_RSPIN:
opt_rspin = opt_str();
break;
@@ -2589,7 +3109,7 @@ static int get_opts(int argc, char **argv)
opt_use_mock_srv = 1;
break;
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
case OPT_PORT:
opt_port = opt_str();
break;
@@ -2618,15 +3138,36 @@ static int get_opts(int argc, char **argv)
case OPT_SRV_UNTRUSTED:
opt_srv_untrusted = opt_str();
break;
+ case OPT_REF_CERT:
+ opt_ref_cert = opt_str();
+ break;
case OPT_RSP_CERT:
opt_rsp_cert = opt_str();
break;
+ case OPT_RSP_KEY:
+ opt_rsp_key = opt_str();
+ break;
+ case OPT_RSP_KEYPASS:
+ opt_rsp_keypass = opt_str();
+ break;
+ case OPT_RSP_CRL:
+ opt_rsp_crl = opt_str();
+ break;
case OPT_RSP_EXTRACERTS:
opt_rsp_extracerts = opt_str();
break;
case OPT_RSP_CAPUBS:
opt_rsp_capubs = opt_str();
break;
+ case OPT_RSP_NEWWITHNEW:
+ opt_rsp_newwithnew = opt_str();
+ break;
+ case OPT_RSP_NEWWITHOLD:
+ opt_rsp_newwithold = opt_str();
+ break;
+ case OPT_RSP_OLDWITHNEW:
+ opt_rsp_oldwithnew = opt_str();
+ break;
case OPT_POLL_COUNT:
opt_poll_count = opt_int_arg();
break;
@@ -2670,15 +3211,14 @@ static int get_opts(int argc, char **argv)
}
/* No extra args. */
- argc = opt_num_rest();
- argv = opt_rest();
- if (argc != 0)
+ if (!opt_check_rest_arg(NULL))
goto opthelp;
return 1;
}
-#ifndef OPENSSL_NO_SOCK
-static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx) {
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
+static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx)
+{
BIO *acbio;
BIO *cbio = NULL;
int keep_alive = 0;
@@ -2686,7 +3226,7 @@ static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx) {
int retry = 1;
int ret = 1;
- if ((acbio = http_server_init_bio(prog, opt_port)) == NULL)
+ if ((acbio = http_server_init(prog, opt_port, opt_verbosity)) == NULL)
return 0;
while (opt_max_msgs <= 0 || msgs < opt_max_msgs) {
char *path = NULL;
@@ -2696,10 +3236,10 @@ static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx) {
ret = http_server_get_asn1_req(ASN1_ITEM_rptr(OSSL_CMP_MSG),
(ASN1_VALUE **)&req, &path,
&cbio, acbio, &keep_alive,
- prog, opt_port, 0, 0);
+ prog, 0, 0);
if (ret == 0) { /* no request yet */
if (retry) {
- ossl_sleep(1000);
+ OSSL_sleep(1000);
retry = 0;
continue;
}
@@ -2713,7 +3253,7 @@ static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx) {
msgs++;
if (req != NULL) {
if (strcmp(path, "") != 0 && strcmp(path, "pkix/") != 0) {
- (void)http_server_send_status(cbio, 404, "Not Found");
+ (void)http_server_send_status(prog, cbio, 404, "Not Found");
CMP_err1("expecting empty path or 'pkix/' but got '%s'",
path);
OPENSSL_free(path);
@@ -2724,11 +3264,11 @@ static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx) {
resp = OSSL_CMP_CTX_server_perform(cmp_ctx, req);
OSSL_CMP_MSG_free(req);
if (resp == NULL) {
- (void)http_server_send_status(cbio,
+ (void)http_server_send_status(prog, cbio,
500, "Internal Server Error");
break; /* treated as fatal error */
}
- ret = http_server_send_asn1_resp(cbio, keep_alive,
+ ret = http_server_send_asn1_resp(prog, cbio, keep_alive,
"application/pkixcmp",
ASN1_ITEM_rptr(OSSL_CMP_MSG),
(const ASN1_VALUE *)resp);
@@ -2755,6 +3295,71 @@ static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx) {
}
#endif
+static void print_keyspec(OSSL_CMP_ATAVS *keySpec)
+{
+ const char *desc = "specifications contained in keySpec from genp";
+ BIO *mem;
+ int i;
+ const char *p;
+ long len;
+
+ if (keySpec == NULL) {
+ CMP_info1("No %s", desc);
+ return;
+ }
+
+ mem = BIO_new(BIO_s_mem());
+ if (mem == NULL) {
+ CMP_err1("Out of memory - cannot dump key %s", desc);
+ return;
+ }
+ BIO_printf(mem, "Key %s:\n", desc);
+
+ for (i = 0; i < sk_OSSL_CMP_ATAV_num(keySpec); i++) {
+ OSSL_CMP_ATAV *atav = sk_OSSL_CMP_ATAV_value(keySpec, i);
+ ASN1_OBJECT *type = OSSL_CMP_ATAV_get0_type(atav /* may be NULL */);
+ int nid = OBJ_obj2nid(type);
+
+ switch (nid) {
+ case NID_id_regCtrl_algId:
+ {
+ X509_ALGOR *alg = OSSL_CMP_ATAV_get0_algId(atav);
+ const ASN1_OBJECT *oid;
+ int paramtype;
+ const void *param;
+
+ X509_ALGOR_get0(&oid, &paramtype, &param, alg);
+ BIO_printf(mem, "Key algorithm: ");
+ i2a_ASN1_OBJECT(mem, oid);
+ if (paramtype == V_ASN1_UNDEF || alg->parameter == NULL) {
+ BIO_printf(mem, "\n");
+ } else {
+ BIO_printf(mem, " - ");
+ ASN1_item_print(mem, (ASN1_VALUE *)alg,
+ 0, ASN1_ITEM_rptr(X509_ALGOR), NULL);
+ }
+ }
+ break;
+ case NID_id_regCtrl_rsaKeyLen:
+ BIO_printf(mem, "Key algorithm: RSA %d\n",
+ OSSL_CMP_ATAV_get_rsaKeyLen(atav));
+ break;
+ default:
+ BIO_printf(mem, "Invalid key spec: %s\n", nid_name(nid));
+ break;
+ }
+ }
+ BIO_printf(mem, "End of key %s", desc);
+
+ len = BIO_get_mem_data(mem, &p);
+ if (len > INT_MAX)
+ CMP_err1("Info too large - cannot dump key %s", desc);
+ else
+ CMP_info2("%.*s", (int)len, p);
+ BIO_free(mem);
+ return;
+}
+
static void print_status(void)
{
/* print PKIStatusInfo */
@@ -2764,7 +3369,7 @@ static void print_status(void)
OSSL_CMP_CTX_snprint_PKIStatus(cmp_ctx, buf, OSSL_CMP_PKISI_BUFLEN);
const char *from = "", *server = "";
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
if (opt_server != NULL) {
from = " from ";
server = opt_server;
@@ -2784,25 +3389,198 @@ static void print_status(void)
OPENSSL_free(buf);
}
-int cmp_main(int argc, char **argv)
+static int do_genm(OSSL_CMP_CTX *ctx)
+{
+ if (opt_infotype == NID_id_it_caCerts) {
+ STACK_OF(X509) *cacerts = NULL;
+
+ if (opt_cacertsout == NULL) {
+ CMP_err("Missing -cacertsout option for -infotype caCerts");
+ return 0;
+ }
+
+ if (!OSSL_CMP_get1_caCerts(ctx, &cacerts))
+ return 0;
+
+ /* could check authorization of sender/origin at this point */
+ if (cacerts == NULL) {
+ CMP_warn("no CA certificates provided by server");
+ } else if (save_free_certs(cacerts, opt_cacertsout, "CA") < 0) {
+ CMP_err1("Failed to store CA certificates from genp in %s",
+ opt_cacertsout);
+ return 0;
+ }
+ return 1;
+ } else if (opt_infotype == NID_id_it_rootCaCert) {
+ X509 *oldwithold = NULL;
+ X509 *newwithnew = NULL;
+ X509 *newwithold = NULL;
+ X509 *oldwithnew = NULL;
+ int res = 0;
+
+ if (opt_newwithnew == NULL) {
+ CMP_err("Missing -newwithnew option for -infotype rootCaCert");
+ return 0;
+ }
+ if (opt_oldwithold == NULL) {
+ CMP_warn("No -oldwithold given, will use all certs given with -trusted as trust anchors for verifying the newWithNew cert");
+ } else {
+ oldwithold = load_cert_pwd(opt_oldwithold, opt_otherpass,
+ "OldWithOld cert for genm with -infotype rootCaCert");
+ if (oldwithold == NULL)
+ goto end_upd;
+ }
+ if (!OSSL_CMP_get1_rootCaKeyUpdate(ctx, oldwithold, &newwithnew,
+ &newwithold, &oldwithnew))
+ goto end_upd;
+ /* At this point might check authorization of response sender/origin */
+
+ if (newwithnew == NULL)
+ CMP_info("no root CA certificate update available");
+ else if (oldwithold == NULL && oldwithnew != NULL)
+ CMP_warn("oldWithNew certificate received in genp for verifying oldWithOld, but oldWithOld was not provided");
+
+ if (save_cert_or_delete(newwithnew, opt_newwithnew,
+ "NewWithNew cert from genp")
+ && save_cert_or_delete(newwithold, opt_newwithold,
+ "NewWithOld cert from genp")
+ && save_cert_or_delete(oldwithnew, opt_oldwithnew,
+ "OldWithNew cert from genp"))
+ res = 1;
+
+ X509_free(newwithnew);
+ X509_free(newwithold);
+ X509_free(oldwithnew);
+ end_upd:
+ X509_free(oldwithold);
+ return res;
+ } else if (opt_infotype == NID_id_it_crlStatusList) {
+ X509_CRL *oldcrl = NULL, *crl = NULL;
+ X509 *crlcert = NULL;
+ int res = 0;
+ const char *desc = "CRL from genp of type 'crls'";
+
+ if (opt_oldcrl == NULL && opt_crlcert == NULL) {
+ CMP_err("Missing -oldcrl and no -crlcert given for -infotype crlStatusList");
+ return 0;
+ }
+ if (opt_crlout == NULL) {
+ CMP_err("Missing -crlout for -infotype crlStatusList");
+ return 0;
+ }
+
+ if (opt_crlcert != NULL) {
+ crlcert = load_cert_pwd(opt_crlcert, opt_otherpass,
+ "Cert for genm with -infotype crlStatusList");
+ if (crlcert == NULL)
+ goto end_crlupd;
+ }
+
+ if (opt_oldcrl != NULL) {
+ oldcrl = load_crl(opt_oldcrl, FORMAT_UNDEF, 0,
+ "CRL for genm with -infotype crlStatusList");
+ if (oldcrl == NULL)
+ goto end_crlupd;
+ }
+
+ if (opt_oldcrl != NULL && opt_crlcert != NULL) {
+ if (X509_NAME_cmp(X509_CRL_get_issuer(oldcrl),
+ X509_get_issuer_name(crlcert))
+ != 0)
+ CMP_warn("-oldcrl and -crlcert have different issuer");
+ }
+
+ if (!OSSL_CMP_get1_crlUpdate(ctx, crlcert, oldcrl, &crl))
+ goto end_crlupd;
+
+ if (crl == NULL)
+ CMP_info("no CRL update available");
+ if (!save_crl_or_delete(crl, opt_crlout, desc))
+ goto end_crlupd;
+
+ res = 1;
+
+ end_crlupd:
+ X509_free(crlcert);
+ X509_CRL_free(oldcrl);
+ X509_CRL_free(crl);
+ return res;
+
+ } else if (opt_infotype == NID_id_it_certReqTemplate) {
+ OSSL_CRMF_CERTTEMPLATE *certTemplate;
+ OSSL_CMP_ATAVS *keySpec;
+ int res = 0;
+
+ if (!OSSL_CMP_get1_certReqTemplate(ctx, &certTemplate, &keySpec))
+ return 0;
+
+ if (certTemplate == NULL) {
+ CMP_warn("no certificate request template available");
+ if (!delete_file(opt_template, "certTemplate from genp"))
+ return 0;
+ if (opt_keyspec != NULL
+ && !delete_file(opt_keyspec, "keySpec from genp"))
+ return 0;
+ return 1;
+ }
+ if (!save_template(opt_template, certTemplate))
+ goto tmpl_end;
+
+ print_keyspec(keySpec);
+ if (opt_keyspec != NULL) {
+ if (keySpec == NULL) {
+ CMP_warn("no key specifications available");
+ if (!delete_file(opt_keyspec, "keySpec from genp"))
+ goto tmpl_end;
+ } else if (!save_keyspec(opt_keyspec, keySpec)) {
+ goto tmpl_end;
+ }
+ }
+
+ res = 1;
+ tmpl_end:
+ OSSL_CRMF_CERTTEMPLATE_free(certTemplate);
+ sk_OSSL_CMP_ATAV_pop_free(keySpec, OSSL_CMP_ATAV_free);
+ return res;
+ } else {
+ OSSL_CMP_ITAV *req;
+ STACK_OF(OSSL_CMP_ITAV) *itavs;
+
+ if (opt_infotype != NID_undef) {
+ CMP_warn1("No specific support for -infotype %s available",
+ opt_infotype_s);
+
+ req = OSSL_CMP_ITAV_create(OBJ_nid2obj(opt_infotype), NULL);
+ if (req == NULL || !OSSL_CMP_CTX_push0_genm_ITAV(ctx, req)) {
+ CMP_err1("Failed to create genm for -infotype %s",
+ opt_infotype_s);
+ return 0;
+ }
+ }
+
+ if ((itavs = OSSL_CMP_exec_GENM_ses(ctx)) != NULL) {
+ int res = print_itavs(itavs);
+
+ sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
+ return res;
+ }
+ if (OSSL_CMP_CTX_get_status(ctx) != OSSL_CMP_PKISTATUS_request)
+ CMP_err("Did not receive response on genm or genp is not valid");
+ return 0;
+ }
+}
+
+static int handle_opts_upfront(int argc, char **argv)
{
- char *configfile = NULL;
int i;
- X509 *newcert = NULL;
- ENGINE *engine = NULL;
- OSSL_CMP_CTX *srv_cmp_ctx = NULL;
- int ret = 0; /* default: failure */
prog = opt_appname(argv[0]);
if (argc <= 1) {
opt_help(cmp_options);
- goto err;
+ return 0;
}
- /*
- * handle options -config, -section, and -verbosity upfront
- * to take effect for other options
- */
+ /* handle -config, -section, and -verbosity to take effect for other opts */
for (i = 1; i < argc - 1; i++) {
if (*argv[i] == '-') {
if (!strcmp(argv[i] + 1, cmp_options[OPT_CONFIG - OPT_HELP].name))
@@ -2813,11 +3591,25 @@ int cmp_main(int argc, char **argv)
else if (strcmp(argv[i] + 1,
cmp_options[OPT_VERBOSITY - OPT_HELP].name) == 0
&& !set_verbosity(atoi(argv[++i])))
- goto err;
+ return 0;
}
}
if (opt_section[0] == '\0') /* empty string */
opt_section = DEFAULT_SECTION;
+ return 1;
+}
+
+int cmp_main(int argc, char **argv)
+{
+ char *configfile = NULL;
+ int i;
+ X509 *newcert = NULL;
+ ENGINE *engine = NULL;
+ OSSL_CMP_CTX *srv_cmp_ctx = NULL;
+ int ret = 0; /* default: failure */
+
+ if (!handle_opts_upfront(argc, argv))
+ goto err;
vpm = X509_VERIFY_PARAM_new();
if (vpm == NULL) {
@@ -2828,7 +3620,8 @@ int cmp_main(int argc, char **argv)
/* read default values for options from config file */
configfile = opt_config != NULL ? opt_config : default_config_file;
if (configfile != NULL && configfile[0] != '\0' /* non-empty string */
- && (configfile != default_config_file || access(configfile, F_OK) != -1)) {
+ && (configfile != default_config_file
+ || access(configfile, F_OK) != -1)) {
CMP_info2("using section(s) '%s' of OpenSSL configuration file '%s'",
opt_section, configfile);
conf = app_load_config(configfile);
@@ -2842,6 +3635,7 @@ int cmp_main(int argc, char **argv)
opt_section, configfile);
} else {
const char *end = opt_section + strlen(opt_section);
+
while ((end = prev_item(opt_section, end)) != NULL) {
if (!NCONF_get_section(conf, opt_item)) {
CMP_err2("no [%s] section found in config file '%s'",
@@ -2862,9 +3656,14 @@ int cmp_main(int argc, char **argv)
}
(void)BIO_flush(bio_err); /* prevent interference with opt_help() */
+ cmp_ctx = OSSL_CMP_CTX_new(app_get0_libctx(), app_get0_propq());
+ if (cmp_ctx == NULL)
+ goto err;
+
ret = get_opts(argc, argv);
if (ret <= 0)
goto err;
+
ret = 0;
if (!app_RAND_load())
goto err;
@@ -2873,28 +3672,29 @@ int cmp_main(int argc, char **argv)
set_base_ui_method(UI_null());
if (opt_engine != NULL) {
- engine = setup_engine_methods(opt_engine, 0 /* not: ENGINE_METHOD_ALL */, 0);
+ engine = setup_engine_methods(opt_engine,
+ 0 /* not: ENGINE_METHOD_ALL */, 0);
if (engine == NULL) {
CMP_err1("cannot load engine %s", opt_engine);
goto err;
}
}
- cmp_ctx = OSSL_CMP_CTX_new(app_get0_libctx(), app_get0_propq());
- if (cmp_ctx == NULL)
- goto err;
OSSL_CMP_CTX_set_log_verbosity(cmp_ctx, opt_verbosity);
if (!OSSL_CMP_CTX_set_log_cb(cmp_ctx, print_to_bio_out)) {
CMP_err1("cannot set up error reporting and logging for %s", prog);
goto err;
}
-#ifndef OPENSSL_NO_SOCK
- if ((opt_tls_cert != NULL || opt_tls_key != NULL
- || opt_tls_keypass != NULL || opt_tls_extra != NULL
- || opt_tls_trusted != NULL || opt_tls_host != NULL)
- && !opt_tls_used)
- CMP_warn("Ingnoring TLS options(s) since -tls_used is not given");
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
+ if (opt_tls_cert == NULL && opt_tls_key == NULL && opt_tls_keypass == NULL
+ && opt_tls_extra == NULL && opt_tls_trusted == NULL
+ && opt_tls_host == NULL) {
+ if (opt_tls_used)
+ CMP_warn("-tls_used given without any other TLS options");
+ } else if (!opt_tls_used) {
+ CMP_warn("ignoring TLS options(s) since -tls_used is not given");
+ }
if (opt_port != NULL) {
if (opt_tls_used) {
CMP_err("-tls_used option not supported with -port option");
@@ -2919,11 +3719,17 @@ int cmp_main(int argc, char **argv)
}
#endif
- if (opt_use_mock_srv
-#ifndef OPENSSL_NO_SOCK
- || opt_port != NULL
+ if (opt_ignore_keyusage)
+ (void)OSSL_CMP_CTX_set_option(cmp_ctx, OSSL_CMP_OPT_IGNORE_KEYUSAGE, 1);
+ if (opt_no_cache_extracerts)
+ (void)OSSL_CMP_CTX_set_option(cmp_ctx, OSSL_CMP_OPT_NO_CACHE_EXTRACERTS,
+ 1);
+
+ if (opt_reqout_only == NULL && (opt_use_mock_srv
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
+ || opt_port != NULL
#endif
- ) {
+ )) {
OSSL_CMP_SRV_CTX *srv_ctx;
if ((srv_ctx = setup_srv_ctx(engine)) == NULL)
@@ -2937,7 +3743,7 @@ int cmp_main(int argc, char **argv)
OSSL_CMP_CTX_set_log_verbosity(srv_cmp_ctx, opt_verbosity);
}
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
if (opt_tls_used && (opt_use_mock_srv || opt_server == NULL)) {
CMP_warn("ignoring -tls_used option since -use_mock_srv is given or -server is not given");
opt_tls_used = 0;
@@ -2950,6 +3756,23 @@ int cmp_main(int argc, char **argv)
/* act as CMP client, possibly using internal mock server */
+ if (opt_reqout_only != NULL) {
+ const char *msg = "option is ignored since -reqout_only option is given";
+
+# if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
+ if (opt_server != NULL)
+ CMP_warn1("-server %s", msg);
+# endif
+ if (opt_use_mock_srv)
+ CMP_warn1("-use_mock_srv %s", msg);
+ if (opt_reqout != NULL)
+ CMP_warn1("-reqout %s", msg);
+ if (opt_rspin != NULL)
+ CMP_warn1("-rspin %s", msg);
+ if (opt_rspout != NULL)
+ CMP_warn1("-rspout %s", msg);
+ opt_reqout = opt_reqout_only;
+ }
if (opt_rspin != NULL) {
if (opt_server != NULL)
CMP_warn("-server option is not used if enough filenames given for -rspin");
@@ -2989,54 +3812,80 @@ int cmp_main(int argc, char **argv)
ret = OSSL_CMP_exec_RR_ses(cmp_ctx);
break;
case CMP_GENM:
- {
- STACK_OF(OSSL_CMP_ITAV) *itavs;
-
- if (opt_infotype != NID_undef) {
- OSSL_CMP_ITAV *itav =
- OSSL_CMP_ITAV_create(OBJ_nid2obj(opt_infotype), NULL);
- if (itav == NULL)
- goto err;
- OSSL_CMP_CTX_push0_genm_ITAV(cmp_ctx, itav);
- }
-
- if ((itavs = OSSL_CMP_exec_GENM_ses(cmp_ctx)) != NULL) {
- print_itavs(itavs);
- sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
- ret = 1;
- }
- break;
- }
+ ret = do_genm(cmp_ctx);
default:
break;
}
- if (OSSL_CMP_CTX_get_status(cmp_ctx) < OSSL_CMP_PKISTATUS_accepted)
- goto err; /* we got no response, maybe even did not send request */
-
+ if (OSSL_CMP_CTX_get_status(cmp_ctx) < OSSL_CMP_PKISTATUS_accepted) {
+ /* we got no response, maybe even did not send request */
+ ret = 0;
+ if (reqout_only_done) {
+ ERR_clear_error();
+ ret = 1;
+ }
+ goto err;
+ }
print_status();
- if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_extraCertsIn(cmp_ctx),
- opt_extracertsout, "extra") < 0)
+ if (!save_cert_or_delete(OSSL_CMP_CTX_get0_validatedSrvCert(cmp_ctx),
+ opt_srvcertout, "validated server"))
ret = 0;
if (!ret)
goto err;
ret = 0;
- if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_caPubs(cmp_ctx),
- opt_cacertsout, "CA") < 0)
+ if (save_free_certs(OSSL_CMP_CTX_get1_extraCertsIn(cmp_ctx),
+ opt_extracertsout, "extra") < 0)
goto err;
- if (newcert != NULL) {
- STACK_OF(X509) *certs = sk_X509_new_null();
-
- if (!X509_add_cert(certs, newcert, X509_ADD_FLAG_UP_REF)) {
- sk_X509_free(certs);
- goto err;
+ if (newcert != NULL && (opt_cmd == CMP_IR || opt_cmd == CMP_CR
+ || opt_cmd == CMP_KUR || opt_cmd == CMP_P10CR)) {
+ STACK_OF(X509) *newchain = OSSL_CMP_CTX_get1_newChain(cmp_ctx);
+
+ if (newcert != NULL && newchain != NULL /* NULL is on error only */
+ && opt_certout != NULL && opt_chainout != NULL
+ && strcmp(opt_certout, opt_chainout) == 0) {
+ if (!X509_add_cert(newchain, newcert, X509_ADD_FLAG_PREPEND
+ | X509_ADD_FLAG_UP_REF)) {
+ sk_X509_pop_free(newchain, X509_free);
+ goto err;
+ }
+ if (!save_free_certs(newchain, opt_chainout, "newly enrolled cert and chain"))
+ goto err;
+ } else {
+ if (save_free_certs(newchain, opt_chainout, "chain") < 0
+ || !save_cert_or_delete(newcert, opt_certout, "newly enrolled"))
+ goto err;
}
- if (save_free_certs(cmp_ctx, certs, opt_certout, "enrolled") < 0)
+ if (save_free_certs(OSSL_CMP_CTX_get1_caPubs(cmp_ctx),
+ opt_cacertsout, "CA") < 0)
goto err;
- }
- if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_newChain(cmp_ctx),
- opt_chainout, "chain") < 0)
- goto err;
+ if (opt_centralkeygen) {
+ EVP_CIPHER *cipher = NULL;
+ char *pass_string = NULL;
+ BIO *out;
+ int result = 1;
+ EVP_PKEY *new_key = OSSL_CMP_CTX_get0_newPkey(cmp_ctx, 1 /* priv */);
+
+ if (new_key == NULL)
+ goto err;
+ if ((out = bio_open_owner(opt_newkeyout, FORMAT_PEM, 1)) == NULL)
+ goto err;
+ if (opt_newkeypass != NULL) {
+ pass_string = get_passwd(opt_newkeypass,
+ "Centrally generated private key password");
+ cipher = EVP_CIPHER_fetch(app_get0_libctx(), SN_aes_256_cbc, app_get0_propq());
+ }
+ CMP_info1("saving centrally generated key to file '%s'", opt_newkeyout);
+ if (PEM_write_bio_PrivateKey(out, new_key, cipher, NULL, 0, NULL,
+ (void *)pass_string) <= 0)
+ result = 0;
+
+ BIO_free(out);
+ clear_free(pass_string);
+ EVP_CIPHER_free(cipher);
+ if (!result)
+ goto err;
+ }
+ }
if (!OSSL_CMP_CTX_reinit(cmp_ctx))
goto err;
}
@@ -3047,7 +3896,7 @@ int cmp_main(int argc, char **argv)
cleanse(opt_keypass);
cleanse(opt_newkeypass);
cleanse(opt_otherpass);
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
cleanse(opt_tls_keypass);
#endif
cleanse(opt_secret);
@@ -3058,15 +3907,16 @@ int cmp_main(int argc, char **argv)
OSSL_CMP_CTX_print_errors(cmp_ctx);
if (cmp_ctx != NULL) {
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
APP_HTTP_TLS_INFO *info = OSSL_CMP_CTX_get_http_cb_arg(cmp_ctx);
+ (void)OSSL_CMP_CTX_set_http_cb_arg(cmp_ctx, NULL);
#endif
ossl_cmp_mock_srv_free(OSSL_CMP_CTX_get_transfer_cb_arg(cmp_ctx));
X509_STORE_free(OSSL_CMP_CTX_get_certConf_cb_arg(cmp_ctx));
/* cannot free info already here, as it may be used indirectly by: */
OSSL_CMP_CTX_free(cmp_ctx);
-#ifndef OPENSSL_NO_SOCK
+#if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
if (info != NULL) {
OPENSSL_free((char *)info->server);
OPENSSL_free((char *)info->port);