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