Synchronise VMS scripts with Unix Makefiles
[openssl.git] / apps / spkac.c
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>
67 #include <openssl/rsa.h>
68 #include <openssl/evp.h>
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
79 int MAIN(int argc, char **argv)
80         {
81         int i,badops=0, ret = 1;
82         BIO *in = NULL,*out = NULL, *key = NULL;
83         int verify=0,noout=0;
84         char *infile = NULL,*outfile = NULL,*prog;
85         char *spkac = "SPKAC", *spksect = "default", *spkstr = NULL;
86         char *challenge = NULL, *keyfile = NULL;
87         LHASH *conf;
88         NETSCAPE_SPKI *spki = NULL;
89         EVP_PKEY *pkey = NULL;
90
91         apps_startup();
92
93         if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
94
95         prog=argv[0];
96         argc--;
97         argv++;
98         while (argc >= 1)
99                 {
100                 if (strcmp(*argv,"-in") == 0)
101                         {
102                         if (--argc < 1) goto bad;
103                         infile= *(++argv);
104                         }
105                 else if (strcmp(*argv,"-out") == 0)
106                         {
107                         if (--argc < 1) goto bad;
108                         outfile= *(++argv);
109                         }
110                 else if (strcmp(*argv,"-key") == 0)
111                         {
112                         if (--argc < 1) goto bad;
113                         keyfile= *(++argv);
114                         }
115                 else if (strcmp(*argv,"-challenge") == 0)
116                         {
117                         if (--argc < 1) goto bad;
118                         challenge= *(++argv);
119                         }
120                 else if (strcmp(*argv,"-spkac") == 0)
121                         {
122                         if (--argc < 1) goto bad;
123                         spkac= *(++argv);
124                         }
125                 else if (strcmp(*argv,"-spksect") == 0)
126                         {
127                         if (--argc < 1) goto bad;
128                         spksect= *(++argv);
129                         }
130                 else if (strcmp(*argv,"-noout") == 0)
131                         noout=1;
132                 else if (strcmp(*argv,"-verify") == 0)
133                         verify=1;
134                 else badops = 1;
135                 argc--;
136                 argv++;
137                 }
138
139         if (badops)
140                 {
141 bad:
142                 BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
143                 BIO_printf(bio_err,"where options are\n");
144                 BIO_printf(bio_err," -in arg       input file\n");
145                 BIO_printf(bio_err," -out arg      output file\n");
146                 BIO_printf(bio_err," -spkac arg    alternative SPKAC name\n");
147                 BIO_printf(bio_err," -noout        don't print SPKAC\n");
148                 BIO_printf(bio_err," -verify       verify SPKAC signature\n");
149                 goto end;
150                 }
151
152         ERR_load_crypto_strings();
153
154         if(keyfile) {
155                 if(strcmp(keyfile, "-")) key = BIO_new_file(keyfile, "r");
156                 else key = BIO_new_fp(stdin, BIO_NOCLOSE);
157                 if(!key) {
158                         BIO_printf(bio_err, "Error opening key file\n");
159                         ERR_print_errors(bio_err);
160                         goto end;
161                 }
162                 pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, NULL);
163                 if(!pkey) {
164                         BIO_printf(bio_err, "Error reading private key\n");
165                         ERR_print_errors(bio_err);
166                         goto end;
167                 }
168                 spki = NETSCAPE_SPKI_new();
169                 if(challenge) ASN1_STRING_set(spki->spkac->challenge,
170                                                  challenge, strlen(challenge));
171                 NETSCAPE_SPKI_set_pubkey(spki, pkey);
172                 NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
173                 spkstr = NETSCAPE_SPKI_b64_encode(spki);
174
175                 if (outfile) out = BIO_new_file(outfile, "w");
176                 else out = BIO_new_fp(stdout, BIO_NOCLOSE);
177
178                 if(!out) {
179                         BIO_printf(bio_err, "Error opening output file\n");
180                         ERR_print_errors(bio_err);
181                         goto end;
182                 }
183                 BIO_printf(out, "SPKAC=%s\n", spkstr);
184                 ret = 0;
185                 goto end;
186         }
187
188         
189
190         if (infile) in = BIO_new_file(infile, "r");
191         else in = BIO_new_fp(stdin, BIO_NOCLOSE);
192
193         if(!in) {
194                 BIO_printf(bio_err, "Error opening input file\n");
195                 ERR_print_errors(bio_err);
196                 goto end;
197         }
198
199         conf = CONF_load_bio(NULL, in, NULL);
200
201         if(!conf) {
202                 BIO_printf(bio_err, "Error parsing config file\n");
203                 ERR_print_errors(bio_err);
204                 goto end;
205         }
206
207         spkstr = CONF_get_string(conf, spksect, spkac);
208                 
209         if(!spkstr) {
210                 BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
211                 ERR_print_errors(bio_err);
212                 goto end;
213         }
214
215         spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
216         if(!spki) {
217                 BIO_printf(bio_err, "Error loading SPKAC\n");
218                 ERR_print_errors(bio_err);
219                 goto end;
220         }
221
222         if (outfile) out = BIO_new_file(outfile, "w");
223         else out = BIO_new_fp(stdout, BIO_NOCLOSE);
224
225         if(!out) {
226                 BIO_printf(bio_err, "Error opening output file\n");
227                 ERR_print_errors(bio_err);
228                 goto end;
229         }
230
231         if(!noout) NETSCAPE_SPKI_print(out, spki);
232         if(verify) {
233                 EVP_PKEY *pktmp;
234                 pktmp = NETSCAPE_SPKI_get_pubkey(spki);
235                 i = NETSCAPE_SPKI_verify(spki, pktmp);
236                 EVP_PKEY_free(pktmp);
237                 if(i) BIO_printf(bio_err, "Signature OK\n");
238                 else {
239                         BIO_printf(bio_err, "Signature Failure\n");
240                         ERR_print_errors(bio_err);
241                         goto end;
242                 }
243         }
244
245         ret = 0;
246
247 end:
248         NETSCAPE_SPKI_free(spki);
249         BIO_free(in);
250         BIO_free(out);
251         BIO_free(key);
252         EVP_PKEY_free(pkey);
253         if(spkstr) Free(spkstr);
254         EXIT(ret);
255         }