]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/spkac.c
Make gcc 2.95.2 happy again, even under ``-Wall -Wshadow -Wpointer-arith -Wcast-align
[thirdparty/openssl.git] / apps / spkac.c
CommitLineData
8ce97163
DSH
1/* apps/spkac.c */
2
3/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
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>
66#include <openssl/err.h>
8ce97163
DSH
67#include <openssl/evp.h>
68#include <openssl/x509.h>
69#include <openssl/pem.h>
70
71#undef PROG
72#define PROG spkac_main
73
74/* -in arg - input file - default stdin
75 * -out arg - output file - default stdout
76 */
77
667ac4ec
RE
78int MAIN(int, char **);
79
8ce97163
DSH
80int MAIN(int argc, char **argv)
81 {
82 int i,badops=0, ret = 1;
83 BIO *in = NULL,*out = NULL, *key = NULL;
82fc1d9c 84 int verify=0,noout=0,pubkey=0;
f07fb9b2 85 char *infile = NULL,*outfile = NULL,*prog, *passin = NULL;
8ce97163
DSH
86 char *spkac = "SPKAC", *spksect = "default", *spkstr = NULL;
87 char *challenge = NULL, *keyfile = NULL;
82fc1d9c 88 LHASH *conf = NULL;
8ce97163
DSH
89 NETSCAPE_SPKI *spki = NULL;
90 EVP_PKEY *pkey = NULL;
91
92 apps_startup();
93
94 if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
95
96 prog=argv[0];
97 argc--;
98 argv++;
99 while (argc >= 1)
100 {
101 if (strcmp(*argv,"-in") == 0)
102 {
103 if (--argc < 1) goto bad;
104 infile= *(++argv);
105 }
106 else if (strcmp(*argv,"-out") == 0)
107 {
108 if (--argc < 1) goto bad;
109 outfile= *(++argv);
110 }
f07fb9b2
DSH
111 else if (strcmp(*argv,"-passin") == 0)
112 {
113 if (--argc < 1) goto bad;
114 passin= *(++argv);
115 }
116 else if (strcmp(*argv,"-envpassin") == 0)
117 {
118 if (--argc < 1) goto bad;
119 if(!(passin= getenv(*(++argv))))
120 {
121 BIO_printf(bio_err,
122 "Can't read environment variable %s\n",
123 *argv);
124 badops = 1;
125 }
126 }
8ce97163
DSH
127 else if (strcmp(*argv,"-key") == 0)
128 {
129 if (--argc < 1) goto bad;
130 keyfile= *(++argv);
131 }
132 else if (strcmp(*argv,"-challenge") == 0)
133 {
134 if (--argc < 1) goto bad;
135 challenge= *(++argv);
136 }
137 else if (strcmp(*argv,"-spkac") == 0)
138 {
139 if (--argc < 1) goto bad;
140 spkac= *(++argv);
141 }
142 else if (strcmp(*argv,"-spksect") == 0)
143 {
144 if (--argc < 1) goto bad;
145 spksect= *(++argv);
146 }
147 else if (strcmp(*argv,"-noout") == 0)
148 noout=1;
82fc1d9c
DSH
149 else if (strcmp(*argv,"-pubkey") == 0)
150 pubkey=1;
8ce97163
DSH
151 else if (strcmp(*argv,"-verify") == 0)
152 verify=1;
153 else badops = 1;
154 argc--;
155 argv++;
156 }
157
158 if (badops)
159 {
160bad:
82fc1d9c 161 BIO_printf(bio_err,"%s [options]\n",prog);
8ce97163 162 BIO_printf(bio_err,"where options are\n");
82fc1d9c
DSH
163 BIO_printf(bio_err," -in arg input file\n");
164 BIO_printf(bio_err," -out arg output file\n");
165 BIO_printf(bio_err," -key arg create SPKAC using private key\n");
f07fb9b2
DSH
166 BIO_printf(bio_err," -passin arg input file pass phrase\n");
167 BIO_printf(bio_err," -envpassin arg environment variable containing input file pass phrase\n");
82fc1d9c
DSH
168 BIO_printf(bio_err," -challenge arg challenge string\n");
169 BIO_printf(bio_err," -spkac arg alternative SPKAC name\n");
170 BIO_printf(bio_err," -noout don't print SPKAC\n");
171 BIO_printf(bio_err," -pubkey output public key\n");
172 BIO_printf(bio_err," -verify verify SPKAC signature\n");
8ce97163
DSH
173 goto end;
174 }
175
176 ERR_load_crypto_strings();
177
178 if(keyfile) {
179 if(strcmp(keyfile, "-")) key = BIO_new_file(keyfile, "r");
180 else key = BIO_new_fp(stdin, BIO_NOCLOSE);
181 if(!key) {
182 BIO_printf(bio_err, "Error opening key file\n");
183 ERR_print_errors(bio_err);
184 goto end;
185 }
f07fb9b2 186 pkey = PEM_read_bio_PrivateKey(key, NULL, PEM_cb, passin);
8ce97163
DSH
187 if(!pkey) {
188 BIO_printf(bio_err, "Error reading private key\n");
189 ERR_print_errors(bio_err);
190 goto end;
191 }
192 spki = NETSCAPE_SPKI_new();
193 if(challenge) ASN1_STRING_set(spki->spkac->challenge,
194 challenge, strlen(challenge));
195 NETSCAPE_SPKI_set_pubkey(spki, pkey);
196 NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
197 spkstr = NETSCAPE_SPKI_b64_encode(spki);
198
199 if (outfile) out = BIO_new_file(outfile, "w");
200 else out = BIO_new_fp(stdout, BIO_NOCLOSE);
201
202 if(!out) {
203 BIO_printf(bio_err, "Error opening output file\n");
204 ERR_print_errors(bio_err);
205 goto end;
206 }
207 BIO_printf(out, "SPKAC=%s\n", spkstr);
82fc1d9c 208 Free(spkstr);
8ce97163
DSH
209 ret = 0;
210 goto end;
211 }
212
213
214
215 if (infile) in = BIO_new_file(infile, "r");
216 else in = BIO_new_fp(stdin, BIO_NOCLOSE);
217
218 if(!in) {
219 BIO_printf(bio_err, "Error opening input file\n");
220 ERR_print_errors(bio_err);
221 goto end;
222 }
223
224 conf = CONF_load_bio(NULL, in, NULL);
225
226 if(!conf) {
227 BIO_printf(bio_err, "Error parsing config file\n");
228 ERR_print_errors(bio_err);
229 goto end;
230 }
231
232 spkstr = CONF_get_string(conf, spksect, spkac);
233
234 if(!spkstr) {
235 BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
236 ERR_print_errors(bio_err);
237 goto end;
238 }
239
240 spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
82fc1d9c 241
8ce97163
DSH
242 if(!spki) {
243 BIO_printf(bio_err, "Error loading SPKAC\n");
244 ERR_print_errors(bio_err);
245 goto end;
246 }
247
248 if (outfile) out = BIO_new_file(outfile, "w");
249 else out = BIO_new_fp(stdout, BIO_NOCLOSE);
250
251 if(!out) {
252 BIO_printf(bio_err, "Error opening output file\n");
253 ERR_print_errors(bio_err);
254 goto end;
255 }
256
257 if(!noout) NETSCAPE_SPKI_print(out, spki);
82fc1d9c 258 pkey = NETSCAPE_SPKI_get_pubkey(spki);
8ce97163 259 if(verify) {
82fc1d9c 260 i = NETSCAPE_SPKI_verify(spki, pkey);
8ce97163
DSH
261 if(i) BIO_printf(bio_err, "Signature OK\n");
262 else {
263 BIO_printf(bio_err, "Signature Failure\n");
264 ERR_print_errors(bio_err);
265 goto end;
266 }
267 }
82fc1d9c 268 if(pubkey) PEM_write_bio_PUBKEY(out, pkey);
8ce97163
DSH
269
270 ret = 0;
271
272end:
82fc1d9c 273 CONF_free(conf);
8ce97163
DSH
274 NETSCAPE_SPKI_free(spki);
275 BIO_free(in);
276 BIO_free(out);
277 BIO_free(key);
278 EVP_PKEY_free(pkey);
8ce97163
DSH
279 EXIT(ret);
280 }