1*1dcdf01fSchristos /*
2*1dcdf01fSchristos  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
360662d10Schristos  *
4*1dcdf01fSchristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*1dcdf01fSchristos  * this file except in compliance with the License.  You can obtain a copy
6*1dcdf01fSchristos  * in the file LICENSE in the source distribution or at
7*1dcdf01fSchristos  * https://www.openssl.org/source/license.html
860662d10Schristos  */
960662d10Schristos 
1060662d10Schristos #include <stdio.h>
1160662d10Schristos #include <stdlib.h>
1260662d10Schristos #include <string.h>
1360662d10Schristos #include "apps.h"
14*1dcdf01fSchristos #include "progs.h"
1560662d10Schristos #include <openssl/bio.h>
1660662d10Schristos #include <openssl/err.h>
1760662d10Schristos #include <openssl/x509.h>
1860662d10Schristos #include <openssl/x509v3.h>
1960662d10Schristos #include <openssl/pem.h>
2060662d10Schristos 
21*1dcdf01fSchristos typedef enum OPTION_choice {
22*1dcdf01fSchristos     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
23*1dcdf01fSchristos     OPT_INFORM, OPT_IN, OPT_OUTFORM, OPT_OUT, OPT_KEYFORM, OPT_KEY,
24*1dcdf01fSchristos     OPT_ISSUER, OPT_LASTUPDATE, OPT_NEXTUPDATE, OPT_FINGERPRINT,
25*1dcdf01fSchristos     OPT_CRLNUMBER, OPT_BADSIG, OPT_GENDELTA, OPT_CAPATH, OPT_CAFILE,
26*1dcdf01fSchristos     OPT_NOCAPATH, OPT_NOCAFILE, OPT_VERIFY, OPT_TEXT, OPT_HASH, OPT_HASH_OLD,
27*1dcdf01fSchristos     OPT_NOOUT, OPT_NAMEOPT, OPT_MD
28*1dcdf01fSchristos } OPTION_CHOICE;
2960662d10Schristos 
30*1dcdf01fSchristos const OPTIONS crl_options[] = {
31*1dcdf01fSchristos     {"help", OPT_HELP, '-', "Display this summary"},
32*1dcdf01fSchristos     {"inform", OPT_INFORM, 'F', "Input format; default PEM"},
33*1dcdf01fSchristos     {"in", OPT_IN, '<', "Input file - default stdin"},
34*1dcdf01fSchristos     {"outform", OPT_OUTFORM, 'F', "Output format - default PEM"},
35*1dcdf01fSchristos     {"out", OPT_OUT, '>', "output file - default stdout"},
36*1dcdf01fSchristos     {"keyform", OPT_KEYFORM, 'F', "Private key file format (PEM or ENGINE)"},
37*1dcdf01fSchristos     {"key", OPT_KEY, '<', "CRL signing Private key to use"},
38*1dcdf01fSchristos     {"issuer", OPT_ISSUER, '-', "Print issuer DN"},
39*1dcdf01fSchristos     {"lastupdate", OPT_LASTUPDATE, '-', "Set lastUpdate field"},
40*1dcdf01fSchristos     {"nextupdate", OPT_NEXTUPDATE, '-', "Set nextUpdate field"},
41*1dcdf01fSchristos     {"noout", OPT_NOOUT, '-', "No CRL output"},
42*1dcdf01fSchristos     {"fingerprint", OPT_FINGERPRINT, '-', "Print the crl fingerprint"},
43*1dcdf01fSchristos     {"crlnumber", OPT_CRLNUMBER, '-', "Print CRL number"},
44*1dcdf01fSchristos     {"badsig", OPT_BADSIG, '-', "Corrupt last byte of loaded CRL signature (for test)" },
45*1dcdf01fSchristos     {"gendelta", OPT_GENDELTA, '<', "Other CRL to compare/diff to the Input one"},
46*1dcdf01fSchristos     {"CApath", OPT_CAPATH, '/', "Verify CRL using certificates in dir"},
47*1dcdf01fSchristos     {"CAfile", OPT_CAFILE, '<', "Verify CRL using certificates in file name"},
48*1dcdf01fSchristos     {"no-CAfile", OPT_NOCAFILE, '-',
49*1dcdf01fSchristos      "Do not load the default certificates file"},
50*1dcdf01fSchristos     {"no-CApath", OPT_NOCAPATH, '-',
51*1dcdf01fSchristos      "Do not load certificates from the default certificates directory"},
52*1dcdf01fSchristos     {"verify", OPT_VERIFY, '-', "Verify CRL signature"},
53*1dcdf01fSchristos     {"text", OPT_TEXT, '-', "Print out a text format version"},
54*1dcdf01fSchristos     {"hash", OPT_HASH, '-', "Print hash value"},
55*1dcdf01fSchristos     {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
56*1dcdf01fSchristos     {"", OPT_MD, '-', "Any supported digest"},
5760662d10Schristos #ifndef OPENSSL_NO_MD5
58*1dcdf01fSchristos     {"hash_old", OPT_HASH_OLD, '-', "Print old-style (MD5) hash value"},
5960662d10Schristos #endif
60*1dcdf01fSchristos     {NULL}
6160662d10Schristos };
6260662d10Schristos 
crl_main(int argc,char ** argv)63*1dcdf01fSchristos int crl_main(int argc, char **argv)
6460662d10Schristos {
6560662d10Schristos     X509_CRL *x = NULL;
6660662d10Schristos     BIO *out = NULL;
67*1dcdf01fSchristos     X509_STORE *store = NULL;
68*1dcdf01fSchristos     X509_STORE_CTX *ctx = NULL;
69*1dcdf01fSchristos     X509_LOOKUP *lookup = NULL;
70*1dcdf01fSchristos     X509_OBJECT *xobj = NULL;
71*1dcdf01fSchristos     EVP_PKEY *pkey;
72*1dcdf01fSchristos     const EVP_MD *digest = EVP_sha1();
7360662d10Schristos     char *infile = NULL, *outfile = NULL, *crldiff = NULL, *keyfile = NULL;
74*1dcdf01fSchristos     const char *CAfile = NULL, *CApath = NULL, *prog;
75*1dcdf01fSchristos     OPTION_CHOICE o;
76*1dcdf01fSchristos     int hash = 0, issuer = 0, lastupdate = 0, nextupdate = 0, noout = 0;
77*1dcdf01fSchristos     int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM;
78*1dcdf01fSchristos     int ret = 1, num = 0, badsig = 0, fingerprint = 0, crlnumber = 0;
79*1dcdf01fSchristos     int text = 0, do_ver = 0, noCAfile = 0, noCApath = 0;
80*1dcdf01fSchristos     int i;
8160662d10Schristos #ifndef OPENSSL_NO_MD5
8260662d10Schristos     int hash_old = 0;
8360662d10Schristos #endif
8460662d10Schristos 
85*1dcdf01fSchristos     prog = opt_init(argc, argv, crl_options);
86*1dcdf01fSchristos     while ((o = opt_next()) != OPT_EOF) {
87*1dcdf01fSchristos         switch (o) {
88*1dcdf01fSchristos         case OPT_EOF:
89*1dcdf01fSchristos         case OPT_ERR:
90*1dcdf01fSchristos  opthelp:
91*1dcdf01fSchristos             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
9260662d10Schristos             goto end;
93*1dcdf01fSchristos         case OPT_HELP:
94*1dcdf01fSchristos             opt_help(crl_options);
95*1dcdf01fSchristos             ret = 0;
9660662d10Schristos             goto end;
97*1dcdf01fSchristos         case OPT_INFORM:
98*1dcdf01fSchristos             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
99*1dcdf01fSchristos                 goto opthelp;
100*1dcdf01fSchristos             break;
101*1dcdf01fSchristos         case OPT_IN:
102*1dcdf01fSchristos             infile = opt_arg();
103*1dcdf01fSchristos             break;
104*1dcdf01fSchristos         case OPT_OUTFORM:
105*1dcdf01fSchristos             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
106*1dcdf01fSchristos                 goto opthelp;
107*1dcdf01fSchristos             break;
108*1dcdf01fSchristos         case OPT_OUT:
109*1dcdf01fSchristos             outfile = opt_arg();
110*1dcdf01fSchristos             break;
111*1dcdf01fSchristos         case OPT_KEYFORM:
112*1dcdf01fSchristos             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
113*1dcdf01fSchristos                 goto opthelp;
114*1dcdf01fSchristos             break;
115*1dcdf01fSchristos         case OPT_KEY:
116*1dcdf01fSchristos             keyfile = opt_arg();
117*1dcdf01fSchristos             break;
118*1dcdf01fSchristos         case OPT_GENDELTA:
119*1dcdf01fSchristos             crldiff = opt_arg();
120*1dcdf01fSchristos             break;
121*1dcdf01fSchristos         case OPT_CAPATH:
122*1dcdf01fSchristos             CApath = opt_arg();
12360662d10Schristos             do_ver = 1;
124*1dcdf01fSchristos             break;
125*1dcdf01fSchristos         case OPT_CAFILE:
126*1dcdf01fSchristos             CAfile = opt_arg();
12760662d10Schristos             do_ver = 1;
128*1dcdf01fSchristos             break;
129*1dcdf01fSchristos         case OPT_NOCAPATH:
130*1dcdf01fSchristos             noCApath =  1;
131*1dcdf01fSchristos             break;
132*1dcdf01fSchristos         case OPT_NOCAFILE:
133*1dcdf01fSchristos             noCAfile =  1;
134*1dcdf01fSchristos             break;
135*1dcdf01fSchristos         case OPT_HASH_OLD:
13660662d10Schristos #ifndef OPENSSL_NO_MD5
13760662d10Schristos             hash_old = ++num;
13860662d10Schristos #endif
13960662d10Schristos             break;
140*1dcdf01fSchristos         case OPT_VERIFY:
141*1dcdf01fSchristos             do_ver = 1;
142*1dcdf01fSchristos             break;
143*1dcdf01fSchristos         case OPT_TEXT:
144*1dcdf01fSchristos             text = 1;
145*1dcdf01fSchristos             break;
146*1dcdf01fSchristos         case OPT_HASH:
147*1dcdf01fSchristos             hash = ++num;
148*1dcdf01fSchristos             break;
149*1dcdf01fSchristos         case OPT_ISSUER:
150*1dcdf01fSchristos             issuer = ++num;
151*1dcdf01fSchristos             break;
152*1dcdf01fSchristos         case OPT_LASTUPDATE:
153*1dcdf01fSchristos             lastupdate = ++num;
154*1dcdf01fSchristos             break;
155*1dcdf01fSchristos         case OPT_NEXTUPDATE:
156*1dcdf01fSchristos             nextupdate = ++num;
157*1dcdf01fSchristos             break;
158*1dcdf01fSchristos         case OPT_NOOUT:
159*1dcdf01fSchristos             noout = ++num;
160*1dcdf01fSchristos             break;
161*1dcdf01fSchristos         case OPT_FINGERPRINT:
162*1dcdf01fSchristos             fingerprint = ++num;
163*1dcdf01fSchristos             break;
164*1dcdf01fSchristos         case OPT_CRLNUMBER:
165*1dcdf01fSchristos             crlnumber = ++num;
166*1dcdf01fSchristos             break;
167*1dcdf01fSchristos         case OPT_BADSIG:
168*1dcdf01fSchristos             badsig = 1;
169*1dcdf01fSchristos             break;
170*1dcdf01fSchristos         case OPT_NAMEOPT:
171*1dcdf01fSchristos             if (!set_nameopt(opt_arg()))
172*1dcdf01fSchristos                 goto opthelp;
173*1dcdf01fSchristos             break;
174*1dcdf01fSchristos         case OPT_MD:
175*1dcdf01fSchristos             if (!opt_md(opt_unknown(), &digest))
176*1dcdf01fSchristos                 goto opthelp;
17760662d10Schristos         }
17860662d10Schristos     }
179*1dcdf01fSchristos     argc = opt_num_rest();
180*1dcdf01fSchristos     if (argc != 0)
181*1dcdf01fSchristos         goto opthelp;
18260662d10Schristos 
18360662d10Schristos     x = load_crl(infile, informat);
184*1dcdf01fSchristos     if (x == NULL)
18560662d10Schristos         goto end;
18660662d10Schristos 
18760662d10Schristos     if (do_ver) {
188*1dcdf01fSchristos         if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
189*1dcdf01fSchristos             goto end;
19060662d10Schristos         lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
19160662d10Schristos         if (lookup == NULL)
19260662d10Schristos             goto end;
193*1dcdf01fSchristos         ctx = X509_STORE_CTX_new();
194*1dcdf01fSchristos         if (ctx == NULL || !X509_STORE_CTX_init(ctx, store, NULL, NULL)) {
19560662d10Schristos             BIO_printf(bio_err, "Error initialising X509 store\n");
19660662d10Schristos             goto end;
19760662d10Schristos         }
19860662d10Schristos 
199*1dcdf01fSchristos         xobj = X509_STORE_CTX_get_obj_by_subject(ctx, X509_LU_X509,
200*1dcdf01fSchristos                                                  X509_CRL_get_issuer(x));
201*1dcdf01fSchristos         if (xobj == NULL) {
20260662d10Schristos             BIO_printf(bio_err, "Error getting CRL issuer certificate\n");
20360662d10Schristos             goto end;
20460662d10Schristos         }
205*1dcdf01fSchristos         pkey = X509_get_pubkey(X509_OBJECT_get0_X509(xobj));
206*1dcdf01fSchristos         X509_OBJECT_free(xobj);
20760662d10Schristos         if (!pkey) {
20860662d10Schristos             BIO_printf(bio_err, "Error getting CRL issuer public key\n");
20960662d10Schristos             goto end;
21060662d10Schristos         }
21160662d10Schristos         i = X509_CRL_verify(x, pkey);
21260662d10Schristos         EVP_PKEY_free(pkey);
21360662d10Schristos         if (i < 0)
21460662d10Schristos             goto end;
21560662d10Schristos         if (i == 0)
21660662d10Schristos             BIO_printf(bio_err, "verify failure\n");
21760662d10Schristos         else
21860662d10Schristos             BIO_printf(bio_err, "verify OK\n");
21960662d10Schristos     }
22060662d10Schristos 
22160662d10Schristos     if (crldiff) {
22260662d10Schristos         X509_CRL *newcrl, *delta;
22360662d10Schristos         if (!keyfile) {
22460662d10Schristos             BIO_puts(bio_err, "Missing CRL signing key\n");
22560662d10Schristos             goto end;
22660662d10Schristos         }
22760662d10Schristos         newcrl = load_crl(crldiff, informat);
22860662d10Schristos         if (!newcrl)
22960662d10Schristos             goto end;
230*1dcdf01fSchristos         pkey = load_key(keyfile, keyformat, 0, NULL, NULL, "CRL signing key");
23160662d10Schristos         if (!pkey) {
23260662d10Schristos             X509_CRL_free(newcrl);
23360662d10Schristos             goto end;
23460662d10Schristos         }
23560662d10Schristos         delta = X509_CRL_diff(x, newcrl, pkey, digest, 0);
23660662d10Schristos         X509_CRL_free(newcrl);
23760662d10Schristos         EVP_PKEY_free(pkey);
23860662d10Schristos         if (delta) {
23960662d10Schristos             X509_CRL_free(x);
24060662d10Schristos             x = delta;
24160662d10Schristos         } else {
24260662d10Schristos             BIO_puts(bio_err, "Error creating delta CRL\n");
24360662d10Schristos             goto end;
24460662d10Schristos         }
24560662d10Schristos     }
24660662d10Schristos 
247*1dcdf01fSchristos     if (badsig) {
248*1dcdf01fSchristos         const ASN1_BIT_STRING *sig;
249*1dcdf01fSchristos 
250*1dcdf01fSchristos         X509_CRL_get0_signature(x, &sig, NULL);
251*1dcdf01fSchristos         corrupt_signature(sig);
252*1dcdf01fSchristos     }
253*1dcdf01fSchristos 
25460662d10Schristos     if (num) {
25560662d10Schristos         for (i = 1; i <= num; i++) {
25660662d10Schristos             if (issuer == i) {
25760662d10Schristos                 print_name(bio_out, "issuer=", X509_CRL_get_issuer(x),
258*1dcdf01fSchristos                            get_nameopt());
25960662d10Schristos             }
26060662d10Schristos             if (crlnumber == i) {
26160662d10Schristos                 ASN1_INTEGER *crlnum;
26260662d10Schristos                 crlnum = X509_CRL_get_ext_d2i(x, NID_crl_number, NULL, NULL);
26360662d10Schristos                 BIO_printf(bio_out, "crlNumber=");
26460662d10Schristos                 if (crlnum) {
26560662d10Schristos                     i2a_ASN1_INTEGER(bio_out, crlnum);
26660662d10Schristos                     ASN1_INTEGER_free(crlnum);
26760662d10Schristos                 } else
26860662d10Schristos                     BIO_puts(bio_out, "<NONE>");
26960662d10Schristos                 BIO_printf(bio_out, "\n");
27060662d10Schristos             }
27160662d10Schristos             if (hash == i) {
27260662d10Schristos                 BIO_printf(bio_out, "%08lx\n",
27360662d10Schristos                            X509_NAME_hash(X509_CRL_get_issuer(x)));
27460662d10Schristos             }
27560662d10Schristos #ifndef OPENSSL_NO_MD5
27660662d10Schristos             if (hash_old == i) {
27760662d10Schristos                 BIO_printf(bio_out, "%08lx\n",
27860662d10Schristos                            X509_NAME_hash_old(X509_CRL_get_issuer(x)));
27960662d10Schristos             }
28060662d10Schristos #endif
28160662d10Schristos             if (lastupdate == i) {
28260662d10Schristos                 BIO_printf(bio_out, "lastUpdate=");
283*1dcdf01fSchristos                 ASN1_TIME_print(bio_out, X509_CRL_get0_lastUpdate(x));
28460662d10Schristos                 BIO_printf(bio_out, "\n");
28560662d10Schristos             }
28660662d10Schristos             if (nextupdate == i) {
28760662d10Schristos                 BIO_printf(bio_out, "nextUpdate=");
288*1dcdf01fSchristos                 if (X509_CRL_get0_nextUpdate(x))
289*1dcdf01fSchristos                     ASN1_TIME_print(bio_out, X509_CRL_get0_nextUpdate(x));
29060662d10Schristos                 else
29160662d10Schristos                     BIO_printf(bio_out, "NONE");
29260662d10Schristos                 BIO_printf(bio_out, "\n");
29360662d10Schristos             }
29460662d10Schristos             if (fingerprint == i) {
29560662d10Schristos                 int j;
29660662d10Schristos                 unsigned int n;
29760662d10Schristos                 unsigned char md[EVP_MAX_MD_SIZE];
29860662d10Schristos 
29960662d10Schristos                 if (!X509_CRL_digest(x, digest, md, &n)) {
30060662d10Schristos                     BIO_printf(bio_err, "out of memory\n");
30160662d10Schristos                     goto end;
30260662d10Schristos                 }
30360662d10Schristos                 BIO_printf(bio_out, "%s Fingerprint=",
30460662d10Schristos                            OBJ_nid2sn(EVP_MD_type(digest)));
30560662d10Schristos                 for (j = 0; j < (int)n; j++) {
30660662d10Schristos                     BIO_printf(bio_out, "%02X%c", md[j], (j + 1 == (int)n)
30760662d10Schristos                                ? '\n' : ':');
30860662d10Schristos                 }
30960662d10Schristos             }
31060662d10Schristos         }
31160662d10Schristos     }
312*1dcdf01fSchristos     out = bio_open_default(outfile, 'w', outformat);
313*1dcdf01fSchristos     if (out == NULL)
31460662d10Schristos         goto end;
31560662d10Schristos 
31660662d10Schristos     if (text)
317*1dcdf01fSchristos         X509_CRL_print_ex(out, x, get_nameopt());
31860662d10Schristos 
31960662d10Schristos     if (noout) {
32060662d10Schristos         ret = 0;
32160662d10Schristos         goto end;
32260662d10Schristos     }
32360662d10Schristos 
32460662d10Schristos     if (outformat == FORMAT_ASN1)
32560662d10Schristos         i = (int)i2d_X509_CRL_bio(out, x);
326*1dcdf01fSchristos     else
32760662d10Schristos         i = PEM_write_bio_X509_CRL(out, x);
32860662d10Schristos     if (!i) {
32960662d10Schristos         BIO_printf(bio_err, "unable to write CRL\n");
33060662d10Schristos         goto end;
33160662d10Schristos     }
33260662d10Schristos     ret = 0;
333*1dcdf01fSchristos 
33460662d10Schristos  end:
33560662d10Schristos     if (ret != 0)
33660662d10Schristos         ERR_print_errors(bio_err);
33760662d10Schristos     BIO_free_all(out);
33860662d10Schristos     X509_CRL_free(x);
339*1dcdf01fSchristos     X509_STORE_CTX_free(ctx);
34060662d10Schristos     X509_STORE_free(store);
341*1dcdf01fSchristos     return ret;
34260662d10Schristos }
343