Path: blob/main/crypto/openssl/demos/cms/cms_ver.c
34879 views
/*1* Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/* Simple S/MIME verification example */10#include <openssl/pem.h>11#include <openssl/cms.h>12#include <openssl/err.h>1314/*15* print any signingTime attributes.16* signingTime is when each party purportedly signed the message.17*/18static void print_signingTime(CMS_ContentInfo *cms)19{20STACK_OF(CMS_SignerInfo) *sis;21CMS_SignerInfo *si;22X509_ATTRIBUTE *attr;23ASN1_TYPE *t;24ASN1_UTCTIME *utctime;25ASN1_GENERALIZEDTIME *gtime;26BIO *b;27int i, loc;2829b = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);30sis = CMS_get0_SignerInfos(cms);31for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {32si = sk_CMS_SignerInfo_value(sis, i);33loc = CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1);34attr = CMS_signed_get_attr(si, loc);35t = X509_ATTRIBUTE_get0_type(attr, 0);36if (t == NULL)37continue;38switch (t->type) {39case V_ASN1_UTCTIME:40utctime = t->value.utctime;41ASN1_UTCTIME_print(b, utctime);42break;43case V_ASN1_GENERALIZEDTIME:44gtime = t->value.generalizedtime;45ASN1_GENERALIZEDTIME_print(b, gtime);46break;47default:48fprintf(stderr, "unrecognized signingTime type\n");49break;50}51BIO_printf(b, ": signingTime from SignerInfo %i\n", i);52}53BIO_free(b);54return;55}5657int main(int argc, char **argv)58{59BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;60X509_STORE *st = NULL;61X509 *cacert = NULL;62CMS_ContentInfo *cms = NULL;63int ret = EXIT_FAILURE;6465OpenSSL_add_all_algorithms();66ERR_load_crypto_strings();6768/* Set up trusted CA certificate store */6970st = X509_STORE_new();71if (st == NULL)72goto err;7374/* Read in CA certificate */75tbio = BIO_new_file("cacert.pem", "r");7677if (tbio == NULL)78goto err;7980cacert = PEM_read_bio_X509(tbio, NULL, 0, NULL);8182if (cacert == NULL)83goto err;8485if (!X509_STORE_add_cert(st, cacert))86goto err;8788/* Open message being verified */8990in = BIO_new_file("smout.txt", "r");9192if (in == NULL)93goto err;9495/* parse message */96cms = SMIME_read_CMS(in, &cont);9798if (cms == NULL)99goto err;100101print_signingTime(cms);102103/* File to output verified content to */104out = BIO_new_file("smver.txt", "w");105if (out == NULL)106goto err;107108if (!CMS_verify(cms, NULL, st, cont, out, 0)) {109fprintf(stderr, "Verification Failure\n");110goto err;111}112113printf("Verification Successful\n");114115ret = EXIT_SUCCESS;116117err:118if (ret != EXIT_SUCCESS) {119fprintf(stderr, "Error Verifying Data\n");120ERR_print_errors_fp(stderr);121}122123X509_STORE_free(st);124CMS_ContentInfo_free(cms);125X509_free(cacert);126BIO_free(in);127BIO_free(out);128BIO_free(tbio);129return ret;130}131132133