VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/apps/cmp.c

Last change on this file was 108206, checked in by vboxsync, 3 months ago

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette