Remove unnecessary #include <openssl/lhash.h> directives.
[openssl.git] / apps / spkac.c
1 /*
2  * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <time.h>
14 #include "apps.h"
15 #include <openssl/bio.h>
16 #include <openssl/conf.h>
17 #include <openssl/err.h>
18 #include <openssl/evp.h>
19 #include <openssl/x509.h>
20 #include <openssl/pem.h>
21
22 typedef enum OPTION_choice {
23     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24     OPT_NOOUT, OPT_PUBKEY, OPT_VERIFY, OPT_IN, OPT_OUT,
25     OPT_ENGINE, OPT_KEY, OPT_CHALLENGE, OPT_PASSIN, OPT_SPKAC,
26     OPT_SPKSECT, OPT_KEYFORM
27 } OPTION_CHOICE;
28
29 const OPTIONS spkac_options[] = {
30     {"help", OPT_HELP, '-', "Display this summary"},
31     {"in", OPT_IN, '<', "Input file"},
32     {"out", OPT_OUT, '>', "Output file"},
33     {"key", OPT_KEY, '<', "Create SPKAC using private key"},
34     {"keyform", OPT_KEYFORM, 'f', "Private key file format - default PEM (PEM, DER, or ENGINE)"},
35     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
36     {"challenge", OPT_CHALLENGE, 's', "Challenge string"},
37     {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"},
38     {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
39     {"pubkey", OPT_PUBKEY, '-', "Output public key"},
40     {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
41     {"spksect", OPT_SPKSECT, 's',
42      "Specify the name of an SPKAC-dedicated section of configuration"},
43 #ifndef OPENSSL_NO_ENGINE
44     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
45 #endif
46     {NULL}
47 };
48
49 int spkac_main(int argc, char **argv)
50 {
51     BIO *out = NULL;
52     CONF *conf = NULL;
53     ENGINE *e = NULL;
54     EVP_PKEY *pkey = NULL;
55     NETSCAPE_SPKI *spki = NULL;
56     char *challenge = NULL, *keyfile = NULL;
57     char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
58     char *spkstr = NULL, *prog;
59     const char *spkac = "SPKAC", *spksect = "default";
60     int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
61     int keyformat = FORMAT_PEM;
62     OPTION_CHOICE o;
63
64     prog = opt_init(argc, argv, spkac_options);
65     while ((o = opt_next()) != OPT_EOF) {
66         switch (o) {
67         case OPT_EOF:
68         case OPT_ERR:
69  opthelp:
70             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
71             goto end;
72         case OPT_HELP:
73             opt_help(spkac_options);
74             ret = 0;
75             goto end;
76         case OPT_IN:
77             infile = opt_arg();
78             break;
79         case OPT_OUT:
80             outfile = opt_arg();
81             break;
82         case OPT_NOOUT:
83             noout = 1;
84             break;
85         case OPT_PUBKEY:
86             pubkey = 1;
87             break;
88         case OPT_VERIFY:
89             verify = 1;
90             break;
91         case OPT_PASSIN:
92             passinarg = opt_arg();
93             break;
94         case OPT_KEY:
95             keyfile = opt_arg();
96             break;
97         case OPT_KEYFORM:
98             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
99                 goto opthelp;
100             break;
101         case OPT_CHALLENGE:
102             challenge = opt_arg();
103             break;
104         case OPT_SPKAC:
105             spkac = opt_arg();
106             break;
107         case OPT_SPKSECT:
108             spksect = opt_arg();
109             break;
110         case OPT_ENGINE:
111             e = setup_engine(opt_arg(), 0);
112             break;
113         }
114     }
115     argc = opt_num_rest();
116     if (argc != 0)
117         goto opthelp;
118
119     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
120         BIO_printf(bio_err, "Error getting password\n");
121         goto end;
122     }
123
124     if (keyfile != NULL) {
125         pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
126                         keyformat, 1, passin, e, "private key");
127         if (pkey == NULL)
128             goto end;
129         spki = NETSCAPE_SPKI_new();
130         if (spki == NULL)
131             goto end;
132         if (challenge != NULL)
133             ASN1_STRING_set(spki->spkac->challenge,
134                             challenge, (int)strlen(challenge));
135         NETSCAPE_SPKI_set_pubkey(spki, pkey);
136         NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
137         spkstr = NETSCAPE_SPKI_b64_encode(spki);
138         if (spkstr == NULL)
139             goto end;
140
141         out = bio_open_default(outfile, 'w', FORMAT_TEXT);
142         if (out == NULL) {
143             OPENSSL_free(spkstr);
144             goto end;
145         }
146         BIO_printf(out, "SPKAC=%s\n", spkstr);
147         OPENSSL_free(spkstr);
148         ret = 0;
149         goto end;
150     }
151
152     if ((conf = app_load_config(infile)) == NULL)
153         goto end;
154
155     spkstr = NCONF_get_string(conf, spksect, spkac);
156
157     if (spkstr == NULL) {
158         BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
159         ERR_print_errors(bio_err);
160         goto end;
161     }
162
163     spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
164
165     if (spki == NULL) {
166         BIO_printf(bio_err, "Error loading SPKAC\n");
167         ERR_print_errors(bio_err);
168         goto end;
169     }
170
171     out = bio_open_default(outfile, 'w', FORMAT_TEXT);
172     if (out == NULL)
173         goto end;
174
175     if (!noout)
176         NETSCAPE_SPKI_print(out, spki);
177     pkey = NETSCAPE_SPKI_get_pubkey(spki);
178     if (verify) {
179         i = NETSCAPE_SPKI_verify(spki, pkey);
180         if (i > 0) {
181             BIO_printf(bio_err, "Signature OK\n");
182         } else {
183             BIO_printf(bio_err, "Signature Failure\n");
184             ERR_print_errors(bio_err);
185             goto end;
186         }
187     }
188     if (pubkey)
189         PEM_write_bio_PUBKEY(out, pkey);
190
191     ret = 0;
192
193  end:
194     NCONF_free(conf);
195     NETSCAPE_SPKI_free(spki);
196     BIO_free_all(out);
197     EVP_PKEY_free(pkey);
198     release_engine(e);
199     OPENSSL_free(passin);
200     return (ret);
201 }