blob: e65d223348d6d4605b979f0e5e3c0e54f43a8ac0 [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Matt Caswella28d06f2021-02-18 14:57:13 +00002 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
Ulf Möllerc7235be2006-02-12 23:11:56 +00003 *
Richard Levittedffa7522018-12-06 13:00:26 +01004 * Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salz846e33c2016-05-17 14:18:30 -04005 * 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
Ulf Möllerc7235be2006-02-12 23:11:56 +00008 */
9
Richard Levittef3852632016-03-18 20:06:29 +010010#include <openssl/opensslconf.h>
Rich Salz1ae56f22020-03-04 16:52:22 -050011#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>
Ulf Möllerc7235be2006-02-12 23:11:56 +000022
Rich Salz18cd23d2015-05-07 23:41:07 -040023/* Request nonce length, in bits (must be a multiple of 8). */
Rich Salz1ae56f22020-03-04 16:52:22 -050024#define NONCE_LENGTH 64
Ulf Möllerc7235be2006-02-12 23:11:56 +000025
Rich Salz18cd23d2015-05-07 23:41:07 -040026/* Name of config entry that defines the OID file. */
Rich Salz1ae56f22020-03-04 16:52:22 -050027#define ENV_OID_FILE "oid_file"
Ulf Möllerc7235be2006-02-12 23:11:56 +000028
Rich Salz18cd23d2015-05-07 23:41:07 -040029/* Is |EXACTLY_ONE| of three pointers set? */
Rich Salz1ae56f22020-03-04 16:52:22 -050030#define EXACTLY_ONE(a, b, c) \
Rich Salz18cd23d2015-05-07 23:41:07 -040031 (( a && !b && !c) || \
32 ( b && !a && !c) || \
33 ( c && !a && !b))
Ulf Möllerc7235be2006-02-12 23:11:56 +000034
35static ASN1_OBJECT *txt2obj(const char *oid);
36static CONF *load_config_file(const char *configfile);
37
38/* Query related functions. */
FdaSilvaYYcc696292016-08-04 23:52:22 +020039static int query_command(const char *data, const char *digest,
Matt Caswell0f113f32015-01-22 03:40:55 +000040 const EVP_MD *md, const char *policy, int no_nonce,
41 int cert, const char *in, const char *out, int text);
FdaSilvaYYcc696292016-08-04 23:52:22 +020042static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
Matt Caswell0f113f32015-01-22 03:40:55 +000043 const char *policy, int no_nonce, int cert);
FdaSilvaYYcc696292016-08-04 23:52:22 +020044static int create_digest(BIO *input, const char *digest,
Matt Caswell0f113f32015-01-22 03:40:55 +000045 const EVP_MD *md, unsigned char **md_value);
Ulf Möllerc7235be2006-02-12 23:11:56 +000046static ASN1_INTEGER *create_nonce(int bits);
47
48/* Reply related functions. */
FdaSilvaYYcc696292016-08-04 23:52:22 +020049static 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);
Ulf Möllerc7235be2006-02-12 23:11:56 +000054static TS_RESP *read_PKCS7(BIO *in_bio);
FdaSilvaYYcc696292016-08-04 23:52:22 +020055static 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);
Matt Caswell0f113f32015-01-22 03:40:55 +000059static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data);
Ulf Möllerc7235be2006-02-12 23:11:56 +000060static ASN1_INTEGER *next_serial(const char *serialfile);
61static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
62
63/* Verify related functions. */
FdaSilvaYYcc696292016-08-04 23:52:22 +020064static int verify_command(const char *data, const char *digest, const char *queryfile,
65 const char *in, int token_in,
Richard Levittefd3397f2019-03-07 15:26:34 +010066 const char *CApath, const char *CAfile,
67 const char *CAstore,
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +010068 char *untrusted, X509_VERIFY_PARAM *vpm);
FdaSilvaYYcc696292016-08-04 23:52:22 +020069static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
70 const char *queryfile,
71 const char *CApath, const char *CAfile,
Richard Levittefd3397f2019-03-07 15:26:34 +010072 const char *CAstore,
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +010073 char *untrusted,
fbroda08538fc2016-03-15 10:08:49 +010074 X509_VERIFY_PARAM *vpm);
FdaSilvaYYcc696292016-08-04 23:52:22 +020075static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
Richard Levittefd3397f2019-03-07 15:26:34 +010076 const char *CAstore, X509_VERIFY_PARAM *vpm);
Rich Salz6d23cf92015-01-12 17:29:26 -050077static int verify_cb(int ok, X509_STORE_CTX *ctx);
Ulf Möllerc7235be2006-02-12 23:11:56 +000078
Rich Salz7e1b7482015-04-24 15:26:15 -040079typedef enum OPTION_choice {
Dr. David von Oheimbb0f96012021-05-01 15:29:00 +020080 OPT_COMMON,
Rich Salz7e1b7482015-04-24 15:26:15 -040081 OPT_ENGINE, OPT_CONFIG, OPT_SECTION, OPT_QUERY, OPT_DATA,
Rich Salz3ee1eac2017-07-05 10:58:48 -040082 OPT_DIGEST, OPT_TSPOLICY, OPT_NO_NONCE, OPT_CERT,
Rich Salz7e1b7482015-04-24 15:26:15 -040083 OPT_IN, OPT_TOKEN_IN, OPT_OUT, OPT_TOKEN_OUT, OPT_TEXT,
84 OPT_REPLY, OPT_QUERYFILE, OPT_PASSIN, OPT_INKEY, OPT_SIGNER,
Richard Levittefd3397f2019-03-07 15:26:34 +010085 OPT_CHAIN, OPT_VERIFY, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE, OPT_UNTRUSTED,
Pauli6bd4e3f2020-02-25 14:29:30 +100086 OPT_MD, OPT_V_ENUM, OPT_R_ENUM, OPT_PROV_ENUM
Rich Salz7e1b7482015-04-24 15:26:15 -040087} OPTION_CHOICE;
Ulf Möllerc7235be2006-02-12 23:11:56 +000088
FdaSilvaYY44c83eb2016-03-13 14:07:50 +010089const OPTIONS ts_options[] = {
Rich Salz5388f982019-11-08 06:08:30 +100090 OPT_SECTION("General"),
Rich Salz7e1b7482015-04-24 15:26:15 -040091 {"help", OPT_HELP, '-', "Display this summary"},
92 {"config", OPT_CONFIG, '<', "Configuration file"},
93 {"section", OPT_SECTION, 's', "Section to use within config file"},
Rich Salz1ae56f22020-03-04 16:52:22 -050094#ifndef OPENSSL_NO_ENGINE
Rich Salz5388f982019-11-08 06:08:30 +100095 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
Rich Salz1ae56f22020-03-04 16:52:22 -050096#endif
Rich Salz48b53522017-05-20 21:44:31 -040097 {"inkey", OPT_INKEY, 's', "File with private key for reply"},
FdaSilvaYY12d56b22016-07-31 19:02:50 +020098 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
Rich Salz7e1b7482015-04-24 15:26:15 -040099 {"chain", OPT_CHAIN, '<', "File with signer CA chain"},
Rich Salz7e1b7482015-04-24 15:26:15 -0400100 {"CAfile", OPT_CAFILE, '<', "File with trusted CA certs"},
Dr. David von Oheimb2b264ae2020-03-06 21:46:33 +0100101 {"CApath", OPT_CAPATH, '/', "Path to trusted CA files"},
Richard Levittefd3397f2019-03-07 15:26:34 +0100102 {"CAstore", OPT_CASTORE, ':', "URI to trusted CA store"},
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +0100103 {"untrusted", OPT_UNTRUSTED, '<', "Extra untrusted certs"},
Rich Salz5388f982019-11-08 06:08:30 +1000104 {"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"},
Rich Salz9c3bcfa2015-05-15 13:50:38 -0400107 {"", OPT_MD, '-', "Any supported digest"},
Rich Salz5388f982019-11-08 06:08:30 +1000108
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,
fbroda08538fc2016-03-15 10:08:49 +0100126 OPT_V_OPTIONS,
Pauli6bd4e3f2020-02-25 14:29:30 +1000127 OPT_PROV_OPTIONS,
Rich Salz7e1b7482015-04-24 15:26:15 -0400128 {NULL}
129};
130
131/*
klemens60250012016-08-05 19:56:58 +0200132 * This command is so complex, special help is needed.
Rich Salz7e1b7482015-04-24 15:26:15 -0400133 */
134static char* opt_helplist[] = {
Rich Salz5388f982019-11-08 06:08:30 +1000135 "",
Rich Salz7e1b7482015-04-24 15:26:15 -0400136 "Typical uses:",
Rich Salz5388f982019-11-08 06:08:30 +1000137 " 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]",
Rich Salz1ae56f22020-03-04 16:52:22 -0500146#ifndef OPENSSL_NO_ENGINE
Rich Salz5388f982019-11-08 06:08:30 +1000147 " [-text] [-engine id]",
Rich Salz1ae56f22020-03-04 16:52:22 -0500148#else
Rich Salz5388f982019-11-08 06:08:30 +1000149 " [-text]",
Rich Salz1ae56f22020-03-04 16:52:22 -0500150#endif
Rich Salz5388f982019-11-08 06:08:30 +1000151 "",
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +0100152 " 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] ...",
Rich Salz7e1b7482015-04-24 15:26:15 -0400155 NULL,
156};
157
158int ts_main(int argc, char **argv)
Matt Caswell0f113f32015-01-22 03:40:55 +0000159{
Matt Caswell0f113f32015-01-22 03:40:55 +0000160 CONF *conf = NULL;
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +0100161 const char *CAfile = NULL, *prog;
162 char *untrusted = NULL;
FdaSilvaYYcc696292016-08-04 23:52:22 +0200163 const char *configfile = default_config_file, *engine = NULL;
Rich Salzd0190e12021-02-08 14:03:35 -0500164 const char *section = NULL, *digestname = NULL;
FdaSilvaYYcc696292016-08-04 23:52:22 +0200165 char **helpp;
166 char *password = NULL;
Rich Salz3ee1eac2017-07-05 10:58:48 -0400167 char *data = NULL, *digest = NULL, *policy = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400168 char *in = NULL, *out = NULL, *queryfile = NULL, *passin = NULL;
169 char *inkey = NULL, *signer = NULL, *chain = NULL, *CApath = NULL;
Richard Levittefd3397f2019-03-07 15:26:34 +0100170 char *CAstore = NULL;
Rich Salz606a4172021-02-17 16:15:27 -0500171 EVP_MD *md = NULL;
Rich Salz7e1b7482015-04-24 15:26:15 -0400172 OPTION_CHOICE o, mode = OPT_ERR;
173 int ret = 1, no_nonce = 0, cert = 0, text = 0;
fbroda08538fc2016-03-15 10:08:49 +0100174 int vpmtouched = 0;
175 X509_VERIFY_PARAM *vpm = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000176 /* Input is ContentInfo instead of TimeStampResp. */
177 int token_in = 0;
178 /* Output is ContentInfo instead of TimeStampResp. */
179 int token_out = 0;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000180
fbroda08538fc2016-03-15 10:08:49 +0100181 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
182 goto end;
183
Rich Salz7e1b7482015-04-24 15:26:15 -0400184 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;
Rich Salz3ee1eac2017-07-05 10:58:48 -0400217 case OPT_R_CASES:
218 if (!opt_rand(o))
219 goto end;
Rich Salz7e1b7482015-04-24 15:26:15 -0400220 break;
Pauli6bd4e3f2020-02-25 14:29:30 +1000221 case OPT_PROV_CASES:
222 if (!opt_provider(o))
223 goto end;
224 break;
fbroda08538fc2016-03-15 10:08:49 +0100225 case OPT_TSPOLICY:
Rich Salz7e1b7482015-04-24 15:26:15 -0400226 policy = opt_arg();
227 break;
228 case OPT_NO_NONCE:
Matt Caswell0f113f32015-01-22 03:40:55 +0000229 no_nonce = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400230 break;
231 case OPT_CERT:
Matt Caswell0f113f32015-01-22 03:40:55 +0000232 cert = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400233 break;
234 case OPT_IN:
235 in = opt_arg();
236 break;
237 case OPT_TOKEN_IN:
Matt Caswell0f113f32015-01-22 03:40:55 +0000238 token_in = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400239 break;
240 case OPT_OUT:
241 out = opt_arg();
242 break;
243 case OPT_TOKEN_OUT:
Matt Caswell0f113f32015-01-22 03:40:55 +0000244 token_out = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400245 break;
246 case OPT_TEXT:
Matt Caswell0f113f32015-01-22 03:40:55 +0000247 text = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400248 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;
Richard Levittefd3397f2019-03-07 15:26:34 +0100270 case OPT_CASTORE:
271 CAstore = opt_arg();
272 break;
Rich Salz7e1b7482015-04-24 15:26:15 -0400273 case OPT_UNTRUSTED:
274 untrusted = opt_arg();
275 break;
276 case OPT_ENGINE:
277 engine = opt_arg();
278 break;
279 case OPT_MD:
Rich Salzd0190e12021-02-08 14:03:35 -0500280 digestname = opt_unknown();
Rich Salz7e1b7482015-04-24 15:26:15 -0400281 break;
fbroda08538fc2016-03-15 10:08:49 +0100282 case OPT_V_CASES:
283 if (!opt_verify(o, vpm))
284 goto end;
285 vpmtouched++;
286 break;
Rich Salz7e1b7482015-04-24 15:26:15 -0400287 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000288 }
Rich Salz021410e2020-11-28 16:12:58 -0500289
290 /* No extra arguments. */
291 argc = opt_num_rest();
292 if (argc != 0 || mode == OPT_ERR)
Rich Salz7e1b7482015-04-24 15:26:15 -0400293 goto opthelp;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000294
Dr. David von Oheimb3ad60302021-04-03 12:53:51 +0200295 if (!app_RAND_load())
296 goto end;
297
Rich Salzd0190e12021-02-08 14:03:35 -0500298 if (digestname != NULL) {
299 if (!opt_md(digestname, &md))
300 goto opthelp;
301 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400302 if (mode == OPT_REPLY && passin &&
303 !app_passwd(passin, NULL, &password, NULL)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000304 BIO_printf(bio_err, "Error getting password.\n");
Rich Salz7e1b7482015-04-24 15:26:15 -0400305 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000306 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000307
kinichirodd0139f2019-12-11 21:12:53 +0900308 if ((conf = load_config_file(configfile)) == NULL)
309 goto end;
Dr. Stephen Hensonc821def2016-05-15 18:43:03 +0100310 if (configfile != default_config_file && !app_load_modules(conf))
Richard Levitte296f54e2015-05-29 08:07:10 +0200311 goto end;
312
Rich Salz18cd23d2015-05-07 23:41:07 -0400313 /* Check parameter consistency and execute the appropriate function. */
Rich Salzf3b3d7f2016-08-30 13:31:18 -0400314 if (mode == OPT_QUERY) {
fbroda08538fc2016-03-15 10:08:49 +0100315 if (vpmtouched)
316 goto opthelp;
Rich Salz18cd23d2015-05-07 23:41:07 -0400317 if ((data != NULL) && (digest != NULL))
Rich Salz7e1b7482015-04-24 15:26:15 -0400318 goto opthelp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000319 ret = !query_command(data, digest, md, policy, no_nonce, cert,
320 in, out, text);
Rich Salzf3b3d7f2016-08-30 13:31:18 -0400321 } else if (mode == OPT_REPLY) {
fbroda08538fc2016-03-15 10:08:49 +0100322 if (vpmtouched)
323 goto opthelp;
Rich Salz18cd23d2015-05-07 23:41:07 -0400324 if ((in != NULL) && (queryfile != NULL))
325 goto opthelp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000326 if (in == NULL) {
Rich Salz18cd23d2015-05-07 23:41:07 -0400327 if ((conf == NULL) || (token_in != 0))
Rich Salz7e1b7482015-04-24 15:26:15 -0400328 goto opthelp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000329 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000330 ret = !reply_command(conf, section, engine, queryfile,
Dr. Stephen Hensone20b4722015-09-11 16:58:57 +0100331 password, inkey, md, signer, chain, policy,
Matt Caswell0f113f32015-01-22 03:40:55 +0000332 in, token_in, out, token_out, text);
Rich Salzf3b3d7f2016-08-30 13:31:18 -0400333
334 } else if (mode == OPT_VERIFY) {
Rich Salz18cd23d2015-05-07 23:41:07 -0400335 if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest))
Rich Salz7e1b7482015-04-24 15:26:15 -0400336 goto opthelp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000337 ret = !verify_command(data, digest, queryfile, in, token_in,
Richard Levittefd3397f2019-03-07 15:26:34 +0100338 CApath, CAfile, CAstore, untrusted,
fbroda08538fc2016-03-15 10:08:49 +0100339 vpmtouched ? vpm : NULL);
Rich Salzf3b3d7f2016-08-30 13:31:18 -0400340 } else {
341 goto opthelp;
Matt Caswell0f113f32015-01-22 03:40:55 +0000342 }
343
Rich Salz7e1b7482015-04-24 15:26:15 -0400344 end:
fbroda08538fc2016-03-15 10:08:49 +0100345 X509_VERIFY_PARAM_free(vpm);
Rich Salz606a4172021-02-17 16:15:27 -0500346 EVP_MD_free(md);
Matt Caswell0f113f32015-01-22 03:40:55 +0000347 NCONF_free(conf);
348 OPENSSL_free(password);
KaoruToda26a7d932017-10-17 23:04:09 +0900349 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000350}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000351
352/*
353 * Configuration file-related function definitions.
354 */
355
356static ASN1_OBJECT *txt2obj(const char *oid)
Matt Caswell0f113f32015-01-22 03:40:55 +0000357{
358 ASN1_OBJECT *oid_obj = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000359
Rich Salz75ebbd92015-05-06 13:43:59 -0400360 if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000361 BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
Ulf Möllerc7235be2006-02-12 23:11:56 +0000362
Matt Caswell0f113f32015-01-22 03:40:55 +0000363 return oid_obj;
364}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000365
366static CONF *load_config_file(const char *configfile)
Matt Caswell0f113f32015-01-22 03:40:55 +0000367{
Rich Salzcc01d212015-05-28 13:52:55 -0400368 CONF *conf = app_load_config(configfile);
Ulf Möllerc7235be2006-02-12 23:11:56 +0000369
Matt Caswell0f113f32015-01-22 03:40:55 +0000370 if (conf != NULL) {
371 const char *p;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000372
Matt Caswell0f113f32015-01-22 03:40:55 +0000373 BIO_printf(bio_err, "Using configuration from %s\n", configfile);
374 p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
375 if (p != NULL) {
376 BIO *oid_bio = BIO_new_file(p, "r");
377 if (!oid_bio)
378 ERR_print_errors(bio_err);
379 else {
380 OBJ_create_objects(oid_bio);
381 BIO_free_all(oid_bio);
382 }
383 } else
384 ERR_clear_error();
Rich Salz7e1b7482015-04-24 15:26:15 -0400385 if (!add_oid_section(conf))
Matt Caswell0f113f32015-01-22 03:40:55 +0000386 ERR_print_errors(bio_err);
387 }
388 return conf;
389}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000390
391/*
392 * Query-related method definitions.
393 */
FdaSilvaYYcc696292016-08-04 23:52:22 +0200394static int query_command(const char *data, const char *digest, const EVP_MD *md,
Matt Caswell0f113f32015-01-22 03:40:55 +0000395 const char *policy, int no_nonce,
396 int cert, const char *in, const char *out, int text)
397{
398 int ret = 0;
399 TS_REQ *query = NULL;
400 BIO *in_bio = NULL;
401 BIO *data_bio = NULL;
402 BIO *out_bio = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000403
Rich Salz18cd23d2015-05-07 23:41:07 -0400404 /* Build query object. */
Matt Caswell0f113f32015-01-22 03:40:55 +0000405 if (in != NULL) {
Richard Levittebdd58d92015-09-04 12:49:06 +0200406 if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000407 goto end;
408 query = d2i_TS_REQ_bio(in_bio, NULL);
409 } else {
Rich Salz75ebbd92015-05-06 13:43:59 -0400410 if (digest == NULL
Richard Levittebdd58d92015-09-04 12:49:06 +0200411 && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000412 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000413 query = create_query(data_bio, digest, md, policy, no_nonce, cert);
Matt Caswell0f113f32015-01-22 03:40:55 +0000414 }
415 if (query == NULL)
416 goto end;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000417
Matt Caswell0f113f32015-01-22 03:40:55 +0000418 if (text) {
Richard Levittebdd58d92015-09-04 12:49:06 +0200419 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
420 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000421 if (!TS_REQ_print_bio(out_bio, query))
422 goto end;
423 } else {
Richard Levittebdd58d92015-09-04 12:49:06 +0200424 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
425 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000426 if (!i2d_TS_REQ_bio(out_bio, query))
427 goto end;
428 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000429
Matt Caswell0f113f32015-01-22 03:40:55 +0000430 ret = 1;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000431
432 end:
Matt Caswell0f113f32015-01-22 03:40:55 +0000433 ERR_print_errors(bio_err);
Matt Caswell0f113f32015-01-22 03:40:55 +0000434 BIO_free_all(in_bio);
435 BIO_free_all(data_bio);
436 BIO_free_all(out_bio);
437 TS_REQ_free(query);
Matt Caswell0f113f32015-01-22 03:40:55 +0000438 return ret;
439}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000440
FdaSilvaYYcc696292016-08-04 23:52:22 +0200441static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
Matt Caswell0f113f32015-01-22 03:40:55 +0000442 const char *policy, int no_nonce, int cert)
443{
444 int ret = 0;
445 TS_REQ *ts_req = NULL;
446 int len;
447 TS_MSG_IMPRINT *msg_imprint = NULL;
448 X509_ALGOR *algo = NULL;
449 unsigned char *data = NULL;
450 ASN1_OBJECT *policy_obj = NULL;
451 ASN1_INTEGER *nonce_asn1 = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000452
Tomas Mraza6dfa182018-12-14 12:10:58 +0100453 if (md == NULL && (md = EVP_get_digestbyname("sha256")) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000454 goto err;
Rich Salz75ebbd92015-05-06 13:43:59 -0400455 if ((ts_req = TS_REQ_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000456 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000457 if (!TS_REQ_set_version(ts_req, 1))
458 goto err;
Rich Salz75ebbd92015-05-06 13:43:59 -0400459 if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000460 goto err;
Rich Salz75ebbd92015-05-06 13:43:59 -0400461 if ((algo = X509_ALGOR_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000462 goto err;
Tomas Mrazed576ac2021-05-21 16:58:08 +0200463 if ((algo->algorithm = OBJ_nid2obj(EVP_MD_get_type(md))) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000464 goto err;
Rich Salz75ebbd92015-05-06 13:43:59 -0400465 if ((algo->parameter = ASN1_TYPE_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000466 goto err;
467 algo->parameter->type = V_ASN1_NULL;
468 if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
469 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000470 if ((len = create_digest(data_bio, digest, md, &data)) == 0)
471 goto err;
472 if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
473 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000474 if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
475 goto err;
Rich Salz75ebbd92015-05-06 13:43:59 -0400476 if (policy && (policy_obj = txt2obj(policy)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000477 goto err;
478 if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
479 goto err;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000480
Matt Caswell0f113f32015-01-22 03:40:55 +0000481 /* Setting nonce if requested. */
Rich Salz75ebbd92015-05-06 13:43:59 -0400482 if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000483 goto err;
484 if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
485 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +0000486 if (!TS_REQ_set_cert_req(ts_req, cert))
487 goto err;
488
489 ret = 1;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000490 err:
Matt Caswell0f113f32015-01-22 03:40:55 +0000491 if (!ret) {
492 TS_REQ_free(ts_req);
493 ts_req = NULL;
494 BIO_printf(bio_err, "could not create query\n");
Rich Salz18cd23d2015-05-07 23:41:07 -0400495 ERR_print_errors(bio_err);
Matt Caswell0f113f32015-01-22 03:40:55 +0000496 }
497 TS_MSG_IMPRINT_free(msg_imprint);
498 X509_ALGOR_free(algo);
499 OPENSSL_free(data);
500 ASN1_OBJECT_free(policy_obj);
501 ASN1_INTEGER_free(nonce_asn1);
502 return ts_req;
503}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000504
FdaSilvaYYcc696292016-08-04 23:52:22 +0200505static int create_digest(BIO *input, const char *digest, const EVP_MD *md,
Matt Caswell0f113f32015-01-22 03:40:55 +0000506 unsigned char **md_value)
507{
508 int md_value_len;
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100509 int rv = 0;
510 EVP_MD_CTX *md_ctx = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000511
Tomas Mrazed576ac2021-05-21 16:58:08 +0200512 md_value_len = EVP_MD_get_size(md);
Matt Caswell0f113f32015-01-22 03:40:55 +0000513 if (md_value_len < 0)
Rich Salz18cd23d2015-05-07 23:41:07 -0400514 return 0;
515
Paul Yang22342122017-06-13 01:24:02 +0800516 if (input != NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000517 unsigned char buffer[4096];
518 int length;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000519
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100520 md_ctx = EVP_MD_CTX_new();
Richard Levitte6e59a892015-11-27 14:02:12 +0100521 if (md_ctx == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000522 return 0;
Richard Levitte6e59a892015-11-27 14:02:12 +0100523 *md_value = app_malloc(md_value_len, "digest buffer");
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100524 if (!EVP_DigestInit(md_ctx, md))
525 goto err;
Richard Levitte6e59a892015-11-27 14:02:12 +0100526 while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100527 if (!EVP_DigestUpdate(md_ctx, buffer, length))
528 goto err;
Richard Levitte6e59a892015-11-27 14:02:12 +0100529 }
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100530 if (!EVP_DigestFinal(md_ctx, *md_value, NULL))
531 goto err;
Tomas Mrazed576ac2021-05-21 16:58:08 +0200532 md_value_len = EVP_MD_get_size(md);
Matt Caswell0f113f32015-01-22 03:40:55 +0000533 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +0000534 long digest_len;
Rich Salz12a765a2019-09-16 15:28:57 -0400535
Rich Salz14f051a2016-04-13 15:58:28 -0400536 *md_value = OPENSSL_hexstr2buf(digest, &digest_len);
Rich Salz12a765a2019-09-16 15:28:57 -0400537 if (*md_value == NULL || md_value_len != digest_len) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000538 OPENSSL_free(*md_value);
539 *md_value = NULL;
540 BIO_printf(bio_err, "bad digest, %d bytes "
541 "must be specified\n", md_value_len);
Rich Salz18cd23d2015-05-07 23:41:07 -0400542 return 0;
Matt Caswell0f113f32015-01-22 03:40:55 +0000543 }
544 }
Dr. Stephen Hensond166ed82016-06-18 15:46:13 +0100545 rv = md_value_len;
546 err:
547 EVP_MD_CTX_free(md_ctx);
548 return rv;
Matt Caswell0f113f32015-01-22 03:40:55 +0000549}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000550
551static ASN1_INTEGER *create_nonce(int bits)
Matt Caswell0f113f32015-01-22 03:40:55 +0000552{
553 unsigned char buf[20];
554 ASN1_INTEGER *nonce = NULL;
555 int len = (bits - 1) / 8 + 1;
556 int i;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000557
Matt Caswell0f113f32015-01-22 03:40:55 +0000558 if (len > (int)sizeof(buf))
559 goto err;
560 if (RAND_bytes(buf, len) <= 0)
561 goto err;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000562
Matt Caswell0f113f32015-01-22 03:40:55 +0000563 /* Find the first non-zero byte and creating ASN1_INTEGER object. */
Rich Salz75ebbd92015-05-06 13:43:59 -0400564 for (i = 0; i < len && !buf[i]; ++i)
565 continue;
566 if ((nonce = ASN1_INTEGER_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000567 goto err;
568 OPENSSL_free(nonce->data);
Matt Caswell0f113f32015-01-22 03:40:55 +0000569 nonce->length = len - i;
Rich Salz68dc6822015-04-30 17:48:31 -0400570 nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
Matt Caswell0f113f32015-01-22 03:40:55 +0000571 memcpy(nonce->data, buf + i, nonce->length);
Matt Caswell0f113f32015-01-22 03:40:55 +0000572 return nonce;
Rich Salz18cd23d2015-05-07 23:41:07 -0400573
Ulf Möllerc7235be2006-02-12 23:11:56 +0000574 err:
Matt Caswell0f113f32015-01-22 03:40:55 +0000575 BIO_printf(bio_err, "could not create nonce\n");
576 ASN1_INTEGER_free(nonce);
577 return NULL;
578}
579
Ulf Möllerc7235be2006-02-12 23:11:56 +0000580/*
581 * Reply-related method definitions.
582 */
583
FdaSilvaYYcc696292016-08-04 23:52:22 +0200584static int reply_command(CONF *conf, const char *section, const char *engine,
585 const char *queryfile, const char *passin, const char *inkey,
586 const EVP_MD *md, const char *signer, const char *chain,
587 const char *policy, const char *in, int token_in,
588 const char *out, int token_out, int text)
Matt Caswell0f113f32015-01-22 03:40:55 +0000589{
590 int ret = 0;
591 TS_RESP *response = NULL;
592 BIO *in_bio = NULL;
593 BIO *query_bio = NULL;
594 BIO *inkey_bio = NULL;
595 BIO *signer_bio = NULL;
596 BIO *out_bio = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000597
Matt Caswell0f113f32015-01-22 03:40:55 +0000598 if (in != NULL) {
599 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
600 goto end;
601 if (token_in) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000602 response = read_PKCS7(in_bio);
603 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +0000604 response = d2i_TS_RESP_bio(in_bio, NULL);
605 }
606 } else {
607 response = create_response(conf, section, engine, queryfile,
Dr. Stephen Hensone20b4722015-09-11 16:58:57 +0100608 passin, inkey, md, signer, chain, policy);
Paul Yang22342122017-06-13 01:24:02 +0800609 if (response != NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000610 BIO_printf(bio_err, "Response has been generated.\n");
611 else
612 BIO_printf(bio_err, "Response is not generated.\n");
613 }
614 if (response == NULL)
615 goto end;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000616
Rich Salz18cd23d2015-05-07 23:41:07 -0400617 /* Write response. */
Matt Caswell0f113f32015-01-22 03:40:55 +0000618 if (text) {
Richard Levittebdd58d92015-09-04 12:49:06 +0200619 if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
620 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000621 if (token_out) {
622 TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
623 if (!TS_TST_INFO_print_bio(out_bio, tst_info))
624 goto end;
625 } else {
626 if (!TS_RESP_print_bio(out_bio, response))
627 goto end;
628 }
629 } else {
Richard Levittebdd58d92015-09-04 12:49:06 +0200630 if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
631 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000632 if (token_out) {
633 PKCS7 *token = TS_RESP_get_token(response);
634 if (!i2d_PKCS7_bio(out_bio, token))
635 goto end;
636 } else {
637 if (!i2d_TS_RESP_bio(out_bio, response))
638 goto end;
639 }
640 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000641
Matt Caswell0f113f32015-01-22 03:40:55 +0000642 ret = 1;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000643
644 end:
Matt Caswell0f113f32015-01-22 03:40:55 +0000645 ERR_print_errors(bio_err);
Matt Caswell0f113f32015-01-22 03:40:55 +0000646 BIO_free_all(in_bio);
647 BIO_free_all(query_bio);
648 BIO_free_all(inkey_bio);
649 BIO_free_all(signer_bio);
650 BIO_free_all(out_bio);
651 TS_RESP_free(response);
Matt Caswell0f113f32015-01-22 03:40:55 +0000652 return ret;
653}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000654
655/* Reads a PKCS7 token and adds default 'granted' status info to it. */
656static TS_RESP *read_PKCS7(BIO *in_bio)
Matt Caswell0f113f32015-01-22 03:40:55 +0000657{
658 int ret = 0;
659 PKCS7 *token = NULL;
660 TS_TST_INFO *tst_info = NULL;
661 TS_RESP *resp = NULL;
662 TS_STATUS_INFO *si = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000663
Rich Salz75ebbd92015-05-06 13:43:59 -0400664 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000665 goto end;
Rich Salz75ebbd92015-05-06 13:43:59 -0400666 if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000667 goto end;
Rich Salz75ebbd92015-05-06 13:43:59 -0400668 if ((resp = TS_RESP_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000669 goto end;
Rich Salz75ebbd92015-05-06 13:43:59 -0400670 if ((si = TS_STATUS_INFO_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000671 goto end;
Rich Salzca4a4942015-06-10 14:07:40 -0400672 if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
Matt Caswell0f113f32015-01-22 03:40:55 +0000673 goto end;
674 if (!TS_RESP_set_status_info(resp, si))
675 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000676 TS_RESP_set_tst_info(resp, token, tst_info);
677 token = NULL; /* Ownership is lost. */
678 tst_info = NULL; /* Ownership is lost. */
Matt Caswell0f113f32015-01-22 03:40:55 +0000679 ret = 1;
Rich Salz18cd23d2015-05-07 23:41:07 -0400680
Ulf Möllerc7235be2006-02-12 23:11:56 +0000681 end:
Matt Caswell0f113f32015-01-22 03:40:55 +0000682 PKCS7_free(token);
683 TS_TST_INFO_free(tst_info);
684 if (!ret) {
685 TS_RESP_free(resp);
686 resp = NULL;
687 }
688 TS_STATUS_INFO_free(si);
689 return resp;
690}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000691
FdaSilvaYYcc696292016-08-04 23:52:22 +0200692static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
693 const char *queryfile, const char *passin,
694 const char *inkey, const EVP_MD *md, const char *signer,
695 const char *chain, const char *policy)
Matt Caswell0f113f32015-01-22 03:40:55 +0000696{
697 int ret = 0;
698 TS_RESP *response = NULL;
699 BIO *query_bio = NULL;
700 TS_RESP_CTX *resp_ctx = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000701
Rich Salz75ebbd92015-05-06 13:43:59 -0400702 if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000703 goto end;
Rich Salz75ebbd92015-05-06 13:43:59 -0400704 if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000705 goto end;
Rich Salz75ebbd92015-05-06 13:43:59 -0400706 if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000707 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000708 if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
709 goto end;
Rich Salz1ae56f22020-03-04 16:52:22 -0500710#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +0000711 if (!TS_CONF_set_crypto_device(conf, section, engine))
712 goto end;
Rich Salz1ae56f22020-03-04 16:52:22 -0500713#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000714 if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
715 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000716 if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
717 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000718 if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
719 goto end;
Dr. Stephen Hensone20b4722015-09-11 16:58:57 +0100720
721 if (md) {
722 if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
723 goto end;
724 } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) {
725 goto end;
726 }
727
Marek Kleinf0ef20b2016-03-01 16:32:10 +0000728 if (!TS_CONF_set_ess_cert_id_digest(conf, section, resp_ctx))
729 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000730 if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
731 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000732 if (!TS_CONF_set_policies(conf, section, resp_ctx))
733 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000734 if (!TS_CONF_set_digests(conf, section, resp_ctx))
735 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000736 if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
737 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000738 if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
739 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000740 if (!TS_CONF_set_ordering(conf, section, resp_ctx))
741 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000742 if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
743 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000744 if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
745 goto end;
Rich Salz75ebbd92015-05-06 13:43:59 -0400746 if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000747 goto end;
Matt Caswell0f113f32015-01-22 03:40:55 +0000748 ret = 1;
Rich Salz18cd23d2015-05-07 23:41:07 -0400749
Ulf Möllerc7235be2006-02-12 23:11:56 +0000750 end:
Matt Caswell0f113f32015-01-22 03:40:55 +0000751 if (!ret) {
752 TS_RESP_free(response);
753 response = NULL;
754 }
755 TS_RESP_CTX_free(resp_ctx);
756 BIO_free_all(query_bio);
Matt Caswell0f113f32015-01-22 03:40:55 +0000757 return response;
758}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000759
Matt Caswell0f113f32015-01-22 03:40:55 +0000760static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
761{
762 const char *serial_file = (const char *)data;
763 ASN1_INTEGER *serial = next_serial(serial_file);
Ulf Möllerc7235be2006-02-12 23:11:56 +0000764
Paul Yang22342122017-06-13 01:24:02 +0800765 if (serial == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000766 TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
767 "Error during serial number "
768 "generation.");
769 TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
Paul Yang22342122017-06-13 01:24:02 +0800770 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +0000771 save_ts_serial(serial_file, serial);
Paul Yang22342122017-06-13 01:24:02 +0800772 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000773
Matt Caswell0f113f32015-01-22 03:40:55 +0000774 return serial;
775}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000776
777static ASN1_INTEGER *next_serial(const char *serialfile)
Matt Caswell0f113f32015-01-22 03:40:55 +0000778{
779 int ret = 0;
780 BIO *in = NULL;
781 ASN1_INTEGER *serial = NULL;
782 BIGNUM *bn = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000783
Rich Salz75ebbd92015-05-06 13:43:59 -0400784 if ((serial = ASN1_INTEGER_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000785 goto err;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000786
Rich Salz75ebbd92015-05-06 13:43:59 -0400787 if ((in = BIO_new_file(serialfile, "r")) == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000788 ERR_clear_error();
789 BIO_printf(bio_err, "Warning: could not open file %s for "
790 "reading, using serial number: 1\n", serialfile);
791 if (!ASN1_INTEGER_set(serial, 1))
792 goto err;
793 } else {
794 char buf[1024];
795 if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
796 BIO_printf(bio_err, "unable to load number from %s\n",
797 serialfile);
798 goto err;
799 }
Rich Salz75ebbd92015-05-06 13:43:59 -0400800 if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000801 goto err;
802 ASN1_INTEGER_free(serial);
803 serial = NULL;
804 if (!BN_add_word(bn, 1))
805 goto err;
Rich Salz75ebbd92015-05-06 13:43:59 -0400806 if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000807 goto err;
808 }
809 ret = 1;
Rich Salz18cd23d2015-05-07 23:41:07 -0400810
Ulf Möllerc7235be2006-02-12 23:11:56 +0000811 err:
Matt Caswell0f113f32015-01-22 03:40:55 +0000812 if (!ret) {
813 ASN1_INTEGER_free(serial);
814 serial = NULL;
815 }
816 BIO_free_all(in);
817 BN_free(bn);
818 return serial;
819}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000820
821static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
Matt Caswell0f113f32015-01-22 03:40:55 +0000822{
823 int ret = 0;
824 BIO *out = NULL;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000825
Rich Salz75ebbd92015-05-06 13:43:59 -0400826 if ((out = BIO_new_file(serialfile, "w")) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000827 goto err;
828 if (i2a_ASN1_INTEGER(out, serial) <= 0)
829 goto err;
830 if (BIO_puts(out, "\n") <= 0)
831 goto err;
832 ret = 1;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000833 err:
Matt Caswell0f113f32015-01-22 03:40:55 +0000834 if (!ret)
835 BIO_printf(bio_err, "could not save serial number to %s\n",
836 serialfile);
837 BIO_free_all(out);
838 return ret;
839}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000840
Rich Salz18cd23d2015-05-07 23:41:07 -0400841
Ulf Möllerc7235be2006-02-12 23:11:56 +0000842/*
843 * Verify-related method definitions.
844 */
845
FdaSilvaYYcc696292016-08-04 23:52:22 +0200846static int verify_command(const char *data, const char *digest, const char *queryfile,
847 const char *in, int token_in,
Richard Levittefd3397f2019-03-07 15:26:34 +0100848 const char *CApath, const char *CAfile,
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +0100849 const char *CAstore, char *untrusted,
fbroda08538fc2016-03-15 10:08:49 +0100850 X509_VERIFY_PARAM *vpm)
Matt Caswell0f113f32015-01-22 03:40:55 +0000851{
852 BIO *in_bio = NULL;
853 PKCS7 *token = NULL;
854 TS_RESP *response = NULL;
855 TS_VERIFY_CTX *verify_ctx = NULL;
856 int ret = 0;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000857
Rich Salz75ebbd92015-05-06 13:43:59 -0400858 if ((in_bio = BIO_new_file(in, "rb")) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000859 goto end;
860 if (token_in) {
Rich Salz75ebbd92015-05-06 13:43:59 -0400861 if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000862 goto end;
863 } else {
Rich Salz75ebbd92015-05-06 13:43:59 -0400864 if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000865 goto end;
866 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000867
Rich Salz75ebbd92015-05-06 13:43:59 -0400868 if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
Richard Levittefd3397f2019-03-07 15:26:34 +0100869 CApath, CAfile, CAstore, untrusted,
fbroda08538fc2016-03-15 10:08:49 +0100870 vpm)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000871 goto end;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000872
Rich Salz18cd23d2015-05-07 23:41:07 -0400873 ret = token_in
874 ? TS_RESP_verify_token(verify_ctx, token)
875 : TS_RESP_verify_response(verify_ctx, response);
Ulf Möllerc7235be2006-02-12 23:11:56 +0000876
877 end:
Matt Caswell0f113f32015-01-22 03:40:55 +0000878 printf("Verification: ");
879 if (ret)
880 printf("OK\n");
881 else {
882 printf("FAILED\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000883 ERR_print_errors(bio_err);
884 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000885
Matt Caswell0f113f32015-01-22 03:40:55 +0000886 BIO_free_all(in_bio);
887 PKCS7_free(token);
888 TS_RESP_free(response);
889 TS_VERIFY_CTX_free(verify_ctx);
890 return ret;
891}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000892
FdaSilvaYYcc696292016-08-04 23:52:22 +0200893static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
894 const char *queryfile,
895 const char *CApath, const char *CAfile,
Richard Levittefd3397f2019-03-07 15:26:34 +0100896 const char *CAstore,
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +0100897 char *untrusted,
fbroda08538fc2016-03-15 10:08:49 +0100898 X509_VERIFY_PARAM *vpm)
Matt Caswell0f113f32015-01-22 03:40:55 +0000899{
900 TS_VERIFY_CTX *ctx = NULL;
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +0100901 STACK_OF(X509) *certs;
Matt Caswell0f113f32015-01-22 03:40:55 +0000902 BIO *input = NULL;
903 TS_REQ *request = NULL;
904 int ret = 0;
Rich Salzca4a4942015-06-10 14:07:40 -0400905 int f = 0;
Ulf Möllerc7235be2006-02-12 23:11:56 +0000906
Matt Caswell0f113f32015-01-22 03:40:55 +0000907 if (data != NULL || digest != NULL) {
Rich Salz75ebbd92015-05-06 13:43:59 -0400908 if ((ctx = TS_VERIFY_CTX_new()) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000909 goto err;
Rich Salzca4a4942015-06-10 14:07:40 -0400910 f = TS_VFY_VERSION | TS_VFY_SIGNER;
Matt Caswell0f113f32015-01-22 03:40:55 +0000911 if (data != NULL) {
Yuchie0670972017-02-05 19:33:47 -0500912 BIO *out = NULL;
913
Rich Salzca4a4942015-06-10 14:07:40 -0400914 f |= TS_VFY_DATA;
Yuchie0670972017-02-05 19:33:47 -0500915 if ((out = BIO_new_file(data, "rb")) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000916 goto err;
Yuchie0670972017-02-05 19:33:47 -0500917 if (TS_VERIFY_CTX_set_data(ctx, out) == NULL) {
918 BIO_free_all(out);
919 goto err;
920 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000921 } else if (digest != NULL) {
922 long imprint_len;
Rich Salz14f051a2016-04-13 15:58:28 -0400923 unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len);
Rich Salzca4a4942015-06-10 14:07:40 -0400924 f |= TS_VFY_IMPRINT;
925 if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000926 BIO_printf(bio_err, "invalid digest string\n");
927 goto err;
928 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000929 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000930
Matt Caswell0f113f32015-01-22 03:40:55 +0000931 } else if (queryfile != NULL) {
Rich Salz75ebbd92015-05-06 13:43:59 -0400932 if ((input = BIO_new_file(queryfile, "rb")) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000933 goto err;
Rich Salz75ebbd92015-05-06 13:43:59 -0400934 if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000935 goto err;
Rich Salz75ebbd92015-05-06 13:43:59 -0400936 if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000937 goto err;
Paul Yang22342122017-06-13 01:24:02 +0800938 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +0000939 return NULL;
Paul Yang22342122017-06-13 01:24:02 +0800940 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000941
Matt Caswell0f113f32015-01-22 03:40:55 +0000942 /* Add the signature verification flag and arguments. */
Rich Salzca4a4942015-06-10 14:07:40 -0400943 TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
Ulf Möllerc7235be2006-02-12 23:11:56 +0000944
Matt Caswell0f113f32015-01-22 03:40:55 +0000945 /* Initialising the X509_STORE object. */
Richard Levittefd3397f2019-03-07 15:26:34 +0100946 if (TS_VERIFY_CTX_set_store(ctx,
947 create_cert_store(CApath, CAfile, CAstore, vpm))
Rich Salzca4a4942015-06-10 14:07:40 -0400948 == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000949 goto err;
950
Dr. David von Oheimbf62846b2021-03-10 17:27:13 +0100951 /* Loading any extra untrusted certificates. */
952 if (untrusted != NULL) {
953 certs = load_certs_multifile(untrusted, NULL, "extra untrusted certs",
954 vpm);
955 if (certs == NULL || TS_VERIFY_CTX_set_certs(ctx, certs) == NULL)
956 goto err;
957 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000958 ret = 1;
Rich Salz18cd23d2015-05-07 23:41:07 -0400959
Ulf Möllerc7235be2006-02-12 23:11:56 +0000960 err:
Matt Caswell0f113f32015-01-22 03:40:55 +0000961 if (!ret) {
962 TS_VERIFY_CTX_free(ctx);
963 ctx = NULL;
964 }
965 BIO_free_all(input);
966 TS_REQ_free(request);
967 return ctx;
968}
Ulf Möllerc7235be2006-02-12 23:11:56 +0000969
FdaSilvaYYcc696292016-08-04 23:52:22 +0200970static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
Richard Levittefd3397f2019-03-07 15:26:34 +0100971 const char *CAstore, X509_VERIFY_PARAM *vpm)
Matt Caswell0f113f32015-01-22 03:40:55 +0000972{
973 X509_STORE *cert_ctx = NULL;
974 X509_LOOKUP *lookup = NULL;
Dr. Matthias St. Pierreb4250012020-10-15 12:55:50 +0300975 OSSL_LIB_CTX *libctx = app_get0_libctx();
Shane Lontis67256822020-07-24 22:53:27 +1000976 const char *propq = app_get0_propq();
Ulf Möllerc7235be2006-02-12 23:11:56 +0000977
Matt Caswell0f113f32015-01-22 03:40:55 +0000978 cert_ctx = X509_STORE_new();
Matt Caswell0f113f32015-01-22 03:40:55 +0000979 X509_STORE_set_verify_cb(cert_ctx, verify_cb);
Matt Caswell96487cd2015-10-30 11:18:04 +0000980 if (CApath != NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000981 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
982 if (lookup == NULL) {
983 BIO_printf(bio_err, "memory allocation failure\n");
984 goto err;
985 }
Richard Levittefd3397f2019-03-07 15:26:34 +0100986 if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400987 BIO_printf(bio_err, "Error loading directory %s\n", CApath);
Matt Caswell0f113f32015-01-22 03:40:55 +0000988 goto err;
989 }
990 }
Ulf Möllerc7235be2006-02-12 23:11:56 +0000991
Matt Caswell96487cd2015-10-30 11:18:04 +0000992 if (CAfile != NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000993 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
994 if (lookup == NULL) {
995 BIO_printf(bio_err, "memory allocation failure\n");
996 goto err;
997 }
Matt Caswelld8652be2020-09-24 10:42:23 +0100998 if (!X509_LOOKUP_load_file_ex(lookup, CAfile, X509_FILETYPE_PEM, libctx,
999 propq)) {
Rich Salz7e1b7482015-04-24 15:26:15 -04001000 BIO_printf(bio_err, "Error loading file %s\n", CAfile);
Matt Caswell0f113f32015-01-22 03:40:55 +00001001 goto err;
1002 }
1003 }
fbroda08538fc2016-03-15 10:08:49 +01001004
Richard Levittefd3397f2019-03-07 15:26:34 +01001005 if (CAstore != NULL) {
1006 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_store());
1007 if (lookup == NULL) {
1008 BIO_printf(bio_err, "memory allocation failure\n");
1009 goto err;
1010 }
Matt Caswelld8652be2020-09-24 10:42:23 +01001011 if (!X509_LOOKUP_load_store_ex(lookup, CAstore, libctx, propq)) {
Richard Levittefd3397f2019-03-07 15:26:34 +01001012 BIO_printf(bio_err, "Error loading store URI %s\n", CAstore);
1013 goto err;
1014 }
1015 }
1016
FdaSilvaYY6b4a77f2016-06-28 22:51:51 +02001017 if (vpm != NULL)
fbroda08538fc2016-03-15 10:08:49 +01001018 X509_STORE_set1_param(cert_ctx, vpm);
1019
Matt Caswell0f113f32015-01-22 03:40:55 +00001020 return cert_ctx;
Rich Salz18cd23d2015-05-07 23:41:07 -04001021
Ulf Möllerc7235be2006-02-12 23:11:56 +00001022 err:
Matt Caswell0f113f32015-01-22 03:40:55 +00001023 X509_STORE_free(cert_ctx);
1024 return NULL;
1025}
Ulf Möllerc7235be2006-02-12 23:11:56 +00001026
Rich Salz6d23cf92015-01-12 17:29:26 -05001027static int verify_cb(int ok, X509_STORE_CTX *ctx)
Matt Caswell0f113f32015-01-22 03:40:55 +00001028{
Matt Caswell0f113f32015-01-22 03:40:55 +00001029 return ok;
1030}