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