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