]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/spkac.c
More tweaks for comments due indent issues
[thirdparty/openssl.git] / apps / spkac.c
CommitLineData
8ce97163
DSH
1/* apps/spkac.c */
2
2e597528 3/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
8ce97163
DSH
4 * project 1999. Based on an original idea by Massimiliano Pala
5 * (madwolf@openca.org).
6 */
7/* ====================================================================
8 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 *
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * licensing@OpenSSL.org.
31 *
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
35 *
36 * 6. Redistributions of any form whatsoever must retain the following
37 * acknowledgment:
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
58 *
59 */
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <time.h>
64#include "apps.h"
65#include <openssl/bio.h>
de83c122 66#include <openssl/conf.h>
8ce97163 67#include <openssl/err.h>
8ce97163 68#include <openssl/evp.h>
37634c8b 69#include <openssl/lhash.h>
8ce97163
DSH
70#include <openssl/x509.h>
71#include <openssl/pem.h>
72
73#undef PROG
74#define PROG spkac_main
75
1d97c843
TH
76/*-
77 * -in arg - input file - default stdin
8ce97163
DSH
78 * -out arg - output file - default stdout
79 */
80
667ac4ec
RE
81int MAIN(int, char **);
82
8ce97163
DSH
83int MAIN(int argc, char **argv)
84 {
5270e702 85 ENGINE *e = NULL;
8ce97163 86 int i,badops=0, ret = 1;
7953b8ff 87 BIO *in = NULL,*out = NULL;
82fc1d9c 88 int verify=0,noout=0,pubkey=0;
a3fe382e
DSH
89 char *infile = NULL,*outfile = NULL,*prog;
90 char *passargin = NULL, *passin = NULL;
7d727231
NL
91 const char *spkac = "SPKAC", *spksect = "default";
92 char *spkstr = NULL;
8ce97163 93 char *challenge = NULL, *keyfile = NULL;
b7a26e6d 94 CONF *conf = NULL;
8ce97163
DSH
95 NETSCAPE_SPKI *spki = NULL;
96 EVP_PKEY *pkey = NULL;
0b13e9f0 97#ifndef OPENSSL_NO_ENGINE
5270e702 98 char *engine=NULL;
0b13e9f0 99#endif
8ce97163
DSH
100
101 apps_startup();
102
103 if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
104
3647bee2
DSH
105 if (!load_config(bio_err, NULL))
106 goto end;
107
8ce97163
DSH
108 prog=argv[0];
109 argc--;
110 argv++;
111 while (argc >= 1)
112 {
113 if (strcmp(*argv,"-in") == 0)
114 {
115 if (--argc < 1) goto bad;
116 infile= *(++argv);
117 }
118 else if (strcmp(*argv,"-out") == 0)
119 {
120 if (--argc < 1) goto bad;
121 outfile= *(++argv);
122 }
f07fb9b2
DSH
123 else if (strcmp(*argv,"-passin") == 0)
124 {
125 if (--argc < 1) goto bad;
a3fe382e 126 passargin= *(++argv);
f07fb9b2 127 }
8ce97163
DSH
128 else if (strcmp(*argv,"-key") == 0)
129 {
130 if (--argc < 1) goto bad;
131 keyfile= *(++argv);
132 }
133 else if (strcmp(*argv,"-challenge") == 0)
134 {
135 if (--argc < 1) goto bad;
136 challenge= *(++argv);
137 }
138 else if (strcmp(*argv,"-spkac") == 0)
139 {
140 if (--argc < 1) goto bad;
141 spkac= *(++argv);
142 }
143 else if (strcmp(*argv,"-spksect") == 0)
144 {
145 if (--argc < 1) goto bad;
146 spksect= *(++argv);
147 }
0b13e9f0 148#ifndef OPENSSL_NO_ENGINE
5270e702
RL
149 else if (strcmp(*argv,"-engine") == 0)
150 {
151 if (--argc < 1) goto bad;
152 engine= *(++argv);
153 }
0b13e9f0 154#endif
8ce97163
DSH
155 else if (strcmp(*argv,"-noout") == 0)
156 noout=1;
82fc1d9c
DSH
157 else if (strcmp(*argv,"-pubkey") == 0)
158 pubkey=1;
8ce97163
DSH
159 else if (strcmp(*argv,"-verify") == 0)
160 verify=1;
161 else badops = 1;
162 argc--;
163 argv++;
164 }
165
166 if (badops)
167 {
168bad:
82fc1d9c 169 BIO_printf(bio_err,"%s [options]\n",prog);
8ce97163 170 BIO_printf(bio_err,"where options are\n");
82fc1d9c
DSH
171 BIO_printf(bio_err," -in arg input file\n");
172 BIO_printf(bio_err," -out arg output file\n");
173 BIO_printf(bio_err," -key arg create SPKAC using private key\n");
a3fe382e 174 BIO_printf(bio_err," -passin arg input file pass phrase source\n");
82fc1d9c
DSH
175 BIO_printf(bio_err," -challenge arg challenge string\n");
176 BIO_printf(bio_err," -spkac arg alternative SPKAC name\n");
177 BIO_printf(bio_err," -noout don't print SPKAC\n");
178 BIO_printf(bio_err," -pubkey output public key\n");
179 BIO_printf(bio_err," -verify verify SPKAC signature\n");
0b13e9f0 180#ifndef OPENSSL_NO_ENGINE
5270e702 181 BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
0b13e9f0 182#endif
8ce97163
DSH
183 goto end;
184 }
185
186 ERR_load_crypto_strings();
a3fe382e
DSH
187 if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
188 BIO_printf(bio_err, "Error getting password\n");
189 goto end;
190 }
8ce97163 191
0b13e9f0 192#ifndef OPENSSL_NO_ENGINE
531d630b 193 e = setup_engine(bio_err, engine, 0);
0b13e9f0 194#endif
5270e702 195
8ce97163 196 if(keyfile) {
7953b8ff
RL
197 pkey = load_key(bio_err,
198 strcmp(keyfile, "-") ? keyfile : NULL,
da9b9724 199 FORMAT_PEM, 1, passin, e, "private key");
8ce97163 200 if(!pkey) {
8ce97163
DSH
201 goto end;
202 }
203 spki = NETSCAPE_SPKI_new();
204 if(challenge) ASN1_STRING_set(spki->spkac->challenge,
7d727231 205 challenge, (int)strlen(challenge));
8ce97163
DSH
206 NETSCAPE_SPKI_set_pubkey(spki, pkey);
207 NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
208 spkstr = NETSCAPE_SPKI_b64_encode(spki);
209
210 if (outfile) out = BIO_new_file(outfile, "w");
645749ef
RL
211 else {
212 out = BIO_new_fp(stdout, BIO_NOCLOSE);
bc36ee62 213#ifdef OPENSSL_SYS_VMS
645749ef
RL
214 {
215 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
216 out = BIO_push(tmpbio, out);
217 }
218#endif
219 }
8ce97163
DSH
220
221 if(!out) {
222 BIO_printf(bio_err, "Error opening output file\n");
223 ERR_print_errors(bio_err);
224 goto end;
225 }
226 BIO_printf(out, "SPKAC=%s\n", spkstr);
26a3a48d 227 OPENSSL_free(spkstr);
8ce97163
DSH
228 ret = 0;
229 goto end;
230 }
231
232
233
234 if (infile) in = BIO_new_file(infile, "r");
235 else in = BIO_new_fp(stdin, BIO_NOCLOSE);
236
237 if(!in) {
238 BIO_printf(bio_err, "Error opening input file\n");
239 ERR_print_errors(bio_err);
240 goto end;
241 }
242
b7a26e6d
DSH
243 conf = NCONF_new(NULL);
244 i = NCONF_load_bio(conf, in, NULL);
8ce97163 245
b7a26e6d 246 if(!i) {
8ce97163
DSH
247 BIO_printf(bio_err, "Error parsing config file\n");
248 ERR_print_errors(bio_err);
249 goto end;
250 }
251
b7a26e6d 252 spkstr = NCONF_get_string(conf, spksect, spkac);
8ce97163
DSH
253
254 if(!spkstr) {
255 BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
256 ERR_print_errors(bio_err);
257 goto end;
258 }
259
260 spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
82fc1d9c 261
8ce97163
DSH
262 if(!spki) {
263 BIO_printf(bio_err, "Error loading SPKAC\n");
264 ERR_print_errors(bio_err);
265 goto end;
266 }
267
268 if (outfile) out = BIO_new_file(outfile, "w");
645749ef
RL
269 else {
270 out = BIO_new_fp(stdout, BIO_NOCLOSE);
bc36ee62 271#ifdef OPENSSL_SYS_VMS
645749ef
RL
272 {
273 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
274 out = BIO_push(tmpbio, out);
275 }
276#endif
277 }
8ce97163
DSH
278
279 if(!out) {
280 BIO_printf(bio_err, "Error opening output file\n");
281 ERR_print_errors(bio_err);
282 goto end;
283 }
284
285 if(!noout) NETSCAPE_SPKI_print(out, spki);
82fc1d9c 286 pkey = NETSCAPE_SPKI_get_pubkey(spki);
8ce97163 287 if(verify) {
82fc1d9c 288 i = NETSCAPE_SPKI_verify(spki, pkey);
bab53405 289 if (i > 0) BIO_printf(bio_err, "Signature OK\n");
8ce97163
DSH
290 else {
291 BIO_printf(bio_err, "Signature Failure\n");
292 ERR_print_errors(bio_err);
293 goto end;
294 }
295 }
82fc1d9c 296 if(pubkey) PEM_write_bio_PUBKEY(out, pkey);
8ce97163
DSH
297
298 ret = 0;
299
300end:
b7a26e6d 301 NCONF_free(conf);
8ce97163
DSH
302 NETSCAPE_SPKI_free(spki);
303 BIO_free(in);
645749ef 304 BIO_free_all(out);
8ce97163 305 EVP_PKEY_free(pkey);
26a3a48d 306 if(passin) OPENSSL_free(passin);
7953b8ff 307 apps_shutdown();
1c3e4a36 308 OPENSSL_EXIT(ret);
8ce97163 309 }