mirror of
https://github.com/openssl/openssl.git
synced 2024-12-27 06:21:43 +08:00
e077455e9e
Since OPENSSL_malloc() and friends report ERR_R_MALLOC_FAILURE, and at least handle the file name and line number they are called from, there's no need to report ERR_R_MALLOC_FAILURE where they are called directly, or when SSLfatal() and RLAYERfatal() is used, the reason `ERR_R_MALLOC_FAILURE` is changed to `ERR_R_CRYPTO_LIB`. There were a number of places where `ERR_R_MALLOC_FAILURE` was reported even though it was a function from a different sub-system that was called. Those places are changed to report ERR_R_{lib}_LIB, where {lib} is the name of that sub-system. Some of them are tricky to get right, as we have a lot of functions that belong in the ASN1 sub-system, and all the `sk_` calls or from the CRYPTO sub-system. Some extra adaptation was necessary where there were custom OPENSSL_malloc() wrappers, and some bugs are fixed alongside these changes. Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Hugo Landau <hlandau@openssl.org> (Merged from https://github.com/openssl/openssl/pull/19301)
103 lines
2.5 KiB
C
103 lines
2.5 KiB
C
/*
|
|
* Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
|
|
*
|
|
* Licensed under the Apache License 2.0 (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
|
|
*/
|
|
|
|
#include "internal/cryptlib.h"
|
|
#include <openssl/asn1t.h>
|
|
#include <openssl/pem.h>
|
|
#include <openssl/x509v3.h>
|
|
#include <openssl/err.h>
|
|
#include <openssl/cms.h>
|
|
#include "cms_local.h"
|
|
|
|
/* CMS DigestedData Utilities */
|
|
|
|
CMS_ContentInfo *ossl_cms_DigestedData_create(const EVP_MD *md,
|
|
OSSL_LIB_CTX *libctx,
|
|
const char *propq)
|
|
{
|
|
CMS_ContentInfo *cms;
|
|
CMS_DigestedData *dd;
|
|
|
|
cms = CMS_ContentInfo_new_ex(libctx, propq);
|
|
if (cms == NULL)
|
|
return NULL;
|
|
|
|
dd = M_ASN1_new_of(CMS_DigestedData);
|
|
|
|
if (dd == NULL)
|
|
goto err;
|
|
|
|
cms->contentType = OBJ_nid2obj(NID_pkcs7_digest);
|
|
cms->d.digestedData = dd;
|
|
|
|
dd->version = 0;
|
|
dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
|
|
|
|
X509_ALGOR_set_md(dd->digestAlgorithm, md);
|
|
|
|
return cms;
|
|
|
|
err:
|
|
CMS_ContentInfo_free(cms);
|
|
return NULL;
|
|
}
|
|
|
|
BIO *ossl_cms_DigestedData_init_bio(const CMS_ContentInfo *cms)
|
|
{
|
|
CMS_DigestedData *dd = cms->d.digestedData;
|
|
|
|
return ossl_cms_DigestAlgorithm_init_bio(dd->digestAlgorithm,
|
|
ossl_cms_get0_cmsctx(cms));
|
|
}
|
|
|
|
int ossl_cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain,
|
|
int verify)
|
|
{
|
|
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
|
unsigned int mdlen;
|
|
int r = 0;
|
|
CMS_DigestedData *dd;
|
|
|
|
if (mctx == NULL) {
|
|
ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB);
|
|
goto err;
|
|
}
|
|
|
|
dd = cms->d.digestedData;
|
|
|
|
if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))
|
|
goto err;
|
|
|
|
if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)
|
|
goto err;
|
|
|
|
if (verify) {
|
|
if (mdlen != (unsigned int)dd->digest->length) {
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_WRONG_LENGTH);
|
|
goto err;
|
|
}
|
|
|
|
if (memcmp(md, dd->digest->data, mdlen))
|
|
ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
|
|
else
|
|
r = 1;
|
|
} else {
|
|
if (!ASN1_STRING_set(dd->digest, md, mdlen))
|
|
goto err;
|
|
r = 1;
|
|
}
|
|
|
|
err:
|
|
EVP_MD_CTX_free(mctx);
|
|
|
|
return r;
|
|
|
|
}
|