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