Add X509_getm_notBefore, X509_getm_notAfter
[openssl.git] / apps / spkac.c
1 /*
2  * Copyright 1999-2016 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/lhash.h>
20 #include <openssl/x509.h>
21 #include <openssl/pem.h>
22
23 typedef enum OPTION_choice {
24     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
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
28 } OPTION_CHOICE;
29
30 OPTIONS spkac_options[] = {
31     {"help", OPT_HELP, '-', "Display this summary"},
32     {"in", OPT_IN, '<', "Input file"},
33     {"out", OPT_OUT, '>', "Output file"},
34     {"key", OPT_KEY, '<', "Create SPKAC using private key"},
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 #ifndef OPENSSL_NO_ENGINE
43     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
44 #endif
45     {NULL}
46 };
47
48 int spkac_main(int argc, char **argv)
49 {
50     BIO *out = NULL;
51     CONF *conf = NULL;
52     ENGINE *e = NULL;
53     EVP_PKEY *pkey = NULL;
54     NETSCAPE_SPKI *spki = NULL;
55     char *challenge = NULL, *keyfile = NULL;
56     char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
57     char *spkstr = NULL, *prog;
58     const char *spkac = "SPKAC", *spksect = "default";
59     int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
60     OPTION_CHOICE o;
61
62     prog = opt_init(argc, argv, spkac_options);
63     while ((o = opt_next()) != OPT_EOF) {
64         switch (o) {
65         case OPT_EOF:
66         case OPT_ERR:
67  opthelp:
68             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
69             goto end;
70         case OPT_HELP:
71             opt_help(spkac_options);
72             ret = 0;
73             goto end;
74         case OPT_IN:
75             infile = opt_arg();
76             break;
77         case OPT_OUT:
78             outfile = opt_arg();
79             break;
80         case OPT_NOOUT:
81             noout = 1;
82             break;
83         case OPT_PUBKEY:
84             pubkey = 1;
85             break;
86         case OPT_VERIFY:
87             verify = 1;
88             break;
89         case OPT_PASSIN:
90             passinarg = opt_arg();
91             break;
92         case OPT_KEY:
93             keyfile = opt_arg();
94             break;
95         case OPT_CHALLENGE:
96             challenge = opt_arg();
97             break;
98         case OPT_SPKAC:
99             spkac = opt_arg();
100             break;
101         case OPT_SPKSECT:
102             spksect = opt_arg();
103             break;
104         case OPT_ENGINE:
105             e = setup_engine(opt_arg(), 0);
106             break;
107         }
108     }
109     argc = opt_num_rest();
110     if (argc != 0)
111         goto opthelp;
112
113     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
114         BIO_printf(bio_err, "Error getting password\n");
115         goto end;
116     }
117
118     if (keyfile) {
119         pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
120                         FORMAT_PEM, 1, passin, e, "private key");
121         if (!pkey) {
122             goto end;
123         }
124         spki = NETSCAPE_SPKI_new();
125         if (challenge)
126             ASN1_STRING_set(spki->spkac->challenge,
127                             challenge, (int)strlen(challenge));
128         NETSCAPE_SPKI_set_pubkey(spki, pkey);
129         NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
130         spkstr = NETSCAPE_SPKI_b64_encode(spki);
131
132         out = bio_open_default(outfile, 'w', FORMAT_TEXT);
133         if (out == NULL)
134             goto end;
135         BIO_printf(out, "SPKAC=%s\n", spkstr);
136         OPENSSL_free(spkstr);
137         ret = 0;
138         goto end;
139     }
140
141     if ((conf = app_load_config(infile)) == NULL)
142         goto end;
143
144     spkstr = NCONF_get_string(conf, spksect, spkac);
145
146     if (spkstr == NULL) {
147         BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
148         ERR_print_errors(bio_err);
149         goto end;
150     }
151
152     spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
153
154     if (!spki) {
155         BIO_printf(bio_err, "Error loading SPKAC\n");
156         ERR_print_errors(bio_err);
157         goto end;
158     }
159
160     out = bio_open_default(outfile, 'w', FORMAT_TEXT);
161     if (out == NULL)
162         goto end;
163
164     if (!noout)
165         NETSCAPE_SPKI_print(out, spki);
166     pkey = NETSCAPE_SPKI_get_pubkey(spki);
167     if (verify) {
168         i = NETSCAPE_SPKI_verify(spki, pkey);
169         if (i > 0)
170             BIO_printf(bio_err, "Signature OK\n");
171         else {
172             BIO_printf(bio_err, "Signature Failure\n");
173             ERR_print_errors(bio_err);
174             goto end;
175         }
176     }
177     if (pubkey)
178         PEM_write_bio_PUBKEY(out, pkey);
179
180     ret = 0;
181
182  end:
183     NCONF_free(conf);
184     NETSCAPE_SPKI_free(spki);
185     BIO_free_all(out);
186     EVP_PKEY_free(pkey);
187     OPENSSL_free(passin);
188     return (ret);
189 }