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