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