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