2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 {
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, OPT_DIGEST,
31 const OPTIONS spkac_options[] = {
32 OPT_SECTION("General"),
33 {"help", OPT_HELP, '-', "Display this summary"},
34 {"spksect", OPT_SPKSECT, 's',
35 "Specify the name of an SPKAC-dedicated section of configuration"},
36 #ifndef OPENSSL_NO_ENGINE
37 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
41 {"in", OPT_IN, '<', "Input file"},
42 {"key", OPT_KEY, '<', "Create SPKAC using private key"},
43 {"keyform", OPT_KEYFORM, 'f', "Private key file format (ENGINE, other values ignored)"},
44 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
45 {"challenge", OPT_CHALLENGE, 's', "Challenge string"},
46 {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"},
48 OPT_SECTION("Output"),
49 {"digest", OPT_DIGEST, 's', "Sign new SPKAC with the specified digest (default: MD5)" },
50 {"out", OPT_OUT, '>', "Output file"},
51 {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
52 {"pubkey", OPT_PUBKEY, '-', "Output public key"},
53 {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
59 int spkac_main(int argc, char **argv)
64 EVP_PKEY *pkey = NULL;
65 NETSCAPE_SPKI *spki = NULL;
66 char *challenge = NULL, *keyfile = NULL;
67 char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
68 char *spkstr = NULL, *prog;
69 const char *spkac = "SPKAC", *spksect = "default";
70 const char *digest = "MD5";
72 int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
73 int keyformat = FORMAT_UNDEF;
76 prog = opt_init(argc, argv, spkac_options);
77 while ((o = opt_next()) != OPT_EOF) {
82 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
85 opt_help(spkac_options);
104 passinarg = opt_arg();
110 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
114 challenge = opt_arg();
126 e = setup_engine(opt_arg(), 0);
129 if (!opt_provider(o))
135 /* No extra arguments. */
136 argc = opt_num_rest();
140 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
141 BIO_printf(bio_err, "Error getting password\n");
145 if (keyfile != NULL) {
146 if (!opt_md(digest, &md))
149 pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
150 keyformat, 1, passin, e, "private key");
153 spki = NETSCAPE_SPKI_new();
156 if (challenge != NULL)
157 ASN1_STRING_set(spki->spkac->challenge,
158 challenge, (int)strlen(challenge));
159 if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
160 BIO_printf(bio_err, "Error setting public key\n");
163 i = NETSCAPE_SPKI_sign(spki, pkey, md);
165 BIO_printf(bio_err, "Error signing SPKAC\n");
168 spkstr = NETSCAPE_SPKI_b64_encode(spki);
172 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
174 OPENSSL_free(spkstr);
177 BIO_printf(out, "SPKAC=%s\n", spkstr);
178 OPENSSL_free(spkstr);
183 if ((conf = app_load_config(infile)) == NULL)
186 spkstr = NCONF_get_string(conf, spksect, spkac);
188 if (spkstr == NULL) {
189 BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
190 ERR_print_errors(bio_err);
194 spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
197 BIO_printf(bio_err, "Error loading SPKAC\n");
198 ERR_print_errors(bio_err);
202 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
207 NETSCAPE_SPKI_print(out, spki);
208 pkey = NETSCAPE_SPKI_get_pubkey(spki);
210 i = NETSCAPE_SPKI_verify(spki, pkey);
212 BIO_printf(bio_err, "Signature OK\n");
214 BIO_printf(bio_err, "Signature Failure\n");
215 ERR_print_errors(bio_err);
220 PEM_write_bio_PUBKEY(out, pkey);
227 NETSCAPE_SPKI_free(spki);
231 OPENSSL_free(passin);