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