test: add import and export key management hooks for the TLS provider.
[openssl.git] / apps / crl.c
1 /*
2  * Copyright 1995-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 "apps.h"
14 #include "progs.h"
15 #include <openssl/bio.h>
16 #include <openssl/err.h>
17 #include <openssl/x509.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/pem.h>
20
21 typedef enum OPTION_choice {
22     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
23     OPT_INFORM, OPT_IN, OPT_OUTFORM, OPT_OUT, OPT_KEYFORM, OPT_KEY,
24     OPT_ISSUER, OPT_LASTUPDATE, OPT_NEXTUPDATE, OPT_FINGERPRINT,
25     OPT_CRLNUMBER, OPT_BADSIG, OPT_GENDELTA, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
26     OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_VERIFY, OPT_TEXT, OPT_HASH,
27     OPT_HASH_OLD, OPT_NOOUT, OPT_NAMEOPT, OPT_MD, OPT_PROV_ENUM
28 } OPTION_CHOICE;
29
30 const OPTIONS crl_options[] = {
31     OPT_SECTION("General"),
32     {"help", OPT_HELP, '-', "Display this summary"},
33     {"verify", OPT_VERIFY, '-', "Verify CRL signature"},
34
35     OPT_SECTION("Input"),
36     {"in", OPT_IN, '<', "Input file - default stdin"},
37     {"inform", OPT_INFORM, 'F', "CRL input format (DER or PEM); has no effect"},
38     {"key", OPT_KEY, '<', "CRL signing Private key to use"},
39     {"keyform", OPT_KEYFORM, 'F', "Private key file format (DER/PEM/P12); has no effect"},
40
41     OPT_SECTION("Output"),
42     {"out", OPT_OUT, '>', "output file - default stdout"},
43     {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
44     {"text", OPT_TEXT, '-', "Print out a text format version"},
45     {"hash", OPT_HASH, '-', "Print hash value"},
46 #ifndef OPENSSL_NO_MD5
47     {"hash_old", OPT_HASH_OLD, '-', "Print old-style (MD5) hash value"},
48 #endif
49     {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
50     {"", OPT_MD, '-', "Any supported digest"},
51
52     OPT_SECTION("CRL"),
53     {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
54     {"lastupdate", OPT_LASTUPDATE, '-', "Set lastUpdate field"},
55     {"nextupdate", OPT_NEXTUPDATE, '-', "Set nextUpdate field"},
56     {"noout", OPT_NOOUT, '-', "No CRL output"},
57     {"fingerprint", OPT_FINGERPRINT, '-', "Print the crl fingerprint"},
58     {"crlnumber", OPT_CRLNUMBER, '-', "Print CRL number"},
59     {"badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" },
60     {"gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one"},
61
62     OPT_SECTION("Certificate"),
63     {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"},
64     {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"},
65     {"CAstore", OPT_CASTORE, ':', "Verify CRL using certificates in store URI"},
66     {"no-CAfile", OPT_NOCAFILE, '-',
67      "Do not load the default certificates file"},
68     {"no-CApath", OPT_NOCAPATH, '-',
69      "Do not load certificates from the default certificates directory"},
70     {"no-CAstore", OPT_NOCASTORE, '-',
71      "Do not load certificates from the default certificates store"},
72     OPT_PROV_OPTIONS,
73     {NULL}
74 };
75
76 int crl_main(int argc, char **argv)
77 {
78     X509_CRL *x = NULL;
79     BIO *out = NULL;
80     X509_STORE *store = NULL;
81     X509_STORE_CTX *ctx = NULL;
82     X509_LOOKUP *lookup = NULL;
83     X509_OBJECT *xobj = NULL;
84     EVP_PKEY *pkey;
85     const EVP_MD *digest = EVP_sha1();
86     char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
87     char *digestname = NULL;
88     const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog;
89     OPTION_CHOICE o;
90     int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
91     int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
92     int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
93     int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0, noCAstore = 0;
94     int i;
95 #ifndef OPENSSL_NO_MD5
96     int hash_old = 0;
97 #endif
98
99     prog = opt_init(argc, argv, crl_options);
100     while ((o = opt_next()) != OPT_EOF) {
101         switch (o) {
102         case OPT_EOF:
103         case OPT_ERR:
104  opthelp:
105             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
106             goto end;
107         case OPT_HELP:
108             opt_help(crl_options);
109             ret = 0;
110             goto end;
111         case OPT_INFORM:
112             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
113                 goto opthelp;
114             break;
115         case OPT_IN:
116             infile = opt_arg();
117             break;
118         case OPT_OUTFORM:
119             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
120                 goto opthelp;
121             break;
122         case OPT_OUT:
123             outfile = opt_arg();
124             break;
125         case OPT_KEYFORM:
126             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
127                 goto opthelp;
128             break;
129         case OPT_KEY:
130             keyfile = opt_arg();
131             break;
132         case OPT_GENDELTA:
133             crldiff = opt_arg();
134             break;
135         case OPT_CAPATH:
136             CApath = opt_arg();
137             do_ver = 1;
138             break;
139         case OPT_CAFILE:
140             CAfile = opt_arg();
141             do_ver = 1;
142             break;
143         case OPT_CASTORE:
144             CAstore = opt_arg();
145             do_ver = 1;
146             break;
147         case OPT_NOCAPATH:
148             noCApath =  1;
149             break;
150         case OPT_NOCAFILE:
151             noCAfile =  1;
152             break;
153         case OPT_NOCASTORE:
154             noCAstore =  1;
155             break;
156         case OPT_HASH_OLD:
157 #ifndef OPENSSL_NO_MD5
158             hash_old = ++num;
159 #endif
160             break;
161         case OPT_VERIFY:
162             do_ver = 1;
163             break;
164         case OPT_TEXT:
165             text = 1;
166             break;
167         case OPT_HASH:
168             hash = ++num;
169             break;
170         case OPT_ISSUER:
171             issuer = ++num;
172             break;
173         case OPT_LASTUPDATE:
174             lastupdate = ++num;
175             break;
176         case OPT_NEXTUPDATE:
177             nextupdate = ++num;
178             break;
179         case OPT_NOOUT:
180             noout = ++num;
181             break;
182         case OPT_FINGERPRINT:
183             fingerprint = ++num;
184             break;
185         case OPT_CRLNUMBER:
186             crlnumber = ++num;
187             break;
188         case OPT_BADSIG:
189             badsig = 1;
190             break;
191         case OPT_NAMEOPT:
192             if (!set_nameopt(opt_arg()))
193                 goto opthelp;
194             break;
195         case OPT_MD:
196             digestname = opt_unknown();
197             break;
198         case OPT_PROV_CASES:
199             if (!opt_provider(o))
200                 goto end;
201             break;
202         }
203     }
204
205     /* No remaining args. */
206     argc = opt_num_rest();
207     if (argc != 0)
208         goto opthelp;
209
210     if (digestname != NULL) {
211         if (!opt_md(digestname, &digest))
212             goto opthelp;
213     }
214     x = load_crl(infile, "CRL");
215     if (x == NULL)
216         goto end;
217
218     if (do_ver) {
219         if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
220                                   CAstore, noCAstore)) == NULL)
221             goto end;
222         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
223         if (lookup == NULL)
224             goto end;
225         ctx = X509_STORE_CTX_new();
226         if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
227             BIO_printf(bio_err, "Error initialising X509 store\n");
228             goto end;
229         }
230
231         xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
232                                                  X509_CRL_get_issuer(x));
233         if (xobj == NULL) {
234             BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
235             goto end;
236         }
237         pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
238         X509_OBJECT_free(xobj);
239         if (pkey == NULL) {
240             BIO_printf(bio_err, "Error getting CRL issuer public key\n");
241             goto end;
242         }
243         i = X509_CRL_verify(x, pkey);
244         EVP_PKEY_free(pkey);
245         if (i < 0)
246             goto end;
247         if (i == 0)
248             BIO_printf(bio_err, "verify failure\n");
249         else
250             BIO_printf(bio_err, "verify OK\n");
251     }
252
253     if (crldiff) {
254         X509_CRL *newcrl, *delta;
255         if (!keyfile) {
256             BIO_puts(bio_err, "Missing CRL signing key\n");
257             goto end;
258         }
259         newcrl = load_crl(crldiff, "other CRL");
260         if (!newcrl)
261             goto end;
262         pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
263         if (pkey == NULL) {
264             X509_CRL_free(newcrl);
265             goto end;
266         }
267         delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
268         X509_CRL_free(newcrl);
269         EVP_PKEY_free(pkey);
270         if (delta) {
271             X509_CRL_free(x);
272             x = delta;
273         } else {
274             BIO_puts(bio_err, "Error creating delta CRL\n");
275             goto end;
276         }
277     }
278
279     if (badsig) {
280         const ASN1_BIT_STRING *sig;
281
282         X509_CRL_get0_signature(x, &sig, NULL);
283         corrupt_signature(sig);
284     }
285
286     if (num) {
287         for (i = 1; i <= num; i++) {
288             if (issuer == i) {
289                 print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
290                            get_nameopt());
291             }
292             if (crlnumber == i) {
293                 ASN1_INTEGER *crlnum;
294
295                 crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
296                 BIO_printf(bio_out, "crlNumber=");
297                 if (crlnum) {
298                     BIO_puts(bio_out, "0x");
299                     i2a_ASN1_INTEGER(bio_out, crlnum);
300                     ASN1_INTEGER_free(crlnum);
301                 } else {
302                     BIO_puts(bio_out, "<NONE>");
303                 }
304                 BIO_printf(bio_out, "\n");
305             }
306             if (hash == i) {
307                 int ok;
308                 unsigned long hash_value =
309                     X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(),
310                                       app_get0_propq(), &ok);
311
312                 BIO_printf(bio_out, "issuer name hash=");
313                 if (ok)
314                     BIO_printf(bio_out, "%08lx\n", hash_value);
315                 else
316                     BIO_puts(bio_out, "<ERROR>");
317             }
318 #ifndef OPENSSL_NO_MD5
319             if (hash_old == i) {
320                 BIO_printf(bio_out, "issuer name old hash=");
321                 BIO_printf(bio_out, "%08lx\n",
322                            X509_NAME_hash_old(X509_CRL_get_issuer(x)));
323             }
324 #endif
325             if (lastupdate == i) {
326                 BIO_printf(bio_out, "lastUpdate=");
327                 ASN1_TIME_print(bio_out, X509_CRL_get0_lastUpdate(x));
328                 BIO_printf(bio_out, "\n");
329             }
330             if (nextupdate == i) {
331                 BIO_printf(bio_out, "nextUpdate=");
332                 if (X509_CRL_get0_nextUpdate(x))
333                     ASN1_TIME_print(bio_out, X509_CRL_get0_nextUpdate(x));
334                 else
335                     BIO_printf(bio_out, "NONE");
336                 BIO_printf(bio_out, "\n");
337             }
338             if (fingerprint == i) {
339                 int j;
340                 unsigned int n;
341                 unsigned char md[EVP_MAX_MD_SIZE];
342
343                 if (!X509_CRL_digest(x, digest, md, &n)) {
344                     BIO_printf(bio_err, "out of memory\n");
345                     goto end;
346                 }
347                 BIO_printf(bio_out, "%s Fingerprint=",
348                            OBJ_nid2sn(EVP_MD_type(digest)));
349                 for (j = 0; j < (int)n; j++) {
350                     BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
351                                ? '\n' : ':');
352                 }
353             }
354         }
355     }
356     out = bio_open_default(outfile, 'w', outformat);
357     if (out == NULL)
358         goto end;
359
360     if (text)
361         X509_CRL_print_ex(out, x, get_nameopt());
362
363     if (noout) {
364         ret = 0;
365         goto end;
366     }
367
368     if (outformat == FORMAT_ASN1)
369         i = (int)i2d_X509_CRL_bio(out, x);
370     else
371         i = PEM_write_bio_X509_CRL(out, x);
372     if (!i) {
373         BIO_printf(bio_err, "unable to write CRL\n");
374         goto end;
375     }
376     ret = 0;
377
378  end:
379     if (ret != 0)
380         ERR_print_errors(bio_err);
381     BIO_free_all(out);
382     X509_CRL_free(x);
383     X509_STORE_CTX_free(ctx);
384     X509_STORE_free(store);
385     return ret;
386 }