Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 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> |
Bodo Möller | de83c12 | 2000-03-05 01:11:44 +0000 | [diff] [blame] | 66 | #include <openssl/conf.h> |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 67 | #include <openssl/err.h> |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 68 | #include <openssl/evp.h> |
Bodo Möller | 37634c8 | 2000-03-03 23:27:56 +0000 | [diff] [blame] | 69 | #include <openssl/lhash.h> |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 70 | #include <openssl/x509.h> |
| 71 | #include <openssl/pem.h> |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 72 | #include <openssl/engine.h> |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 73 | |
| 74 | #undef PROG |
| 75 | #define PROG spkac_main |
| 76 | |
| 77 | /* -in arg - input file - default stdin |
| 78 | * -out arg - output file - default stdout |
| 79 | */ |
| 80 | |
Ralf S. Engelschall | 667ac4e | 2000-02-11 09:47:18 +0000 | [diff] [blame] | 81 | int MAIN(int, char **); |
| 82 | |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 83 | int MAIN(int argc, char **argv) |
| 84 | { |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 85 | ENGINE *e = NULL; |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 86 | int i,badops=0, ret = 1; |
| 87 | BIO *in = NULL,*out = NULL, *key = NULL; |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 88 | int verify=0,noout=0,pubkey=0; |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 89 | char *infile = NULL,*outfile = NULL,*prog; |
| 90 | char *passargin = NULL, *passin = NULL; |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 91 | char *spkac = "SPKAC", *spksect = "default", *spkstr = NULL; |
| 92 | char *challenge = NULL, *keyfile = NULL; |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 93 | LHASH *conf = NULL; |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 94 | NETSCAPE_SPKI *spki = NULL; |
| 95 | EVP_PKEY *pkey = NULL; |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 96 | char *engine=NULL; |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 97 | |
| 98 | apps_startup(); |
| 99 | |
| 100 | if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); |
| 101 | |
| 102 | prog=argv[0]; |
| 103 | argc--; |
| 104 | argv++; |
| 105 | while (argc >= 1) |
| 106 | { |
| 107 | if (strcmp(*argv,"-in") == 0) |
| 108 | { |
| 109 | if (--argc < 1) goto bad; |
| 110 | infile= *(++argv); |
| 111 | } |
| 112 | else if (strcmp(*argv,"-out") == 0) |
| 113 | { |
| 114 | if (--argc < 1) goto bad; |
| 115 | outfile= *(++argv); |
| 116 | } |
Dr. Stephen Henson | f07fb9b | 2000-02-08 01:34:59 +0000 | [diff] [blame] | 117 | else if (strcmp(*argv,"-passin") == 0) |
| 118 | { |
| 119 | if (--argc < 1) goto bad; |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 120 | passargin= *(++argv); |
Dr. Stephen Henson | f07fb9b | 2000-02-08 01:34:59 +0000 | [diff] [blame] | 121 | } |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 122 | else if (strcmp(*argv,"-key") == 0) |
| 123 | { |
| 124 | if (--argc < 1) goto bad; |
| 125 | keyfile= *(++argv); |
| 126 | } |
| 127 | else if (strcmp(*argv,"-challenge") == 0) |
| 128 | { |
| 129 | if (--argc < 1) goto bad; |
| 130 | challenge= *(++argv); |
| 131 | } |
| 132 | else if (strcmp(*argv,"-spkac") == 0) |
| 133 | { |
| 134 | if (--argc < 1) goto bad; |
| 135 | spkac= *(++argv); |
| 136 | } |
| 137 | else if (strcmp(*argv,"-spksect") == 0) |
| 138 | { |
| 139 | if (--argc < 1) goto bad; |
| 140 | spksect= *(++argv); |
| 141 | } |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 142 | else if (strcmp(*argv,"-engine") == 0) |
| 143 | { |
| 144 | if (--argc < 1) goto bad; |
| 145 | engine= *(++argv); |
| 146 | } |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 147 | else if (strcmp(*argv,"-noout") == 0) |
| 148 | noout=1; |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 149 | else if (strcmp(*argv,"-pubkey") == 0) |
| 150 | pubkey=1; |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 151 | else if (strcmp(*argv,"-verify") == 0) |
| 152 | verify=1; |
| 153 | else badops = 1; |
| 154 | argc--; |
| 155 | argv++; |
| 156 | } |
| 157 | |
| 158 | if (badops) |
| 159 | { |
| 160 | bad: |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 161 | BIO_printf(bio_err,"%s [options]\n",prog); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 162 | BIO_printf(bio_err,"where options are\n"); |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 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"); |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 166 | BIO_printf(bio_err," -passin arg input file pass phrase source\n"); |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 167 | BIO_printf(bio_err," -challenge arg challenge string\n"); |
| 168 | BIO_printf(bio_err," -spkac arg alternative SPKAC name\n"); |
| 169 | BIO_printf(bio_err," -noout don't print SPKAC\n"); |
| 170 | BIO_printf(bio_err," -pubkey output public key\n"); |
| 171 | BIO_printf(bio_err," -verify verify SPKAC signature\n"); |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 172 | BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n"); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 173 | goto end; |
| 174 | } |
| 175 | |
| 176 | ERR_load_crypto_strings(); |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 177 | if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { |
| 178 | BIO_printf(bio_err, "Error getting password\n"); |
| 179 | goto end; |
| 180 | } |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 181 | |
Richard Levitte | 5270e70 | 2000-10-26 21:07:28 +0000 | [diff] [blame] | 182 | if (engine != NULL) |
| 183 | { |
| 184 | if((e = ENGINE_by_id(engine)) == NULL) |
| 185 | { |
| 186 | BIO_printf(bio_err,"invalid engine \"%s\"\n", |
| 187 | engine); |
| 188 | goto end; |
| 189 | } |
| 190 | if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) |
| 191 | { |
| 192 | BIO_printf(bio_err,"can't use that engine\n"); |
| 193 | goto end; |
| 194 | } |
| 195 | BIO_printf(bio_err,"engine \"%s\" set.\n", engine); |
| 196 | /* Free our "structural" reference. */ |
| 197 | ENGINE_free(e); |
| 198 | } |
| 199 | |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 200 | if(keyfile) { |
| 201 | if(strcmp(keyfile, "-")) key = BIO_new_file(keyfile, "r"); |
| 202 | else key = BIO_new_fp(stdin, BIO_NOCLOSE); |
| 203 | if(!key) { |
| 204 | BIO_printf(bio_err, "Error opening key file\n"); |
| 205 | ERR_print_errors(bio_err); |
| 206 | goto end; |
| 207 | } |
Dr. Stephen Henson | a3fe382 | 2000-02-16 23:16:01 +0000 | [diff] [blame] | 208 | pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, passin); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 209 | if(!pkey) { |
| 210 | BIO_printf(bio_err, "Error reading private key\n"); |
| 211 | ERR_print_errors(bio_err); |
| 212 | goto end; |
| 213 | } |
| 214 | spki = NETSCAPE_SPKI_new(); |
| 215 | if(challenge) ASN1_STRING_set(spki->spkac->challenge, |
| 216 | challenge, strlen(challenge)); |
| 217 | NETSCAPE_SPKI_set_pubkey(spki, pkey); |
| 218 | NETSCAPE_SPKI_sign(spki, pkey, EVP_md5()); |
| 219 | spkstr = NETSCAPE_SPKI_b64_encode(spki); |
| 220 | |
| 221 | if (outfile) out = BIO_new_file(outfile, "w"); |
Richard Levitte | 645749e | 2000-09-20 13:55:50 +0000 | [diff] [blame] | 222 | else { |
| 223 | out = BIO_new_fp(stdout, BIO_NOCLOSE); |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame^] | 224 | #ifdef OPENSSL_SYS_VMS |
Richard Levitte | 645749e | 2000-09-20 13:55:50 +0000 | [diff] [blame] | 225 | { |
| 226 | BIO *tmpbio = BIO_new(BIO_f_linebuffer()); |
| 227 | out = BIO_push(tmpbio, out); |
| 228 | } |
| 229 | #endif |
| 230 | } |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 231 | |
| 232 | if(!out) { |
| 233 | BIO_printf(bio_err, "Error opening output file\n"); |
| 234 | ERR_print_errors(bio_err); |
| 235 | goto end; |
| 236 | } |
| 237 | BIO_printf(out, "SPKAC=%s\n", spkstr); |
Richard Levitte | 26a3a48 | 2000-06-01 22:19:21 +0000 | [diff] [blame] | 238 | OPENSSL_free(spkstr); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 239 | ret = 0; |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | |
| 245 | if (infile) in = BIO_new_file(infile, "r"); |
| 246 | else in = BIO_new_fp(stdin, BIO_NOCLOSE); |
| 247 | |
| 248 | if(!in) { |
| 249 | BIO_printf(bio_err, "Error opening input file\n"); |
| 250 | ERR_print_errors(bio_err); |
| 251 | goto end; |
| 252 | } |
| 253 | |
| 254 | conf = CONF_load_bio(NULL, in, NULL); |
| 255 | |
| 256 | if(!conf) { |
| 257 | BIO_printf(bio_err, "Error parsing config file\n"); |
| 258 | ERR_print_errors(bio_err); |
| 259 | goto end; |
| 260 | } |
| 261 | |
| 262 | spkstr = CONF_get_string(conf, spksect, spkac); |
| 263 | |
| 264 | if(!spkstr) { |
| 265 | BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac); |
| 266 | ERR_print_errors(bio_err); |
| 267 | goto end; |
| 268 | } |
| 269 | |
| 270 | spki = NETSCAPE_SPKI_b64_decode(spkstr, -1); |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 271 | |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 272 | if(!spki) { |
| 273 | BIO_printf(bio_err, "Error loading SPKAC\n"); |
| 274 | ERR_print_errors(bio_err); |
| 275 | goto end; |
| 276 | } |
| 277 | |
| 278 | if (outfile) out = BIO_new_file(outfile, "w"); |
Richard Levitte | 645749e | 2000-09-20 13:55:50 +0000 | [diff] [blame] | 279 | else { |
| 280 | out = BIO_new_fp(stdout, BIO_NOCLOSE); |
Richard Levitte | bc36ee6 | 2001-02-20 08:13:47 +0000 | [diff] [blame^] | 281 | #ifdef OPENSSL_SYS_VMS |
Richard Levitte | 645749e | 2000-09-20 13:55:50 +0000 | [diff] [blame] | 282 | { |
| 283 | BIO *tmpbio = BIO_new(BIO_f_linebuffer()); |
| 284 | out = BIO_push(tmpbio, out); |
| 285 | } |
| 286 | #endif |
| 287 | } |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 288 | |
| 289 | if(!out) { |
| 290 | BIO_printf(bio_err, "Error opening output file\n"); |
| 291 | ERR_print_errors(bio_err); |
| 292 | goto end; |
| 293 | } |
| 294 | |
| 295 | if(!noout) NETSCAPE_SPKI_print(out, spki); |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 296 | pkey = NETSCAPE_SPKI_get_pubkey(spki); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 297 | if(verify) { |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 298 | i = NETSCAPE_SPKI_verify(spki, pkey); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 299 | if(i) BIO_printf(bio_err, "Signature OK\n"); |
| 300 | else { |
| 301 | BIO_printf(bio_err, "Signature Failure\n"); |
| 302 | ERR_print_errors(bio_err); |
| 303 | goto end; |
| 304 | } |
| 305 | } |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 306 | if(pubkey) PEM_write_bio_PUBKEY(out, pkey); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 307 | |
| 308 | ret = 0; |
| 309 | |
| 310 | end: |
Dr. Stephen Henson | 82fc1d9 | 2000-02-03 02:56:48 +0000 | [diff] [blame] | 311 | CONF_free(conf); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 312 | NETSCAPE_SPKI_free(spki); |
| 313 | BIO_free(in); |
Richard Levitte | 645749e | 2000-09-20 13:55:50 +0000 | [diff] [blame] | 314 | BIO_free_all(out); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 315 | BIO_free(key); |
| 316 | EVP_PKEY_free(pkey); |
Richard Levitte | 26a3a48 | 2000-06-01 22:19:21 +0000 | [diff] [blame] | 317 | if(passin) OPENSSL_free(passin); |
Dr. Stephen Henson | 8ce9716 | 1999-09-03 01:08:34 +0000 | [diff] [blame] | 318 | EXIT(ret); |
| 319 | } |