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