]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
6738bf14 | 2 | * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. |
8ce97163 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
8ce97163 | 8 | */ |
846e33c7 | 9 | |
8ce97163 DSH |
10 | #include <stdio.h> |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <time.h> | |
14 | #include "apps.h" | |
dab2cd68 | 15 | #include "progs.h" |
8ce97163 | 16 | #include <openssl/bio.h> |
de83c122 | 17 | #include <openssl/conf.h> |
8ce97163 | 18 | #include <openssl/err.h> |
8ce97163 DSH |
19 | #include <openssl/evp.h> |
20 | #include <openssl/x509.h> | |
21 | #include <openssl/pem.h> | |
22 | ||
7e1b7485 RS |
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, | |
66e59702 | 27 | OPT_SPKSECT, OPT_KEYFORM |
7e1b7485 RS |
28 | } OPTION_CHOICE; |
29 | ||
44c83ebd | 30 | const OPTIONS spkac_options[] = { |
5388f986 | 31 | OPT_SECTION("General"), |
7e1b7485 | 32 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
33 | {"spksect", OPT_SPKSECT, 's', |
34 | "Specify the name of an SPKAC-dedicated section of configuration"}, | |
35 | #ifndef OPENSSL_NO_ENGINE | |
36 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
37 | #endif | |
38 | ||
39 | OPT_SECTION("Input"), | |
7e1b7485 | 40 | {"in", OPT_IN, '<', "Input file"}, |
7e1b7485 | 41 | {"key", OPT_KEY, '<', "Create SPKAC using private key"}, |
66e59702 | 42 | {"keyform", OPT_KEYFORM, 'f', "Private key file format - default PEM (PEM, DER, or ENGINE)"}, |
7e1b7485 RS |
43 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
44 | {"challenge", OPT_CHALLENGE, 's', "Challenge string"}, | |
45 | {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"}, | |
5388f986 RS |
46 | |
47 | OPT_SECTION("Output"), | |
48 | {"out", OPT_OUT, '>', "Output file"}, | |
7e1b7485 RS |
49 | {"noout", OPT_NOOUT, '-', "Don't print SPKAC"}, |
50 | {"pubkey", OPT_PUBKEY, '-', "Output public key"}, | |
51 | {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"}, | |
7e1b7485 RS |
52 | {NULL} |
53 | }; | |
667ac4ec | 54 | |
7e1b7485 | 55 | int spkac_main(int argc, char **argv) |
0f113f3e | 56 | { |
cc01d217 | 57 | BIO *out = NULL; |
0f113f3e | 58 | CONF *conf = NULL; |
7e1b7485 | 59 | ENGINE *e = NULL; |
0f113f3e | 60 | EVP_PKEY *pkey = NULL; |
7e1b7485 | 61 | NETSCAPE_SPKI *spki = NULL; |
333b070e | 62 | char *challenge = NULL, *keyfile = NULL; |
7e1b7485 RS |
63 | char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL; |
64 | char *spkstr = NULL, *prog; | |
65 | const char *spkac = "SPKAC", *spksect = "default"; | |
66 | int i, ret = 1, verify = 0, noout = 0, pubkey = 0; | |
66e59702 | 67 | int keyformat = FORMAT_PEM; |
7e1b7485 RS |
68 | OPTION_CHOICE o; |
69 | ||
70 | prog = opt_init(argc, argv, spkac_options); | |
71 | while ((o = opt_next()) != OPT_EOF) { | |
72 | switch (o) { | |
73 | case OPT_EOF: | |
74 | case OPT_ERR: | |
03358517 | 75 | opthelp: |
7e1b7485 RS |
76 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
77 | goto end; | |
78 | case OPT_HELP: | |
79 | opt_help(spkac_options); | |
80 | ret = 0; | |
81 | goto end; | |
82 | case OPT_IN: | |
83 | infile = opt_arg(); | |
84 | break; | |
85 | case OPT_OUT: | |
86 | outfile = opt_arg(); | |
87 | break; | |
88 | case OPT_NOOUT: | |
0f113f3e | 89 | noout = 1; |
7e1b7485 RS |
90 | break; |
91 | case OPT_PUBKEY: | |
0f113f3e | 92 | pubkey = 1; |
7e1b7485 RS |
93 | break; |
94 | case OPT_VERIFY: | |
0f113f3e | 95 | verify = 1; |
7e1b7485 RS |
96 | break; |
97 | case OPT_PASSIN: | |
98 | passinarg = opt_arg(); | |
99 | break; | |
100 | case OPT_KEY: | |
101 | keyfile = opt_arg(); | |
102 | break; | |
66e59702 LF |
103 | case OPT_KEYFORM: |
104 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat)) | |
105 | goto opthelp; | |
32c69853 | 106 | break; |
7e1b7485 RS |
107 | case OPT_CHALLENGE: |
108 | challenge = opt_arg(); | |
109 | break; | |
110 | case OPT_SPKAC: | |
111 | spkac = opt_arg(); | |
112 | break; | |
113 | case OPT_SPKSECT: | |
114 | spksect = opt_arg(); | |
115 | break; | |
116 | case OPT_ENGINE: | |
333b070e | 117 | e = setup_engine(opt_arg(), 0); |
7e1b7485 | 118 | break; |
7e1b7485 | 119 | } |
0f113f3e | 120 | } |
7e1b7485 | 121 | argc = opt_num_rest(); |
03358517 KR |
122 | if (argc != 0) |
123 | goto opthelp; | |
8ce97163 | 124 | |
7e1b7485 | 125 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
126 | BIO_printf(bio_err, "Error getting password\n"); |
127 | goto end; | |
128 | } | |
5270e702 | 129 | |
f2582f08 | 130 | if (keyfile != NULL) { |
7e1b7485 | 131 | pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL, |
66e59702 | 132 | keyformat, 1, passin, e, "private key"); |
f2582f08 | 133 | if (pkey == NULL) |
0f113f3e | 134 | goto end; |
0f113f3e | 135 | spki = NETSCAPE_SPKI_new(); |
f2582f08 PY |
136 | if (spki == NULL) |
137 | goto end; | |
138 | if (challenge != NULL) | |
0f113f3e MC |
139 | ASN1_STRING_set(spki->spkac->challenge, |
140 | challenge, (int)strlen(challenge)); | |
141 | NETSCAPE_SPKI_set_pubkey(spki, pkey); | |
142 | NETSCAPE_SPKI_sign(spki, pkey, EVP_md5()); | |
143 | spkstr = NETSCAPE_SPKI_b64_encode(spki); | |
f2582f08 PY |
144 | if (spkstr == NULL) |
145 | goto end; | |
8ce97163 | 146 | |
bdd58d98 | 147 | out = bio_open_default(outfile, 'w', FORMAT_TEXT); |
08f6ae5b MC |
148 | if (out == NULL) { |
149 | OPENSSL_free(spkstr); | |
0f113f3e | 150 | goto end; |
08f6ae5b | 151 | } |
0f113f3e MC |
152 | BIO_printf(out, "SPKAC=%s\n", spkstr); |
153 | OPENSSL_free(spkstr); | |
154 | ret = 0; | |
155 | goto end; | |
156 | } | |
8ce97163 | 157 | |
cc01d217 | 158 | if ((conf = app_load_config(infile)) == NULL) |
0f113f3e | 159 | goto end; |
8ce97163 | 160 | |
0f113f3e | 161 | spkstr = NCONF_get_string(conf, spksect, spkac); |
8ce97163 | 162 | |
96487cdd | 163 | if (spkstr == NULL) { |
0f113f3e MC |
164 | BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac); |
165 | ERR_print_errors(bio_err); | |
166 | goto end; | |
167 | } | |
8ce97163 | 168 | |
0f113f3e | 169 | spki = NETSCAPE_SPKI_b64_decode(spkstr, -1); |
8ce97163 | 170 | |
f2582f08 | 171 | if (spki == NULL) { |
0f113f3e MC |
172 | BIO_printf(bio_err, "Error loading SPKAC\n"); |
173 | ERR_print_errors(bio_err); | |
174 | goto end; | |
175 | } | |
8ce97163 | 176 | |
bdd58d98 | 177 | out = bio_open_default(outfile, 'w', FORMAT_TEXT); |
7e1b7485 | 178 | if (out == NULL) |
0f113f3e | 179 | goto end; |
8ce97163 | 180 | |
0f113f3e MC |
181 | if (!noout) |
182 | NETSCAPE_SPKI_print(out, spki); | |
183 | pkey = NETSCAPE_SPKI_get_pubkey(spki); | |
184 | if (verify) { | |
185 | i = NETSCAPE_SPKI_verify(spki, pkey); | |
f2582f08 | 186 | if (i > 0) { |
0f113f3e | 187 | BIO_printf(bio_err, "Signature OK\n"); |
f2582f08 | 188 | } else { |
0f113f3e MC |
189 | BIO_printf(bio_err, "Signature Failure\n"); |
190 | ERR_print_errors(bio_err); | |
191 | goto end; | |
192 | } | |
193 | } | |
194 | if (pubkey) | |
195 | PEM_write_bio_PUBKEY(out, pkey); | |
8ce97163 | 196 | |
0f113f3e | 197 | ret = 0; |
8ce97163 | 198 | |
0f113f3e MC |
199 | end: |
200 | NCONF_free(conf); | |
201 | NETSCAPE_SPKI_free(spki); | |
0f113f3e MC |
202 | BIO_free_all(out); |
203 | EVP_PKEY_free(pkey); | |
dd1abd44 | 204 | release_engine(e); |
b548a1f1 | 205 | OPENSSL_free(passin); |
26a7d938 | 206 | return ret; |
0f113f3e | 207 | } |