10Sstevel@tonic-gate /* apps/spkac.c */
20Sstevel@tonic-gate
30Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
40Sstevel@tonic-gate * project 1999. Based on an original idea by Massimiliano Pala
50Sstevel@tonic-gate * (madwolf@openca.org).
60Sstevel@tonic-gate */
70Sstevel@tonic-gate /* ====================================================================
80Sstevel@tonic-gate * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
110Sstevel@tonic-gate * modification, are permitted provided that the following conditions
120Sstevel@tonic-gate * are met:
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
150Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
180Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
190Sstevel@tonic-gate * the documentation and/or other materials provided with the
200Sstevel@tonic-gate * distribution.
210Sstevel@tonic-gate *
220Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
230Sstevel@tonic-gate * software must display the following acknowledgment:
240Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
250Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
260Sstevel@tonic-gate *
270Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
280Sstevel@tonic-gate * endorse or promote products derived from this software without
290Sstevel@tonic-gate * prior written permission. For written permission, please contact
300Sstevel@tonic-gate * licensing@OpenSSL.org.
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
330Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
340Sstevel@tonic-gate * permission of the OpenSSL Project.
350Sstevel@tonic-gate *
360Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
370Sstevel@tonic-gate * acknowledgment:
380Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
390Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
420Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
440Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
450Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
460Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
470Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
480Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
490Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
500Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
510Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
520Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
530Sstevel@tonic-gate * ====================================================================
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
560Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
570Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
580Sstevel@tonic-gate *
590Sstevel@tonic-gate */
600Sstevel@tonic-gate #include <stdio.h>
610Sstevel@tonic-gate #include <stdlib.h>
620Sstevel@tonic-gate #include <string.h>
630Sstevel@tonic-gate #include <time.h>
640Sstevel@tonic-gate #include "apps.h"
650Sstevel@tonic-gate #include <openssl/bio.h>
660Sstevel@tonic-gate #include <openssl/conf.h>
670Sstevel@tonic-gate #include <openssl/err.h>
680Sstevel@tonic-gate #include <openssl/evp.h>
690Sstevel@tonic-gate #include <openssl/lhash.h>
700Sstevel@tonic-gate #include <openssl/x509.h>
710Sstevel@tonic-gate #include <openssl/pem.h>
720Sstevel@tonic-gate
730Sstevel@tonic-gate #undef PROG
740Sstevel@tonic-gate #define PROG spkac_main
750Sstevel@tonic-gate
760Sstevel@tonic-gate /* -in arg - input file - default stdin
770Sstevel@tonic-gate * -out arg - output file - default stdout
780Sstevel@tonic-gate */
790Sstevel@tonic-gate
800Sstevel@tonic-gate int MAIN(int, char **);
810Sstevel@tonic-gate
MAIN(int argc,char ** argv)820Sstevel@tonic-gate int MAIN(int argc, char **argv)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate ENGINE *e = NULL;
850Sstevel@tonic-gate int i,badops=0, ret = 1;
860Sstevel@tonic-gate BIO *in = NULL,*out = NULL;
870Sstevel@tonic-gate int verify=0,noout=0,pubkey=0;
880Sstevel@tonic-gate char *infile = NULL,*outfile = NULL,*prog;
890Sstevel@tonic-gate char *passargin = NULL, *passin = NULL;
902139Sjp161948 const char *spkac = "SPKAC", *spksect = "default";
912139Sjp161948 char *spkstr = NULL;
920Sstevel@tonic-gate char *challenge = NULL, *keyfile = NULL;
930Sstevel@tonic-gate CONF *conf = NULL;
940Sstevel@tonic-gate NETSCAPE_SPKI *spki = NULL;
950Sstevel@tonic-gate EVP_PKEY *pkey = NULL;
960Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
970Sstevel@tonic-gate char *engine=NULL;
980Sstevel@tonic-gate #endif
990Sstevel@tonic-gate
1000Sstevel@tonic-gate apps_startup();
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate if (!load_config(bio_err, NULL))
1050Sstevel@tonic-gate goto end;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate prog=argv[0];
1080Sstevel@tonic-gate argc--;
1090Sstevel@tonic-gate argv++;
1100Sstevel@tonic-gate while (argc >= 1)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate if (strcmp(*argv,"-in") == 0)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate if (--argc < 1) goto bad;
1150Sstevel@tonic-gate infile= *(++argv);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate else if (strcmp(*argv,"-out") == 0)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate if (--argc < 1) goto bad;
1200Sstevel@tonic-gate outfile= *(++argv);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate else if (strcmp(*argv,"-passin") == 0)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate if (--argc < 1) goto bad;
1250Sstevel@tonic-gate passargin= *(++argv);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate else if (strcmp(*argv,"-key") == 0)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate if (--argc < 1) goto bad;
1300Sstevel@tonic-gate keyfile= *(++argv);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate else if (strcmp(*argv,"-challenge") == 0)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate if (--argc < 1) goto bad;
1350Sstevel@tonic-gate challenge= *(++argv);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate else if (strcmp(*argv,"-spkac") == 0)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate if (--argc < 1) goto bad;
1400Sstevel@tonic-gate spkac= *(++argv);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate else if (strcmp(*argv,"-spksect") == 0)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate if (--argc < 1) goto bad;
1450Sstevel@tonic-gate spksect= *(++argv);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1480Sstevel@tonic-gate else if (strcmp(*argv,"-engine") == 0)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate if (--argc < 1) goto bad;
1510Sstevel@tonic-gate engine= *(++argv);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate else if (strcmp(*argv,"-noout") == 0)
1550Sstevel@tonic-gate noout=1;
1560Sstevel@tonic-gate else if (strcmp(*argv,"-pubkey") == 0)
1570Sstevel@tonic-gate pubkey=1;
1580Sstevel@tonic-gate else if (strcmp(*argv,"-verify") == 0)
1590Sstevel@tonic-gate verify=1;
1600Sstevel@tonic-gate else badops = 1;
1610Sstevel@tonic-gate argc--;
1620Sstevel@tonic-gate argv++;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if (badops)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate bad:
1680Sstevel@tonic-gate BIO_printf(bio_err,"%s [options]\n",prog);
1690Sstevel@tonic-gate BIO_printf(bio_err,"where options are\n");
1700Sstevel@tonic-gate BIO_printf(bio_err," -in arg input file\n");
1710Sstevel@tonic-gate BIO_printf(bio_err," -out arg output file\n");
1720Sstevel@tonic-gate BIO_printf(bio_err," -key arg create SPKAC using private key\n");
1730Sstevel@tonic-gate BIO_printf(bio_err," -passin arg input file pass phrase source\n");
1740Sstevel@tonic-gate BIO_printf(bio_err," -challenge arg challenge string\n");
1750Sstevel@tonic-gate BIO_printf(bio_err," -spkac arg alternative SPKAC name\n");
1760Sstevel@tonic-gate BIO_printf(bio_err," -noout don't print SPKAC\n");
1770Sstevel@tonic-gate BIO_printf(bio_err," -pubkey output public key\n");
1780Sstevel@tonic-gate BIO_printf(bio_err," -verify verify SPKAC signature\n");
1790Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1800Sstevel@tonic-gate BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
1810Sstevel@tonic-gate #endif
1820Sstevel@tonic-gate goto end;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate ERR_load_crypto_strings();
1860Sstevel@tonic-gate if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
1870Sstevel@tonic-gate BIO_printf(bio_err, "Error getting password\n");
1880Sstevel@tonic-gate goto end;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1920Sstevel@tonic-gate e = setup_engine(bio_err, engine, 0);
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate if(keyfile) {
1960Sstevel@tonic-gate pkey = load_key(bio_err,
1970Sstevel@tonic-gate strcmp(keyfile, "-") ? keyfile : NULL,
1980Sstevel@tonic-gate FORMAT_PEM, 1, passin, e, "private key");
1990Sstevel@tonic-gate if(!pkey) {
2000Sstevel@tonic-gate goto end;
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate spki = NETSCAPE_SPKI_new();
2030Sstevel@tonic-gate if(challenge) ASN1_STRING_set(spki->spkac->challenge,
2042139Sjp161948 challenge, (int)strlen(challenge));
2050Sstevel@tonic-gate NETSCAPE_SPKI_set_pubkey(spki, pkey);
2060Sstevel@tonic-gate NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
2070Sstevel@tonic-gate spkstr = NETSCAPE_SPKI_b64_encode(spki);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate if (outfile) out = BIO_new_file(outfile, "w");
2100Sstevel@tonic-gate else {
2110Sstevel@tonic-gate out = BIO_new_fp(stdout, BIO_NOCLOSE);
2120Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
2150Sstevel@tonic-gate out = BIO_push(tmpbio, out);
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate #endif
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if(!out) {
2210Sstevel@tonic-gate BIO_printf(bio_err, "Error opening output file\n");
2220Sstevel@tonic-gate ERR_print_errors(bio_err);
2230Sstevel@tonic-gate goto end;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate BIO_printf(out, "SPKAC=%s\n", spkstr);
2260Sstevel@tonic-gate OPENSSL_free(spkstr);
2270Sstevel@tonic-gate ret = 0;
2280Sstevel@tonic-gate goto end;
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (infile) in = BIO_new_file(infile, "r");
2340Sstevel@tonic-gate else in = BIO_new_fp(stdin, BIO_NOCLOSE);
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if(!in) {
2370Sstevel@tonic-gate BIO_printf(bio_err, "Error opening input file\n");
2380Sstevel@tonic-gate ERR_print_errors(bio_err);
2390Sstevel@tonic-gate goto end;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate conf = NCONF_new(NULL);
2430Sstevel@tonic-gate i = NCONF_load_bio(conf, in, NULL);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate if(!i) {
2460Sstevel@tonic-gate BIO_printf(bio_err, "Error parsing config file\n");
2470Sstevel@tonic-gate ERR_print_errors(bio_err);
2480Sstevel@tonic-gate goto end;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate spkstr = NCONF_get_string(conf, spksect, spkac);
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate if(!spkstr) {
2540Sstevel@tonic-gate BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
2550Sstevel@tonic-gate ERR_print_errors(bio_err);
2560Sstevel@tonic-gate goto end;
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate if(!spki) {
2620Sstevel@tonic-gate BIO_printf(bio_err, "Error loading SPKAC\n");
2630Sstevel@tonic-gate ERR_print_errors(bio_err);
2640Sstevel@tonic-gate goto end;
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate if (outfile) out = BIO_new_file(outfile, "w");
2680Sstevel@tonic-gate else {
2690Sstevel@tonic-gate out = BIO_new_fp(stdout, BIO_NOCLOSE);
2700Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
2730Sstevel@tonic-gate out = BIO_push(tmpbio, out);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate #endif
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate if(!out) {
2790Sstevel@tonic-gate BIO_printf(bio_err, "Error opening output file\n");
2800Sstevel@tonic-gate ERR_print_errors(bio_err);
2810Sstevel@tonic-gate goto end;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate if(!noout) NETSCAPE_SPKI_print(out, spki);
2850Sstevel@tonic-gate pkey = NETSCAPE_SPKI_get_pubkey(spki);
2860Sstevel@tonic-gate if(verify) {
2870Sstevel@tonic-gate i = NETSCAPE_SPKI_verify(spki, pkey);
288*8545SJan.Pechanec@Sun.COM if (i > 0) BIO_printf(bio_err, "Signature OK\n");
2890Sstevel@tonic-gate else {
2900Sstevel@tonic-gate BIO_printf(bio_err, "Signature Failure\n");
2910Sstevel@tonic-gate ERR_print_errors(bio_err);
2920Sstevel@tonic-gate goto end;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate if(pubkey) PEM_write_bio_PUBKEY(out, pkey);
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate ret = 0;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate end:
3000Sstevel@tonic-gate NCONF_free(conf);
3010Sstevel@tonic-gate NETSCAPE_SPKI_free(spki);
3020Sstevel@tonic-gate BIO_free(in);
3030Sstevel@tonic-gate BIO_free_all(out);
3040Sstevel@tonic-gate EVP_PKEY_free(pkey);
3050Sstevel@tonic-gate if(passin) OPENSSL_free(passin);
3060Sstevel@tonic-gate apps_shutdown();
3070Sstevel@tonic-gate OPENSSL_EXIT(ret);
3080Sstevel@tonic-gate }
309