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