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