[BACK]Return to crl.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / openssl

Annotation of src/usr.bin/openssl/crl.c, Revision 1.2

1.2     ! jsing       1: /* $OpenBSD: crl.c,v 1.1 2014/08/26 17:47:24 jsing Exp $ */
1.1       jsing       2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
                      3:  * All rights reserved.
                      4:  *
                      5:  * This package is an SSL implementation written
                      6:  * by Eric Young (eay@cryptsoft.com).
                      7:  * The implementation was written so as to conform with Netscapes SSL.
                      8:  *
                      9:  * This library is free for commercial and non-commercial use as long as
                     10:  * the following conditions are aheared to.  The following conditions
                     11:  * apply to all code found in this distribution, be it the RC4, RSA,
                     12:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
                     13:  * included with this distribution is covered by the same copyright terms
                     14:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
                     15:  *
                     16:  * Copyright remains Eric Young's, and as such any Copyright notices in
                     17:  * the code are not to be removed.
                     18:  * If this package is used in a product, Eric Young should be given attribution
                     19:  * as the author of the parts of the library used.
                     20:  * This can be in the form of a textual message at program startup or
                     21:  * in documentation (online or textual) provided with the package.
                     22:  *
                     23:  * Redistribution and use in source and binary forms, with or without
                     24:  * modification, are permitted provided that the following conditions
                     25:  * are met:
                     26:  * 1. Redistributions of source code must retain the copyright
                     27:  *    notice, this list of conditions and the following disclaimer.
                     28:  * 2. Redistributions in binary form must reproduce the above copyright
                     29:  *    notice, this list of conditions and the following disclaimer in the
                     30:  *    documentation and/or other materials provided with the distribution.
                     31:  * 3. All advertising materials mentioning features or use of this software
                     32:  *    must display the following acknowledgement:
                     33:  *    "This product includes cryptographic software written by
                     34:  *     Eric Young (eay@cryptsoft.com)"
                     35:  *    The word 'cryptographic' can be left out if the rouines from the library
                     36:  *    being used are not cryptographic related :-).
                     37:  * 4. If you include any Windows specific code (or a derivative thereof) from
                     38:  *    the apps directory (application code) you must include an acknowledgement:
                     39:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
                     40:  *
                     41:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
                     42:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     43:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     44:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     45:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     46:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     47:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     49:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     50:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     51:  * SUCH DAMAGE.
                     52:  *
                     53:  * The licence and distribution terms for any publically available version or
                     54:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
                     55:  * copied and put under another distribution licence
                     56:  * [including the GNU Public Licence.]
                     57:  */
                     58:
                     59: #include <stdio.h>
                     60: #include <stdlib.h>
                     61: #include <string.h>
                     62:
                     63: #include "apps.h"
                     64:
                     65: #include <openssl/bio.h>
                     66: #include <openssl/err.h>
                     67: #include <openssl/pem.h>
                     68: #include <openssl/x509.h>
                     69: #include <openssl/x509v3.h>
                     70:
                     71: #define        POSTFIX ".rvk"
                     72:
1.2     ! jsing      73: static struct {
        !            74:        char *cafile;
        !            75:        char *capath;
        !            76:        int crlnumber;
        !            77:        int fingerprint;
        !            78:        int hash;
        !            79:        int hash_old;
        !            80:        char *infile;
        !            81:        int informat;
        !            82:        int issuer;
        !            83:        int lastupdate;
        !            84:        char *nameopt;
        !            85:        int nextupdate;
        !            86:        int noout;
        !            87:        char *outfile;
        !            88:        int outformat;
        !            89:        int text;
        !            90:        int verify;
        !            91: } crl_config;
        !            92:
        !            93: static struct option crl_options[] = {
        !            94:        {
        !            95:                .name = "CAfile",
        !            96:                .argname = "file",
        !            97:                .desc = "Verify the CRL using certificates in the given file",
        !            98:                .type = OPTION_ARG,
        !            99:                .opt.arg = &crl_config.cafile,
        !           100:        },
        !           101:        {
        !           102:                .name = "CApath",
        !           103:                .argname = "path",
        !           104:                .desc = "Verify the CRL using certificates in the given path",
        !           105:                .type = OPTION_ARG,
        !           106:                .opt.arg = &crl_config.capath,
        !           107:        },
        !           108:        {
        !           109:                .name = "crlnumber",
        !           110:                .desc = "Print the CRL number",
        !           111:                .type = OPTION_FLAG_ORD,
        !           112:                .opt.flag = &crl_config.crlnumber,
        !           113:        },
        !           114:        {
        !           115:                .name = "fingerprint",
        !           116:                .desc = "Print the CRL fingerprint",
        !           117:                .type = OPTION_FLAG_ORD,
        !           118:                .opt.flag = &crl_config.fingerprint,
        !           119:        },
        !           120:        {
        !           121:                .name = "hash",
        !           122:                .desc = "Print the hash of the issuer name",
        !           123:                .type = OPTION_FLAG_ORD,
        !           124:                .opt.flag = &crl_config.hash,
        !           125:        },
        !           126:        {
        !           127:                .name = "hash_old",
        !           128:                .desc = "Print an old-style (MD5) hash of the issuer name",
        !           129:                .type = OPTION_FLAG_ORD,
        !           130:                .opt.flag = &crl_config.hash_old,
        !           131:        },
        !           132:        {
        !           133:                .name = "in",
        !           134:                .argname = "file",
        !           135:                .desc = "Input file to read from (stdin if unspecified)",
        !           136:                .type = OPTION_ARG,
        !           137:                .opt.arg = &crl_config.infile,
        !           138:        },
        !           139:        {
        !           140:                .name = "inform",
        !           141:                .argname = "format",
        !           142:                .desc = "Input format (DER or PEM)",
        !           143:                .type = OPTION_ARG_FORMAT,
        !           144:                .opt.value = &crl_config.informat,
        !           145:        },
        !           146:        {
        !           147:                .name = "issuer",
        !           148:                .desc = "Print the issuer name",
        !           149:                .type = OPTION_FLAG_ORD,
        !           150:                .opt.flag = &crl_config.issuer,
        !           151:        },
        !           152:        {
        !           153:                .name = "lastupdate",
        !           154:                .desc = "Print the lastUpdate field",
        !           155:                .type = OPTION_FLAG_ORD,
        !           156:                .opt.flag = &crl_config.lastupdate,
        !           157:        },
        !           158:        {
        !           159:                .name = "nameopt",
        !           160:                .argname = "options",
        !           161:                .desc = "Specify certificate name options",
        !           162:                .type = OPTION_ARG,
        !           163:                .opt.arg = &crl_config.nameopt,
        !           164:        },
        !           165:        {
        !           166:                .name = "nextupdate",
        !           167:                .desc = "Print the nextUpdate field",
        !           168:                .type = OPTION_FLAG_ORD,
        !           169:                .opt.flag = &crl_config.nextupdate,
        !           170:        },
        !           171:        {
        !           172:                .name = "noout",
        !           173:                .desc = "Do not output the encoded version of the CRL",
        !           174:                .type = OPTION_FLAG,
        !           175:                .opt.flag = &crl_config.noout,
        !           176:        },
        !           177:        {
        !           178:                .name = "out",
        !           179:                .argname = "file",
        !           180:                .desc = "Output file to write to (stdout if unspecified)",
        !           181:                .type = OPTION_ARG,
        !           182:                .opt.arg = &crl_config.outfile,
        !           183:        },
        !           184:        {
        !           185:                .name = "outform",
        !           186:                .argname = "format",
        !           187:                .desc = "Output format (DER or PEM)",
        !           188:                .type = OPTION_ARG_FORMAT,
        !           189:                .opt.value = &crl_config.outformat,
        !           190:        },
        !           191:        {
        !           192:                .name = "text",
        !           193:                .desc = "Print out the CRL in text form",
        !           194:                .type = OPTION_FLAG,
        !           195:                .opt.flag = &crl_config.text,
        !           196:        },
        !           197:        {
        !           198:                .name = "verify",
        !           199:                .desc = "Verify the signature on the CRL",
        !           200:                .type = OPTION_FLAG,
        !           201:                .opt.flag = &crl_config.verify,
        !           202:        },
        !           203:        {},
1.1       jsing     204: };
                    205:
1.2     ! jsing     206: static void
        !           207: crl_usage(void)
        !           208: {
        !           209:        fprintf(stderr,
        !           210:            "usage: crl [-CAfile file] [-CApath dir] [-fingerprint] [-hash]\n"
        !           211:            "    [-in file] [-inform DER | PEM] [-issuer] [-lastupdate]\n"
        !           212:            "    [-nextupdate] [-noout] [-out file] [-outform DER | PEM]\n"
        !           213:            "    [-text]\n\n");
        !           214:        options_usage(crl_options);
        !           215: }
        !           216:
1.1       jsing     217: static X509_CRL *load_crl(char *file, int format);
                    218: static BIO *bio_out = NULL;
                    219:
                    220: int crl_main(int, char **);
                    221:
                    222: int
                    223: crl_main(int argc, char **argv)
                    224: {
                    225:        unsigned long nmflag = 0;
                    226:        X509_CRL *x = NULL;
1.2     ! jsing     227:        int ret = 1, i;
1.1       jsing     228:        BIO *out = NULL;
                    229:        X509_STORE *store = NULL;
                    230:        X509_STORE_CTX ctx;
                    231:        X509_LOOKUP *lookup = NULL;
                    232:        X509_OBJECT xobj;
                    233:        EVP_PKEY *pkey;
1.2     ! jsing     234:        const EVP_MD *digest;
        !           235:        char *digest_name = NULL;
1.1       jsing     236:
1.2     ! jsing     237:        if (bio_out == NULL) {
1.1       jsing     238:                if ((bio_out = BIO_new(BIO_s_file())) != NULL) {
                    239:                        BIO_set_fp(bio_out, stdout, BIO_NOCLOSE);
                    240:                }
1.2     ! jsing     241:        }
        !           242:
        !           243:        digest = EVP_sha1();
        !           244:
        !           245:        memset(&crl_config, 0, sizeof(crl_config));
        !           246:        crl_config.informat = FORMAT_PEM;
        !           247:        crl_config.outformat = FORMAT_PEM;
1.1       jsing     248:
1.2     ! jsing     249:        if (options_parse(argc, argv, crl_options, &digest_name) != 0) {
        !           250:                crl_usage();
        !           251:                goto end;
        !           252:        }
        !           253:
        !           254:        if (crl_config.cafile != NULL || crl_config.capath != NULL)
        !           255:                crl_config.verify = 1;
        !           256:
        !           257:        if (crl_config.nameopt != NULL) {
        !           258:                if (set_name_ex(&nmflag, crl_config.nameopt) != 1) {
        !           259:                        fprintf(stderr,
        !           260:                            "Invalid -nameopt argument '%s'\n",
        !           261:                            crl_config.nameopt);
        !           262:                        goto end;
1.1       jsing     263:                }
1.2     ! jsing     264:        }
        !           265:
        !           266:        if (digest_name != NULL) {
        !           267:                if ((digest = EVP_get_digestbyname(digest_name)) == NULL) {
        !           268:                        fprintf(stderr,
        !           269:                            "Unknown message digest algorithm '%s'\n",
        !           270:                            digest_name);
        !           271:                        goto end;
1.1       jsing     272:                }
                    273:        }
                    274:
                    275:        ERR_load_crypto_strings();
1.2     ! jsing     276:        x = load_crl(crl_config.infile, crl_config.informat);
        !           277:        if (x == NULL)
1.1       jsing     278:                goto end;
1.2     ! jsing     279:
        !           280:        if (crl_config.verify) {
1.1       jsing     281:                store = X509_STORE_new();
                    282:                lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
                    283:                if (lookup == NULL)
                    284:                        goto end;
1.2     ! jsing     285:                if (!X509_LOOKUP_load_file(lookup, crl_config.cafile,
        !           286:                    X509_FILETYPE_PEM))
1.1       jsing     287:                        X509_LOOKUP_load_file(lookup, NULL,
                    288:                            X509_FILETYPE_DEFAULT);
                    289:
                    290:                lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
                    291:                if (lookup == NULL)
                    292:                        goto end;
1.2     ! jsing     293:                if (!X509_LOOKUP_add_dir(lookup, crl_config.capath,
        !           294:                    X509_FILETYPE_PEM))
1.1       jsing     295:                        X509_LOOKUP_add_dir(lookup, NULL,
                    296:                            X509_FILETYPE_DEFAULT);
                    297:                ERR_clear_error();
                    298:
                    299:                if (!X509_STORE_CTX_init(&ctx, store, NULL, NULL)) {
                    300:                        BIO_printf(bio_err,
                    301:                            "Error initialising X509 store\n");
                    302:                        goto end;
                    303:                }
                    304:                i = X509_STORE_get_by_subject(&ctx, X509_LU_X509,
                    305:                    X509_CRL_get_issuer(x), &xobj);
                    306:                if (i <= 0) {
                    307:                        BIO_printf(bio_err,
                    308:                            "Error getting CRL issuer certificate\n");
                    309:                        goto end;
                    310:                }
                    311:                pkey = X509_get_pubkey(xobj.data.x509);
                    312:                X509_OBJECT_free_contents(&xobj);
                    313:                if (!pkey) {
                    314:                        BIO_printf(bio_err,
                    315:                            "Error getting CRL issuer public key\n");
                    316:                        goto end;
                    317:                }
                    318:                i = X509_CRL_verify(x, pkey);
                    319:                EVP_PKEY_free(pkey);
                    320:                if (i < 0)
                    321:                        goto end;
                    322:                if (i == 0)
                    323:                        BIO_printf(bio_err, "verify failure\n");
                    324:                else
                    325:                        BIO_printf(bio_err, "verify OK\n");
                    326:        }
1.2     ! jsing     327:
        !           328:        /* Print requested information the order that the flags were given. */
        !           329:        for (i = 1; i <= argc; i++) {
        !           330:                if (crl_config.issuer == i) {
        !           331:                        print_name(bio_out, "issuer=",
        !           332:                            X509_CRL_get_issuer(x), nmflag);
        !           333:                }
        !           334:                if (crl_config.crlnumber == i) {
        !           335:                        ASN1_INTEGER *crlnum;
        !           336:                        crlnum = X509_CRL_get_ext_d2i(x,
        !           337:                            NID_crl_number, NULL, NULL);
        !           338:                        BIO_printf(bio_out, "crlNumber=");
        !           339:                        if (crlnum) {
        !           340:                                i2a_ASN1_INTEGER(bio_out, crlnum);
        !           341:                                ASN1_INTEGER_free(crlnum);
        !           342:                        } else
        !           343:                                BIO_puts(bio_out, "<NONE>");
        !           344:                        BIO_printf(bio_out, "\n");
        !           345:                }
        !           346:                if (crl_config.hash == i) {
        !           347:                        BIO_printf(bio_out, "%08lx\n",
        !           348:                            X509_NAME_hash(X509_CRL_get_issuer(x)));
        !           349:                }
1.1       jsing     350: #ifndef OPENSSL_NO_MD5
1.2     ! jsing     351:                if (crl_config.hash_old == i) {
        !           352:                        BIO_printf(bio_out, "%08lx\n",
        !           353:                            X509_NAME_hash_old(X509_CRL_get_issuer(x)));
        !           354:                }
1.1       jsing     355: #endif
1.2     ! jsing     356:                if (crl_config.lastupdate == i) {
        !           357:                        BIO_printf(bio_out, "lastUpdate=");
        !           358:                        ASN1_TIME_print(bio_out,
        !           359:                            X509_CRL_get_lastUpdate(x));
        !           360:                        BIO_printf(bio_out, "\n");
        !           361:                }
        !           362:                if (crl_config.nextupdate == i) {
        !           363:                        BIO_printf(bio_out, "nextUpdate=");
        !           364:                        if (X509_CRL_get_nextUpdate(x))
1.1       jsing     365:                                ASN1_TIME_print(bio_out,
1.2     ! jsing     366:                                    X509_CRL_get_nextUpdate(x));
        !           367:                        else
        !           368:                                BIO_printf(bio_out, "NONE");
        !           369:                        BIO_printf(bio_out, "\n");
        !           370:                }
        !           371:                if (crl_config.fingerprint == i) {
        !           372:                        int j;
        !           373:                        unsigned int n;
        !           374:                        unsigned char md[EVP_MAX_MD_SIZE];
        !           375:
        !           376:                        if (!X509_CRL_digest(x, digest, md, &n)) {
        !           377:                                BIO_printf(bio_err, "out of memory\n");
        !           378:                                goto end;
1.1       jsing     379:                        }
1.2     ! jsing     380:                        BIO_printf(bio_out, "%s Fingerprint=",
        !           381:                            OBJ_nid2sn(EVP_MD_type(digest)));
        !           382:                        for (j = 0; j < (int) n; j++) {
        !           383:                                BIO_printf(bio_out, "%02X%c", md[j],
        !           384:                                    (j + 1 == (int)n) ? '\n' : ':');
1.1       jsing     385:                        }
                    386:                }
                    387:        }
1.2     ! jsing     388:
1.1       jsing     389:        out = BIO_new(BIO_s_file());
                    390:        if (out == NULL) {
                    391:                ERR_print_errors(bio_err);
                    392:                goto end;
                    393:        }
1.2     ! jsing     394:        if (crl_config.outfile == NULL) {
1.1       jsing     395:                BIO_set_fp(out, stdout, BIO_NOCLOSE);
                    396:        } else {
1.2     ! jsing     397:                if (BIO_write_filename(out, crl_config.outfile) <= 0) {
        !           398:                        perror(crl_config.outfile);
1.1       jsing     399:                        goto end;
                    400:                }
                    401:        }
                    402:
1.2     ! jsing     403:        if (crl_config.text)
1.1       jsing     404:                X509_CRL_print(out, x);
                    405:
1.2     ! jsing     406:        if (crl_config.noout) {
1.1       jsing     407:                ret = 0;
                    408:                goto end;
                    409:        }
1.2     ! jsing     410:        if (crl_config.outformat == FORMAT_ASN1)
1.1       jsing     411:                i = (int) i2d_X509_CRL_bio(out, x);
1.2     ! jsing     412:        else if (crl_config.outformat == FORMAT_PEM)
1.1       jsing     413:                i = PEM_write_bio_X509_CRL(out, x);
                    414:        else {
                    415:                BIO_printf(bio_err,
                    416:                    "bad output format specified for outfile\n");
                    417:                goto end;
                    418:        }
                    419:        if (!i) {
                    420:                BIO_printf(bio_err, "unable to write CRL\n");
                    421:                goto end;
                    422:        }
                    423:        ret = 0;
                    424:
                    425: end:
                    426:        BIO_free_all(out);
                    427:        BIO_free_all(bio_out);
                    428:        bio_out = NULL;
                    429:        X509_CRL_free(x);
                    430:        if (store) {
                    431:                X509_STORE_CTX_cleanup(&ctx);
                    432:                X509_STORE_free(store);
                    433:        }
                    434:
                    435:        return (ret);
                    436: }
                    437:
                    438: static X509_CRL *
                    439: load_crl(char *infile, int format)
                    440: {
                    441:        X509_CRL *x = NULL;
                    442:        BIO *in = NULL;
                    443:
                    444:        in = BIO_new(BIO_s_file());
                    445:        if (in == NULL) {
                    446:                ERR_print_errors(bio_err);
                    447:                goto end;
                    448:        }
                    449:        if (infile == NULL)
                    450:                BIO_set_fp(in, stdin, BIO_NOCLOSE);
                    451:        else {
                    452:                if (BIO_read_filename(in, infile) <= 0) {
                    453:                        perror(infile);
                    454:                        goto end;
                    455:                }
                    456:        }
                    457:        if (format == FORMAT_ASN1)
                    458:                x = d2i_X509_CRL_bio(in, NULL);
                    459:        else if (format == FORMAT_PEM)
                    460:                x = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
                    461:        else {
                    462:                BIO_printf(bio_err,
                    463:                    "bad input format specified for input crl\n");
                    464:                goto end;
                    465:        }
                    466:        if (x == NULL) {
                    467:                BIO_printf(bio_err, "unable to load CRL\n");
                    468:                ERR_print_errors(bio_err);
                    469:                goto end;
                    470:        }
                    471:
                    472: end:
                    473:        BIO_free(in);
                    474:        return (x);
                    475: }