xref: /netbsd-src/crypto/external/bsd/openssl/dist/apps/crl.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1c7da899bSchristos /*
2*b0d17251Schristos  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos  *
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  */
9a89c9211Schristos 
10a89c9211Schristos #include <stdio.h>
11a89c9211Schristos #include <stdlib.h>
12a89c9211Schristos #include <string.h>
13a89c9211Schristos #include "apps.h"
1413d40330Schristos #include "progs.h"
15a89c9211Schristos #include <openssl/bio.h>
16a89c9211Schristos #include <openssl/err.h>
17a89c9211Schristos #include <openssl/x509.h>
18a89c9211Schristos #include <openssl/x509v3.h>
19a89c9211Schristos #include <openssl/pem.h>
20a89c9211Schristos 
21c7da899bSchristos typedef enum OPTION_choice {
22*b0d17251Schristos     OPT_COMMON,
23c7da899bSchristos     OPT_INFORM, OPT_IN, OPT_OUTFORM, OPT_OUT, OPT_KEYFORM, OPT_KEY,
24c7da899bSchristos     OPT_ISSUER, OPT_LASTUPDATE, OPT_NEXTUPDATE, OPT_FINGERPRINT,
25*b0d17251Schristos     OPT_CRLNUMBER, OPT_BADSIG, OPT_GENDELTA, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
26*b0d17251Schristos     OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE, OPT_VERIFY, OPT_DATEOPT, OPT_TEXT, OPT_HASH,
27*b0d17251Schristos     OPT_HASH_OLD, OPT_NOOUT, OPT_NAMEOPT, OPT_MD, OPT_PROV_ENUM
28c7da899bSchristos } OPTION_CHOICE;
29a89c9211Schristos 
3013d40330Schristos const OPTIONS crl_options[] = {
31*b0d17251Schristos     OPT_SECTION("General"),
32c7da899bSchristos     {"help", OPT_HELP, '-', "Display this summary"},
33*b0d17251Schristos     {"verify", OPT_VERIFY, '-', "Verify CRL signature"},
34*b0d17251Schristos 
35*b0d17251Schristos     OPT_SECTION("Input"),
36c7da899bSchristos     {"in", OPT_IN, '<', "Input file - default stdin"},
37*b0d17251Schristos     {"inform", OPT_INFORM, 'F', "CRL input format (DER or PEM); has no effect"},
38c7da899bSchristos     {"key", OPT_KEY, '<', "CRL signing Private key to use"},
39*b0d17251Schristos     {"keyform", OPT_KEYFORM, 'F', "Private key file format (DER/PEM/P12); has no effect"},
40*b0d17251Schristos 
41*b0d17251Schristos     OPT_SECTION("Output"),
42*b0d17251Schristos     {"out", OPT_OUT, '>', "output file - default stdout"},
43*b0d17251Schristos     {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
44*b0d17251Schristos     {"dateopt", OPT_DATEOPT, 's', "Datetime format used for printing. (rfc_822/iso_8601). Default is rfc_822."},
45*b0d17251Schristos     {"text", OPT_TEXT, '-', "Print out a text format version"},
46*b0d17251Schristos     {"hash", OPT_HASH, '-', "Print hash value"},
47*b0d17251Schristos #ifndef OPENSSL_NO_MD5
48*b0d17251Schristos     {"hash_old", OPT_HASH_OLD, '-', "Print old-style (MD5) hash value"},
49*b0d17251Schristos #endif
50*b0d17251Schristos     {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
51*b0d17251Schristos     {"", OPT_MD, '-', "Any supported digest"},
52*b0d17251Schristos 
53*b0d17251Schristos     OPT_SECTION("CRL"),
54c7da899bSchristos     {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
55c7da899bSchristos     {"lastupdate", OPT_LASTUPDATE, '-', "Set lastUpdate field"},
56c7da899bSchristos     {"nextupdate", OPT_NEXTUPDATE, '-', "Set nextUpdate field"},
57c7da899bSchristos     {"noout", OPT_NOOUT, '-', "No CRL output"},
58c7da899bSchristos     {"fingerprint", OPT_FINGERPRINT, '-', "Print the crl fingerprint"},
59c7da899bSchristos     {"crlnumber", OPT_CRLNUMBER, '-', "Print CRL number"},
60c7da899bSchristos     {"badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" },
61c7da899bSchristos     {"gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one"},
62*b0d17251Schristos 
63*b0d17251Schristos     OPT_SECTION("Certificate"),
64c7da899bSchristos     {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"},
65c7da899bSchristos     {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"},
66*b0d17251Schristos     {"CAstore", OPT_CASTORE, ':', "Verify CRL using certificates in store URI"},
67c7da899bSchristos     {"no-CAfile", OPT_NOCAFILE, '-',
68c7da899bSchristos      "Do not load the default certificates file"},
69c7da899bSchristos     {"no-CApath", OPT_NOCAPATH, '-',
70c7da899bSchristos      "Do not load certificates from the default certificates directory"},
71*b0d17251Schristos     {"no-CAstore", OPT_NOCASTORE, '-',
72*b0d17251Schristos      "Do not load certificates from the default certificates store"},
73*b0d17251Schristos     OPT_PROV_OPTIONS,
74c7da899bSchristos     {NULL}
75a89c9211Schristos };
76a89c9211Schristos 
crl_main(int argc,char ** argv)77c7da899bSchristos int crl_main(int argc, char **argv)
78a89c9211Schristos {
79a89c9211Schristos     X509_CRL *x = NULL;
80a89c9211Schristos     BIO *out = NULL;
81c7da899bSchristos     X509_STORE *store = NULL;
82c7da899bSchristos     X509_STORE_CTX *ctx = NULL;
83c7da899bSchristos     X509_LOOKUP *lookup = NULL;
84c7da899bSchristos     X509_OBJECT *xobj = NULL;
85c7da899bSchristos     EVP_PKEY *pkey;
86*b0d17251Schristos     EVP_MD *digest = (EVP_MD *)EVP_sha1();
87218f7bfcSspz     char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
88*b0d17251Schristos     char *digestname = NULL;
89*b0d17251Schristos     const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog;
90c7da899bSchristos     OPTION_CHOICE o;
91c7da899bSchristos     int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
92*b0d17251Schristos     int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, keyformat = FORMAT_UNDEF;
93c7da899bSchristos     int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
94*b0d17251Schristos     int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0, noCAstore = 0;
95*b0d17251Schristos     unsigned long dateopt = ASN1_DTFLGS_RFC822;
96c7da899bSchristos     int i;
97057199e6Schristos #ifndef OPENSSL_NO_MD5
98057199e6Schristos     int hash_old = 0;
99057199e6Schristos #endif
100a89c9211Schristos 
101c7da899bSchristos     prog = opt_init(argc, argv, crl_options);
102c7da899bSchristos     while ((o = opt_next()) != OPT_EOF) {
103c7da899bSchristos         switch (o) {
104c7da899bSchristos         case OPT_EOF:
105c7da899bSchristos         case OPT_ERR:
106c7da899bSchristos  opthelp:
107c7da899bSchristos             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
108a89c9211Schristos             goto end;
109c7da899bSchristos         case OPT_HELP:
110c7da899bSchristos             opt_help(crl_options);
111c7da899bSchristos             ret = 0;
112635165faSspz             goto end;
113c7da899bSchristos         case OPT_INFORM:
114c7da899bSchristos             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
115c7da899bSchristos                 goto opthelp;
116c7da899bSchristos             break;
117c7da899bSchristos         case OPT_IN:
118c7da899bSchristos             infile = opt_arg();
119c7da899bSchristos             break;
120c7da899bSchristos         case OPT_OUTFORM:
121c7da899bSchristos             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
122c7da899bSchristos                 goto opthelp;
123c7da899bSchristos             break;
124c7da899bSchristos         case OPT_OUT:
125c7da899bSchristos             outfile = opt_arg();
126c7da899bSchristos             break;
127c7da899bSchristos         case OPT_KEYFORM:
128*b0d17251Schristos             if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
129c7da899bSchristos                 goto opthelp;
130c7da899bSchristos             break;
131c7da899bSchristos         case OPT_KEY:
132c7da899bSchristos             keyfile = opt_arg();
133c7da899bSchristos             break;
134c7da899bSchristos         case OPT_GENDELTA:
135c7da899bSchristos             crldiff = opt_arg();
136c7da899bSchristos             break;
137c7da899bSchristos         case OPT_CAPATH:
138c7da899bSchristos             CApath = opt_arg();
139a89c9211Schristos             do_ver = 1;
140c7da899bSchristos             break;
141c7da899bSchristos         case OPT_CAFILE:
142c7da899bSchristos             CAfile = opt_arg();
143a89c9211Schristos             do_ver = 1;
144c7da899bSchristos             break;
145*b0d17251Schristos         case OPT_CASTORE:
146*b0d17251Schristos             CAstore = opt_arg();
147*b0d17251Schristos             do_ver = 1;
148*b0d17251Schristos             break;
149c7da899bSchristos         case OPT_NOCAPATH:
150c7da899bSchristos             noCApath =  1;
151c7da899bSchristos             break;
152c7da899bSchristos         case OPT_NOCAFILE:
153c7da899bSchristos             noCAfile =  1;
154c7da899bSchristos             break;
155*b0d17251Schristos         case OPT_NOCASTORE:
156*b0d17251Schristos             noCAstore =  1;
157*b0d17251Schristos             break;
158c7da899bSchristos         case OPT_HASH_OLD:
159057199e6Schristos #ifndef OPENSSL_NO_MD5
160057199e6Schristos             hash_old = ++num;
161057199e6Schristos #endif
162a89c9211Schristos             break;
163c7da899bSchristos         case OPT_VERIFY:
164c7da899bSchristos             do_ver = 1;
165c7da899bSchristos             break;
166*b0d17251Schristos         case OPT_DATEOPT:
167*b0d17251Schristos             if (!set_dateopt(&dateopt, opt_arg()))
168*b0d17251Schristos                 goto opthelp;
169*b0d17251Schristos             break;
170c7da899bSchristos         case OPT_TEXT:
171c7da899bSchristos             text = 1;
172c7da899bSchristos             break;
173c7da899bSchristos         case OPT_HASH:
174c7da899bSchristos             hash = ++num;
175c7da899bSchristos             break;
176c7da899bSchristos         case OPT_ISSUER:
177c7da899bSchristos             issuer = ++num;
178c7da899bSchristos             break;
179c7da899bSchristos         case OPT_LASTUPDATE:
180c7da899bSchristos             lastupdate = ++num;
181c7da899bSchristos             break;
182c7da899bSchristos         case OPT_NEXTUPDATE:
183c7da899bSchristos             nextupdate = ++num;
184c7da899bSchristos             break;
185c7da899bSchristos         case OPT_NOOUT:
186*b0d17251Schristos             noout = 1;
187c7da899bSchristos             break;
188c7da899bSchristos         case OPT_FINGERPRINT:
189c7da899bSchristos             fingerprint = ++num;
190c7da899bSchristos             break;
191c7da899bSchristos         case OPT_CRLNUMBER:
192c7da899bSchristos             crlnumber = ++num;
193c7da899bSchristos             break;
194c7da899bSchristos         case OPT_BADSIG:
195c7da899bSchristos             badsig = 1;
196c7da899bSchristos             break;
197c7da899bSchristos         case OPT_NAMEOPT:
19813d40330Schristos             if (!set_nameopt(opt_arg()))
199c7da899bSchristos                 goto opthelp;
200c7da899bSchristos             break;
201c7da899bSchristos         case OPT_MD:
202*b0d17251Schristos             digestname = opt_unknown();
203*b0d17251Schristos             break;
204*b0d17251Schristos         case OPT_PROV_CASES:
205*b0d17251Schristos             if (!opt_provider(o))
206*b0d17251Schristos                 goto end;
207*b0d17251Schristos             break;
208a89c9211Schristos         }
209a89c9211Schristos     }
210*b0d17251Schristos 
211*b0d17251Schristos     /* No remaining args. */
212c7da899bSchristos     argc = opt_num_rest();
213c7da899bSchristos     if (argc != 0)
214c7da899bSchristos         goto opthelp;
215a89c9211Schristos 
216*b0d17251Schristos     if (digestname != NULL) {
217*b0d17251Schristos         if (!opt_md(digestname, &digest))
218*b0d17251Schristos             goto opthelp;
219*b0d17251Schristos     }
220*b0d17251Schristos     x = load_crl(infile, informat, 1, "CRL");
221c7da899bSchristos     if (x == NULL)
222635165faSspz         goto end;
223a89c9211Schristos 
224a89c9211Schristos     if (do_ver) {
225*b0d17251Schristos         if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
226*b0d17251Schristos                                   CAstore, noCAstore)) == NULL)
227c7da899bSchristos             goto end;
228a89c9211Schristos         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
229635165faSspz         if (lookup == NULL)
230635165faSspz             goto end;
231c7da899bSchristos         ctx = X509_STORE_CTX_new();
232c7da899bSchristos         if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
233635165faSspz             BIO_printf(bio_err, "Error initialising X509 store\n");
234a89c9211Schristos             goto end;
235a89c9211Schristos         }
236a89c9211Schristos 
237c7da899bSchristos         xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
238c7da899bSchristos                                                  X509_CRL_get_issuer(x));
239c7da899bSchristos         if (xobj == NULL) {
240635165faSspz             BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
241a89c9211Schristos             goto end;
242a89c9211Schristos         }
243c7da899bSchristos         pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
244c7da899bSchristos         X509_OBJECT_free(xobj);
245*b0d17251Schristos         if (pkey == NULL) {
246635165faSspz             BIO_printf(bio_err, "Error getting CRL issuer public key\n");
247a89c9211Schristos             goto end;
248a89c9211Schristos         }
249a89c9211Schristos         i = X509_CRL_verify(x, pkey);
250a89c9211Schristos         EVP_PKEY_free(pkey);
251635165faSspz         if (i < 0)
252635165faSspz             goto end;
253635165faSspz         if (i == 0)
254635165faSspz             BIO_printf(bio_err, "verify failure\n");
255635165faSspz         else
256635165faSspz             BIO_printf(bio_err, "verify OK\n");
257a89c9211Schristos     }
258a89c9211Schristos 
259*b0d17251Schristos     if (crldiff != NULL) {
260218f7bfcSspz         X509_CRL *newcrl, *delta;
261218f7bfcSspz         if (!keyfile) {
262218f7bfcSspz             BIO_puts(bio_err, "Missing CRL signing key\n");
263218f7bfcSspz             goto end;
264218f7bfcSspz         }
265*b0d17251Schristos         newcrl = load_crl(crldiff, informat, 0, "other CRL");
266218f7bfcSspz         if (!newcrl)
267218f7bfcSspz             goto end;
268c7da899bSchristos         pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
269*b0d17251Schristos         if (pkey == NULL) {
270218f7bfcSspz             X509_CRL_free(newcrl);
271218f7bfcSspz             goto end;
272218f7bfcSspz         }
273218f7bfcSspz         delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
274218f7bfcSspz         X509_CRL_free(newcrl);
275218f7bfcSspz         EVP_PKEY_free(pkey);
276218f7bfcSspz         if (delta) {
277218f7bfcSspz             X509_CRL_free(x);
278218f7bfcSspz             x = delta;
279218f7bfcSspz         } else {
280218f7bfcSspz             BIO_puts(bio_err, "Error creating delta CRL\n");
281218f7bfcSspz             goto end;
282218f7bfcSspz         }
283218f7bfcSspz     }
284218f7bfcSspz 
285c7da899bSchristos     if (badsig) {
286c7da899bSchristos         const ASN1_BIT_STRING *sig;
287c7da899bSchristos 
288c7da899bSchristos         X509_CRL_get0_signature(x, &sig, NULL);
289c7da899bSchristos         corrupt_signature(sig);
290c7da899bSchristos     }
291c7da899bSchristos 
292635165faSspz     if (num) {
293635165faSspz         for (i = 1; i <= num; i++) {
294635165faSspz             if (issuer == i) {
295*b0d17251Schristos                 print_name(bio_out, "issuer=", X509_CRL_get_issuer(x));
296a89c9211Schristos             }
297635165faSspz             if (crlnumber == i) {
298a89c9211Schristos                 ASN1_INTEGER *crlnum;
299*b0d17251Schristos 
300635165faSspz                 crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
301a89c9211Schristos                 BIO_printf(bio_out, "crlNumber=");
302635165faSspz                 if (crlnum) {
303*b0d17251Schristos                     BIO_puts(bio_out, "0x");
304a89c9211Schristos                     i2a_ASN1_INTEGER(bio_out, crlnum);
305a89c9211Schristos                     ASN1_INTEGER_free(crlnum);
306*b0d17251Schristos                 } else {
307a89c9211Schristos                     BIO_puts(bio_out, "<NONE>");
308*b0d17251Schristos                 }
309a89c9211Schristos                 BIO_printf(bio_out, "\n");
310a89c9211Schristos             }
311635165faSspz             if (hash == i) {
312*b0d17251Schristos                 int ok;
313*b0d17251Schristos                 unsigned long hash_value =
314*b0d17251Schristos                     X509_NAME_hash_ex(X509_CRL_get_issuer(x), app_get0_libctx(),
315*b0d17251Schristos                                       app_get0_propq(), &ok);
316*b0d17251Schristos 
317*b0d17251Schristos                 if (num > 1)
318*b0d17251Schristos                     BIO_printf(bio_out, "issuer name hash=");
319*b0d17251Schristos                 if (ok) {
320*b0d17251Schristos                     BIO_printf(bio_out, "%08lx\n", hash_value);
321*b0d17251Schristos                 } else {
322*b0d17251Schristos                     BIO_puts(bio_out, "<ERROR>");
323*b0d17251Schristos                     goto end;
324*b0d17251Schristos                 }
325a89c9211Schristos             }
326057199e6Schristos #ifndef OPENSSL_NO_MD5
327635165faSspz             if (hash_old == i) {
328*b0d17251Schristos                 if (num > 1)
329*b0d17251Schristos                     BIO_printf(bio_out, "issuer name old hash=");
330057199e6Schristos                 BIO_printf(bio_out, "%08lx\n",
331635165faSspz                            X509_NAME_hash_old(X509_CRL_get_issuer(x)));
332057199e6Schristos             }
333057199e6Schristos #endif
334635165faSspz             if (lastupdate == i) {
335a89c9211Schristos                 BIO_printf(bio_out, "lastUpdate=");
336*b0d17251Schristos                 ASN1_TIME_print_ex(bio_out, X509_CRL_get0_lastUpdate(x), dateopt);
337a89c9211Schristos                 BIO_printf(bio_out, "\n");
338a89c9211Schristos             }
339635165faSspz             if (nextupdate == i) {
340a89c9211Schristos                 BIO_printf(bio_out, "nextUpdate=");
341c7da899bSchristos                 if (X509_CRL_get0_nextUpdate(x))
342*b0d17251Schristos                     ASN1_TIME_print_ex(bio_out, X509_CRL_get0_nextUpdate(x), dateopt);
343a89c9211Schristos                 else
344a89c9211Schristos                     BIO_printf(bio_out, "NONE");
345a89c9211Schristos                 BIO_printf(bio_out, "\n");
346a89c9211Schristos             }
347635165faSspz             if (fingerprint == i) {
348a89c9211Schristos                 int j;
349a89c9211Schristos                 unsigned int n;
350a89c9211Schristos                 unsigned char md[EVP_MAX_MD_SIZE];
351a89c9211Schristos 
352635165faSspz                 if (!X509_CRL_digest(x, digest, md, &n)) {
353a89c9211Schristos                     BIO_printf(bio_err, "out of memory\n");
354a89c9211Schristos                     goto end;
355a89c9211Schristos                 }
356a89c9211Schristos                 BIO_printf(bio_out, "%s Fingerprint=",
357*b0d17251Schristos                            EVP_MD_get0_name(digest));
358635165faSspz                 for (j = 0; j < (int)n; j++) {
359635165faSspz                     BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
360a89c9211Schristos                                ? '\n' : ':');
361a89c9211Schristos                 }
362a89c9211Schristos             }
363a89c9211Schristos         }
364a89c9211Schristos     }
365c7da899bSchristos     out = bio_open_default(outfile, 'w', outformat);
366c7da899bSchristos     if (out == NULL)
367a89c9211Schristos         goto end;
368a89c9211Schristos 
369635165faSspz     if (text)
37013d40330Schristos         X509_CRL_print_ex(out, x, get_nameopt());
371a89c9211Schristos 
372635165faSspz     if (noout) {
373a89c9211Schristos         ret = 0;
374a89c9211Schristos         goto end;
375a89c9211Schristos     }
376a89c9211Schristos 
377a89c9211Schristos     if (outformat == FORMAT_ASN1)
378a89c9211Schristos         i = (int)i2d_X509_CRL_bio(out, x);
379c7da899bSchristos     else
380a89c9211Schristos         i = PEM_write_bio_X509_CRL(out, x);
381635165faSspz     if (!i) {
382635165faSspz         BIO_printf(bio_err, "unable to write CRL\n");
383635165faSspz         goto end;
384635165faSspz     }
385a89c9211Schristos     ret = 0;
386c7da899bSchristos 
387a89c9211Schristos  end:
388218f7bfcSspz     if (ret != 0)
389218f7bfcSspz         ERR_print_errors(bio_err);
390a89c9211Schristos     BIO_free_all(out);
391*b0d17251Schristos     EVP_MD_free(digest);
392a89c9211Schristos     X509_CRL_free(x);
393c7da899bSchristos     X509_STORE_CTX_free(ctx);
394a89c9211Schristos     X509_STORE_free(store);
39513d40330Schristos     return ret;
396a89c9211Schristos }
397