]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/spkac.c
Update copyright year
[thirdparty/openssl.git] / apps / spkac.c
1 /*
2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <time.h>
14 #include "apps.h"
15 #include "progs.h"
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>
22
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,
28 OPT_PROV_ENUM
29 } OPTION_CHOICE;
30
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"},
38 #endif
39
40 OPT_SECTION("Input"),
41 {"in", OPT_IN, '<', "Input file"},
42 {"key", OPT_KEY, '<', "Create SPKAC using private key"},
43 {"keyform", OPT_KEYFORM, 'f', "Private key file format - default PEM (PEM, DER, or ENGINE)"},
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"},
47
48 OPT_SECTION("Output"),
49 {"out", OPT_OUT, '>', "Output file"},
50 {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
51 {"pubkey", OPT_PUBKEY, '-', "Output public key"},
52 {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
53
54 OPT_PROV_OPTIONS,
55 {NULL}
56 };
57
58 int spkac_main(int argc, char **argv)
59 {
60 BIO *out = NULL;
61 CONF *conf = NULL;
62 ENGINE *e = NULL;
63 EVP_PKEY *pkey = NULL;
64 NETSCAPE_SPKI *spki = NULL;
65 char *challenge = NULL, *keyfile = NULL;
66 char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
67 char *spkstr = NULL, *prog;
68 const char *spkac = "SPKAC", *spksect = "default";
69 int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
70 int keyformat = FORMAT_PEM;
71 OPTION_CHOICE o;
72
73 prog = opt_init(argc, argv, spkac_options);
74 while ((o = opt_next()) != OPT_EOF) {
75 switch (o) {
76 case OPT_EOF:
77 case OPT_ERR:
78 opthelp:
79 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
80 goto end;
81 case OPT_HELP:
82 opt_help(spkac_options);
83 ret = 0;
84 goto end;
85 case OPT_IN:
86 infile = opt_arg();
87 break;
88 case OPT_OUT:
89 outfile = opt_arg();
90 break;
91 case OPT_NOOUT:
92 noout = 1;
93 break;
94 case OPT_PUBKEY:
95 pubkey = 1;
96 break;
97 case OPT_VERIFY:
98 verify = 1;
99 break;
100 case OPT_PASSIN:
101 passinarg = opt_arg();
102 break;
103 case OPT_KEY:
104 keyfile = opt_arg();
105 break;
106 case OPT_KEYFORM:
107 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
108 goto opthelp;
109 break;
110 case OPT_CHALLENGE:
111 challenge = opt_arg();
112 break;
113 case OPT_SPKAC:
114 spkac = opt_arg();
115 break;
116 case OPT_SPKSECT:
117 spksect = opt_arg();
118 break;
119 case OPT_ENGINE:
120 e = setup_engine(opt_arg(), 0);
121 break;
122 case OPT_PROV_CASES:
123 if (!opt_provider(o))
124 goto end;
125 break;
126 }
127 }
128 argc = opt_num_rest();
129 if (argc != 0)
130 goto opthelp;
131
132 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
133 BIO_printf(bio_err, "Error getting password\n");
134 goto end;
135 }
136
137 if (keyfile != NULL) {
138 pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
139 keyformat, 1, passin, e, "private key");
140 if (pkey == NULL)
141 goto end;
142 spki = NETSCAPE_SPKI_new();
143 if (spki == NULL)
144 goto end;
145 if (challenge != NULL)
146 ASN1_STRING_set(spki->spkac->challenge,
147 challenge, (int)strlen(challenge));
148 if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
149 BIO_printf(bio_err, "Error setting public key\n");
150 goto end;
151 }
152 i = NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
153 if (i <= 0) {
154 BIO_printf(bio_err, "Error signing SPKAC\n");
155 goto end;
156 }
157 spkstr = NETSCAPE_SPKI_b64_encode(spki);
158 if (spkstr == NULL)
159 goto end;
160
161 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
162 if (out == NULL) {
163 OPENSSL_free(spkstr);
164 goto end;
165 }
166 BIO_printf(out, "SPKAC=%s\n", spkstr);
167 OPENSSL_free(spkstr);
168 ret = 0;
169 goto end;
170 }
171
172 if ((conf = app_load_config(infile)) == NULL)
173 goto end;
174
175 spkstr = NCONF_get_string(conf, spksect, spkac);
176
177 if (spkstr == NULL) {
178 BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
179 ERR_print_errors(bio_err);
180 goto end;
181 }
182
183 spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
184
185 if (spki == NULL) {
186 BIO_printf(bio_err, "Error loading SPKAC\n");
187 ERR_print_errors(bio_err);
188 goto end;
189 }
190
191 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
192 if (out == NULL)
193 goto end;
194
195 if (!noout)
196 NETSCAPE_SPKI_print(out, spki);
197 pkey = NETSCAPE_SPKI_get_pubkey(spki);
198 if (verify) {
199 i = NETSCAPE_SPKI_verify(spki, pkey);
200 if (i > 0) {
201 BIO_printf(bio_err, "Signature OK\n");
202 } else {
203 BIO_printf(bio_err, "Signature Failure\n");
204 ERR_print_errors(bio_err);
205 goto end;
206 }
207 }
208 if (pubkey)
209 PEM_write_bio_PUBKEY(out, pkey);
210
211 ret = 0;
212
213 end:
214 NCONF_free(conf);
215 NETSCAPE_SPKI_free(spki);
216 BIO_free_all(out);
217 EVP_PKEY_free(pkey);
218 release_engine(e);
219 OPENSSL_free(passin);
220 return ret;
221 }