1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 1999-2021 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 <stdio.h> 11e1051a39Sopenharmony_ci#include <stdlib.h> 12e1051a39Sopenharmony_ci#include <string.h> 13e1051a39Sopenharmony_ci#include <time.h> 14e1051a39Sopenharmony_ci#include "apps.h" 15e1051a39Sopenharmony_ci#include "progs.h" 16e1051a39Sopenharmony_ci#include <openssl/bio.h> 17e1051a39Sopenharmony_ci#include <openssl/conf.h> 18e1051a39Sopenharmony_ci#include <openssl/err.h> 19e1051a39Sopenharmony_ci#include <openssl/evp.h> 20e1051a39Sopenharmony_ci#include <openssl/x509.h> 21e1051a39Sopenharmony_ci#include <openssl/pem.h> 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_citypedef enum OPTION_choice { 24e1051a39Sopenharmony_ci OPT_COMMON, 25e1051a39Sopenharmony_ci OPT_NOOUT, OPT_PUBKEY, OPT_VERIFY, OPT_IN, OPT_OUT, 26e1051a39Sopenharmony_ci OPT_ENGINE, OPT_KEY, OPT_CHALLENGE, OPT_PASSIN, OPT_SPKAC, 27e1051a39Sopenharmony_ci OPT_SPKSECT, OPT_KEYFORM, OPT_DIGEST, 28e1051a39Sopenharmony_ci OPT_PROV_ENUM 29e1051a39Sopenharmony_ci} OPTION_CHOICE; 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_ciconst OPTIONS spkac_options[] = { 32e1051a39Sopenharmony_ci OPT_SECTION("General"), 33e1051a39Sopenharmony_ci {"help", OPT_HELP, '-', "Display this summary"}, 34e1051a39Sopenharmony_ci {"spksect", OPT_SPKSECT, 's', 35e1051a39Sopenharmony_ci "Specify the name of an SPKAC-dedicated section of configuration"}, 36e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE 37e1051a39Sopenharmony_ci {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, 38e1051a39Sopenharmony_ci#endif 39e1051a39Sopenharmony_ci 40e1051a39Sopenharmony_ci OPT_SECTION("Input"), 41e1051a39Sopenharmony_ci {"in", OPT_IN, '<', "Input file"}, 42e1051a39Sopenharmony_ci {"key", OPT_KEY, '<', "Create SPKAC using private key"}, 43e1051a39Sopenharmony_ci {"keyform", OPT_KEYFORM, 'f', "Private key file format (ENGINE, other values ignored)"}, 44e1051a39Sopenharmony_ci {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, 45e1051a39Sopenharmony_ci {"challenge", OPT_CHALLENGE, 's', "Challenge string"}, 46e1051a39Sopenharmony_ci {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"}, 47e1051a39Sopenharmony_ci 48e1051a39Sopenharmony_ci OPT_SECTION("Output"), 49e1051a39Sopenharmony_ci {"digest", OPT_DIGEST, 's', "Sign new SPKAC with the specified digest (default: MD5)" }, 50e1051a39Sopenharmony_ci {"out", OPT_OUT, '>', "Output file"}, 51e1051a39Sopenharmony_ci {"noout", OPT_NOOUT, '-', "Don't print SPKAC"}, 52e1051a39Sopenharmony_ci {"pubkey", OPT_PUBKEY, '-', "Output public key"}, 53e1051a39Sopenharmony_ci {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"}, 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_ci OPT_PROV_OPTIONS, 56e1051a39Sopenharmony_ci {NULL} 57e1051a39Sopenharmony_ci}; 58e1051a39Sopenharmony_ci 59e1051a39Sopenharmony_ciint spkac_main(int argc, char **argv) 60e1051a39Sopenharmony_ci{ 61e1051a39Sopenharmony_ci BIO *out = NULL; 62e1051a39Sopenharmony_ci CONF *conf = NULL; 63e1051a39Sopenharmony_ci ENGINE *e = NULL; 64e1051a39Sopenharmony_ci EVP_PKEY *pkey = NULL; 65e1051a39Sopenharmony_ci NETSCAPE_SPKI *spki = NULL; 66e1051a39Sopenharmony_ci char *challenge = NULL, *keyfile = NULL; 67e1051a39Sopenharmony_ci char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL; 68e1051a39Sopenharmony_ci char *spkstr = NULL, *prog; 69e1051a39Sopenharmony_ci const char *spkac = "SPKAC", *spksect = "default"; 70e1051a39Sopenharmony_ci const char *digest = "MD5"; 71e1051a39Sopenharmony_ci EVP_MD *md = NULL; 72e1051a39Sopenharmony_ci int i, ret = 1, verify = 0, noout = 0, pubkey = 0; 73e1051a39Sopenharmony_ci int keyformat = FORMAT_UNDEF; 74e1051a39Sopenharmony_ci OPTION_CHOICE o; 75e1051a39Sopenharmony_ci 76e1051a39Sopenharmony_ci prog = opt_init(argc, argv, spkac_options); 77e1051a39Sopenharmony_ci while ((o = opt_next()) != OPT_EOF) { 78e1051a39Sopenharmony_ci switch (o) { 79e1051a39Sopenharmony_ci case OPT_EOF: 80e1051a39Sopenharmony_ci case OPT_ERR: 81e1051a39Sopenharmony_ci opthelp: 82e1051a39Sopenharmony_ci BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); 83e1051a39Sopenharmony_ci goto end; 84e1051a39Sopenharmony_ci case OPT_HELP: 85e1051a39Sopenharmony_ci opt_help(spkac_options); 86e1051a39Sopenharmony_ci ret = 0; 87e1051a39Sopenharmony_ci goto end; 88e1051a39Sopenharmony_ci case OPT_IN: 89e1051a39Sopenharmony_ci infile = opt_arg(); 90e1051a39Sopenharmony_ci break; 91e1051a39Sopenharmony_ci case OPT_OUT: 92e1051a39Sopenharmony_ci outfile = opt_arg(); 93e1051a39Sopenharmony_ci break; 94e1051a39Sopenharmony_ci case OPT_NOOUT: 95e1051a39Sopenharmony_ci noout = 1; 96e1051a39Sopenharmony_ci break; 97e1051a39Sopenharmony_ci case OPT_PUBKEY: 98e1051a39Sopenharmony_ci pubkey = 1; 99e1051a39Sopenharmony_ci break; 100e1051a39Sopenharmony_ci case OPT_VERIFY: 101e1051a39Sopenharmony_ci verify = 1; 102e1051a39Sopenharmony_ci break; 103e1051a39Sopenharmony_ci case OPT_PASSIN: 104e1051a39Sopenharmony_ci passinarg = opt_arg(); 105e1051a39Sopenharmony_ci break; 106e1051a39Sopenharmony_ci case OPT_KEY: 107e1051a39Sopenharmony_ci keyfile = opt_arg(); 108e1051a39Sopenharmony_ci break; 109e1051a39Sopenharmony_ci case OPT_KEYFORM: 110e1051a39Sopenharmony_ci if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat)) 111e1051a39Sopenharmony_ci goto opthelp; 112e1051a39Sopenharmony_ci break; 113e1051a39Sopenharmony_ci case OPT_CHALLENGE: 114e1051a39Sopenharmony_ci challenge = opt_arg(); 115e1051a39Sopenharmony_ci break; 116e1051a39Sopenharmony_ci case OPT_SPKAC: 117e1051a39Sopenharmony_ci spkac = opt_arg(); 118e1051a39Sopenharmony_ci break; 119e1051a39Sopenharmony_ci case OPT_SPKSECT: 120e1051a39Sopenharmony_ci spksect = opt_arg(); 121e1051a39Sopenharmony_ci break; 122e1051a39Sopenharmony_ci case OPT_DIGEST: 123e1051a39Sopenharmony_ci digest = opt_arg(); 124e1051a39Sopenharmony_ci break; 125e1051a39Sopenharmony_ci case OPT_ENGINE: 126e1051a39Sopenharmony_ci e = setup_engine(opt_arg(), 0); 127e1051a39Sopenharmony_ci break; 128e1051a39Sopenharmony_ci case OPT_PROV_CASES: 129e1051a39Sopenharmony_ci if (!opt_provider(o)) 130e1051a39Sopenharmony_ci goto end; 131e1051a39Sopenharmony_ci break; 132e1051a39Sopenharmony_ci } 133e1051a39Sopenharmony_ci } 134e1051a39Sopenharmony_ci 135e1051a39Sopenharmony_ci /* No extra arguments. */ 136e1051a39Sopenharmony_ci argc = opt_num_rest(); 137e1051a39Sopenharmony_ci if (argc != 0) 138e1051a39Sopenharmony_ci goto opthelp; 139e1051a39Sopenharmony_ci 140e1051a39Sopenharmony_ci if (!app_passwd(passinarg, NULL, &passin, NULL)) { 141e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error getting password\n"); 142e1051a39Sopenharmony_ci goto end; 143e1051a39Sopenharmony_ci } 144e1051a39Sopenharmony_ci 145e1051a39Sopenharmony_ci if (keyfile != NULL) { 146e1051a39Sopenharmony_ci if (!opt_md(digest, &md)) 147e1051a39Sopenharmony_ci goto end; 148e1051a39Sopenharmony_ci 149e1051a39Sopenharmony_ci pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL, 150e1051a39Sopenharmony_ci keyformat, 1, passin, e, "private key"); 151e1051a39Sopenharmony_ci if (pkey == NULL) 152e1051a39Sopenharmony_ci goto end; 153e1051a39Sopenharmony_ci spki = NETSCAPE_SPKI_new(); 154e1051a39Sopenharmony_ci if (spki == NULL) 155e1051a39Sopenharmony_ci goto end; 156e1051a39Sopenharmony_ci if (challenge != NULL) 157e1051a39Sopenharmony_ci ASN1_STRING_set(spki->spkac->challenge, 158e1051a39Sopenharmony_ci challenge, (int)strlen(challenge)); 159e1051a39Sopenharmony_ci if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) { 160e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error setting public key\n"); 161e1051a39Sopenharmony_ci goto end; 162e1051a39Sopenharmony_ci } 163e1051a39Sopenharmony_ci i = NETSCAPE_SPKI_sign(spki, pkey, md); 164e1051a39Sopenharmony_ci if (i <= 0) { 165e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error signing SPKAC\n"); 166e1051a39Sopenharmony_ci goto end; 167e1051a39Sopenharmony_ci } 168e1051a39Sopenharmony_ci spkstr = NETSCAPE_SPKI_b64_encode(spki); 169e1051a39Sopenharmony_ci if (spkstr == NULL) 170e1051a39Sopenharmony_ci goto end; 171e1051a39Sopenharmony_ci 172e1051a39Sopenharmony_ci out = bio_open_default(outfile, 'w', FORMAT_TEXT); 173e1051a39Sopenharmony_ci if (out == NULL) { 174e1051a39Sopenharmony_ci OPENSSL_free(spkstr); 175e1051a39Sopenharmony_ci goto end; 176e1051a39Sopenharmony_ci } 177e1051a39Sopenharmony_ci BIO_printf(out, "SPKAC=%s\n", spkstr); 178e1051a39Sopenharmony_ci OPENSSL_free(spkstr); 179e1051a39Sopenharmony_ci ret = 0; 180e1051a39Sopenharmony_ci goto end; 181e1051a39Sopenharmony_ci } 182e1051a39Sopenharmony_ci 183e1051a39Sopenharmony_ci if ((conf = app_load_config(infile)) == NULL) 184e1051a39Sopenharmony_ci goto end; 185e1051a39Sopenharmony_ci 186e1051a39Sopenharmony_ci spkstr = NCONF_get_string(conf, spksect, spkac); 187e1051a39Sopenharmony_ci 188e1051a39Sopenharmony_ci if (spkstr == NULL) { 189e1051a39Sopenharmony_ci BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac); 190e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 191e1051a39Sopenharmony_ci goto end; 192e1051a39Sopenharmony_ci } 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ci spki = NETSCAPE_SPKI_b64_decode(spkstr, -1); 195e1051a39Sopenharmony_ci 196e1051a39Sopenharmony_ci if (spki == NULL) { 197e1051a39Sopenharmony_ci BIO_printf(bio_err, "Error loading SPKAC\n"); 198e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 199e1051a39Sopenharmony_ci goto end; 200e1051a39Sopenharmony_ci } 201e1051a39Sopenharmony_ci 202e1051a39Sopenharmony_ci out = bio_open_default(outfile, 'w', FORMAT_TEXT); 203e1051a39Sopenharmony_ci if (out == NULL) 204e1051a39Sopenharmony_ci goto end; 205e1051a39Sopenharmony_ci 206e1051a39Sopenharmony_ci if (!noout) 207e1051a39Sopenharmony_ci NETSCAPE_SPKI_print(out, spki); 208e1051a39Sopenharmony_ci pkey = NETSCAPE_SPKI_get_pubkey(spki); 209e1051a39Sopenharmony_ci if (verify) { 210e1051a39Sopenharmony_ci i = NETSCAPE_SPKI_verify(spki, pkey); 211e1051a39Sopenharmony_ci if (i > 0) { 212e1051a39Sopenharmony_ci BIO_printf(bio_err, "Signature OK\n"); 213e1051a39Sopenharmony_ci } else { 214e1051a39Sopenharmony_ci BIO_printf(bio_err, "Signature Failure\n"); 215e1051a39Sopenharmony_ci ERR_print_errors(bio_err); 216e1051a39Sopenharmony_ci goto end; 217e1051a39Sopenharmony_ci } 218e1051a39Sopenharmony_ci } 219e1051a39Sopenharmony_ci if (pubkey) 220e1051a39Sopenharmony_ci PEM_write_bio_PUBKEY(out, pkey); 221e1051a39Sopenharmony_ci 222e1051a39Sopenharmony_ci ret = 0; 223e1051a39Sopenharmony_ci 224e1051a39Sopenharmony_ci end: 225e1051a39Sopenharmony_ci EVP_MD_free(md); 226e1051a39Sopenharmony_ci NCONF_free(conf); 227e1051a39Sopenharmony_ci NETSCAPE_SPKI_free(spki); 228e1051a39Sopenharmony_ci BIO_free_all(out); 229e1051a39Sopenharmony_ci EVP_PKEY_free(pkey); 230e1051a39Sopenharmony_ci release_engine(e); 231e1051a39Sopenharmony_ci OPENSSL_free(passin); 232e1051a39Sopenharmony_ci return ret; 233e1051a39Sopenharmony_ci} 234