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