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