2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
16 #include <openssl/bio.h>
17 #include <openssl/conf.h>
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
21 #include <openssl/pem.h>
23 typedef enum OPTION_choice {
24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
25 OPT_NOOUT, OPT_PUBKEY, OPT_VERIFY, OPT_IN, OPT_OUT,
26 OPT_ENGINE, OPT_KEY, OPT_CHALLENGE, OPT_PASSIN, OPT_SPKAC,
27 OPT_SPKSECT, OPT_KEYFORM
30 const OPTIONS spkac_options[] = {
31 {"help", OPT_HELP, '-', "Display this summary"},
32 {"in", OPT_IN, '<', "Input file"},
33 {"out", OPT_OUT, '>', "Output file"},
34 {"key", OPT_KEY, '<', "Create SPKAC using private key"},
35 {"keyform", OPT_KEYFORM, 'f', "Private key file format - default PEM (PEM, DER, or ENGINE)"},
36 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
37 {"challenge", OPT_CHALLENGE, 's', "Challenge string"},
38 {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"},
39 {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
40 {"pubkey", OPT_PUBKEY, '-', "Output public key"},
41 {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
42 {"spksect", OPT_SPKSECT, 's',
43 "Specify the name of an SPKAC-dedicated section of configuration"},
44 #ifndef OPENSSL_NO_ENGINE
45 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
50 int spkac_main(int argc, char **argv)
55 EVP_PKEY *pkey = NULL;
56 NETSCAPE_SPKI *spki = NULL;
57 char *challenge = NULL, *keyfile = NULL;
58 char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
59 char *spkstr = NULL, *prog;
60 const char *spkac = "SPKAC", *spksect = "default";
61 int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
62 int keyformat = FORMAT_PEM;
65 prog = opt_init(argc, argv, spkac_options);
66 while ((o = opt_next()) != OPT_EOF) {
71 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
74 opt_help(spkac_options);
93 passinarg = opt_arg();
99 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
103 challenge = opt_arg();
112 e = setup_engine(opt_arg(), 0);
116 argc = opt_num_rest();
120 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
121 BIO_printf(bio_err, "Error getting password\n");
125 if (keyfile != NULL) {
126 pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
127 keyformat, 1, passin, e, "private key");
130 spki = NETSCAPE_SPKI_new();
133 if (challenge != NULL)
134 ASN1_STRING_set(spki->spkac->challenge,
135 challenge, (int)strlen(challenge));
136 NETSCAPE_SPKI_set_pubkey(spki, pkey);
137 NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
138 spkstr = NETSCAPE_SPKI_b64_encode(spki);
142 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
144 OPENSSL_free(spkstr);
147 BIO_printf(out, "SPKAC=%s\n", spkstr);
148 OPENSSL_free(spkstr);
153 if ((conf = app_load_config(infile)) == NULL)
156 spkstr = NCONF_get_string(conf, spksect, spkac);
158 if (spkstr == NULL) {
159 BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
160 ERR_print_errors(bio_err);
164 spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
167 BIO_printf(bio_err, "Error loading SPKAC\n");
168 ERR_print_errors(bio_err);
172 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
177 NETSCAPE_SPKI_print(out, spki);
178 pkey = NETSCAPE_SPKI_get_pubkey(spki);
180 i = NETSCAPE_SPKI_verify(spki, pkey);
182 BIO_printf(bio_err, "Signature OK\n");
184 BIO_printf(bio_err, "Signature Failure\n");
185 ERR_print_errors(bio_err);
190 PEM_write_bio_PUBKEY(out, pkey);
196 NETSCAPE_SPKI_free(spki);
200 OPENSSL_free(passin);