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