xref: /freebsd-src/crypto/openssl/apps/cmp.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  * Copyright Nokia 2007-2019
4*b077aed3SPierre Pronchery  * Copyright Siemens AG 2015-2019
5*b077aed3SPierre Pronchery  *
6*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
7*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
8*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
9*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
10*b077aed3SPierre Pronchery  */
11*b077aed3SPierre Pronchery 
12*b077aed3SPierre Pronchery /* This app is disabled when OPENSSL_NO_CMP is defined. */
13*b077aed3SPierre Pronchery 
14*b077aed3SPierre Pronchery #include <string.h>
15*b077aed3SPierre Pronchery #include <ctype.h>
16*b077aed3SPierre Pronchery 
17*b077aed3SPierre Pronchery #include "apps.h"
18*b077aed3SPierre Pronchery #include "http_server.h"
19*b077aed3SPierre Pronchery #include "s_apps.h"
20*b077aed3SPierre Pronchery #include "progs.h"
21*b077aed3SPierre Pronchery 
22*b077aed3SPierre Pronchery #include "cmp_mock_srv.h"
23*b077aed3SPierre Pronchery 
24*b077aed3SPierre Pronchery /* tweaks needed due to missing unistd.h on Windows */
25*b077aed3SPierre Pronchery #if defined(_WIN32) && !defined(__BORLANDC__)
26*b077aed3SPierre Pronchery # define access _access
27*b077aed3SPierre Pronchery #endif
28*b077aed3SPierre Pronchery #ifndef F_OK
29*b077aed3SPierre Pronchery # define F_OK 0
30*b077aed3SPierre Pronchery #endif
31*b077aed3SPierre Pronchery 
32*b077aed3SPierre Pronchery #include <openssl/ui.h>
33*b077aed3SPierre Pronchery #include <openssl/pkcs12.h>
34*b077aed3SPierre Pronchery #include <openssl/ssl.h>
35*b077aed3SPierre Pronchery 
36*b077aed3SPierre Pronchery /* explicit #includes not strictly needed since implied by the above: */
37*b077aed3SPierre Pronchery #include <stdlib.h>
38*b077aed3SPierre Pronchery #include <openssl/cmp.h>
39*b077aed3SPierre Pronchery #include <openssl/cmp_util.h>
40*b077aed3SPierre Pronchery #include <openssl/crmf.h>
41*b077aed3SPierre Pronchery #include <openssl/crypto.h>
42*b077aed3SPierre Pronchery #include <openssl/err.h>
43*b077aed3SPierre Pronchery #include <openssl/store.h>
44*b077aed3SPierre Pronchery #include <openssl/objects.h>
45*b077aed3SPierre Pronchery #include <openssl/x509.h>
46*b077aed3SPierre Pronchery 
47*b077aed3SPierre Pronchery static char *prog;
48*b077aed3SPierre Pronchery static char *opt_config = NULL;
49*b077aed3SPierre Pronchery #define CMP_SECTION "cmp"
50*b077aed3SPierre Pronchery #define SECTION_NAME_MAX 40 /* max length of section name */
51*b077aed3SPierre Pronchery #define DEFAULT_SECTION "default"
52*b077aed3SPierre Pronchery static char *opt_section = CMP_SECTION;
53*b077aed3SPierre Pronchery static int opt_verbosity = OSSL_CMP_LOG_INFO;
54*b077aed3SPierre Pronchery 
55*b077aed3SPierre Pronchery static int read_config(void);
56*b077aed3SPierre Pronchery 
57*b077aed3SPierre Pronchery static CONF *conf = NULL; /* OpenSSL config file context structure */
58*b077aed3SPierre Pronchery static OSSL_CMP_CTX *cmp_ctx = NULL; /* the client-side CMP context */
59*b077aed3SPierre Pronchery 
60*b077aed3SPierre Pronchery /* the type of cmp command we want to send */
61*b077aed3SPierre Pronchery typedef enum {
62*b077aed3SPierre Pronchery     CMP_IR,
63*b077aed3SPierre Pronchery     CMP_KUR,
64*b077aed3SPierre Pronchery     CMP_CR,
65*b077aed3SPierre Pronchery     CMP_P10CR,
66*b077aed3SPierre Pronchery     CMP_RR,
67*b077aed3SPierre Pronchery     CMP_GENM
68*b077aed3SPierre Pronchery } cmp_cmd_t;
69*b077aed3SPierre Pronchery 
70*b077aed3SPierre Pronchery /* message transfer */
71*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
72*b077aed3SPierre Pronchery static char *opt_server = NULL;
73*b077aed3SPierre Pronchery static char *opt_proxy = NULL;
74*b077aed3SPierre Pronchery static char *opt_no_proxy = NULL;
75*b077aed3SPierre Pronchery #endif
76*b077aed3SPierre Pronchery static char *opt_recipient = NULL;
77*b077aed3SPierre Pronchery static char *opt_path = NULL;
78*b077aed3SPierre Pronchery static int opt_keep_alive = 1;
79*b077aed3SPierre Pronchery static int opt_msg_timeout = -1;
80*b077aed3SPierre Pronchery static int opt_total_timeout = -1;
81*b077aed3SPierre Pronchery 
82*b077aed3SPierre Pronchery /* server authentication */
83*b077aed3SPierre Pronchery static char *opt_trusted = NULL;
84*b077aed3SPierre Pronchery static char *opt_untrusted = NULL;
85*b077aed3SPierre Pronchery static char *opt_srvcert = NULL;
86*b077aed3SPierre Pronchery static char *opt_expect_sender = NULL;
87*b077aed3SPierre Pronchery static int opt_ignore_keyusage = 0;
88*b077aed3SPierre Pronchery static int opt_unprotected_errors = 0;
89*b077aed3SPierre Pronchery static char *opt_extracertsout = NULL;
90*b077aed3SPierre Pronchery static char *opt_cacertsout = NULL;
91*b077aed3SPierre Pronchery 
92*b077aed3SPierre Pronchery /* client authentication */
93*b077aed3SPierre Pronchery static char *opt_ref = NULL;
94*b077aed3SPierre Pronchery static char *opt_secret = NULL;
95*b077aed3SPierre Pronchery static char *opt_cert = NULL;
96*b077aed3SPierre Pronchery static char *opt_own_trusted = NULL;
97*b077aed3SPierre Pronchery static char *opt_key = NULL;
98*b077aed3SPierre Pronchery static char *opt_keypass = NULL;
99*b077aed3SPierre Pronchery static char *opt_digest = NULL;
100*b077aed3SPierre Pronchery static char *opt_mac = NULL;
101*b077aed3SPierre Pronchery static char *opt_extracerts = NULL;
102*b077aed3SPierre Pronchery static int opt_unprotected_requests = 0;
103*b077aed3SPierre Pronchery 
104*b077aed3SPierre Pronchery /* generic message */
105*b077aed3SPierre Pronchery static char *opt_cmd_s = NULL;
106*b077aed3SPierre Pronchery static int opt_cmd = -1;
107*b077aed3SPierre Pronchery static char *opt_geninfo = NULL;
108*b077aed3SPierre Pronchery static char *opt_infotype_s = NULL;
109*b077aed3SPierre Pronchery static int opt_infotype = NID_undef;
110*b077aed3SPierre Pronchery 
111*b077aed3SPierre Pronchery /* certificate enrollment */
112*b077aed3SPierre Pronchery static char *opt_newkey = NULL;
113*b077aed3SPierre Pronchery static char *opt_newkeypass = NULL;
114*b077aed3SPierre Pronchery static char *opt_subject = NULL;
115*b077aed3SPierre Pronchery static char *opt_issuer = NULL;
116*b077aed3SPierre Pronchery static int opt_days = 0;
117*b077aed3SPierre Pronchery static char *opt_reqexts = NULL;
118*b077aed3SPierre Pronchery static char *opt_sans = NULL;
119*b077aed3SPierre Pronchery static int opt_san_nodefault = 0;
120*b077aed3SPierre Pronchery static char *opt_policies = NULL;
121*b077aed3SPierre Pronchery static char *opt_policy_oids = NULL;
122*b077aed3SPierre Pronchery static int opt_policy_oids_critical = 0;
123*b077aed3SPierre Pronchery static int opt_popo = OSSL_CRMF_POPO_NONE - 1;
124*b077aed3SPierre Pronchery static char *opt_csr = NULL;
125*b077aed3SPierre Pronchery static char *opt_out_trusted = NULL;
126*b077aed3SPierre Pronchery static int opt_implicit_confirm = 0;
127*b077aed3SPierre Pronchery static int opt_disable_confirm = 0;
128*b077aed3SPierre Pronchery static char *opt_certout = NULL;
129*b077aed3SPierre Pronchery static char *opt_chainout = NULL;
130*b077aed3SPierre Pronchery 
131*b077aed3SPierre Pronchery /* certificate enrollment and revocation */
132*b077aed3SPierre Pronchery static char *opt_oldcert = NULL;
133*b077aed3SPierre Pronchery static int opt_revreason = CRL_REASON_NONE;
134*b077aed3SPierre Pronchery 
135*b077aed3SPierre Pronchery /* credentials format */
136*b077aed3SPierre Pronchery static char *opt_certform_s = "PEM";
137*b077aed3SPierre Pronchery static int opt_certform = FORMAT_PEM;
138*b077aed3SPierre Pronchery static char *opt_keyform_s = NULL;
139*b077aed3SPierre Pronchery static int opt_keyform = FORMAT_UNDEF;
140*b077aed3SPierre Pronchery static char *opt_otherpass = NULL;
141*b077aed3SPierre Pronchery static char *opt_engine = NULL;
142*b077aed3SPierre Pronchery 
143*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
144*b077aed3SPierre Pronchery /* TLS connection */
145*b077aed3SPierre Pronchery static int opt_tls_used = 0;
146*b077aed3SPierre Pronchery static char *opt_tls_cert = NULL;
147*b077aed3SPierre Pronchery static char *opt_tls_key = NULL;
148*b077aed3SPierre Pronchery static char *opt_tls_keypass = NULL;
149*b077aed3SPierre Pronchery static char *opt_tls_extra = NULL;
150*b077aed3SPierre Pronchery static char *opt_tls_trusted = NULL;
151*b077aed3SPierre Pronchery static char *opt_tls_host = NULL;
152*b077aed3SPierre Pronchery #endif
153*b077aed3SPierre Pronchery 
154*b077aed3SPierre Pronchery /* client-side debugging */
155*b077aed3SPierre Pronchery static int opt_batch = 0;
156*b077aed3SPierre Pronchery static int opt_repeat = 1;
157*b077aed3SPierre Pronchery static char *opt_reqin = NULL;
158*b077aed3SPierre Pronchery static int opt_reqin_new_tid = 0;
159*b077aed3SPierre Pronchery static char *opt_reqout = NULL;
160*b077aed3SPierre Pronchery static char *opt_rspin = NULL;
161*b077aed3SPierre Pronchery static int rspin_in_use = 0;
162*b077aed3SPierre Pronchery static char *opt_rspout = NULL;
163*b077aed3SPierre Pronchery static int opt_use_mock_srv = 0;
164*b077aed3SPierre Pronchery 
165*b077aed3SPierre Pronchery /* mock server */
166*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
167*b077aed3SPierre Pronchery static char *opt_port = NULL;
168*b077aed3SPierre Pronchery static int opt_max_msgs = 0;
169*b077aed3SPierre Pronchery #endif
170*b077aed3SPierre Pronchery static char *opt_srv_ref = NULL;
171*b077aed3SPierre Pronchery static char *opt_srv_secret = NULL;
172*b077aed3SPierre Pronchery static char *opt_srv_cert = NULL;
173*b077aed3SPierre Pronchery static char *opt_srv_key = NULL;
174*b077aed3SPierre Pronchery static char *opt_srv_keypass = NULL;
175*b077aed3SPierre Pronchery 
176*b077aed3SPierre Pronchery static char *opt_srv_trusted = NULL;
177*b077aed3SPierre Pronchery static char *opt_srv_untrusted = NULL;
178*b077aed3SPierre Pronchery static char *opt_rsp_cert = NULL;
179*b077aed3SPierre Pronchery static char *opt_rsp_extracerts = NULL;
180*b077aed3SPierre Pronchery static char *opt_rsp_capubs = NULL;
181*b077aed3SPierre Pronchery static int opt_poll_count = 0;
182*b077aed3SPierre Pronchery static int opt_check_after = 1;
183*b077aed3SPierre Pronchery static int opt_grant_implicitconf = 0;
184*b077aed3SPierre Pronchery 
185*b077aed3SPierre Pronchery static int opt_pkistatus = OSSL_CMP_PKISTATUS_accepted;
186*b077aed3SPierre Pronchery static int opt_failure = INT_MIN;
187*b077aed3SPierre Pronchery static int opt_failurebits = 0;
188*b077aed3SPierre Pronchery static char *opt_statusstring = NULL;
189*b077aed3SPierre Pronchery static int opt_send_error = 0;
190*b077aed3SPierre Pronchery static int opt_send_unprotected = 0;
191*b077aed3SPierre Pronchery static int opt_send_unprot_err = 0;
192*b077aed3SPierre Pronchery static int opt_accept_unprotected = 0;
193*b077aed3SPierre Pronchery static int opt_accept_unprot_err = 0;
194*b077aed3SPierre Pronchery static int opt_accept_raverified = 0;
195*b077aed3SPierre Pronchery 
196*b077aed3SPierre Pronchery static X509_VERIFY_PARAM *vpm = NULL;
197*b077aed3SPierre Pronchery 
198*b077aed3SPierre Pronchery typedef enum OPTION_choice {
199*b077aed3SPierre Pronchery     OPT_COMMON,
200*b077aed3SPierre Pronchery     OPT_CONFIG, OPT_SECTION, OPT_VERBOSITY,
201*b077aed3SPierre Pronchery 
202*b077aed3SPierre Pronchery     OPT_CMD, OPT_INFOTYPE, OPT_GENINFO,
203*b077aed3SPierre Pronchery 
204*b077aed3SPierre Pronchery     OPT_NEWKEY, OPT_NEWKEYPASS, OPT_SUBJECT, OPT_ISSUER,
205*b077aed3SPierre Pronchery     OPT_DAYS, OPT_REQEXTS,
206*b077aed3SPierre Pronchery     OPT_SANS, OPT_SAN_NODEFAULT,
207*b077aed3SPierre Pronchery     OPT_POLICIES, OPT_POLICY_OIDS, OPT_POLICY_OIDS_CRITICAL,
208*b077aed3SPierre Pronchery     OPT_POPO, OPT_CSR,
209*b077aed3SPierre Pronchery     OPT_OUT_TRUSTED, OPT_IMPLICIT_CONFIRM, OPT_DISABLE_CONFIRM,
210*b077aed3SPierre Pronchery     OPT_CERTOUT, OPT_CHAINOUT,
211*b077aed3SPierre Pronchery 
212*b077aed3SPierre Pronchery     OPT_OLDCERT, OPT_REVREASON,
213*b077aed3SPierre Pronchery 
214*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
215*b077aed3SPierre Pronchery     OPT_SERVER, OPT_PROXY, OPT_NO_PROXY,
216*b077aed3SPierre Pronchery #endif
217*b077aed3SPierre Pronchery     OPT_RECIPIENT, OPT_PATH,
218*b077aed3SPierre Pronchery     OPT_KEEP_ALIVE, OPT_MSG_TIMEOUT, OPT_TOTAL_TIMEOUT,
219*b077aed3SPierre Pronchery 
220*b077aed3SPierre Pronchery     OPT_TRUSTED, OPT_UNTRUSTED, OPT_SRVCERT,
221*b077aed3SPierre Pronchery     OPT_EXPECT_SENDER,
222*b077aed3SPierre Pronchery     OPT_IGNORE_KEYUSAGE, OPT_UNPROTECTED_ERRORS,
223*b077aed3SPierre Pronchery     OPT_EXTRACERTSOUT, OPT_CACERTSOUT,
224*b077aed3SPierre Pronchery 
225*b077aed3SPierre Pronchery     OPT_REF, OPT_SECRET, OPT_CERT, OPT_OWN_TRUSTED, OPT_KEY, OPT_KEYPASS,
226*b077aed3SPierre Pronchery     OPT_DIGEST, OPT_MAC, OPT_EXTRACERTS,
227*b077aed3SPierre Pronchery     OPT_UNPROTECTED_REQUESTS,
228*b077aed3SPierre Pronchery 
229*b077aed3SPierre Pronchery     OPT_CERTFORM, OPT_KEYFORM,
230*b077aed3SPierre Pronchery     OPT_OTHERPASS,
231*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
232*b077aed3SPierre Pronchery     OPT_ENGINE,
233*b077aed3SPierre Pronchery #endif
234*b077aed3SPierre Pronchery     OPT_PROV_ENUM,
235*b077aed3SPierre Pronchery     OPT_R_ENUM,
236*b077aed3SPierre Pronchery 
237*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
238*b077aed3SPierre Pronchery     OPT_TLS_USED, OPT_TLS_CERT, OPT_TLS_KEY,
239*b077aed3SPierre Pronchery     OPT_TLS_KEYPASS,
240*b077aed3SPierre Pronchery     OPT_TLS_EXTRA, OPT_TLS_TRUSTED, OPT_TLS_HOST,
241*b077aed3SPierre Pronchery #endif
242*b077aed3SPierre Pronchery 
243*b077aed3SPierre Pronchery     OPT_BATCH, OPT_REPEAT,
244*b077aed3SPierre Pronchery     OPT_REQIN, OPT_REQIN_NEW_TID, OPT_REQOUT, OPT_RSPIN, OPT_RSPOUT,
245*b077aed3SPierre Pronchery     OPT_USE_MOCK_SRV,
246*b077aed3SPierre Pronchery 
247*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
248*b077aed3SPierre Pronchery     OPT_PORT, OPT_MAX_MSGS,
249*b077aed3SPierre Pronchery #endif
250*b077aed3SPierre Pronchery     OPT_SRV_REF, OPT_SRV_SECRET,
251*b077aed3SPierre Pronchery     OPT_SRV_CERT, OPT_SRV_KEY, OPT_SRV_KEYPASS,
252*b077aed3SPierre Pronchery     OPT_SRV_TRUSTED, OPT_SRV_UNTRUSTED,
253*b077aed3SPierre Pronchery     OPT_RSP_CERT, OPT_RSP_EXTRACERTS, OPT_RSP_CAPUBS,
254*b077aed3SPierre Pronchery     OPT_POLL_COUNT, OPT_CHECK_AFTER,
255*b077aed3SPierre Pronchery     OPT_GRANT_IMPLICITCONF,
256*b077aed3SPierre Pronchery     OPT_PKISTATUS, OPT_FAILURE,
257*b077aed3SPierre Pronchery     OPT_FAILUREBITS, OPT_STATUSSTRING,
258*b077aed3SPierre Pronchery     OPT_SEND_ERROR, OPT_SEND_UNPROTECTED,
259*b077aed3SPierre Pronchery     OPT_SEND_UNPROT_ERR, OPT_ACCEPT_UNPROTECTED,
260*b077aed3SPierre Pronchery     OPT_ACCEPT_UNPROT_ERR, OPT_ACCEPT_RAVERIFIED,
261*b077aed3SPierre Pronchery 
262*b077aed3SPierre Pronchery     OPT_V_ENUM
263*b077aed3SPierre Pronchery } OPTION_CHOICE;
264*b077aed3SPierre Pronchery 
265*b077aed3SPierre Pronchery const OPTIONS cmp_options[] = {
266*b077aed3SPierre Pronchery     /* entries must be in the same order as enumerated above!! */
267*b077aed3SPierre Pronchery     {"help", OPT_HELP, '-', "Display this summary"},
268*b077aed3SPierre Pronchery     {"config", OPT_CONFIG, 's',
269*b077aed3SPierre Pronchery      "Configuration file to use. \"\" = none. Default from env variable OPENSSL_CONF"},
270*b077aed3SPierre Pronchery     {"section", OPT_SECTION, 's',
271*b077aed3SPierre Pronchery      "Section(s) in config file to get options from. \"\" = 'default'. Default 'cmp'"},
272*b077aed3SPierre Pronchery     {"verbosity", OPT_VERBOSITY, 'N',
273*b077aed3SPierre Pronchery      "Log level; 3=ERR, 4=WARN, 6=INFO, 7=DEBUG, 8=TRACE. Default 6 = INFO"},
274*b077aed3SPierre Pronchery 
275*b077aed3SPierre Pronchery     OPT_SECTION("Generic message"),
276*b077aed3SPierre Pronchery     {"cmd", OPT_CMD, 's', "CMP request to send: ir/cr/kur/p10cr/rr/genm"},
277*b077aed3SPierre Pronchery     {"infotype", OPT_INFOTYPE, 's',
278*b077aed3SPierre Pronchery      "InfoType name for requesting specific info in genm, e.g. 'signKeyPairTypes'"},
279*b077aed3SPierre Pronchery     {"geninfo", OPT_GENINFO, 's',
280*b077aed3SPierre Pronchery      "generalInfo integer values to place in request PKIHeader with given OID"},
281*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
282*b077aed3SPierre Pronchery      "specified in the form <OID>:int:<n>, e.g. \"1.2.3.4:int:56789\""},
283*b077aed3SPierre Pronchery 
284*b077aed3SPierre Pronchery     OPT_SECTION("Certificate enrollment"),
285*b077aed3SPierre Pronchery     {"newkey", OPT_NEWKEY, 's',
286*b077aed3SPierre Pronchery      "Private or public key for the requested cert. Default: CSR key or client key"},
287*b077aed3SPierre Pronchery     {"newkeypass", OPT_NEWKEYPASS, 's', "New private key pass phrase source"},
288*b077aed3SPierre Pronchery     {"subject", OPT_SUBJECT, 's',
289*b077aed3SPierre Pronchery      "Distinguished Name (DN) of subject to use in the requested cert template"},
290*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
291*b077aed3SPierre Pronchery      "For kur, default is subject of -csr arg or reference cert (see -oldcert)"},
292*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
293*b077aed3SPierre Pronchery      "this default is used for ir and cr only if no Subject Alt Names are set"},
294*b077aed3SPierre Pronchery     {"issuer", OPT_ISSUER, 's',
295*b077aed3SPierre Pronchery      "DN of the issuer to place in the requested certificate template"},
296*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
297*b077aed3SPierre Pronchery      "also used as recipient if neither -recipient nor -srvcert are given"},
298*b077aed3SPierre Pronchery     {"days", OPT_DAYS, 'N',
299*b077aed3SPierre Pronchery      "Requested validity time of the new certificate in number of days"},
300*b077aed3SPierre Pronchery     {"reqexts", OPT_REQEXTS, 's',
301*b077aed3SPierre Pronchery      "Name of config file section defining certificate request extensions."},
302*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
303*b077aed3SPierre Pronchery      "Augments or replaces any extensions contained CSR given with -csr"},
304*b077aed3SPierre Pronchery     {"sans", OPT_SANS, 's',
305*b077aed3SPierre Pronchery      "Subject Alt Names (IPADDR/DNS/URI) to add as (critical) cert req extension"},
306*b077aed3SPierre Pronchery     {"san_nodefault", OPT_SAN_NODEFAULT, '-',
307*b077aed3SPierre Pronchery      "Do not take default SANs from reference certificate (see -oldcert)"},
308*b077aed3SPierre Pronchery     {"policies", OPT_POLICIES, 's',
309*b077aed3SPierre Pronchery      "Name of config file section defining policies certificate request extension"},
310*b077aed3SPierre Pronchery     {"policy_oids", OPT_POLICY_OIDS, 's',
311*b077aed3SPierre Pronchery      "Policy OID(s) to add as policies certificate request extension"},
312*b077aed3SPierre Pronchery     {"policy_oids_critical", OPT_POLICY_OIDS_CRITICAL, '-',
313*b077aed3SPierre Pronchery      "Flag the policy OID(s) given with -policy_oids as critical"},
314*b077aed3SPierre Pronchery     {"popo", OPT_POPO, 'n',
315*b077aed3SPierre Pronchery      "Proof-of-Possession (POPO) method to use for ir/cr/kur where"},
316*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
317*b077aed3SPierre Pronchery      "-1 = NONE, 0 = RAVERIFIED, 1 = SIGNATURE (default), 2 = KEYENC"},
318*b077aed3SPierre Pronchery     {"csr", OPT_CSR, 's',
319*b077aed3SPierre Pronchery      "PKCS#10 CSR file in PEM or DER format to convert or to use in p10cr"},
320*b077aed3SPierre Pronchery     {"out_trusted", OPT_OUT_TRUSTED, 's',
321*b077aed3SPierre Pronchery      "Certificates to trust when verifying newly enrolled certificates"},
322*b077aed3SPierre Pronchery     {"implicit_confirm", OPT_IMPLICIT_CONFIRM, '-',
323*b077aed3SPierre Pronchery      "Request implicit confirmation of newly enrolled certificates"},
324*b077aed3SPierre Pronchery     {"disable_confirm", OPT_DISABLE_CONFIRM, '-',
325*b077aed3SPierre Pronchery      "Do not confirm newly enrolled certificate w/o requesting implicit"},
326*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
327*b077aed3SPierre Pronchery      "confirmation. WARNING: This leads to behavior violating RFC 4210"},
328*b077aed3SPierre Pronchery     {"certout", OPT_CERTOUT, 's',
329*b077aed3SPierre Pronchery      "File to save newly enrolled certificate"},
330*b077aed3SPierre Pronchery     {"chainout", OPT_CHAINOUT, 's',
331*b077aed3SPierre Pronchery      "File to save the chain of newly enrolled certificate"},
332*b077aed3SPierre Pronchery 
333*b077aed3SPierre Pronchery     OPT_SECTION("Certificate enrollment and revocation"),
334*b077aed3SPierre Pronchery 
335*b077aed3SPierre Pronchery     {"oldcert", OPT_OLDCERT, 's',
336*b077aed3SPierre Pronchery      "Certificate to be updated (defaulting to -cert) or to be revoked in rr;"},
337*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
338*b077aed3SPierre Pronchery      "also used as reference (defaulting to -cert) for subject DN and SANs."},
339*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
340*b077aed3SPierre Pronchery      "Issuer is used as recipient unless -recipient, -srvcert, or -issuer given"},
341*b077aed3SPierre Pronchery     {"revreason", OPT_REVREASON, 'n',
342*b077aed3SPierre Pronchery      "Reason code to include in revocation request (rr); possible values:"},
343*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
344*b077aed3SPierre Pronchery      "0..6, 8..10 (see RFC5280, 5.3.1) or -1. Default -1 = none included"},
345*b077aed3SPierre Pronchery 
346*b077aed3SPierre Pronchery     OPT_SECTION("Message transfer"),
347*b077aed3SPierre Pronchery #ifdef OPENSSL_NO_SOCK
348*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
349*b077aed3SPierre Pronchery      "NOTE: -server, -proxy, and -no_proxy not supported due to no-sock build"},
350*b077aed3SPierre Pronchery #else
351*b077aed3SPierre Pronchery     {"server", OPT_SERVER, 's',
352*b077aed3SPierre Pronchery      "[http[s]://]address[:port][/path] of CMP server. Default port 80 or 443."},
353*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
354*b077aed3SPierre Pronchery      "address may be a DNS name or an IP address; path can be overridden by -path"},
355*b077aed3SPierre Pronchery     {"proxy", OPT_PROXY, 's',
356*b077aed3SPierre Pronchery      "[http[s]://]address[:port][/path] of HTTP(S) proxy to use; path is ignored"},
357*b077aed3SPierre Pronchery     {"no_proxy", OPT_NO_PROXY, 's',
358*b077aed3SPierre Pronchery      "List of addresses of servers not to use HTTP(S) proxy for"},
359*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
360*b077aed3SPierre Pronchery      "Default from environment variable 'no_proxy', else 'NO_PROXY', else none"},
361*b077aed3SPierre Pronchery #endif
362*b077aed3SPierre Pronchery     {"recipient", OPT_RECIPIENT, 's',
363*b077aed3SPierre Pronchery      "DN of CA. Default: subject of -srvcert, -issuer, issuer of -oldcert or -cert"},
364*b077aed3SPierre Pronchery     {"path", OPT_PATH, 's',
365*b077aed3SPierre Pronchery      "HTTP path (aka CMP alias) at the CMP server. Default from -server, else \"/\""},
366*b077aed3SPierre Pronchery     {"keep_alive", OPT_KEEP_ALIVE, 'N',
367*b077aed3SPierre Pronchery      "Persistent HTTP connections. 0: no, 1 (the default): request, 2: require"},
368*b077aed3SPierre Pronchery     {"msg_timeout", OPT_MSG_TIMEOUT, 'N',
369*b077aed3SPierre Pronchery      "Number of seconds allowed per CMP message round trip, or 0 for infinite"},
370*b077aed3SPierre Pronchery     {"total_timeout", OPT_TOTAL_TIMEOUT, 'N',
371*b077aed3SPierre Pronchery      "Overall time an enrollment incl. polling may take. Default 0 = infinite"},
372*b077aed3SPierre Pronchery 
373*b077aed3SPierre Pronchery     OPT_SECTION("Server authentication"),
374*b077aed3SPierre Pronchery     {"trusted", OPT_TRUSTED, 's',
375*b077aed3SPierre Pronchery      "Certificates to use as trust anchors when verifying signed CMP responses"},
376*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0, "unless -srvcert is given"},
377*b077aed3SPierre Pronchery     {"untrusted", OPT_UNTRUSTED, 's',
378*b077aed3SPierre Pronchery      "Intermediate CA certs for chain construction for CMP/TLS/enrolled certs"},
379*b077aed3SPierre Pronchery     {"srvcert", OPT_SRVCERT, 's',
380*b077aed3SPierre Pronchery      "Server cert to pin and trust directly when verifying signed CMP responses"},
381*b077aed3SPierre Pronchery     {"expect_sender", OPT_EXPECT_SENDER, 's',
382*b077aed3SPierre Pronchery      "DN of expected sender of responses. Defaults to subject of -srvcert, if any"},
383*b077aed3SPierre Pronchery     {"ignore_keyusage", OPT_IGNORE_KEYUSAGE, '-',
384*b077aed3SPierre Pronchery      "Ignore CMP signer cert key usage, else 'digitalSignature' must be allowed"},
385*b077aed3SPierre Pronchery     {"unprotected_errors", OPT_UNPROTECTED_ERRORS, '-',
386*b077aed3SPierre Pronchery      "Accept missing or invalid protection of regular error messages and negative"},
387*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
388*b077aed3SPierre Pronchery      "certificate responses (ip/cp/kup), revocation responses (rp), and PKIConf"},
389*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
390*b077aed3SPierre Pronchery      "WARNING: This setting leads to behavior allowing violation of RFC 4210"},
391*b077aed3SPierre Pronchery     {"extracertsout", OPT_EXTRACERTSOUT, 's',
392*b077aed3SPierre Pronchery      "File to save extra certificates received in the extraCerts field"},
393*b077aed3SPierre Pronchery     {"cacertsout", OPT_CACERTSOUT, 's',
394*b077aed3SPierre Pronchery      "File to save CA certificates received in the caPubs field of 'ip' messages"},
395*b077aed3SPierre Pronchery 
396*b077aed3SPierre Pronchery     OPT_SECTION("Client authentication"),
397*b077aed3SPierre Pronchery     {"ref", OPT_REF, 's',
398*b077aed3SPierre Pronchery      "Reference value to use as senderKID in case no -cert is given"},
399*b077aed3SPierre Pronchery     {"secret", OPT_SECRET, 's',
400*b077aed3SPierre Pronchery      "Prefer PBM (over signatures) for protecting msgs with given password source"},
401*b077aed3SPierre Pronchery     {"cert", OPT_CERT, 's',
402*b077aed3SPierre Pronchery      "Client's CMP signer certificate; its public key must match the -key argument"},
403*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
404*b077aed3SPierre Pronchery      "This also used as default reference for subject DN and SANs."},
405*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
406*b077aed3SPierre Pronchery      "Any further certs included are appended to the untrusted certs"},
407*b077aed3SPierre Pronchery     {"own_trusted", OPT_OWN_TRUSTED, 's',
408*b077aed3SPierre Pronchery      "Optional certs to verify chain building for own CMP signer cert"},
409*b077aed3SPierre Pronchery     {"key", OPT_KEY, 's', "CMP signer private key, not used when -secret given"},
410*b077aed3SPierre Pronchery     {"keypass", OPT_KEYPASS, 's',
411*b077aed3SPierre Pronchery      "Client private key (and cert and old cert) pass phrase source"},
412*b077aed3SPierre Pronchery     {"digest", OPT_DIGEST, 's',
413*b077aed3SPierre Pronchery      "Digest to use in message protection and POPO signatures. Default \"sha256\""},
414*b077aed3SPierre Pronchery     {"mac", OPT_MAC, 's',
415*b077aed3SPierre Pronchery      "MAC algorithm to use in PBM-based message protection. Default \"hmac-sha1\""},
416*b077aed3SPierre Pronchery     {"extracerts", OPT_EXTRACERTS, 's',
417*b077aed3SPierre Pronchery      "Certificates to append in extraCerts field of outgoing messages."},
418*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
419*b077aed3SPierre Pronchery      "This can be used as the default CMP signer cert chain to include"},
420*b077aed3SPierre Pronchery     {"unprotected_requests", OPT_UNPROTECTED_REQUESTS, '-',
421*b077aed3SPierre Pronchery      "Send request messages without CMP-level protection"},
422*b077aed3SPierre Pronchery 
423*b077aed3SPierre Pronchery     OPT_SECTION("Credentials format"),
424*b077aed3SPierre Pronchery     {"certform", OPT_CERTFORM, 's',
425*b077aed3SPierre Pronchery      "Format (PEM or DER) to use when saving a certificate to a file. Default PEM"},
426*b077aed3SPierre Pronchery     {"keyform", OPT_KEYFORM, 's',
427*b077aed3SPierre Pronchery      "Format of the key input (ENGINE, other values ignored)"},
428*b077aed3SPierre Pronchery     {"otherpass", OPT_OTHERPASS, 's',
429*b077aed3SPierre Pronchery      "Pass phrase source potentially needed for loading certificates of others"},
430*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
431*b077aed3SPierre Pronchery     {"engine", OPT_ENGINE, 's',
432*b077aed3SPierre Pronchery      "Use crypto engine with given identifier, possibly a hardware device."},
433*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
434*b077aed3SPierre Pronchery      "Engines may also be defined in OpenSSL config file engine section."},
435*b077aed3SPierre Pronchery #endif
436*b077aed3SPierre Pronchery     OPT_PROV_OPTIONS,
437*b077aed3SPierre Pronchery     OPT_R_OPTIONS,
438*b077aed3SPierre Pronchery 
439*b077aed3SPierre Pronchery     OPT_SECTION("TLS connection"),
440*b077aed3SPierre Pronchery #ifdef OPENSSL_NO_SOCK
441*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
442*b077aed3SPierre Pronchery      "NOTE: -tls_used and all other TLS options not supported due to no-sock build"},
443*b077aed3SPierre Pronchery #else
444*b077aed3SPierre Pronchery     {"tls_used", OPT_TLS_USED, '-',
445*b077aed3SPierre Pronchery      "Enable using TLS (also when other TLS options are not set)"},
446*b077aed3SPierre Pronchery     {"tls_cert", OPT_TLS_CERT, 's',
447*b077aed3SPierre Pronchery      "Client's TLS certificate. May include chain to be provided to TLS server"},
448*b077aed3SPierre Pronchery     {"tls_key", OPT_TLS_KEY, 's',
449*b077aed3SPierre Pronchery      "Private key for the client's TLS certificate"},
450*b077aed3SPierre Pronchery     {"tls_keypass", OPT_TLS_KEYPASS, 's',
451*b077aed3SPierre Pronchery      "Pass phrase source for the client's private TLS key (and TLS cert)"},
452*b077aed3SPierre Pronchery     {"tls_extra", OPT_TLS_EXTRA, 's',
453*b077aed3SPierre Pronchery      "Extra certificates to provide to TLS server during TLS handshake"},
454*b077aed3SPierre Pronchery     {"tls_trusted", OPT_TLS_TRUSTED, 's',
455*b077aed3SPierre Pronchery      "Trusted certificates to use for verifying the TLS server certificate;"},
456*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0, "this implies host name validation"},
457*b077aed3SPierre Pronchery     {"tls_host", OPT_TLS_HOST, 's',
458*b077aed3SPierre Pronchery      "Address to be checked (rather than -server) during TLS host name validation"},
459*b077aed3SPierre Pronchery #endif
460*b077aed3SPierre Pronchery 
461*b077aed3SPierre Pronchery     OPT_SECTION("Client-side debugging"),
462*b077aed3SPierre Pronchery     {"batch", OPT_BATCH, '-',
463*b077aed3SPierre Pronchery      "Do not interactively prompt for input when a password is required etc."},
464*b077aed3SPierre Pronchery     {"repeat", OPT_REPEAT, 'p',
465*b077aed3SPierre Pronchery      "Invoke the transaction the given positive number of times. Default 1"},
466*b077aed3SPierre Pronchery     {"reqin", OPT_REQIN, 's',
467*b077aed3SPierre Pronchery      "Take sequence of CMP requests to send to server from file(s)"},
468*b077aed3SPierre Pronchery     {"reqin_new_tid", OPT_REQIN_NEW_TID, '-',
469*b077aed3SPierre Pronchery      "Use fresh transactionID for CMP requests read from -reqin"},
470*b077aed3SPierre Pronchery     {"reqout", OPT_REQOUT, 's',
471*b077aed3SPierre Pronchery      "Save sequence of CMP requests created by the client to file(s)"},
472*b077aed3SPierre Pronchery     {"rspin", OPT_RSPIN, 's',
473*b077aed3SPierre Pronchery      "Process sequence of CMP responses provided in file(s), skipping server"},
474*b077aed3SPierre Pronchery     {"rspout", OPT_RSPOUT, 's',
475*b077aed3SPierre Pronchery      "Save sequence of actually used CMP responses to file(s)"},
476*b077aed3SPierre Pronchery 
477*b077aed3SPierre Pronchery     {"use_mock_srv", OPT_USE_MOCK_SRV, '-',
478*b077aed3SPierre Pronchery      "Use internal mock server at API level, bypassing socket-based HTTP"},
479*b077aed3SPierre Pronchery 
480*b077aed3SPierre Pronchery     OPT_SECTION("Mock server"),
481*b077aed3SPierre Pronchery #ifdef OPENSSL_NO_SOCK
482*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
483*b077aed3SPierre Pronchery      "NOTE: -port and -max_msgs not supported due to no-sock build"},
484*b077aed3SPierre Pronchery #else
485*b077aed3SPierre Pronchery     {"port", OPT_PORT, 's',
486*b077aed3SPierre Pronchery      "Act as HTTP-based mock server listening on given port"},
487*b077aed3SPierre Pronchery     {"max_msgs", OPT_MAX_MSGS, 'N',
488*b077aed3SPierre Pronchery      "max number of messages handled by HTTP mock server. Default: 0 = unlimited"},
489*b077aed3SPierre Pronchery #endif
490*b077aed3SPierre Pronchery 
491*b077aed3SPierre Pronchery     {"srv_ref", OPT_SRV_REF, 's',
492*b077aed3SPierre Pronchery      "Reference value to use as senderKID of server in case no -srv_cert is given"},
493*b077aed3SPierre Pronchery     {"srv_secret", OPT_SRV_SECRET, 's',
494*b077aed3SPierre Pronchery      "Password source for server authentication with a pre-shared key (secret)"},
495*b077aed3SPierre Pronchery     {"srv_cert", OPT_SRV_CERT, 's', "Certificate of the server"},
496*b077aed3SPierre Pronchery     {"srv_key", OPT_SRV_KEY, 's',
497*b077aed3SPierre Pronchery      "Private key used by the server for signing messages"},
498*b077aed3SPierre Pronchery     {"srv_keypass", OPT_SRV_KEYPASS, 's',
499*b077aed3SPierre Pronchery      "Server private key (and cert) pass phrase source"},
500*b077aed3SPierre Pronchery 
501*b077aed3SPierre Pronchery     {"srv_trusted", OPT_SRV_TRUSTED, 's',
502*b077aed3SPierre Pronchery      "Trusted certificates for client authentication"},
503*b077aed3SPierre Pronchery     {"srv_untrusted", OPT_SRV_UNTRUSTED, 's',
504*b077aed3SPierre Pronchery      "Intermediate certs that may be useful for verifying CMP protection"},
505*b077aed3SPierre Pronchery     {"rsp_cert", OPT_RSP_CERT, 's',
506*b077aed3SPierre Pronchery      "Certificate to be returned as mock enrollment result"},
507*b077aed3SPierre Pronchery     {"rsp_extracerts", OPT_RSP_EXTRACERTS, 's',
508*b077aed3SPierre Pronchery      "Extra certificates to be included in mock certification responses"},
509*b077aed3SPierre Pronchery     {"rsp_capubs", OPT_RSP_CAPUBS, 's',
510*b077aed3SPierre Pronchery      "CA certificates to be included in mock ip response"},
511*b077aed3SPierre Pronchery     {"poll_count", OPT_POLL_COUNT, 'N',
512*b077aed3SPierre Pronchery      "Number of times the client must poll before receiving a certificate"},
513*b077aed3SPierre Pronchery     {"check_after", OPT_CHECK_AFTER, 'N',
514*b077aed3SPierre Pronchery      "The check_after value (time to wait) to include in poll response"},
515*b077aed3SPierre Pronchery     {"grant_implicitconf", OPT_GRANT_IMPLICITCONF, '-',
516*b077aed3SPierre Pronchery      "Grant implicit confirmation of newly enrolled certificate"},
517*b077aed3SPierre Pronchery 
518*b077aed3SPierre Pronchery     {"pkistatus", OPT_PKISTATUS, 'N',
519*b077aed3SPierre Pronchery      "PKIStatus to be included in server response. Possible values: 0..6"},
520*b077aed3SPierre Pronchery     {"failure", OPT_FAILURE, 'N',
521*b077aed3SPierre Pronchery      "A single failure info bit number to include in server response, 0..26"},
522*b077aed3SPierre Pronchery     {"failurebits", OPT_FAILUREBITS, 'N',
523*b077aed3SPierre Pronchery      "Number representing failure bits to include in server response, 0..2^27 - 1"},
524*b077aed3SPierre Pronchery     {"statusstring", OPT_STATUSSTRING, 's',
525*b077aed3SPierre Pronchery      "Status string to be included in server response"},
526*b077aed3SPierre Pronchery     {"send_error", OPT_SEND_ERROR, '-',
527*b077aed3SPierre Pronchery      "Force server to reply with error message"},
528*b077aed3SPierre Pronchery     {"send_unprotected", OPT_SEND_UNPROTECTED, '-',
529*b077aed3SPierre Pronchery      "Send response messages without CMP-level protection"},
530*b077aed3SPierre Pronchery     {"send_unprot_err", OPT_SEND_UNPROT_ERR, '-',
531*b077aed3SPierre Pronchery      "In case of negative responses, server shall send unprotected error messages,"},
532*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
533*b077aed3SPierre Pronchery      "certificate responses (ip/cp/kup), and revocation responses (rp)."},
534*b077aed3SPierre Pronchery     {OPT_MORE_STR, 0, 0,
535*b077aed3SPierre Pronchery      "WARNING: This setting leads to behavior violating RFC 4210"},
536*b077aed3SPierre Pronchery     {"accept_unprotected", OPT_ACCEPT_UNPROTECTED, '-',
537*b077aed3SPierre Pronchery      "Accept missing or invalid protection of requests"},
538*b077aed3SPierre Pronchery     {"accept_unprot_err", OPT_ACCEPT_UNPROT_ERR, '-',
539*b077aed3SPierre Pronchery      "Accept unprotected error messages from client"},
540*b077aed3SPierre Pronchery     {"accept_raverified", OPT_ACCEPT_RAVERIFIED, '-',
541*b077aed3SPierre Pronchery      "Accept RAVERIFIED as proof-of-possession (POPO)"},
542*b077aed3SPierre Pronchery 
543*b077aed3SPierre Pronchery     OPT_V_OPTIONS,
544*b077aed3SPierre Pronchery     {NULL}
545*b077aed3SPierre Pronchery };
546*b077aed3SPierre Pronchery 
547*b077aed3SPierre Pronchery typedef union {
548*b077aed3SPierre Pronchery     char **txt;
549*b077aed3SPierre Pronchery     int *num;
550*b077aed3SPierre Pronchery     long *num_long;
551*b077aed3SPierre Pronchery } varref;
552*b077aed3SPierre Pronchery static varref cmp_vars[] = { /* must be in same order as enumerated above! */
553*b077aed3SPierre Pronchery     {&opt_config}, {&opt_section}, {(char **)&opt_verbosity},
554*b077aed3SPierre Pronchery 
555*b077aed3SPierre Pronchery     {&opt_cmd_s}, {&opt_infotype_s}, {&opt_geninfo},
556*b077aed3SPierre Pronchery 
557*b077aed3SPierre Pronchery     {&opt_newkey}, {&opt_newkeypass}, {&opt_subject}, {&opt_issuer},
558*b077aed3SPierre Pronchery     {(char **)&opt_days}, {&opt_reqexts},
559*b077aed3SPierre Pronchery     {&opt_sans}, {(char **)&opt_san_nodefault},
560*b077aed3SPierre Pronchery     {&opt_policies}, {&opt_policy_oids}, {(char **)&opt_policy_oids_critical},
561*b077aed3SPierre Pronchery     {(char **)&opt_popo}, {&opt_csr},
562*b077aed3SPierre Pronchery     {&opt_out_trusted},
563*b077aed3SPierre Pronchery     {(char **)&opt_implicit_confirm}, {(char **)&opt_disable_confirm},
564*b077aed3SPierre Pronchery     {&opt_certout}, {&opt_chainout},
565*b077aed3SPierre Pronchery 
566*b077aed3SPierre Pronchery     {&opt_oldcert}, {(char **)&opt_revreason},
567*b077aed3SPierre Pronchery 
568*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
569*b077aed3SPierre Pronchery     {&opt_server}, {&opt_proxy}, {&opt_no_proxy},
570*b077aed3SPierre Pronchery #endif
571*b077aed3SPierre Pronchery     {&opt_recipient}, {&opt_path}, {(char **)&opt_keep_alive},
572*b077aed3SPierre Pronchery     {(char **)&opt_msg_timeout}, {(char **)&opt_total_timeout},
573*b077aed3SPierre Pronchery 
574*b077aed3SPierre Pronchery     {&opt_trusted}, {&opt_untrusted}, {&opt_srvcert},
575*b077aed3SPierre Pronchery     {&opt_expect_sender},
576*b077aed3SPierre Pronchery     {(char **)&opt_ignore_keyusage}, {(char **)&opt_unprotected_errors},
577*b077aed3SPierre Pronchery     {&opt_extracertsout}, {&opt_cacertsout},
578*b077aed3SPierre Pronchery 
579*b077aed3SPierre Pronchery     {&opt_ref}, {&opt_secret},
580*b077aed3SPierre Pronchery     {&opt_cert}, {&opt_own_trusted}, {&opt_key}, {&opt_keypass},
581*b077aed3SPierre Pronchery     {&opt_digest}, {&opt_mac}, {&opt_extracerts},
582*b077aed3SPierre Pronchery     {(char **)&opt_unprotected_requests},
583*b077aed3SPierre Pronchery 
584*b077aed3SPierre Pronchery     {&opt_certform_s}, {&opt_keyform_s},
585*b077aed3SPierre Pronchery     {&opt_otherpass},
586*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
587*b077aed3SPierre Pronchery     {&opt_engine},
588*b077aed3SPierre Pronchery #endif
589*b077aed3SPierre Pronchery 
590*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
591*b077aed3SPierre Pronchery     {(char **)&opt_tls_used}, {&opt_tls_cert}, {&opt_tls_key},
592*b077aed3SPierre Pronchery     {&opt_tls_keypass},
593*b077aed3SPierre Pronchery     {&opt_tls_extra}, {&opt_tls_trusted}, {&opt_tls_host},
594*b077aed3SPierre Pronchery #endif
595*b077aed3SPierre Pronchery 
596*b077aed3SPierre Pronchery     {(char **)&opt_batch}, {(char **)&opt_repeat},
597*b077aed3SPierre Pronchery     {&opt_reqin}, {(char **)&opt_reqin_new_tid},
598*b077aed3SPierre Pronchery     {&opt_reqout}, {&opt_rspin}, {&opt_rspout},
599*b077aed3SPierre Pronchery 
600*b077aed3SPierre Pronchery     {(char **)&opt_use_mock_srv},
601*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
602*b077aed3SPierre Pronchery     {&opt_port}, {(char **)&opt_max_msgs},
603*b077aed3SPierre Pronchery #endif
604*b077aed3SPierre Pronchery     {&opt_srv_ref}, {&opt_srv_secret},
605*b077aed3SPierre Pronchery     {&opt_srv_cert}, {&opt_srv_key}, {&opt_srv_keypass},
606*b077aed3SPierre Pronchery     {&opt_srv_trusted}, {&opt_srv_untrusted},
607*b077aed3SPierre Pronchery     {&opt_rsp_cert}, {&opt_rsp_extracerts}, {&opt_rsp_capubs},
608*b077aed3SPierre Pronchery     {(char **)&opt_poll_count}, {(char **)&opt_check_after},
609*b077aed3SPierre Pronchery     {(char **)&opt_grant_implicitconf},
610*b077aed3SPierre Pronchery     {(char **)&opt_pkistatus}, {(char **)&opt_failure},
611*b077aed3SPierre Pronchery     {(char **)&opt_failurebits}, {&opt_statusstring},
612*b077aed3SPierre Pronchery     {(char **)&opt_send_error}, {(char **)&opt_send_unprotected},
613*b077aed3SPierre Pronchery     {(char **)&opt_send_unprot_err}, {(char **)&opt_accept_unprotected},
614*b077aed3SPierre Pronchery     {(char **)&opt_accept_unprot_err}, {(char **)&opt_accept_raverified},
615*b077aed3SPierre Pronchery 
616*b077aed3SPierre Pronchery     {NULL}
617*b077aed3SPierre Pronchery };
618*b077aed3SPierre Pronchery 
619*b077aed3SPierre Pronchery #define FUNC (strcmp(OPENSSL_FUNC, "(unknown function)") == 0   \
620*b077aed3SPierre Pronchery               ? "CMP" : OPENSSL_FUNC)
621*b077aed3SPierre Pronchery #define CMP_print(bio, level, prefix, msg, a1, a2, a3) \
622*b077aed3SPierre Pronchery     ((void)(level > opt_verbosity ? 0 : \
623*b077aed3SPierre Pronchery             (BIO_printf(bio, "%s:%s:%d:CMP %s: " msg "\n", \
624*b077aed3SPierre Pronchery                         FUNC, OPENSSL_FILE, OPENSSL_LINE, prefix, a1, a2, a3))))
625*b077aed3SPierre Pronchery #define CMP_DEBUG(m, a1, a2, a3) \
626*b077aed3SPierre Pronchery     CMP_print(bio_out, OSSL_CMP_LOG_DEBUG, "debug", m, a1, a2, a3)
627*b077aed3SPierre Pronchery #define CMP_debug(msg)             CMP_DEBUG(msg"%s%s%s", "", "", "")
628*b077aed3SPierre Pronchery #define CMP_debug1(msg, a1)        CMP_DEBUG(msg"%s%s",   a1, "", "")
629*b077aed3SPierre Pronchery #define CMP_debug2(msg, a1, a2)    CMP_DEBUG(msg"%s",     a1, a2, "")
630*b077aed3SPierre Pronchery #define CMP_debug3(msg, a1, a2, a3) CMP_DEBUG(msg,        a1, a2, a3)
631*b077aed3SPierre Pronchery #define CMP_INFO(msg, a1, a2, a3) \
632*b077aed3SPierre Pronchery     CMP_print(bio_out, OSSL_CMP_LOG_INFO, "info", msg, a1, a2, a3)
633*b077aed3SPierre Pronchery #define CMP_info(msg)              CMP_INFO(msg"%s%s%s", "", "", "")
634*b077aed3SPierre Pronchery #define CMP_info1(msg, a1)         CMP_INFO(msg"%s%s",   a1, "", "")
635*b077aed3SPierre Pronchery #define CMP_info2(msg, a1, a2)     CMP_INFO(msg"%s",     a1, a2, "")
636*b077aed3SPierre Pronchery #define CMP_info3(msg, a1, a2, a3) CMP_INFO(msg,         a1, a2, a3)
637*b077aed3SPierre Pronchery #define CMP_WARN(m, a1, a2, a3) \
638*b077aed3SPierre Pronchery     CMP_print(bio_out, OSSL_CMP_LOG_WARNING, "warning", m, a1, a2, a3)
639*b077aed3SPierre Pronchery #define CMP_warn(msg)              CMP_WARN(msg"%s%s%s", "", "", "")
640*b077aed3SPierre Pronchery #define CMP_warn1(msg, a1)         CMP_WARN(msg"%s%s",   a1, "", "")
641*b077aed3SPierre Pronchery #define CMP_warn2(msg, a1, a2)     CMP_WARN(msg"%s",     a1, a2, "")
642*b077aed3SPierre Pronchery #define CMP_warn3(msg, a1, a2, a3) CMP_WARN(msg,         a1, a2, a3)
643*b077aed3SPierre Pronchery #define CMP_ERR(msg, a1, a2, a3) \
644*b077aed3SPierre Pronchery     CMP_print(bio_err, OSSL_CMP_LOG_ERR, "error", msg, a1, a2, a3)
645*b077aed3SPierre Pronchery #define CMP_err(msg)               CMP_ERR(msg"%s%s%s", "", "", "")
646*b077aed3SPierre Pronchery #define CMP_err1(msg, a1)          CMP_ERR(msg"%s%s",   a1, "", "")
647*b077aed3SPierre Pronchery #define CMP_err2(msg, a1, a2)      CMP_ERR(msg"%s",     a1, a2, "")
648*b077aed3SPierre Pronchery #define CMP_err3(msg, a1, a2, a3)  CMP_ERR(msg,         a1, a2, a3)
649*b077aed3SPierre Pronchery 
650*b077aed3SPierre Pronchery static int print_to_bio_out(const char *func, const char *file, int line,
651*b077aed3SPierre Pronchery                             OSSL_CMP_severity level, const char *msg)
652*b077aed3SPierre Pronchery {
653*b077aed3SPierre Pronchery     return OSSL_CMP_print_to_bio(bio_out, func, file, line, level, msg);
654*b077aed3SPierre Pronchery }
655*b077aed3SPierre Pronchery 
656*b077aed3SPierre Pronchery static int print_to_bio_err(const char *func, const char *file, int line,
657*b077aed3SPierre Pronchery                             OSSL_CMP_severity level, const char *msg)
658*b077aed3SPierre Pronchery {
659*b077aed3SPierre Pronchery     return OSSL_CMP_print_to_bio(bio_err, func, file, line, level, msg);
660*b077aed3SPierre Pronchery }
661*b077aed3SPierre Pronchery 
662*b077aed3SPierre Pronchery static int set_verbosity(int level)
663*b077aed3SPierre Pronchery {
664*b077aed3SPierre Pronchery     if (level < OSSL_CMP_LOG_EMERG || level > OSSL_CMP_LOG_MAX) {
665*b077aed3SPierre Pronchery         CMP_err1("Logging verbosity level %d out of range (0 .. 8)", level);
666*b077aed3SPierre Pronchery         return 0;
667*b077aed3SPierre Pronchery     }
668*b077aed3SPierre Pronchery     opt_verbosity = level;
669*b077aed3SPierre Pronchery     return 1;
670*b077aed3SPierre Pronchery }
671*b077aed3SPierre Pronchery 
672*b077aed3SPierre Pronchery static EVP_PKEY *load_key_pwd(const char *uri, int format,
673*b077aed3SPierre Pronchery                               const char *pass, ENGINE *eng, const char *desc)
674*b077aed3SPierre Pronchery {
675*b077aed3SPierre Pronchery     char *pass_string = get_passwd(pass, desc);
676*b077aed3SPierre Pronchery     EVP_PKEY *pkey = load_key(uri, format, 0, pass_string, eng, desc);
677*b077aed3SPierre Pronchery 
678*b077aed3SPierre Pronchery     clear_free(pass_string);
679*b077aed3SPierre Pronchery     return pkey;
680*b077aed3SPierre Pronchery }
681*b077aed3SPierre Pronchery 
682*b077aed3SPierre Pronchery static X509 *load_cert_pwd(const char *uri, const char *pass, const char *desc)
683*b077aed3SPierre Pronchery {
684*b077aed3SPierre Pronchery     X509 *cert;
685*b077aed3SPierre Pronchery     char *pass_string = get_passwd(pass, desc);
686*b077aed3SPierre Pronchery 
687*b077aed3SPierre Pronchery     cert = load_cert_pass(uri, FORMAT_UNDEF, 0, pass_string, desc);
688*b077aed3SPierre Pronchery     clear_free(pass_string);
689*b077aed3SPierre Pronchery     return cert;
690*b077aed3SPierre Pronchery }
691*b077aed3SPierre Pronchery 
692*b077aed3SPierre Pronchery static X509_REQ *load_csr_autofmt(const char *infile, const char *desc)
693*b077aed3SPierre Pronchery {
694*b077aed3SPierre Pronchery     X509_REQ *csr;
695*b077aed3SPierre Pronchery     BIO *bio_bak = bio_err;
696*b077aed3SPierre Pronchery 
697*b077aed3SPierre Pronchery     bio_err = NULL; /* do not show errors on more than one try */
698*b077aed3SPierre Pronchery     csr = load_csr(infile, FORMAT_PEM, desc);
699*b077aed3SPierre Pronchery     bio_err = bio_bak;
700*b077aed3SPierre Pronchery     if (csr == NULL) {
701*b077aed3SPierre Pronchery         ERR_clear_error();
702*b077aed3SPierre Pronchery         csr = load_csr(infile, FORMAT_ASN1, desc);
703*b077aed3SPierre Pronchery     }
704*b077aed3SPierre Pronchery     if (csr == NULL) {
705*b077aed3SPierre Pronchery         ERR_print_errors(bio_err);
706*b077aed3SPierre Pronchery         BIO_printf(bio_err, "error: unable to load %s from file '%s'\n", desc,
707*b077aed3SPierre Pronchery                    infile);
708*b077aed3SPierre Pronchery     } else {
709*b077aed3SPierre Pronchery         EVP_PKEY *pkey = X509_REQ_get0_pubkey(csr);
710*b077aed3SPierre Pronchery         int ret = do_X509_REQ_verify(csr, pkey, NULL /* vfyopts */);
711*b077aed3SPierre Pronchery 
712*b077aed3SPierre Pronchery         if (pkey == NULL || ret < 0)
713*b077aed3SPierre Pronchery             CMP_warn("error while verifying CSR self-signature");
714*b077aed3SPierre Pronchery         else if (ret == 0)
715*b077aed3SPierre Pronchery             CMP_warn("CSR self-signature does not match the contents");
716*b077aed3SPierre Pronchery     }
717*b077aed3SPierre Pronchery     return csr;
718*b077aed3SPierre Pronchery }
719*b077aed3SPierre Pronchery 
720*b077aed3SPierre Pronchery /* set expected host name/IP addr and clears the email addr in the given ts */
721*b077aed3SPierre Pronchery static int truststore_set_host_etc(X509_STORE *ts, const char *host)
722*b077aed3SPierre Pronchery {
723*b077aed3SPierre Pronchery     X509_VERIFY_PARAM *ts_vpm = X509_STORE_get0_param(ts);
724*b077aed3SPierre Pronchery 
725*b077aed3SPierre Pronchery     /* first clear any host names, IP, and email addresses */
726*b077aed3SPierre Pronchery     if (!X509_VERIFY_PARAM_set1_host(ts_vpm, NULL, 0)
727*b077aed3SPierre Pronchery             || !X509_VERIFY_PARAM_set1_ip(ts_vpm, NULL, 0)
728*b077aed3SPierre Pronchery             || !X509_VERIFY_PARAM_set1_email(ts_vpm, NULL, 0))
729*b077aed3SPierre Pronchery         return 0;
730*b077aed3SPierre Pronchery     X509_VERIFY_PARAM_set_hostflags(ts_vpm,
731*b077aed3SPierre Pronchery                                     X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT |
732*b077aed3SPierre Pronchery                                     X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
733*b077aed3SPierre Pronchery     return (host != NULL && X509_VERIFY_PARAM_set1_ip_asc(ts_vpm, host))
734*b077aed3SPierre Pronchery         || X509_VERIFY_PARAM_set1_host(ts_vpm, host, 0);
735*b077aed3SPierre Pronchery }
736*b077aed3SPierre Pronchery 
737*b077aed3SPierre Pronchery /* write OSSL_CMP_MSG DER-encoded to the specified file name item */
738*b077aed3SPierre Pronchery static int write_PKIMESSAGE(const OSSL_CMP_MSG *msg, char **filenames)
739*b077aed3SPierre Pronchery {
740*b077aed3SPierre Pronchery     char *file;
741*b077aed3SPierre Pronchery 
742*b077aed3SPierre Pronchery     if (msg == NULL || filenames == NULL) {
743*b077aed3SPierre Pronchery         CMP_err("NULL arg to write_PKIMESSAGE");
744*b077aed3SPierre Pronchery         return 0;
745*b077aed3SPierre Pronchery     }
746*b077aed3SPierre Pronchery     if (*filenames == NULL) {
747*b077aed3SPierre Pronchery         CMP_err("not enough file names provided for writing PKIMessage");
748*b077aed3SPierre Pronchery         return 0;
749*b077aed3SPierre Pronchery     }
750*b077aed3SPierre Pronchery 
751*b077aed3SPierre Pronchery     file = *filenames;
752*b077aed3SPierre Pronchery     *filenames = next_item(file);
753*b077aed3SPierre Pronchery     if (OSSL_CMP_MSG_write(file, msg) < 0) {
754*b077aed3SPierre Pronchery         CMP_err1("cannot write PKIMessage to file '%s'", file);
755*b077aed3SPierre Pronchery         return 0;
756*b077aed3SPierre Pronchery     }
757*b077aed3SPierre Pronchery     return 1;
758*b077aed3SPierre Pronchery }
759*b077aed3SPierre Pronchery 
760*b077aed3SPierre Pronchery /* read DER-encoded OSSL_CMP_MSG from the specified file name item */
761*b077aed3SPierre Pronchery static OSSL_CMP_MSG *read_PKIMESSAGE(const char *desc, char **filenames)
762*b077aed3SPierre Pronchery {
763*b077aed3SPierre Pronchery     char *file;
764*b077aed3SPierre Pronchery     OSSL_CMP_MSG *ret;
765*b077aed3SPierre Pronchery 
766*b077aed3SPierre Pronchery     if (filenames == NULL || desc == NULL) {
767*b077aed3SPierre Pronchery         CMP_err("NULL arg to read_PKIMESSAGE");
768*b077aed3SPierre Pronchery         return NULL;
769*b077aed3SPierre Pronchery     }
770*b077aed3SPierre Pronchery     if (*filenames == NULL) {
771*b077aed3SPierre Pronchery         CMP_err("not enough file names provided for reading PKIMessage");
772*b077aed3SPierre Pronchery         return NULL;
773*b077aed3SPierre Pronchery     }
774*b077aed3SPierre Pronchery 
775*b077aed3SPierre Pronchery     file = *filenames;
776*b077aed3SPierre Pronchery     *filenames = next_item(file);
777*b077aed3SPierre Pronchery 
778*b077aed3SPierre Pronchery     ret = OSSL_CMP_MSG_read(file, app_get0_libctx(), app_get0_propq());
779*b077aed3SPierre Pronchery     if (ret == NULL)
780*b077aed3SPierre Pronchery         CMP_err1("cannot read PKIMessage from file '%s'", file);
781*b077aed3SPierre Pronchery     else
782*b077aed3SPierre Pronchery         CMP_info2("%s %s", desc, file);
783*b077aed3SPierre Pronchery     return ret;
784*b077aed3SPierre Pronchery }
785*b077aed3SPierre Pronchery 
786*b077aed3SPierre Pronchery /*-
787*b077aed3SPierre Pronchery  * Sends the PKIMessage req and on success place the response in *res
788*b077aed3SPierre Pronchery  * basically like OSSL_CMP_MSG_http_perform(), but in addition allows
789*b077aed3SPierre Pronchery  * to dump the sequence of requests and responses to files and/or
790*b077aed3SPierre Pronchery  * to take the sequence of requests and responses from files.
791*b077aed3SPierre Pronchery  */
792*b077aed3SPierre Pronchery static OSSL_CMP_MSG *read_write_req_resp(OSSL_CMP_CTX *ctx,
793*b077aed3SPierre Pronchery                                          const OSSL_CMP_MSG *req)
794*b077aed3SPierre Pronchery {
795*b077aed3SPierre Pronchery     OSSL_CMP_MSG *req_new = NULL;
796*b077aed3SPierre Pronchery     OSSL_CMP_MSG *res = NULL;
797*b077aed3SPierre Pronchery     OSSL_CMP_PKIHEADER *hdr;
798*b077aed3SPierre Pronchery     const char *prev_opt_rspin = opt_rspin;
799*b077aed3SPierre Pronchery 
800*b077aed3SPierre Pronchery     if (req != NULL && opt_reqout != NULL
801*b077aed3SPierre Pronchery             && !write_PKIMESSAGE(req, &opt_reqout))
802*b077aed3SPierre Pronchery         goto err;
803*b077aed3SPierre Pronchery     if (opt_reqin != NULL && opt_rspin == NULL) {
804*b077aed3SPierre Pronchery         if ((req_new = read_PKIMESSAGE("actually sending", &opt_reqin)) == NULL)
805*b077aed3SPierre Pronchery             goto err;
806*b077aed3SPierre Pronchery         /*-
807*b077aed3SPierre Pronchery          * The transaction ID in req_new read from opt_reqin may not be fresh.
808*b077aed3SPierre Pronchery          * In this case the server may complain "Transaction id already in use."
809*b077aed3SPierre Pronchery          * The following workaround unfortunately requires re-protection.
810*b077aed3SPierre Pronchery          */
811*b077aed3SPierre Pronchery         if (opt_reqin_new_tid
812*b077aed3SPierre Pronchery                 && !OSSL_CMP_MSG_update_transactionID(ctx, req_new))
813*b077aed3SPierre Pronchery             goto err;
814*b077aed3SPierre Pronchery 
815*b077aed3SPierre Pronchery         /*
816*b077aed3SPierre Pronchery          * Except for first request, need to satisfy recipNonce check by server.
817*b077aed3SPierre Pronchery          * Unfortunately requires re-protection if protection is required.
818*b077aed3SPierre Pronchery          */
819*b077aed3SPierre Pronchery         if (!OSSL_CMP_MSG_update_recipNonce(ctx, req_new))
820*b077aed3SPierre Pronchery             goto err;
821*b077aed3SPierre Pronchery     }
822*b077aed3SPierre Pronchery 
823*b077aed3SPierre Pronchery     if (opt_rspin != NULL) {
824*b077aed3SPierre Pronchery         res = read_PKIMESSAGE("actually using", &opt_rspin);
825*b077aed3SPierre Pronchery     } else {
826*b077aed3SPierre Pronchery         const OSSL_CMP_MSG *actual_req = req_new != NULL ? req_new : req;
827*b077aed3SPierre Pronchery 
828*b077aed3SPierre Pronchery         if (opt_use_mock_srv) {
829*b077aed3SPierre Pronchery             if (rspin_in_use)
830*b077aed3SPierre Pronchery                 CMP_warn("too few -rspin filename arguments; resorting to using mock server");
831*b077aed3SPierre Pronchery             res = OSSL_CMP_CTX_server_perform(ctx, actual_req);
832*b077aed3SPierre Pronchery         } else {
833*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
834*b077aed3SPierre Pronchery             if (opt_server == NULL) {
835*b077aed3SPierre Pronchery                 CMP_err("missing -server or -use_mock_srv option, or too few -rspin filename arguments");
836*b077aed3SPierre Pronchery                 goto err;
837*b077aed3SPierre Pronchery             }
838*b077aed3SPierre Pronchery             if (rspin_in_use)
839*b077aed3SPierre Pronchery                 CMP_warn("too few -rspin filename arguments; resorting to contacting server");
840*b077aed3SPierre Pronchery             res = OSSL_CMP_MSG_http_perform(ctx, actual_req);
841*b077aed3SPierre Pronchery #else
842*b077aed3SPierre Pronchery             CMP_err("-server not supported on no-sock build; missing -use_mock_srv option or too few -rspin filename arguments");
843*b077aed3SPierre Pronchery #endif
844*b077aed3SPierre Pronchery         }
845*b077aed3SPierre Pronchery         rspin_in_use = 0;
846*b077aed3SPierre Pronchery     }
847*b077aed3SPierre Pronchery     if (res == NULL)
848*b077aed3SPierre Pronchery         goto err;
849*b077aed3SPierre Pronchery 
850*b077aed3SPierre Pronchery     if (req_new != NULL || prev_opt_rspin != NULL) {
851*b077aed3SPierre Pronchery         /* need to satisfy nonce and transactionID checks by client */
852*b077aed3SPierre Pronchery         ASN1_OCTET_STRING *nonce;
853*b077aed3SPierre Pronchery         ASN1_OCTET_STRING *tid;
854*b077aed3SPierre Pronchery 
855*b077aed3SPierre Pronchery         hdr = OSSL_CMP_MSG_get0_header(res);
856*b077aed3SPierre Pronchery         nonce = OSSL_CMP_HDR_get0_recipNonce(hdr);
857*b077aed3SPierre Pronchery         tid = OSSL_CMP_HDR_get0_transactionID(hdr);
858*b077aed3SPierre Pronchery         if (!OSSL_CMP_CTX_set1_senderNonce(ctx, nonce)
859*b077aed3SPierre Pronchery                 || !OSSL_CMP_CTX_set1_transactionID(ctx, tid)) {
860*b077aed3SPierre Pronchery             OSSL_CMP_MSG_free(res);
861*b077aed3SPierre Pronchery             res = NULL;
862*b077aed3SPierre Pronchery             goto err;
863*b077aed3SPierre Pronchery         }
864*b077aed3SPierre Pronchery     }
865*b077aed3SPierre Pronchery 
866*b077aed3SPierre Pronchery     if (opt_rspout != NULL && !write_PKIMESSAGE(res, &opt_rspout)) {
867*b077aed3SPierre Pronchery         OSSL_CMP_MSG_free(res);
868*b077aed3SPierre Pronchery         res = NULL;
869*b077aed3SPierre Pronchery     }
870*b077aed3SPierre Pronchery 
871*b077aed3SPierre Pronchery  err:
872*b077aed3SPierre Pronchery     OSSL_CMP_MSG_free(req_new);
873*b077aed3SPierre Pronchery     return res;
874*b077aed3SPierre Pronchery }
875*b077aed3SPierre Pronchery 
876*b077aed3SPierre Pronchery static int set_name(const char *str,
877*b077aed3SPierre Pronchery                     int (*set_fn) (OSSL_CMP_CTX *ctx, const X509_NAME *name),
878*b077aed3SPierre Pronchery                     OSSL_CMP_CTX *ctx, const char *desc)
879*b077aed3SPierre Pronchery {
880*b077aed3SPierre Pronchery     if (str != NULL) {
881*b077aed3SPierre Pronchery         X509_NAME *n = parse_name(str, MBSTRING_ASC, 1, desc);
882*b077aed3SPierre Pronchery 
883*b077aed3SPierre Pronchery         if (n == NULL)
884*b077aed3SPierre Pronchery             return 0;
885*b077aed3SPierre Pronchery         if (!(*set_fn) (ctx, n)) {
886*b077aed3SPierre Pronchery             X509_NAME_free(n);
887*b077aed3SPierre Pronchery             CMP_err("out of memory");
888*b077aed3SPierre Pronchery             return 0;
889*b077aed3SPierre Pronchery         }
890*b077aed3SPierre Pronchery         X509_NAME_free(n);
891*b077aed3SPierre Pronchery     }
892*b077aed3SPierre Pronchery     return 1;
893*b077aed3SPierre Pronchery }
894*b077aed3SPierre Pronchery 
895*b077aed3SPierre Pronchery static int set_gennames(OSSL_CMP_CTX *ctx, char *names, const char *desc)
896*b077aed3SPierre Pronchery {
897*b077aed3SPierre Pronchery     char *next;
898*b077aed3SPierre Pronchery 
899*b077aed3SPierre Pronchery     for (; names != NULL; names = next) {
900*b077aed3SPierre Pronchery         GENERAL_NAME *n;
901*b077aed3SPierre Pronchery 
902*b077aed3SPierre Pronchery         next = next_item(names);
903*b077aed3SPierre Pronchery         if (strcmp(names, "critical") == 0) {
904*b077aed3SPierre Pronchery             (void)OSSL_CMP_CTX_set_option(ctx,
905*b077aed3SPierre Pronchery                                           OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL,
906*b077aed3SPierre Pronchery                                           1);
907*b077aed3SPierre Pronchery             continue;
908*b077aed3SPierre Pronchery         }
909*b077aed3SPierre Pronchery 
910*b077aed3SPierre Pronchery         /* try IP address first, then URI or domain name */
911*b077aed3SPierre Pronchery         (void)ERR_set_mark();
912*b077aed3SPierre Pronchery         n = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_IPADD, names, 0);
913*b077aed3SPierre Pronchery         if (n == NULL)
914*b077aed3SPierre Pronchery             n = a2i_GENERAL_NAME(NULL, NULL, NULL,
915*b077aed3SPierre Pronchery                                  strchr(names, ':') != NULL ? GEN_URI : GEN_DNS,
916*b077aed3SPierre Pronchery                                  names, 0);
917*b077aed3SPierre Pronchery         (void)ERR_pop_to_mark();
918*b077aed3SPierre Pronchery 
919*b077aed3SPierre Pronchery         if (n == NULL) {
920*b077aed3SPierre Pronchery             CMP_err2("bad syntax of %s '%s'", desc, names);
921*b077aed3SPierre Pronchery             return 0;
922*b077aed3SPierre Pronchery         }
923*b077aed3SPierre Pronchery         if (!OSSL_CMP_CTX_push1_subjectAltName(ctx, n)) {
924*b077aed3SPierre Pronchery             GENERAL_NAME_free(n);
925*b077aed3SPierre Pronchery             CMP_err("out of memory");
926*b077aed3SPierre Pronchery             return 0;
927*b077aed3SPierre Pronchery         }
928*b077aed3SPierre Pronchery         GENERAL_NAME_free(n);
929*b077aed3SPierre Pronchery     }
930*b077aed3SPierre Pronchery     return 1;
931*b077aed3SPierre Pronchery }
932*b077aed3SPierre Pronchery 
933*b077aed3SPierre Pronchery static X509_STORE *load_trusted(char *input, int for_new_cert, const char *desc)
934*b077aed3SPierre Pronchery {
935*b077aed3SPierre Pronchery     X509_STORE *ts = load_certstore(input, opt_otherpass, desc, vpm);
936*b077aed3SPierre Pronchery 
937*b077aed3SPierre Pronchery     if (ts == NULL)
938*b077aed3SPierre Pronchery         return NULL;
939*b077aed3SPierre Pronchery     X509_STORE_set_verify_cb(ts, X509_STORE_CTX_print_verify_cb);
940*b077aed3SPierre Pronchery 
941*b077aed3SPierre Pronchery     /* copy vpm to store */
942*b077aed3SPierre Pronchery     if (X509_STORE_set1_param(ts, vpm /* may be NULL */)
943*b077aed3SPierre Pronchery             && (for_new_cert || truststore_set_host_etc(ts, NULL)))
944*b077aed3SPierre Pronchery         return ts;
945*b077aed3SPierre Pronchery     BIO_printf(bio_err, "error setting verification parameters for %s\n", desc);
946*b077aed3SPierre Pronchery     OSSL_CMP_CTX_print_errors(cmp_ctx);
947*b077aed3SPierre Pronchery     X509_STORE_free(ts);
948*b077aed3SPierre Pronchery     return NULL;
949*b077aed3SPierre Pronchery }
950*b077aed3SPierre Pronchery 
951*b077aed3SPierre Pronchery typedef int (*add_X509_stack_fn_t)(void *ctx, const STACK_OF(X509) *certs);
952*b077aed3SPierre Pronchery 
953*b077aed3SPierre Pronchery static int setup_certs(char *files, const char *desc, void *ctx,
954*b077aed3SPierre Pronchery                        add_X509_stack_fn_t set1_fn)
955*b077aed3SPierre Pronchery {
956*b077aed3SPierre Pronchery     STACK_OF(X509) *certs;
957*b077aed3SPierre Pronchery     int ok;
958*b077aed3SPierre Pronchery 
959*b077aed3SPierre Pronchery     if (files == NULL)
960*b077aed3SPierre Pronchery         return 1;
961*b077aed3SPierre Pronchery     if ((certs = load_certs_multifile(files, opt_otherpass, desc, vpm)) == NULL)
962*b077aed3SPierre Pronchery         return 0;
963*b077aed3SPierre Pronchery     ok = (*set1_fn)(ctx, certs);
964*b077aed3SPierre Pronchery     sk_X509_pop_free(certs, X509_free);
965*b077aed3SPierre Pronchery     return ok;
966*b077aed3SPierre Pronchery }
967*b077aed3SPierre Pronchery 
968*b077aed3SPierre Pronchery 
969*b077aed3SPierre Pronchery /*
970*b077aed3SPierre Pronchery  * parse and transform some options, checking their syntax.
971*b077aed3SPierre Pronchery  * Returns 1 on success, 0 on error
972*b077aed3SPierre Pronchery  */
973*b077aed3SPierre Pronchery static int transform_opts(void)
974*b077aed3SPierre Pronchery {
975*b077aed3SPierre Pronchery     if (opt_cmd_s != NULL) {
976*b077aed3SPierre Pronchery         if (!strcmp(opt_cmd_s, "ir")) {
977*b077aed3SPierre Pronchery             opt_cmd = CMP_IR;
978*b077aed3SPierre Pronchery         } else if (!strcmp(opt_cmd_s, "kur")) {
979*b077aed3SPierre Pronchery             opt_cmd = CMP_KUR;
980*b077aed3SPierre Pronchery         } else if (!strcmp(opt_cmd_s, "cr")) {
981*b077aed3SPierre Pronchery             opt_cmd = CMP_CR;
982*b077aed3SPierre Pronchery         } else if (!strcmp(opt_cmd_s, "p10cr")) {
983*b077aed3SPierre Pronchery             opt_cmd = CMP_P10CR;
984*b077aed3SPierre Pronchery         } else if (!strcmp(opt_cmd_s, "rr")) {
985*b077aed3SPierre Pronchery             opt_cmd = CMP_RR;
986*b077aed3SPierre Pronchery         } else if (!strcmp(opt_cmd_s, "genm")) {
987*b077aed3SPierre Pronchery             opt_cmd = CMP_GENM;
988*b077aed3SPierre Pronchery         } else {
989*b077aed3SPierre Pronchery             CMP_err1("unknown cmp command '%s'", opt_cmd_s);
990*b077aed3SPierre Pronchery             return 0;
991*b077aed3SPierre Pronchery         }
992*b077aed3SPierre Pronchery     } else {
993*b077aed3SPierre Pronchery         CMP_err("no cmp command to execute");
994*b077aed3SPierre Pronchery         return 0;
995*b077aed3SPierre Pronchery     }
996*b077aed3SPierre Pronchery 
997*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
998*b077aed3SPierre Pronchery # define FORMAT_OPTIONS (OPT_FMT_PEMDER | OPT_FMT_PKCS12 | OPT_FMT_ENGINE)
999*b077aed3SPierre Pronchery #else
1000*b077aed3SPierre Pronchery # define FORMAT_OPTIONS (OPT_FMT_PEMDER | OPT_FMT_PKCS12)
1001*b077aed3SPierre Pronchery #endif
1002*b077aed3SPierre Pronchery 
1003*b077aed3SPierre Pronchery     if (opt_keyform_s != NULL
1004*b077aed3SPierre Pronchery             && !opt_format(opt_keyform_s, FORMAT_OPTIONS, &opt_keyform)) {
1005*b077aed3SPierre Pronchery         CMP_err("unknown option given for key loading format");
1006*b077aed3SPierre Pronchery         return 0;
1007*b077aed3SPierre Pronchery     }
1008*b077aed3SPierre Pronchery 
1009*b077aed3SPierre Pronchery #undef FORMAT_OPTIONS
1010*b077aed3SPierre Pronchery 
1011*b077aed3SPierre Pronchery     if (opt_certform_s != NULL
1012*b077aed3SPierre Pronchery             && !opt_format(opt_certform_s, OPT_FMT_PEMDER, &opt_certform)) {
1013*b077aed3SPierre Pronchery         CMP_err("unknown option given for certificate storing format");
1014*b077aed3SPierre Pronchery         return 0;
1015*b077aed3SPierre Pronchery     }
1016*b077aed3SPierre Pronchery 
1017*b077aed3SPierre Pronchery     return 1;
1018*b077aed3SPierre Pronchery }
1019*b077aed3SPierre Pronchery 
1020*b077aed3SPierre Pronchery static OSSL_CMP_SRV_CTX *setup_srv_ctx(ENGINE *engine)
1021*b077aed3SPierre Pronchery {
1022*b077aed3SPierre Pronchery     OSSL_CMP_CTX *ctx; /* extra CMP (client) ctx partly used by server */
1023*b077aed3SPierre Pronchery     OSSL_CMP_SRV_CTX *srv_ctx = ossl_cmp_mock_srv_new(app_get0_libctx(),
1024*b077aed3SPierre Pronchery                                                       app_get0_propq());
1025*b077aed3SPierre Pronchery 
1026*b077aed3SPierre Pronchery     if (srv_ctx == NULL)
1027*b077aed3SPierre Pronchery         return NULL;
1028*b077aed3SPierre Pronchery     ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx);
1029*b077aed3SPierre Pronchery 
1030*b077aed3SPierre Pronchery     if (opt_srv_ref == NULL) {
1031*b077aed3SPierre Pronchery         if (opt_srv_cert == NULL) {
1032*b077aed3SPierre Pronchery             /* opt_srv_cert should determine the sender */
1033*b077aed3SPierre Pronchery             CMP_err("must give -srv_ref for mock server if no -srv_cert given");
1034*b077aed3SPierre Pronchery             goto err;
1035*b077aed3SPierre Pronchery         }
1036*b077aed3SPierre Pronchery     } else {
1037*b077aed3SPierre Pronchery         if (!OSSL_CMP_CTX_set1_referenceValue(ctx, (unsigned char *)opt_srv_ref,
1038*b077aed3SPierre Pronchery                                               strlen(opt_srv_ref)))
1039*b077aed3SPierre Pronchery             goto err;
1040*b077aed3SPierre Pronchery     }
1041*b077aed3SPierre Pronchery 
1042*b077aed3SPierre Pronchery     if (opt_srv_secret != NULL) {
1043*b077aed3SPierre Pronchery         int res;
1044*b077aed3SPierre Pronchery         char *pass_str = get_passwd(opt_srv_secret, "PBMAC secret of mock server");
1045*b077aed3SPierre Pronchery 
1046*b077aed3SPierre Pronchery         if (pass_str != NULL) {
1047*b077aed3SPierre Pronchery             cleanse(opt_srv_secret);
1048*b077aed3SPierre Pronchery             res = OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)pass_str,
1049*b077aed3SPierre Pronchery                                                 strlen(pass_str));
1050*b077aed3SPierre Pronchery             clear_free(pass_str);
1051*b077aed3SPierre Pronchery             if (res == 0)
1052*b077aed3SPierre Pronchery                 goto err;
1053*b077aed3SPierre Pronchery         }
1054*b077aed3SPierre Pronchery     } else if (opt_srv_cert == NULL) {
1055*b077aed3SPierre Pronchery         CMP_err("server credentials (-srv_secret or -srv_cert) must be given if -use_mock_srv or -port is used");
1056*b077aed3SPierre Pronchery         goto err;
1057*b077aed3SPierre Pronchery     } else {
1058*b077aed3SPierre Pronchery         CMP_warn("server will not be able to handle PBM-protected requests since -srv_secret is not given");
1059*b077aed3SPierre Pronchery     }
1060*b077aed3SPierre Pronchery 
1061*b077aed3SPierre Pronchery     if (opt_srv_secret == NULL
1062*b077aed3SPierre Pronchery             && ((opt_srv_cert == NULL) != (opt_srv_key == NULL))) {
1063*b077aed3SPierre Pronchery         CMP_err("must give both -srv_cert and -srv_key options or neither");
1064*b077aed3SPierre Pronchery         goto err;
1065*b077aed3SPierre Pronchery     }
1066*b077aed3SPierre Pronchery     if (opt_srv_cert != NULL) {
1067*b077aed3SPierre Pronchery         X509 *srv_cert = load_cert_pwd(opt_srv_cert, opt_srv_keypass,
1068*b077aed3SPierre Pronchery                                        "certificate of the mock server");
1069*b077aed3SPierre Pronchery 
1070*b077aed3SPierre Pronchery         if (srv_cert == NULL || !OSSL_CMP_CTX_set1_cert(ctx, srv_cert)) {
1071*b077aed3SPierre Pronchery             X509_free(srv_cert);
1072*b077aed3SPierre Pronchery             goto err;
1073*b077aed3SPierre Pronchery         }
1074*b077aed3SPierre Pronchery         X509_free(srv_cert);
1075*b077aed3SPierre Pronchery     }
1076*b077aed3SPierre Pronchery     if (opt_srv_key != NULL) {
1077*b077aed3SPierre Pronchery         EVP_PKEY *pkey = load_key_pwd(opt_srv_key, opt_keyform,
1078*b077aed3SPierre Pronchery                                       opt_srv_keypass,
1079*b077aed3SPierre Pronchery                                       engine, "private key for mock server cert");
1080*b077aed3SPierre Pronchery 
1081*b077aed3SPierre Pronchery         if (pkey == NULL || !OSSL_CMP_CTX_set1_pkey(ctx, pkey)) {
1082*b077aed3SPierre Pronchery             EVP_PKEY_free(pkey);
1083*b077aed3SPierre Pronchery             goto err;
1084*b077aed3SPierre Pronchery         }
1085*b077aed3SPierre Pronchery         EVP_PKEY_free(pkey);
1086*b077aed3SPierre Pronchery     }
1087*b077aed3SPierre Pronchery     cleanse(opt_srv_keypass);
1088*b077aed3SPierre Pronchery 
1089*b077aed3SPierre Pronchery     if (opt_srv_trusted != NULL) {
1090*b077aed3SPierre Pronchery         X509_STORE *ts =
1091*b077aed3SPierre Pronchery             load_trusted(opt_srv_trusted, 0, "certs trusted by mock server");
1092*b077aed3SPierre Pronchery 
1093*b077aed3SPierre Pronchery         if (ts == NULL || !OSSL_CMP_CTX_set0_trustedStore(ctx, ts)) {
1094*b077aed3SPierre Pronchery             X509_STORE_free(ts);
1095*b077aed3SPierre Pronchery             goto err;
1096*b077aed3SPierre Pronchery         }
1097*b077aed3SPierre Pronchery     } else {
1098*b077aed3SPierre Pronchery         CMP_warn("mock server will not be able to handle signature-protected requests since -srv_trusted is not given");
1099*b077aed3SPierre Pronchery     }
1100*b077aed3SPierre Pronchery     if (!setup_certs(opt_srv_untrusted,
1101*b077aed3SPierre Pronchery                      "untrusted certificates for mock server", ctx,
1102*b077aed3SPierre Pronchery                      (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
1103*b077aed3SPierre Pronchery         goto err;
1104*b077aed3SPierre Pronchery 
1105*b077aed3SPierre Pronchery     if (opt_rsp_cert == NULL) {
1106*b077aed3SPierre Pronchery         CMP_warn("no -rsp_cert given for mock server");
1107*b077aed3SPierre Pronchery     } else {
1108*b077aed3SPierre Pronchery         X509 *cert = load_cert_pwd(opt_rsp_cert, opt_keypass,
1109*b077aed3SPierre Pronchery                                    "cert to be returned by the mock server");
1110*b077aed3SPierre Pronchery 
1111*b077aed3SPierre Pronchery         if (cert == NULL)
1112*b077aed3SPierre Pronchery             goto err;
1113*b077aed3SPierre Pronchery         /* from server perspective the server is the client */
1114*b077aed3SPierre Pronchery         if (!ossl_cmp_mock_srv_set1_certOut(srv_ctx, cert)) {
1115*b077aed3SPierre Pronchery             X509_free(cert);
1116*b077aed3SPierre Pronchery             goto err;
1117*b077aed3SPierre Pronchery         }
1118*b077aed3SPierre Pronchery         X509_free(cert);
1119*b077aed3SPierre Pronchery     }
1120*b077aed3SPierre Pronchery     if (!setup_certs(opt_rsp_extracerts,
1121*b077aed3SPierre Pronchery                      "CMP extra certificates for mock server", srv_ctx,
1122*b077aed3SPierre Pronchery                      (add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_chainOut))
1123*b077aed3SPierre Pronchery         goto err;
1124*b077aed3SPierre Pronchery     if (!setup_certs(opt_rsp_capubs, "caPubs for mock server", srv_ctx,
1125*b077aed3SPierre Pronchery                      (add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_caPubsOut))
1126*b077aed3SPierre Pronchery         goto err;
1127*b077aed3SPierre Pronchery     (void)ossl_cmp_mock_srv_set_pollCount(srv_ctx, opt_poll_count);
1128*b077aed3SPierre Pronchery     (void)ossl_cmp_mock_srv_set_checkAfterTime(srv_ctx, opt_check_after);
1129*b077aed3SPierre Pronchery     if (opt_grant_implicitconf)
1130*b077aed3SPierre Pronchery         (void)OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(srv_ctx, 1);
1131*b077aed3SPierre Pronchery 
1132*b077aed3SPierre Pronchery     if (opt_failure != INT_MIN) { /* option has been set explicity */
1133*b077aed3SPierre Pronchery         if (opt_failure < 0 || OSSL_CMP_PKIFAILUREINFO_MAX < opt_failure) {
1134*b077aed3SPierre Pronchery             CMP_err1("-failure out of range, should be >= 0 and <= %d",
1135*b077aed3SPierre Pronchery                      OSSL_CMP_PKIFAILUREINFO_MAX);
1136*b077aed3SPierre Pronchery             goto err;
1137*b077aed3SPierre Pronchery         }
1138*b077aed3SPierre Pronchery         if (opt_failurebits != 0)
1139*b077aed3SPierre Pronchery             CMP_warn("-failurebits overrides -failure");
1140*b077aed3SPierre Pronchery         else
1141*b077aed3SPierre Pronchery             opt_failurebits = 1 << opt_failure;
1142*b077aed3SPierre Pronchery     }
1143*b077aed3SPierre Pronchery     if ((unsigned)opt_failurebits > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
1144*b077aed3SPierre Pronchery         CMP_err("-failurebits out of range");
1145*b077aed3SPierre Pronchery         goto err;
1146*b077aed3SPierre Pronchery     }
1147*b077aed3SPierre Pronchery     if (!ossl_cmp_mock_srv_set_statusInfo(srv_ctx, opt_pkistatus,
1148*b077aed3SPierre Pronchery                                           opt_failurebits, opt_statusstring))
1149*b077aed3SPierre Pronchery         goto err;
1150*b077aed3SPierre Pronchery 
1151*b077aed3SPierre Pronchery     if (opt_send_error)
1152*b077aed3SPierre Pronchery         (void)ossl_cmp_mock_srv_set_sendError(srv_ctx, 1);
1153*b077aed3SPierre Pronchery 
1154*b077aed3SPierre Pronchery     if (opt_send_unprotected)
1155*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1);
1156*b077aed3SPierre Pronchery     if (opt_send_unprot_err)
1157*b077aed3SPierre Pronchery         (void)OSSL_CMP_SRV_CTX_set_send_unprotected_errors(srv_ctx, 1);
1158*b077aed3SPierre Pronchery     if (opt_accept_unprotected)
1159*b077aed3SPierre Pronchery         (void)OSSL_CMP_SRV_CTX_set_accept_unprotected(srv_ctx, 1);
1160*b077aed3SPierre Pronchery     if (opt_accept_unprot_err)
1161*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
1162*b077aed3SPierre Pronchery     if (opt_accept_raverified)
1163*b077aed3SPierre Pronchery         (void)OSSL_CMP_SRV_CTX_set_accept_raverified(srv_ctx, 1);
1164*b077aed3SPierre Pronchery 
1165*b077aed3SPierre Pronchery     return srv_ctx;
1166*b077aed3SPierre Pronchery 
1167*b077aed3SPierre Pronchery  err:
1168*b077aed3SPierre Pronchery     ossl_cmp_mock_srv_free(srv_ctx);
1169*b077aed3SPierre Pronchery     return NULL;
1170*b077aed3SPierre Pronchery }
1171*b077aed3SPierre Pronchery 
1172*b077aed3SPierre Pronchery /*
1173*b077aed3SPierre Pronchery  * set up verification aspects of OSSL_CMP_CTX w.r.t. opts from config file/CLI.
1174*b077aed3SPierre Pronchery  * Returns pointer on success, NULL on error
1175*b077aed3SPierre Pronchery  */
1176*b077aed3SPierre Pronchery static int setup_verification_ctx(OSSL_CMP_CTX *ctx)
1177*b077aed3SPierre Pronchery {
1178*b077aed3SPierre Pronchery     if (!setup_certs(opt_untrusted, "untrusted certificates", ctx,
1179*b077aed3SPierre Pronchery                      (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
1180*b077aed3SPierre Pronchery         return 0;
1181*b077aed3SPierre Pronchery 
1182*b077aed3SPierre Pronchery     if (opt_srvcert != NULL || opt_trusted != NULL) {
1183*b077aed3SPierre Pronchery         X509 *srvcert;
1184*b077aed3SPierre Pronchery         X509_STORE *ts;
1185*b077aed3SPierre Pronchery         int ok;
1186*b077aed3SPierre Pronchery 
1187*b077aed3SPierre Pronchery         if (opt_srvcert != NULL) {
1188*b077aed3SPierre Pronchery             if (opt_trusted != NULL) {
1189*b077aed3SPierre Pronchery                 CMP_warn("-trusted option is ignored since -srvcert option is present");
1190*b077aed3SPierre Pronchery                 opt_trusted = NULL;
1191*b077aed3SPierre Pronchery             }
1192*b077aed3SPierre Pronchery             if (opt_recipient != NULL) {
1193*b077aed3SPierre Pronchery                 CMP_warn("-recipient option is ignored since -srvcert option is present");
1194*b077aed3SPierre Pronchery                 opt_recipient = NULL;
1195*b077aed3SPierre Pronchery             }
1196*b077aed3SPierre Pronchery             srvcert = load_cert_pwd(opt_srvcert, opt_otherpass,
1197*b077aed3SPierre Pronchery                                     "directly trusted CMP server certificate");
1198*b077aed3SPierre Pronchery             ok = srvcert != NULL && OSSL_CMP_CTX_set1_srvCert(ctx, srvcert);
1199*b077aed3SPierre Pronchery             X509_free(srvcert);
1200*b077aed3SPierre Pronchery             if (!ok)
1201*b077aed3SPierre Pronchery                 return 0;
1202*b077aed3SPierre Pronchery         }
1203*b077aed3SPierre Pronchery         if (opt_trusted != NULL) {
1204*b077aed3SPierre Pronchery             /*
1205*b077aed3SPierre Pronchery              * the 0 arg below clears any expected host/ip/email address;
1206*b077aed3SPierre Pronchery              * opt_expect_sender is used instead
1207*b077aed3SPierre Pronchery              */
1208*b077aed3SPierre Pronchery             ts = load_trusted(opt_trusted, 0, "certs trusted by client");
1209*b077aed3SPierre Pronchery 
1210*b077aed3SPierre Pronchery             if (ts == NULL || !OSSL_CMP_CTX_set0_trustedStore(ctx, ts)) {
1211*b077aed3SPierre Pronchery                 X509_STORE_free(ts);
1212*b077aed3SPierre Pronchery                 return 0;
1213*b077aed3SPierre Pronchery             }
1214*b077aed3SPierre Pronchery         }
1215*b077aed3SPierre Pronchery     }
1216*b077aed3SPierre Pronchery 
1217*b077aed3SPierre Pronchery     if (opt_ignore_keyusage)
1218*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_IGNORE_KEYUSAGE, 1);
1219*b077aed3SPierre Pronchery 
1220*b077aed3SPierre Pronchery     if (opt_unprotected_errors)
1221*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
1222*b077aed3SPierre Pronchery 
1223*b077aed3SPierre Pronchery     if (opt_out_trusted != NULL) { /* for use in OSSL_CMP_certConf_cb() */
1224*b077aed3SPierre Pronchery         X509_VERIFY_PARAM *out_vpm = NULL;
1225*b077aed3SPierre Pronchery         X509_STORE *out_trusted =
1226*b077aed3SPierre Pronchery             load_trusted(opt_out_trusted, 1,
1227*b077aed3SPierre Pronchery                          "trusted certs for verifying newly enrolled cert");
1228*b077aed3SPierre Pronchery 
1229*b077aed3SPierre Pronchery         if (out_trusted == NULL)
1230*b077aed3SPierre Pronchery             return 0;
1231*b077aed3SPierre Pronchery         /* ignore any -attime here, new certs are current anyway */
1232*b077aed3SPierre Pronchery         out_vpm = X509_STORE_get0_param(out_trusted);
1233*b077aed3SPierre Pronchery         X509_VERIFY_PARAM_clear_flags(out_vpm, X509_V_FLAG_USE_CHECK_TIME);
1234*b077aed3SPierre Pronchery 
1235*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_certConf_cb_arg(ctx, out_trusted);
1236*b077aed3SPierre Pronchery     }
1237*b077aed3SPierre Pronchery 
1238*b077aed3SPierre Pronchery     if (opt_disable_confirm)
1239*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_DISABLE_CONFIRM, 1);
1240*b077aed3SPierre Pronchery 
1241*b077aed3SPierre Pronchery     if (opt_implicit_confirm)
1242*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM, 1);
1243*b077aed3SPierre Pronchery 
1244*b077aed3SPierre Pronchery     return 1;
1245*b077aed3SPierre Pronchery }
1246*b077aed3SPierre Pronchery 
1247*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
1248*b077aed3SPierre Pronchery /*
1249*b077aed3SPierre Pronchery  * set up ssl_ctx for the OSSL_CMP_CTX based on options from config file/CLI.
1250*b077aed3SPierre Pronchery  * Returns pointer on success, NULL on error
1251*b077aed3SPierre Pronchery  */
1252*b077aed3SPierre Pronchery static SSL_CTX *setup_ssl_ctx(OSSL_CMP_CTX *ctx, const char *host,
1253*b077aed3SPierre Pronchery                               ENGINE *engine)
1254*b077aed3SPierre Pronchery {
1255*b077aed3SPierre Pronchery     STACK_OF(X509) *untrusted = OSSL_CMP_CTX_get0_untrusted(ctx);
1256*b077aed3SPierre Pronchery     EVP_PKEY *pkey = NULL;
1257*b077aed3SPierre Pronchery     X509_STORE *trust_store = NULL;
1258*b077aed3SPierre Pronchery     SSL_CTX *ssl_ctx;
1259*b077aed3SPierre Pronchery     int i;
1260*b077aed3SPierre Pronchery 
1261*b077aed3SPierre Pronchery     ssl_ctx = SSL_CTX_new(TLS_client_method());
1262*b077aed3SPierre Pronchery     if (ssl_ctx == NULL)
1263*b077aed3SPierre Pronchery         return NULL;
1264*b077aed3SPierre Pronchery 
1265*b077aed3SPierre Pronchery     if (opt_tls_trusted != NULL) {
1266*b077aed3SPierre Pronchery         trust_store = load_trusted(opt_tls_trusted, 0, "trusted TLS certs");
1267*b077aed3SPierre Pronchery         if (trust_store == NULL)
1268*b077aed3SPierre Pronchery             goto err;
1269*b077aed3SPierre Pronchery         SSL_CTX_set_cert_store(ssl_ctx, trust_store);
1270*b077aed3SPierre Pronchery     }
1271*b077aed3SPierre Pronchery 
1272*b077aed3SPierre Pronchery     if (opt_tls_cert != NULL && opt_tls_key != NULL) {
1273*b077aed3SPierre Pronchery         X509 *cert;
1274*b077aed3SPierre Pronchery         STACK_OF(X509) *certs = NULL;
1275*b077aed3SPierre Pronchery         int ok;
1276*b077aed3SPierre Pronchery 
1277*b077aed3SPierre Pronchery         if (!load_cert_certs(opt_tls_cert, &cert, &certs, 0, opt_tls_keypass,
1278*b077aed3SPierre Pronchery                              "TLS client certificate (optionally with chain)",
1279*b077aed3SPierre Pronchery                              vpm))
1280*b077aed3SPierre Pronchery             /* need opt_tls_keypass if opt_tls_cert is encrypted PKCS#12 file */
1281*b077aed3SPierre Pronchery             goto err;
1282*b077aed3SPierre Pronchery 
1283*b077aed3SPierre Pronchery         ok = SSL_CTX_use_certificate(ssl_ctx, cert) > 0;
1284*b077aed3SPierre Pronchery         X509_free(cert);
1285*b077aed3SPierre Pronchery 
1286*b077aed3SPierre Pronchery         /*
1287*b077aed3SPierre Pronchery          * Any further certs and any untrusted certs are used for constructing
1288*b077aed3SPierre Pronchery          * the chain to be provided with the TLS client cert to the TLS server.
1289*b077aed3SPierre Pronchery          */
1290*b077aed3SPierre Pronchery         if (!ok || !SSL_CTX_set0_chain(ssl_ctx, certs)) {
1291*b077aed3SPierre Pronchery             CMP_err1("unable to use client TLS certificate file '%s'",
1292*b077aed3SPierre Pronchery                      opt_tls_cert);
1293*b077aed3SPierre Pronchery             sk_X509_pop_free(certs, X509_free);
1294*b077aed3SPierre Pronchery             goto err;
1295*b077aed3SPierre Pronchery         }
1296*b077aed3SPierre Pronchery         for (i = 0; i < sk_X509_num(untrusted); i++) {
1297*b077aed3SPierre Pronchery             cert = sk_X509_value(untrusted, i);
1298*b077aed3SPierre Pronchery             if (!SSL_CTX_add1_chain_cert(ssl_ctx, cert)) {
1299*b077aed3SPierre Pronchery                 CMP_err("could not add untrusted cert to TLS client cert chain");
1300*b077aed3SPierre Pronchery                 goto err;
1301*b077aed3SPierre Pronchery             }
1302*b077aed3SPierre Pronchery         }
1303*b077aed3SPierre Pronchery 
1304*b077aed3SPierre Pronchery         {
1305*b077aed3SPierre Pronchery             X509_VERIFY_PARAM *tls_vpm = NULL;
1306*b077aed3SPierre Pronchery             unsigned long bak_flags = 0; /* compiler warns without init */
1307*b077aed3SPierre Pronchery 
1308*b077aed3SPierre Pronchery             if (trust_store != NULL) {
1309*b077aed3SPierre Pronchery                 tls_vpm = X509_STORE_get0_param(trust_store);
1310*b077aed3SPierre Pronchery                 bak_flags = X509_VERIFY_PARAM_get_flags(tls_vpm);
1311*b077aed3SPierre Pronchery                 /* disable any cert status/revocation checking etc. */
1312*b077aed3SPierre Pronchery                 X509_VERIFY_PARAM_clear_flags(tls_vpm,
1313*b077aed3SPierre Pronchery                                               ~(X509_V_FLAG_USE_CHECK_TIME
1314*b077aed3SPierre Pronchery                                                 | X509_V_FLAG_NO_CHECK_TIME
1315*b077aed3SPierre Pronchery                                                 | X509_V_FLAG_PARTIAL_CHAIN
1316*b077aed3SPierre Pronchery                                                 | X509_V_FLAG_POLICY_CHECK));
1317*b077aed3SPierre Pronchery             }
1318*b077aed3SPierre Pronchery             CMP_debug("trying to build cert chain for own TLS cert");
1319*b077aed3SPierre Pronchery             if (SSL_CTX_build_cert_chain(ssl_ctx,
1320*b077aed3SPierre Pronchery                                          SSL_BUILD_CHAIN_FLAG_UNTRUSTED |
1321*b077aed3SPierre Pronchery                                          SSL_BUILD_CHAIN_FLAG_NO_ROOT)) {
1322*b077aed3SPierre Pronchery                 CMP_debug("success building cert chain for own TLS cert");
1323*b077aed3SPierre Pronchery             } else {
1324*b077aed3SPierre Pronchery                 OSSL_CMP_CTX_print_errors(ctx);
1325*b077aed3SPierre Pronchery                 CMP_warn("could not build cert chain for own TLS cert");
1326*b077aed3SPierre Pronchery             }
1327*b077aed3SPierre Pronchery             if (trust_store != NULL)
1328*b077aed3SPierre Pronchery                 X509_VERIFY_PARAM_set_flags(tls_vpm, bak_flags);
1329*b077aed3SPierre Pronchery         }
1330*b077aed3SPierre Pronchery 
1331*b077aed3SPierre Pronchery         /* If present we append to the list also the certs from opt_tls_extra */
1332*b077aed3SPierre Pronchery         if (opt_tls_extra != NULL) {
1333*b077aed3SPierre Pronchery             STACK_OF(X509) *tls_extra = load_certs_multifile(opt_tls_extra,
1334*b077aed3SPierre Pronchery                                                              opt_otherpass,
1335*b077aed3SPierre Pronchery                                                              "extra certificates for TLS",
1336*b077aed3SPierre Pronchery                                                              vpm);
1337*b077aed3SPierre Pronchery             int res = 1;
1338*b077aed3SPierre Pronchery 
1339*b077aed3SPierre Pronchery             if (tls_extra == NULL)
1340*b077aed3SPierre Pronchery                 goto err;
1341*b077aed3SPierre Pronchery             for (i = 0; i < sk_X509_num(tls_extra); i++) {
1342*b077aed3SPierre Pronchery                 cert = sk_X509_value(tls_extra, i);
1343*b077aed3SPierre Pronchery                 if (res != 0)
1344*b077aed3SPierre Pronchery                     res = SSL_CTX_add_extra_chain_cert(ssl_ctx, cert);
1345*b077aed3SPierre Pronchery                 if (res == 0)
1346*b077aed3SPierre Pronchery                     X509_free(cert);
1347*b077aed3SPierre Pronchery             }
1348*b077aed3SPierre Pronchery             sk_X509_free(tls_extra);
1349*b077aed3SPierre Pronchery             if (res == 0) {
1350*b077aed3SPierre Pronchery                 BIO_printf(bio_err, "error: unable to add TLS extra certs\n");
1351*b077aed3SPierre Pronchery                 goto err;
1352*b077aed3SPierre Pronchery             }
1353*b077aed3SPierre Pronchery         }
1354*b077aed3SPierre Pronchery 
1355*b077aed3SPierre Pronchery         pkey = load_key_pwd(opt_tls_key, opt_keyform, opt_tls_keypass,
1356*b077aed3SPierre Pronchery                             engine, "TLS client private key");
1357*b077aed3SPierre Pronchery         cleanse(opt_tls_keypass);
1358*b077aed3SPierre Pronchery         if (pkey == NULL)
1359*b077aed3SPierre Pronchery             goto err;
1360*b077aed3SPierre Pronchery         /*
1361*b077aed3SPierre Pronchery          * verify the key matches the cert,
1362*b077aed3SPierre Pronchery          * not using SSL_CTX_check_private_key(ssl_ctx)
1363*b077aed3SPierre Pronchery          * because it gives poor and sometimes misleading diagnostics
1364*b077aed3SPierre Pronchery          */
1365*b077aed3SPierre Pronchery         if (!X509_check_private_key(SSL_CTX_get0_certificate(ssl_ctx),
1366*b077aed3SPierre Pronchery                                     pkey)) {
1367*b077aed3SPierre Pronchery             CMP_err2("TLS private key '%s' does not match the TLS certificate '%s'\n",
1368*b077aed3SPierre Pronchery                      opt_tls_key, opt_tls_cert);
1369*b077aed3SPierre Pronchery             EVP_PKEY_free(pkey);
1370*b077aed3SPierre Pronchery             pkey = NULL; /* otherwise, for some reason double free! */
1371*b077aed3SPierre Pronchery             goto err;
1372*b077aed3SPierre Pronchery         }
1373*b077aed3SPierre Pronchery         if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) <= 0) {
1374*b077aed3SPierre Pronchery             CMP_err1("unable to use TLS client private key '%s'", opt_tls_key);
1375*b077aed3SPierre Pronchery             EVP_PKEY_free(pkey);
1376*b077aed3SPierre Pronchery             pkey = NULL; /* otherwise, for some reason double free! */
1377*b077aed3SPierre Pronchery             goto err;
1378*b077aed3SPierre Pronchery         }
1379*b077aed3SPierre Pronchery         EVP_PKEY_free(pkey); /* we do not need the handle any more */
1380*b077aed3SPierre Pronchery     }
1381*b077aed3SPierre Pronchery     if (opt_tls_trusted != NULL) {
1382*b077aed3SPierre Pronchery         /* enable and parameterize server hostname/IP address check */
1383*b077aed3SPierre Pronchery         if (!truststore_set_host_etc(trust_store,
1384*b077aed3SPierre Pronchery                                      opt_tls_host != NULL ? opt_tls_host : host))
1385*b077aed3SPierre Pronchery             goto err;
1386*b077aed3SPierre Pronchery         SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
1387*b077aed3SPierre Pronchery     }
1388*b077aed3SPierre Pronchery     return ssl_ctx;
1389*b077aed3SPierre Pronchery  err:
1390*b077aed3SPierre Pronchery     SSL_CTX_free(ssl_ctx);
1391*b077aed3SPierre Pronchery     return NULL;
1392*b077aed3SPierre Pronchery }
1393*b077aed3SPierre Pronchery #endif /* OPENSSL_NO_SOCK */
1394*b077aed3SPierre Pronchery 
1395*b077aed3SPierre Pronchery /*
1396*b077aed3SPierre Pronchery  * set up protection aspects of OSSL_CMP_CTX based on options from config
1397*b077aed3SPierre Pronchery  * file/CLI while parsing options and checking their consistency.
1398*b077aed3SPierre Pronchery  * Returns 1 on success, 0 on error
1399*b077aed3SPierre Pronchery  */
1400*b077aed3SPierre Pronchery static int setup_protection_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
1401*b077aed3SPierre Pronchery {
1402*b077aed3SPierre Pronchery     if (!opt_unprotected_requests && opt_secret == NULL && opt_key == NULL) {
1403*b077aed3SPierre Pronchery         CMP_err("must give -key or -secret unless -unprotected_requests is used");
1404*b077aed3SPierre Pronchery         return 0;
1405*b077aed3SPierre Pronchery     }
1406*b077aed3SPierre Pronchery 
1407*b077aed3SPierre Pronchery     if (opt_ref == NULL && opt_cert == NULL && opt_subject == NULL) {
1408*b077aed3SPierre Pronchery         /* cert or subject should determine the sender */
1409*b077aed3SPierre Pronchery         CMP_err("must give -ref if no -cert and no -subject given");
1410*b077aed3SPierre Pronchery         return 0;
1411*b077aed3SPierre Pronchery     }
1412*b077aed3SPierre Pronchery     if (!opt_secret && ((opt_cert == NULL) != (opt_key == NULL))) {
1413*b077aed3SPierre Pronchery         CMP_err("must give both -cert and -key options or neither");
1414*b077aed3SPierre Pronchery         return 0;
1415*b077aed3SPierre Pronchery     }
1416*b077aed3SPierre Pronchery     if (opt_secret != NULL) {
1417*b077aed3SPierre Pronchery         char *pass_string = get_passwd(opt_secret, "PBMAC");
1418*b077aed3SPierre Pronchery         int res;
1419*b077aed3SPierre Pronchery 
1420*b077aed3SPierre Pronchery         if (pass_string != NULL) {
1421*b077aed3SPierre Pronchery             cleanse(opt_secret);
1422*b077aed3SPierre Pronchery             res = OSSL_CMP_CTX_set1_secretValue(ctx,
1423*b077aed3SPierre Pronchery                                                 (unsigned char *)pass_string,
1424*b077aed3SPierre Pronchery                                                 strlen(pass_string));
1425*b077aed3SPierre Pronchery             clear_free(pass_string);
1426*b077aed3SPierre Pronchery             if (res == 0)
1427*b077aed3SPierre Pronchery                 return 0;
1428*b077aed3SPierre Pronchery         }
1429*b077aed3SPierre Pronchery         if (opt_cert != NULL || opt_key != NULL)
1430*b077aed3SPierre Pronchery             CMP_warn("-cert and -key not used for protection since -secret is given");
1431*b077aed3SPierre Pronchery     }
1432*b077aed3SPierre Pronchery     if (opt_ref != NULL
1433*b077aed3SPierre Pronchery             && !OSSL_CMP_CTX_set1_referenceValue(ctx, (unsigned char *)opt_ref,
1434*b077aed3SPierre Pronchery                                                  strlen(opt_ref)))
1435*b077aed3SPierre Pronchery         return 0;
1436*b077aed3SPierre Pronchery 
1437*b077aed3SPierre Pronchery     if (opt_key != NULL) {
1438*b077aed3SPierre Pronchery         EVP_PKEY *pkey = load_key_pwd(opt_key, opt_keyform, opt_keypass, engine,
1439*b077aed3SPierre Pronchery                                       "private key for CMP client certificate");
1440*b077aed3SPierre Pronchery 
1441*b077aed3SPierre Pronchery         if (pkey == NULL || !OSSL_CMP_CTX_set1_pkey(ctx, pkey)) {
1442*b077aed3SPierre Pronchery             EVP_PKEY_free(pkey);
1443*b077aed3SPierre Pronchery             return 0;
1444*b077aed3SPierre Pronchery         }
1445*b077aed3SPierre Pronchery         EVP_PKEY_free(pkey);
1446*b077aed3SPierre Pronchery     }
1447*b077aed3SPierre Pronchery     if (opt_secret == NULL && opt_srvcert == NULL && opt_trusted == NULL)
1448*b077aed3SPierre Pronchery         CMP_warn("will not authenticate server due to missing -secret, -trusted, or -srvcert");
1449*b077aed3SPierre Pronchery 
1450*b077aed3SPierre Pronchery     if (opt_cert != NULL) {
1451*b077aed3SPierre Pronchery         X509 *cert;
1452*b077aed3SPierre Pronchery         STACK_OF(X509) *certs = NULL;
1453*b077aed3SPierre Pronchery         X509_STORE *own_trusted = NULL;
1454*b077aed3SPierre Pronchery         int ok;
1455*b077aed3SPierre Pronchery 
1456*b077aed3SPierre Pronchery         if (!load_cert_certs(opt_cert, &cert, &certs, 0, opt_keypass,
1457*b077aed3SPierre Pronchery                              "CMP client certificate (optionally with chain)",
1458*b077aed3SPierre Pronchery                              vpm))
1459*b077aed3SPierre Pronchery             /* opt_keypass is needed if opt_cert is an encrypted PKCS#12 file */
1460*b077aed3SPierre Pronchery             return 0;
1461*b077aed3SPierre Pronchery         ok = OSSL_CMP_CTX_set1_cert(ctx, cert);
1462*b077aed3SPierre Pronchery         X509_free(cert);
1463*b077aed3SPierre Pronchery         if (!ok) {
1464*b077aed3SPierre Pronchery             CMP_err("out of memory");
1465*b077aed3SPierre Pronchery         } else {
1466*b077aed3SPierre Pronchery             if (opt_own_trusted != NULL) {
1467*b077aed3SPierre Pronchery                 own_trusted = load_trusted(opt_own_trusted, 0,
1468*b077aed3SPierre Pronchery                                            "trusted certs for verifying own CMP signer cert");
1469*b077aed3SPierre Pronchery                 ok = own_trusted != NULL;
1470*b077aed3SPierre Pronchery             }
1471*b077aed3SPierre Pronchery             ok = ok && OSSL_CMP_CTX_build_cert_chain(ctx, own_trusted, certs);
1472*b077aed3SPierre Pronchery         }
1473*b077aed3SPierre Pronchery         X509_STORE_free(own_trusted);
1474*b077aed3SPierre Pronchery         sk_X509_pop_free(certs, X509_free);
1475*b077aed3SPierre Pronchery         if (!ok)
1476*b077aed3SPierre Pronchery             return 0;
1477*b077aed3SPierre Pronchery     } else if (opt_own_trusted != NULL) {
1478*b077aed3SPierre Pronchery         CMP_warn("-own_trusted option is ignored without -cert");
1479*b077aed3SPierre Pronchery     }
1480*b077aed3SPierre Pronchery 
1481*b077aed3SPierre Pronchery     if (!setup_certs(opt_extracerts, "extra certificates for CMP", ctx,
1482*b077aed3SPierre Pronchery                      (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_extraCertsOut))
1483*b077aed3SPierre Pronchery         return 0;
1484*b077aed3SPierre Pronchery     cleanse(opt_otherpass);
1485*b077aed3SPierre Pronchery 
1486*b077aed3SPierre Pronchery     if (opt_unprotected_requests)
1487*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1);
1488*b077aed3SPierre Pronchery 
1489*b077aed3SPierre Pronchery     if (opt_digest != NULL) {
1490*b077aed3SPierre Pronchery         int digest = OBJ_ln2nid(opt_digest);
1491*b077aed3SPierre Pronchery 
1492*b077aed3SPierre Pronchery         if (digest == NID_undef) {
1493*b077aed3SPierre Pronchery             CMP_err1("digest algorithm name not recognized: '%s'", opt_digest);
1494*b077aed3SPierre Pronchery             return 0;
1495*b077aed3SPierre Pronchery         }
1496*b077aed3SPierre Pronchery         if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_DIGEST_ALGNID, digest)
1497*b077aed3SPierre Pronchery             || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_OWF_ALGNID, digest)) {
1498*b077aed3SPierre Pronchery             CMP_err1("digest algorithm name not supported: '%s'", opt_digest);
1499*b077aed3SPierre Pronchery             return 0;
1500*b077aed3SPierre Pronchery         }
1501*b077aed3SPierre Pronchery     }
1502*b077aed3SPierre Pronchery 
1503*b077aed3SPierre Pronchery     if (opt_mac != NULL) {
1504*b077aed3SPierre Pronchery         int mac = OBJ_ln2nid(opt_mac);
1505*b077aed3SPierre Pronchery         if (mac == NID_undef) {
1506*b077aed3SPierre Pronchery             CMP_err1("MAC algorithm name not recognized: '%s'", opt_mac);
1507*b077aed3SPierre Pronchery             return 0;
1508*b077aed3SPierre Pronchery         }
1509*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_MAC_ALGNID, mac);
1510*b077aed3SPierre Pronchery     }
1511*b077aed3SPierre Pronchery     return 1;
1512*b077aed3SPierre Pronchery }
1513*b077aed3SPierre Pronchery 
1514*b077aed3SPierre Pronchery /*
1515*b077aed3SPierre Pronchery  * set up IR/CR/KUR/CertConf/RR specific parts of the OSSL_CMP_CTX
1516*b077aed3SPierre Pronchery  * based on options from config file/CLI.
1517*b077aed3SPierre Pronchery  * Returns pointer on success, NULL on error
1518*b077aed3SPierre Pronchery  */
1519*b077aed3SPierre Pronchery static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
1520*b077aed3SPierre Pronchery {
1521*b077aed3SPierre Pronchery     X509_REQ *csr = NULL;
1522*b077aed3SPierre Pronchery     X509_EXTENSIONS *exts = NULL;
1523*b077aed3SPierre Pronchery     X509V3_CTX ext_ctx;
1524*b077aed3SPierre Pronchery 
1525*b077aed3SPierre Pronchery     if (opt_subject == NULL
1526*b077aed3SPierre Pronchery             && opt_csr == NULL && opt_oldcert == NULL && opt_cert == NULL
1527*b077aed3SPierre Pronchery             && opt_cmd != CMP_RR && opt_cmd != CMP_GENM)
1528*b077aed3SPierre Pronchery         CMP_warn("no -subject given; no -csr or -oldcert or -cert available for fallback");
1529*b077aed3SPierre Pronchery 
1530*b077aed3SPierre Pronchery     if (opt_cmd == CMP_IR || opt_cmd == CMP_CR || opt_cmd == CMP_KUR) {
1531*b077aed3SPierre Pronchery         if (opt_newkey == NULL
1532*b077aed3SPierre Pronchery             && opt_key == NULL && opt_csr == NULL && opt_oldcert == NULL) {
1533*b077aed3SPierre Pronchery             CMP_err("missing -newkey (or -key) to be certified and no -csr, -oldcert, or -cert given for fallback public key");
1534*b077aed3SPierre Pronchery             return 0;
1535*b077aed3SPierre Pronchery         }
1536*b077aed3SPierre Pronchery         if (opt_newkey == NULL
1537*b077aed3SPierre Pronchery             && opt_popo != OSSL_CRMF_POPO_NONE
1538*b077aed3SPierre Pronchery             && opt_popo != OSSL_CRMF_POPO_RAVERIFIED) {
1539*b077aed3SPierre Pronchery             if (opt_csr != NULL) {
1540*b077aed3SPierre Pronchery                 CMP_err1("no -newkey option given with private key for POPO, -csr option only provides public key%s",
1541*b077aed3SPierre Pronchery                         opt_key == NULL ? "" :
1542*b077aed3SPierre Pronchery                         ", and -key option superseded by by -csr");
1543*b077aed3SPierre Pronchery                 return 0;
1544*b077aed3SPierre Pronchery             }
1545*b077aed3SPierre Pronchery             if (opt_key == NULL) {
1546*b077aed3SPierre Pronchery                 CMP_err("missing -newkey (or -key) option for POPO");
1547*b077aed3SPierre Pronchery                 return 0;
1548*b077aed3SPierre Pronchery             }
1549*b077aed3SPierre Pronchery         }
1550*b077aed3SPierre Pronchery         if (opt_certout == NULL) {
1551*b077aed3SPierre Pronchery             CMP_err("-certout not given, nowhere to save newly enrolled certificate");
1552*b077aed3SPierre Pronchery             return 0;
1553*b077aed3SPierre Pronchery         }
1554*b077aed3SPierre Pronchery         if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject")
1555*b077aed3SPierre Pronchery                 || !set_name(opt_issuer, OSSL_CMP_CTX_set1_issuer, ctx, "issuer"))
1556*b077aed3SPierre Pronchery             return 0;
1557*b077aed3SPierre Pronchery     } else {
1558*b077aed3SPierre Pronchery         const char *msg = "option is ignored for commands other than 'ir', 'cr', and 'kur'";
1559*b077aed3SPierre Pronchery 
1560*b077aed3SPierre Pronchery         if (opt_subject != NULL) {
1561*b077aed3SPierre Pronchery             if (opt_ref == NULL && opt_cert == NULL) {
1562*b077aed3SPierre Pronchery                 /* use subject as default sender unless oldcert subject is used */
1563*b077aed3SPierre Pronchery                 if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject"))
1564*b077aed3SPierre Pronchery                     return 0;
1565*b077aed3SPierre Pronchery             } else {
1566*b077aed3SPierre Pronchery                 CMP_warn1("-subject %s since -ref or -cert is given", msg);
1567*b077aed3SPierre Pronchery             }
1568*b077aed3SPierre Pronchery         }
1569*b077aed3SPierre Pronchery         if (opt_issuer != NULL)
1570*b077aed3SPierre Pronchery             CMP_warn1("-issuer %s", msg);
1571*b077aed3SPierre Pronchery         if (opt_reqexts != NULL)
1572*b077aed3SPierre Pronchery             CMP_warn1("-reqexts %s", msg);
1573*b077aed3SPierre Pronchery         if (opt_san_nodefault)
1574*b077aed3SPierre Pronchery             CMP_warn1("-san_nodefault %s", msg);
1575*b077aed3SPierre Pronchery         if (opt_sans != NULL)
1576*b077aed3SPierre Pronchery             CMP_warn1("-sans %s", msg);
1577*b077aed3SPierre Pronchery         if (opt_policies != NULL)
1578*b077aed3SPierre Pronchery             CMP_warn1("-policies %s", msg);
1579*b077aed3SPierre Pronchery         if (opt_policy_oids != NULL)
1580*b077aed3SPierre Pronchery             CMP_warn1("-policy_oids %s", msg);
1581*b077aed3SPierre Pronchery     }
1582*b077aed3SPierre Pronchery     if (opt_cmd == CMP_KUR) {
1583*b077aed3SPierre Pronchery         char *ref_cert = opt_oldcert != NULL ? opt_oldcert : opt_cert;
1584*b077aed3SPierre Pronchery 
1585*b077aed3SPierre Pronchery         if (ref_cert == NULL && opt_csr == NULL) {
1586*b077aed3SPierre Pronchery             CMP_err("missing -oldcert for certificate to be updated and no -csr given");
1587*b077aed3SPierre Pronchery             return 0;
1588*b077aed3SPierre Pronchery         }
1589*b077aed3SPierre Pronchery         if (opt_subject != NULL)
1590*b077aed3SPierre Pronchery             CMP_warn2("given -subject '%s' overrides the subject of '%s' for KUR",
1591*b077aed3SPierre Pronchery                       opt_subject, ref_cert != NULL ? ref_cert : opt_csr);
1592*b077aed3SPierre Pronchery     }
1593*b077aed3SPierre Pronchery     if (opt_cmd == CMP_RR) {
1594*b077aed3SPierre Pronchery         if (opt_oldcert == NULL && opt_csr == NULL) {
1595*b077aed3SPierre Pronchery             CMP_err("missing -oldcert for certificate to be revoked and no -csr given");
1596*b077aed3SPierre Pronchery             return 0;
1597*b077aed3SPierre Pronchery         }
1598*b077aed3SPierre Pronchery         if (opt_oldcert != NULL && opt_csr != NULL)
1599*b077aed3SPierre Pronchery             CMP_warn("ignoring -csr since certificate to be revoked is given");
1600*b077aed3SPierre Pronchery     }
1601*b077aed3SPierre Pronchery     if (opt_cmd == CMP_P10CR && opt_csr == NULL) {
1602*b077aed3SPierre Pronchery         CMP_err("missing PKCS#10 CSR for p10cr");
1603*b077aed3SPierre Pronchery         return 0;
1604*b077aed3SPierre Pronchery     }
1605*b077aed3SPierre Pronchery 
1606*b077aed3SPierre Pronchery     if (opt_recipient == NULL && opt_srvcert == NULL && opt_issuer == NULL
1607*b077aed3SPierre Pronchery             && opt_oldcert == NULL && opt_cert == NULL)
1608*b077aed3SPierre Pronchery         CMP_warn("missing -recipient, -srvcert, -issuer, -oldcert or -cert; recipient will be set to \"NULL-DN\"");
1609*b077aed3SPierre Pronchery 
1610*b077aed3SPierre Pronchery     if (opt_cmd == CMP_P10CR || opt_cmd == CMP_RR) {
1611*b077aed3SPierre Pronchery         const char *msg = "option is ignored for 'p10cr' and 'rr' commands";
1612*b077aed3SPierre Pronchery 
1613*b077aed3SPierre Pronchery         if (opt_newkeypass != NULL)
1614*b077aed3SPierre Pronchery             CMP_warn1("-newkeytype %s", msg);
1615*b077aed3SPierre Pronchery         if (opt_newkey != NULL)
1616*b077aed3SPierre Pronchery             CMP_warn1("-newkey %s", msg);
1617*b077aed3SPierre Pronchery         if (opt_days != 0)
1618*b077aed3SPierre Pronchery             CMP_warn1("-days %s", msg);
1619*b077aed3SPierre Pronchery         if (opt_popo != OSSL_CRMF_POPO_NONE - 1)
1620*b077aed3SPierre Pronchery             CMP_warn1("-popo %s", msg);
1621*b077aed3SPierre Pronchery     } else if (opt_newkey != NULL) {
1622*b077aed3SPierre Pronchery         const char *file = opt_newkey;
1623*b077aed3SPierre Pronchery         const int format = opt_keyform;
1624*b077aed3SPierre Pronchery         const char *pass = opt_newkeypass;
1625*b077aed3SPierre Pronchery         const char *desc = "new private key for cert to be enrolled";
1626*b077aed3SPierre Pronchery         EVP_PKEY *pkey;
1627*b077aed3SPierre Pronchery         int priv = 1;
1628*b077aed3SPierre Pronchery         BIO *bio_bak = bio_err;
1629*b077aed3SPierre Pronchery 
1630*b077aed3SPierre Pronchery         bio_err = NULL; /* suppress diagnostics on first try loading key */
1631*b077aed3SPierre Pronchery         pkey = load_key_pwd(file, format, pass, engine, desc);
1632*b077aed3SPierre Pronchery         bio_err = bio_bak;
1633*b077aed3SPierre Pronchery         if (pkey == NULL) {
1634*b077aed3SPierre Pronchery             ERR_clear_error();
1635*b077aed3SPierre Pronchery             desc = opt_csr == NULL
1636*b077aed3SPierre Pronchery             ? "fallback public key for cert to be enrolled"
1637*b077aed3SPierre Pronchery             : "public key for checking cert resulting from p10cr";
1638*b077aed3SPierre Pronchery             pkey = load_pubkey(file, format, 0, pass, engine, desc);
1639*b077aed3SPierre Pronchery             priv = 0;
1640*b077aed3SPierre Pronchery         }
1641*b077aed3SPierre Pronchery         cleanse(opt_newkeypass);
1642*b077aed3SPierre Pronchery         if (pkey == NULL || !OSSL_CMP_CTX_set0_newPkey(ctx, priv, pkey)) {
1643*b077aed3SPierre Pronchery             EVP_PKEY_free(pkey);
1644*b077aed3SPierre Pronchery             return 0;
1645*b077aed3SPierre Pronchery         }
1646*b077aed3SPierre Pronchery     }
1647*b077aed3SPierre Pronchery 
1648*b077aed3SPierre Pronchery     if (opt_days > 0
1649*b077aed3SPierre Pronchery             && !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_VALIDITY_DAYS,
1650*b077aed3SPierre Pronchery                                         opt_days)) {
1651*b077aed3SPierre Pronchery         CMP_err("could not set requested cert validity period");
1652*b077aed3SPierre Pronchery         return 0;
1653*b077aed3SPierre Pronchery     }
1654*b077aed3SPierre Pronchery 
1655*b077aed3SPierre Pronchery     if (opt_policies != NULL && opt_policy_oids != NULL) {
1656*b077aed3SPierre Pronchery         CMP_err("cannot have policies both via -policies and via -policy_oids");
1657*b077aed3SPierre Pronchery         return 0;
1658*b077aed3SPierre Pronchery     }
1659*b077aed3SPierre Pronchery 
1660*b077aed3SPierre Pronchery     if (opt_csr != NULL) {
1661*b077aed3SPierre Pronchery         if (opt_cmd == CMP_GENM) {
1662*b077aed3SPierre Pronchery             CMP_warn("-csr option is ignored for command 'genm'");
1663*b077aed3SPierre Pronchery         } else {
1664*b077aed3SPierre Pronchery             if ((csr = load_csr_autofmt(opt_csr, "PKCS#10 CSR")) == NULL)
1665*b077aed3SPierre Pronchery                 return 0;
1666*b077aed3SPierre Pronchery             if (!OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
1667*b077aed3SPierre Pronchery                 goto oom;
1668*b077aed3SPierre Pronchery         }
1669*b077aed3SPierre Pronchery     }
1670*b077aed3SPierre Pronchery     if (opt_reqexts != NULL || opt_policies != NULL) {
1671*b077aed3SPierre Pronchery         if ((exts = sk_X509_EXTENSION_new_null()) == NULL)
1672*b077aed3SPierre Pronchery             goto oom;
1673*b077aed3SPierre Pronchery         X509V3_set_ctx(&ext_ctx, NULL, NULL, csr, NULL, X509V3_CTX_REPLACE);
1674*b077aed3SPierre Pronchery         X509V3_set_nconf(&ext_ctx, conf);
1675*b077aed3SPierre Pronchery         if (opt_reqexts != NULL
1676*b077aed3SPierre Pronchery             && !X509V3_EXT_add_nconf_sk(conf, &ext_ctx, opt_reqexts, &exts)) {
1677*b077aed3SPierre Pronchery             CMP_err1("cannot load certificate request extension section '%s'",
1678*b077aed3SPierre Pronchery                      opt_reqexts);
1679*b077aed3SPierre Pronchery             goto exts_err;
1680*b077aed3SPierre Pronchery         }
1681*b077aed3SPierre Pronchery         if (opt_policies != NULL
1682*b077aed3SPierre Pronchery             && !X509V3_EXT_add_nconf_sk(conf, &ext_ctx, opt_policies, &exts)) {
1683*b077aed3SPierre Pronchery             CMP_err1("cannot load policy cert request extension section '%s'",
1684*b077aed3SPierre Pronchery                      opt_policies);
1685*b077aed3SPierre Pronchery             goto exts_err;
1686*b077aed3SPierre Pronchery         }
1687*b077aed3SPierre Pronchery         OSSL_CMP_CTX_set0_reqExtensions(ctx, exts);
1688*b077aed3SPierre Pronchery     }
1689*b077aed3SPierre Pronchery     X509_REQ_free(csr);
1690*b077aed3SPierre Pronchery     /* After here, must not goto oom/exts_err */
1691*b077aed3SPierre Pronchery 
1692*b077aed3SPierre Pronchery     if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) && opt_sans != NULL) {
1693*b077aed3SPierre Pronchery         CMP_err("cannot have Subject Alternative Names both via -reqexts and via -sans");
1694*b077aed3SPierre Pronchery         return 0;
1695*b077aed3SPierre Pronchery     }
1696*b077aed3SPierre Pronchery     if (!set_gennames(ctx, opt_sans, "Subject Alternative Name"))
1697*b077aed3SPierre Pronchery         return 0;
1698*b077aed3SPierre Pronchery 
1699*b077aed3SPierre Pronchery     if (opt_san_nodefault) {
1700*b077aed3SPierre Pronchery         if (opt_sans != NULL)
1701*b077aed3SPierre Pronchery             CMP_warn("-opt_san_nodefault has no effect when -sans is used");
1702*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx,
1703*b077aed3SPierre Pronchery                                       OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT, 1);
1704*b077aed3SPierre Pronchery     }
1705*b077aed3SPierre Pronchery 
1706*b077aed3SPierre Pronchery     if (opt_policy_oids_critical) {
1707*b077aed3SPierre Pronchery         if (opt_policy_oids == NULL)
1708*b077aed3SPierre Pronchery             CMP_warn("-opt_policy_oids_critical has no effect unless -policy_oids is given");
1709*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POLICIES_CRITICAL, 1);
1710*b077aed3SPierre Pronchery     }
1711*b077aed3SPierre Pronchery 
1712*b077aed3SPierre Pronchery     while (opt_policy_oids != NULL) {
1713*b077aed3SPierre Pronchery         ASN1_OBJECT *policy;
1714*b077aed3SPierre Pronchery         POLICYINFO *pinfo;
1715*b077aed3SPierre Pronchery         char *next = next_item(opt_policy_oids);
1716*b077aed3SPierre Pronchery 
1717*b077aed3SPierre Pronchery         if ((policy = OBJ_txt2obj(opt_policy_oids, 1)) == 0) {
1718*b077aed3SPierre Pronchery             CMP_err1("unknown policy OID '%s'", opt_policy_oids);
1719*b077aed3SPierre Pronchery             return 0;
1720*b077aed3SPierre Pronchery         }
1721*b077aed3SPierre Pronchery 
1722*b077aed3SPierre Pronchery         if ((pinfo = POLICYINFO_new()) == NULL) {
1723*b077aed3SPierre Pronchery             ASN1_OBJECT_free(policy);
1724*b077aed3SPierre Pronchery             return 0;
1725*b077aed3SPierre Pronchery         }
1726*b077aed3SPierre Pronchery         pinfo->policyid = policy;
1727*b077aed3SPierre Pronchery 
1728*b077aed3SPierre Pronchery         if (!OSSL_CMP_CTX_push0_policy(ctx, pinfo)) {
1729*b077aed3SPierre Pronchery             CMP_err1("cannot add policy with OID '%s'", opt_policy_oids);
1730*b077aed3SPierre Pronchery             POLICYINFO_free(pinfo);
1731*b077aed3SPierre Pronchery             return 0;
1732*b077aed3SPierre Pronchery         }
1733*b077aed3SPierre Pronchery         opt_policy_oids = next;
1734*b077aed3SPierre Pronchery     }
1735*b077aed3SPierre Pronchery 
1736*b077aed3SPierre Pronchery     if (opt_popo >= OSSL_CRMF_POPO_NONE)
1737*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POPO_METHOD, opt_popo);
1738*b077aed3SPierre Pronchery 
1739*b077aed3SPierre Pronchery     if (opt_oldcert != NULL) {
1740*b077aed3SPierre Pronchery         if (opt_cmd == CMP_GENM) {
1741*b077aed3SPierre Pronchery             CMP_warn("-oldcert option is ignored for command 'genm'");
1742*b077aed3SPierre Pronchery         } else {
1743*b077aed3SPierre Pronchery             X509 *oldcert = load_cert_pwd(opt_oldcert, opt_keypass,
1744*b077aed3SPierre Pronchery                                           opt_cmd == CMP_KUR ?
1745*b077aed3SPierre Pronchery                                           "certificate to be updated" :
1746*b077aed3SPierre Pronchery                                           opt_cmd == CMP_RR ?
1747*b077aed3SPierre Pronchery                                           "certificate to be revoked" :
1748*b077aed3SPierre Pronchery                                           "reference certificate (oldcert)");
1749*b077aed3SPierre Pronchery             /* opt_keypass needed if opt_oldcert is an encrypted PKCS#12 file */
1750*b077aed3SPierre Pronchery 
1751*b077aed3SPierre Pronchery             if (oldcert == NULL)
1752*b077aed3SPierre Pronchery                 return 0;
1753*b077aed3SPierre Pronchery             if (!OSSL_CMP_CTX_set1_oldCert(ctx, oldcert)) {
1754*b077aed3SPierre Pronchery                 X509_free(oldcert);
1755*b077aed3SPierre Pronchery                 CMP_err("out of memory");
1756*b077aed3SPierre Pronchery                 return 0;
1757*b077aed3SPierre Pronchery             }
1758*b077aed3SPierre Pronchery             X509_free(oldcert);
1759*b077aed3SPierre Pronchery         }
1760*b077aed3SPierre Pronchery     }
1761*b077aed3SPierre Pronchery     cleanse(opt_keypass);
1762*b077aed3SPierre Pronchery     if (opt_revreason > CRL_REASON_NONE)
1763*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_REVOCATION_REASON,
1764*b077aed3SPierre Pronchery                                       opt_revreason);
1765*b077aed3SPierre Pronchery 
1766*b077aed3SPierre Pronchery     return 1;
1767*b077aed3SPierre Pronchery 
1768*b077aed3SPierre Pronchery  oom:
1769*b077aed3SPierre Pronchery     CMP_err("out of memory");
1770*b077aed3SPierre Pronchery  exts_err:
1771*b077aed3SPierre Pronchery     sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
1772*b077aed3SPierre Pronchery     X509_REQ_free(csr);
1773*b077aed3SPierre Pronchery     return 0;
1774*b077aed3SPierre Pronchery }
1775*b077aed3SPierre Pronchery 
1776*b077aed3SPierre Pronchery static int handle_opt_geninfo(OSSL_CMP_CTX *ctx)
1777*b077aed3SPierre Pronchery {
1778*b077aed3SPierre Pronchery     long value;
1779*b077aed3SPierre Pronchery     ASN1_OBJECT *type;
1780*b077aed3SPierre Pronchery     ASN1_INTEGER *aint;
1781*b077aed3SPierre Pronchery     ASN1_TYPE *val;
1782*b077aed3SPierre Pronchery     OSSL_CMP_ITAV *itav;
1783*b077aed3SPierre Pronchery     char *endstr;
1784*b077aed3SPierre Pronchery     char *valptr = strchr(opt_geninfo, ':');
1785*b077aed3SPierre Pronchery 
1786*b077aed3SPierre Pronchery     if (valptr == NULL) {
1787*b077aed3SPierre Pronchery         CMP_err("missing ':' in -geninfo option");
1788*b077aed3SPierre Pronchery         return 0;
1789*b077aed3SPierre Pronchery     }
1790*b077aed3SPierre Pronchery     valptr[0] = '\0';
1791*b077aed3SPierre Pronchery     valptr++;
1792*b077aed3SPierre Pronchery 
1793*b077aed3SPierre Pronchery     if (OPENSSL_strncasecmp(valptr, "int:", 4) != 0) {
1794*b077aed3SPierre Pronchery         CMP_err("missing 'int:' in -geninfo option");
1795*b077aed3SPierre Pronchery         return 0;
1796*b077aed3SPierre Pronchery     }
1797*b077aed3SPierre Pronchery     valptr += 4;
1798*b077aed3SPierre Pronchery 
1799*b077aed3SPierre Pronchery     value = strtol(valptr, &endstr, 10);
1800*b077aed3SPierre Pronchery     if (endstr == valptr || *endstr != '\0') {
1801*b077aed3SPierre Pronchery         CMP_err("cannot parse int in -geninfo option");
1802*b077aed3SPierre Pronchery         return 0;
1803*b077aed3SPierre Pronchery     }
1804*b077aed3SPierre Pronchery 
1805*b077aed3SPierre Pronchery     type = OBJ_txt2obj(opt_geninfo, 1);
1806*b077aed3SPierre Pronchery     if (type == NULL) {
1807*b077aed3SPierre Pronchery         CMP_err("cannot parse OID in -geninfo option");
1808*b077aed3SPierre Pronchery         return 0;
1809*b077aed3SPierre Pronchery     }
1810*b077aed3SPierre Pronchery 
1811*b077aed3SPierre Pronchery     if ((aint = ASN1_INTEGER_new()) == NULL)
1812*b077aed3SPierre Pronchery         goto oom;
1813*b077aed3SPierre Pronchery 
1814*b077aed3SPierre Pronchery     val = ASN1_TYPE_new();
1815*b077aed3SPierre Pronchery     if (!ASN1_INTEGER_set(aint, value) || val == NULL) {
1816*b077aed3SPierre Pronchery         ASN1_INTEGER_free(aint);
1817*b077aed3SPierre Pronchery         goto oom;
1818*b077aed3SPierre Pronchery     }
1819*b077aed3SPierre Pronchery     ASN1_TYPE_set(val, V_ASN1_INTEGER, aint);
1820*b077aed3SPierre Pronchery     itav = OSSL_CMP_ITAV_create(type, val);
1821*b077aed3SPierre Pronchery     if (itav == NULL) {
1822*b077aed3SPierre Pronchery         ASN1_TYPE_free(val);
1823*b077aed3SPierre Pronchery         goto oom;
1824*b077aed3SPierre Pronchery     }
1825*b077aed3SPierre Pronchery 
1826*b077aed3SPierre Pronchery     if (!OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav)) {
1827*b077aed3SPierre Pronchery         OSSL_CMP_ITAV_free(itav);
1828*b077aed3SPierre Pronchery         return 0;
1829*b077aed3SPierre Pronchery     }
1830*b077aed3SPierre Pronchery     return 1;
1831*b077aed3SPierre Pronchery 
1832*b077aed3SPierre Pronchery  oom:
1833*b077aed3SPierre Pronchery     ASN1_OBJECT_free(type);
1834*b077aed3SPierre Pronchery     CMP_err("out of memory");
1835*b077aed3SPierre Pronchery     return 0;
1836*b077aed3SPierre Pronchery }
1837*b077aed3SPierre Pronchery 
1838*b077aed3SPierre Pronchery 
1839*b077aed3SPierre Pronchery /*
1840*b077aed3SPierre Pronchery  * set up the client-side OSSL_CMP_CTX based on options from config file/CLI
1841*b077aed3SPierre Pronchery  * while parsing options and checking their consistency.
1842*b077aed3SPierre Pronchery  * Prints reason for error to bio_err.
1843*b077aed3SPierre Pronchery  * Returns 1 on success, 0 on error
1844*b077aed3SPierre Pronchery  */
1845*b077aed3SPierre Pronchery static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
1846*b077aed3SPierre Pronchery {
1847*b077aed3SPierre Pronchery     int ret = 0;
1848*b077aed3SPierre Pronchery     char *host = NULL, *port = NULL, *path = NULL, *used_path = opt_path;
1849*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
1850*b077aed3SPierre Pronchery     int portnum, ssl;
1851*b077aed3SPierre Pronchery     static char server_port[32] = { '\0' };
1852*b077aed3SPierre Pronchery     const char *proxy_host = NULL;
1853*b077aed3SPierre Pronchery #endif
1854*b077aed3SPierre Pronchery     char server_buf[200] = "mock server";
1855*b077aed3SPierre Pronchery     char proxy_buf[200] = "";
1856*b077aed3SPierre Pronchery 
1857*b077aed3SPierre Pronchery     if (!opt_use_mock_srv && opt_rspin == NULL) { /* note: -port is not given */
1858*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
1859*b077aed3SPierre Pronchery         if (opt_server == NULL) {
1860*b077aed3SPierre Pronchery             CMP_err("missing -server or -use_mock_srv or -rspin option");
1861*b077aed3SPierre Pronchery             goto err;
1862*b077aed3SPierre Pronchery         }
1863*b077aed3SPierre Pronchery #else
1864*b077aed3SPierre Pronchery         CMP_err("missing -use_mock_srv or -rspin option; -server option is not supported due to no-sock build");
1865*b077aed3SPierre Pronchery         goto err;
1866*b077aed3SPierre Pronchery #endif
1867*b077aed3SPierre Pronchery     }
1868*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
1869*b077aed3SPierre Pronchery     if (opt_server == NULL) {
1870*b077aed3SPierre Pronchery         if (opt_proxy != NULL)
1871*b077aed3SPierre Pronchery             CMP_warn("ignoring -proxy option since -server is not given");
1872*b077aed3SPierre Pronchery         if (opt_no_proxy != NULL)
1873*b077aed3SPierre Pronchery             CMP_warn("ignoring -no_proxy option since -server is not given");
1874*b077aed3SPierre Pronchery         if (opt_tls_used) {
1875*b077aed3SPierre Pronchery             CMP_warn("ignoring -tls_used option since -server is not given");
1876*b077aed3SPierre Pronchery             opt_tls_used = 0;
1877*b077aed3SPierre Pronchery         }
1878*b077aed3SPierre Pronchery         goto set_path;
1879*b077aed3SPierre Pronchery     }
1880*b077aed3SPierre Pronchery     if (!OSSL_HTTP_parse_url(opt_server, &ssl, NULL /* user */, &host, &port,
1881*b077aed3SPierre Pronchery                              &portnum, &path, NULL /* q */, NULL /* frag */)) {
1882*b077aed3SPierre Pronchery         CMP_err1("cannot parse -server URL: %s", opt_server);
1883*b077aed3SPierre Pronchery         goto err;
1884*b077aed3SPierre Pronchery     }
1885*b077aed3SPierre Pronchery     if (ssl && !opt_tls_used) {
1886*b077aed3SPierre Pronchery         CMP_err("missing -tls_used option since -server URL indicates https");
1887*b077aed3SPierre Pronchery         goto err;
1888*b077aed3SPierre Pronchery     }
1889*b077aed3SPierre Pronchery 
1890*b077aed3SPierre Pronchery     BIO_snprintf(server_port, sizeof(server_port), "%s", port);
1891*b077aed3SPierre Pronchery     if (opt_path == NULL)
1892*b077aed3SPierre Pronchery         used_path = path;
1893*b077aed3SPierre Pronchery     if (!OSSL_CMP_CTX_set1_server(ctx, host)
1894*b077aed3SPierre Pronchery             || !OSSL_CMP_CTX_set_serverPort(ctx, portnum))
1895*b077aed3SPierre Pronchery         goto oom;
1896*b077aed3SPierre Pronchery     if (opt_proxy != NULL && !OSSL_CMP_CTX_set1_proxy(ctx, opt_proxy))
1897*b077aed3SPierre Pronchery         goto oom;
1898*b077aed3SPierre Pronchery     if (opt_no_proxy != NULL && !OSSL_CMP_CTX_set1_no_proxy(ctx, opt_no_proxy))
1899*b077aed3SPierre Pronchery         goto oom;
1900*b077aed3SPierre Pronchery     (void)BIO_snprintf(server_buf, sizeof(server_buf), "http%s://%s:%s/%s",
1901*b077aed3SPierre Pronchery                        opt_tls_used ? "s" : "", host, port,
1902*b077aed3SPierre Pronchery                        *used_path == '/' ? used_path + 1 : used_path);
1903*b077aed3SPierre Pronchery 
1904*b077aed3SPierre Pronchery     proxy_host = OSSL_HTTP_adapt_proxy(opt_proxy, opt_no_proxy, host, ssl);
1905*b077aed3SPierre Pronchery     if (proxy_host != NULL)
1906*b077aed3SPierre Pronchery         (void)BIO_snprintf(proxy_buf, sizeof(proxy_buf), " via %s", proxy_host);
1907*b077aed3SPierre Pronchery 
1908*b077aed3SPierre Pronchery  set_path:
1909*b077aed3SPierre Pronchery #endif
1910*b077aed3SPierre Pronchery 
1911*b077aed3SPierre Pronchery     if (!OSSL_CMP_CTX_set1_serverPath(ctx, used_path))
1912*b077aed3SPierre Pronchery         goto oom;
1913*b077aed3SPierre Pronchery     if (!transform_opts())
1914*b077aed3SPierre Pronchery         goto err;
1915*b077aed3SPierre Pronchery 
1916*b077aed3SPierre Pronchery     if (opt_infotype_s != NULL) {
1917*b077aed3SPierre Pronchery         char id_buf[100] = "id-it-";
1918*b077aed3SPierre Pronchery 
1919*b077aed3SPierre Pronchery         strncat(id_buf, opt_infotype_s, sizeof(id_buf) - strlen(id_buf) - 1);
1920*b077aed3SPierre Pronchery         if ((opt_infotype = OBJ_sn2nid(id_buf)) == NID_undef) {
1921*b077aed3SPierre Pronchery             CMP_err("unknown OID name in -infotype option");
1922*b077aed3SPierre Pronchery             goto err;
1923*b077aed3SPierre Pronchery         }
1924*b077aed3SPierre Pronchery     }
1925*b077aed3SPierre Pronchery 
1926*b077aed3SPierre Pronchery     if (!setup_verification_ctx(ctx))
1927*b077aed3SPierre Pronchery         goto err;
1928*b077aed3SPierre Pronchery 
1929*b077aed3SPierre Pronchery     if (opt_keep_alive != 1)
1930*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_KEEP_ALIVE,
1931*b077aed3SPierre Pronchery                                       opt_keep_alive);
1932*b077aed3SPierre Pronchery     if (opt_total_timeout > 0 && opt_msg_timeout > 0
1933*b077aed3SPierre Pronchery             && opt_total_timeout < opt_msg_timeout) {
1934*b077aed3SPierre Pronchery         CMP_err2("-total_timeout argument = %d must not be < %d (-msg_timeout)",
1935*b077aed3SPierre Pronchery                  opt_total_timeout, opt_msg_timeout);
1936*b077aed3SPierre Pronchery         goto err;
1937*b077aed3SPierre Pronchery     }
1938*b077aed3SPierre Pronchery     if (opt_msg_timeout >= 0) /* must do this before setup_ssl_ctx() */
1939*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_MSG_TIMEOUT,
1940*b077aed3SPierre Pronchery                                       opt_msg_timeout);
1941*b077aed3SPierre Pronchery     if (opt_total_timeout >= 0)
1942*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_TOTAL_TIMEOUT,
1943*b077aed3SPierre Pronchery                                       opt_total_timeout);
1944*b077aed3SPierre Pronchery 
1945*b077aed3SPierre Pronchery     if (opt_rspin != NULL) {
1946*b077aed3SPierre Pronchery         rspin_in_use = 1;
1947*b077aed3SPierre Pronchery         if (opt_reqin != NULL)
1948*b077aed3SPierre Pronchery             CMP_warn("-reqin is ignored since -rspin is present");
1949*b077aed3SPierre Pronchery     }
1950*b077aed3SPierre Pronchery     if (opt_reqin_new_tid && opt_reqin == NULL)
1951*b077aed3SPierre Pronchery         CMP_warn("-reqin_new_tid is ignored since -reqin is not present");
1952*b077aed3SPierre Pronchery     if (opt_reqin != NULL || opt_reqout != NULL
1953*b077aed3SPierre Pronchery             || opt_rspin != NULL || opt_rspout != NULL || opt_use_mock_srv)
1954*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_transfer_cb(ctx, read_write_req_resp);
1955*b077aed3SPierre Pronchery 
1956*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
1957*b077aed3SPierre Pronchery     if (opt_tls_used) {
1958*b077aed3SPierre Pronchery         APP_HTTP_TLS_INFO *info;
1959*b077aed3SPierre Pronchery 
1960*b077aed3SPierre Pronchery         if (opt_tls_cert != NULL
1961*b077aed3SPierre Pronchery             || opt_tls_key != NULL || opt_tls_keypass != NULL) {
1962*b077aed3SPierre Pronchery             if (opt_tls_key == NULL) {
1963*b077aed3SPierre Pronchery                 CMP_err("missing -tls_key option");
1964*b077aed3SPierre Pronchery                 goto err;
1965*b077aed3SPierre Pronchery             } else if (opt_tls_cert == NULL) {
1966*b077aed3SPierre Pronchery                 CMP_err("missing -tls_cert option");
1967*b077aed3SPierre Pronchery                 goto err;
1968*b077aed3SPierre Pronchery             }
1969*b077aed3SPierre Pronchery         }
1970*b077aed3SPierre Pronchery 
1971*b077aed3SPierre Pronchery         if ((info = OPENSSL_zalloc(sizeof(*info))) == NULL)
1972*b077aed3SPierre Pronchery             goto err;
1973*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_http_cb_arg(ctx, info);
1974*b077aed3SPierre Pronchery         info->ssl_ctx = setup_ssl_ctx(ctx, host, engine);
1975*b077aed3SPierre Pronchery         info->server = host;
1976*b077aed3SPierre Pronchery         host = NULL; /* prevent deallocation */
1977*b077aed3SPierre Pronchery         if ((info->port = OPENSSL_strdup(server_port)) == NULL)
1978*b077aed3SPierre Pronchery             goto err;
1979*b077aed3SPierre Pronchery         /* workaround for callback design flaw, see #17088: */
1980*b077aed3SPierre Pronchery         info->use_proxy = proxy_host != NULL;
1981*b077aed3SPierre Pronchery         info->timeout = OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_MSG_TIMEOUT);
1982*b077aed3SPierre Pronchery 
1983*b077aed3SPierre Pronchery         if (info->ssl_ctx == NULL)
1984*b077aed3SPierre Pronchery             goto err;
1985*b077aed3SPierre Pronchery         (void)OSSL_CMP_CTX_set_http_cb(ctx, app_http_tls_cb);
1986*b077aed3SPierre Pronchery     }
1987*b077aed3SPierre Pronchery #endif
1988*b077aed3SPierre Pronchery 
1989*b077aed3SPierre Pronchery     if (!setup_protection_ctx(ctx, engine))
1990*b077aed3SPierre Pronchery         goto err;
1991*b077aed3SPierre Pronchery 
1992*b077aed3SPierre Pronchery     if (!setup_request_ctx(ctx, engine))
1993*b077aed3SPierre Pronchery         goto err;
1994*b077aed3SPierre Pronchery 
1995*b077aed3SPierre Pronchery     if (!set_name(opt_recipient, OSSL_CMP_CTX_set1_recipient, ctx, "recipient")
1996*b077aed3SPierre Pronchery             || !set_name(opt_expect_sender, OSSL_CMP_CTX_set1_expected_sender,
1997*b077aed3SPierre Pronchery                          ctx, "expected sender"))
1998*b077aed3SPierre Pronchery         goto err;
1999*b077aed3SPierre Pronchery 
2000*b077aed3SPierre Pronchery     if (opt_geninfo != NULL && !handle_opt_geninfo(ctx))
2001*b077aed3SPierre Pronchery         goto err;
2002*b077aed3SPierre Pronchery 
2003*b077aed3SPierre Pronchery     /* not printing earlier, to minimize confusion in case setup fails before */
2004*b077aed3SPierre Pronchery     if (opt_rspin != NULL)
2005*b077aed3SPierre Pronchery         CMP_info2("will contact %s%s "
2006*b077aed3SPierre Pronchery                   "only if -rspin argument gives too few filenames",
2007*b077aed3SPierre Pronchery                   server_buf, proxy_buf);
2008*b077aed3SPierre Pronchery     else
2009*b077aed3SPierre Pronchery         CMP_info2("will contact %s%s", server_buf, proxy_buf);
2010*b077aed3SPierre Pronchery 
2011*b077aed3SPierre Pronchery     ret = 1;
2012*b077aed3SPierre Pronchery 
2013*b077aed3SPierre Pronchery  err:
2014*b077aed3SPierre Pronchery     OPENSSL_free(host);
2015*b077aed3SPierre Pronchery     OPENSSL_free(port);
2016*b077aed3SPierre Pronchery     OPENSSL_free(path);
2017*b077aed3SPierre Pronchery     return ret;
2018*b077aed3SPierre Pronchery  oom:
2019*b077aed3SPierre Pronchery     CMP_err("out of memory");
2020*b077aed3SPierre Pronchery     goto err;
2021*b077aed3SPierre Pronchery }
2022*b077aed3SPierre Pronchery 
2023*b077aed3SPierre Pronchery /*
2024*b077aed3SPierre Pronchery  * write out the given certificate to the output specified by bio.
2025*b077aed3SPierre Pronchery  * Depending on options use either PEM or DER format.
2026*b077aed3SPierre Pronchery  * Returns 1 on success, 0 on error
2027*b077aed3SPierre Pronchery  */
2028*b077aed3SPierre Pronchery static int write_cert(BIO *bio, X509 *cert)
2029*b077aed3SPierre Pronchery {
2030*b077aed3SPierre Pronchery     if ((opt_certform == FORMAT_PEM && PEM_write_bio_X509(bio, cert))
2031*b077aed3SPierre Pronchery             || (opt_certform == FORMAT_ASN1 && i2d_X509_bio(bio, cert)))
2032*b077aed3SPierre Pronchery         return 1;
2033*b077aed3SPierre Pronchery     if (opt_certform != FORMAT_PEM && opt_certform != FORMAT_ASN1)
2034*b077aed3SPierre Pronchery         BIO_printf(bio_err,
2035*b077aed3SPierre Pronchery                    "error: unsupported type '%s' for writing certificates\n",
2036*b077aed3SPierre Pronchery                    opt_certform_s);
2037*b077aed3SPierre Pronchery     return 0;
2038*b077aed3SPierre Pronchery }
2039*b077aed3SPierre Pronchery 
2040*b077aed3SPierre Pronchery /*
2041*b077aed3SPierre Pronchery  * If destFile != NULL writes out a stack of certs to the given file.
2042*b077aed3SPierre Pronchery  * In any case frees the certs.
2043*b077aed3SPierre Pronchery  * Depending on options use either PEM or DER format,
2044*b077aed3SPierre Pronchery  * where DER does not make much sense for writing more than one cert!
2045*b077aed3SPierre Pronchery  * Returns number of written certificates on success, -1 on error.
2046*b077aed3SPierre Pronchery  */
2047*b077aed3SPierre Pronchery static int save_free_certs(OSSL_CMP_CTX *ctx,
2048*b077aed3SPierre Pronchery                            STACK_OF(X509) *certs, char *destFile, char *desc)
2049*b077aed3SPierre Pronchery {
2050*b077aed3SPierre Pronchery     BIO *bio = NULL;
2051*b077aed3SPierre Pronchery     int i;
2052*b077aed3SPierre Pronchery     int n = sk_X509_num(certs);
2053*b077aed3SPierre Pronchery 
2054*b077aed3SPierre Pronchery     if (destFile == NULL)
2055*b077aed3SPierre Pronchery         goto end;
2056*b077aed3SPierre Pronchery     CMP_info3("received %d %s certificate(s), saving to file '%s'",
2057*b077aed3SPierre Pronchery               n, desc, destFile);
2058*b077aed3SPierre Pronchery     if (n > 1 && opt_certform != FORMAT_PEM)
2059*b077aed3SPierre Pronchery         CMP_warn("saving more than one certificate in non-PEM format");
2060*b077aed3SPierre Pronchery 
2061*b077aed3SPierre Pronchery     if (destFile == NULL || (bio = BIO_new(BIO_s_file())) == NULL
2062*b077aed3SPierre Pronchery             || !BIO_write_filename(bio, (char *)destFile)) {
2063*b077aed3SPierre Pronchery         CMP_err1("could not open file '%s' for writing", destFile);
2064*b077aed3SPierre Pronchery         n = -1;
2065*b077aed3SPierre Pronchery         goto end;
2066*b077aed3SPierre Pronchery     }
2067*b077aed3SPierre Pronchery 
2068*b077aed3SPierre Pronchery     for (i = 0; i < n; i++) {
2069*b077aed3SPierre Pronchery         if (!write_cert(bio, sk_X509_value(certs, i))) {
2070*b077aed3SPierre Pronchery             CMP_err1("cannot write certificate to file '%s'", destFile);
2071*b077aed3SPierre Pronchery             n = -1;
2072*b077aed3SPierre Pronchery             goto end;
2073*b077aed3SPierre Pronchery         }
2074*b077aed3SPierre Pronchery     }
2075*b077aed3SPierre Pronchery 
2076*b077aed3SPierre Pronchery  end:
2077*b077aed3SPierre Pronchery     BIO_free(bio);
2078*b077aed3SPierre Pronchery     sk_X509_pop_free(certs, X509_free);
2079*b077aed3SPierre Pronchery     return n;
2080*b077aed3SPierre Pronchery }
2081*b077aed3SPierre Pronchery 
2082*b077aed3SPierre Pronchery static void print_itavs(STACK_OF(OSSL_CMP_ITAV) *itavs)
2083*b077aed3SPierre Pronchery {
2084*b077aed3SPierre Pronchery     OSSL_CMP_ITAV *itav = NULL;
2085*b077aed3SPierre Pronchery     char buf[128];
2086*b077aed3SPierre Pronchery     int i, r;
2087*b077aed3SPierre Pronchery     int n = sk_OSSL_CMP_ITAV_num(itavs); /* itavs == NULL leads to 0 */
2088*b077aed3SPierre Pronchery 
2089*b077aed3SPierre Pronchery     if (n == 0) {
2090*b077aed3SPierre Pronchery         CMP_info("genp contains no ITAV");
2091*b077aed3SPierre Pronchery         return;
2092*b077aed3SPierre Pronchery     }
2093*b077aed3SPierre Pronchery 
2094*b077aed3SPierre Pronchery     for (i = 0; i < n; i++) {
2095*b077aed3SPierre Pronchery         itav = sk_OSSL_CMP_ITAV_value(itavs, i);
2096*b077aed3SPierre Pronchery         r = OBJ_obj2txt(buf, 128, OSSL_CMP_ITAV_get0_type(itav), 0);
2097*b077aed3SPierre Pronchery         if (r < 0)
2098*b077aed3SPierre Pronchery             CMP_err("could not get ITAV details");
2099*b077aed3SPierre Pronchery         else if (r == 0)
2100*b077aed3SPierre Pronchery             CMP_info("genp contains empty ITAV");
2101*b077aed3SPierre Pronchery         else
2102*b077aed3SPierre Pronchery             CMP_info1("genp contains ITAV of type: %s", buf);
2103*b077aed3SPierre Pronchery     }
2104*b077aed3SPierre Pronchery }
2105*b077aed3SPierre Pronchery 
2106*b077aed3SPierre Pronchery static char opt_item[SECTION_NAME_MAX + 1];
2107*b077aed3SPierre Pronchery /* get previous name from a comma or space-separated list of names */
2108*b077aed3SPierre Pronchery static const char *prev_item(const char *opt, const char *end)
2109*b077aed3SPierre Pronchery {
2110*b077aed3SPierre Pronchery     const char *beg;
2111*b077aed3SPierre Pronchery     size_t len;
2112*b077aed3SPierre Pronchery 
2113*b077aed3SPierre Pronchery     if (end == opt)
2114*b077aed3SPierre Pronchery         return NULL;
2115*b077aed3SPierre Pronchery     beg = end;
2116*b077aed3SPierre Pronchery     while (beg > opt) {
2117*b077aed3SPierre Pronchery         --beg;
2118*b077aed3SPierre Pronchery         if (beg[0] == ',' || isspace(beg[0])) {
2119*b077aed3SPierre Pronchery             ++beg;
2120*b077aed3SPierre Pronchery             break;
2121*b077aed3SPierre Pronchery         }
2122*b077aed3SPierre Pronchery     }
2123*b077aed3SPierre Pronchery     len = end - beg;
2124*b077aed3SPierre Pronchery     if (len > SECTION_NAME_MAX) {
2125*b077aed3SPierre Pronchery         CMP_warn3("using only first %d characters of section name starting with \"%.*s\"",
2126*b077aed3SPierre Pronchery                   SECTION_NAME_MAX, SECTION_NAME_MAX, beg);
2127*b077aed3SPierre Pronchery         len = SECTION_NAME_MAX;
2128*b077aed3SPierre Pronchery     }
2129*b077aed3SPierre Pronchery     memcpy(opt_item, beg, len);
2130*b077aed3SPierre Pronchery     opt_item[len] = '\0';
2131*b077aed3SPierre Pronchery     while (beg > opt) {
2132*b077aed3SPierre Pronchery         --beg;
2133*b077aed3SPierre Pronchery         if (beg[0] != ',' && !isspace(beg[0])) {
2134*b077aed3SPierre Pronchery             ++beg;
2135*b077aed3SPierre Pronchery             break;
2136*b077aed3SPierre Pronchery         }
2137*b077aed3SPierre Pronchery     }
2138*b077aed3SPierre Pronchery     return beg;
2139*b077aed3SPierre Pronchery }
2140*b077aed3SPierre Pronchery 
2141*b077aed3SPierre Pronchery /* get str value for name from a comma-separated hierarchy of config sections */
2142*b077aed3SPierre Pronchery static char *conf_get_string(const CONF *src_conf, const char *groups,
2143*b077aed3SPierre Pronchery                              const char *name)
2144*b077aed3SPierre Pronchery {
2145*b077aed3SPierre Pronchery     char *res = NULL;
2146*b077aed3SPierre Pronchery     const char *end = groups + strlen(groups);
2147*b077aed3SPierre Pronchery 
2148*b077aed3SPierre Pronchery     while ((end = prev_item(groups, end)) != NULL) {
2149*b077aed3SPierre Pronchery         if ((res = NCONF_get_string(src_conf, opt_item, name)) != NULL)
2150*b077aed3SPierre Pronchery             return res;
2151*b077aed3SPierre Pronchery     }
2152*b077aed3SPierre Pronchery     return res;
2153*b077aed3SPierre Pronchery }
2154*b077aed3SPierre Pronchery 
2155*b077aed3SPierre Pronchery /* get long val for name from a comma-separated hierarchy of config sections */
2156*b077aed3SPierre Pronchery static int conf_get_number_e(const CONF *conf_, const char *groups,
2157*b077aed3SPierre Pronchery                              const char *name, long *result)
2158*b077aed3SPierre Pronchery {
2159*b077aed3SPierre Pronchery     char *str = conf_get_string(conf_, groups, name);
2160*b077aed3SPierre Pronchery     char *tailptr;
2161*b077aed3SPierre Pronchery     long res;
2162*b077aed3SPierre Pronchery 
2163*b077aed3SPierre Pronchery     if (str == NULL || *str == '\0')
2164*b077aed3SPierre Pronchery         return 0;
2165*b077aed3SPierre Pronchery 
2166*b077aed3SPierre Pronchery     res = strtol(str, &tailptr, 10);
2167*b077aed3SPierre Pronchery     if (res == LONG_MIN || res == LONG_MAX || *tailptr != '\0')
2168*b077aed3SPierre Pronchery         return 0;
2169*b077aed3SPierre Pronchery 
2170*b077aed3SPierre Pronchery     *result = res;
2171*b077aed3SPierre Pronchery     return 1;
2172*b077aed3SPierre Pronchery }
2173*b077aed3SPierre Pronchery 
2174*b077aed3SPierre Pronchery /*
2175*b077aed3SPierre Pronchery  * use the command line option table to read values from the CMP section
2176*b077aed3SPierre Pronchery  * of openssl.cnf.  Defaults are taken from the config file, they can be
2177*b077aed3SPierre Pronchery  * overwritten on the command line.
2178*b077aed3SPierre Pronchery  */
2179*b077aed3SPierre Pronchery static int read_config(void)
2180*b077aed3SPierre Pronchery {
2181*b077aed3SPierre Pronchery     unsigned int i;
2182*b077aed3SPierre Pronchery     long num = 0;
2183*b077aed3SPierre Pronchery     char *txt = NULL;
2184*b077aed3SPierre Pronchery     const OPTIONS *opt;
2185*b077aed3SPierre Pronchery     int start_opt = OPT_VERBOSITY - OPT_HELP;
2186*b077aed3SPierre Pronchery     int start_idx = OPT_VERBOSITY - 2;
2187*b077aed3SPierre Pronchery     /*
2188*b077aed3SPierre Pronchery      * starting with offset OPT_VERBOSITY because OPT_CONFIG and OPT_SECTION
2189*b077aed3SPierre Pronchery      * would not make sense within the config file.
2190*b077aed3SPierre Pronchery      */
2191*b077aed3SPierre Pronchery     int n_options = OSSL_NELEM(cmp_options) - 1;
2192*b077aed3SPierre Pronchery 
2193*b077aed3SPierre Pronchery     for (opt = &cmp_options[start_opt], i = start_idx;
2194*b077aed3SPierre Pronchery          opt->name != NULL; i++, opt++)
2195*b077aed3SPierre Pronchery         if (!strcmp(opt->name, OPT_SECTION_STR)
2196*b077aed3SPierre Pronchery                 || !strcmp(opt->name, OPT_MORE_STR))
2197*b077aed3SPierre Pronchery             n_options--;
2198*b077aed3SPierre Pronchery     OPENSSL_assert(OSSL_NELEM(cmp_vars) == n_options
2199*b077aed3SPierre Pronchery                  + OPT_PROV__FIRST + 1 - OPT_PROV__LAST
2200*b077aed3SPierre Pronchery                  + OPT_R__FIRST + 1 - OPT_R__LAST
2201*b077aed3SPierre Pronchery                  + OPT_V__FIRST + 1 - OPT_V__LAST);
2202*b077aed3SPierre Pronchery     for (opt = &cmp_options[start_opt], i = start_idx;
2203*b077aed3SPierre Pronchery          opt->name != NULL; i++, opt++) {
2204*b077aed3SPierre Pronchery         int provider_option = (OPT_PROV__FIRST <= opt->retval
2205*b077aed3SPierre Pronchery                                && opt->retval < OPT_PROV__LAST);
2206*b077aed3SPierre Pronchery         int rand_state_option = (OPT_R__FIRST <= opt->retval
2207*b077aed3SPierre Pronchery                                  && opt->retval < OPT_R__LAST);
2208*b077aed3SPierre Pronchery         int verification_option = (OPT_V__FIRST <= opt->retval
2209*b077aed3SPierre Pronchery                                    && opt->retval < OPT_V__LAST);
2210*b077aed3SPierre Pronchery 
2211*b077aed3SPierre Pronchery         if (strcmp(opt->name, OPT_SECTION_STR) == 0
2212*b077aed3SPierre Pronchery                 || strcmp(opt->name, OPT_MORE_STR) == 0) {
2213*b077aed3SPierre Pronchery             i--;
2214*b077aed3SPierre Pronchery             continue;
2215*b077aed3SPierre Pronchery         }
2216*b077aed3SPierre Pronchery         if (provider_option || rand_state_option || verification_option)
2217*b077aed3SPierre Pronchery             i--;
2218*b077aed3SPierre Pronchery         switch (opt->valtype) {
2219*b077aed3SPierre Pronchery         case '-':
2220*b077aed3SPierre Pronchery         case 'p':
2221*b077aed3SPierre Pronchery         case 'n':
2222*b077aed3SPierre Pronchery         case 'N':
2223*b077aed3SPierre Pronchery         case 'l':
2224*b077aed3SPierre Pronchery             if (!conf_get_number_e(conf, opt_section, opt->name, &num)) {
2225*b077aed3SPierre Pronchery                 ERR_clear_error();
2226*b077aed3SPierre Pronchery                 continue; /* option not provided */
2227*b077aed3SPierre Pronchery             }
2228*b077aed3SPierre Pronchery             if (opt->valtype == 'p' && num <= 0) {
2229*b077aed3SPierre Pronchery                 opt_printf_stderr("Non-positive number \"%ld\" for config option -%s\n",
2230*b077aed3SPierre Pronchery                                   num, opt->name);
2231*b077aed3SPierre Pronchery                 return -1;
2232*b077aed3SPierre Pronchery             }
2233*b077aed3SPierre Pronchery             if (opt->valtype == 'N' && num < 0) {
2234*b077aed3SPierre Pronchery                 opt_printf_stderr("Negative number \"%ld\" for config option -%s\n",
2235*b077aed3SPierre Pronchery                                   num, opt->name);
2236*b077aed3SPierre Pronchery                 return -1;
2237*b077aed3SPierre Pronchery             }
2238*b077aed3SPierre Pronchery             break;
2239*b077aed3SPierre Pronchery         case 's':
2240*b077aed3SPierre Pronchery         case '>':
2241*b077aed3SPierre Pronchery         case 'M':
2242*b077aed3SPierre Pronchery             txt = conf_get_string(conf, opt_section, opt->name);
2243*b077aed3SPierre Pronchery             if (txt == NULL) {
2244*b077aed3SPierre Pronchery                 ERR_clear_error();
2245*b077aed3SPierre Pronchery                 continue; /* option not provided */
2246*b077aed3SPierre Pronchery             }
2247*b077aed3SPierre Pronchery             break;
2248*b077aed3SPierre Pronchery         default:
2249*b077aed3SPierre Pronchery             CMP_err2("internal: unsupported type '%c' for option '%s'",
2250*b077aed3SPierre Pronchery                      opt->valtype, opt->name);
2251*b077aed3SPierre Pronchery             return 0;
2252*b077aed3SPierre Pronchery             break;
2253*b077aed3SPierre Pronchery         }
2254*b077aed3SPierre Pronchery         if (provider_option || verification_option) {
2255*b077aed3SPierre Pronchery             int conf_argc = 1;
2256*b077aed3SPierre Pronchery             char *conf_argv[3];
2257*b077aed3SPierre Pronchery             char arg1[82];
2258*b077aed3SPierre Pronchery 
2259*b077aed3SPierre Pronchery             BIO_snprintf(arg1, 81, "-%s", (char *)opt->name);
2260*b077aed3SPierre Pronchery             conf_argv[0] = prog;
2261*b077aed3SPierre Pronchery             conf_argv[1] = arg1;
2262*b077aed3SPierre Pronchery             if (opt->valtype == '-') {
2263*b077aed3SPierre Pronchery                 if (num != 0)
2264*b077aed3SPierre Pronchery                     conf_argc = 2;
2265*b077aed3SPierre Pronchery             } else {
2266*b077aed3SPierre Pronchery                 conf_argc = 3;
2267*b077aed3SPierre Pronchery                 conf_argv[2] = conf_get_string(conf, opt_section, opt->name);
2268*b077aed3SPierre Pronchery                 /* not NULL */
2269*b077aed3SPierre Pronchery             }
2270*b077aed3SPierre Pronchery             if (conf_argc > 1) {
2271*b077aed3SPierre Pronchery                 (void)opt_init(conf_argc, conf_argv, cmp_options);
2272*b077aed3SPierre Pronchery 
2273*b077aed3SPierre Pronchery                 if (provider_option
2274*b077aed3SPierre Pronchery                     ? !opt_provider(opt_next())
2275*b077aed3SPierre Pronchery                     : !opt_verify(opt_next(), vpm)) {
2276*b077aed3SPierre Pronchery                     CMP_err2("for option '%s' in config file section '%s'",
2277*b077aed3SPierre Pronchery                              opt->name, opt_section);
2278*b077aed3SPierre Pronchery                     return 0;
2279*b077aed3SPierre Pronchery                 }
2280*b077aed3SPierre Pronchery             }
2281*b077aed3SPierre Pronchery         } else {
2282*b077aed3SPierre Pronchery             switch (opt->valtype) {
2283*b077aed3SPierre Pronchery             case '-':
2284*b077aed3SPierre Pronchery             case 'p':
2285*b077aed3SPierre Pronchery             case 'n':
2286*b077aed3SPierre Pronchery             case 'N':
2287*b077aed3SPierre Pronchery                 if (num < INT_MIN || INT_MAX < num) {
2288*b077aed3SPierre Pronchery                     BIO_printf(bio_err,
2289*b077aed3SPierre Pronchery                                "integer value out of range for option '%s'\n",
2290*b077aed3SPierre Pronchery                                opt->name);
2291*b077aed3SPierre Pronchery                     return 0;
2292*b077aed3SPierre Pronchery                 }
2293*b077aed3SPierre Pronchery                 *cmp_vars[i].num = (int)num;
2294*b077aed3SPierre Pronchery                 break;
2295*b077aed3SPierre Pronchery             case 'l':
2296*b077aed3SPierre Pronchery                 *cmp_vars[i].num_long = num;
2297*b077aed3SPierre Pronchery                 break;
2298*b077aed3SPierre Pronchery             default:
2299*b077aed3SPierre Pronchery                 if (txt != NULL && txt[0] == '\0')
2300*b077aed3SPierre Pronchery                     txt = NULL; /* reset option on empty string input */
2301*b077aed3SPierre Pronchery                 *cmp_vars[i].txt = txt;
2302*b077aed3SPierre Pronchery                 break;
2303*b077aed3SPierre Pronchery             }
2304*b077aed3SPierre Pronchery         }
2305*b077aed3SPierre Pronchery     }
2306*b077aed3SPierre Pronchery 
2307*b077aed3SPierre Pronchery     return 1;
2308*b077aed3SPierre Pronchery }
2309*b077aed3SPierre Pronchery 
2310*b077aed3SPierre Pronchery static char *opt_str(void)
2311*b077aed3SPierre Pronchery {
2312*b077aed3SPierre Pronchery     char *arg = opt_arg();
2313*b077aed3SPierre Pronchery 
2314*b077aed3SPierre Pronchery     if (arg[0] == '\0') {
2315*b077aed3SPierre Pronchery         CMP_warn1("%s option argument is empty string, resetting option",
2316*b077aed3SPierre Pronchery                   opt_flag());
2317*b077aed3SPierre Pronchery         arg = NULL;
2318*b077aed3SPierre Pronchery     } else if (arg[0] == '-') {
2319*b077aed3SPierre Pronchery         CMP_warn1("%s option argument starts with hyphen", opt_flag());
2320*b077aed3SPierre Pronchery     }
2321*b077aed3SPierre Pronchery     return arg;
2322*b077aed3SPierre Pronchery }
2323*b077aed3SPierre Pronchery 
2324*b077aed3SPierre Pronchery /* returns 1 on success, 0 on error, -1 on -help (i.e., stop with success) */
2325*b077aed3SPierre Pronchery static int get_opts(int argc, char **argv)
2326*b077aed3SPierre Pronchery {
2327*b077aed3SPierre Pronchery     OPTION_CHOICE o;
2328*b077aed3SPierre Pronchery 
2329*b077aed3SPierre Pronchery     prog = opt_init(argc, argv, cmp_options);
2330*b077aed3SPierre Pronchery 
2331*b077aed3SPierre Pronchery     while ((o = opt_next()) != OPT_EOF) {
2332*b077aed3SPierre Pronchery         switch (o) {
2333*b077aed3SPierre Pronchery         case OPT_EOF:
2334*b077aed3SPierre Pronchery         case OPT_ERR:
2335*b077aed3SPierre Pronchery  opthelp:
2336*b077aed3SPierre Pronchery             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
2337*b077aed3SPierre Pronchery             return 0;
2338*b077aed3SPierre Pronchery         case OPT_HELP:
2339*b077aed3SPierre Pronchery             opt_help(cmp_options);
2340*b077aed3SPierre Pronchery             return -1;
2341*b077aed3SPierre Pronchery         case OPT_CONFIG: /* has already been handled */
2342*b077aed3SPierre Pronchery         case OPT_SECTION: /* has already been handled */
2343*b077aed3SPierre Pronchery             break;
2344*b077aed3SPierre Pronchery         case OPT_VERBOSITY:
2345*b077aed3SPierre Pronchery             if (!set_verbosity(opt_int_arg()))
2346*b077aed3SPierre Pronchery                 goto opthelp;
2347*b077aed3SPierre Pronchery             break;
2348*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
2349*b077aed3SPierre Pronchery         case OPT_SERVER:
2350*b077aed3SPierre Pronchery             opt_server = opt_str();
2351*b077aed3SPierre Pronchery             break;
2352*b077aed3SPierre Pronchery         case OPT_PROXY:
2353*b077aed3SPierre Pronchery             opt_proxy = opt_str();
2354*b077aed3SPierre Pronchery             break;
2355*b077aed3SPierre Pronchery         case OPT_NO_PROXY:
2356*b077aed3SPierre Pronchery             opt_no_proxy = opt_str();
2357*b077aed3SPierre Pronchery             break;
2358*b077aed3SPierre Pronchery #endif
2359*b077aed3SPierre Pronchery         case OPT_RECIPIENT:
2360*b077aed3SPierre Pronchery             opt_recipient = opt_str();
2361*b077aed3SPierre Pronchery             break;
2362*b077aed3SPierre Pronchery         case OPT_PATH:
2363*b077aed3SPierre Pronchery             opt_path = opt_str();
2364*b077aed3SPierre Pronchery             break;
2365*b077aed3SPierre Pronchery         case OPT_KEEP_ALIVE:
2366*b077aed3SPierre Pronchery             opt_keep_alive = opt_int_arg();
2367*b077aed3SPierre Pronchery             if (opt_keep_alive > 2) {
2368*b077aed3SPierre Pronchery                 CMP_err("-keep_alive argument must be 0, 1, or 2");
2369*b077aed3SPierre Pronchery                 goto opthelp;
2370*b077aed3SPierre Pronchery             }
2371*b077aed3SPierre Pronchery             break;
2372*b077aed3SPierre Pronchery         case OPT_MSG_TIMEOUT:
2373*b077aed3SPierre Pronchery             opt_msg_timeout = opt_int_arg();
2374*b077aed3SPierre Pronchery             break;
2375*b077aed3SPierre Pronchery         case OPT_TOTAL_TIMEOUT:
2376*b077aed3SPierre Pronchery             opt_total_timeout = opt_int_arg();
2377*b077aed3SPierre Pronchery             break;
2378*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
2379*b077aed3SPierre Pronchery         case OPT_TLS_USED:
2380*b077aed3SPierre Pronchery             opt_tls_used = 1;
2381*b077aed3SPierre Pronchery             break;
2382*b077aed3SPierre Pronchery         case OPT_TLS_CERT:
2383*b077aed3SPierre Pronchery             opt_tls_cert = opt_str();
2384*b077aed3SPierre Pronchery             break;
2385*b077aed3SPierre Pronchery         case OPT_TLS_KEY:
2386*b077aed3SPierre Pronchery             opt_tls_key = opt_str();
2387*b077aed3SPierre Pronchery             break;
2388*b077aed3SPierre Pronchery         case OPT_TLS_KEYPASS:
2389*b077aed3SPierre Pronchery             opt_tls_keypass = opt_str();
2390*b077aed3SPierre Pronchery             break;
2391*b077aed3SPierre Pronchery         case OPT_TLS_EXTRA:
2392*b077aed3SPierre Pronchery             opt_tls_extra = opt_str();
2393*b077aed3SPierre Pronchery             break;
2394*b077aed3SPierre Pronchery         case OPT_TLS_TRUSTED:
2395*b077aed3SPierre Pronchery             opt_tls_trusted = opt_str();
2396*b077aed3SPierre Pronchery             break;
2397*b077aed3SPierre Pronchery         case OPT_TLS_HOST:
2398*b077aed3SPierre Pronchery             opt_tls_host = opt_str();
2399*b077aed3SPierre Pronchery             break;
2400*b077aed3SPierre Pronchery #endif
2401*b077aed3SPierre Pronchery 
2402*b077aed3SPierre Pronchery         case OPT_REF:
2403*b077aed3SPierre Pronchery             opt_ref = opt_str();
2404*b077aed3SPierre Pronchery             break;
2405*b077aed3SPierre Pronchery         case OPT_SECRET:
2406*b077aed3SPierre Pronchery             opt_secret = opt_str();
2407*b077aed3SPierre Pronchery             break;
2408*b077aed3SPierre Pronchery         case OPT_CERT:
2409*b077aed3SPierre Pronchery             opt_cert = opt_str();
2410*b077aed3SPierre Pronchery             break;
2411*b077aed3SPierre Pronchery         case OPT_OWN_TRUSTED:
2412*b077aed3SPierre Pronchery             opt_own_trusted = opt_str();
2413*b077aed3SPierre Pronchery             break;
2414*b077aed3SPierre Pronchery         case OPT_KEY:
2415*b077aed3SPierre Pronchery             opt_key = opt_str();
2416*b077aed3SPierre Pronchery             break;
2417*b077aed3SPierre Pronchery         case OPT_KEYPASS:
2418*b077aed3SPierre Pronchery             opt_keypass = opt_str();
2419*b077aed3SPierre Pronchery             break;
2420*b077aed3SPierre Pronchery         case OPT_DIGEST:
2421*b077aed3SPierre Pronchery             opt_digest = opt_str();
2422*b077aed3SPierre Pronchery             break;
2423*b077aed3SPierre Pronchery         case OPT_MAC:
2424*b077aed3SPierre Pronchery             opt_mac = opt_str();
2425*b077aed3SPierre Pronchery             break;
2426*b077aed3SPierre Pronchery         case OPT_EXTRACERTS:
2427*b077aed3SPierre Pronchery             opt_extracerts = opt_str();
2428*b077aed3SPierre Pronchery             break;
2429*b077aed3SPierre Pronchery         case OPT_UNPROTECTED_REQUESTS:
2430*b077aed3SPierre Pronchery             opt_unprotected_requests = 1;
2431*b077aed3SPierre Pronchery             break;
2432*b077aed3SPierre Pronchery 
2433*b077aed3SPierre Pronchery         case OPT_TRUSTED:
2434*b077aed3SPierre Pronchery             opt_trusted = opt_str();
2435*b077aed3SPierre Pronchery             break;
2436*b077aed3SPierre Pronchery         case OPT_UNTRUSTED:
2437*b077aed3SPierre Pronchery             opt_untrusted = opt_str();
2438*b077aed3SPierre Pronchery             break;
2439*b077aed3SPierre Pronchery         case OPT_SRVCERT:
2440*b077aed3SPierre Pronchery             opt_srvcert = opt_str();
2441*b077aed3SPierre Pronchery             break;
2442*b077aed3SPierre Pronchery         case OPT_EXPECT_SENDER:
2443*b077aed3SPierre Pronchery             opt_expect_sender = opt_str();
2444*b077aed3SPierre Pronchery             break;
2445*b077aed3SPierre Pronchery         case OPT_IGNORE_KEYUSAGE:
2446*b077aed3SPierre Pronchery             opt_ignore_keyusage = 1;
2447*b077aed3SPierre Pronchery             break;
2448*b077aed3SPierre Pronchery         case OPT_UNPROTECTED_ERRORS:
2449*b077aed3SPierre Pronchery             opt_unprotected_errors = 1;
2450*b077aed3SPierre Pronchery             break;
2451*b077aed3SPierre Pronchery         case OPT_EXTRACERTSOUT:
2452*b077aed3SPierre Pronchery             opt_extracertsout = opt_str();
2453*b077aed3SPierre Pronchery             break;
2454*b077aed3SPierre Pronchery         case OPT_CACERTSOUT:
2455*b077aed3SPierre Pronchery             opt_cacertsout = opt_str();
2456*b077aed3SPierre Pronchery             break;
2457*b077aed3SPierre Pronchery 
2458*b077aed3SPierre Pronchery         case OPT_V_CASES:
2459*b077aed3SPierre Pronchery             if (!opt_verify(o, vpm))
2460*b077aed3SPierre Pronchery                 goto opthelp;
2461*b077aed3SPierre Pronchery             break;
2462*b077aed3SPierre Pronchery         case OPT_CMD:
2463*b077aed3SPierre Pronchery             opt_cmd_s = opt_str();
2464*b077aed3SPierre Pronchery             break;
2465*b077aed3SPierre Pronchery         case OPT_INFOTYPE:
2466*b077aed3SPierre Pronchery             opt_infotype_s = opt_str();
2467*b077aed3SPierre Pronchery             break;
2468*b077aed3SPierre Pronchery         case OPT_GENINFO:
2469*b077aed3SPierre Pronchery             opt_geninfo = opt_str();
2470*b077aed3SPierre Pronchery             break;
2471*b077aed3SPierre Pronchery 
2472*b077aed3SPierre Pronchery         case OPT_NEWKEY:
2473*b077aed3SPierre Pronchery             opt_newkey = opt_str();
2474*b077aed3SPierre Pronchery             break;
2475*b077aed3SPierre Pronchery         case OPT_NEWKEYPASS:
2476*b077aed3SPierre Pronchery             opt_newkeypass = opt_str();
2477*b077aed3SPierre Pronchery             break;
2478*b077aed3SPierre Pronchery         case OPT_SUBJECT:
2479*b077aed3SPierre Pronchery             opt_subject = opt_str();
2480*b077aed3SPierre Pronchery             break;
2481*b077aed3SPierre Pronchery         case OPT_ISSUER:
2482*b077aed3SPierre Pronchery             opt_issuer = opt_str();
2483*b077aed3SPierre Pronchery             break;
2484*b077aed3SPierre Pronchery         case OPT_DAYS:
2485*b077aed3SPierre Pronchery             opt_days = opt_int_arg();
2486*b077aed3SPierre Pronchery             break;
2487*b077aed3SPierre Pronchery         case OPT_REQEXTS:
2488*b077aed3SPierre Pronchery             opt_reqexts = opt_str();
2489*b077aed3SPierre Pronchery             break;
2490*b077aed3SPierre Pronchery         case OPT_SANS:
2491*b077aed3SPierre Pronchery             opt_sans = opt_str();
2492*b077aed3SPierre Pronchery             break;
2493*b077aed3SPierre Pronchery         case OPT_SAN_NODEFAULT:
2494*b077aed3SPierre Pronchery             opt_san_nodefault = 1;
2495*b077aed3SPierre Pronchery             break;
2496*b077aed3SPierre Pronchery         case OPT_POLICIES:
2497*b077aed3SPierre Pronchery             opt_policies = opt_str();
2498*b077aed3SPierre Pronchery             break;
2499*b077aed3SPierre Pronchery         case OPT_POLICY_OIDS:
2500*b077aed3SPierre Pronchery             opt_policy_oids = opt_str();
2501*b077aed3SPierre Pronchery             break;
2502*b077aed3SPierre Pronchery         case OPT_POLICY_OIDS_CRITICAL:
2503*b077aed3SPierre Pronchery             opt_policy_oids_critical = 1;
2504*b077aed3SPierre Pronchery             break;
2505*b077aed3SPierre Pronchery         case OPT_POPO:
2506*b077aed3SPierre Pronchery             opt_popo = opt_int_arg();
2507*b077aed3SPierre Pronchery             if (opt_popo < OSSL_CRMF_POPO_NONE
2508*b077aed3SPierre Pronchery                     || opt_popo > OSSL_CRMF_POPO_KEYENC) {
2509*b077aed3SPierre Pronchery                 CMP_err("invalid popo spec. Valid values are -1 .. 2");
2510*b077aed3SPierre Pronchery                 goto opthelp;
2511*b077aed3SPierre Pronchery             }
2512*b077aed3SPierre Pronchery             break;
2513*b077aed3SPierre Pronchery         case OPT_CSR:
2514*b077aed3SPierre Pronchery             opt_csr = opt_arg();
2515*b077aed3SPierre Pronchery             break;
2516*b077aed3SPierre Pronchery         case OPT_OUT_TRUSTED:
2517*b077aed3SPierre Pronchery             opt_out_trusted = opt_str();
2518*b077aed3SPierre Pronchery             break;
2519*b077aed3SPierre Pronchery         case OPT_IMPLICIT_CONFIRM:
2520*b077aed3SPierre Pronchery             opt_implicit_confirm = 1;
2521*b077aed3SPierre Pronchery             break;
2522*b077aed3SPierre Pronchery         case OPT_DISABLE_CONFIRM:
2523*b077aed3SPierre Pronchery             opt_disable_confirm = 1;
2524*b077aed3SPierre Pronchery             break;
2525*b077aed3SPierre Pronchery         case OPT_CERTOUT:
2526*b077aed3SPierre Pronchery             opt_certout = opt_str();
2527*b077aed3SPierre Pronchery             break;
2528*b077aed3SPierre Pronchery         case OPT_CHAINOUT:
2529*b077aed3SPierre Pronchery             opt_chainout = opt_str();
2530*b077aed3SPierre Pronchery             break;
2531*b077aed3SPierre Pronchery         case OPT_OLDCERT:
2532*b077aed3SPierre Pronchery             opt_oldcert = opt_str();
2533*b077aed3SPierre Pronchery             break;
2534*b077aed3SPierre Pronchery         case OPT_REVREASON:
2535*b077aed3SPierre Pronchery             opt_revreason = opt_int_arg();
2536*b077aed3SPierre Pronchery                 if (opt_revreason < CRL_REASON_NONE
2537*b077aed3SPierre Pronchery                     || opt_revreason > CRL_REASON_AA_COMPROMISE
2538*b077aed3SPierre Pronchery                     || opt_revreason == 7) {
2539*b077aed3SPierre Pronchery                 CMP_err("invalid revreason. Valid values are -1 .. 6, 8 .. 10");
2540*b077aed3SPierre Pronchery                 goto opthelp;
2541*b077aed3SPierre Pronchery             }
2542*b077aed3SPierre Pronchery             break;
2543*b077aed3SPierre Pronchery         case OPT_CERTFORM:
2544*b077aed3SPierre Pronchery             opt_certform_s = opt_str();
2545*b077aed3SPierre Pronchery             break;
2546*b077aed3SPierre Pronchery         case OPT_KEYFORM:
2547*b077aed3SPierre Pronchery             opt_keyform_s = opt_str();
2548*b077aed3SPierre Pronchery             break;
2549*b077aed3SPierre Pronchery         case OPT_OTHERPASS:
2550*b077aed3SPierre Pronchery             opt_otherpass = opt_str();
2551*b077aed3SPierre Pronchery             break;
2552*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
2553*b077aed3SPierre Pronchery         case OPT_ENGINE:
2554*b077aed3SPierre Pronchery             opt_engine = opt_str();
2555*b077aed3SPierre Pronchery             break;
2556*b077aed3SPierre Pronchery #endif
2557*b077aed3SPierre Pronchery         case OPT_PROV_CASES:
2558*b077aed3SPierre Pronchery             if (!opt_provider(o))
2559*b077aed3SPierre Pronchery                 goto opthelp;
2560*b077aed3SPierre Pronchery             break;
2561*b077aed3SPierre Pronchery         case OPT_R_CASES:
2562*b077aed3SPierre Pronchery             if (!opt_rand(o))
2563*b077aed3SPierre Pronchery                 goto opthelp;
2564*b077aed3SPierre Pronchery             break;
2565*b077aed3SPierre Pronchery 
2566*b077aed3SPierre Pronchery         case OPT_BATCH:
2567*b077aed3SPierre Pronchery             opt_batch = 1;
2568*b077aed3SPierre Pronchery             break;
2569*b077aed3SPierre Pronchery         case OPT_REPEAT:
2570*b077aed3SPierre Pronchery             opt_repeat = opt_int_arg();
2571*b077aed3SPierre Pronchery             break;
2572*b077aed3SPierre Pronchery         case OPT_REQIN:
2573*b077aed3SPierre Pronchery             opt_reqin = opt_str();
2574*b077aed3SPierre Pronchery             break;
2575*b077aed3SPierre Pronchery         case OPT_REQIN_NEW_TID:
2576*b077aed3SPierre Pronchery             opt_reqin_new_tid = 1;
2577*b077aed3SPierre Pronchery             break;
2578*b077aed3SPierre Pronchery         case OPT_REQOUT:
2579*b077aed3SPierre Pronchery             opt_reqout = opt_str();
2580*b077aed3SPierre Pronchery             break;
2581*b077aed3SPierre Pronchery         case OPT_RSPIN:
2582*b077aed3SPierre Pronchery             opt_rspin = opt_str();
2583*b077aed3SPierre Pronchery             break;
2584*b077aed3SPierre Pronchery         case OPT_RSPOUT:
2585*b077aed3SPierre Pronchery             opt_rspout = opt_str();
2586*b077aed3SPierre Pronchery             break;
2587*b077aed3SPierre Pronchery         case OPT_USE_MOCK_SRV:
2588*b077aed3SPierre Pronchery             opt_use_mock_srv = 1;
2589*b077aed3SPierre Pronchery             break;
2590*b077aed3SPierre Pronchery 
2591*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
2592*b077aed3SPierre Pronchery         case OPT_PORT:
2593*b077aed3SPierre Pronchery             opt_port = opt_str();
2594*b077aed3SPierre Pronchery             break;
2595*b077aed3SPierre Pronchery         case OPT_MAX_MSGS:
2596*b077aed3SPierre Pronchery             opt_max_msgs = opt_int_arg();
2597*b077aed3SPierre Pronchery             break;
2598*b077aed3SPierre Pronchery #endif
2599*b077aed3SPierre Pronchery         case OPT_SRV_REF:
2600*b077aed3SPierre Pronchery             opt_srv_ref = opt_str();
2601*b077aed3SPierre Pronchery             break;
2602*b077aed3SPierre Pronchery         case OPT_SRV_SECRET:
2603*b077aed3SPierre Pronchery             opt_srv_secret = opt_str();
2604*b077aed3SPierre Pronchery             break;
2605*b077aed3SPierre Pronchery         case OPT_SRV_CERT:
2606*b077aed3SPierre Pronchery             opt_srv_cert = opt_str();
2607*b077aed3SPierre Pronchery             break;
2608*b077aed3SPierre Pronchery         case OPT_SRV_KEY:
2609*b077aed3SPierre Pronchery             opt_srv_key = opt_str();
2610*b077aed3SPierre Pronchery             break;
2611*b077aed3SPierre Pronchery         case OPT_SRV_KEYPASS:
2612*b077aed3SPierre Pronchery             opt_srv_keypass = opt_str();
2613*b077aed3SPierre Pronchery             break;
2614*b077aed3SPierre Pronchery         case OPT_SRV_TRUSTED:
2615*b077aed3SPierre Pronchery             opt_srv_trusted = opt_str();
2616*b077aed3SPierre Pronchery             break;
2617*b077aed3SPierre Pronchery         case OPT_SRV_UNTRUSTED:
2618*b077aed3SPierre Pronchery             opt_srv_untrusted = opt_str();
2619*b077aed3SPierre Pronchery             break;
2620*b077aed3SPierre Pronchery         case OPT_RSP_CERT:
2621*b077aed3SPierre Pronchery             opt_rsp_cert = opt_str();
2622*b077aed3SPierre Pronchery             break;
2623*b077aed3SPierre Pronchery         case OPT_RSP_EXTRACERTS:
2624*b077aed3SPierre Pronchery             opt_rsp_extracerts = opt_str();
2625*b077aed3SPierre Pronchery             break;
2626*b077aed3SPierre Pronchery         case OPT_RSP_CAPUBS:
2627*b077aed3SPierre Pronchery             opt_rsp_capubs = opt_str();
2628*b077aed3SPierre Pronchery             break;
2629*b077aed3SPierre Pronchery         case OPT_POLL_COUNT:
2630*b077aed3SPierre Pronchery             opt_poll_count = opt_int_arg();
2631*b077aed3SPierre Pronchery             break;
2632*b077aed3SPierre Pronchery         case OPT_CHECK_AFTER:
2633*b077aed3SPierre Pronchery             opt_check_after = opt_int_arg();
2634*b077aed3SPierre Pronchery             break;
2635*b077aed3SPierre Pronchery         case OPT_GRANT_IMPLICITCONF:
2636*b077aed3SPierre Pronchery             opt_grant_implicitconf = 1;
2637*b077aed3SPierre Pronchery             break;
2638*b077aed3SPierre Pronchery         case OPT_PKISTATUS:
2639*b077aed3SPierre Pronchery             opt_pkistatus = opt_int_arg();
2640*b077aed3SPierre Pronchery             break;
2641*b077aed3SPierre Pronchery         case OPT_FAILURE:
2642*b077aed3SPierre Pronchery             opt_failure = opt_int_arg();
2643*b077aed3SPierre Pronchery             break;
2644*b077aed3SPierre Pronchery         case OPT_FAILUREBITS:
2645*b077aed3SPierre Pronchery             opt_failurebits = opt_int_arg();
2646*b077aed3SPierre Pronchery             break;
2647*b077aed3SPierre Pronchery         case OPT_STATUSSTRING:
2648*b077aed3SPierre Pronchery             opt_statusstring = opt_str();
2649*b077aed3SPierre Pronchery             break;
2650*b077aed3SPierre Pronchery         case OPT_SEND_ERROR:
2651*b077aed3SPierre Pronchery             opt_send_error = 1;
2652*b077aed3SPierre Pronchery             break;
2653*b077aed3SPierre Pronchery         case OPT_SEND_UNPROTECTED:
2654*b077aed3SPierre Pronchery             opt_send_unprotected = 1;
2655*b077aed3SPierre Pronchery             break;
2656*b077aed3SPierre Pronchery         case OPT_SEND_UNPROT_ERR:
2657*b077aed3SPierre Pronchery             opt_send_unprot_err = 1;
2658*b077aed3SPierre Pronchery             break;
2659*b077aed3SPierre Pronchery         case OPT_ACCEPT_UNPROTECTED:
2660*b077aed3SPierre Pronchery             opt_accept_unprotected = 1;
2661*b077aed3SPierre Pronchery             break;
2662*b077aed3SPierre Pronchery         case OPT_ACCEPT_UNPROT_ERR:
2663*b077aed3SPierre Pronchery             opt_accept_unprot_err = 1;
2664*b077aed3SPierre Pronchery             break;
2665*b077aed3SPierre Pronchery         case OPT_ACCEPT_RAVERIFIED:
2666*b077aed3SPierre Pronchery             opt_accept_raverified = 1;
2667*b077aed3SPierre Pronchery             break;
2668*b077aed3SPierre Pronchery         }
2669*b077aed3SPierre Pronchery     }
2670*b077aed3SPierre Pronchery 
2671*b077aed3SPierre Pronchery     /* No extra args. */
2672*b077aed3SPierre Pronchery     argc = opt_num_rest();
2673*b077aed3SPierre Pronchery     argv = opt_rest();
2674*b077aed3SPierre Pronchery     if (argc != 0)
2675*b077aed3SPierre Pronchery         goto opthelp;
2676*b077aed3SPierre Pronchery     return 1;
2677*b077aed3SPierre Pronchery }
2678*b077aed3SPierre Pronchery 
2679*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
2680*b077aed3SPierre Pronchery static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx) {
2681*b077aed3SPierre Pronchery     BIO *acbio;
2682*b077aed3SPierre Pronchery     BIO *cbio = NULL;
2683*b077aed3SPierre Pronchery     int keep_alive = 0;
2684*b077aed3SPierre Pronchery     int msgs = 0;
2685*b077aed3SPierre Pronchery     int retry = 1;
2686*b077aed3SPierre Pronchery     int ret = 1;
2687*b077aed3SPierre Pronchery 
2688*b077aed3SPierre Pronchery     if ((acbio = http_server_init_bio(prog, opt_port)) == NULL)
2689*b077aed3SPierre Pronchery         return 0;
2690*b077aed3SPierre Pronchery     while (opt_max_msgs <= 0 || msgs < opt_max_msgs) {
2691*b077aed3SPierre Pronchery         char *path = NULL;
2692*b077aed3SPierre Pronchery         OSSL_CMP_MSG *req = NULL;
2693*b077aed3SPierre Pronchery         OSSL_CMP_MSG *resp = NULL;
2694*b077aed3SPierre Pronchery 
2695*b077aed3SPierre Pronchery         ret = http_server_get_asn1_req(ASN1_ITEM_rptr(OSSL_CMP_MSG),
2696*b077aed3SPierre Pronchery                                        (ASN1_VALUE **)&req, &path,
2697*b077aed3SPierre Pronchery                                        &cbio, acbio, &keep_alive,
2698*b077aed3SPierre Pronchery                                        prog, opt_port, 0, 0);
2699*b077aed3SPierre Pronchery         if (ret == 0) { /* no request yet */
2700*b077aed3SPierre Pronchery             if (retry) {
2701*b077aed3SPierre Pronchery                 ossl_sleep(1000);
2702*b077aed3SPierre Pronchery                 retry = 0;
2703*b077aed3SPierre Pronchery                 continue;
2704*b077aed3SPierre Pronchery             }
2705*b077aed3SPierre Pronchery             ret = 0;
2706*b077aed3SPierre Pronchery             goto next;
2707*b077aed3SPierre Pronchery         }
2708*b077aed3SPierre Pronchery         if (ret++ == -1) /* fatal error */
2709*b077aed3SPierre Pronchery             break;
2710*b077aed3SPierre Pronchery 
2711*b077aed3SPierre Pronchery         ret = 0;
2712*b077aed3SPierre Pronchery         msgs++;
2713*b077aed3SPierre Pronchery         if (req != NULL) {
2714*b077aed3SPierre Pronchery             if (strcmp(path, "") != 0 && strcmp(path, "pkix/") != 0) {
2715*b077aed3SPierre Pronchery                 (void)http_server_send_status(cbio, 404, "Not Found");
2716*b077aed3SPierre Pronchery                 CMP_err1("expecting empty path or 'pkix/' but got '%s'",
2717*b077aed3SPierre Pronchery                          path);
2718*b077aed3SPierre Pronchery                 OPENSSL_free(path);
2719*b077aed3SPierre Pronchery                 OSSL_CMP_MSG_free(req);
2720*b077aed3SPierre Pronchery                 goto next;
2721*b077aed3SPierre Pronchery             }
2722*b077aed3SPierre Pronchery             OPENSSL_free(path);
2723*b077aed3SPierre Pronchery             resp = OSSL_CMP_CTX_server_perform(cmp_ctx, req);
2724*b077aed3SPierre Pronchery             OSSL_CMP_MSG_free(req);
2725*b077aed3SPierre Pronchery             if (resp == NULL) {
2726*b077aed3SPierre Pronchery                 (void)http_server_send_status(cbio,
2727*b077aed3SPierre Pronchery                                               500, "Internal Server Error");
2728*b077aed3SPierre Pronchery                 break; /* treated as fatal error */
2729*b077aed3SPierre Pronchery             }
2730*b077aed3SPierre Pronchery             ret = http_server_send_asn1_resp(cbio, keep_alive,
2731*b077aed3SPierre Pronchery                                              "application/pkixcmp",
2732*b077aed3SPierre Pronchery                                              ASN1_ITEM_rptr(OSSL_CMP_MSG),
2733*b077aed3SPierre Pronchery                                              (const ASN1_VALUE *)resp);
2734*b077aed3SPierre Pronchery             OSSL_CMP_MSG_free(resp);
2735*b077aed3SPierre Pronchery             if (!ret)
2736*b077aed3SPierre Pronchery                 break; /* treated as fatal error */
2737*b077aed3SPierre Pronchery         }
2738*b077aed3SPierre Pronchery     next:
2739*b077aed3SPierre Pronchery         if (!ret) { /* on transmission error, cancel CMP transaction */
2740*b077aed3SPierre Pronchery             (void)OSSL_CMP_CTX_set1_transactionID(srv_cmp_ctx, NULL);
2741*b077aed3SPierre Pronchery             (void)OSSL_CMP_CTX_set1_senderNonce(srv_cmp_ctx, NULL);
2742*b077aed3SPierre Pronchery         }
2743*b077aed3SPierre Pronchery         if (!ret || !keep_alive
2744*b077aed3SPierre Pronchery             || OSSL_CMP_CTX_get_status(srv_cmp_ctx) != OSSL_CMP_PKISTATUS_trans
2745*b077aed3SPierre Pronchery             /* transaction closed by OSSL_CMP_CTX_server_perform() */) {
2746*b077aed3SPierre Pronchery             BIO_free_all(cbio);
2747*b077aed3SPierre Pronchery             cbio = NULL;
2748*b077aed3SPierre Pronchery         }
2749*b077aed3SPierre Pronchery     }
2750*b077aed3SPierre Pronchery 
2751*b077aed3SPierre Pronchery     BIO_free_all(cbio);
2752*b077aed3SPierre Pronchery     BIO_free_all(acbio);
2753*b077aed3SPierre Pronchery     return ret;
2754*b077aed3SPierre Pronchery }
2755*b077aed3SPierre Pronchery #endif
2756*b077aed3SPierre Pronchery 
2757*b077aed3SPierre Pronchery static void print_status(void)
2758*b077aed3SPierre Pronchery {
2759*b077aed3SPierre Pronchery     /* print PKIStatusInfo */
2760*b077aed3SPierre Pronchery     int status = OSSL_CMP_CTX_get_status(cmp_ctx);
2761*b077aed3SPierre Pronchery     char *buf = app_malloc(OSSL_CMP_PKISI_BUFLEN, "PKIStatusInfo buf");
2762*b077aed3SPierre Pronchery     const char *string =
2763*b077aed3SPierre Pronchery         OSSL_CMP_CTX_snprint_PKIStatus(cmp_ctx, buf, OSSL_CMP_PKISI_BUFLEN);
2764*b077aed3SPierre Pronchery     const char *from = "", *server = "";
2765*b077aed3SPierre Pronchery 
2766*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
2767*b077aed3SPierre Pronchery     if (opt_server != NULL) {
2768*b077aed3SPierre Pronchery         from = " from ";
2769*b077aed3SPierre Pronchery         server = opt_server;
2770*b077aed3SPierre Pronchery     }
2771*b077aed3SPierre Pronchery #endif
2772*b077aed3SPierre Pronchery     CMP_print(bio_err,
2773*b077aed3SPierre Pronchery               status == OSSL_CMP_PKISTATUS_accepted
2774*b077aed3SPierre Pronchery               ? OSSL_CMP_LOG_INFO :
2775*b077aed3SPierre Pronchery               status == OSSL_CMP_PKISTATUS_rejection
2776*b077aed3SPierre Pronchery               || status == OSSL_CMP_PKISTATUS_waiting
2777*b077aed3SPierre Pronchery               ? OSSL_CMP_LOG_ERR : OSSL_CMP_LOG_WARNING,
2778*b077aed3SPierre Pronchery               status == OSSL_CMP_PKISTATUS_accepted ? "info" :
2779*b077aed3SPierre Pronchery               status == OSSL_CMP_PKISTATUS_rejection ? "server error" :
2780*b077aed3SPierre Pronchery               status == OSSL_CMP_PKISTATUS_waiting ? "internal error"
2781*b077aed3SPierre Pronchery               : "warning", "received%s%s %s", from, server,
2782*b077aed3SPierre Pronchery               string != NULL ? string : "<unknown PKIStatus>");
2783*b077aed3SPierre Pronchery     OPENSSL_free(buf);
2784*b077aed3SPierre Pronchery }
2785*b077aed3SPierre Pronchery 
2786*b077aed3SPierre Pronchery int cmp_main(int argc, char **argv)
2787*b077aed3SPierre Pronchery {
2788*b077aed3SPierre Pronchery     char *configfile = NULL;
2789*b077aed3SPierre Pronchery     int i;
2790*b077aed3SPierre Pronchery     X509 *newcert = NULL;
2791*b077aed3SPierre Pronchery     ENGINE *engine = NULL;
2792*b077aed3SPierre Pronchery     OSSL_CMP_CTX *srv_cmp_ctx = NULL;
2793*b077aed3SPierre Pronchery     int ret = 0; /* default: failure */
2794*b077aed3SPierre Pronchery 
2795*b077aed3SPierre Pronchery     prog = opt_appname(argv[0]);
2796*b077aed3SPierre Pronchery     if (argc <= 1) {
2797*b077aed3SPierre Pronchery         opt_help(cmp_options);
2798*b077aed3SPierre Pronchery         goto err;
2799*b077aed3SPierre Pronchery     }
2800*b077aed3SPierre Pronchery 
2801*b077aed3SPierre Pronchery     /*
2802*b077aed3SPierre Pronchery      * handle options -config, -section, and -verbosity upfront
2803*b077aed3SPierre Pronchery      * to take effect for other options
2804*b077aed3SPierre Pronchery      */
2805*b077aed3SPierre Pronchery     for (i = 1; i < argc - 1; i++) {
2806*b077aed3SPierre Pronchery         if (*argv[i] == '-') {
2807*b077aed3SPierre Pronchery             if (!strcmp(argv[i] + 1, cmp_options[OPT_CONFIG - OPT_HELP].name))
2808*b077aed3SPierre Pronchery                 opt_config = argv[++i];
2809*b077aed3SPierre Pronchery             else if (!strcmp(argv[i] + 1,
2810*b077aed3SPierre Pronchery                              cmp_options[OPT_SECTION - OPT_HELP].name))
2811*b077aed3SPierre Pronchery                 opt_section = argv[++i];
2812*b077aed3SPierre Pronchery             else if (strcmp(argv[i] + 1,
2813*b077aed3SPierre Pronchery                             cmp_options[OPT_VERBOSITY - OPT_HELP].name) == 0
2814*b077aed3SPierre Pronchery                      && !set_verbosity(atoi(argv[++i])))
2815*b077aed3SPierre Pronchery                 goto err;
2816*b077aed3SPierre Pronchery         }
2817*b077aed3SPierre Pronchery     }
2818*b077aed3SPierre Pronchery     if (opt_section[0] == '\0') /* empty string */
2819*b077aed3SPierre Pronchery         opt_section = DEFAULT_SECTION;
2820*b077aed3SPierre Pronchery 
2821*b077aed3SPierre Pronchery     vpm = X509_VERIFY_PARAM_new();
2822*b077aed3SPierre Pronchery     if (vpm == NULL) {
2823*b077aed3SPierre Pronchery         CMP_err("out of memory");
2824*b077aed3SPierre Pronchery         goto err;
2825*b077aed3SPierre Pronchery     }
2826*b077aed3SPierre Pronchery 
2827*b077aed3SPierre Pronchery     /* read default values for options from config file */
2828*b077aed3SPierre Pronchery     configfile = opt_config != NULL ? opt_config : default_config_file;
2829*b077aed3SPierre Pronchery     if (configfile != NULL && configfile[0] != '\0' /* non-empty string */
2830*b077aed3SPierre Pronchery             && (configfile != default_config_file || access(configfile, F_OK) != -1)) {
2831*b077aed3SPierre Pronchery         CMP_info2("using section(s) '%s' of OpenSSL configuration file '%s'",
2832*b077aed3SPierre Pronchery                   opt_section, configfile);
2833*b077aed3SPierre Pronchery         conf = app_load_config(configfile);
2834*b077aed3SPierre Pronchery         if (conf == NULL) {
2835*b077aed3SPierre Pronchery             goto err;
2836*b077aed3SPierre Pronchery         } else {
2837*b077aed3SPierre Pronchery             if (strcmp(opt_section, CMP_SECTION) == 0) { /* default */
2838*b077aed3SPierre Pronchery                 if (!NCONF_get_section(conf, opt_section))
2839*b077aed3SPierre Pronchery                     CMP_info2("no [%s] section found in config file '%s';"
2840*b077aed3SPierre Pronchery                               " will thus use just [default] and unnamed section if present",
2841*b077aed3SPierre Pronchery                               opt_section, configfile);
2842*b077aed3SPierre Pronchery             } else {
2843*b077aed3SPierre Pronchery                 const char *end = opt_section + strlen(opt_section);
2844*b077aed3SPierre Pronchery                 while ((end = prev_item(opt_section, end)) != NULL) {
2845*b077aed3SPierre Pronchery                     if (!NCONF_get_section(conf, opt_item)) {
2846*b077aed3SPierre Pronchery                         CMP_err2("no [%s] section found in config file '%s'",
2847*b077aed3SPierre Pronchery                                  opt_item, configfile);
2848*b077aed3SPierre Pronchery                         goto err;
2849*b077aed3SPierre Pronchery                     }
2850*b077aed3SPierre Pronchery                 }
2851*b077aed3SPierre Pronchery             }
2852*b077aed3SPierre Pronchery             ret = read_config();
2853*b077aed3SPierre Pronchery             if (!set_verbosity(opt_verbosity)) /* just for checking range */
2854*b077aed3SPierre Pronchery                 ret = -1;
2855*b077aed3SPierre Pronchery             if (ret <= 0) {
2856*b077aed3SPierre Pronchery                 if (ret == -1)
2857*b077aed3SPierre Pronchery                     BIO_printf(bio_err, "Use -help for summary.\n");
2858*b077aed3SPierre Pronchery                 goto err;
2859*b077aed3SPierre Pronchery             }
2860*b077aed3SPierre Pronchery         }
2861*b077aed3SPierre Pronchery     }
2862*b077aed3SPierre Pronchery     (void)BIO_flush(bio_err); /* prevent interference with opt_help() */
2863*b077aed3SPierre Pronchery 
2864*b077aed3SPierre Pronchery     ret = get_opts(argc, argv);
2865*b077aed3SPierre Pronchery     if (ret <= 0)
2866*b077aed3SPierre Pronchery         goto err;
2867*b077aed3SPierre Pronchery     ret = 0;
2868*b077aed3SPierre Pronchery     if (!app_RAND_load())
2869*b077aed3SPierre Pronchery         goto err;
2870*b077aed3SPierre Pronchery 
2871*b077aed3SPierre Pronchery     if (opt_batch)
2872*b077aed3SPierre Pronchery         set_base_ui_method(UI_null());
2873*b077aed3SPierre Pronchery 
2874*b077aed3SPierre Pronchery     if (opt_engine != NULL) {
2875*b077aed3SPierre Pronchery         engine = setup_engine_methods(opt_engine, 0 /* not: ENGINE_METHOD_ALL */, 0);
2876*b077aed3SPierre Pronchery         if (engine == NULL) {
2877*b077aed3SPierre Pronchery             CMP_err1("cannot load engine %s", opt_engine);
2878*b077aed3SPierre Pronchery             goto err;
2879*b077aed3SPierre Pronchery         }
2880*b077aed3SPierre Pronchery     }
2881*b077aed3SPierre Pronchery 
2882*b077aed3SPierre Pronchery     cmp_ctx = OSSL_CMP_CTX_new(app_get0_libctx(), app_get0_propq());
2883*b077aed3SPierre Pronchery     if (cmp_ctx == NULL)
2884*b077aed3SPierre Pronchery         goto err;
2885*b077aed3SPierre Pronchery     OSSL_CMP_CTX_set_log_verbosity(cmp_ctx, opt_verbosity);
2886*b077aed3SPierre Pronchery     if (!OSSL_CMP_CTX_set_log_cb(cmp_ctx, print_to_bio_out)) {
2887*b077aed3SPierre Pronchery         CMP_err1("cannot set up error reporting and logging for %s", prog);
2888*b077aed3SPierre Pronchery         goto err;
2889*b077aed3SPierre Pronchery     }
2890*b077aed3SPierre Pronchery 
2891*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
2892*b077aed3SPierre Pronchery     if ((opt_tls_cert != NULL || opt_tls_key != NULL
2893*b077aed3SPierre Pronchery          || opt_tls_keypass != NULL || opt_tls_extra != NULL
2894*b077aed3SPierre Pronchery          || opt_tls_trusted != NULL || opt_tls_host != NULL)
2895*b077aed3SPierre Pronchery             && !opt_tls_used)
2896*b077aed3SPierre Pronchery         CMP_warn("Ingnoring TLS options(s) since -tls_used is not given");
2897*b077aed3SPierre Pronchery     if (opt_port != NULL) {
2898*b077aed3SPierre Pronchery         if (opt_tls_used) {
2899*b077aed3SPierre Pronchery             CMP_err("-tls_used option not supported with -port option");
2900*b077aed3SPierre Pronchery             goto err;
2901*b077aed3SPierre Pronchery         }
2902*b077aed3SPierre Pronchery         if (opt_server != NULL || opt_use_mock_srv) {
2903*b077aed3SPierre Pronchery             CMP_err("The -port option excludes -server and -use_mock_srv");
2904*b077aed3SPierre Pronchery             goto err;
2905*b077aed3SPierre Pronchery         }
2906*b077aed3SPierre Pronchery         if (opt_reqin != NULL || opt_reqout != NULL) {
2907*b077aed3SPierre Pronchery             CMP_err("The -port option does not support -reqin and -reqout");
2908*b077aed3SPierre Pronchery             goto err;
2909*b077aed3SPierre Pronchery         }
2910*b077aed3SPierre Pronchery         if (opt_rspin != NULL || opt_rspout != NULL) {
2911*b077aed3SPierre Pronchery             CMP_err("The -port option does not support -rspin and -rspout");
2912*b077aed3SPierre Pronchery             goto err;
2913*b077aed3SPierre Pronchery         }
2914*b077aed3SPierre Pronchery     }
2915*b077aed3SPierre Pronchery     if (opt_server != NULL && opt_use_mock_srv) {
2916*b077aed3SPierre Pronchery         CMP_err("cannot use both -server and -use_mock_srv options");
2917*b077aed3SPierre Pronchery         goto err;
2918*b077aed3SPierre Pronchery     }
2919*b077aed3SPierre Pronchery #endif
2920*b077aed3SPierre Pronchery 
2921*b077aed3SPierre Pronchery     if (opt_use_mock_srv
2922*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
2923*b077aed3SPierre Pronchery         || opt_port != NULL
2924*b077aed3SPierre Pronchery #endif
2925*b077aed3SPierre Pronchery         ) {
2926*b077aed3SPierre Pronchery         OSSL_CMP_SRV_CTX *srv_ctx;
2927*b077aed3SPierre Pronchery 
2928*b077aed3SPierre Pronchery         if ((srv_ctx = setup_srv_ctx(engine)) == NULL)
2929*b077aed3SPierre Pronchery             goto err;
2930*b077aed3SPierre Pronchery         srv_cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx);
2931*b077aed3SPierre Pronchery         OSSL_CMP_CTX_set_transfer_cb_arg(cmp_ctx, srv_ctx);
2932*b077aed3SPierre Pronchery         if (!OSSL_CMP_CTX_set_log_cb(srv_cmp_ctx, print_to_bio_err)) {
2933*b077aed3SPierre Pronchery             CMP_err1("cannot set up error reporting and logging for %s", prog);
2934*b077aed3SPierre Pronchery             goto err;
2935*b077aed3SPierre Pronchery         }
2936*b077aed3SPierre Pronchery         OSSL_CMP_CTX_set_log_verbosity(srv_cmp_ctx, opt_verbosity);
2937*b077aed3SPierre Pronchery     }
2938*b077aed3SPierre Pronchery 
2939*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
2940*b077aed3SPierre Pronchery     if (opt_tls_used && (opt_use_mock_srv || opt_server == NULL)) {
2941*b077aed3SPierre Pronchery         CMP_warn("ignoring -tls_used option since -use_mock_srv is given or -server is not given");
2942*b077aed3SPierre Pronchery         opt_tls_used = 0;
2943*b077aed3SPierre Pronchery     }
2944*b077aed3SPierre Pronchery 
2945*b077aed3SPierre Pronchery     if (opt_port != NULL) { /* act as very basic CMP HTTP server */
2946*b077aed3SPierre Pronchery         ret = cmp_server(srv_cmp_ctx);
2947*b077aed3SPierre Pronchery         goto err;
2948*b077aed3SPierre Pronchery     }
2949*b077aed3SPierre Pronchery 
2950*b077aed3SPierre Pronchery     /* act as CMP client, possibly using internal mock server */
2951*b077aed3SPierre Pronchery 
2952*b077aed3SPierre Pronchery     if (opt_rspin != NULL) {
2953*b077aed3SPierre Pronchery         if (opt_server != NULL)
2954*b077aed3SPierre Pronchery             CMP_warn("-server option is not used if enough filenames given for -rspin");
2955*b077aed3SPierre Pronchery         if (opt_use_mock_srv)
2956*b077aed3SPierre Pronchery             CMP_warn("-use_mock_srv option is not used if enough filenames given for -rspin");
2957*b077aed3SPierre Pronchery     }
2958*b077aed3SPierre Pronchery #endif
2959*b077aed3SPierre Pronchery 
2960*b077aed3SPierre Pronchery     if (!setup_client_ctx(cmp_ctx, engine)) {
2961*b077aed3SPierre Pronchery         CMP_err("cannot set up CMP context");
2962*b077aed3SPierre Pronchery         goto err;
2963*b077aed3SPierre Pronchery     }
2964*b077aed3SPierre Pronchery     for (i = 0; i < opt_repeat; i++) {
2965*b077aed3SPierre Pronchery         /* everything is ready, now connect and perform the command! */
2966*b077aed3SPierre Pronchery         switch (opt_cmd) {
2967*b077aed3SPierre Pronchery         case CMP_IR:
2968*b077aed3SPierre Pronchery             newcert = OSSL_CMP_exec_IR_ses(cmp_ctx);
2969*b077aed3SPierre Pronchery             if (newcert != NULL)
2970*b077aed3SPierre Pronchery                 ret = 1;
2971*b077aed3SPierre Pronchery             break;
2972*b077aed3SPierre Pronchery         case CMP_KUR:
2973*b077aed3SPierre Pronchery             newcert = OSSL_CMP_exec_KUR_ses(cmp_ctx);
2974*b077aed3SPierre Pronchery             if (newcert != NULL)
2975*b077aed3SPierre Pronchery                 ret = 1;
2976*b077aed3SPierre Pronchery             break;
2977*b077aed3SPierre Pronchery         case CMP_CR:
2978*b077aed3SPierre Pronchery             newcert = OSSL_CMP_exec_CR_ses(cmp_ctx);
2979*b077aed3SPierre Pronchery             if (newcert != NULL)
2980*b077aed3SPierre Pronchery                 ret = 1;
2981*b077aed3SPierre Pronchery             break;
2982*b077aed3SPierre Pronchery         case CMP_P10CR:
2983*b077aed3SPierre Pronchery             newcert = OSSL_CMP_exec_P10CR_ses(cmp_ctx);
2984*b077aed3SPierre Pronchery             if (newcert != NULL)
2985*b077aed3SPierre Pronchery                 ret = 1;
2986*b077aed3SPierre Pronchery             break;
2987*b077aed3SPierre Pronchery         case CMP_RR:
2988*b077aed3SPierre Pronchery             ret = OSSL_CMP_exec_RR_ses(cmp_ctx);
2989*b077aed3SPierre Pronchery             break;
2990*b077aed3SPierre Pronchery         case CMP_GENM:
2991*b077aed3SPierre Pronchery             {
2992*b077aed3SPierre Pronchery                 STACK_OF(OSSL_CMP_ITAV) *itavs;
2993*b077aed3SPierre Pronchery 
2994*b077aed3SPierre Pronchery                 if (opt_infotype != NID_undef) {
2995*b077aed3SPierre Pronchery                     OSSL_CMP_ITAV *itav =
2996*b077aed3SPierre Pronchery                         OSSL_CMP_ITAV_create(OBJ_nid2obj(opt_infotype), NULL);
2997*b077aed3SPierre Pronchery                     if (itav == NULL)
2998*b077aed3SPierre Pronchery                         goto err;
2999*b077aed3SPierre Pronchery                     OSSL_CMP_CTX_push0_genm_ITAV(cmp_ctx, itav);
3000*b077aed3SPierre Pronchery                 }
3001*b077aed3SPierre Pronchery 
3002*b077aed3SPierre Pronchery                 if ((itavs = OSSL_CMP_exec_GENM_ses(cmp_ctx)) != NULL) {
3003*b077aed3SPierre Pronchery                     print_itavs(itavs);
3004*b077aed3SPierre Pronchery                     sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
3005*b077aed3SPierre Pronchery                     ret = 1;
3006*b077aed3SPierre Pronchery                 }
3007*b077aed3SPierre Pronchery                 break;
3008*b077aed3SPierre Pronchery             }
3009*b077aed3SPierre Pronchery         default:
3010*b077aed3SPierre Pronchery             break;
3011*b077aed3SPierre Pronchery         }
3012*b077aed3SPierre Pronchery         if (OSSL_CMP_CTX_get_status(cmp_ctx) < OSSL_CMP_PKISTATUS_accepted)
3013*b077aed3SPierre Pronchery             goto err; /* we got no response, maybe even did not send request */
3014*b077aed3SPierre Pronchery 
3015*b077aed3SPierre Pronchery         print_status();
3016*b077aed3SPierre Pronchery         if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_extraCertsIn(cmp_ctx),
3017*b077aed3SPierre Pronchery                             opt_extracertsout, "extra") < 0)
3018*b077aed3SPierre Pronchery             ret = 0;
3019*b077aed3SPierre Pronchery         if (!ret)
3020*b077aed3SPierre Pronchery             goto err;
3021*b077aed3SPierre Pronchery         ret = 0;
3022*b077aed3SPierre Pronchery         if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_caPubs(cmp_ctx),
3023*b077aed3SPierre Pronchery                             opt_cacertsout, "CA") < 0)
3024*b077aed3SPierre Pronchery             goto err;
3025*b077aed3SPierre Pronchery         if (newcert != NULL) {
3026*b077aed3SPierre Pronchery             STACK_OF(X509) *certs = sk_X509_new_null();
3027*b077aed3SPierre Pronchery 
3028*b077aed3SPierre Pronchery             if (!X509_add_cert(certs, newcert, X509_ADD_FLAG_UP_REF)) {
3029*b077aed3SPierre Pronchery                 sk_X509_free(certs);
3030*b077aed3SPierre Pronchery                 goto err;
3031*b077aed3SPierre Pronchery             }
3032*b077aed3SPierre Pronchery             if (save_free_certs(cmp_ctx, certs, opt_certout, "enrolled") < 0)
3033*b077aed3SPierre Pronchery                 goto err;
3034*b077aed3SPierre Pronchery         }
3035*b077aed3SPierre Pronchery         if (save_free_certs(cmp_ctx, OSSL_CMP_CTX_get1_newChain(cmp_ctx),
3036*b077aed3SPierre Pronchery                             opt_chainout, "chain") < 0)
3037*b077aed3SPierre Pronchery             goto err;
3038*b077aed3SPierre Pronchery 
3039*b077aed3SPierre Pronchery         if (!OSSL_CMP_CTX_reinit(cmp_ctx))
3040*b077aed3SPierre Pronchery             goto err;
3041*b077aed3SPierre Pronchery     }
3042*b077aed3SPierre Pronchery     ret = 1;
3043*b077aed3SPierre Pronchery 
3044*b077aed3SPierre Pronchery  err:
3045*b077aed3SPierre Pronchery     /* in case we ended up here on error without proper cleaning */
3046*b077aed3SPierre Pronchery     cleanse(opt_keypass);
3047*b077aed3SPierre Pronchery     cleanse(opt_newkeypass);
3048*b077aed3SPierre Pronchery     cleanse(opt_otherpass);
3049*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
3050*b077aed3SPierre Pronchery     cleanse(opt_tls_keypass);
3051*b077aed3SPierre Pronchery #endif
3052*b077aed3SPierre Pronchery     cleanse(opt_secret);
3053*b077aed3SPierre Pronchery     cleanse(opt_srv_keypass);
3054*b077aed3SPierre Pronchery     cleanse(opt_srv_secret);
3055*b077aed3SPierre Pronchery 
3056*b077aed3SPierre Pronchery     if (ret != 1)
3057*b077aed3SPierre Pronchery         OSSL_CMP_CTX_print_errors(cmp_ctx);
3058*b077aed3SPierre Pronchery 
3059*b077aed3SPierre Pronchery     if (cmp_ctx != NULL) {
3060*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
3061*b077aed3SPierre Pronchery         APP_HTTP_TLS_INFO *info = OSSL_CMP_CTX_get_http_cb_arg(cmp_ctx);
3062*b077aed3SPierre Pronchery 
3063*b077aed3SPierre Pronchery #endif
3064*b077aed3SPierre Pronchery         ossl_cmp_mock_srv_free(OSSL_CMP_CTX_get_transfer_cb_arg(cmp_ctx));
3065*b077aed3SPierre Pronchery         X509_STORE_free(OSSL_CMP_CTX_get_certConf_cb_arg(cmp_ctx));
3066*b077aed3SPierre Pronchery         /* cannot free info already here, as it may be used indirectly by: */
3067*b077aed3SPierre Pronchery         OSSL_CMP_CTX_free(cmp_ctx);
3068*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_SOCK
3069*b077aed3SPierre Pronchery         if (info != NULL) {
3070*b077aed3SPierre Pronchery             OPENSSL_free((char *)info->server);
3071*b077aed3SPierre Pronchery             OPENSSL_free((char *)info->port);
3072*b077aed3SPierre Pronchery             APP_HTTP_TLS_INFO_free(info);
3073*b077aed3SPierre Pronchery         }
3074*b077aed3SPierre Pronchery #endif
3075*b077aed3SPierre Pronchery     }
3076*b077aed3SPierre Pronchery     X509_VERIFY_PARAM_free(vpm);
3077*b077aed3SPierre Pronchery     release_engine(engine);
3078*b077aed3SPierre Pronchery 
3079*b077aed3SPierre Pronchery     NCONF_free(conf); /* must not do as long as opt_... variables are used */
3080*b077aed3SPierre Pronchery     OSSL_CMP_log_close();
3081*b077aed3SPierre Pronchery 
3082*b077aed3SPierre Pronchery     return ret == 0 ? EXIT_FAILURE : EXIT_SUCCESS; /* ret == -1 for -help */
3083*b077aed3SPierre Pronchery }
3084