2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2008-03-12 21:14:28 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* 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-12 21:14:28 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2008-03-12 21:14:28 +00:00
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/x509v3.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/cms.h>
|
2019-09-27 21:58:06 +00:00
|
|
|
#include "cms_local.h"
|
2008-03-12 21:14:28 +00:00
|
|
|
|
|
|
|
/* CMS DigestedData Utilities */
|
|
|
|
|
|
|
|
CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
CMS_ContentInfo *cms;
|
|
|
|
CMS_DigestedData *dd;
|
|
|
|
cms = CMS_ContentInfo_new();
|
2015-10-30 11:12:26 +00:00
|
|
|
if (cms == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
2008-03-12 21:14:28 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
dd = M_ASN1_new_of(CMS_DigestedData);
|
2008-03-12 21:14:28 +00:00
|
|
|
|
2015-10-30 11:12:26 +00:00
|
|
|
if (dd == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2008-03-12 21:14:28 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
cms->contentType = OBJ_nid2obj(NID_pkcs7_digest);
|
|
|
|
cms->d.digestedData = dd;
|
2008-03-12 21:14:28 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
dd->version = 0;
|
|
|
|
dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
|
2008-03-12 21:14:28 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
X509_ALGOR_set_md(dd->digestAlgorithm, md);
|
2008-03-12 21:14:28 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
return cms;
|
2008-03-12 21:14:28 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
err:
|
2015-05-01 18:37:16 +00:00
|
|
|
CMS_ContentInfo_free(cms);
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-03-12 21:14:28 +00:00
|
|
|
|
|
|
|
BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
CMS_DigestedData *dd;
|
|
|
|
dd = cms->d.digestedData;
|
|
|
|
return cms_DigestAlgorithm_init_bio(dd->digestAlgorithm);
|
|
|
|
}
|
2008-03-12 21:14:28 +00:00
|
|
|
|
|
|
|
int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int mdlen;
|
|
|
|
int r = 0;
|
|
|
|
CMS_DigestedData *dd;
|
2015-11-27 13:02:12 +00:00
|
|
|
|
|
|
|
if (mctx == NULL) {
|
|
|
|
CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
dd = cms->d.digestedData;
|
|
|
|
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
|
2015-11-27 13:02:12 +00:00
|
|
|
if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)
|
2015-01-22 03:40:55 +00: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-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(mctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|