2 * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
18 /* CMS DigestedData Utilities */
20 CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md)
24 cms = CMS_ContentInfo_new();
28 dd = M_ASN1_new_of(CMS_DigestedData);
33 cms->contentType = OBJ_nid2obj(NID_pkcs7_digest);
34 cms->d.digestedData = dd;
37 dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
39 X509_ALGOR_set_md(dd->digestAlgorithm, md);
44 CMS_ContentInfo_free(cms);
48 BIO *cms_DigestedData_init_bio(const CMS_ContentInfo *cms)
51 dd = cms->d.digestedData;
52 return cms_DigestAlgorithm_init_bio(dd->digestAlgorithm);
55 int cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain, int verify)
57 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
58 unsigned char md[EVP_MAX_MD_SIZE];
64 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE);
68 dd = cms->d.digestedData;
70 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))
73 if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)
77 if (mdlen != (unsigned int)dd->digest->length) {
78 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
79 CMS_R_MESSAGEDIGEST_WRONG_LENGTH);
83 if (memcmp(md, dd->digest->data, mdlen))
84 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
85 CMS_R_VERIFICATION_FAILURE);
89 if (!ASN1_STRING_set(dd->digest, md, mdlen))
95 EVP_MD_CTX_free(mctx);