xref: /freebsd/crypto/openssl/apps/cmp.c (revision 10a428653ee7216475f1ddce3fb4cbf1200319f8)
1 /*
2  * Copyright 2007-2026 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2019
4  * Copyright Siemens AG 2015-2019
5  *
6  * Licensed under the Apache License 2.0 (the "License").  You may not use
7  * this file except in compliance with the License.  You can obtain a copy
8  * in the file LICENSE in the source distribution or at
9  * https://www.openssl.org/source/license.html
10  */
11 
12 /* This app is disabled when OPENSSL_NO_CMP is defined. */
13 #include "internal/e_os.h"
14 
15 #include <string.h>
16 #include <ctype.h>
17 
18 #include "apps.h"
19 #include "http_server.h"
20 #include "s_apps.h"
21 #include "progs.h"
22 
23 #include "cmp_mock_srv.h"
24 
25 /* tweaks needed due to missing unistd.h on Windows */
26 #if defined(_WIN32) && !defined(__BORLANDC__)
27 #define access _access
28 #endif
29 #ifndef F_OK
30 #define F_OK 0
31 #endif
32 
33 #include <openssl/ui.h>
34 #include <openssl/pkcs12.h>
35 #include <openssl/ssl.h>
36 
37 /* explicit #includes not strictly needed since implied by the above: */
38 #include <stdlib.h>
39 #include <openssl/cmp.h>
40 #include <openssl/cmp_util.h>
41 #include <openssl/crmf.h>
42 #include <openssl/crypto.h>
43 #include <openssl/err.h>
44 #include <openssl/store.h>
45 #include <openssl/objects.h>
46 #include <openssl/x509.h>
47 
48 static char *prog;
49 static char *opt_config = NULL;
50 #define CMP_SECTION "cmp"
51 #define SECTION_NAME_MAX 40 /* max length of section name */
52 #define DEFAULT_SECTION "default"
53 static char *opt_section = CMP_SECTION;
54 static int opt_verbosity = OSSL_CMP_LOG_INFO;
55 
56 static int read_config(void);
57 
58 static CONF *conf = NULL; /* OpenSSL config file context structure */
59 static OSSL_CMP_CTX *cmp_ctx = NULL; /* the client-side CMP context */
60 
61 /* the type of cmp command we want to send */
62 typedef enum {
63     CMP_IR,
64     CMP_KUR,
65     CMP_CR,
66     CMP_P10CR,
67     CMP_RR,
68     CMP_GENM
69 } cmp_cmd_t;
70 
71 /* message transfer */
72 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
73 static char *opt_server = NULL;
74 static char *opt_proxy = NULL;
75 static char *opt_no_proxy = NULL;
76 #endif
77 static char *opt_recipient = NULL;
78 static char *opt_path = NULL;
79 static int opt_keep_alive = 1;
80 static int opt_msg_timeout = -1;
81 static int opt_total_timeout = -1;
82 
83 /* server authentication */
84 static char *opt_trusted = NULL;
85 static char *opt_untrusted = NULL;
86 static char *opt_srvcert = NULL;
87 static char *opt_expect_sender = NULL;
88 static int opt_ignore_keyusage = 0;
89 static int opt_unprotected_errors = 0;
90 static int opt_no_cache_extracerts = 0;
91 static char *opt_srvcertout = NULL;
92 static char *opt_extracertsout = NULL;
93 static char *opt_cacertsout = NULL;
94 static char *opt_oldwithold = NULL;
95 static char *opt_newwithnew = NULL;
96 static char *opt_newwithold = NULL;
97 static char *opt_oldwithnew = NULL;
98 static char *opt_crlcert = NULL;
99 static char *opt_oldcrl = NULL;
100 static char *opt_crlout = NULL;
101 static char *opt_template = NULL;
102 static char *opt_keyspec = NULL;
103 
104 /* client authentication */
105 static char *opt_ref = NULL;
106 static char *opt_secret = NULL;
107 static char *opt_cert = NULL;
108 static char *opt_own_trusted = NULL;
109 static char *opt_key = NULL;
110 static char *opt_keypass = NULL;
111 static char *opt_digest = NULL;
112 static char *opt_mac = NULL;
113 static char *opt_extracerts = NULL;
114 static int opt_unprotected_requests = 0;
115 
116 /* generic message */
117 static char *opt_cmd_s = NULL;
118 static int opt_cmd = -1;
119 static char *opt_geninfo = NULL;
120 static char *opt_infotype_s = NULL;
121 static int opt_infotype = NID_undef;
122 static char *opt_profile = NULL;
123 
124 /* certificate enrollment */
125 static char *opt_newkey = NULL;
126 static char *opt_newkeypass = NULL;
127 static int opt_centralkeygen = 0;
128 static char *opt_newkeyout = NULL;
129 static char *opt_subject = NULL;
130 static int opt_days = 0;
131 static char *opt_reqexts = NULL;
132 static char *opt_sans = NULL;
133 static int opt_san_nodefault = 0;
134 static char *opt_policies = NULL;
135 static char *opt_policy_oids = NULL;
136 static int opt_policy_oids_critical = 0;
137 static int opt_popo = OSSL_CRMF_POPO_NONE - 1;
138 static char *opt_csr = NULL;
139 static char *opt_out_trusted = NULL;
140 static int opt_implicit_confirm = 0;
141 static int opt_disable_confirm = 0;
142 static char *opt_certout = NULL;
143 static char *opt_chainout = NULL;
144 
145 /* certificate enrollment and revocation */
146 static char *opt_oldcert = NULL;
147 static char *opt_issuer = NULL;
148 static char *opt_serial = NULL;
149 static int opt_revreason = CRL_REASON_NONE;
150 
151 /* credentials format */
152 static char *opt_certform_s = "PEM";
153 static int opt_certform = FORMAT_PEM;
154 /*
155  * DER format is the preferred choice for saving a CRL because it allows for
156  * more efficient storage, especially when dealing with large CRLs.
157  */
158 static char *opt_crlform_s = "DER";
159 static int opt_crlform = FORMAT_ASN1;
160 static char *opt_keyform_s = NULL;
161 static int opt_keyform = FORMAT_UNDEF;
162 static char *opt_otherpass = NULL;
163 static char *opt_engine = NULL;
164 
165 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
166 /* TLS connection */
167 static int opt_tls_used = 0;
168 static char *opt_tls_cert = NULL;
169 static char *opt_tls_key = NULL;
170 static char *opt_tls_keypass = NULL;
171 static char *opt_tls_extra = NULL;
172 static char *opt_tls_trusted = NULL;
173 static char *opt_tls_host = NULL;
174 #endif
175 
176 /* client-side debugging */
177 static int opt_batch = 0;
178 static int opt_repeat = 1;
179 static char *opt_reqin = NULL;
180 static int opt_reqin_new_tid = 0;
181 static char *opt_reqout = NULL;
182 static char *opt_reqout_only = NULL;
183 static int reqout_only_done = 0;
184 static char *opt_rspin = NULL;
185 static int rspin_in_use = 0;
186 static char *opt_rspout = NULL;
187 static int opt_use_mock_srv = 0;
188 
189 /* mock server */
190 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
191 static char *opt_port = NULL;
192 static int opt_max_msgs = 0;
193 #endif
194 static char *opt_srv_ref = NULL;
195 static char *opt_srv_secret = NULL;
196 static char *opt_srv_cert = NULL;
197 static char *opt_srv_key = NULL;
198 static char *opt_srv_keypass = NULL;
199 
200 static char *opt_srv_trusted = NULL;
201 static char *opt_srv_untrusted = NULL;
202 static char *opt_ref_cert = NULL;
203 static char *opt_rsp_cert = NULL;
204 static char *opt_rsp_key = NULL;
205 static char *opt_rsp_keypass = NULL;
206 static char *opt_rsp_crl = NULL;
207 static char *opt_rsp_extracerts = NULL;
208 static char *opt_rsp_capubs = NULL;
209 static char *opt_rsp_newwithnew = NULL;
210 static char *opt_rsp_newwithold = NULL;
211 static char *opt_rsp_oldwithnew = NULL;
212 
213 static int opt_poll_count = 0;
214 static int opt_check_after = 1;
215 static int opt_grant_implicitconf = 0;
216 
217 static int opt_pkistatus = OSSL_CMP_PKISTATUS_accepted;
218 static int opt_failure = INT_MIN;
219 static int opt_failurebits = 0;
220 static char *opt_statusstring = NULL;
221 static int opt_send_error = 0;
222 static int opt_send_unprotected = 0;
223 static int opt_send_unprot_err = 0;
224 static int opt_accept_unprotected = 0;
225 static int opt_accept_unprot_err = 0;
226 static int opt_accept_raverified = 0;
227 
228 static X509_VERIFY_PARAM *vpm = NULL;
229 
230 typedef enum OPTION_choice {
231     OPT_COMMON,
232     OPT_CONFIG,
233     OPT_SECTION,
234     OPT_VERBOSITY,
235 
236     OPT_CMD,
237     OPT_INFOTYPE,
238     OPT_PROFILE,
239     OPT_GENINFO,
240     OPT_TEMPLATE,
241     OPT_KEYSPEC,
242 
243     OPT_NEWKEY,
244     OPT_NEWKEYPASS,
245     OPT_CENTRALKEYGEN,
246     OPT_NEWKEYOUT,
247     OPT_SUBJECT,
248     OPT_DAYS,
249     OPT_REQEXTS,
250     OPT_SANS,
251     OPT_SAN_NODEFAULT,
252     OPT_POLICIES,
253     OPT_POLICY_OIDS,
254     OPT_POLICY_OIDS_CRITICAL,
255     OPT_POPO,
256     OPT_CSR,
257     OPT_OUT_TRUSTED,
258     OPT_IMPLICIT_CONFIRM,
259     OPT_DISABLE_CONFIRM,
260     OPT_CERTOUT,
261     OPT_CHAINOUT,
262 
263     OPT_OLDCERT,
264     OPT_ISSUER,
265     OPT_SERIAL,
266     OPT_REVREASON,
267 
268 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
269     OPT_SERVER,
270     OPT_PROXY,
271     OPT_NO_PROXY,
272 #endif
273     OPT_RECIPIENT,
274     OPT_PATH,
275     OPT_KEEP_ALIVE,
276     OPT_MSG_TIMEOUT,
277     OPT_TOTAL_TIMEOUT,
278 
279     OPT_TRUSTED,
280     OPT_UNTRUSTED,
281     OPT_SRVCERT,
282     OPT_EXPECT_SENDER,
283     OPT_IGNORE_KEYUSAGE,
284     OPT_UNPROTECTED_ERRORS,
285     OPT_NO_CACHE_EXTRACERTS,
286     OPT_SRVCERTOUT,
287     OPT_EXTRACERTSOUT,
288     OPT_CACERTSOUT,
289     OPT_OLDWITHOLD,
290     OPT_NEWWITHNEW,
291     OPT_NEWWITHOLD,
292     OPT_OLDWITHNEW,
293     OPT_CRLCERT,
294     OPT_OLDCRL,
295     OPT_CRLOUT,
296 
297     OPT_REF,
298     OPT_SECRET,
299     OPT_CERT,
300     OPT_OWN_TRUSTED,
301     OPT_KEY,
302     OPT_KEYPASS,
303     OPT_DIGEST,
304     OPT_MAC,
305     OPT_EXTRACERTS,
306     OPT_UNPROTECTED_REQUESTS,
307 
308     OPT_CERTFORM,
309     OPT_CRLFORM,
310     OPT_KEYFORM,
311     OPT_OTHERPASS,
312 #ifndef OPENSSL_NO_ENGINE
313     OPT_ENGINE,
314 #endif
315     OPT_PROV_ENUM,
316     OPT_R_ENUM,
317 
318 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
319     OPT_TLS_USED,
320     OPT_TLS_CERT,
321     OPT_TLS_KEY,
322     OPT_TLS_KEYPASS,
323     OPT_TLS_EXTRA,
324     OPT_TLS_TRUSTED,
325     OPT_TLS_HOST,
326 #endif
327 
328     OPT_BATCH,
329     OPT_REPEAT,
330     OPT_REQIN,
331     OPT_REQIN_NEW_TID,
332     OPT_REQOUT,
333     OPT_REQOUT_ONLY,
334     OPT_RSPIN,
335     OPT_RSPOUT,
336     OPT_USE_MOCK_SRV,
337 
338 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
339     OPT_PORT,
340     OPT_MAX_MSGS,
341 #endif
342     OPT_SRV_REF,
343     OPT_SRV_SECRET,
344     OPT_SRV_CERT,
345     OPT_SRV_KEY,
346     OPT_SRV_KEYPASS,
347     OPT_SRV_TRUSTED,
348     OPT_SRV_UNTRUSTED,
349     OPT_REF_CERT,
350     OPT_RSP_CERT,
351     OPT_RSP_KEY,
352     OPT_RSP_KEYPASS,
353     OPT_RSP_CRL,
354     OPT_RSP_EXTRACERTS,
355     OPT_RSP_CAPUBS,
356     OPT_RSP_NEWWITHNEW,
357     OPT_RSP_NEWWITHOLD,
358     OPT_RSP_OLDWITHNEW,
359     OPT_POLL_COUNT,
360     OPT_CHECK_AFTER,
361     OPT_GRANT_IMPLICITCONF,
362     OPT_PKISTATUS,
363     OPT_FAILURE,
364     OPT_FAILUREBITS,
365     OPT_STATUSSTRING,
366     OPT_SEND_ERROR,
367     OPT_SEND_UNPROTECTED,
368     OPT_SEND_UNPROT_ERR,
369     OPT_ACCEPT_UNPROTECTED,
370     OPT_ACCEPT_UNPROT_ERR,
371     OPT_ACCEPT_RAVERIFIED,
372 
373     OPT_V_ENUM
374 } OPTION_CHOICE;
375 
376 const OPTIONS cmp_options[] = {
377     /* entries must be in the same order as enumerated above!! */
378     { "help", OPT_HELP, '-', "Display this summary" },
379     { "config", OPT_CONFIG, 's',
380         "Configuration file to use. \"\" = none. Default from env variable OPENSSL_CONF" },
381     { "section", OPT_SECTION, 's',
382         "Section(s) in config file to get options from. \"\" = 'default'. Default 'cmp'" },
383     { "verbosity", OPT_VERBOSITY, 'N',
384         "Log level; 3=ERR, 4=WARN, 6=INFO, 7=DEBUG, 8=TRACE. Default 6 = INFO" },
385 
386     OPT_SECTION("Generic message"),
387     { "cmd", OPT_CMD, 's', "CMP request to send: ir/cr/kur/p10cr/rr/genm" },
388     { "infotype", OPT_INFOTYPE, 's',
389         "InfoType name for requesting specific info in genm, with specific support" },
390     { OPT_MORE_STR, 0, 0,
391         "for 'caCerts' and 'rootCaCert'" },
392     { "profile", OPT_PROFILE, 's',
393         "Certificate profile name to place in generalInfo field of request PKIHeader" },
394     { "geninfo", OPT_GENINFO, 's',
395         "Comma-separated list of OID and value to place in generalInfo PKIHeader" },
396     { OPT_MORE_STR, 0, 0,
397         "of form <OID>:int:<n> or <OID>:str:<s>, e.g. \'1.2.3.4:int:56789, id-kp:str:name'" },
398     { "template", OPT_TEMPLATE, 's',
399         "File to save certTemplate received in genp of type certReqTemplate" },
400     { "keyspec", OPT_KEYSPEC, 's',
401         "Optional file to save Key specification received in genp of type certReqTemplate" },
402 
403     OPT_SECTION("Certificate enrollment"),
404     { "newkey", OPT_NEWKEY, 's',
405         "Private or public key for the requested cert. Default: CSR key or client key" },
406     { "newkeypass", OPT_NEWKEYPASS, 's', "New private key pass phrase source" },
407     { "centralkeygen", OPT_CENTRALKEYGEN, '-',
408         "Request central (server-side) key generation. Default is local generation" },
409     { "newkeyout", OPT_NEWKEYOUT, 's',
410         "File to save centrally generated key, in PEM format" },
411     { "subject", OPT_SUBJECT, 's',
412         "Distinguished Name (DN) of subject to use in the requested cert template" },
413     { OPT_MORE_STR, 0, 0,
414         "For kur, default is subject of -csr arg or reference cert (see -oldcert)" },
415     { OPT_MORE_STR, 0, 0,
416         "this default is used for ir and cr only if no Subject Alt Names are set" },
417     { "days", OPT_DAYS, 'N',
418         "Requested validity time of the new certificate in number of days" },
419     { "reqexts", OPT_REQEXTS, 's',
420         "Name of config file section defining certificate request extensions." },
421     { OPT_MORE_STR, 0, 0,
422         "Augments or replaces any extensions contained CSR given with -csr" },
423     { "sans", OPT_SANS, 's',
424         "Subject Alt Names (IPADDR/DNS/URI) to add as (critical) cert req extension" },
425     { "san_nodefault", OPT_SAN_NODEFAULT, '-',
426         "Do not take default SANs from reference certificate (see -oldcert)" },
427     { "policies", OPT_POLICIES, 's',
428         "Name of config file section defining policies certificate request extension" },
429     { "policy_oids", OPT_POLICY_OIDS, 's',
430         "Policy OID(s) to add as policies certificate request extension" },
431     { "policy_oids_critical", OPT_POLICY_OIDS_CRITICAL, '-',
432         "Flag the policy OID(s) given with -policy_oids as critical" },
433     { "popo", OPT_POPO, 'n',
434         "Proof-of-Possession (POPO) method to use for ir/cr/kur where" },
435     { OPT_MORE_STR, 0, 0,
436         "-1 = NONE, 0 = RAVERIFIED, 1 = SIGNATURE (default), 2 = KEYENC" },
437     { "csr", OPT_CSR, 's',
438         "PKCS#10 CSR file in PEM or DER format to convert or to use in p10cr" },
439     { "out_trusted", OPT_OUT_TRUSTED, 's',
440         "Certificates to trust when verifying newly enrolled certificates" },
441     { "implicit_confirm", OPT_IMPLICIT_CONFIRM, '-',
442         "Request implicit confirmation of newly enrolled certificates" },
443     { "disable_confirm", OPT_DISABLE_CONFIRM, '-',
444         "Do not confirm newly enrolled certificate w/o requesting implicit" },
445     { OPT_MORE_STR, 0, 0,
446         "confirmation. WARNING: This leads to behavior violating RFC 9810" },
447     { "certout", OPT_CERTOUT, 's',
448         "File to save newly enrolled certificate" },
449     { "chainout", OPT_CHAINOUT, 's',
450         "File to save the chain of newly enrolled certificate" },
451 
452     OPT_SECTION("Certificate enrollment and revocation"),
453 
454     { "oldcert", OPT_OLDCERT, 's',
455         "Certificate to be updated (defaulting to -cert) or to be revoked in rr;" },
456     { OPT_MORE_STR, 0, 0,
457         "also used as reference (defaulting to -cert) for subject DN and SANs." },
458     { OPT_MORE_STR, 0, 0,
459         "Issuer is used as recipient unless -recipient, -srvcert, or -issuer given" },
460     { "issuer", OPT_ISSUER, 's',
461         "DN of the issuer to place in the certificate template of ir/cr/kur/rr;" },
462     { OPT_MORE_STR, 0, 0,
463         "also used as recipient if neither -recipient nor -srvcert are given" },
464     { "serial", OPT_SERIAL, 's',
465         "Serial number of certificate to be revoked in revocation request (rr)" },
466     { "revreason", OPT_REVREASON, 'n',
467         "Reason code to include in revocation request (rr); possible values:" },
468     { OPT_MORE_STR, 0, 0,
469         "0..6, 8..10 (see RFC5280, 5.3.1) or -1. Default -1 = none included" },
470 
471     OPT_SECTION("Message transfer"),
472 #if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
473     { OPT_MORE_STR, 0, 0,
474         "NOTE: -server, -proxy, and -no_proxy not supported due to no-sock/no-http build" },
475 #else
476     { "server", OPT_SERVER, 's',
477         "[http[s]://]address[:port][/path] of CMP server. Default port 80 or 443." },
478     { OPT_MORE_STR, 0, 0,
479         "address may be a DNS name or an IP address; path can be overridden by -path" },
480     { "proxy", OPT_PROXY, 's',
481         "[http[s]://]address[:port][/path] of HTTP(S) proxy to use; path is ignored" },
482     { "no_proxy", OPT_NO_PROXY, 's',
483         "List of addresses of servers not to use HTTP(S) proxy for" },
484     { OPT_MORE_STR, 0, 0,
485         "Default from environment variable 'no_proxy', else 'NO_PROXY', else none" },
486 #endif
487     { "recipient", OPT_RECIPIENT, 's',
488         "DN of CA. Default: subject of -srvcert, -issuer, issuer of -oldcert or -cert" },
489     { "path", OPT_PATH, 's',
490         "HTTP path (aka CMP alias) at the CMP server. Default from -server, else \"/\"" },
491     { "keep_alive", OPT_KEEP_ALIVE, 'N',
492         "Persistent HTTP connections. 0: no, 1 (the default): request, 2: require" },
493     { "msg_timeout", OPT_MSG_TIMEOUT, 'N',
494         "Number of seconds allowed per CMP message round trip, or 0 for infinite" },
495     { "total_timeout", OPT_TOTAL_TIMEOUT, 'N',
496         "Overall time an enrollment incl. polling may take. Default 0 = infinite" },
497 
498     OPT_SECTION("Server authentication"),
499     { "trusted", OPT_TRUSTED, 's',
500         "Certificates to use as trust anchors when verifying signed CMP responses" },
501     { OPT_MORE_STR, 0, 0, "unless -srvcert is given" },
502     { "untrusted", OPT_UNTRUSTED, 's',
503         "Intermediate CA certs for chain construction for CMP/TLS/enrolled certs" },
504     { "srvcert", OPT_SRVCERT, 's',
505         "Server cert to pin and trust directly when verifying signed CMP responses" },
506     { "expect_sender", OPT_EXPECT_SENDER, 's',
507         "DN of expected sender of responses. Defaults to subject of -srvcert, if any" },
508     { "ignore_keyusage", OPT_IGNORE_KEYUSAGE, '-',
509         "Ignore CMP signer cert key usage, else 'digitalSignature' must be allowed" },
510     { "unprotected_errors", OPT_UNPROTECTED_ERRORS, '-',
511         "Accept missing or invalid protection of regular error messages and negative" },
512     { OPT_MORE_STR, 0, 0,
513         "certificate responses (ip/cp/kup), revocation responses (rp), and PKIConf" },
514     { OPT_MORE_STR, 0, 0,
515         "WARNING: This setting leads to behavior allowing violation of RFC 9810" },
516     { "no_cache_extracerts", OPT_NO_CACHE_EXTRACERTS, '-',
517         "Do not keep certificates received in the extraCerts CMP message field" },
518     { "srvcertout", OPT_SRVCERTOUT, 's',
519         "File to save the server cert used and validated for CMP response protection" },
520     { "extracertsout", OPT_EXTRACERTSOUT, 's',
521         "File to save extra certificates received in the extraCerts field" },
522     { "cacertsout", OPT_CACERTSOUT, 's',
523         "File to save CA certs received in caPubs field or genp with id-it-caCerts" },
524     { "oldwithold", OPT_OLDWITHOLD, 's',
525         "Root CA certificate to request update for in genm of type rootCaCert" },
526     { "newwithnew", OPT_NEWWITHNEW, 's',
527         "File to save NewWithNew cert received in genp of type rootCaKeyUpdate" },
528     { "newwithold", OPT_NEWWITHOLD, 's',
529         "File to save NewWithOld cert received in genp of type rootCaKeyUpdate" },
530     { "oldwithnew", OPT_OLDWITHNEW, 's',
531         "File to save OldWithNew cert received in genp of type rootCaKeyUpdate" },
532     { "crlcert", OPT_CRLCERT, 's',
533         "certificate to request a CRL for in genm of type crlStatusList" },
534     { "oldcrl", OPT_OLDCRL, 's',
535         "CRL to request update for in genm of type crlStatusList" },
536     { "crlout", OPT_CRLOUT, 's',
537         "File to save new CRL received in genp of type 'crls'" },
538 
539     OPT_SECTION("Client authentication"),
540     { "ref", OPT_REF, 's',
541         "Reference value to use as senderKID in case no -cert is given" },
542     { "secret", OPT_SECRET, 's',
543         "Prefer PBM (over signatures) for protecting msgs with given password source" },
544     { "cert", OPT_CERT, 's',
545         "Client's CMP signer certificate; its public key must match the -key argument" },
546     { OPT_MORE_STR, 0, 0,
547         "This also used as default reference for subject DN and SANs." },
548     { OPT_MORE_STR, 0, 0,
549         "Any further certs included are appended to the untrusted certs" },
550     { "own_trusted", OPT_OWN_TRUSTED, 's',
551         "Optional certs to verify chain building for own CMP signer cert" },
552     { "key", OPT_KEY, 's', "CMP signer private key, not used when -secret given" },
553     { "keypass", OPT_KEYPASS, 's',
554         "Client private key (and cert and old cert) pass phrase source" },
555     { "digest", OPT_DIGEST, 's',
556         "Digest to use in message protection and POPO signatures. Default \"sha256\"" },
557     { "mac", OPT_MAC, 's',
558         "MAC algorithm to use in PBM-based message protection. Default \"hmac-sha1\"" },
559     { "extracerts", OPT_EXTRACERTS, 's',
560         "Certificates to append in extraCerts field of outgoing messages." },
561     { OPT_MORE_STR, 0, 0,
562         "This can be used as the default CMP signer cert chain to include" },
563     { "unprotected_requests", OPT_UNPROTECTED_REQUESTS, '-',
564         "Send request messages without CMP-level protection" },
565 
566     OPT_SECTION("Credentials format"),
567     { "certform", OPT_CERTFORM, 's',
568         "Format (PEM or DER) to use when saving a certificate to a file. Default PEM" },
569     { "crlform", OPT_CRLFORM, 's',
570         "Format (PEM or DER) to use when saving a CRL to a file. Default DER" },
571     { "keyform", OPT_KEYFORM, 's',
572         "Format of the key input (ENGINE, other values ignored)" },
573     { "otherpass", OPT_OTHERPASS, 's',
574         "Pass phrase source potentially needed for loading certificates of others" },
575 #ifndef OPENSSL_NO_ENGINE
576     { "engine", OPT_ENGINE, 's',
577         "Use crypto engine with given identifier, possibly a hardware device." },
578     { OPT_MORE_STR, 0, 0,
579         "Engines may also be defined in OpenSSL config file engine section." },
580 #endif
581     OPT_PROV_OPTIONS,
582     OPT_R_OPTIONS,
583 
584     OPT_SECTION("TLS connection"),
585 #if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
586     { OPT_MORE_STR, 0, 0,
587         "NOTE: -tls_used and all other TLS options not supported due to no-sock/no-http build" },
588 #else
589     { "tls_used", OPT_TLS_USED, '-',
590         "Enable using TLS (also when other TLS options are not set)" },
591     { "tls_cert", OPT_TLS_CERT, 's',
592         "Client's TLS certificate. May include chain to be provided to TLS server" },
593     { "tls_key", OPT_TLS_KEY, 's',
594         "Private key for the client's TLS certificate" },
595     { "tls_keypass", OPT_TLS_KEYPASS, 's',
596         "Pass phrase source for the client's private TLS key (and TLS cert)" },
597     { "tls_extra", OPT_TLS_EXTRA, 's',
598         "Extra certificates to provide to TLS server during TLS handshake" },
599     { "tls_trusted", OPT_TLS_TRUSTED, 's',
600         "Trusted certificates to use for verifying the TLS server certificate;" },
601     { OPT_MORE_STR, 0, 0, "this implies hostname validation" },
602     { "tls_host", OPT_TLS_HOST, 's',
603         "Address to be checked (rather than -server) during TLS hostname validation" },
604 #endif
605 
606     OPT_SECTION("Client-side debugging"),
607     { "batch", OPT_BATCH, '-',
608         "Do not interactively prompt for input when a password is required etc." },
609     { "repeat", OPT_REPEAT, 'p',
610         "Invoke the transaction the given positive number of times. Default 1" },
611     { "reqin", OPT_REQIN, 's',
612         "Take sequence of CMP requests to send to server from file(s)" },
613     { "reqin_new_tid", OPT_REQIN_NEW_TID, '-',
614         "Use fresh transactionID for CMP requests read from -reqin" },
615     { "reqout", OPT_REQOUT, 's',
616         "Save sequence of CMP requests created by the client to file(s)" },
617     { "reqout_only", OPT_REQOUT_ONLY, 's',
618         "Save first CMP request created by the client to file and exit" },
619     { "rspin", OPT_RSPIN, 's',
620         "Process sequence of CMP responses provided in file(s), skipping server" },
621     { "rspout", OPT_RSPOUT, 's',
622         "Save sequence of actually used CMP responses to file(s)" },
623 
624     { "use_mock_srv", OPT_USE_MOCK_SRV, '-',
625         "Use internal mock server at API level, bypassing socket-based HTTP" },
626 
627     OPT_SECTION("Mock server"),
628 #if defined(OPENSSL_NO_SOCK) || defined(OPENSSL_NO_HTTP)
629     { OPT_MORE_STR, 0, 0,
630         "NOTE: -port and -max_msgs not supported due to no-sock/no-http build" },
631 #else
632     { "port", OPT_PORT, 's',
633         "Act as HTTP-based mock server listening on given port" },
634     { "max_msgs", OPT_MAX_MSGS, 'N',
635         "max number of messages handled by HTTP mock server. Default: 0 = unlimited" },
636 #endif
637 
638     { "srv_ref", OPT_SRV_REF, 's',
639         "Reference value to use as senderKID of server in case no -srv_cert is given" },
640     { "srv_secret", OPT_SRV_SECRET, 's',
641         "Password source for server authentication with a pre-shared key (secret)" },
642     { "srv_cert", OPT_SRV_CERT, 's', "Certificate of the server" },
643     { "srv_key", OPT_SRV_KEY, 's',
644         "Private key used by the server for signing messages" },
645     { "srv_keypass", OPT_SRV_KEYPASS, 's',
646         "Server private key (and cert) pass phrase source" },
647 
648     { "srv_trusted", OPT_SRV_TRUSTED, 's',
649         "Trusted certificates for client authentication" },
650     { "srv_untrusted", OPT_SRV_UNTRUSTED, 's',
651         "Intermediate certs that may be useful for verifying CMP protection" },
652     { "ref_cert", OPT_RSP_CERT, 's',
653         "Certificate to be expected for rr and any oldCertID in kur messages" },
654     { "rsp_cert", OPT_RSP_CERT, 's',
655         "Certificate to be returned as mock enrollment result" },
656     { "rsp_key", OPT_RSP_KEY, 's',
657         "Private key for the certificate to be returned as mock enrollment result" },
658     { OPT_MORE_STR, 0, 0,
659         "Key to be returned for central key pair generation" },
660     { "rsp_keypass", OPT_RSP_KEYPASS, 's',
661         "Response private key (and cert) pass phrase source" },
662     { "rsp_crl", OPT_RSP_CRL, 's',
663         "CRL to be returned in genp of type crls" },
664     { "rsp_extracerts", OPT_RSP_EXTRACERTS, 's',
665         "Extra certificates to be included in mock certification responses" },
666     { "rsp_capubs", OPT_RSP_CAPUBS, 's',
667         "CA certificates to be included in mock ip response" },
668     { "rsp_newwithnew", OPT_RSP_NEWWITHNEW, 's',
669         "New root CA certificate to include in genp of type rootCaKeyUpdate" },
670     { "rsp_newwithold", OPT_RSP_NEWWITHOLD, 's',
671         "NewWithOld transition cert to include in genp of type rootCaKeyUpdate" },
672     { "rsp_oldwithnew", OPT_RSP_OLDWITHNEW, 's',
673         "OldWithNew transition cert to include in genp of type rootCaKeyUpdate" },
674     { "poll_count", OPT_POLL_COUNT, 'N',
675         "Number of times the client must poll before receiving a certificate" },
676     { "check_after", OPT_CHECK_AFTER, 'N',
677         "The check_after value (time to wait) to include in poll response" },
678     { "grant_implicitconf", OPT_GRANT_IMPLICITCONF, '-',
679         "Grant implicit confirmation of newly enrolled certificate" },
680 
681     { "pkistatus", OPT_PKISTATUS, 'N',
682         "PKIStatus to be included in server response. Possible values: 0..6" },
683     { "failure", OPT_FAILURE, 'N',
684         "A single failure info bit number to include in server response, 0..26" },
685     { "failurebits", OPT_FAILUREBITS, 'N',
686         "Number representing failure bits to include in server response, 0..2^27 - 1" },
687     { "statusstring", OPT_STATUSSTRING, 's',
688         "Status string to be included in server response" },
689     { "send_error", OPT_SEND_ERROR, '-',
690         "Force server to reply with error message" },
691     { "send_unprotected", OPT_SEND_UNPROTECTED, '-',
692         "Send response messages without CMP-level protection" },
693     { "send_unprot_err", OPT_SEND_UNPROT_ERR, '-',
694         "In case of negative responses, server shall send unprotected error messages," },
695     { OPT_MORE_STR, 0, 0,
696         "certificate responses (ip/cp/kup), and revocation responses (rp)." },
697     { OPT_MORE_STR, 0, 0,
698         "WARNING: This setting leads to behavior violating RFC 9810" },
699     { "accept_unprotected", OPT_ACCEPT_UNPROTECTED, '-',
700         "Accept missing or invalid protection of requests" },
701     { "accept_unprot_err", OPT_ACCEPT_UNPROT_ERR, '-',
702         "Accept unprotected error messages from client" },
703     { "accept_raverified", OPT_ACCEPT_RAVERIFIED, '-',
704         "Accept RAVERIFIED as proof-of-possession (POPO)" },
705 
706     OPT_V_OPTIONS,
707     { NULL }
708 };
709 
710 typedef union {
711     char **txt;
712     int *num;
713     long *num_long;
714 } varref;
715 static varref cmp_vars[] = { /* must be in same order as enumerated above! */
716     { &opt_config }, { &opt_section }, { (char **)&opt_verbosity },
717 
718     { &opt_cmd_s }, { &opt_infotype_s }, { &opt_profile }, { &opt_geninfo },
719     { &opt_template }, { &opt_keyspec },
720 
721     { &opt_newkey }, { &opt_newkeypass }, { (char **)&opt_centralkeygen },
722     { &opt_newkeyout }, { &opt_subject }, { (char **)&opt_days }, { &opt_reqexts },
723     { &opt_sans }, { (char **)&opt_san_nodefault },
724     { &opt_policies }, { &opt_policy_oids }, { (char **)&opt_policy_oids_critical },
725     { (char **)&opt_popo }, { &opt_csr },
726     { &opt_out_trusted },
727     { (char **)&opt_implicit_confirm }, { (char **)&opt_disable_confirm },
728     { &opt_certout }, { &opt_chainout },
729 
730     { &opt_oldcert }, { &opt_issuer }, { &opt_serial }, { (char **)&opt_revreason },
731 
732 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
733     { &opt_server }, { &opt_proxy }, { &opt_no_proxy },
734 #endif
735     { &opt_recipient }, { &opt_path }, { (char **)&opt_keep_alive },
736     { (char **)&opt_msg_timeout }, { (char **)&opt_total_timeout },
737 
738     { &opt_trusted }, { &opt_untrusted }, { &opt_srvcert },
739     { &opt_expect_sender },
740     { (char **)&opt_ignore_keyusage }, { (char **)&opt_unprotected_errors },
741     { (char **)&opt_no_cache_extracerts },
742     { &opt_srvcertout }, { &opt_extracertsout }, { &opt_cacertsout },
743     { &opt_oldwithold }, { &opt_newwithnew }, { &opt_newwithold }, { &opt_oldwithnew },
744     { &opt_crlcert }, { &opt_oldcrl }, { &opt_crlout },
745 
746     { &opt_ref }, { &opt_secret },
747     { &opt_cert }, { &opt_own_trusted }, { &opt_key }, { &opt_keypass },
748     { &opt_digest }, { &opt_mac }, { &opt_extracerts },
749     { (char **)&opt_unprotected_requests },
750 
751     { &opt_certform_s }, { &opt_crlform_s }, { &opt_keyform_s },
752     { &opt_otherpass },
753 #ifndef OPENSSL_NO_ENGINE
754     { &opt_engine },
755 #endif
756 
757 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
758     { (char **)&opt_tls_used }, { &opt_tls_cert }, { &opt_tls_key },
759     { &opt_tls_keypass },
760     { &opt_tls_extra }, { &opt_tls_trusted }, { &opt_tls_host },
761 #endif
762 
763     { (char **)&opt_batch }, { (char **)&opt_repeat },
764     { &opt_reqin }, { (char **)&opt_reqin_new_tid },
765     { &opt_reqout }, { &opt_reqout_only }, { &opt_rspin }, { &opt_rspout },
766 
767     { (char **)&opt_use_mock_srv },
768 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
769     { &opt_port }, { (char **)&opt_max_msgs },
770 #endif
771     { &opt_srv_ref }, { &opt_srv_secret },
772     { &opt_srv_cert }, { &opt_srv_key }, { &opt_srv_keypass },
773     { &opt_srv_trusted }, { &opt_srv_untrusted },
774     { &opt_ref_cert }, { &opt_rsp_cert }, { &opt_rsp_key }, { &opt_rsp_keypass },
775     { &opt_rsp_crl }, { &opt_rsp_extracerts }, { &opt_rsp_capubs },
776     { &opt_rsp_newwithnew }, { &opt_rsp_newwithold }, { &opt_rsp_oldwithnew },
777 
778     { (char **)&opt_poll_count }, { (char **)&opt_check_after },
779     { (char **)&opt_grant_implicitconf },
780     { (char **)&opt_pkistatus }, { (char **)&opt_failure },
781     { (char **)&opt_failurebits }, { &opt_statusstring },
782     { (char **)&opt_send_error }, { (char **)&opt_send_unprotected },
783     { (char **)&opt_send_unprot_err }, { (char **)&opt_accept_unprotected },
784     { (char **)&opt_accept_unprot_err }, { (char **)&opt_accept_raverified },
785 
786     { NULL }
787 };
788 
789 #define FUNC (strcmp(OPENSSL_FUNC, "(unknown function)") == 0 \
790         ? "CMP"                                               \
791         : OPENSSL_FUNC)
792 #define CMP_print(bio, level, prefix, msg, a1, a2, a3) \
793     ((void)(level > opt_verbosity ? 0 : (BIO_printf(bio, "%s:%s:%d:CMP %s: " msg "\n", FUNC, OPENSSL_FILE, OPENSSL_LINE, prefix, a1, a2, a3))))
794 #define CMP_DEBUG(m, a1, a2, a3) \
795     CMP_print(bio_out, OSSL_CMP_LOG_DEBUG, "debug", m, a1, a2, a3)
796 #define CMP_debug(msg) CMP_DEBUG(msg "%s%s%s", "", "", "")
797 #define CMP_debug1(msg, a1) CMP_DEBUG(msg "%s%s", a1, "", "")
798 #define CMP_debug2(msg, a1, a2) CMP_DEBUG(msg "%s", a1, a2, "")
799 #define CMP_debug3(msg, a1, a2, a3) CMP_DEBUG(msg, a1, a2, a3)
800 #define CMP_INFO(msg, a1, a2, a3) \
801     CMP_print(bio_out, OSSL_CMP_LOG_INFO, "info", msg, a1, a2, a3)
802 #define CMP_info(msg) CMP_INFO(msg "%s%s%s", "", "", "")
803 #define CMP_info1(msg, a1) CMP_INFO(msg "%s%s", a1, "", "")
804 #define CMP_info2(msg, a1, a2) CMP_INFO(msg "%s", a1, a2, "")
805 #define CMP_info3(msg, a1, a2, a3) CMP_INFO(msg, a1, a2, a3)
806 #define CMP_WARN(m, a1, a2, a3) \
807     CMP_print(bio_out, OSSL_CMP_LOG_WARNING, "warning", m, a1, a2, a3)
808 #define CMP_warn(msg) CMP_WARN(msg "%s%s%s", "", "", "")
809 #define CMP_warn1(msg, a1) CMP_WARN(msg "%s%s", a1, "", "")
810 #define CMP_warn2(msg, a1, a2) CMP_WARN(msg "%s", a1, a2, "")
811 #define CMP_warn3(msg, a1, a2, a3) CMP_WARN(msg, a1, a2, a3)
812 #define CMP_ERR(msg, a1, a2, a3) \
813     CMP_print(bio_err, OSSL_CMP_LOG_ERR, "error", msg, a1, a2, a3)
814 #define CMP_err(msg) CMP_ERR(msg "%s%s%s", "", "", "")
815 #define CMP_err1(msg, a1) CMP_ERR(msg "%s%s", a1, "", "")
816 #define CMP_err2(msg, a1, a2) CMP_ERR(msg "%s", a1, a2, "")
817 #define CMP_err3(msg, a1, a2, a3) CMP_ERR(msg, a1, a2, a3)
818 
print_to_bio_out(const char * func,const char * file,int line,OSSL_CMP_severity level,const char * msg)819 static int print_to_bio_out(const char *func, const char *file, int line,
820     OSSL_CMP_severity level, const char *msg)
821 {
822     return OSSL_CMP_print_to_bio(bio_out, func, file, line, level, msg);
823 }
824 
print_to_bio_err(const char * func,const char * file,int line,OSSL_CMP_severity level,const char * msg)825 static int print_to_bio_err(const char *func, const char *file, int line,
826     OSSL_CMP_severity level, const char *msg)
827 {
828     return OSSL_CMP_print_to_bio(bio_err, func, file, line, level, msg);
829 }
830 
set_verbosity(int level)831 static int set_verbosity(int level)
832 {
833     if (level < OSSL_CMP_LOG_EMERG || level > OSSL_CMP_LOG_MAX) {
834         CMP_err1("Logging verbosity level %d out of range (0 .. 8)", level);
835         return 0;
836     }
837     opt_verbosity = level;
838     return 1;
839 }
840 
load_key_pwd(const char * uri,int format,const char * pass,ENGINE * eng,const char * desc)841 static EVP_PKEY *load_key_pwd(const char *uri, int format,
842     const char *pass, ENGINE *eng, const char *desc)
843 {
844     char *pass_string = get_passwd(pass, desc);
845     EVP_PKEY *pkey = load_key(uri, format, 0, pass_string, eng, desc);
846 
847     clear_free(pass_string);
848     return pkey;
849 }
850 
load_cert_pwd(const char * uri,const char * pass,const char * desc)851 static X509 *load_cert_pwd(const char *uri, const char *pass, const char *desc)
852 {
853     X509 *cert;
854     char *pass_string = get_passwd(pass, desc);
855 
856     cert = load_cert_pass(uri, FORMAT_UNDEF, 0, pass_string, desc);
857     clear_free(pass_string);
858     return cert;
859 }
860 
861 /* set expected hostname/IP addr and clears the email addr in the given ts */
truststore_set_host_etc(X509_STORE * ts,const char * host)862 static int truststore_set_host_etc(X509_STORE *ts, const char *host)
863 {
864     X509_VERIFY_PARAM *ts_vpm = X509_STORE_get0_param(ts);
865 
866     /* first clear any hostnames, IP, and email addresses */
867     if (!X509_VERIFY_PARAM_set1_host(ts_vpm, NULL, 0)
868         || !X509_VERIFY_PARAM_set1_ip(ts_vpm, NULL, 0)
869         || !X509_VERIFY_PARAM_set1_email(ts_vpm, NULL, 0))
870         return 0;
871     X509_VERIFY_PARAM_set_hostflags(ts_vpm,
872         X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT | X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
873     return (host != NULL && X509_VERIFY_PARAM_set1_ip_asc(ts_vpm, host))
874         || X509_VERIFY_PARAM_set1_host(ts_vpm, host, 0);
875 }
876 
877 /* write OSSL_CMP_MSG DER-encoded to the specified file name item */
write_PKIMESSAGE(const OSSL_CMP_MSG * msg,char ** filenames)878 static int write_PKIMESSAGE(const OSSL_CMP_MSG *msg, char **filenames)
879 {
880     char *file;
881 
882     if (msg == NULL || filenames == NULL) {
883         CMP_err("NULL arg to write_PKIMESSAGE");
884         return 0;
885     }
886     if (*filenames == NULL) {
887         CMP_err("not enough file names provided for writing PKIMessage");
888         return 0;
889     }
890 
891     file = *filenames;
892     *filenames = next_item(file);
893     if (OSSL_CMP_MSG_write(file, msg) < 0) {
894         CMP_err1("cannot write PKIMessage to file '%s'", file);
895         return 0;
896     }
897     return 1;
898 }
899 
900 /* read DER-encoded OSSL_CMP_MSG from the specified file name item */
read_PKIMESSAGE(const char * desc,char ** filenames)901 static OSSL_CMP_MSG *read_PKIMESSAGE(const char *desc, char **filenames)
902 {
903     char *file;
904     OSSL_CMP_MSG *ret;
905 
906     if (filenames == NULL || desc == NULL) {
907         CMP_err("NULL arg to read_PKIMESSAGE");
908         return NULL;
909     }
910     if (*filenames == NULL) {
911         CMP_err("not enough file names provided for reading PKIMessage");
912         return NULL;
913     }
914 
915     file = *filenames;
916     *filenames = next_item(file);
917 
918     ret = OSSL_CMP_MSG_read(file, app_get0_libctx(), app_get0_propq());
919     if (ret == NULL)
920         CMP_err1("cannot read PKIMessage from file '%s'", file);
921     else
922         CMP_info2("%s %s", desc, file);
923     return ret;
924 }
925 
926 /*-
927  * Sends the PKIMessage req and on success place the response in *res
928  * basically like OSSL_CMP_MSG_http_perform(), but in addition allows
929  * to dump the sequence of requests and responses to files and/or
930  * to take the sequence of requests and responses from files.
931  */
read_write_req_resp(OSSL_CMP_CTX * ctx,const OSSL_CMP_MSG * req)932 static OSSL_CMP_MSG *read_write_req_resp(OSSL_CMP_CTX *ctx,
933     const OSSL_CMP_MSG *req)
934 {
935     OSSL_CMP_MSG *req_new = NULL;
936     OSSL_CMP_MSG *res = NULL;
937     OSSL_CMP_PKIHEADER *hdr;
938     const char *prev_opt_rspin = opt_rspin;
939 
940     if (opt_reqout_only != NULL) {
941         if (OSSL_CMP_MSG_write(opt_reqout_only, req) < 0)
942             CMP_err1("cannot write request PKIMessage to file '%s'",
943                 opt_reqout_only);
944         else
945             reqout_only_done = 1;
946         return NULL; /* stop at this point, not contacting any server */
947     }
948     if (opt_reqout != NULL && !write_PKIMESSAGE(req, &opt_reqout))
949         goto err;
950     if (opt_reqin != NULL && opt_rspin == NULL) {
951         if ((req_new = read_PKIMESSAGE("actually sending", &opt_reqin)) == NULL)
952             goto err;
953         /*-
954          * The transaction ID in req_new read from opt_reqin may not be fresh.
955          * In this case the server may complain "Transaction id already in use."
956          * The following workaround unfortunately requires re-protection.
957          */
958         if (opt_reqin_new_tid
959             && !OSSL_CMP_MSG_update_transactionID(ctx, req_new))
960             goto err;
961 
962         /*
963          * Except for first request, need to satisfy recipNonce check by server.
964          * Unfortunately requires re-protection if protection is required.
965          */
966         if (!OSSL_CMP_MSG_update_recipNonce(ctx, req_new))
967             goto err;
968     }
969 
970     if (opt_rspin != NULL) {
971         res = read_PKIMESSAGE("actually using", &opt_rspin);
972     } else {
973         const OSSL_CMP_MSG *actual_req = req_new != NULL ? req_new : req;
974 
975         if (opt_use_mock_srv) {
976             if (rspin_in_use)
977                 CMP_warn("too few -rspin filename arguments; resorting to using mock server");
978             res = OSSL_CMP_CTX_server_perform(ctx, actual_req);
979         } else {
980 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
981             if (opt_server == NULL) {
982                 CMP_err("missing -server or -use_mock_srv option, or too few -rspin filename arguments");
983                 goto err;
984             }
985             if (rspin_in_use)
986                 CMP_warn("too few -rspin filename arguments; resorting to contacting server");
987             res = OSSL_CMP_MSG_http_perform(ctx, actual_req);
988 #else
989             CMP_err("-server not supported on no-sock/no-http build; missing -use_mock_srv option or too few -rspin filename arguments");
990 #endif
991         }
992         rspin_in_use = 0;
993     }
994     if (res == NULL)
995         goto err;
996 
997     if (req_new != NULL || prev_opt_rspin != NULL) {
998         /* need to satisfy nonce and transactionID checks by client */
999         ASN1_OCTET_STRING *nonce;
1000         ASN1_OCTET_STRING *tid;
1001 
1002         hdr = OSSL_CMP_MSG_get0_header(res);
1003         nonce = OSSL_CMP_HDR_get0_recipNonce(hdr);
1004         tid = OSSL_CMP_HDR_get0_transactionID(hdr);
1005         if (!OSSL_CMP_CTX_set1_senderNonce(ctx, nonce)
1006             || !OSSL_CMP_CTX_set1_transactionID(ctx, tid)) {
1007             OSSL_CMP_MSG_free(res);
1008             res = NULL;
1009             goto err;
1010         }
1011     }
1012 
1013     if (opt_rspout != NULL && !write_PKIMESSAGE(res, &opt_rspout)) {
1014         OSSL_CMP_MSG_free(res);
1015         res = NULL;
1016     }
1017 
1018 err:
1019     OSSL_CMP_MSG_free(req_new);
1020     return res;
1021 }
1022 
set_name(const char * str,int (* set_fn)(OSSL_CMP_CTX * ctx,const X509_NAME * name),OSSL_CMP_CTX * ctx,const char * desc)1023 static int set_name(const char *str,
1024     int (*set_fn)(OSSL_CMP_CTX *ctx, const X509_NAME *name),
1025     OSSL_CMP_CTX *ctx, const char *desc)
1026 {
1027     if (str != NULL) {
1028         X509_NAME *n = parse_name(str, MBSTRING_UTF8, 1, desc);
1029 
1030         if (n == NULL)
1031             return 0;
1032         if (!(*set_fn)(ctx, n)) {
1033             X509_NAME_free(n);
1034             CMP_err("out of memory");
1035             return 0;
1036         }
1037         X509_NAME_free(n);
1038     }
1039     return 1;
1040 }
1041 
set_gennames(OSSL_CMP_CTX * ctx,char * names,const char * desc)1042 static int set_gennames(OSSL_CMP_CTX *ctx, char *names, const char *desc)
1043 {
1044     char *next;
1045 
1046     for (; names != NULL; names = next) {
1047         GENERAL_NAME *n;
1048 
1049         next = next_item(names);
1050         if (strcmp(names, "critical") == 0) {
1051             (void)OSSL_CMP_CTX_set_option(ctx,
1052                 OSSL_CMP_OPT_SUBJECTALTNAME_CRITICAL,
1053                 1);
1054             continue;
1055         }
1056 
1057         /* try IP address first, then email/URI/domain name */
1058         (void)ERR_set_mark();
1059         n = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_IPADD, names, 0);
1060         if (n == NULL)
1061             n = a2i_GENERAL_NAME(NULL, NULL, NULL,
1062                 strchr(names, '@') != NULL ? GEN_EMAIL : strchr(names, ':') != NULL ? GEN_URI
1063                                                                                     : GEN_DNS,
1064                 names, 0);
1065         (void)ERR_pop_to_mark();
1066 
1067         if (n == NULL) {
1068             CMP_err2("bad syntax of %s '%s'", desc, names);
1069             return 0;
1070         }
1071         if (!OSSL_CMP_CTX_push1_subjectAltName(ctx, n)) {
1072             GENERAL_NAME_free(n);
1073             CMP_err("out of memory");
1074             return 0;
1075         }
1076         GENERAL_NAME_free(n);
1077     }
1078     return 1;
1079 }
1080 
load_trusted(char * input,int for_new_cert,const char * desc)1081 static X509_STORE *load_trusted(char *input, int for_new_cert, const char *desc)
1082 {
1083     X509_STORE *ts = load_certstore(input, opt_otherpass, desc, vpm);
1084 
1085     if (ts == NULL)
1086         return NULL;
1087     X509_STORE_set_verify_cb(ts, X509_STORE_CTX_print_verify_cb);
1088 
1089     /* copy vpm to store */
1090     if (X509_STORE_set1_param(ts, vpm /* may be NULL */)
1091         && (for_new_cert || truststore_set_host_etc(ts, NULL)))
1092         return ts;
1093     BIO_printf(bio_err, "error setting verification parameters for %s\n", desc);
1094     OSSL_CMP_CTX_print_errors(cmp_ctx);
1095     X509_STORE_free(ts);
1096     return NULL;
1097 }
1098 
1099 typedef int (*add_X509_fn_t)(void *ctx, const X509 *cert);
setup_cert(void * ctx,const char * file,const char * pass,const char * desc,add_X509_fn_t set1_fn)1100 static int setup_cert(void *ctx, const char *file, const char *pass,
1101     const char *desc, add_X509_fn_t set1_fn)
1102 {
1103     X509 *cert;
1104     int ok;
1105 
1106     if (file == NULL)
1107         return 1;
1108     if ((cert = load_cert_pwd(file, pass, desc)) == NULL)
1109         return 0;
1110     ok = (*set1_fn)(ctx, cert);
1111     X509_free(cert);
1112     return ok;
1113 }
1114 
1115 typedef int (*add_X509_stack_fn_t)(void *ctx, const STACK_OF(X509) *certs);
setup_certs(char * files,const char * desc,void * ctx,add_X509_stack_fn_t set1_fn)1116 static int setup_certs(char *files, const char *desc, void *ctx,
1117     add_X509_stack_fn_t set1_fn)
1118 {
1119     STACK_OF(X509) *certs;
1120     int ok;
1121 
1122     if (files == NULL)
1123         return 1;
1124     if ((certs = load_certs_multifile(files, opt_otherpass, desc, vpm)) == NULL)
1125         return 0;
1126     ok = (*set1_fn)(ctx, certs);
1127     OSSL_STACK_OF_X509_free(certs);
1128     return ok;
1129 }
1130 
setup_mock_crlout(void * ctx,const char * file,const char * desc)1131 static int setup_mock_crlout(void *ctx, const char *file, const char *desc)
1132 {
1133     X509_CRL *crl;
1134     int ok;
1135 
1136     if (file == NULL)
1137         return 1;
1138     if ((crl = load_crl(file, FORMAT_UNDEF, 0, desc)) == NULL)
1139         return 0;
1140     ok = ossl_cmp_mock_srv_set1_crlOut(ctx, crl);
1141     X509_CRL_free(crl);
1142     return ok;
1143 }
1144 /*
1145  * parse and transform some options, checking their syntax.
1146  * Returns 1 on success, 0 on error
1147  */
transform_opts(void)1148 static int transform_opts(void)
1149 {
1150     if (opt_cmd_s != NULL) {
1151         if (!strcmp(opt_cmd_s, "ir")) {
1152             opt_cmd = CMP_IR;
1153         } else if (!strcmp(opt_cmd_s, "kur")) {
1154             opt_cmd = CMP_KUR;
1155         } else if (!strcmp(opt_cmd_s, "cr")) {
1156             opt_cmd = CMP_CR;
1157         } else if (!strcmp(opt_cmd_s, "p10cr")) {
1158             opt_cmd = CMP_P10CR;
1159         } else if (!strcmp(opt_cmd_s, "rr")) {
1160             opt_cmd = CMP_RR;
1161         } else if (!strcmp(opt_cmd_s, "genm")) {
1162             opt_cmd = CMP_GENM;
1163         } else {
1164             CMP_err1("unknown cmp command '%s'", opt_cmd_s);
1165             return 0;
1166         }
1167     } else {
1168         CMP_err("no cmp command to execute");
1169         return 0;
1170     }
1171 
1172 #ifndef OPENSSL_NO_ENGINE
1173 #define FORMAT_OPTIONS (OPT_FMT_PEMDER | OPT_FMT_PKCS12 | OPT_FMT_ENGINE)
1174 #else
1175 #define FORMAT_OPTIONS (OPT_FMT_PEMDER | OPT_FMT_PKCS12)
1176 #endif
1177 
1178     if (opt_keyform_s != NULL
1179         && !opt_format(opt_keyform_s, FORMAT_OPTIONS, &opt_keyform)) {
1180         CMP_err("unknown option given for key loading format");
1181         return 0;
1182     }
1183 
1184 #undef FORMAT_OPTIONS
1185 
1186     if (opt_certform_s != NULL
1187         && !opt_format(opt_certform_s, OPT_FMT_PEMDER, &opt_certform)) {
1188         CMP_err("unknown option given for certificate storing format");
1189         return 0;
1190     }
1191     if (opt_crlform_s != NULL
1192         && !opt_format(opt_crlform_s, OPT_FMT_PEMDER, &opt_crlform)) {
1193         CMP_err("unknown option given for CRL storing format");
1194         return 0;
1195     }
1196 
1197     return 1;
1198 }
1199 
setup_srv_ctx(ENGINE * engine)1200 static OSSL_CMP_SRV_CTX *setup_srv_ctx(ENGINE *engine)
1201 {
1202     OSSL_CMP_CTX *ctx; /* extra CMP (client) ctx partly used by server */
1203     OSSL_CMP_SRV_CTX *srv_ctx = ossl_cmp_mock_srv_new(app_get0_libctx(),
1204         app_get0_propq());
1205 
1206     if (srv_ctx == NULL)
1207         return NULL;
1208     ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx);
1209 
1210     if (opt_srv_ref == NULL) {
1211         if (opt_srv_cert == NULL) {
1212             /* opt_srv_cert should determine the sender */
1213             CMP_err("must give -srv_ref for mock server if no -srv_cert given");
1214             goto err;
1215         }
1216     } else {
1217         if (!OSSL_CMP_CTX_set1_referenceValue(ctx, (unsigned char *)opt_srv_ref,
1218                 strlen(opt_srv_ref)))
1219             goto err;
1220     }
1221 
1222     if (opt_srv_secret != NULL) {
1223         int res;
1224         char *pass_str = get_passwd(opt_srv_secret, "PBMAC secret of mock server");
1225 
1226         if (pass_str != NULL) {
1227             cleanse(opt_srv_secret);
1228             res = OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)pass_str,
1229                 strlen(pass_str));
1230             clear_free(pass_str);
1231             if (res == 0)
1232                 goto err;
1233         }
1234     } else if (opt_srv_cert == NULL) {
1235         CMP_err("server credentials (-srv_secret or -srv_cert) must be given if -use_mock_srv or -port is used");
1236         goto err;
1237     } else {
1238         CMP_warn("server will not be able to handle PBM-protected requests since -srv_secret is not given");
1239     }
1240 
1241     if (opt_srv_secret == NULL
1242         && ((opt_srv_cert == NULL) != (opt_srv_key == NULL))) {
1243         CMP_err("must give both -srv_cert and -srv_key options or neither");
1244         goto err;
1245     }
1246     if (!setup_cert(ctx, opt_srv_cert, opt_srv_keypass,
1247             "signer certificate of the mock server",
1248             (add_X509_fn_t)OSSL_CMP_CTX_set1_cert))
1249         goto err;
1250     if (opt_srv_key != NULL) {
1251         EVP_PKEY *pkey = load_key_pwd(opt_srv_key, opt_keyform,
1252             opt_srv_keypass,
1253             engine, "private key for mock server cert");
1254 
1255         if (pkey == NULL || !OSSL_CMP_CTX_set1_pkey(ctx, pkey)) {
1256             EVP_PKEY_free(pkey);
1257             goto err;
1258         }
1259         EVP_PKEY_free(pkey);
1260     }
1261     cleanse(opt_srv_keypass);
1262 
1263     if (opt_srv_trusted != NULL) {
1264         X509_STORE *ts = load_trusted(opt_srv_trusted, 0, "certs trusted by mock server");
1265 
1266         if (ts == NULL || !OSSL_CMP_CTX_set0_trusted(ctx, ts)) {
1267             X509_STORE_free(ts);
1268             goto err;
1269         }
1270     } else {
1271         CMP_warn("mock server will not be able to handle signature-protected requests since -srv_trusted is not given");
1272     }
1273     if (!setup_certs(opt_srv_untrusted,
1274             "untrusted certificates for mock server", ctx,
1275             (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
1276         goto err;
1277 
1278     if (!setup_cert(srv_ctx, opt_ref_cert, opt_otherpass,
1279             "reference cert to be expected by the mock server",
1280             (add_X509_fn_t)ossl_cmp_mock_srv_set1_refCert))
1281         goto err;
1282     if (opt_rsp_cert == NULL) {
1283         CMP_warn("no -rsp_cert given for mock server");
1284     } else {
1285         if (!setup_cert(srv_ctx, opt_rsp_cert, opt_rsp_keypass,
1286                 "cert the mock server returns on certificate requests",
1287                 (add_X509_fn_t)ossl_cmp_mock_srv_set1_certOut))
1288             goto err;
1289     }
1290     if (opt_rsp_key != NULL) {
1291         EVP_PKEY *pkey = load_key_pwd(opt_rsp_key, opt_keyform,
1292             opt_rsp_keypass, engine,
1293             "private key for enrollment cert");
1294 
1295         if (pkey == NULL
1296             || !ossl_cmp_mock_srv_set1_keyOut(srv_ctx, pkey)) {
1297             EVP_PKEY_free(pkey);
1298             goto err;
1299         }
1300         EVP_PKEY_free(pkey);
1301     }
1302     cleanse(opt_rsp_keypass);
1303 
1304     if (!setup_mock_crlout(srv_ctx, opt_rsp_crl,
1305             "CRL to be returned by the mock server"))
1306         goto err;
1307     if (!setup_certs(opt_rsp_extracerts,
1308             "CMP extra certificates for mock server", srv_ctx,
1309             (add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_chainOut))
1310         goto err;
1311     if (!setup_certs(opt_rsp_capubs, "caPubs for mock server", srv_ctx,
1312             (add_X509_stack_fn_t)ossl_cmp_mock_srv_set1_caPubsOut))
1313         goto err;
1314     if (!setup_cert(srv_ctx, opt_rsp_newwithnew, opt_otherpass,
1315             "NewWithNew cert the mock server returns in rootCaKeyUpdate",
1316             (add_X509_fn_t)ossl_cmp_mock_srv_set1_newWithNew)
1317         || !setup_cert(srv_ctx, opt_rsp_newwithold, opt_otherpass,
1318             "NewWithOld cert the mock server returns in rootCaKeyUpdate",
1319             (add_X509_fn_t)ossl_cmp_mock_srv_set1_newWithOld)
1320         || !setup_cert(srv_ctx, opt_rsp_oldwithnew, opt_otherpass,
1321             "OldWithNew cert the mock server returns in rootCaKeyUpdate",
1322             (add_X509_fn_t)ossl_cmp_mock_srv_set1_oldWithNew))
1323         goto err;
1324     (void)ossl_cmp_mock_srv_set_pollCount(srv_ctx, opt_poll_count);
1325     (void)ossl_cmp_mock_srv_set_checkAfterTime(srv_ctx, opt_check_after);
1326     if (opt_grant_implicitconf)
1327         (void)OSSL_CMP_SRV_CTX_set_grant_implicit_confirm(srv_ctx, 1);
1328 
1329     if (opt_failure != INT_MIN) { /* option has been set explicitly */
1330         if (opt_failure < 0 || OSSL_CMP_PKIFAILUREINFO_MAX < opt_failure) {
1331             CMP_err1("-failure out of range, should be >= 0 and <= %d",
1332                 OSSL_CMP_PKIFAILUREINFO_MAX);
1333             goto err;
1334         }
1335         if (opt_failurebits != 0)
1336             CMP_warn("-failurebits overrides -failure");
1337         else
1338             opt_failurebits = 1 << opt_failure;
1339     }
1340     if ((unsigned)opt_failurebits > OSSL_CMP_PKIFAILUREINFO_MAX_BIT_PATTERN) {
1341         CMP_err("-failurebits out of range");
1342         goto err;
1343     }
1344     if (!ossl_cmp_mock_srv_set_statusInfo(srv_ctx, opt_pkistatus,
1345             opt_failurebits, opt_statusstring))
1346         goto err;
1347 
1348     if (opt_send_error)
1349         (void)ossl_cmp_mock_srv_set_sendError(srv_ctx, 1);
1350 
1351     if (opt_send_unprotected)
1352         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1);
1353     if (opt_send_unprot_err)
1354         (void)OSSL_CMP_SRV_CTX_set_send_unprotected_errors(srv_ctx, 1);
1355     if (opt_accept_unprotected)
1356         (void)OSSL_CMP_SRV_CTX_set_accept_unprotected(srv_ctx, 1);
1357     if (opt_accept_unprot_err)
1358         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
1359     if (opt_accept_raverified)
1360         (void)OSSL_CMP_SRV_CTX_set_accept_raverified(srv_ctx, 1);
1361 
1362     return srv_ctx;
1363 
1364 err:
1365     ossl_cmp_mock_srv_free(srv_ctx);
1366     return NULL;
1367 }
1368 
1369 /*
1370  * set up verification aspects of OSSL_CMP_CTX w.r.t. opts from config file/CLI.
1371  * Returns pointer on success, NULL on error
1372  */
setup_verification_ctx(OSSL_CMP_CTX * ctx)1373 static int setup_verification_ctx(OSSL_CMP_CTX *ctx)
1374 {
1375     if (!setup_certs(opt_untrusted, "untrusted certificates", ctx,
1376             (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_untrusted))
1377         return 0;
1378 
1379     if (opt_srvcert != NULL || opt_trusted != NULL) {
1380         if (opt_srvcert != NULL) {
1381             if (opt_trusted != NULL) {
1382                 CMP_warn("-trusted option is ignored since -srvcert option is present");
1383                 opt_trusted = NULL;
1384             }
1385             if (opt_recipient != NULL) {
1386                 CMP_warn("-recipient option is ignored since -srvcert option is present");
1387                 opt_recipient = NULL;
1388             }
1389             if (!setup_cert(ctx, opt_srvcert, opt_otherpass,
1390                     "directly trusted CMP server certificate",
1391                     (add_X509_fn_t)OSSL_CMP_CTX_set1_srvCert))
1392                 return 0;
1393         }
1394         if (opt_trusted != NULL) {
1395             X509_STORE *ts;
1396 
1397             /*
1398              * the 0 arg below clears any expected host/ip/email address;
1399              * opt_expect_sender is used instead
1400              */
1401             ts = load_trusted(opt_trusted, 0, "certs trusted by client");
1402 
1403             if (ts == NULL || !OSSL_CMP_CTX_set0_trusted(ctx, ts)) {
1404                 X509_STORE_free(ts);
1405                 return 0;
1406             }
1407         }
1408     }
1409 
1410     if (opt_unprotected_errors)
1411         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_ERRORS, 1);
1412 
1413     if (opt_out_trusted != NULL) { /* for use in OSSL_CMP_certConf_cb() */
1414         X509_VERIFY_PARAM *out_vpm = NULL;
1415         X509_STORE *out_trusted = load_trusted(opt_out_trusted, 1,
1416             "trusted certs for verifying newly enrolled cert");
1417 
1418         if (out_trusted == NULL)
1419             return 0;
1420         /* ignore any -attime here, new certs are current anyway */
1421         out_vpm = X509_STORE_get0_param(out_trusted);
1422         X509_VERIFY_PARAM_clear_flags(out_vpm, X509_V_FLAG_USE_CHECK_TIME);
1423 
1424         if (!OSSL_CMP_CTX_set_certConf_cb_arg(ctx, out_trusted)) {
1425             X509_STORE_free(out_trusted);
1426             return 0;
1427         }
1428     }
1429 
1430     if (opt_disable_confirm)
1431         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_DISABLE_CONFIRM, 1);
1432 
1433     if (opt_implicit_confirm)
1434         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_IMPLICIT_CONFIRM, 1);
1435 
1436     return 1;
1437 }
1438 
1439 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
1440 /*
1441  * set up ssl_ctx for the OSSL_CMP_CTX based on options from config file/CLI.
1442  * Returns pointer on success, NULL on error
1443  */
setup_ssl_ctx(OSSL_CMP_CTX * ctx,const char * host,ENGINE * engine)1444 static SSL_CTX *setup_ssl_ctx(OSSL_CMP_CTX *ctx, const char *host,
1445     ENGINE *engine)
1446 {
1447     STACK_OF(X509) *untrusted = OSSL_CMP_CTX_get0_untrusted(ctx);
1448     EVP_PKEY *pkey = NULL;
1449     X509_STORE *trust_store = NULL;
1450     SSL_CTX *ssl_ctx;
1451     int i;
1452 
1453     ssl_ctx = SSL_CTX_new(TLS_client_method());
1454     if (ssl_ctx == NULL)
1455         return NULL;
1456 
1457     if (opt_tls_trusted != NULL) {
1458         trust_store = load_trusted(opt_tls_trusted, 0, "trusted TLS certs");
1459         if (trust_store == NULL)
1460             goto err;
1461         SSL_CTX_set_cert_store(ssl_ctx, trust_store);
1462         SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);
1463     } else {
1464         CMP_warn("-tls_used given without -tls_trusted; will not authenticate the TLS server");
1465     }
1466 
1467     if (opt_tls_cert != NULL && opt_tls_key != NULL) {
1468         X509 *cert;
1469         STACK_OF(X509) *certs = NULL;
1470         int ok;
1471 
1472         if (!load_cert_certs(opt_tls_cert, &cert, &certs, 0, opt_tls_keypass,
1473                 "TLS client certificate (optionally with chain)",
1474                 vpm))
1475             /* need opt_tls_keypass if opt_tls_cert is encrypted PKCS#12 file */
1476             goto err;
1477 
1478         ok = SSL_CTX_use_certificate(ssl_ctx, cert) > 0;
1479         X509_free(cert);
1480 
1481         /*
1482          * Any further certs and any untrusted certs are used for constructing
1483          * the chain to be provided with the TLS client cert to the TLS server.
1484          */
1485         if (!ok || !SSL_CTX_set0_chain(ssl_ctx, certs)) {
1486             CMP_err1("unable to use client TLS certificate file '%s'",
1487                 opt_tls_cert);
1488             OSSL_STACK_OF_X509_free(certs);
1489             goto err;
1490         }
1491         for (i = 0; i < sk_X509_num(untrusted); i++) {
1492             cert = sk_X509_value(untrusted, i);
1493             if (!SSL_CTX_add1_chain_cert(ssl_ctx, cert)) {
1494                 CMP_err("could not add untrusted cert to TLS client cert chain");
1495                 goto err;
1496             }
1497         }
1498 
1499         {
1500             X509_VERIFY_PARAM *tls_vpm = NULL;
1501             unsigned long bak_flags = 0; /* compiler warns without init */
1502 
1503             if (trust_store != NULL) {
1504                 tls_vpm = X509_STORE_get0_param(trust_store);
1505                 bak_flags = X509_VERIFY_PARAM_get_flags(tls_vpm);
1506                 /* disable any cert status/revocation checking etc. */
1507                 X509_VERIFY_PARAM_clear_flags(tls_vpm,
1508                     ~(X509_V_FLAG_USE_CHECK_TIME
1509                         | X509_V_FLAG_NO_CHECK_TIME
1510                         | X509_V_FLAG_PARTIAL_CHAIN
1511                         | X509_V_FLAG_POLICY_CHECK));
1512             }
1513             CMP_debug("trying to build cert chain for own TLS cert");
1514             if (SSL_CTX_build_cert_chain(ssl_ctx,
1515                     SSL_BUILD_CHAIN_FLAG_UNTRUSTED | SSL_BUILD_CHAIN_FLAG_NO_ROOT)) {
1516                 CMP_debug("success building cert chain for own TLS cert");
1517             } else {
1518                 OSSL_CMP_CTX_print_errors(ctx);
1519                 CMP_warn("could not build cert chain for own TLS cert");
1520             }
1521             if (trust_store != NULL)
1522                 X509_VERIFY_PARAM_set_flags(tls_vpm, bak_flags);
1523         }
1524 
1525         /* If present we append to the list also the certs from opt_tls_extra */
1526         if (opt_tls_extra != NULL) {
1527             STACK_OF(X509) *tls_extra = load_certs_multifile(opt_tls_extra,
1528                 opt_otherpass,
1529                 "extra certificates for TLS",
1530                 vpm);
1531             int res = 1;
1532 
1533             if (tls_extra == NULL)
1534                 goto err;
1535             for (i = 0; i < sk_X509_num(tls_extra); i++) {
1536                 cert = sk_X509_value(tls_extra, i);
1537                 if (res != 0)
1538                     res = SSL_CTX_add_extra_chain_cert(ssl_ctx, cert);
1539                 if (res == 0)
1540                     X509_free(cert);
1541             }
1542             sk_X509_free(tls_extra);
1543             if (res == 0) {
1544                 BIO_printf(bio_err, "error: unable to add TLS extra certs\n");
1545                 goto err;
1546             }
1547         }
1548 
1549         pkey = load_key_pwd(opt_tls_key, opt_keyform, opt_tls_keypass,
1550             engine, "TLS client private key");
1551         cleanse(opt_tls_keypass);
1552         if (pkey == NULL)
1553             goto err;
1554         /*
1555          * verify the key matches the cert,
1556          * not using SSL_CTX_check_private_key(ssl_ctx)
1557          * because it gives poor and sometimes misleading diagnostics
1558          */
1559         if (!X509_check_private_key(SSL_CTX_get0_certificate(ssl_ctx),
1560                 pkey)) {
1561             CMP_err2("TLS private key '%s' does not match the TLS certificate '%s'\n",
1562                 opt_tls_key, opt_tls_cert);
1563             EVP_PKEY_free(pkey);
1564             pkey = NULL; /* otherwise, for some reason double free! */
1565             goto err;
1566         }
1567         if (SSL_CTX_use_PrivateKey(ssl_ctx, pkey) <= 0) {
1568             CMP_err1("unable to use TLS client private key '%s'", opt_tls_key);
1569             EVP_PKEY_free(pkey);
1570             pkey = NULL; /* otherwise, for some reason double free! */
1571             goto err;
1572         }
1573         EVP_PKEY_free(pkey); /* we do not need the handle any more */
1574     } else {
1575         CMP_warn("-tls_used given without -tls_key; cannot authenticate to the TLS server");
1576     }
1577     if (trust_store != NULL) {
1578         /*
1579          * Enable and parameterize server hostname/IP address check.
1580          * If we did this before checking our own TLS cert
1581          * the expected hostname would mislead the check.
1582          */
1583         if (!truststore_set_host_etc(trust_store,
1584                 opt_tls_host != NULL ? opt_tls_host : host))
1585             goto err;
1586     }
1587     return ssl_ctx;
1588 err:
1589     SSL_CTX_free(ssl_ctx);
1590     return NULL;
1591 }
1592 #endif /* OPENSSL_NO_SOCK */
1593 
1594 /*
1595  * set up protection aspects of OSSL_CMP_CTX based on options from config
1596  * file/CLI while parsing options and checking their consistency.
1597  * Returns 1 on success, 0 on error
1598  */
setup_protection_ctx(OSSL_CMP_CTX * ctx,ENGINE * engine)1599 static int setup_protection_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
1600 {
1601     if (!opt_unprotected_requests && opt_secret == NULL && opt_key == NULL) {
1602         CMP_err("must give -key or -secret unless -unprotected_requests is used");
1603         return 0;
1604     }
1605 
1606     if (opt_ref == NULL && opt_cert == NULL && opt_subject == NULL) {
1607         /* cert or subject should determine the sender */
1608         CMP_err("must give -ref if no -cert and no -subject given");
1609         return 0;
1610     }
1611     if (opt_secret == NULL && ((opt_cert == NULL) != (opt_key == NULL))) {
1612         CMP_err("must give both -cert and -key options or neither");
1613         return 0;
1614     }
1615     if (opt_secret != NULL) {
1616         char *pass_string = get_passwd(opt_secret, "PBMAC");
1617         int res;
1618 
1619         if (pass_string != NULL) {
1620             cleanse(opt_secret);
1621             res = OSSL_CMP_CTX_set1_secretValue(ctx,
1622                 (unsigned char *)pass_string,
1623                 strlen(pass_string));
1624             clear_free(pass_string);
1625             if (res == 0)
1626                 return 0;
1627         }
1628         if (opt_cert != NULL || opt_key != NULL)
1629             CMP_warn("-cert and -key not used for protection since -secret is given");
1630     }
1631     if (opt_ref != NULL
1632         && !OSSL_CMP_CTX_set1_referenceValue(ctx, (unsigned char *)opt_ref,
1633             strlen(opt_ref)))
1634         return 0;
1635 
1636     if (opt_key != NULL) {
1637         EVP_PKEY *pkey = load_key_pwd(opt_key, opt_keyform, opt_keypass, engine,
1638             "private key for CMP client certificate");
1639 
1640         if (pkey == NULL || !OSSL_CMP_CTX_set1_pkey(ctx, pkey)) {
1641             EVP_PKEY_free(pkey);
1642             return 0;
1643         }
1644         EVP_PKEY_free(pkey);
1645     }
1646     if (opt_secret == NULL && opt_srvcert == NULL && opt_trusted == NULL)
1647         CMP_warn("will not authenticate server due to missing -secret, -trusted, or -srvcert");
1648 
1649     if (opt_cert != NULL) {
1650         X509 *cert;
1651         STACK_OF(X509) *certs = NULL;
1652         X509_STORE *own_trusted = NULL;
1653         int ok;
1654 
1655         if (!load_cert_certs(opt_cert, &cert, &certs, 0, opt_keypass,
1656                 "CMP client certificate (optionally with chain)",
1657                 vpm))
1658             /* opt_keypass is needed if opt_cert is an encrypted PKCS#12 file */
1659             return 0;
1660         ok = OSSL_CMP_CTX_set1_cert(ctx, cert);
1661         X509_free(cert);
1662         if (!ok) {
1663             CMP_err("out of memory");
1664         } else {
1665             if (opt_own_trusted != NULL) {
1666                 own_trusted = load_trusted(opt_own_trusted, 0,
1667                     "trusted certs for verifying own CMP signer cert");
1668                 ok = own_trusted != NULL;
1669             }
1670             ok = ok && OSSL_CMP_CTX_build_cert_chain(ctx, own_trusted, certs);
1671         }
1672         X509_STORE_free(own_trusted);
1673         OSSL_STACK_OF_X509_free(certs);
1674         if (!ok)
1675             return 0;
1676     } else if (opt_own_trusted != NULL) {
1677         CMP_warn("-own_trusted option is ignored without -cert");
1678     }
1679 
1680     if (!setup_certs(opt_extracerts, "extra certificates for CMP", ctx,
1681             (add_X509_stack_fn_t)OSSL_CMP_CTX_set1_extraCertsOut))
1682         return 0;
1683     cleanse(opt_otherpass);
1684 
1685     if (opt_unprotected_requests)
1686         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_UNPROTECTED_SEND, 1);
1687 
1688     if (opt_digest != NULL) {
1689         int digest = OBJ_ln2nid(opt_digest);
1690 
1691         if (digest == NID_undef) {
1692             CMP_err1("digest algorithm name not recognized: '%s'", opt_digest);
1693             return 0;
1694         }
1695         if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_DIGEST_ALGNID, digest)
1696             || !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_OWF_ALGNID, digest)) {
1697             CMP_err1("digest algorithm name not supported: '%s'", opt_digest);
1698             return 0;
1699         }
1700     }
1701 
1702     if (opt_mac != NULL) {
1703         int mac = OBJ_ln2nid(opt_mac);
1704 
1705         if (mac == NID_undef) {
1706             CMP_err1("MAC algorithm name not recognized: '%s'", opt_mac);
1707             return 0;
1708         }
1709         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_MAC_ALGNID, mac);
1710     }
1711     return 1;
1712 }
1713 
set_fallback_pubkey(OSSL_CMP_CTX * ctx)1714 static int set_fallback_pubkey(OSSL_CMP_CTX *ctx)
1715 {
1716     char *file = opt_reqin, *end = file, bak;
1717     OSSL_CMP_MSG *req;
1718     const X509_PUBKEY *pubkey;
1719     EVP_PKEY *pkey;
1720     EVP_PKEY *pkey1;
1721     int res = 0;
1722 
1723     /* temporarily separate first file name in opt_reqin */
1724     while (*end != ',' && !isspace(_UC(*end)) && *end != '\0')
1725         end++;
1726     bak = *end;
1727     *end = '\0';
1728     req = OSSL_CMP_MSG_read(file, app_get0_libctx(), app_get0_propq());
1729     *end = bak;
1730 
1731     if (req == NULL) {
1732         CMP_err1("failed to load ir/cr/kur file '%s' attempting to get fallback public key",
1733             file);
1734         return 0;
1735     }
1736     if ((pubkey = OSSL_CMP_MSG_get0_certreq_publickey(req)) == NULL
1737         || (pkey = X509_PUBKEY_get0(pubkey)) == NULL) {
1738         CMP_err1("failed to get fallback public key from ir/cr/kur file '%s'",
1739             file);
1740         goto err;
1741     }
1742     pkey1 = EVP_PKEY_dup(pkey);
1743     if (pkey == NULL || !OSSL_CMP_CTX_set0_newPkey(ctx, 0 /* priv */, pkey1)) {
1744         EVP_PKEY_free(pkey1);
1745         CMP_err1("failed to get fallback public key obtained from ir/cr/kur file '%s'",
1746             file);
1747         goto err;
1748     }
1749     res = 1;
1750 
1751 err:
1752     OSSL_CMP_MSG_free(req);
1753     return res;
1754 }
1755 
1756 /*
1757  * Set up IR/CR/P10CR/KUR/CertConf/RR/GENM specific parts of the OSSL_CMP_CTX
1758  * based on options from CLI and/or config file.
1759  * Returns 1 on success, 0 on error
1760  */
setup_request_ctx(OSSL_CMP_CTX * ctx,ENGINE * engine)1761 static int setup_request_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
1762 {
1763     X509_REQ *csr = NULL;
1764     X509_EXTENSIONS *exts = NULL;
1765     X509V3_CTX ext_ctx;
1766 
1767     if (opt_subject == NULL
1768         && opt_csr == NULL && opt_oldcert == NULL && opt_cert == NULL
1769         && opt_cmd != CMP_RR && opt_cmd != CMP_GENM)
1770         CMP_warn("no -subject given; no -csr or -oldcert or -cert available for fallback");
1771 
1772     if (!set_name(opt_issuer, OSSL_CMP_CTX_set1_issuer, ctx, "issuer"))
1773         return 0;
1774     if (opt_cmd == CMP_IR || opt_cmd == CMP_CR || opt_cmd == CMP_KUR) {
1775         if (opt_reqin == NULL && opt_newkey == NULL && !opt_centralkeygen
1776             && opt_key == NULL && opt_csr == NULL && opt_oldcert == NULL) {
1777             CMP_err("missing -newkey (or -key) to be certified and no -csr, -oldcert, -cert, or -reqin option given, which could provide fallback public key."
1778                     " Neither central key generation is requested.");
1779             return 0;
1780         }
1781         if (opt_popo == OSSL_CRMF_POPO_NONE && !opt_centralkeygen) {
1782             CMP_info("POPO is disabled, which implies -centralkeygen");
1783             opt_centralkeygen = 1;
1784         }
1785         if (opt_centralkeygen) {
1786             if (opt_popo > OSSL_CRMF_POPO_NONE) {
1787                 CMP_err1("-popo value %d is inconsistent with -centralkeygen", opt_popo);
1788                 return 0;
1789             }
1790             if (opt_newkeyout == NULL) {
1791                 CMP_err("-newkeyout not given, nowhere to save centrally generated key");
1792                 return 0;
1793             }
1794             opt_popo = OSSL_CRMF_POPO_NONE;
1795         }
1796         if (opt_newkey == NULL
1797             && opt_popo != OSSL_CRMF_POPO_NONE
1798             && opt_popo != OSSL_CRMF_POPO_RAVERIFIED) {
1799             if (opt_csr != NULL) {
1800                 CMP_err1("no -newkey option given with private key for POPO, -csr option provides just public key%s",
1801                     opt_key == NULL ? "" : ", and -key option superseded by -csr");
1802                 if (opt_reqin != NULL)
1803                     CMP_info("since -reqin is used, may use -popo -1 or -popo 0 to disable the needless generation of a POPO");
1804                 return 0;
1805             }
1806             if (opt_key == NULL) {
1807                 CMP_err("missing -newkey (or -key) option for key to be certified and for POPO");
1808                 return 0;
1809             }
1810         }
1811         if (opt_certout == NULL && opt_reqout_only == NULL) {
1812             CMP_err("-certout not given, nowhere to save newly enrolled certificate");
1813             return 0;
1814         }
1815         if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject"))
1816             return 0;
1817     } else {
1818         const char *msg = "option is ignored for commands other than 'ir', 'cr', and 'kur'";
1819 
1820         if (opt_subject != NULL) {
1821             if (opt_ref == NULL && opt_cert == NULL) {
1822                 /* will use subject as sender unless oldcert subject is used */
1823                 if (!set_name(opt_subject, OSSL_CMP_CTX_set1_subjectName, ctx, "subject"))
1824                     return 0;
1825             } else {
1826                 CMP_warn1("-subject %s since sender is taken from -ref or -cert",
1827                     msg);
1828             }
1829         }
1830         if (opt_issuer != NULL && opt_cmd != CMP_RR)
1831             CMP_warn1("-issuer %s and 'rr'", msg);
1832         if (opt_reqexts != NULL)
1833             CMP_warn1("-reqexts %s", msg);
1834         if (opt_san_nodefault)
1835             CMP_warn1("-san_nodefault %s", msg);
1836         if (opt_sans != NULL)
1837             CMP_warn1("-sans %s", msg);
1838         if (opt_policies != NULL)
1839             CMP_warn1("-policies %s", msg);
1840         if (opt_policy_oids != NULL)
1841             CMP_warn1("-policy_oids %s", msg);
1842         if (opt_popo != OSSL_CRMF_POPO_NONE - 1)
1843             CMP_warn1("-popo %s", msg);
1844         if (opt_centralkeygen)
1845             CMP_warn1("-popo -1 or -centralkeygen %s", msg);
1846         if (opt_newkeyout != NULL)
1847             CMP_warn1("-newkeyout %s", msg);
1848         if (opt_cmd != CMP_P10CR) {
1849             if (opt_implicit_confirm)
1850                 CMP_warn1("-implicit_confirm %s, and 'p10cr'", msg);
1851             if (opt_disable_confirm)
1852                 CMP_warn1("-disable_confirm %s, and 'p10cr'", msg);
1853             if (opt_certout != NULL)
1854                 CMP_warn1("-certout %s, and 'p10cr'", msg);
1855             if (opt_chainout != NULL)
1856                 CMP_warn1("-chainout %s, and 'p10cr'", msg);
1857         }
1858     }
1859     if (opt_cmd == CMP_KUR) {
1860         char *ref_cert = opt_oldcert != NULL ? opt_oldcert : opt_cert;
1861 
1862         if (ref_cert == NULL && opt_csr == NULL) {
1863             CMP_err("missing -oldcert for certificate to be updated and no -csr given");
1864             return 0;
1865         }
1866         if (opt_subject != NULL)
1867             CMP_warn2("given -subject '%s' overrides the subject of '%s' for KUR",
1868                 opt_subject, ref_cert != NULL ? ref_cert : opt_csr);
1869     }
1870     if (opt_cmd == CMP_RR) {
1871         if (opt_issuer == NULL && opt_serial == NULL) {
1872             if (opt_oldcert == NULL && opt_csr == NULL) {
1873                 CMP_err("missing -oldcert or -issuer and -serial for certificate to be revoked and no -csr given");
1874                 return 0;
1875             }
1876             if (opt_oldcert != NULL && opt_csr != NULL)
1877                 CMP_warn("ignoring -csr since certificate to be revoked is given");
1878         } else {
1879 #define OSSL_CMP_RR_MSG "since -issuer and -serial is given for command 'rr'"
1880             if (opt_issuer == NULL || opt_serial == NULL) {
1881                 CMP_err("Must give both -issuer and -serial options or neither");
1882                 return 0;
1883             }
1884             if (opt_oldcert != NULL)
1885                 CMP_warn("Ignoring -oldcert " OSSL_CMP_RR_MSG);
1886             if (opt_csr != NULL)
1887                 CMP_warn("Ignoring -csr " OSSL_CMP_RR_MSG);
1888         }
1889         if (opt_serial != NULL) {
1890             ASN1_INTEGER *sno;
1891 
1892             if ((sno = s2i_ASN1_INTEGER(NULL, opt_serial)) == NULL) {
1893                 CMP_err1("cannot read serial number: '%s'", opt_serial);
1894                 return 0;
1895             }
1896             if (!OSSL_CMP_CTX_set1_serialNumber(ctx, sno)) {
1897                 ASN1_INTEGER_free(sno);
1898                 CMP_err("out of memory");
1899                 return 0;
1900             }
1901             ASN1_INTEGER_free(sno);
1902         }
1903         if (opt_revreason > CRL_REASON_NONE)
1904             (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_REVOCATION_REASON,
1905                 opt_revreason);
1906     } else {
1907         if (opt_serial != NULL)
1908             CMP_warn("Ignoring -serial for command other than 'rr'");
1909     }
1910     if (opt_cmd == CMP_P10CR && opt_csr == NULL) {
1911         CMP_err("missing PKCS#10 CSR for p10cr");
1912         return 0;
1913     }
1914 
1915     if (opt_recipient == NULL && opt_srvcert == NULL && opt_issuer == NULL
1916         && opt_oldcert == NULL && opt_cert == NULL)
1917         CMP_warn("missing -recipient, -srvcert, -issuer, -oldcert or -cert; recipient for any requests not covered by -reqin will be set to \"NULL-DN\"");
1918 
1919     if (opt_cmd == CMP_P10CR || opt_cmd == CMP_RR || opt_cmd == CMP_GENM) {
1920         const char *msg = "option is ignored for 'p10cr', 'rr', and 'genm' commands";
1921 
1922         if (opt_newkeypass != NULL)
1923             CMP_warn1("-newkeypass %s", msg);
1924         if (opt_newkey != NULL)
1925             CMP_warn1("-newkey %s", msg);
1926         if (opt_days != 0)
1927             CMP_warn1("-days %s", msg);
1928         if (opt_popo != OSSL_CRMF_POPO_NONE - 1)
1929             CMP_warn1("-popo %s", msg);
1930         if (opt_out_trusted != NULL)
1931             CMP_warn1("-out_trusted %s", msg);
1932     } else if (opt_newkey != NULL) {
1933         const char *file = opt_newkey;
1934         const int format = opt_keyform;
1935         const char *pass = opt_newkeypass;
1936         const char *desc = "new private key for cert to be enrolled";
1937         EVP_PKEY *pkey;
1938         int priv = 1;
1939         BIO *bio_bak = bio_err;
1940 
1941         bio_err = NULL; /* suppress diagnostics on first try loading key */
1942         pkey = load_key_pwd(file, format, pass, engine, desc);
1943         bio_err = bio_bak;
1944         if (pkey == NULL) {
1945             ERR_clear_error();
1946             desc = opt_csr == NULL
1947                 ? "fallback public key for cert to be enrolled"
1948                 : "public key for checking cert resulting from p10cr";
1949             pkey = load_pubkey(file, format, 0, pass, engine, desc);
1950             priv = 0;
1951         }
1952 
1953         if (pkey == NULL || !OSSL_CMP_CTX_set0_newPkey(ctx, priv, pkey)) {
1954             EVP_PKEY_free(pkey);
1955             return 0;
1956         }
1957     } else if (opt_reqin != NULL
1958         && opt_key == NULL && opt_csr == NULL && opt_oldcert == NULL
1959         && !opt_centralkeygen) {
1960         if (!set_fallback_pubkey(ctx))
1961             return 0;
1962     }
1963 
1964     if (opt_days > 0
1965         && !OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_VALIDITY_DAYS,
1966             opt_days)) {
1967         CMP_err("could not set requested cert validity period");
1968         return 0;
1969     }
1970 
1971     if (opt_policies != NULL && opt_policy_oids != NULL) {
1972         CMP_err("cannot have policies both via -policies and via -policy_oids");
1973         return 0;
1974     }
1975 
1976     if (opt_csr != NULL) {
1977         if (opt_cmd == CMP_GENM) {
1978             CMP_warn("-csr option is ignored for 'genm' command");
1979         } else {
1980             csr = load_csr_autofmt(opt_csr, FORMAT_UNDEF, NULL, "PKCS#10 CSR");
1981             if (csr == NULL)
1982                 return 0;
1983             if (!OSSL_CMP_CTX_set1_p10CSR(ctx, csr))
1984                 goto oom;
1985         }
1986     }
1987     if (opt_reqexts != NULL || opt_policies != NULL) {
1988         if ((exts = sk_X509_EXTENSION_new_null()) == NULL)
1989             goto oom;
1990         X509V3_set_ctx(&ext_ctx, NULL, NULL, csr, NULL, X509V3_CTX_REPLACE);
1991         X509V3_set_nconf(&ext_ctx, conf);
1992         if (opt_reqexts != NULL
1993             && !X509V3_EXT_add_nconf_sk(conf, &ext_ctx, opt_reqexts, &exts)) {
1994             CMP_err1("cannot load certificate request extension section '%s'",
1995                 opt_reqexts);
1996             goto exts_err;
1997         }
1998         if (opt_policies != NULL
1999             && !X509V3_EXT_add_nconf_sk(conf, &ext_ctx, opt_policies, &exts)) {
2000             CMP_err1("cannot load policy cert request extension section '%s'",
2001                 opt_policies);
2002             goto exts_err;
2003         }
2004         OSSL_CMP_CTX_set0_reqExtensions(ctx, exts);
2005     }
2006     X509_REQ_free(csr);
2007     /* After here, must not goto oom/exts_err */
2008 
2009     if (OSSL_CMP_CTX_reqExtensions_have_SAN(ctx) && opt_sans != NULL) {
2010         CMP_err("cannot have Subject Alternative Names both via -reqexts and via -sans");
2011         return 0;
2012     }
2013     if (!set_gennames(ctx, opt_sans, "Subject Alternative Name"))
2014         return 0;
2015 
2016     if (opt_san_nodefault) {
2017         if (opt_sans != NULL)
2018             CMP_warn("-opt_san_nodefault has no effect when -sans is used");
2019         (void)OSSL_CMP_CTX_set_option(ctx,
2020             OSSL_CMP_OPT_SUBJECTALTNAME_NODEFAULT, 1);
2021     }
2022 
2023     if (opt_policy_oids_critical) {
2024         if (opt_policy_oids == NULL)
2025             CMP_warn("-opt_policy_oids_critical has no effect unless -policy_oids is given");
2026         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POLICIES_CRITICAL, 1);
2027     }
2028 
2029     while (opt_policy_oids != NULL) {
2030         ASN1_OBJECT *policy;
2031         POLICYINFO *pinfo;
2032         char *next = next_item(opt_policy_oids);
2033 
2034         if ((policy = OBJ_txt2obj(opt_policy_oids, 1)) == 0) {
2035             CMP_err1("Invalid -policy_oids arg '%s'", opt_policy_oids);
2036             return 0;
2037         }
2038         if (OBJ_obj2nid(policy) == NID_undef)
2039             CMP_warn1("Unknown -policy_oids arg: %.40s", opt_policy_oids);
2040 
2041         if ((pinfo = POLICYINFO_new()) == NULL) {
2042             ASN1_OBJECT_free(policy);
2043             return 0;
2044         }
2045         pinfo->policyid = policy;
2046 
2047         if (!OSSL_CMP_CTX_push0_policy(ctx, pinfo)) {
2048             CMP_err1("cannot add policy with OID '%s'", opt_policy_oids);
2049             POLICYINFO_free(pinfo);
2050             return 0;
2051         }
2052         opt_policy_oids = next;
2053     }
2054     if (opt_popo >= OSSL_CRMF_POPO_NONE)
2055         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_POPO_METHOD, opt_popo);
2056 
2057     if (opt_oldcert != NULL) {
2058         if (opt_cmd == CMP_GENM) {
2059             CMP_warn("-oldcert option is ignored for 'genm' command");
2060         } else {
2061             if (!setup_cert(ctx, opt_oldcert, opt_keypass,
2062                     /* needed if opt_oldcert is encrypted PKCS12 file */
2063                     opt_cmd == CMP_KUR ? "certificate to be updated" : opt_cmd == CMP_RR ? "certificate to be revoked"
2064                                                                                          : "reference certificate (oldcert)",
2065                     (add_X509_fn_t)OSSL_CMP_CTX_set1_oldCert))
2066                 return 0;
2067         }
2068     }
2069     cleanse(opt_keypass);
2070 
2071     return 1;
2072 
2073 oom:
2074     CMP_err("out of memory");
2075 exts_err:
2076     sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
2077     X509_REQ_free(csr);
2078     return 0;
2079 }
2080 
add_certProfile(OSSL_CMP_CTX * ctx,const char * name)2081 static int add_certProfile(OSSL_CMP_CTX *ctx, const char *name)
2082 {
2083     OSSL_CMP_ITAV *itav = NULL;
2084     STACK_OF(ASN1_UTF8STRING) *sk;
2085     ASN1_UTF8STRING *utf8string;
2086 
2087     if (ctx == NULL || name == NULL)
2088         return 0;
2089 
2090     if ((sk = sk_ASN1_UTF8STRING_new_reserve(NULL, 1)) == NULL)
2091         return 0;
2092     if ((utf8string = ASN1_UTF8STRING_new()) == NULL)
2093         goto err;
2094     if (!ASN1_STRING_set(utf8string, name, (int)strlen(name))) {
2095         ASN1_STRING_free(utf8string);
2096         goto err;
2097     }
2098     /* Due to sk_ASN1_UTF8STRING_new_reserve(NULL, 1), this surely succeeds: */
2099     (void)sk_ASN1_UTF8STRING_push(sk, utf8string);
2100     if ((itav = OSSL_CMP_ITAV_new0_certProfile(sk)) == NULL)
2101         goto err;
2102     if (OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav))
2103         return 1;
2104     OSSL_CMP_ITAV_free(itav);
2105     return 0;
2106 
2107 err:
2108     sk_ASN1_UTF8STRING_pop_free(sk, ASN1_UTF8STRING_free);
2109     return 0;
2110 }
2111 
handle_opt_geninfo(OSSL_CMP_CTX * ctx)2112 static int handle_opt_geninfo(OSSL_CMP_CTX *ctx)
2113 {
2114     ASN1_OBJECT *obj = NULL;
2115     ASN1_TYPE *type = NULL;
2116     long value;
2117     ASN1_INTEGER *aint = NULL;
2118     ASN1_UTF8STRING *text = NULL;
2119     OSSL_CMP_ITAV *itav;
2120     char *ptr = opt_geninfo, *oid, *end;
2121 
2122     do {
2123         while (isspace(_UC(*ptr)))
2124             ptr++;
2125         oid = ptr;
2126         if ((ptr = strchr(oid, ':')) == NULL) {
2127             CMP_err1("Missing ':' in -geninfo arg %.40s", oid);
2128             return 0;
2129         }
2130         *ptr++ = '\0';
2131         if ((obj = OBJ_txt2obj(oid, 0)) == NULL) {
2132             CMP_err1("Invalid OID in -geninfo arg %.40s", oid);
2133             return 0;
2134         }
2135         if (OBJ_obj2nid(obj) == NID_undef)
2136             CMP_warn1("Unknown OID in -geninfo arg: %.40s", oid);
2137         if ((type = ASN1_TYPE_new()) == NULL)
2138             goto oom;
2139 
2140         if (CHECK_AND_SKIP_CASE_PREFIX(ptr, "int:")) {
2141             value = strtol(ptr, &end, 10);
2142             if (end == ptr) {
2143                 CMP_err1("Cannot parse int in -geninfo arg %.40s", ptr);
2144                 goto err;
2145             }
2146             ptr = end;
2147             if (*ptr != '\0') {
2148                 if (*ptr != ',') {
2149                     CMP_err1("Missing ',' or end of -geninfo arg after int at %.40s",
2150                         ptr);
2151                     goto err;
2152                 }
2153                 ptr++;
2154             }
2155 
2156             if ((aint = ASN1_INTEGER_new()) == NULL
2157                 || !ASN1_INTEGER_set(aint, value))
2158                 goto oom;
2159             ASN1_TYPE_set(type, V_ASN1_INTEGER, aint);
2160             aint = NULL;
2161 
2162         } else if (CHECK_AND_SKIP_CASE_PREFIX(ptr, "str:")) {
2163             end = strchr(ptr, ',');
2164             if (end == NULL)
2165                 end = ptr + strlen(ptr);
2166             else
2167                 *end++ = '\0';
2168             if ((text = ASN1_UTF8STRING_new()) == NULL
2169                 || !ASN1_STRING_set(text, ptr, -1))
2170                 goto oom;
2171             ptr = end;
2172             ASN1_TYPE_set(type, V_ASN1_UTF8STRING, text);
2173             text = NULL;
2174 
2175         } else {
2176             CMP_err1("Missing 'int:' or 'str:' in -geninfo arg %.40s", ptr);
2177             goto err;
2178         }
2179 
2180         if ((itav = OSSL_CMP_ITAV_create(obj, type)) == NULL) {
2181             CMP_err("Unable to create 'OSSL_CMP_ITAV' structure");
2182             goto err;
2183         }
2184         obj = NULL;
2185         type = NULL;
2186 
2187         if (!OSSL_CMP_CTX_push0_geninfo_ITAV(ctx, itav)) {
2188             CMP_err("Failed to add ITAV for geninfo of the PKI message header");
2189             OSSL_CMP_ITAV_free(itav);
2190             return 0;
2191         }
2192     } while (*ptr != '\0');
2193     return 1;
2194 
2195 oom:
2196     CMP_err("out of memory");
2197 err:
2198     ASN1_OBJECT_free(obj);
2199     ASN1_TYPE_free(type);
2200     ASN1_INTEGER_free(aint);
2201     ASN1_UTF8STRING_free(text);
2202     return 0;
2203 }
2204 
2205 /*
2206  * set up the client-side OSSL_CMP_CTX based on options from config file/CLI
2207  * while parsing options and checking their consistency.
2208  * Prints reason for error to bio_err.
2209  * Returns 1 on success, 0 on error
2210  */
setup_client_ctx(OSSL_CMP_CTX * ctx,ENGINE * engine)2211 static int setup_client_ctx(OSSL_CMP_CTX *ctx, ENGINE *engine)
2212 {
2213     int ret = 0;
2214     char *host = NULL, *port = NULL, *path = NULL, *used_path = opt_path;
2215 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
2216     int portnum, use_ssl;
2217     static char server_port[32] = { '\0' };
2218     const char *proxy_host = NULL;
2219 #endif
2220     char server_buf[200] = "mock server";
2221     char proxy_buf[200] = "";
2222 
2223     if (!opt_use_mock_srv)
2224         strcpy(server_buf, "no server");
2225     if (!opt_use_mock_srv && opt_rspin == NULL) { /* note: -port is not given */
2226 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
2227         if (opt_server == NULL && opt_reqout_only == NULL) {
2228             CMP_err("missing -server or -use_mock_srv or -rspin option");
2229             goto err;
2230         }
2231 #else
2232         CMP_err("missing -use_mock_srv or -rspin option; -server option is not supported due to no-sock build");
2233         goto err;
2234 #endif
2235     }
2236 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
2237     if (opt_server == NULL) {
2238         if (opt_proxy != NULL)
2239             CMP_warn("ignoring -proxy option since -server is not given");
2240         if (opt_no_proxy != NULL)
2241             CMP_warn("ignoring -no_proxy option since -server is not given");
2242         goto set_path;
2243     }
2244     if (!OSSL_HTTP_parse_url(opt_server, &use_ssl, NULL /* user */,
2245             &host, &port, &portnum,
2246             &path, NULL /* q */, NULL /* frag */)) {
2247         CMP_err1("cannot parse -server URL: %s", opt_server);
2248         goto err;
2249     }
2250     if (use_ssl && !opt_tls_used) {
2251         CMP_warn("assuming -tls_used since -server URL indicates HTTPS");
2252         opt_tls_used = 1;
2253     }
2254     if (!OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_USE_TLS, opt_tls_used))
2255         goto err;
2256 
2257     BIO_snprintf(server_port, sizeof(server_port), "%s", port);
2258     if (opt_path == NULL)
2259         used_path = path;
2260     if (!OSSL_CMP_CTX_set1_server(ctx, host)
2261         || !OSSL_CMP_CTX_set_serverPort(ctx, portnum))
2262         goto oom;
2263     if (opt_proxy != NULL && !OSSL_CMP_CTX_set1_proxy(ctx, opt_proxy))
2264         goto oom;
2265     if (opt_no_proxy != NULL && !OSSL_CMP_CTX_set1_no_proxy(ctx, opt_no_proxy))
2266         goto oom;
2267     (void)BIO_snprintf(server_buf, sizeof(server_buf), "http%s://%s:%s/%s",
2268         opt_tls_used ? "s" : "", host, port,
2269         *used_path == '/' ? used_path + 1 : used_path);
2270 
2271     proxy_host = OSSL_HTTP_adapt_proxy(opt_proxy, opt_no_proxy, host, use_ssl);
2272     if (proxy_host != NULL)
2273         (void)BIO_snprintf(proxy_buf, sizeof(proxy_buf), " via %s", proxy_host);
2274 
2275 set_path:
2276 #endif
2277 
2278     if (!OSSL_CMP_CTX_set1_serverPath(ctx, used_path))
2279         goto oom;
2280     if (!transform_opts())
2281         goto err;
2282 
2283     if (opt_infotype_s == NULL) {
2284         if (opt_cmd == CMP_GENM)
2285             CMP_warn("no -infotype option given for genm");
2286     } else if (opt_cmd != CMP_GENM) {
2287         CMP_warn("-infotype option is ignored for commands other than 'genm'");
2288     } else {
2289         char id_buf[100] = "id-it-";
2290 
2291         strncat(id_buf, opt_infotype_s, sizeof(id_buf) - strlen(id_buf) - 1);
2292         if ((opt_infotype = OBJ_sn2nid(id_buf)) == NID_undef) {
2293             CMP_err("unknown OID name in -infotype option");
2294             goto err;
2295         }
2296     }
2297     if (opt_cmd != CMP_GENM || opt_infotype != NID_id_it_rootCaCert) {
2298         const char *msg = "option is ignored unless -cmd 'genm' and -infotype rootCaCert is given";
2299 
2300         if (opt_oldwithold != NULL)
2301             CMP_warn1("-oldwithold %s", msg);
2302         if (opt_newwithnew != NULL)
2303             CMP_warn1("-newwithnew %s", msg);
2304         if (opt_newwithold != NULL)
2305             CMP_warn1("-newwithold %s", msg);
2306         if (opt_oldwithnew != NULL)
2307             CMP_warn1("-oldwithnew %s", msg);
2308     }
2309     if (opt_cmd != CMP_GENM || opt_infotype != NID_id_it_certReqTemplate) {
2310         const char *msg = "option is ignored unless -cmd 'genm' and -infotype 'certReqTemplate' is given";
2311 
2312         if (opt_template != NULL)
2313             CMP_warn1("-template %s", msg);
2314         if (opt_keyspec != NULL)
2315             CMP_warn1("-keyspec %s", msg);
2316     } else {
2317         if (opt_template == NULL)
2318             CMP_err("missing -template option for genm with infotype certReqTemplate");
2319     }
2320 
2321     if (!setup_verification_ctx(ctx))
2322         goto err;
2323 
2324     if (opt_keep_alive != 1)
2325         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_KEEP_ALIVE,
2326             opt_keep_alive);
2327     if (opt_total_timeout > 0 && opt_msg_timeout > 0
2328         && opt_total_timeout < opt_msg_timeout) {
2329         CMP_err2("-total_timeout argument = %d must not be < %d (-msg_timeout)",
2330             opt_total_timeout, opt_msg_timeout);
2331         goto err;
2332     }
2333     if (opt_msg_timeout >= 0) /* must do this before setup_ssl_ctx() */
2334         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_MSG_TIMEOUT,
2335             opt_msg_timeout);
2336     if (opt_total_timeout >= 0)
2337         (void)OSSL_CMP_CTX_set_option(ctx, OSSL_CMP_OPT_TOTAL_TIMEOUT,
2338             opt_total_timeout);
2339 
2340     if (opt_rspin != NULL) {
2341         rspin_in_use = 1;
2342         if (opt_reqin != NULL)
2343             CMP_warn("-reqin is ignored since -rspin is present");
2344     }
2345     if (opt_reqin_new_tid && opt_reqin == NULL)
2346         CMP_warn("-reqin_new_tid is ignored since -reqin is not present");
2347     if (opt_reqin != NULL || opt_reqout != NULL
2348         || opt_rspin != NULL || opt_rspout != NULL || opt_use_mock_srv)
2349         (void)OSSL_CMP_CTX_set_transfer_cb(ctx, read_write_req_resp);
2350 
2351 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
2352     if (opt_tls_used) {
2353         APP_HTTP_TLS_INFO *info;
2354 
2355         if (opt_tls_cert != NULL
2356             || opt_tls_key != NULL || opt_tls_keypass != NULL) {
2357             if (opt_tls_key == NULL) {
2358                 CMP_err("missing -tls_key option");
2359                 goto err;
2360             } else if (opt_tls_cert == NULL) {
2361                 CMP_err("missing -tls_cert option");
2362                 goto err;
2363             }
2364         }
2365 
2366         if ((info = OPENSSL_zalloc(sizeof(*info))) == NULL)
2367             goto err;
2368         APP_HTTP_TLS_INFO_free(OSSL_CMP_CTX_get_http_cb_arg(ctx));
2369         (void)OSSL_CMP_CTX_set_http_cb_arg(ctx, info);
2370         info->ssl_ctx = setup_ssl_ctx(ctx, host, engine);
2371         info->server = host;
2372         host = NULL; /* prevent deallocation */
2373         if ((info->port = OPENSSL_strdup(server_port)) == NULL)
2374             goto err;
2375         /* workaround for callback design flaw, see #17088: */
2376         info->use_proxy = proxy_host != NULL;
2377         info->timeout = OSSL_CMP_CTX_get_option(ctx, OSSL_CMP_OPT_MSG_TIMEOUT);
2378 
2379         if (info->ssl_ctx == NULL)
2380             goto err;
2381         (void)OSSL_CMP_CTX_set_http_cb(ctx, app_http_tls_cb);
2382     }
2383 #endif
2384 
2385     if (!setup_protection_ctx(ctx, engine))
2386         goto err;
2387 
2388     if (!setup_request_ctx(ctx, engine))
2389         goto err;
2390 
2391     if (!set_name(opt_recipient, OSSL_CMP_CTX_set1_recipient, ctx, "recipient")
2392         || !set_name(opt_expect_sender, OSSL_CMP_CTX_set1_expected_sender,
2393             ctx, "expected sender"))
2394         goto err;
2395 
2396     if (opt_geninfo != NULL && !handle_opt_geninfo(ctx))
2397         goto err;
2398     if (opt_profile != NULL && !add_certProfile(ctx, opt_profile))
2399         goto err;
2400 
2401     /* not printing earlier, to minimize confusion in case setup fails before */
2402     if (opt_reqout_only == NULL)
2403         CMP_info3("will contact %s%s%s ", server_buf, proxy_buf,
2404             opt_rspin == NULL ? "" : " only if -rspin argument gives too few filenames");
2405 
2406     ret = 1;
2407 
2408 err:
2409     OPENSSL_free(host);
2410     OPENSSL_free(port);
2411     OPENSSL_free(path);
2412     return ret;
2413 oom:
2414     CMP_err("out of memory");
2415     goto err;
2416 }
2417 
2418 /*
2419  * write out the given certificate to the output specified by bio.
2420  * Depending on options use either PEM or DER format.
2421  * Returns 1 on success, 0 on error
2422  */
write_cert(BIO * bio,X509 * cert)2423 static int write_cert(BIO *bio, X509 *cert)
2424 {
2425     if ((opt_certform == FORMAT_PEM && PEM_write_bio_X509(bio, cert))
2426         || (opt_certform == FORMAT_ASN1 && i2d_X509_bio(bio, cert)))
2427         return 1;
2428     if (opt_certform != FORMAT_PEM && opt_certform != FORMAT_ASN1)
2429         BIO_printf(bio_err,
2430             "error: unsupported type '%s' for writing certificates\n",
2431             opt_certform_s);
2432     return 0;
2433 }
2434 
write_crl(BIO * bio,X509_CRL * crl)2435 static int write_crl(BIO *bio, X509_CRL *crl)
2436 {
2437     if (opt_crlform != FORMAT_PEM && opt_crlform != FORMAT_ASN1) {
2438         BIO_printf(bio_err, "error: unsupported type '%s' for writing CRLs\n",
2439             opt_crlform_s);
2440         return 0;
2441     }
2442 
2443     return opt_crlform == FORMAT_PEM ? PEM_write_bio_X509_CRL(bio, crl)
2444                                      : i2d_X509_CRL_bio(bio, crl);
2445 }
2446 
2447 /*
2448  * If file != NULL writes out a stack of certs to the given file.
2449  * If certs is NULL, the file is emptied.
2450  * Frees the certs if present.
2451  * Depending on options use either PEM or DER format,
2452  * where DER does not make much sense for writing more than one cert!
2453  * Returns number of written certificates on success, -1 on error.
2454  */
save_free_certs(STACK_OF (X509)* certs,const char * file,const char * desc)2455 static int save_free_certs(STACK_OF(X509) *certs,
2456     const char *file, const char *desc)
2457 {
2458     BIO *bio = NULL;
2459     int i;
2460     int n = sk_X509_num(certs /* may be NULL */);
2461 
2462     if (n < 0)
2463         n = 0;
2464     if (file == NULL)
2465         goto end;
2466     if (certs != NULL)
2467         CMP_info3("received %d %s certificate(s), saving to file '%s'",
2468             n, desc, file);
2469     if (n > 1 && opt_certform != FORMAT_PEM)
2470         CMP_warn("saving more than one certificate in non-PEM format");
2471 
2472     if ((bio = BIO_new(BIO_s_file())) == NULL
2473         || !BIO_write_filename(bio, (char *)file)) {
2474         CMP_err3("could not open file '%s' for %s %s certificate(s)",
2475             file, certs == NULL ? "deleting" : "writing", desc);
2476         n = -1;
2477         goto end;
2478     }
2479 
2480     for (i = 0; i < n; i++) {
2481         if (!write_cert(bio, sk_X509_value(certs, i))) {
2482             CMP_err2("cannot write %s certificate to file '%s'", desc, file);
2483             n = -1;
2484             goto end;
2485         }
2486     }
2487 
2488 end:
2489     BIO_free(bio);
2490     OSSL_STACK_OF_X509_free(certs);
2491     return n;
2492 }
2493 
save_crl(X509_CRL * crl,const char * file,const char * desc)2494 static int save_crl(X509_CRL *crl,
2495     const char *file, const char *desc)
2496 {
2497     BIO *bio = NULL;
2498     int res = 0;
2499 
2500     if (file == NULL)
2501         return 1;
2502     if (crl != NULL)
2503         CMP_info2("received %s, saving to file '%s'", desc, file);
2504 
2505     if ((bio = BIO_new(BIO_s_file())) == NULL
2506         || !BIO_write_filename(bio, (char *)file)) {
2507         CMP_err2("could not open file '%s' for writing %s",
2508             file, desc);
2509         goto end;
2510     }
2511 
2512     if (!write_crl(bio, crl)) {
2513         CMP_err2("cannot write %s to file '%s'", desc, file);
2514         goto end;
2515     }
2516     res = 1;
2517 
2518 end:
2519     BIO_free(bio);
2520     return res;
2521 }
2522 
delete_file(const char * file,const char * desc)2523 static int delete_file(const char *file, const char *desc)
2524 {
2525     if (file == NULL)
2526         return 1;
2527 
2528     if (unlink(file) != 0 && errno != ENOENT) {
2529         CMP_err2("Failed to delete %s, which should be done to indicate there is no %s",
2530             file, desc);
2531         return 0;
2532     }
2533     return 1;
2534 }
2535 
save_cert_or_delete(X509 * cert,const char * file,const char * desc)2536 static int save_cert_or_delete(X509 *cert, const char *file, const char *desc)
2537 {
2538     if (file == NULL)
2539         return 1;
2540     if (cert == NULL) {
2541         char desc_cert[80];
2542 
2543         BIO_snprintf(desc_cert, sizeof(desc_cert), "%s certificate", desc);
2544         return delete_file(file, desc_cert);
2545     } else {
2546         STACK_OF(X509) *certs = sk_X509_new_null();
2547 
2548         if (!X509_add_cert(certs, cert, X509_ADD_FLAG_UP_REF)) {
2549             sk_X509_free(certs);
2550             return 0;
2551         }
2552         return save_free_certs(certs, file, desc) >= 0;
2553     }
2554 }
2555 
save_crl_or_delete(X509_CRL * crl,const char * file,const char * desc)2556 static int save_crl_or_delete(X509_CRL *crl, const char *file, const char *desc)
2557 {
2558     if (file == NULL)
2559         return 1;
2560     return (crl == NULL) ? delete_file(file, desc) : save_crl(crl, file, desc);
2561 }
2562 
save_template(const char * file,const OSSL_CRMF_CERTTEMPLATE * tmpl)2563 static int save_template(const char *file, const OSSL_CRMF_CERTTEMPLATE *tmpl)
2564 {
2565     BIO *bio = BIO_new_file(file, "wb");
2566 
2567     if (bio == NULL) {
2568         CMP_err1("error saving certTemplate from genp: cannot open file %s",
2569             file);
2570         return 0;
2571     }
2572     if (!ASN1_i2d_bio_of(OSSL_CRMF_CERTTEMPLATE, i2d_OSSL_CRMF_CERTTEMPLATE,
2573             bio, tmpl)) {
2574         CMP_err1("error saving certTemplate from genp: cannot write file %s",
2575             file);
2576         BIO_free(bio);
2577         return 0;
2578     } else {
2579         CMP_info1("stored certTemplate from genp to file '%s'", file);
2580     }
2581     BIO_free(bio);
2582     return 1;
2583 }
2584 
save_keyspec(const char * file,const OSSL_CMP_ATAVS * keyspec)2585 static int save_keyspec(const char *file, const OSSL_CMP_ATAVS *keyspec)
2586 {
2587     BIO *bio = BIO_new_file(file, "wb");
2588 
2589     if (bio == NULL) {
2590         CMP_err1("error saving keySpec from genp: cannot open file %s", file);
2591         return 0;
2592     }
2593 
2594     if (!ASN1_i2d_bio_of(OSSL_CMP_ATAVS, i2d_OSSL_CMP_ATAVS, bio, keyspec)) {
2595         CMP_err1("error saving keySpec from genp: cannot write file %s", file);
2596         BIO_free(bio);
2597         return 0;
2598     } else {
2599         CMP_info1("stored keySpec from genp to file '%s'", file);
2600     }
2601     BIO_free(bio);
2602     return 1;
2603 }
2604 
nid_name(int nid)2605 static const char *nid_name(int nid)
2606 {
2607     const char *name = OBJ_nid2ln(nid);
2608 
2609     if (name == NULL)
2610         name = OBJ_nid2sn(nid);
2611     if (name == NULL)
2612         name = "<unknown OID>";
2613     return name;
2614 }
2615 
print_itavs(const STACK_OF (OSSL_CMP_ITAV)* itavs)2616 static int print_itavs(const STACK_OF(OSSL_CMP_ITAV) *itavs)
2617 {
2618     int i, ret = 1;
2619     int n = sk_OSSL_CMP_ITAV_num(itavs);
2620 
2621     if (n <= 0) { /* also in case itavs == NULL */
2622         CMP_info("genp does not contain any ITAV");
2623         return ret;
2624     }
2625 
2626     for (i = 1; i <= n; i++) {
2627         OSSL_CMP_ITAV *itav = sk_OSSL_CMP_ITAV_value(itavs, i - 1);
2628         ASN1_OBJECT *type = OSSL_CMP_ITAV_get0_type(itav);
2629         char name[80];
2630 
2631         if (itav == NULL) {
2632             CMP_err1("could not get ITAV #%d from genp", i);
2633             ret = 0;
2634             continue;
2635         }
2636         if (i2t_ASN1_OBJECT(name, sizeof(name), type) <= 0) {
2637             CMP_err1("error parsing type of ITAV #%d from genp", i);
2638             ret = 0;
2639         } else {
2640             CMP_info2("ITAV #%d from genp infoType=%s", i, name);
2641         }
2642     }
2643     return ret;
2644 }
2645 
2646 static char opt_item[SECTION_NAME_MAX + 1];
2647 /* get previous name from a comma or space-separated list of names */
prev_item(const char * opt,const char * end)2648 static const char *prev_item(const char *opt, const char *end)
2649 {
2650     const char *beg;
2651     size_t len;
2652 
2653     if (end == opt)
2654         return NULL;
2655     beg = end;
2656     while (beg > opt) {
2657         --beg;
2658         if (beg[0] == ',' || isspace(_UC(beg[0]))) {
2659             ++beg;
2660             break;
2661         }
2662     }
2663     len = end - beg;
2664     if (len > SECTION_NAME_MAX) {
2665         CMP_warn3("using only first %d characters of section name starting with \"%.*s\"",
2666             SECTION_NAME_MAX, SECTION_NAME_MAX, beg);
2667         len = SECTION_NAME_MAX;
2668     }
2669     memcpy(opt_item, beg, len);
2670     opt_item[len] = '\0';
2671     while (beg > opt) {
2672         --beg;
2673         if (beg[0] != ',' && !isspace(_UC(beg[0]))) {
2674             ++beg;
2675             break;
2676         }
2677     }
2678     return beg;
2679 }
2680 
2681 /* 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)2682 static char *conf_get_string(const CONF *src_conf, const char *groups,
2683     const char *name)
2684 {
2685     char *res = NULL;
2686     const char *end = groups + strlen(groups);
2687 
2688     while ((end = prev_item(groups, end)) != NULL) {
2689         if ((res = app_conf_try_string(src_conf, opt_item, name)) != NULL)
2690             return res;
2691     }
2692     return res;
2693 }
2694 
2695 /* 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)2696 static int conf_get_number_e(const CONF *conf_, const char *groups,
2697     const char *name, long *result)
2698 {
2699     char *str = conf_get_string(conf_, groups, name);
2700     char *tailptr;
2701     long res;
2702 
2703     if (str == NULL || *str == '\0')
2704         return 0;
2705 
2706     res = strtol(str, &tailptr, 10);
2707     if (res == LONG_MIN || res == LONG_MAX || *tailptr != '\0')
2708         return 0;
2709 
2710     *result = res;
2711     return 1;
2712 }
2713 
2714 /*
2715  * use the command line option table to read values from the CMP section
2716  * of openssl.cnf.  Defaults are taken from the config file, they can be
2717  * overwritten on the command line.
2718  */
read_config(void)2719 static int read_config(void)
2720 {
2721     unsigned int i;
2722     long num = 0;
2723     char *txt = NULL;
2724     const OPTIONS *opt;
2725     int start_opt = OPT_VERBOSITY - OPT_HELP;
2726     int start_idx = OPT_VERBOSITY - 2;
2727     /*
2728      * starting with offset OPT_VERBOSITY because OPT_CONFIG and OPT_SECTION
2729      * would not make sense within the config file.
2730      */
2731     int n_options = OSSL_NELEM(cmp_options) - 1;
2732 
2733     for (opt = &cmp_options[start_opt], i = start_idx;
2734         opt->name != NULL; i++, opt++)
2735         if (!strcmp(opt->name, OPT_SECTION_STR)
2736             || !strcmp(opt->name, OPT_MORE_STR))
2737             n_options--;
2738     OPENSSL_assert(OSSL_NELEM(cmp_vars) == n_options + OPT_PROV__FIRST + 1 - OPT_PROV__LAST + OPT_R__FIRST + 1 - OPT_R__LAST + OPT_V__FIRST + 1 - OPT_V__LAST);
2739     for (opt = &cmp_options[start_opt], i = start_idx;
2740         opt->name != NULL; i++, opt++) {
2741         int provider_option = (OPT_PROV__FIRST <= opt->retval
2742             && opt->retval < OPT_PROV__LAST);
2743         int rand_state_option = (OPT_R__FIRST <= opt->retval
2744             && opt->retval < OPT_R__LAST);
2745         int verification_option = (OPT_V__FIRST <= opt->retval
2746             && opt->retval < OPT_V__LAST);
2747 
2748         if (strcmp(opt->name, OPT_SECTION_STR) == 0
2749             || strcmp(opt->name, OPT_MORE_STR) == 0) {
2750             i--;
2751             continue;
2752         }
2753         if (provider_option || rand_state_option || verification_option)
2754             i--;
2755         switch (opt->valtype) {
2756         case '-':
2757         case 'p':
2758         case 'n':
2759         case 'N':
2760         case 'l':
2761             if (!conf_get_number_e(conf, opt_section, opt->name, &num)) {
2762                 ERR_clear_error();
2763                 continue; /* option not provided */
2764             }
2765             if (opt->valtype == 'p' && num <= 0) {
2766                 opt_printf_stderr("Non-positive number \"%ld\" for config option -%s\n",
2767                     num, opt->name);
2768                 return -1;
2769             }
2770             if (opt->valtype == 'N' && num < 0) {
2771                 opt_printf_stderr("Negative number \"%ld\" for config option -%s\n",
2772                     num, opt->name);
2773                 return -1;
2774             }
2775             break;
2776         case 's':
2777         case '>':
2778         case 'M':
2779             txt = conf_get_string(conf, opt_section, opt->name);
2780             if (txt == NULL) {
2781                 ERR_clear_error();
2782                 continue; /* option not provided */
2783             }
2784             break;
2785         default:
2786             CMP_err2("internal: unsupported type '%c' for option '%s'",
2787                 opt->valtype, opt->name);
2788             return 0;
2789             break;
2790         }
2791         if (provider_option || verification_option) {
2792             int conf_argc = 1;
2793             char *conf_argv[3];
2794             char arg1[82];
2795 
2796             BIO_snprintf(arg1, 81, "-%s", (char *)opt->name);
2797             conf_argv[0] = prog;
2798             conf_argv[1] = arg1;
2799             if (opt->valtype == '-') {
2800                 if (num != 0)
2801                     conf_argc = 2;
2802             } else {
2803                 conf_argc = 3;
2804                 conf_argv[2] = conf_get_string(conf, opt_section, opt->name);
2805                 /* not NULL */
2806             }
2807             if (conf_argc > 1) {
2808                 (void)opt_init(conf_argc, conf_argv, cmp_options);
2809 
2810                 if (provider_option
2811                         ? !opt_provider(opt_next())
2812                         : !opt_verify(opt_next(), vpm)) {
2813                     CMP_err2("for option '%s' in config file section '%s'",
2814                         opt->name, opt_section);
2815                     return 0;
2816                 }
2817             }
2818         } else {
2819             switch (opt->valtype) {
2820             case '-':
2821             case 'p':
2822             case 'n':
2823             case 'N':
2824                 if (num < INT_MIN || INT_MAX < num) {
2825                     BIO_printf(bio_err,
2826                         "integer value out of range for option '%s'\n",
2827                         opt->name);
2828                     return 0;
2829                 }
2830                 *cmp_vars[i].num = (int)num;
2831                 break;
2832             case 'l':
2833                 *cmp_vars[i].num_long = num;
2834                 break;
2835             default:
2836                 if (txt != NULL && txt[0] == '\0')
2837                     txt = NULL; /* reset option on empty string input */
2838                 *cmp_vars[i].txt = txt;
2839                 break;
2840             }
2841         }
2842     }
2843 
2844     return 1;
2845 }
2846 
opt_str(void)2847 static char *opt_str(void)
2848 {
2849     char *arg = opt_arg();
2850 
2851     if (arg[0] == '\0') {
2852         CMP_warn1("%s option argument is empty string, resetting option",
2853             opt_flag());
2854         arg = NULL;
2855     } else if (arg[0] == '-') {
2856         CMP_warn1("%s option argument starts with hyphen", opt_flag());
2857     }
2858     return arg;
2859 }
2860 
2861 /* returns 1 on success, 0 on error, -1 on -help (i.e., stop with success) */
get_opts(int argc,char ** argv)2862 static int get_opts(int argc, char **argv)
2863 {
2864     OPTION_CHOICE o;
2865 
2866     prog = opt_init(argc, argv, cmp_options);
2867 
2868     while ((o = opt_next()) != OPT_EOF) {
2869         switch (o) {
2870         case OPT_EOF:
2871         case OPT_ERR:
2872         opthelp:
2873             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
2874             return 0;
2875         case OPT_HELP:
2876             opt_help(cmp_options);
2877             return -1;
2878         case OPT_CONFIG: /* has already been handled */
2879         case OPT_SECTION: /* has already been handled */
2880             break;
2881         case OPT_VERBOSITY:
2882             if (!set_verbosity(opt_int_arg()))
2883                 goto opthelp;
2884             break;
2885 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
2886         case OPT_SERVER:
2887             opt_server = opt_str();
2888             break;
2889         case OPT_PROXY:
2890             opt_proxy = opt_str();
2891             break;
2892         case OPT_NO_PROXY:
2893             opt_no_proxy = opt_str();
2894             break;
2895 #endif
2896         case OPT_RECIPIENT:
2897             opt_recipient = opt_str();
2898             break;
2899         case OPT_PATH:
2900             opt_path = opt_str();
2901             break;
2902         case OPT_KEEP_ALIVE:
2903             opt_keep_alive = opt_int_arg();
2904             if (opt_keep_alive > 2) {
2905                 CMP_err("-keep_alive argument must be 0, 1, or 2");
2906                 goto opthelp;
2907             }
2908             break;
2909         case OPT_MSG_TIMEOUT:
2910             opt_msg_timeout = opt_int_arg();
2911             break;
2912         case OPT_TOTAL_TIMEOUT:
2913             opt_total_timeout = opt_int_arg();
2914             break;
2915 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
2916         case OPT_TLS_USED:
2917             opt_tls_used = 1;
2918             break;
2919         case OPT_TLS_CERT:
2920             opt_tls_cert = opt_str();
2921             break;
2922         case OPT_TLS_KEY:
2923             opt_tls_key = opt_str();
2924             break;
2925         case OPT_TLS_KEYPASS:
2926             opt_tls_keypass = opt_str();
2927             break;
2928         case OPT_TLS_EXTRA:
2929             opt_tls_extra = opt_str();
2930             break;
2931         case OPT_TLS_TRUSTED:
2932             opt_tls_trusted = opt_str();
2933             break;
2934         case OPT_TLS_HOST:
2935             opt_tls_host = opt_str();
2936             break;
2937 #endif
2938 
2939         case OPT_REF:
2940             opt_ref = opt_str();
2941             break;
2942         case OPT_SECRET:
2943             opt_secret = opt_str();
2944             break;
2945         case OPT_CERT:
2946             opt_cert = opt_str();
2947             break;
2948         case OPT_OWN_TRUSTED:
2949             opt_own_trusted = opt_str();
2950             break;
2951         case OPT_KEY:
2952             opt_key = opt_str();
2953             break;
2954         case OPT_KEYPASS:
2955             opt_keypass = opt_str();
2956             break;
2957         case OPT_DIGEST:
2958             opt_digest = opt_str();
2959             break;
2960         case OPT_MAC:
2961             opt_mac = opt_str();
2962             break;
2963         case OPT_EXTRACERTS:
2964             opt_extracerts = opt_str();
2965             break;
2966         case OPT_UNPROTECTED_REQUESTS:
2967             opt_unprotected_requests = 1;
2968             break;
2969 
2970         case OPT_TRUSTED:
2971             opt_trusted = opt_str();
2972             break;
2973         case OPT_UNTRUSTED:
2974             opt_untrusted = opt_str();
2975             break;
2976         case OPT_SRVCERT:
2977             opt_srvcert = opt_str();
2978             break;
2979         case OPT_EXPECT_SENDER:
2980             opt_expect_sender = opt_str();
2981             break;
2982         case OPT_IGNORE_KEYUSAGE:
2983             opt_ignore_keyusage = 1;
2984             break;
2985         case OPT_UNPROTECTED_ERRORS:
2986             opt_unprotected_errors = 1;
2987             break;
2988         case OPT_NO_CACHE_EXTRACERTS:
2989             opt_no_cache_extracerts = 1;
2990             break;
2991         case OPT_SRVCERTOUT:
2992             opt_srvcertout = opt_str();
2993             break;
2994         case OPT_EXTRACERTSOUT:
2995             opt_extracertsout = opt_str();
2996             break;
2997         case OPT_CACERTSOUT:
2998             opt_cacertsout = opt_str();
2999             break;
3000         case OPT_OLDWITHOLD:
3001             opt_oldwithold = opt_str();
3002             break;
3003         case OPT_NEWWITHNEW:
3004             opt_newwithnew = opt_str();
3005             break;
3006         case OPT_NEWWITHOLD:
3007             opt_newwithold = opt_str();
3008             break;
3009         case OPT_OLDWITHNEW:
3010             opt_oldwithnew = opt_str();
3011             break;
3012         case OPT_CRLCERT:
3013             opt_crlcert = opt_str();
3014             break;
3015         case OPT_OLDCRL:
3016             opt_oldcrl = opt_str();
3017             break;
3018         case OPT_CRLOUT:
3019             opt_crlout = opt_str();
3020             break;
3021 
3022         case OPT_V_CASES:
3023             if (!opt_verify(o, vpm))
3024                 goto opthelp;
3025             break;
3026         case OPT_CMD:
3027             opt_cmd_s = opt_str();
3028             break;
3029         case OPT_INFOTYPE:
3030             opt_infotype_s = opt_str();
3031             break;
3032         case OPT_PROFILE:
3033             opt_profile = opt_str();
3034             break;
3035         case OPT_GENINFO:
3036             opt_geninfo = opt_str();
3037             break;
3038         case OPT_TEMPLATE:
3039             opt_template = opt_str();
3040             break;
3041         case OPT_KEYSPEC:
3042             opt_keyspec = opt_str();
3043             break;
3044         case OPT_NEWKEY:
3045             opt_newkey = opt_str();
3046             break;
3047         case OPT_NEWKEYPASS:
3048             opt_newkeypass = opt_str();
3049             break;
3050         case OPT_CENTRALKEYGEN:
3051             opt_centralkeygen = 1;
3052             break;
3053         case OPT_NEWKEYOUT:
3054             opt_newkeyout = opt_str();
3055             break;
3056         case OPT_SUBJECT:
3057             opt_subject = opt_str();
3058             break;
3059         case OPT_DAYS:
3060             opt_days = opt_int_arg();
3061             break;
3062         case OPT_REQEXTS:
3063             opt_reqexts = opt_str();
3064             break;
3065         case OPT_SANS:
3066             opt_sans = opt_str();
3067             break;
3068         case OPT_SAN_NODEFAULT:
3069             opt_san_nodefault = 1;
3070             break;
3071         case OPT_POLICIES:
3072             opt_policies = opt_str();
3073             break;
3074         case OPT_POLICY_OIDS:
3075             opt_policy_oids = opt_str();
3076             break;
3077         case OPT_POLICY_OIDS_CRITICAL:
3078             opt_policy_oids_critical = 1;
3079             break;
3080         case OPT_POPO:
3081             opt_popo = opt_int_arg();
3082             if (opt_popo < OSSL_CRMF_POPO_NONE
3083                 || opt_popo > OSSL_CRMF_POPO_KEYENC) {
3084                 CMP_err("invalid popo spec. Valid values are -1 .. 2");
3085                 goto opthelp;
3086             }
3087             break;
3088         case OPT_CSR:
3089             opt_csr = opt_str();
3090             break;
3091         case OPT_OUT_TRUSTED:
3092             opt_out_trusted = opt_str();
3093             break;
3094         case OPT_IMPLICIT_CONFIRM:
3095             opt_implicit_confirm = 1;
3096             break;
3097         case OPT_DISABLE_CONFIRM:
3098             opt_disable_confirm = 1;
3099             break;
3100         case OPT_CERTOUT:
3101             opt_certout = opt_str();
3102             break;
3103         case OPT_CHAINOUT:
3104             opt_chainout = opt_str();
3105             break;
3106         case OPT_OLDCERT:
3107             opt_oldcert = opt_str();
3108             break;
3109         case OPT_REVREASON:
3110             opt_revreason = opt_int_arg();
3111             if (opt_revreason < CRL_REASON_NONE
3112                 || opt_revreason > CRL_REASON_AA_COMPROMISE
3113                 || opt_revreason == 7) {
3114                 CMP_err("invalid revreason. Valid values are -1 .. 6, 8 .. 10");
3115                 goto opthelp;
3116             }
3117             break;
3118         case OPT_ISSUER:
3119             opt_issuer = opt_str();
3120             break;
3121         case OPT_SERIAL:
3122             opt_serial = opt_str();
3123             break;
3124         case OPT_CERTFORM:
3125             opt_certform_s = opt_str();
3126             break;
3127         case OPT_CRLFORM:
3128             opt_crlform_s = opt_str();
3129             break;
3130         case OPT_KEYFORM:
3131             opt_keyform_s = opt_str();
3132             break;
3133         case OPT_OTHERPASS:
3134             opt_otherpass = opt_str();
3135             break;
3136 #ifndef OPENSSL_NO_ENGINE
3137         case OPT_ENGINE:
3138             opt_engine = opt_str();
3139             break;
3140 #endif
3141         case OPT_PROV_CASES:
3142             if (!opt_provider(o))
3143                 goto opthelp;
3144             break;
3145         case OPT_R_CASES:
3146             if (!opt_rand(o))
3147                 goto opthelp;
3148             break;
3149 
3150         case OPT_BATCH:
3151             opt_batch = 1;
3152             break;
3153         case OPT_REPEAT:
3154             opt_repeat = opt_int_arg();
3155             break;
3156         case OPT_REQIN:
3157             opt_reqin = opt_str();
3158             break;
3159         case OPT_REQIN_NEW_TID:
3160             opt_reqin_new_tid = 1;
3161             break;
3162         case OPT_REQOUT:
3163             opt_reqout = opt_str();
3164             break;
3165         case OPT_REQOUT_ONLY:
3166             opt_reqout_only = opt_str();
3167             break;
3168         case OPT_RSPIN:
3169             opt_rspin = opt_str();
3170             break;
3171         case OPT_RSPOUT:
3172             opt_rspout = opt_str();
3173             break;
3174         case OPT_USE_MOCK_SRV:
3175             opt_use_mock_srv = 1;
3176             break;
3177 
3178 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3179         case OPT_PORT:
3180             opt_port = opt_str();
3181             break;
3182         case OPT_MAX_MSGS:
3183             opt_max_msgs = opt_int_arg();
3184             break;
3185 #endif
3186         case OPT_SRV_REF:
3187             opt_srv_ref = opt_str();
3188             break;
3189         case OPT_SRV_SECRET:
3190             opt_srv_secret = opt_str();
3191             break;
3192         case OPT_SRV_CERT:
3193             opt_srv_cert = opt_str();
3194             break;
3195         case OPT_SRV_KEY:
3196             opt_srv_key = opt_str();
3197             break;
3198         case OPT_SRV_KEYPASS:
3199             opt_srv_keypass = opt_str();
3200             break;
3201         case OPT_SRV_TRUSTED:
3202             opt_srv_trusted = opt_str();
3203             break;
3204         case OPT_SRV_UNTRUSTED:
3205             opt_srv_untrusted = opt_str();
3206             break;
3207         case OPT_REF_CERT:
3208             opt_ref_cert = opt_str();
3209             break;
3210         case OPT_RSP_CERT:
3211             opt_rsp_cert = opt_str();
3212             break;
3213         case OPT_RSP_KEY:
3214             opt_rsp_key = opt_str();
3215             break;
3216         case OPT_RSP_KEYPASS:
3217             opt_rsp_keypass = opt_str();
3218             break;
3219         case OPT_RSP_CRL:
3220             opt_rsp_crl = opt_str();
3221             break;
3222         case OPT_RSP_EXTRACERTS:
3223             opt_rsp_extracerts = opt_str();
3224             break;
3225         case OPT_RSP_CAPUBS:
3226             opt_rsp_capubs = opt_str();
3227             break;
3228         case OPT_RSP_NEWWITHNEW:
3229             opt_rsp_newwithnew = opt_str();
3230             break;
3231         case OPT_RSP_NEWWITHOLD:
3232             opt_rsp_newwithold = opt_str();
3233             break;
3234         case OPT_RSP_OLDWITHNEW:
3235             opt_rsp_oldwithnew = opt_str();
3236             break;
3237         case OPT_POLL_COUNT:
3238             opt_poll_count = opt_int_arg();
3239             break;
3240         case OPT_CHECK_AFTER:
3241             opt_check_after = opt_int_arg();
3242             break;
3243         case OPT_GRANT_IMPLICITCONF:
3244             opt_grant_implicitconf = 1;
3245             break;
3246         case OPT_PKISTATUS:
3247             opt_pkistatus = opt_int_arg();
3248             break;
3249         case OPT_FAILURE:
3250             opt_failure = opt_int_arg();
3251             break;
3252         case OPT_FAILUREBITS:
3253             opt_failurebits = opt_int_arg();
3254             break;
3255         case OPT_STATUSSTRING:
3256             opt_statusstring = opt_str();
3257             break;
3258         case OPT_SEND_ERROR:
3259             opt_send_error = 1;
3260             break;
3261         case OPT_SEND_UNPROTECTED:
3262             opt_send_unprotected = 1;
3263             break;
3264         case OPT_SEND_UNPROT_ERR:
3265             opt_send_unprot_err = 1;
3266             break;
3267         case OPT_ACCEPT_UNPROTECTED:
3268             opt_accept_unprotected = 1;
3269             break;
3270         case OPT_ACCEPT_UNPROT_ERR:
3271             opt_accept_unprot_err = 1;
3272             break;
3273         case OPT_ACCEPT_RAVERIFIED:
3274             opt_accept_raverified = 1;
3275             break;
3276         }
3277     }
3278 
3279     /* No extra args. */
3280     if (!opt_check_rest_arg(NULL))
3281         goto opthelp;
3282     return 1;
3283 }
3284 
3285 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
cmp_server(OSSL_CMP_CTX * srv_cmp_ctx)3286 static int cmp_server(OSSL_CMP_CTX *srv_cmp_ctx)
3287 {
3288     BIO *acbio;
3289     BIO *cbio = NULL;
3290     int keep_alive = 0;
3291     int msgs = 0;
3292     int retry = 1;
3293     int ret = 1;
3294 
3295     if ((acbio = http_server_init(prog, opt_port, opt_verbosity)) == NULL)
3296         return 0;
3297     while (opt_max_msgs <= 0 || msgs < opt_max_msgs) {
3298         char *path = NULL;
3299         OSSL_CMP_MSG *req = NULL;
3300         OSSL_CMP_MSG *resp = NULL;
3301 
3302         ret = http_server_get_asn1_req(ASN1_ITEM_rptr(OSSL_CMP_MSG),
3303             (ASN1_VALUE **)&req, &path,
3304             &cbio, acbio, &keep_alive,
3305             prog, 0, 0);
3306         if (ret == 0) { /* no request yet */
3307             if (retry) {
3308                 OSSL_sleep(1000);
3309                 retry = 0;
3310                 continue;
3311             }
3312             ret = 0;
3313             goto next;
3314         }
3315         if (ret++ == -1) /* fatal error */
3316             break;
3317 
3318         ret = 0;
3319         msgs++;
3320         if (req != NULL) {
3321             if (strcmp(path, "") != 0 && strcmp(path, "pkix/") != 0) {
3322                 (void)http_server_send_status(prog, cbio, 404, "Not Found");
3323                 CMP_err1("expecting empty path or 'pkix/' but got '%s'",
3324                     path);
3325                 OPENSSL_free(path);
3326                 OSSL_CMP_MSG_free(req);
3327                 goto next;
3328             }
3329             OPENSSL_free(path);
3330             resp = OSSL_CMP_CTX_server_perform(cmp_ctx /* of client */, req);
3331             OSSL_CMP_MSG_free(req);
3332             if (resp == NULL) {
3333                 (void)http_server_send_status(prog, cbio,
3334                     500, "Internal Server Error");
3335                 break; /* treated as fatal error */
3336             }
3337             ret = http_server_send_asn1_resp(prog, cbio, keep_alive,
3338                 "application/pkixcmp",
3339                 ASN1_ITEM_rptr(OSSL_CMP_MSG),
3340                 (const ASN1_VALUE *)resp);
3341             OSSL_CMP_MSG_free(resp);
3342             if (!ret)
3343                 break; /* treated as fatal error */
3344         }
3345     next:
3346         if (!ret) { /* on transmission error, cancel CMP transaction */
3347             (void)OSSL_CMP_CTX_set1_transactionID(srv_cmp_ctx, NULL);
3348             (void)OSSL_CMP_CTX_set1_senderNonce(srv_cmp_ctx, NULL);
3349         }
3350         if (!ret || !keep_alive
3351             || OSSL_CMP_CTX_get_status(srv_cmp_ctx) != OSSL_CMP_PKISTATUS_trans
3352             /* transaction closed by OSSL_CMP_CTX_server_perform() */) {
3353             BIO_free_all(cbio);
3354             cbio = NULL;
3355         }
3356     }
3357 
3358     BIO_free_all(cbio);
3359     BIO_free_all(acbio);
3360     return ret;
3361 }
3362 #endif
3363 
print_keyspec(OSSL_CMP_ATAVS * keySpec)3364 static void print_keyspec(OSSL_CMP_ATAVS *keySpec)
3365 {
3366     const char *desc = "specifications contained in keySpec from genp";
3367     BIO *mem;
3368     int i;
3369     const char *p;
3370     long len;
3371 
3372     if (keySpec == NULL) {
3373         CMP_info1("No %s", desc);
3374         return;
3375     }
3376 
3377     mem = BIO_new(BIO_s_mem());
3378     if (mem == NULL) {
3379         CMP_err1("Out of memory - cannot dump key %s", desc);
3380         return;
3381     }
3382     BIO_printf(mem, "Key %s:\n", desc);
3383 
3384     for (i = 0; i < sk_OSSL_CMP_ATAV_num(keySpec); i++) {
3385         OSSL_CMP_ATAV *atav = sk_OSSL_CMP_ATAV_value(keySpec, i);
3386         ASN1_OBJECT *type = OSSL_CMP_ATAV_get0_type(atav /* may be NULL */);
3387         int nid = OBJ_obj2nid(type);
3388 
3389         switch (nid) {
3390         case NID_id_regCtrl_algId: {
3391             X509_ALGOR *alg = OSSL_CMP_ATAV_get0_algId(atav);
3392             const ASN1_OBJECT *oid;
3393             int paramtype;
3394             const void *param;
3395 
3396             /* NULL check to prevent dereferencing a NULL pointer when print_keyspec is called */
3397             if (alg == NULL) {
3398                 BIO_puts(mem, "Key algorithm: <absent>\n");
3399                 break;
3400             }
3401 
3402             X509_ALGOR_get0(&oid, &paramtype, &param, alg);
3403             BIO_printf(mem, "Key algorithm: ");
3404             i2a_ASN1_OBJECT(mem, oid);
3405             if (paramtype == V_ASN1_UNDEF || alg->parameter == NULL) {
3406                 BIO_printf(mem, "\n");
3407             } else {
3408                 BIO_printf(mem, " - ");
3409                 ASN1_item_print(mem, (ASN1_VALUE *)alg,
3410                     0, ASN1_ITEM_rptr(X509_ALGOR), NULL);
3411             }
3412         } break;
3413         case NID_id_regCtrl_rsaKeyLen:
3414             BIO_printf(mem, "Key algorithm: RSA %d\n",
3415                 OSSL_CMP_ATAV_get_rsaKeyLen(atav));
3416             break;
3417         default:
3418             BIO_printf(mem, "Invalid key spec: %s\n", nid_name(nid));
3419             break;
3420         }
3421     }
3422     BIO_printf(mem, "End of key %s", desc);
3423 
3424     len = BIO_get_mem_data(mem, &p);
3425     if (len > INT_MAX)
3426         CMP_err1("Info too large - cannot dump key %s", desc);
3427     else
3428         CMP_info2("%.*s", (int)len, p);
3429     BIO_free(mem);
3430     return;
3431 }
3432 
print_status(void)3433 static void print_status(void)
3434 {
3435     /* print PKIStatusInfo */
3436     int status = OSSL_CMP_CTX_get_status(cmp_ctx);
3437     char *buf = app_malloc(OSSL_CMP_PKISI_BUFLEN, "PKIStatusInfo buf");
3438     const char *string = OSSL_CMP_CTX_snprint_PKIStatus(cmp_ctx, buf, OSSL_CMP_PKISI_BUFLEN);
3439     const char *from = "", *server = "";
3440 
3441 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3442     if (opt_server != NULL) {
3443         from = " from ";
3444         server = opt_server;
3445     }
3446 #endif
3447     CMP_print(bio_err,
3448         status == OSSL_CMP_PKISTATUS_accepted
3449             ? OSSL_CMP_LOG_INFO
3450             : status == OSSL_CMP_PKISTATUS_rejection
3451                 || status == OSSL_CMP_PKISTATUS_waiting
3452             ? OSSL_CMP_LOG_ERR
3453             : OSSL_CMP_LOG_WARNING,
3454         status == OSSL_CMP_PKISTATUS_accepted ? "info" : status == OSSL_CMP_PKISTATUS_rejection ? "server error"
3455             : status == OSSL_CMP_PKISTATUS_waiting                                              ? "internal error"
3456                                                                                                 : "warning",
3457         "received%s%s %s", from, server,
3458         string != NULL ? string : "<unknown PKIStatus>");
3459     OPENSSL_free(buf);
3460 }
3461 
do_genm(OSSL_CMP_CTX * ctx)3462 static int do_genm(OSSL_CMP_CTX *ctx)
3463 {
3464     if (opt_infotype == NID_id_it_caCerts) {
3465         STACK_OF(X509) *cacerts = NULL;
3466 
3467         if (opt_cacertsout == NULL) {
3468             CMP_err("Missing -cacertsout option for -infotype caCerts");
3469             return 0;
3470         }
3471 
3472         if (!OSSL_CMP_get1_caCerts(ctx, &cacerts))
3473             return 0;
3474 
3475         /* could check authorization of sender/origin at this point */
3476         if (cacerts == NULL) {
3477             CMP_warn("no CA certificates provided by server");
3478         } else if (save_free_certs(cacerts, opt_cacertsout, "CA") < 0) {
3479             CMP_err1("Failed to store CA certificates from genp in %s",
3480                 opt_cacertsout);
3481             return 0;
3482         }
3483         return 1;
3484     } else if (opt_infotype == NID_id_it_rootCaCert) {
3485         X509 *oldwithold = NULL;
3486         X509 *newwithnew = NULL;
3487         X509 *newwithold = NULL;
3488         X509 *oldwithnew = NULL;
3489         int res = 0;
3490 
3491         if (opt_newwithnew == NULL) {
3492             CMP_err("Missing -newwithnew option for -infotype rootCaCert");
3493             return 0;
3494         }
3495         if (opt_oldwithold == NULL) {
3496             CMP_warn("No -oldwithold given, will use all certs given with -trusted as trust anchors for verifying the newWithNew cert");
3497         } else {
3498             oldwithold = load_cert_pwd(opt_oldwithold, opt_otherpass,
3499                 "OldWithOld cert for genm with -infotype rootCaCert");
3500             if (oldwithold == NULL)
3501                 goto end_upd;
3502         }
3503         if (!OSSL_CMP_get1_rootCaKeyUpdate(ctx, oldwithold, &newwithnew,
3504                 &newwithold, &oldwithnew))
3505             goto end_upd;
3506         /* At this point might check authorization of response sender/origin */
3507 
3508         if (newwithnew == NULL)
3509             CMP_info("no root CA certificate update available");
3510         else if (oldwithold == NULL && oldwithnew != NULL)
3511             CMP_warn("oldWithNew certificate received in genp for verifying oldWithOld, but oldWithOld was not provided");
3512 
3513         if (save_cert_or_delete(newwithnew, opt_newwithnew,
3514                 "NewWithNew cert from genp")
3515             && save_cert_or_delete(newwithold, opt_newwithold,
3516                 "NewWithOld cert from genp")
3517             && save_cert_or_delete(oldwithnew, opt_oldwithnew,
3518                 "OldWithNew cert from genp"))
3519             res = 1;
3520 
3521         X509_free(newwithnew);
3522         X509_free(newwithold);
3523         X509_free(oldwithnew);
3524     end_upd:
3525         X509_free(oldwithold);
3526         return res;
3527     } else if (opt_infotype == NID_id_it_crlStatusList) {
3528         X509_CRL *oldcrl = NULL, *crl = NULL;
3529         X509 *crlcert = NULL;
3530         int res = 0;
3531         const char *desc = "CRL from genp of type 'crls'";
3532 
3533         if (opt_oldcrl == NULL && opt_crlcert == NULL) {
3534             CMP_err("Missing -oldcrl and no -crlcert given for -infotype crlStatusList");
3535             return 0;
3536         }
3537         if (opt_crlout == NULL) {
3538             CMP_err("Missing -crlout for -infotype crlStatusList");
3539             return 0;
3540         }
3541 
3542         if (opt_crlcert != NULL) {
3543             crlcert = load_cert_pwd(opt_crlcert, opt_otherpass,
3544                 "Cert for genm with -infotype crlStatusList");
3545             if (crlcert == NULL)
3546                 goto end_crlupd;
3547         }
3548 
3549         if (opt_oldcrl != NULL) {
3550             oldcrl = load_crl(opt_oldcrl, FORMAT_UNDEF, 0,
3551                 "CRL for genm with -infotype crlStatusList");
3552             if (oldcrl == NULL)
3553                 goto end_crlupd;
3554         }
3555 
3556         if (opt_oldcrl != NULL && opt_crlcert != NULL) {
3557             if (X509_NAME_cmp(X509_CRL_get_issuer(oldcrl),
3558                     X509_get_issuer_name(crlcert))
3559                 != 0)
3560                 CMP_warn("-oldcrl and -crlcert have different issuer");
3561         }
3562 
3563         if (!OSSL_CMP_get1_crlUpdate(ctx, crlcert, oldcrl, &crl))
3564             goto end_crlupd;
3565 
3566         if (crl == NULL)
3567             CMP_info("no CRL update available");
3568         if (!save_crl_or_delete(crl, opt_crlout, desc))
3569             goto end_crlupd;
3570 
3571         res = 1;
3572 
3573     end_crlupd:
3574         X509_free(crlcert);
3575         X509_CRL_free(oldcrl);
3576         X509_CRL_free(crl);
3577         return res;
3578 
3579     } else if (opt_infotype == NID_id_it_certReqTemplate) {
3580         OSSL_CRMF_CERTTEMPLATE *certTemplate;
3581         OSSL_CMP_ATAVS *keySpec;
3582         int res = 0;
3583 
3584         if (!OSSL_CMP_get1_certReqTemplate(ctx, &certTemplate, &keySpec))
3585             return 0;
3586 
3587         if (certTemplate == NULL) {
3588             CMP_warn("no certificate request template available");
3589             if (!delete_file(opt_template, "certTemplate from genp"))
3590                 return 0;
3591             if (opt_keyspec != NULL
3592                 && !delete_file(opt_keyspec, "keySpec from genp"))
3593                 return 0;
3594             return 1;
3595         }
3596         if (!save_template(opt_template, certTemplate))
3597             goto tmpl_end;
3598 
3599         print_keyspec(keySpec);
3600         if (opt_keyspec != NULL) {
3601             if (keySpec == NULL) {
3602                 CMP_warn("no key specifications available");
3603                 if (!delete_file(opt_keyspec, "keySpec from genp"))
3604                     goto tmpl_end;
3605             } else if (!save_keyspec(opt_keyspec, keySpec)) {
3606                 goto tmpl_end;
3607             }
3608         }
3609 
3610         res = 1;
3611     tmpl_end:
3612         OSSL_CRMF_CERTTEMPLATE_free(certTemplate);
3613         sk_OSSL_CMP_ATAV_pop_free(keySpec, OSSL_CMP_ATAV_free);
3614         return res;
3615     } else {
3616         OSSL_CMP_ITAV *req;
3617         STACK_OF(OSSL_CMP_ITAV) *itavs;
3618 
3619         if (opt_infotype != NID_undef) {
3620             CMP_warn1("No specific support for -infotype %s available",
3621                 opt_infotype_s);
3622 
3623             req = OSSL_CMP_ITAV_create(OBJ_nid2obj(opt_infotype), NULL);
3624             if (req == NULL || !OSSL_CMP_CTX_push0_genm_ITAV(ctx, req)) {
3625                 CMP_err1("Failed to create genm for -infotype %s",
3626                     opt_infotype_s);
3627                 return 0;
3628             }
3629         }
3630 
3631         if ((itavs = OSSL_CMP_exec_GENM_ses(ctx)) != NULL) {
3632             int res = print_itavs(itavs);
3633 
3634             sk_OSSL_CMP_ITAV_pop_free(itavs, OSSL_CMP_ITAV_free);
3635             return res;
3636         }
3637         if (OSSL_CMP_CTX_get_status(ctx) != OSSL_CMP_PKISTATUS_request)
3638             CMP_err("Did not receive response on genm or genp is not valid");
3639         return 0;
3640     }
3641 }
3642 
handle_opts_upfront(int argc,char ** argv)3643 static int handle_opts_upfront(int argc, char **argv)
3644 {
3645     int i;
3646 
3647     prog = opt_appname(argv[0]);
3648     if (argc <= 1) {
3649         opt_help(cmp_options);
3650         return 0;
3651     }
3652 
3653     /* handle -config, -section, and -verbosity to take effect for other opts */
3654     for (i = 1; i < argc - 1; i++) {
3655         if (*argv[i] == '-') {
3656             if (!strcmp(argv[i] + 1, cmp_options[OPT_CONFIG - OPT_HELP].name))
3657                 opt_config = argv[++i];
3658             else if (!strcmp(argv[i] + 1,
3659                          cmp_options[OPT_SECTION - OPT_HELP].name))
3660                 opt_section = argv[++i];
3661             else if (strcmp(argv[i] + 1,
3662                          cmp_options[OPT_VERBOSITY - OPT_HELP].name)
3663                     == 0
3664                 && !set_verbosity(atoi(argv[++i])))
3665                 return 0;
3666         }
3667     }
3668     if (opt_section[0] == '\0') /* empty string */
3669         opt_section = DEFAULT_SECTION;
3670     return 1;
3671 }
3672 
cmp_main(int argc,char ** argv)3673 int cmp_main(int argc, char **argv)
3674 {
3675     char *configfile = NULL;
3676     int i;
3677     X509 *newcert = NULL;
3678     ENGINE *engine = NULL;
3679     int ret = 0; /* default: failure */
3680 
3681     if (!handle_opts_upfront(argc, argv))
3682         goto err;
3683 
3684     vpm = X509_VERIFY_PARAM_new();
3685     if (vpm == NULL) {
3686         CMP_err("out of memory");
3687         goto err;
3688     }
3689 
3690     /* read default values for options from config file */
3691     configfile = opt_config != NULL ? opt_config : default_config_file;
3692     if (configfile != NULL && configfile[0] != '\0' /* non-empty string */
3693         && (configfile != default_config_file
3694             || access(configfile, F_OK) != -1)) {
3695         CMP_info2("using section(s) '%s' of OpenSSL configuration file '%s'",
3696             opt_section, configfile);
3697         conf = app_load_config(configfile);
3698         if (conf == NULL) {
3699             goto err;
3700         } else {
3701             if (strcmp(opt_section, CMP_SECTION) == 0) { /* default */
3702                 if (!NCONF_get_section(conf, opt_section))
3703                     CMP_info2("no [%s] section found in config file '%s';"
3704                               " will thus use just [default] and unnamed section if present",
3705                         opt_section, configfile);
3706             } else {
3707                 const char *end = opt_section + strlen(opt_section);
3708 
3709                 while ((end = prev_item(opt_section, end)) != NULL) {
3710                     if (!NCONF_get_section(conf, opt_item)) {
3711                         CMP_err2("no [%s] section found in config file '%s'",
3712                             opt_item, configfile);
3713                         goto err;
3714                     }
3715                 }
3716             }
3717             ret = read_config();
3718             if (!set_verbosity(opt_verbosity)) /* just for checking range */
3719                 ret = -1;
3720             if (ret <= 0) {
3721                 if (ret == -1)
3722                     BIO_printf(bio_err, "Use -help for summary.\n");
3723                 goto err;
3724             }
3725         }
3726     }
3727     (void)BIO_flush(bio_err); /* prevent interference with opt_help() */
3728 
3729     cmp_ctx = OSSL_CMP_CTX_new(app_get0_libctx(), app_get0_propq());
3730     if (cmp_ctx == NULL)
3731         goto err;
3732 
3733     ret = get_opts(argc, argv);
3734     if (ret <= 0)
3735         goto err;
3736 
3737     ret = 0;
3738     if (!app_RAND_load())
3739         goto err;
3740 
3741     if (opt_batch)
3742         set_base_ui_method(UI_null());
3743 
3744     if (opt_engine != NULL) {
3745         engine = setup_engine_methods(opt_engine,
3746             0 /* not: ENGINE_METHOD_ALL */, 0);
3747         if (engine == NULL) {
3748             CMP_err1("cannot load engine %s", opt_engine);
3749             goto err;
3750         }
3751     }
3752 
3753     OSSL_CMP_CTX_set_log_verbosity(cmp_ctx, opt_verbosity);
3754     if (!OSSL_CMP_CTX_set_log_cb(cmp_ctx, print_to_bio_out)) {
3755         CMP_err1("cannot set up error reporting and logging for %s", prog);
3756         goto err;
3757     }
3758 
3759 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3760     if (opt_tls_cert == NULL && opt_tls_key == NULL && opt_tls_keypass == NULL
3761         && opt_tls_extra == NULL && opt_tls_trusted == NULL
3762         && opt_tls_host == NULL) {
3763         if (opt_tls_used)
3764             CMP_warn("-tls_used given without any other TLS options");
3765     } else if (!opt_tls_used) {
3766         CMP_warn("ignoring TLS options(s) since -tls_used is not given");
3767     }
3768     if (opt_port != NULL) {
3769         if (opt_tls_used) {
3770             CMP_err("-tls_used option not supported with -port option");
3771             goto err;
3772         }
3773         if (opt_server != NULL || opt_use_mock_srv) {
3774             CMP_err("The -port option excludes -server and -use_mock_srv");
3775             goto err;
3776         }
3777         if (opt_reqin != NULL || opt_reqout != NULL) {
3778             CMP_err("The -port option does not support -reqin and -reqout");
3779             goto err;
3780         }
3781         if (opt_rspin != NULL || opt_rspout != NULL) {
3782             CMP_err("The -port option does not support -rspin and -rspout");
3783             goto err;
3784         }
3785     }
3786 
3787     if (opt_server != NULL && opt_use_mock_srv) {
3788         CMP_err("cannot use both -server and -use_mock_srv options");
3789         goto err;
3790     }
3791     if ((opt_server == NULL || opt_use_mock_srv) && opt_tls_used) {
3792         CMP_warn("ignoring -tls_used option since -server is not given or -use_mock_srv is given");
3793         opt_tls_used = 0;
3794     }
3795 
3796 #endif
3797 
3798     if (opt_ignore_keyusage)
3799         (void)OSSL_CMP_CTX_set_option(cmp_ctx, OSSL_CMP_OPT_IGNORE_KEYUSAGE, 1);
3800     if (opt_no_cache_extracerts)
3801         (void)OSSL_CMP_CTX_set_option(cmp_ctx, OSSL_CMP_OPT_NO_CACHE_EXTRACERTS, 1);
3802 
3803     if (opt_reqout_only == NULL && (opt_use_mock_srv
3804 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3805             || opt_port != NULL
3806 #endif
3807             )) {
3808         OSSL_CMP_SRV_CTX *srv_ctx;
3809         OSSL_CMP_CTX *srv_cmp_ctx;
3810 
3811         if ((srv_ctx = setup_srv_ctx(engine)) == NULL)
3812             goto err;
3813         OSSL_CMP_CTX_set_transfer_cb_arg(cmp_ctx /* of client */, srv_ctx);
3814 
3815         srv_cmp_ctx = OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx);
3816         if (!OSSL_CMP_CTX_set_log_cb(srv_cmp_ctx, print_to_bio_err)) {
3817             CMP_err1("cannot set up server-side error reporting and logging for %s", prog);
3818             goto err;
3819         }
3820         OSSL_CMP_CTX_set_log_verbosity(srv_cmp_ctx, opt_verbosity);
3821 
3822 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3823         if (opt_port != NULL) { /* act as very basic CMP HTTP server only */
3824             ret = cmp_server(srv_cmp_ctx);
3825             goto err;
3826         }
3827 #endif
3828     }
3829 
3830     /* act as CMP client, possibly using internal mock server */
3831 
3832     if (opt_reqout_only != NULL) {
3833         const char *msg = "option is ignored since -reqout_only option is given";
3834 
3835 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3836         if (opt_port != NULL) {
3837             CMP_err("the -reqout_only client option does not combine with -port implying server behavior");
3838             goto err;
3839         }
3840         if (opt_server != NULL)
3841             CMP_warn1("-server %s", msg);
3842 #endif
3843         if (opt_use_mock_srv)
3844             CMP_warn1("-use_mock_srv %s", msg);
3845         if (opt_reqout != NULL)
3846             CMP_warn1("-reqout %s", msg);
3847         if (opt_rspin != NULL)
3848             CMP_warn1("-rspin %s", msg);
3849         if (opt_rspout != NULL)
3850             CMP_warn1("-rspout %s", msg);
3851         opt_reqout = opt_reqout_only;
3852     }
3853     if (opt_rspin != NULL) {
3854 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3855         if (opt_server != NULL)
3856             CMP_warn("-server option is not used if enough filenames given for -rspin");
3857 #endif
3858         if (opt_use_mock_srv)
3859             CMP_warn("-use_mock_srv option is not used if enough filenames given for -rspin");
3860     }
3861 
3862     if (!setup_client_ctx(cmp_ctx, engine)) {
3863         CMP_err("cannot set up CMP context");
3864         goto err;
3865     }
3866     for (i = 0; i < opt_repeat; i++) {
3867         /* everything is ready, now connect and perform the command! */
3868         switch (opt_cmd) {
3869         case CMP_IR:
3870             newcert = OSSL_CMP_exec_IR_ses(cmp_ctx);
3871             if (newcert != NULL)
3872                 ret = 1;
3873             break;
3874         case CMP_KUR:
3875             newcert = OSSL_CMP_exec_KUR_ses(cmp_ctx);
3876             if (newcert != NULL)
3877                 ret = 1;
3878             break;
3879         case CMP_CR:
3880             newcert = OSSL_CMP_exec_CR_ses(cmp_ctx);
3881             if (newcert != NULL)
3882                 ret = 1;
3883             break;
3884         case CMP_P10CR:
3885             newcert = OSSL_CMP_exec_P10CR_ses(cmp_ctx);
3886             if (newcert != NULL)
3887                 ret = 1;
3888             break;
3889         case CMP_RR:
3890             ret = OSSL_CMP_exec_RR_ses(cmp_ctx);
3891             break;
3892         case CMP_GENM:
3893             ret = do_genm(cmp_ctx);
3894         default:
3895             break;
3896         }
3897         if (OSSL_CMP_CTX_get_status(cmp_ctx) < OSSL_CMP_PKISTATUS_accepted) {
3898             /* we got no response, maybe even did not send request */
3899             ret = 0;
3900             if (reqout_only_done) {
3901                 ERR_clear_error();
3902                 ret = 1;
3903             }
3904             goto err;
3905         }
3906         print_status();
3907         if (!save_cert_or_delete(OSSL_CMP_CTX_get0_validatedSrvCert(cmp_ctx),
3908                 opt_srvcertout, "validated server"))
3909             ret = 0;
3910         if (!ret)
3911             goto err;
3912         ret = 0;
3913         if (save_free_certs(OSSL_CMP_CTX_get1_extraCertsIn(cmp_ctx),
3914                 opt_extracertsout, "extra")
3915             < 0)
3916             goto err;
3917         if (newcert != NULL && (opt_cmd == CMP_IR || opt_cmd == CMP_CR || opt_cmd == CMP_KUR || opt_cmd == CMP_P10CR)) {
3918             STACK_OF(X509) *newchain = OSSL_CMP_CTX_get1_newChain(cmp_ctx);
3919 
3920             if (newcert != NULL && newchain != NULL /* NULL is on error only */
3921                 && opt_certout != NULL && opt_chainout != NULL
3922                 && strcmp(opt_certout, opt_chainout) == 0) {
3923                 if (!X509_add_cert(newchain, newcert, X509_ADD_FLAG_PREPEND | X509_ADD_FLAG_UP_REF)) {
3924                     sk_X509_pop_free(newchain, X509_free);
3925                     goto err;
3926                 }
3927                 if (!save_free_certs(newchain, opt_chainout, "newly enrolled cert and chain"))
3928                     goto err;
3929             } else {
3930                 if (save_free_certs(newchain, opt_chainout, "chain") < 0
3931                     || !save_cert_or_delete(newcert, opt_certout, "newly enrolled"))
3932                     goto err;
3933             }
3934             if (save_free_certs(OSSL_CMP_CTX_get1_caPubs(cmp_ctx),
3935                     opt_cacertsout, "CA")
3936                 < 0)
3937                 goto err;
3938             if (opt_centralkeygen) {
3939                 EVP_CIPHER *cipher = NULL;
3940                 char *pass_string = NULL;
3941                 BIO *out;
3942                 int result = 1;
3943                 EVP_PKEY *new_key = OSSL_CMP_CTX_get0_newPkey(cmp_ctx, 1 /* priv */);
3944 
3945                 if (new_key == NULL)
3946                     goto err;
3947                 if ((out = bio_open_owner(opt_newkeyout, FORMAT_PEM, 1)) == NULL)
3948                     goto err;
3949                 if (opt_newkeypass != NULL) {
3950                     pass_string = get_passwd(opt_newkeypass,
3951                         "Centrally generated private key password");
3952                     cipher = EVP_CIPHER_fetch(app_get0_libctx(), SN_aes_256_cbc, app_get0_propq());
3953                 }
3954 
3955                 CMP_info1("saving centrally generated key to file '%s'", opt_newkeyout);
3956                 if (PEM_write_bio_PrivateKey(out, new_key, cipher, NULL, 0, NULL,
3957                         (void *)pass_string)
3958                     <= 0)
3959                     result = 0;
3960 
3961                 BIO_free(out);
3962                 clear_free(pass_string);
3963                 EVP_CIPHER_free(cipher);
3964                 if (!result)
3965                     goto err;
3966             }
3967         }
3968         if (!OSSL_CMP_CTX_reinit(cmp_ctx))
3969             goto err;
3970     }
3971     ret = 1;
3972 
3973 err:
3974     /* in case we ended up here on error without proper cleaning */
3975     cleanse(opt_keypass);
3976     cleanse(opt_newkeypass);
3977     cleanse(opt_otherpass);
3978 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3979     cleanse(opt_tls_keypass);
3980 #endif
3981     cleanse(opt_secret);
3982     cleanse(opt_srv_keypass);
3983     cleanse(opt_srv_secret);
3984 
3985     if (ret != 1)
3986         OSSL_CMP_CTX_print_errors(cmp_ctx);
3987 
3988     if (cmp_ctx != NULL) {
3989 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3990         APP_HTTP_TLS_INFO *info = OSSL_CMP_CTX_get_http_cb_arg(cmp_ctx);
3991 
3992         (void)OSSL_CMP_CTX_set_http_cb_arg(cmp_ctx, NULL);
3993 #endif
3994         ossl_cmp_mock_srv_free(OSSL_CMP_CTX_get_transfer_cb_arg(cmp_ctx));
3995         X509_STORE_free(OSSL_CMP_CTX_get_certConf_cb_arg(cmp_ctx));
3996         /* cannot free info already here, as it may be used indirectly by: */
3997         OSSL_CMP_CTX_free(cmp_ctx);
3998 #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_HTTP)
3999         if (info != NULL) {
4000             OPENSSL_free((char *)info->server);
4001             OPENSSL_free((char *)info->port);
4002             APP_HTTP_TLS_INFO_free(info);
4003         }
4004 #endif
4005     }
4006     X509_VERIFY_PARAM_free(vpm);
4007     release_engine(engine);
4008 
4009     NCONF_free(conf); /* must not do as long as opt_... variables are used */
4010     OSSL_CMP_log_close();
4011 
4012     return ret == 0 ? EXIT_FAILURE : EXIT_SUCCESS; /* ret == -1 for -help */
4013 }
4014