[331] | 1 | /*
|
---|
| 2 | * Copyright 1995-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/err.h>
|
---|
| 16 | #include <openssl/objects.h>
|
---|
| 17 | #include <openssl/evp.h>
|
---|
| 18 | #include <openssl/x509.h>
|
---|
| 19 | #include <openssl/pkcs7.h>
|
---|
| 20 | #include <openssl/pem.h>
|
---|
| 21 |
|
---|
| 22 | typedef enum OPTION_choice {
|
---|
| 23 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
|
---|
| 24 | OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_NOOUT,
|
---|
| 25 | OPT_TEXT, OPT_PRINT, OPT_PRINT_CERTS, OPT_ENGINE
|
---|
| 26 | } OPTION_CHOICE;
|
---|
| 27 |
|
---|
| 28 | OPTIONS pkcs7_options[] = {
|
---|
| 29 | {"help", OPT_HELP, '-', "Display this summary"},
|
---|
| 30 | {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
|
---|
| 31 | {"in", OPT_IN, '<', "Input file"},
|
---|
| 32 | {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
|
---|
| 33 | {"out", OPT_OUT, '>', "Output file"},
|
---|
| 34 | {"noout", OPT_NOOUT, '-', "Don't output encoded data"},
|
---|
| 35 | {"text", OPT_TEXT, '-', "Print full details of certificates"},
|
---|
| 36 | {"print", OPT_PRINT, '-', "Print out all fields of the PKCS7 structure"},
|
---|
| 37 | {"print_certs", OPT_PRINT_CERTS, '-',
|
---|
| 38 | "Print_certs print any certs or crl in the input"},
|
---|
| 39 | #ifndef OPENSSL_NO_ENGINE
|
---|
| 40 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
|
---|
| 41 | #endif
|
---|
| 42 | {NULL}
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | int pkcs7_main(int argc, char **argv)
|
---|
| 46 | {
|
---|
| 47 | ENGINE *e = NULL;
|
---|
| 48 | PKCS7 *p7 = NULL;
|
---|
| 49 | BIO *in = NULL, *out = NULL;
|
---|
| 50 | int informat = FORMAT_PEM, outformat = FORMAT_PEM;
|
---|
| 51 | char *infile = NULL, *outfile = NULL, *prog;
|
---|
| 52 | int i, print_certs = 0, text = 0, noout = 0, p7_print = 0, ret = 1;
|
---|
| 53 | OPTION_CHOICE o;
|
---|
| 54 |
|
---|
| 55 | prog = opt_init(argc, argv, pkcs7_options);
|
---|
| 56 | while ((o = opt_next()) != OPT_EOF) {
|
---|
| 57 | switch (o) {
|
---|
| 58 | case OPT_EOF:
|
---|
| 59 | case OPT_ERR:
|
---|
| 60 | opthelp:
|
---|
| 61 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
|
---|
| 62 | goto end;
|
---|
| 63 | case OPT_HELP:
|
---|
| 64 | opt_help(pkcs7_options);
|
---|
| 65 | ret = 0;
|
---|
| 66 | goto end;
|
---|
| 67 | case OPT_INFORM:
|
---|
| 68 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
|
---|
| 69 | goto opthelp;
|
---|
| 70 | break;
|
---|
| 71 | case OPT_OUTFORM:
|
---|
| 72 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
|
---|
| 73 | goto opthelp;
|
---|
| 74 | break;
|
---|
| 75 | case OPT_IN:
|
---|
| 76 | infile = opt_arg();
|
---|
| 77 | break;
|
---|
| 78 | case OPT_OUT:
|
---|
| 79 | outfile = opt_arg();
|
---|
| 80 | break;
|
---|
| 81 | case OPT_NOOUT:
|
---|
| 82 | noout = 1;
|
---|
| 83 | break;
|
---|
| 84 | case OPT_TEXT:
|
---|
| 85 | text = 1;
|
---|
| 86 | break;
|
---|
| 87 | case OPT_PRINT:
|
---|
| 88 | p7_print = 1;
|
---|
| 89 | break;
|
---|
| 90 | case OPT_PRINT_CERTS:
|
---|
| 91 | print_certs = 1;
|
---|
| 92 | break;
|
---|
| 93 | case OPT_ENGINE:
|
---|
| 94 | e = setup_engine(opt_arg(), 0);
|
---|
| 95 | break;
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 | argc = opt_num_rest();
|
---|
| 99 | if (argc != 0)
|
---|
| 100 | goto opthelp;
|
---|
| 101 |
|
---|
| 102 | in = bio_open_default(infile, 'r', informat);
|
---|
| 103 | if (in == NULL)
|
---|
| 104 | goto end;
|
---|
| 105 |
|
---|
| 106 | if (informat == FORMAT_ASN1)
|
---|
| 107 | p7 = d2i_PKCS7_bio(in, NULL);
|
---|
| 108 | else
|
---|
| 109 | p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
|
---|
| 110 | if (p7 == NULL) {
|
---|
| 111 | BIO_printf(bio_err, "unable to load PKCS7 object\n");
|
---|
| 112 | ERR_print_errors(bio_err);
|
---|
| 113 | goto end;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | out = bio_open_default(outfile, 'w', outformat);
|
---|
| 117 | if (out == NULL)
|
---|
| 118 | goto end;
|
---|
| 119 |
|
---|
| 120 | if (p7_print)
|
---|
| 121 | PKCS7_print_ctx(out, p7, 0, NULL);
|
---|
| 122 |
|
---|
| 123 | if (print_certs) {
|
---|
| 124 | STACK_OF(X509) *certs = NULL;
|
---|
| 125 | STACK_OF(X509_CRL) *crls = NULL;
|
---|
| 126 |
|
---|
| 127 | i = OBJ_obj2nid(p7->type);
|
---|
| 128 | switch (i) {
|
---|
| 129 | case NID_pkcs7_signed:
|
---|
| 130 | if (p7->d.sign != NULL) {
|
---|
| 131 | certs = p7->d.sign->cert;
|
---|
| 132 | crls = p7->d.sign->crl;
|
---|
| 133 | }
|
---|
| 134 | break;
|
---|
| 135 | case NID_pkcs7_signedAndEnveloped:
|
---|
| 136 | if (p7->d.signed_and_enveloped != NULL) {
|
---|
| 137 | certs = p7->d.signed_and_enveloped->cert;
|
---|
| 138 | crls = p7->d.signed_and_enveloped->crl;
|
---|
| 139 | }
|
---|
| 140 | break;
|
---|
| 141 | default:
|
---|
| 142 | break;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (certs != NULL) {
|
---|
| 146 | X509 *x;
|
---|
| 147 |
|
---|
| 148 | for (i = 0; i < sk_X509_num(certs); i++) {
|
---|
| 149 | x = sk_X509_value(certs, i);
|
---|
| 150 | if (text)
|
---|
| 151 | X509_print(out, x);
|
---|
| 152 | else
|
---|
| 153 | dump_cert_text(out, x);
|
---|
| 154 |
|
---|
| 155 | if (!noout)
|
---|
| 156 | PEM_write_bio_X509(out, x);
|
---|
| 157 | BIO_puts(out, "\n");
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | if (crls != NULL) {
|
---|
| 161 | X509_CRL *crl;
|
---|
| 162 |
|
---|
| 163 | for (i = 0; i < sk_X509_CRL_num(crls); i++) {
|
---|
| 164 | crl = sk_X509_CRL_value(crls, i);
|
---|
| 165 |
|
---|
| 166 | X509_CRL_print(out, crl);
|
---|
| 167 |
|
---|
| 168 | if (!noout)
|
---|
| 169 | PEM_write_bio_X509_CRL(out, crl);
|
---|
| 170 | BIO_puts(out, "\n");
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | ret = 0;
|
---|
| 175 | goto end;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | if (!noout) {
|
---|
| 179 | if (outformat == FORMAT_ASN1)
|
---|
| 180 | i = i2d_PKCS7_bio(out, p7);
|
---|
| 181 | else
|
---|
| 182 | i = PEM_write_bio_PKCS7(out, p7);
|
---|
| 183 |
|
---|
| 184 | if (!i) {
|
---|
| 185 | BIO_printf(bio_err, "unable to write pkcs7 object\n");
|
---|
| 186 | ERR_print_errors(bio_err);
|
---|
| 187 | goto end;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | ret = 0;
|
---|
| 191 | end:
|
---|
| 192 | PKCS7_free(p7);
|
---|
| 193 | release_engine(e);
|
---|
| 194 | BIO_free(in);
|
---|
| 195 | BIO_free_all(out);
|
---|
| 196 | return (ret);
|
---|
| 197 | }
|
---|