hkdf: when HMAC key is all zeros, still set a valid key length
[openssl.git] / apps / spkac.c
1 /*
2  * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 "progs.h"
16 #include <openssl/bio.h>
17 #include <openssl/conf.h>
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
21 #include <openssl/pem.h>
22
23 typedef enum OPTION_choice {
24     OPT_COMMON,
25     OPT_NOOUT, OPT_PUBKEY, OPT_VERIFY, OPT_IN, OPT_OUT,
26     OPT_ENGINE, OPT_KEY, OPT_CHALLENGE, OPT_PASSIN, OPT_SPKAC,
27     OPT_SPKSECT, OPT_KEYFORM, OPT_DIGEST,
28     OPT_PROV_ENUM
29 } OPTION_CHOICE;
30
31 const OPTIONS spkac_options[] = {
32     OPT_SECTION("General"),
33     {"help", OPT_HELP, '-', "Display this summary"},
34     {"spksect", OPT_SPKSECT, 's',
35      "Specify the name of an SPKAC-dedicated section of configuration"},
36 #ifndef OPENSSL_NO_ENGINE
37     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
38 #endif
39
40     OPT_SECTION("Input"),
41     {"in", OPT_IN, '<', "Input file"},
42     {"key", OPT_KEY, '<', "Create SPKAC using private key"},
43     {"keyform", OPT_KEYFORM, 'f', "Private key file format (ENGINE, other values ignored)"},
44     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
45     {"challenge", OPT_CHALLENGE, 's', "Challenge string"},
46     {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"},
47
48     OPT_SECTION("Output"),
49     {"digest", OPT_DIGEST, 's', "Sign new SPKAC with the specified digest (default: MD5)" },
50     {"out", OPT_OUT, '>', "Output file"},
51     {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
52     {"pubkey", OPT_PUBKEY, '-', "Output public key"},
53     {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
54
55     OPT_PROV_OPTIONS,
56     {NULL}
57 };
58
59 int spkac_main(int argc, char **argv)
60 {
61     BIO *out = NULL;
62     CONF *conf = NULL;
63     ENGINE *e = NULL;
64     EVP_PKEY *pkey = NULL;
65     NETSCAPE_SPKI *spki = NULL;
66     char *challenge = NULL, *keyfile = NULL;
67     char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
68     char *spkstr = NULL, *prog;
69     const char *spkac = "SPKAC", *spksect = "default";
70     const char *digest = "MD5";
71     EVP_MD *md = NULL;
72     int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
73     int keyformat = FORMAT_UNDEF;
74     OPTION_CHOICE o;
75
76     prog = opt_init(argc, argv, spkac_options);
77     while ((o = opt_next()) != OPT_EOF) {
78         switch (o) {
79         case OPT_EOF:
80         case OPT_ERR:
81  opthelp:
82             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
83             goto end;
84         case OPT_HELP:
85             opt_help(spkac_options);
86             ret = 0;
87             goto end;
88         case OPT_IN:
89             infile = opt_arg();
90             break;
91         case OPT_OUT:
92             outfile = opt_arg();
93             break;
94         case OPT_NOOUT:
95             noout = 1;
96             break;
97         case OPT_PUBKEY:
98             pubkey = 1;
99             break;
100         case OPT_VERIFY:
101             verify = 1;
102             break;
103         case OPT_PASSIN:
104             passinarg = opt_arg();
105             break;
106         case OPT_KEY:
107             keyfile = opt_arg();
108             break;
109         case OPT_KEYFORM:
110             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
111                 goto opthelp;
112             break;
113         case OPT_CHALLENGE:
114             challenge = opt_arg();
115             break;
116         case OPT_SPKAC:
117             spkac = opt_arg();
118             break;
119         case OPT_SPKSECT:
120             spksect = opt_arg();
121             break;
122         case OPT_DIGEST:
123             digest = opt_arg();
124             break;
125         case OPT_ENGINE:
126             e = setup_engine(opt_arg(), 0);
127             break;
128         case OPT_PROV_CASES:
129             if (!opt_provider(o))
130                 goto end;
131             break;
132         }
133     }
134
135     /* No extra arguments. */
136     if (!opt_check_rest_arg(NULL))
137         goto opthelp;
138
139     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
140         BIO_printf(bio_err, "Error getting password\n");
141         goto end;
142     }
143
144     if (keyfile != NULL) {
145         if (!opt_md(digest, &md))
146             goto end;
147
148         pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
149                         keyformat, 1, passin, e, "private key");
150         if (pkey == NULL)
151             goto end;
152         spki = NETSCAPE_SPKI_new();
153         if (spki == NULL)
154             goto end;
155         if (challenge != NULL)
156             ASN1_STRING_set(spki->spkac->challenge,
157                             challenge, (int)strlen(challenge));
158         if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
159             BIO_printf(bio_err, "Error setting public key\n");
160             goto end;
161         }
162         i = NETSCAPE_SPKI_sign(spki, pkey, md);
163         if (i <= 0) {
164             BIO_printf(bio_err, "Error signing SPKAC\n");
165             goto end;
166         }
167         spkstr = NETSCAPE_SPKI_b64_encode(spki);
168         if (spkstr == NULL)
169             goto end;
170
171         out = bio_open_default(outfile, 'w', FORMAT_TEXT);
172         if (out == NULL) {
173             OPENSSL_free(spkstr);
174             goto end;
175         }
176         BIO_printf(out, "SPKAC=%s\n", spkstr);
177         OPENSSL_free(spkstr);
178         ret = 0;
179         goto end;
180     }
181
182     if ((conf = app_load_config(infile)) == NULL)
183         goto end;
184
185     spkstr = NCONF_get_string(conf, spksect, spkac);
186
187     if (spkstr == NULL) {
188         BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
189         ERR_print_errors(bio_err);
190         goto end;
191     }
192
193     spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
194
195     if (spki == NULL) {
196         BIO_printf(bio_err, "Error loading SPKAC\n");
197         ERR_print_errors(bio_err);
198         goto end;
199     }
200
201     out = bio_open_default(outfile, 'w', FORMAT_TEXT);
202     if (out == NULL)
203         goto end;
204
205     if (!noout)
206         NETSCAPE_SPKI_print(out, spki);
207     pkey = NETSCAPE_SPKI_get_pubkey(spki);
208     if (verify) {
209         i = NETSCAPE_SPKI_verify(spki, pkey);
210         if (i > 0) {
211             BIO_printf(bio_err, "Signature OK\n");
212         } else {
213             BIO_printf(bio_err, "Signature Failure\n");
214             ERR_print_errors(bio_err);
215             goto end;
216         }
217     }
218     if (pubkey)
219         PEM_write_bio_PUBKEY(out, pkey);
220
221     ret = 0;
222
223  end:
224     EVP_MD_free(md);
225     NCONF_free(conf);
226     NETSCAPE_SPKI_free(spki);
227     BIO_free_all(out);
228     EVP_PKEY_free(pkey);
229     release_engine(e);
230     OPENSSL_free(passin);
231     return ret;
232 }