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