Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/cms/cms_dd.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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
8
 */
9
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>
16
#include "cms_local.h"
17
18
/* CMS DigestedData Utilities */
19
20
CMS_ContentInfo *ossl_cms_DigestedData_create(const EVP_MD *md,
21
                                              OSSL_LIB_CTX *libctx,
22
                                              const char *propq)
23
0
{
24
0
    CMS_ContentInfo *cms;
25
0
    CMS_DigestedData *dd;
26
27
0
    cms = CMS_ContentInfo_new_ex(libctx, propq);
28
0
    if (cms == NULL)
29
0
        return NULL;
30
31
0
    dd = M_ASN1_new_of(CMS_DigestedData);
32
33
0
    if (dd == NULL)
34
0
        goto err;
35
36
0
    cms->contentType = OBJ_nid2obj(NID_pkcs7_digest);
37
0
    cms->d.digestedData = dd;
38
39
0
    dd->version = 0;
40
0
    dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
41
42
0
    X509_ALGOR_set_md(dd->digestAlgorithm, md);
43
44
0
    return cms;
45
46
0
 err:
47
0
    CMS_ContentInfo_free(cms);
48
0
    return NULL;
49
0
}
50
51
BIO *ossl_cms_DigestedData_init_bio(const CMS_ContentInfo *cms)
52
0
{
53
0
    CMS_DigestedData *dd = cms->d.digestedData;
54
55
0
    return ossl_cms_DigestAlgorithm_init_bio(dd->digestAlgorithm,
56
0
                                             ossl_cms_get0_cmsctx(cms));
57
0
}
58
59
int ossl_cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain,
60
                                   int verify)
61
0
{
62
0
    EVP_MD_CTX *mctx = EVP_MD_CTX_new();
63
0
    unsigned char md[EVP_MAX_MD_SIZE];
64
0
    unsigned int mdlen;
65
0
    int r = 0;
66
0
    CMS_DigestedData *dd;
67
68
0
    if (mctx == NULL) {
69
0
        ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
70
0
        goto err;
71
0
    }
72
73
0
    dd = cms->d.digestedData;
74
75
0
    if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))
76
0
        goto err;
77
78
0
    if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)
79
0
        goto err;
80
81
0
    if (verify) {
82
0
        if (mdlen != (unsigned int)dd->digest->length) {
83
0
            ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_WRONG_LENGTH);
84
0
            goto err;
85
0
        }
86
87
0
        if (memcmp(md, dd->digest->data, mdlen))
88
0
            ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE);
89
0
        else
90
0
            r = 1;
91
0
    } else {
92
0
        if (!ASN1_STRING_set(dd->digest, md, mdlen))
93
0
            goto err;
94
0
        r = 1;
95
0
    }
96
97
0
 err:
98
0
    EVP_MD_CTX_free(mctx);
99
100
0
    return r;
101
102
0
}