2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2016-05-18 02:52:22 +08:00
|
|
|
* Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2008-03-13 05:14:28 +08:00
|
|
|
*
|
2018-12-06 20:32:50 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2008-03-13 05:14:28 +08:00
|
|
|
*/
|
|
|
|
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2008-03-13 05:14:28 +08:00
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/x509v3.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/cms.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "cms_local.h"
|
2008-03-13 05:14:28 +08:00
|
|
|
|
|
|
|
/* CMS DigestedData Utilities */
|
|
|
|
|
|
|
|
CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
CMS_ContentInfo *cms;
|
|
|
|
CMS_DigestedData *dd;
|
|
|
|
cms = CMS_ContentInfo_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (cms == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
dd = M_ASN1_new_of(CMS_DigestedData);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-10-30 19:12:26 +08:00
|
|
|
if (dd == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
cms->contentType = OBJ_nid2obj(NID_pkcs7_digest);
|
|
|
|
cms->d.digestedData = dd;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
dd->version = 0;
|
|
|
|
dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
X509_ALGOR_set_md(dd->digestAlgorithm, md);
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
return cms;
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-05-02 02:37:16 +08:00
|
|
|
CMS_ContentInfo_free(cms);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
BIO *cms_DigestedData_init_bio(const CMS_ContentInfo *cms)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
CMS_DigestedData *dd;
|
|
|
|
dd = cms->d.digestedData;
|
|
|
|
return cms_DigestAlgorithm_init_bio(dd->digestAlgorithm);
|
|
|
|
}
|
2008-03-13 05:14:28 +08:00
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
int cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain, int verify)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
|
2015-01-22 11:40:55 +08:00
|
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int mdlen;
|
|
|
|
int r = 0;
|
|
|
|
CMS_DigestedData *dd;
|
2015-11-27 21:02:12 +08:00
|
|
|
|
|
|
|
if (mctx == NULL) {
|
|
|
|
CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
dd = cms->d.digestedData;
|
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
2015-11-27 21:02:12 +08:00
|
|
|
if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (verify) {
|
|
|
|
if (mdlen != (unsigned int)dd->digest->length) {
|
|
|
|
CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
|
|
|
|
CMS_R_MESSAGEDIGEST_WRONG_LENGTH);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(md, dd->digest->data, mdlen))
|
|
|
|
CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
|
|
|
|
CMS_R_VERIFICATION_FAILURE);
|
|
|
|
else
|
|
|
|
r = 1;
|
|
|
|
} else {
|
|
|
|
if (!ASN1_STRING_set(dd->digest, md, mdlen))
|
|
|
|
goto err;
|
|
|
|
r = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2015-12-02 07:49:35 +08:00
|
|
|
EVP_MD_CTX_free(mctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|