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