]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
a28d06f3 | 2 | * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. |
c7235be6 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
c7235be6 UM |
8 | */ |
9 | ||
f3852635 | 10 | #include <openssl/opensslconf.h> |
1ae56f2f RS |
11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include "apps.h" | |
15 | #include "progs.h" | |
16 | #include <openssl/bio.h> | |
17 | #include <openssl/err.h> | |
18 | #include <openssl/pem.h> | |
19 | #include <openssl/rand.h> | |
20 | #include <openssl/ts.h> | |
21 | #include <openssl/bn.h> | |
c7235be6 | 22 | |
18cd23df | 23 | /* Request nonce length, in bits (must be a multiple of 8). */ |
1ae56f2f | 24 | #define NONCE_LENGTH 64 |
c7235be6 | 25 | |
18cd23df | 26 | /* Name of config entry that defines the OID file. */ |
1ae56f2f | 27 | #define ENV_OID_FILE "oid_file" |
c7235be6 | 28 | |
18cd23df | 29 | /* Is |EXACTLY_ONE| of three pointers set? */ |
1ae56f2f | 30 | #define EXACTLY_ONE(a, b, c) \ |
18cd23df RS |
31 | (( a && !b && !c) || \ |
32 | ( b && !a && !c) || \ | |
33 | ( c && !a && !b)) | |
c7235be6 UM |
34 | |
35 | static ASN1_OBJECT *txt2obj(const char *oid); | |
36 | static CONF *load_config_file(const char *configfile); | |
37 | ||
38 | /* Query related functions. */ | |
cc696296 | 39 | static int query_command(const char *data, const char *digest, |
0f113f3e MC |
40 | const EVP_MD *md, const char *policy, int no_nonce, |
41 | int cert, const char *in, const char *out, int text); | |
cc696296 | 42 | static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md, |
0f113f3e | 43 | const char *policy, int no_nonce, int cert); |
cc696296 | 44 | static int create_digest(BIO *input, const char *digest, |
0f113f3e | 45 | const EVP_MD *md, unsigned char **md_value); |
c7235be6 UM |
46 | static ASN1_INTEGER *create_nonce(int bits); |
47 | ||
48 | /* Reply related functions. */ | |
cc696296 F |
49 | static int reply_command(CONF *conf, const char *section, const char *engine, |
50 | const char *queryfile, const char *passin, const char *inkey, | |
51 | const EVP_MD *md, const char *signer, const char *chain, | |
52 | const char *policy, const char *in, int token_in, | |
53 | const char *out, int token_out, int text); | |
c7235be6 | 54 | static TS_RESP *read_PKCS7(BIO *in_bio); |
cc696296 F |
55 | static TS_RESP *create_response(CONF *conf, const char *section, const char *engine, |
56 | const char *queryfile, const char *passin, | |
57 | const char *inkey, const EVP_MD *md, const char *signer, | |
58 | const char *chain, const char *policy); | |
0f113f3e | 59 | static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data); |
c7235be6 UM |
60 | static ASN1_INTEGER *next_serial(const char *serialfile); |
61 | static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial); | |
62 | ||
63 | /* Verify related functions. */ | |
cc696296 F |
64 | static int verify_command(const char *data, const char *digest, const char *queryfile, |
65 | const char *in, int token_in, | |
fd3397fc RL |
66 | const char *CApath, const char *CAfile, |
67 | const char *CAstore, | |
68 | const char *untrusted, X509_VERIFY_PARAM *vpm); | |
cc696296 F |
69 | static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest, |
70 | const char *queryfile, | |
71 | const char *CApath, const char *CAfile, | |
fd3397fc | 72 | const char *CAstore, |
cc696296 | 73 | const char *untrusted, |
08538fc0 | 74 | X509_VERIFY_PARAM *vpm); |
cc696296 | 75 | static X509_STORE *create_cert_store(const char *CApath, const char *CAfile, |
fd3397fc | 76 | const char *CAstore, X509_VERIFY_PARAM *vpm); |
6d23cf97 | 77 | static int verify_cb(int ok, X509_STORE_CTX *ctx); |
c7235be6 | 78 | |
7e1b7485 RS |
79 | typedef enum OPTION_choice { |
80 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
81 | OPT_ENGINE, OPT_CONFIG, OPT_SECTION, OPT_QUERY, OPT_DATA, | |
3ee1eac2 | 82 | OPT_DIGEST, OPT_TSPOLICY, OPT_NO_NONCE, OPT_CERT, |
7e1b7485 RS |
83 | OPT_IN, OPT_TOKEN_IN, OPT_OUT, OPT_TOKEN_OUT, OPT_TEXT, |
84 | OPT_REPLY, OPT_QUERYFILE, OPT_PASSIN, OPT_INKEY, OPT_SIGNER, | |
fd3397fc | 85 | OPT_CHAIN, OPT_VERIFY, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE, OPT_UNTRUSTED, |
6bd4e3f2 | 86 | OPT_MD, OPT_V_ENUM, OPT_R_ENUM, OPT_PROV_ENUM |
7e1b7485 RS |
87 | } OPTION_CHOICE; |
88 | ||
44c83ebd | 89 | const OPTIONS ts_options[] = { |
5388f986 | 90 | OPT_SECTION("General"), |
7e1b7485 RS |
91 | {"help", OPT_HELP, '-', "Display this summary"}, |
92 | {"config", OPT_CONFIG, '<', "Configuration file"}, | |
93 | {"section", OPT_SECTION, 's', "Section to use within config file"}, | |
1ae56f2f | 94 | #ifndef OPENSSL_NO_ENGINE |
5388f986 | 95 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
1ae56f2f | 96 | #endif |
48b53522 | 97 | {"inkey", OPT_INKEY, 's', "File with private key for reply"}, |
12d56b29 | 98 | {"signer", OPT_SIGNER, 's', "Signer certificate file"}, |
7e1b7485 | 99 | {"chain", OPT_CHAIN, '<', "File with signer CA chain"}, |
7e1b7485 | 100 | {"CAfile", OPT_CAFILE, '<', "File with trusted CA certs"}, |
2b264aee | 101 | {"CApath", OPT_CAPATH, '/', "Path to trusted CA files"}, |
fd3397fc | 102 | {"CAstore", OPT_CASTORE, ':', "URI to trusted CA store"}, |
7e1b7485 | 103 | {"untrusted", OPT_UNTRUSTED, '<', "File with untrusted certs"}, |
5388f986 RS |
104 | {"token_in", OPT_TOKEN_IN, '-', "Input is a PKCS#7 file"}, |
105 | {"token_out", OPT_TOKEN_OUT, '-', "Output is a PKCS#7 file"}, | |
106 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, | |
9c3bcfa0 | 107 | {"", OPT_MD, '-', "Any supported digest"}, |
5388f986 RS |
108 | |
109 | OPT_SECTION("Query"), | |
110 | {"query", OPT_QUERY, '-', "Generate a TS query"}, | |
111 | {"data", OPT_DATA, '<', "File to hash"}, | |
112 | {"digest", OPT_DIGEST, 's', "Digest (as a hex string)"}, | |
113 | {"queryfile", OPT_QUERYFILE, '<', "File containing a TS query"}, | |
114 | {"cert", OPT_CERT, '-', "Put cert request into query"}, | |
115 | {"in", OPT_IN, '<', "Input file"}, | |
116 | ||
117 | OPT_SECTION("Verify"), | |
118 | {"verify", OPT_VERIFY, '-', "Verify a TS response"}, | |
119 | {"reply", OPT_REPLY, '-', "Generate a TS reply"}, | |
120 | {"tspolicy", OPT_TSPOLICY, 's', "Policy OID to use"}, | |
121 | {"no_nonce", OPT_NO_NONCE, '-', "Do not include a nonce"}, | |
122 | {"out", OPT_OUT, '>', "Output file"}, | |
123 | {"text", OPT_TEXT, '-', "Output text (not DER)"}, | |
124 | ||
125 | OPT_R_OPTIONS, | |
08538fc0 | 126 | OPT_V_OPTIONS, |
6bd4e3f2 | 127 | OPT_PROV_OPTIONS, |
7e1b7485 RS |
128 | {NULL} |
129 | }; | |
c7235be6 | 130 | |
7e1b7485 | 131 | /* |
60250017 | 132 | * This command is so complex, special help is needed. |
7e1b7485 RS |
133 | */ |
134 | static char* opt_helplist[] = { | |
5388f986 | 135 | "", |
7e1b7485 | 136 | "Typical uses:", |
5388f986 RS |
137 | " openssl ts -query [-rand file...] [-config file] [-data file]", |
138 | " [-digest hexstring] [-tspolicy oid] [-no_nonce] [-cert]", | |
139 | " [-in file] [-out file] [-text]", | |
140 | "", | |
141 | " openssl ts -reply [-config file] [-section tsa_section]", | |
142 | " [-queryfile file] [-passin password]", | |
143 | " [-signer tsa_cert.pem] [-inkey private_key.pem]", | |
144 | " [-chain certs_file.pem] [-tspolicy oid]", | |
145 | " [-in file] [-token_in] [-out file] [-token_out]", | |
1ae56f2f | 146 | #ifndef OPENSSL_NO_ENGINE |
5388f986 | 147 | " [-text] [-engine id]", |
1ae56f2f | 148 | #else |
5388f986 | 149 | " [-text]", |
1ae56f2f | 150 | #endif |
5388f986 RS |
151 | "", |
152 | " openssl ts -verify -CApath dir -CAfile file.pem -CAstore uri", | |
153 | " -untrusted file.pem [-data file] [-digest hexstring]", | |
154 | " [-queryfile file] -in file [-token_in] ...", | |
7e1b7485 RS |
155 | NULL, |
156 | }; | |
157 | ||
158 | int ts_main(int argc, char **argv) | |
0f113f3e | 159 | { |
0f113f3e | 160 | CONF *conf = NULL; |
cc696296 F |
161 | const char *CAfile = NULL, *untrusted = NULL, *prog; |
162 | const char *configfile = default_config_file, *engine = NULL; | |
d0190e11 | 163 | const char *section = NULL, *digestname = NULL; |
cc696296 F |
164 | char **helpp; |
165 | char *password = NULL; | |
3ee1eac2 | 166 | char *data = NULL, *digest = NULL, *policy = NULL; |
7e1b7485 RS |
167 | char *in = NULL, *out = NULL, *queryfile = NULL, *passin = NULL; |
168 | char *inkey = NULL, *signer = NULL, *chain = NULL, *CApath = NULL; | |
fd3397fc | 169 | char *CAstore = NULL; |
0f113f3e | 170 | const EVP_MD *md = NULL; |
7e1b7485 RS |
171 | OPTION_CHOICE o, mode = OPT_ERR; |
172 | int ret = 1, no_nonce = 0, cert = 0, text = 0; | |
08538fc0 | 173 | int vpmtouched = 0; |
174 | X509_VERIFY_PARAM *vpm = NULL; | |
0f113f3e MC |
175 | /* Input is ContentInfo instead of TimeStampResp. */ |
176 | int token_in = 0; | |
177 | /* Output is ContentInfo instead of TimeStampResp. */ | |
178 | int token_out = 0; | |
0f113f3e | 179 | |
08538fc0 | 180 | if ((vpm = X509_VERIFY_PARAM_new()) == NULL) |
181 | goto end; | |
182 | ||
7e1b7485 RS |
183 | prog = opt_init(argc, argv, ts_options); |
184 | while ((o = opt_next()) != OPT_EOF) { | |
185 | switch (o) { | |
186 | case OPT_EOF: | |
187 | case OPT_ERR: | |
188 | opthelp: | |
189 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
190 | goto end; | |
191 | case OPT_HELP: | |
192 | opt_help(ts_options); | |
193 | for (helpp = opt_helplist; *helpp; ++helpp) | |
194 | BIO_printf(bio_err, "%s\n", *helpp); | |
195 | ret = 0; | |
196 | goto end; | |
197 | case OPT_CONFIG: | |
198 | configfile = opt_arg(); | |
199 | break; | |
200 | case OPT_SECTION: | |
201 | section = opt_arg(); | |
202 | break; | |
203 | case OPT_QUERY: | |
204 | case OPT_REPLY: | |
205 | case OPT_VERIFY: | |
206 | if (mode != OPT_ERR) | |
207 | goto opthelp; | |
208 | mode = o; | |
209 | break; | |
210 | case OPT_DATA: | |
211 | data = opt_arg(); | |
212 | break; | |
213 | case OPT_DIGEST: | |
214 | digest = opt_arg(); | |
215 | break; | |
3ee1eac2 RS |
216 | case OPT_R_CASES: |
217 | if (!opt_rand(o)) | |
218 | goto end; | |
7e1b7485 | 219 | break; |
6bd4e3f2 P |
220 | case OPT_PROV_CASES: |
221 | if (!opt_provider(o)) | |
222 | goto end; | |
223 | break; | |
08538fc0 | 224 | case OPT_TSPOLICY: |
7e1b7485 RS |
225 | policy = opt_arg(); |
226 | break; | |
227 | case OPT_NO_NONCE: | |
0f113f3e | 228 | no_nonce = 1; |
7e1b7485 RS |
229 | break; |
230 | case OPT_CERT: | |
0f113f3e | 231 | cert = 1; |
7e1b7485 RS |
232 | break; |
233 | case OPT_IN: | |
234 | in = opt_arg(); | |
235 | break; | |
236 | case OPT_TOKEN_IN: | |
0f113f3e | 237 | token_in = 1; |
7e1b7485 RS |
238 | break; |
239 | case OPT_OUT: | |
240 | out = opt_arg(); | |
241 | break; | |
242 | case OPT_TOKEN_OUT: | |
0f113f3e | 243 | token_out = 1; |
7e1b7485 RS |
244 | break; |
245 | case OPT_TEXT: | |
0f113f3e | 246 | text = 1; |
7e1b7485 RS |
247 | break; |
248 | case OPT_QUERYFILE: | |
249 | queryfile = opt_arg(); | |
250 | break; | |
251 | case OPT_PASSIN: | |
252 | passin = opt_arg(); | |
253 | break; | |
254 | case OPT_INKEY: | |
255 | inkey = opt_arg(); | |
256 | break; | |
257 | case OPT_SIGNER: | |
258 | signer = opt_arg(); | |
259 | break; | |
260 | case OPT_CHAIN: | |
261 | chain = opt_arg(); | |
262 | break; | |
263 | case OPT_CAPATH: | |
264 | CApath = opt_arg(); | |
265 | break; | |
266 | case OPT_CAFILE: | |
267 | CAfile = opt_arg(); | |
268 | break; | |
fd3397fc RL |
269 | case OPT_CASTORE: |
270 | CAstore = opt_arg(); | |
271 | break; | |
7e1b7485 RS |
272 | case OPT_UNTRUSTED: |
273 | untrusted = opt_arg(); | |
274 | break; | |
275 | case OPT_ENGINE: | |
276 | engine = opt_arg(); | |
277 | break; | |
278 | case OPT_MD: | |
d0190e11 | 279 | digestname = opt_unknown(); |
7e1b7485 | 280 | break; |
08538fc0 | 281 | case OPT_V_CASES: |
282 | if (!opt_verify(o, vpm)) | |
283 | goto end; | |
284 | vpmtouched++; | |
285 | break; | |
7e1b7485 | 286 | } |
0f113f3e | 287 | } |
021410ea RS |
288 | |
289 | /* No extra arguments. */ | |
290 | argc = opt_num_rest(); | |
291 | if (argc != 0 || mode == OPT_ERR) | |
7e1b7485 | 292 | goto opthelp; |
0f113f3e | 293 | |
51e5df0e | 294 | app_RAND_load(); |
d0190e11 RS |
295 | if (digestname != NULL) { |
296 | if (!opt_md(digestname, &md)) | |
297 | goto opthelp; | |
298 | } | |
7e1b7485 RS |
299 | if (mode == OPT_REPLY && passin && |
300 | !app_passwd(passin, NULL, &password, NULL)) { | |
0f113f3e | 301 | BIO_printf(bio_err, "Error getting password.\n"); |
7e1b7485 | 302 | goto end; |
0f113f3e MC |
303 | } |
304 | ||
dd0139f4 | 305 | if ((conf = load_config_file(configfile)) == NULL) |
306 | goto end; | |
c821defc | 307 | if (configfile != default_config_file && !app_load_modules(conf)) |
296f54ee RL |
308 | goto end; |
309 | ||
18cd23df | 310 | /* Check parameter consistency and execute the appropriate function. */ |
f3b3d7f0 | 311 | if (mode == OPT_QUERY) { |
08538fc0 | 312 | if (vpmtouched) |
313 | goto opthelp; | |
18cd23df | 314 | if ((data != NULL) && (digest != NULL)) |
7e1b7485 | 315 | goto opthelp; |
0f113f3e MC |
316 | ret = !query_command(data, digest, md, policy, no_nonce, cert, |
317 | in, out, text); | |
f3b3d7f0 | 318 | } else if (mode == OPT_REPLY) { |
08538fc0 | 319 | if (vpmtouched) |
320 | goto opthelp; | |
18cd23df RS |
321 | if ((in != NULL) && (queryfile != NULL)) |
322 | goto opthelp; | |
0f113f3e | 323 | if (in == NULL) { |
18cd23df | 324 | if ((conf == NULL) || (token_in != 0)) |
7e1b7485 | 325 | goto opthelp; |
0f113f3e | 326 | } |
0f113f3e | 327 | ret = !reply_command(conf, section, engine, queryfile, |
e20b4727 | 328 | password, inkey, md, signer, chain, policy, |
0f113f3e | 329 | in, token_in, out, token_out, text); |
f3b3d7f0 RS |
330 | |
331 | } else if (mode == OPT_VERIFY) { | |
18cd23df | 332 | if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest)) |
7e1b7485 | 333 | goto opthelp; |
0f113f3e | 334 | ret = !verify_command(data, digest, queryfile, in, token_in, |
fd3397fc | 335 | CApath, CAfile, CAstore, untrusted, |
08538fc0 | 336 | vpmtouched ? vpm : NULL); |
f3b3d7f0 RS |
337 | } else { |
338 | goto opthelp; | |
0f113f3e MC |
339 | } |
340 | ||
7e1b7485 | 341 | end: |
08538fc0 | 342 | X509_VERIFY_PARAM_free(vpm); |
0f113f3e MC |
343 | NCONF_free(conf); |
344 | OPENSSL_free(password); | |
26a7d938 | 345 | return ret; |
0f113f3e | 346 | } |
c7235be6 UM |
347 | |
348 | /* | |
349 | * Configuration file-related function definitions. | |
350 | */ | |
351 | ||
352 | static ASN1_OBJECT *txt2obj(const char *oid) | |
0f113f3e MC |
353 | { |
354 | ASN1_OBJECT *oid_obj = NULL; | |
c7235be6 | 355 | |
75ebbd9a | 356 | if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL) |
0f113f3e | 357 | BIO_printf(bio_err, "cannot convert %s to OID\n", oid); |
c7235be6 | 358 | |
0f113f3e MC |
359 | return oid_obj; |
360 | } | |
c7235be6 UM |
361 | |
362 | static CONF *load_config_file(const char *configfile) | |
0f113f3e | 363 | { |
cc01d217 | 364 | CONF *conf = app_load_config(configfile); |
0f113f3e MC |
365 | |
366 | if (conf != NULL) { | |
367 | const char *p; | |
368 | ||
369 | BIO_printf(bio_err, "Using configuration from %s\n", configfile); | |
370 | p = NCONF_get_string(conf, NULL, ENV_OID_FILE); | |
371 | if (p != NULL) { | |
372 | BIO *oid_bio = BIO_new_file(p, "r"); | |
373 | if (!oid_bio) | |
374 | ERR_print_errors(bio_err); | |
375 | else { | |
376 | OBJ_create_objects(oid_bio); | |
377 | BIO_free_all(oid_bio); | |
378 | } | |
379 | } else | |
380 | ERR_clear_error(); | |
7e1b7485 | 381 | if (!add_oid_section(conf)) |
0f113f3e MC |
382 | ERR_print_errors(bio_err); |
383 | } | |
384 | return conf; | |
385 | } | |
c7235be6 UM |
386 | |
387 | /* | |
388 | * Query-related method definitions. | |
389 | */ | |
cc696296 | 390 | static int query_command(const char *data, const char *digest, const EVP_MD *md, |
0f113f3e MC |
391 | const char *policy, int no_nonce, |
392 | int cert, const char *in, const char *out, int text) | |
393 | { | |
394 | int ret = 0; | |
395 | TS_REQ *query = NULL; | |
396 | BIO *in_bio = NULL; | |
397 | BIO *data_bio = NULL; | |
398 | BIO *out_bio = NULL; | |
399 | ||
18cd23df | 400 | /* Build query object. */ |
0f113f3e | 401 | if (in != NULL) { |
bdd58d98 | 402 | if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL) |
0f113f3e MC |
403 | goto end; |
404 | query = d2i_TS_REQ_bio(in_bio, NULL); | |
405 | } else { | |
75ebbd9a | 406 | if (digest == NULL |
bdd58d98 | 407 | && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL) |
0f113f3e | 408 | goto end; |
0f113f3e | 409 | query = create_query(data_bio, digest, md, policy, no_nonce, cert); |
0f113f3e MC |
410 | } |
411 | if (query == NULL) | |
412 | goto end; | |
413 | ||
0f113f3e | 414 | if (text) { |
bdd58d98 RL |
415 | if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL) |
416 | goto end; | |
0f113f3e MC |
417 | if (!TS_REQ_print_bio(out_bio, query)) |
418 | goto end; | |
419 | } else { | |
bdd58d98 RL |
420 | if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL) |
421 | goto end; | |
0f113f3e MC |
422 | if (!i2d_TS_REQ_bio(out_bio, query)) |
423 | goto end; | |
424 | } | |
425 | ||
426 | ret = 1; | |
c7235be6 UM |
427 | |
428 | end: | |
0f113f3e | 429 | ERR_print_errors(bio_err); |
0f113f3e MC |
430 | BIO_free_all(in_bio); |
431 | BIO_free_all(data_bio); | |
432 | BIO_free_all(out_bio); | |
433 | TS_REQ_free(query); | |
0f113f3e MC |
434 | return ret; |
435 | } | |
c7235be6 | 436 | |
cc696296 | 437 | static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md, |
0f113f3e MC |
438 | const char *policy, int no_nonce, int cert) |
439 | { | |
440 | int ret = 0; | |
441 | TS_REQ *ts_req = NULL; | |
442 | int len; | |
443 | TS_MSG_IMPRINT *msg_imprint = NULL; | |
444 | X509_ALGOR *algo = NULL; | |
445 | unsigned char *data = NULL; | |
446 | ASN1_OBJECT *policy_obj = NULL; | |
447 | ASN1_INTEGER *nonce_asn1 = NULL; | |
448 | ||
a6dfa188 | 449 | if (md == NULL && (md = EVP_get_digestbyname("sha256")) == NULL) |
0f113f3e | 450 | goto err; |
75ebbd9a | 451 | if ((ts_req = TS_REQ_new()) == NULL) |
0f113f3e | 452 | goto err; |
0f113f3e MC |
453 | if (!TS_REQ_set_version(ts_req, 1)) |
454 | goto err; | |
75ebbd9a | 455 | if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL) |
0f113f3e | 456 | goto err; |
75ebbd9a | 457 | if ((algo = X509_ALGOR_new()) == NULL) |
0f113f3e | 458 | goto err; |
75ebbd9a | 459 | if ((algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))) == NULL) |
0f113f3e | 460 | goto err; |
75ebbd9a | 461 | if ((algo->parameter = ASN1_TYPE_new()) == NULL) |
0f113f3e MC |
462 | goto err; |
463 | algo->parameter->type = V_ASN1_NULL; | |
464 | if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo)) | |
465 | goto err; | |
0f113f3e MC |
466 | if ((len = create_digest(data_bio, digest, md, &data)) == 0) |
467 | goto err; | |
468 | if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len)) | |
469 | goto err; | |
0f113f3e MC |
470 | if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint)) |
471 | goto err; | |
75ebbd9a | 472 | if (policy && (policy_obj = txt2obj(policy)) == NULL) |
0f113f3e MC |
473 | goto err; |
474 | if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj)) | |
475 | goto err; | |
476 | ||
477 | /* Setting nonce if requested. */ | |
75ebbd9a | 478 | if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL) |
0f113f3e MC |
479 | goto err; |
480 | if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1)) | |
481 | goto err; | |
0f113f3e MC |
482 | if (!TS_REQ_set_cert_req(ts_req, cert)) |
483 | goto err; | |
484 | ||
485 | ret = 1; | |
c7235be6 | 486 | err: |
0f113f3e MC |
487 | if (!ret) { |
488 | TS_REQ_free(ts_req); | |
489 | ts_req = NULL; | |
490 | BIO_printf(bio_err, "could not create query\n"); | |
18cd23df | 491 | ERR_print_errors(bio_err); |
0f113f3e MC |
492 | } |
493 | TS_MSG_IMPRINT_free(msg_imprint); | |
494 | X509_ALGOR_free(algo); | |
495 | OPENSSL_free(data); | |
496 | ASN1_OBJECT_free(policy_obj); | |
497 | ASN1_INTEGER_free(nonce_asn1); | |
498 | return ts_req; | |
499 | } | |
c7235be6 | 500 | |
cc696296 | 501 | static int create_digest(BIO *input, const char *digest, const EVP_MD *md, |
0f113f3e MC |
502 | unsigned char **md_value) |
503 | { | |
504 | int md_value_len; | |
d166ed8c DSH |
505 | int rv = 0; |
506 | EVP_MD_CTX *md_ctx = NULL; | |
0f113f3e MC |
507 | |
508 | md_value_len = EVP_MD_size(md); | |
509 | if (md_value_len < 0) | |
18cd23df RS |
510 | return 0; |
511 | ||
2234212c | 512 | if (input != NULL) { |
0f113f3e MC |
513 | unsigned char buffer[4096]; |
514 | int length; | |
515 | ||
d166ed8c | 516 | md_ctx = EVP_MD_CTX_new(); |
6e59a892 RL |
517 | if (md_ctx == NULL) |
518 | return 0; | |
68dc6824 | 519 | *md_value = app_malloc(md_value_len, "digest buffer"); |
d166ed8c DSH |
520 | if (!EVP_DigestInit(md_ctx, md)) |
521 | goto err; | |
0f113f3e | 522 | while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) { |
d166ed8c DSH |
523 | if (!EVP_DigestUpdate(md_ctx, buffer, length)) |
524 | goto err; | |
0f113f3e | 525 | } |
d166ed8c DSH |
526 | if (!EVP_DigestFinal(md_ctx, *md_value, NULL)) |
527 | goto err; | |
528 | md_value_len = EVP_MD_size(md); | |
0f113f3e | 529 | } else { |
0f113f3e | 530 | long digest_len; |
12a765a5 | 531 | |
14f051a0 | 532 | *md_value = OPENSSL_hexstr2buf(digest, &digest_len); |
12a765a5 | 533 | if (*md_value == NULL || md_value_len != digest_len) { |
0f113f3e MC |
534 | OPENSSL_free(*md_value); |
535 | *md_value = NULL; | |
536 | BIO_printf(bio_err, "bad digest, %d bytes " | |
537 | "must be specified\n", md_value_len); | |
18cd23df | 538 | return 0; |
0f113f3e MC |
539 | } |
540 | } | |
d166ed8c DSH |
541 | rv = md_value_len; |
542 | err: | |
543 | EVP_MD_CTX_free(md_ctx); | |
544 | return rv; | |
0f113f3e | 545 | } |
c7235be6 UM |
546 | |
547 | static ASN1_INTEGER *create_nonce(int bits) | |
0f113f3e MC |
548 | { |
549 | unsigned char buf[20]; | |
550 | ASN1_INTEGER *nonce = NULL; | |
551 | int len = (bits - 1) / 8 + 1; | |
552 | int i; | |
553 | ||
0f113f3e MC |
554 | if (len > (int)sizeof(buf)) |
555 | goto err; | |
556 | if (RAND_bytes(buf, len) <= 0) | |
557 | goto err; | |
558 | ||
559 | /* Find the first non-zero byte and creating ASN1_INTEGER object. */ | |
75ebbd9a RS |
560 | for (i = 0; i < len && !buf[i]; ++i) |
561 | continue; | |
562 | if ((nonce = ASN1_INTEGER_new()) == NULL) | |
0f113f3e MC |
563 | goto err; |
564 | OPENSSL_free(nonce->data); | |
0f113f3e | 565 | nonce->length = len - i; |
68dc6824 | 566 | nonce->data = app_malloc(nonce->length + 1, "nonce buffer"); |
0f113f3e | 567 | memcpy(nonce->data, buf + i, nonce->length); |
0f113f3e | 568 | return nonce; |
18cd23df | 569 | |
c7235be6 | 570 | err: |
0f113f3e MC |
571 | BIO_printf(bio_err, "could not create nonce\n"); |
572 | ASN1_INTEGER_free(nonce); | |
573 | return NULL; | |
574 | } | |
575 | ||
c7235be6 UM |
576 | /* |
577 | * Reply-related method definitions. | |
578 | */ | |
579 | ||
cc696296 F |
580 | static int reply_command(CONF *conf, const char *section, const char *engine, |
581 | const char *queryfile, const char *passin, const char *inkey, | |
582 | const EVP_MD *md, const char *signer, const char *chain, | |
583 | const char *policy, const char *in, int token_in, | |
584 | const char *out, int token_out, int text) | |
0f113f3e MC |
585 | { |
586 | int ret = 0; | |
587 | TS_RESP *response = NULL; | |
588 | BIO *in_bio = NULL; | |
589 | BIO *query_bio = NULL; | |
590 | BIO *inkey_bio = NULL; | |
591 | BIO *signer_bio = NULL; | |
592 | BIO *out_bio = NULL; | |
593 | ||
0f113f3e MC |
594 | if (in != NULL) { |
595 | if ((in_bio = BIO_new_file(in, "rb")) == NULL) | |
596 | goto end; | |
597 | if (token_in) { | |
0f113f3e MC |
598 | response = read_PKCS7(in_bio); |
599 | } else { | |
0f113f3e MC |
600 | response = d2i_TS_RESP_bio(in_bio, NULL); |
601 | } | |
602 | } else { | |
603 | response = create_response(conf, section, engine, queryfile, | |
e20b4727 | 604 | passin, inkey, md, signer, chain, policy); |
2234212c | 605 | if (response != NULL) |
0f113f3e MC |
606 | BIO_printf(bio_err, "Response has been generated.\n"); |
607 | else | |
608 | BIO_printf(bio_err, "Response is not generated.\n"); | |
609 | } | |
610 | if (response == NULL) | |
611 | goto end; | |
612 | ||
18cd23df | 613 | /* Write response. */ |
0f113f3e | 614 | if (text) { |
bdd58d98 RL |
615 | if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL) |
616 | goto end; | |
0f113f3e MC |
617 | if (token_out) { |
618 | TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response); | |
619 | if (!TS_TST_INFO_print_bio(out_bio, tst_info)) | |
620 | goto end; | |
621 | } else { | |
622 | if (!TS_RESP_print_bio(out_bio, response)) | |
623 | goto end; | |
624 | } | |
625 | } else { | |
bdd58d98 RL |
626 | if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL) |
627 | goto end; | |
0f113f3e MC |
628 | if (token_out) { |
629 | PKCS7 *token = TS_RESP_get_token(response); | |
630 | if (!i2d_PKCS7_bio(out_bio, token)) | |
631 | goto end; | |
632 | } else { | |
633 | if (!i2d_TS_RESP_bio(out_bio, response)) | |
634 | goto end; | |
635 | } | |
636 | } | |
637 | ||
638 | ret = 1; | |
c7235be6 UM |
639 | |
640 | end: | |
0f113f3e | 641 | ERR_print_errors(bio_err); |
0f113f3e MC |
642 | BIO_free_all(in_bio); |
643 | BIO_free_all(query_bio); | |
644 | BIO_free_all(inkey_bio); | |
645 | BIO_free_all(signer_bio); | |
646 | BIO_free_all(out_bio); | |
647 | TS_RESP_free(response); | |
0f113f3e MC |
648 | return ret; |
649 | } | |
c7235be6 UM |
650 | |
651 | /* Reads a PKCS7 token and adds default 'granted' status info to it. */ | |
652 | static TS_RESP *read_PKCS7(BIO *in_bio) | |
0f113f3e MC |
653 | { |
654 | int ret = 0; | |
655 | PKCS7 *token = NULL; | |
656 | TS_TST_INFO *tst_info = NULL; | |
657 | TS_RESP *resp = NULL; | |
658 | TS_STATUS_INFO *si = NULL; | |
659 | ||
75ebbd9a | 660 | if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL) |
0f113f3e | 661 | goto end; |
75ebbd9a | 662 | if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL) |
0f113f3e | 663 | goto end; |
75ebbd9a | 664 | if ((resp = TS_RESP_new()) == NULL) |
0f113f3e | 665 | goto end; |
75ebbd9a | 666 | if ((si = TS_STATUS_INFO_new()) == NULL) |
0f113f3e | 667 | goto end; |
ca4a494c | 668 | if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED)) |
0f113f3e MC |
669 | goto end; |
670 | if (!TS_RESP_set_status_info(resp, si)) | |
671 | goto end; | |
0f113f3e MC |
672 | TS_RESP_set_tst_info(resp, token, tst_info); |
673 | token = NULL; /* Ownership is lost. */ | |
674 | tst_info = NULL; /* Ownership is lost. */ | |
0f113f3e | 675 | ret = 1; |
18cd23df | 676 | |
c7235be6 | 677 | end: |
0f113f3e MC |
678 | PKCS7_free(token); |
679 | TS_TST_INFO_free(tst_info); | |
680 | if (!ret) { | |
681 | TS_RESP_free(resp); | |
682 | resp = NULL; | |
683 | } | |
684 | TS_STATUS_INFO_free(si); | |
685 | return resp; | |
686 | } | |
687 | ||
cc696296 F |
688 | static TS_RESP *create_response(CONF *conf, const char *section, const char *engine, |
689 | const char *queryfile, const char *passin, | |
690 | const char *inkey, const EVP_MD *md, const char *signer, | |
691 | const char *chain, const char *policy) | |
0f113f3e MC |
692 | { |
693 | int ret = 0; | |
694 | TS_RESP *response = NULL; | |
695 | BIO *query_bio = NULL; | |
696 | TS_RESP_CTX *resp_ctx = NULL; | |
697 | ||
75ebbd9a | 698 | if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL) |
0f113f3e | 699 | goto end; |
75ebbd9a | 700 | if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL) |
0f113f3e | 701 | goto end; |
75ebbd9a | 702 | if ((resp_ctx = TS_RESP_CTX_new()) == NULL) |
0f113f3e | 703 | goto end; |
0f113f3e MC |
704 | if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx)) |
705 | goto end; | |
1ae56f2f | 706 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
707 | if (!TS_CONF_set_crypto_device(conf, section, engine)) |
708 | goto end; | |
1ae56f2f | 709 | #endif |
0f113f3e MC |
710 | if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx)) |
711 | goto end; | |
0f113f3e MC |
712 | if (!TS_CONF_set_certs(conf, section, chain, resp_ctx)) |
713 | goto end; | |
0f113f3e MC |
714 | if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx)) |
715 | goto end; | |
e20b4727 DSH |
716 | |
717 | if (md) { | |
718 | if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md)) | |
719 | goto end; | |
720 | } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) { | |
721 | goto end; | |
722 | } | |
723 | ||
f0ef20bf MK |
724 | if (!TS_CONF_set_ess_cert_id_digest(conf, section, resp_ctx)) |
725 | goto end; | |
0f113f3e MC |
726 | if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx)) |
727 | goto end; | |
0f113f3e MC |
728 | if (!TS_CONF_set_policies(conf, section, resp_ctx)) |
729 | goto end; | |
0f113f3e MC |
730 | if (!TS_CONF_set_digests(conf, section, resp_ctx)) |
731 | goto end; | |
0f113f3e MC |
732 | if (!TS_CONF_set_accuracy(conf, section, resp_ctx)) |
733 | goto end; | |
0f113f3e MC |
734 | if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx)) |
735 | goto end; | |
0f113f3e MC |
736 | if (!TS_CONF_set_ordering(conf, section, resp_ctx)) |
737 | goto end; | |
0f113f3e MC |
738 | if (!TS_CONF_set_tsa_name(conf, section, resp_ctx)) |
739 | goto end; | |
0f113f3e MC |
740 | if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx)) |
741 | goto end; | |
75ebbd9a | 742 | if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL) |
0f113f3e | 743 | goto end; |
0f113f3e | 744 | ret = 1; |
18cd23df | 745 | |
c7235be6 | 746 | end: |
0f113f3e MC |
747 | if (!ret) { |
748 | TS_RESP_free(response); | |
749 | response = NULL; | |
750 | } | |
751 | TS_RESP_CTX_free(resp_ctx); | |
752 | BIO_free_all(query_bio); | |
0f113f3e MC |
753 | return response; |
754 | } | |
755 | ||
756 | static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data) | |
757 | { | |
758 | const char *serial_file = (const char *)data; | |
759 | ASN1_INTEGER *serial = next_serial(serial_file); | |
760 | ||
2234212c | 761 | if (serial == NULL) { |
0f113f3e MC |
762 | TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION, |
763 | "Error during serial number " | |
764 | "generation."); | |
765 | TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE); | |
2234212c | 766 | } else { |
0f113f3e | 767 | save_ts_serial(serial_file, serial); |
2234212c | 768 | } |
0f113f3e MC |
769 | |
770 | return serial; | |
771 | } | |
c7235be6 UM |
772 | |
773 | static ASN1_INTEGER *next_serial(const char *serialfile) | |
0f113f3e MC |
774 | { |
775 | int ret = 0; | |
776 | BIO *in = NULL; | |
777 | ASN1_INTEGER *serial = NULL; | |
778 | BIGNUM *bn = NULL; | |
779 | ||
75ebbd9a | 780 | if ((serial = ASN1_INTEGER_new()) == NULL) |
0f113f3e MC |
781 | goto err; |
782 | ||
75ebbd9a | 783 | if ((in = BIO_new_file(serialfile, "r")) == NULL) { |
0f113f3e MC |
784 | ERR_clear_error(); |
785 | BIO_printf(bio_err, "Warning: could not open file %s for " | |
786 | "reading, using serial number: 1\n", serialfile); | |
787 | if (!ASN1_INTEGER_set(serial, 1)) | |
788 | goto err; | |
789 | } else { | |
790 | char buf[1024]; | |
791 | if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) { | |
792 | BIO_printf(bio_err, "unable to load number from %s\n", | |
793 | serialfile); | |
794 | goto err; | |
795 | } | |
75ebbd9a | 796 | if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL) |
0f113f3e MC |
797 | goto err; |
798 | ASN1_INTEGER_free(serial); | |
799 | serial = NULL; | |
800 | if (!BN_add_word(bn, 1)) | |
801 | goto err; | |
75ebbd9a | 802 | if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) |
0f113f3e MC |
803 | goto err; |
804 | } | |
805 | ret = 1; | |
18cd23df | 806 | |
c7235be6 | 807 | err: |
0f113f3e MC |
808 | if (!ret) { |
809 | ASN1_INTEGER_free(serial); | |
810 | serial = NULL; | |
811 | } | |
812 | BIO_free_all(in); | |
813 | BN_free(bn); | |
814 | return serial; | |
815 | } | |
c7235be6 UM |
816 | |
817 | static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial) | |
0f113f3e MC |
818 | { |
819 | int ret = 0; | |
820 | BIO *out = NULL; | |
821 | ||
75ebbd9a | 822 | if ((out = BIO_new_file(serialfile, "w")) == NULL) |
0f113f3e MC |
823 | goto err; |
824 | if (i2a_ASN1_INTEGER(out, serial) <= 0) | |
825 | goto err; | |
826 | if (BIO_puts(out, "\n") <= 0) | |
827 | goto err; | |
828 | ret = 1; | |
c7235be6 | 829 | err: |
0f113f3e MC |
830 | if (!ret) |
831 | BIO_printf(bio_err, "could not save serial number to %s\n", | |
832 | serialfile); | |
833 | BIO_free_all(out); | |
834 | return ret; | |
835 | } | |
c7235be6 | 836 | |
18cd23df | 837 | |
c7235be6 UM |
838 | /* |
839 | * Verify-related method definitions. | |
840 | */ | |
841 | ||
cc696296 F |
842 | static int verify_command(const char *data, const char *digest, const char *queryfile, |
843 | const char *in, int token_in, | |
fd3397fc RL |
844 | const char *CApath, const char *CAfile, |
845 | const char *CAstore, const char *untrusted, | |
08538fc0 | 846 | X509_VERIFY_PARAM *vpm) |
0f113f3e MC |
847 | { |
848 | BIO *in_bio = NULL; | |
849 | PKCS7 *token = NULL; | |
850 | TS_RESP *response = NULL; | |
851 | TS_VERIFY_CTX *verify_ctx = NULL; | |
852 | int ret = 0; | |
853 | ||
75ebbd9a | 854 | if ((in_bio = BIO_new_file(in, "rb")) == NULL) |
0f113f3e MC |
855 | goto end; |
856 | if (token_in) { | |
75ebbd9a | 857 | if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL) |
0f113f3e MC |
858 | goto end; |
859 | } else { | |
75ebbd9a | 860 | if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL) |
0f113f3e MC |
861 | goto end; |
862 | } | |
863 | ||
75ebbd9a | 864 | if ((verify_ctx = create_verify_ctx(data, digest, queryfile, |
fd3397fc | 865 | CApath, CAfile, CAstore, untrusted, |
08538fc0 | 866 | vpm)) == NULL) |
0f113f3e MC |
867 | goto end; |
868 | ||
18cd23df RS |
869 | ret = token_in |
870 | ? TS_RESP_verify_token(verify_ctx, token) | |
871 | : TS_RESP_verify_response(verify_ctx, response); | |
c7235be6 UM |
872 | |
873 | end: | |
0f113f3e MC |
874 | printf("Verification: "); |
875 | if (ret) | |
876 | printf("OK\n"); | |
877 | else { | |
878 | printf("FAILED\n"); | |
0f113f3e MC |
879 | ERR_print_errors(bio_err); |
880 | } | |
881 | ||
0f113f3e MC |
882 | BIO_free_all(in_bio); |
883 | PKCS7_free(token); | |
884 | TS_RESP_free(response); | |
885 | TS_VERIFY_CTX_free(verify_ctx); | |
886 | return ret; | |
887 | } | |
888 | ||
cc696296 F |
889 | static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest, |
890 | const char *queryfile, | |
891 | const char *CApath, const char *CAfile, | |
fd3397fc | 892 | const char *CAstore, |
cc696296 | 893 | const char *untrusted, |
08538fc0 | 894 | X509_VERIFY_PARAM *vpm) |
0f113f3e MC |
895 | { |
896 | TS_VERIFY_CTX *ctx = NULL; | |
897 | BIO *input = NULL; | |
898 | TS_REQ *request = NULL; | |
899 | int ret = 0; | |
ca4a494c | 900 | int f = 0; |
0f113f3e MC |
901 | |
902 | if (data != NULL || digest != NULL) { | |
75ebbd9a | 903 | if ((ctx = TS_VERIFY_CTX_new()) == NULL) |
0f113f3e | 904 | goto err; |
ca4a494c | 905 | f = TS_VFY_VERSION | TS_VFY_SIGNER; |
0f113f3e | 906 | if (data != NULL) { |
e0670973 Y |
907 | BIO *out = NULL; |
908 | ||
ca4a494c | 909 | f |= TS_VFY_DATA; |
e0670973 Y |
910 | if ((out = BIO_new_file(data, "rb")) == NULL) |
911 | goto err; | |
912 | if (TS_VERIFY_CTX_set_data(ctx, out) == NULL) { | |
913 | BIO_free_all(out); | |
0f113f3e | 914 | goto err; |
e0670973 | 915 | } |
0f113f3e MC |
916 | } else if (digest != NULL) { |
917 | long imprint_len; | |
14f051a0 | 918 | unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len); |
ca4a494c RS |
919 | f |= TS_VFY_IMPRINT; |
920 | if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) { | |
0f113f3e MC |
921 | BIO_printf(bio_err, "invalid digest string\n"); |
922 | goto err; | |
923 | } | |
0f113f3e MC |
924 | } |
925 | ||
926 | } else if (queryfile != NULL) { | |
75ebbd9a | 927 | if ((input = BIO_new_file(queryfile, "rb")) == NULL) |
0f113f3e | 928 | goto err; |
75ebbd9a | 929 | if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL) |
0f113f3e | 930 | goto err; |
75ebbd9a | 931 | if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL) |
0f113f3e | 932 | goto err; |
2234212c | 933 | } else { |
0f113f3e | 934 | return NULL; |
2234212c | 935 | } |
0f113f3e MC |
936 | |
937 | /* Add the signature verification flag and arguments. */ | |
ca4a494c | 938 | TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE); |
0f113f3e MC |
939 | |
940 | /* Initialising the X509_STORE object. */ | |
fd3397fc RL |
941 | if (TS_VERIFY_CTX_set_store(ctx, |
942 | create_cert_store(CApath, CAfile, CAstore, vpm)) | |
ca4a494c | 943 | == NULL) |
0f113f3e MC |
944 | goto err; |
945 | ||
946 | /* Loading untrusted certificates. */ | |
ca4a494c | 947 | if (untrusted |
1af26e53 | 948 | && TS_VERIFY_CTX_set_certs(ctx, TS_CONF_load_certs(untrusted)) == NULL) |
0f113f3e | 949 | goto err; |
0f113f3e | 950 | ret = 1; |
18cd23df | 951 | |
c7235be6 | 952 | err: |
0f113f3e MC |
953 | if (!ret) { |
954 | TS_VERIFY_CTX_free(ctx); | |
955 | ctx = NULL; | |
956 | } | |
957 | BIO_free_all(input); | |
958 | TS_REQ_free(request); | |
959 | return ctx; | |
960 | } | |
c7235be6 | 961 | |
cc696296 | 962 | static X509_STORE *create_cert_store(const char *CApath, const char *CAfile, |
fd3397fc | 963 | const char *CAstore, X509_VERIFY_PARAM *vpm) |
0f113f3e MC |
964 | { |
965 | X509_STORE *cert_ctx = NULL; | |
966 | X509_LOOKUP *lookup = NULL; | |
b4250010 | 967 | OSSL_LIB_CTX *libctx = app_get0_libctx(); |
6725682d | 968 | const char *propq = app_get0_propq(); |
0f113f3e | 969 | |
0f113f3e | 970 | cert_ctx = X509_STORE_new(); |
0f113f3e | 971 | X509_STORE_set_verify_cb(cert_ctx, verify_cb); |
96487cdd | 972 | if (CApath != NULL) { |
0f113f3e MC |
973 | lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir()); |
974 | if (lookup == NULL) { | |
975 | BIO_printf(bio_err, "memory allocation failure\n"); | |
976 | goto err; | |
977 | } | |
fd3397fc | 978 | if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) { |
7e1b7485 | 979 | BIO_printf(bio_err, "Error loading directory %s\n", CApath); |
0f113f3e MC |
980 | goto err; |
981 | } | |
982 | } | |
983 | ||
96487cdd | 984 | if (CAfile != NULL) { |
0f113f3e MC |
985 | lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file()); |
986 | if (lookup == NULL) { | |
987 | BIO_printf(bio_err, "memory allocation failure\n"); | |
988 | goto err; | |
989 | } | |
d8652be0 MC |
990 | if (!X509_LOOKUP_load_file_ex(lookup, CAfile, X509_FILETYPE_PEM, libctx, |
991 | propq)) { | |
7e1b7485 | 992 | BIO_printf(bio_err, "Error loading file %s\n", CAfile); |
0f113f3e MC |
993 | goto err; |
994 | } | |
995 | } | |
08538fc0 | 996 | |
fd3397fc RL |
997 | if (CAstore != NULL) { |
998 | lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_store()); | |
999 | if (lookup == NULL) { | |
1000 | BIO_printf(bio_err, "memory allocation failure\n"); | |
1001 | goto err; | |
1002 | } | |
d8652be0 | 1003 | if (!X509_LOOKUP_load_store_ex(lookup, CAstore, libctx, propq)) { |
fd3397fc RL |
1004 | BIO_printf(bio_err, "Error loading store URI %s\n", CAstore); |
1005 | goto err; | |
1006 | } | |
1007 | } | |
1008 | ||
6b4a77f5 | 1009 | if (vpm != NULL) |
08538fc0 | 1010 | X509_STORE_set1_param(cert_ctx, vpm); |
1011 | ||
0f113f3e | 1012 | return cert_ctx; |
18cd23df | 1013 | |
c7235be6 | 1014 | err: |
0f113f3e MC |
1015 | X509_STORE_free(cert_ctx); |
1016 | return NULL; | |
1017 | } | |
c7235be6 | 1018 | |
6d23cf97 | 1019 | static int verify_cb(int ok, X509_STORE_CTX *ctx) |
0f113f3e | 1020 | { |
0f113f3e MC |
1021 | return ok; |
1022 | } |