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