]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
8020d79b | 2 | * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved. |
8931b30d | 3 | * |
08ddd302 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
8931b30d DSH |
8 | */ |
9 | ||
b39fc560 | 10 | #include "internal/cryptlib.h" |
8931b30d DSH |
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> | |
706457b7 | 16 | #include "cms_local.h" |
8931b30d | 17 | |
8931b30d DSH |
18 | /* CMS DigestedData Utilities */ |
19 | ||
53155f1c SL |
20 | CMS_ContentInfo *ossl_cms_DigestedData_create(const EVP_MD *md, |
21 | OSSL_LIB_CTX *libctx, | |
22 | const char *propq) | |
0f113f3e MC |
23 | { |
24 | CMS_ContentInfo *cms; | |
25 | CMS_DigestedData *dd; | |
c1669f41 | 26 | |
d8652be0 | 27 | cms = CMS_ContentInfo_new_ex(libctx, propq); |
90945fa3 | 28 | if (cms == NULL) |
0f113f3e | 29 | return NULL; |
8931b30d | 30 | |
0f113f3e | 31 | dd = M_ASN1_new_of(CMS_DigestedData); |
8931b30d | 32 | |
90945fa3 | 33 | if (dd == NULL) |
0f113f3e | 34 | goto err; |
8931b30d | 35 | |
0f113f3e MC |
36 | cms->contentType = OBJ_nid2obj(NID_pkcs7_digest); |
37 | cms->d.digestedData = dd; | |
8931b30d | 38 | |
0f113f3e MC |
39 | dd->version = 0; |
40 | dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data); | |
8931b30d | 41 | |
0f113f3e | 42 | X509_ALGOR_set_md(dd->digestAlgorithm, md); |
8931b30d | 43 | |
0f113f3e | 44 | return cms; |
8931b30d | 45 | |
0f113f3e | 46 | err: |
25aaa98a | 47 | CMS_ContentInfo_free(cms); |
0f113f3e MC |
48 | return NULL; |
49 | } | |
8931b30d | 50 | |
53155f1c | 51 | BIO *ossl_cms_DigestedData_init_bio(const CMS_ContentInfo *cms) |
0f113f3e | 52 | { |
c1669f41 SL |
53 | CMS_DigestedData *dd = cms->d.digestedData; |
54 | ||
53155f1c SL |
55 | return ossl_cms_DigestAlgorithm_init_bio(dd->digestAlgorithm, |
56 | ossl_cms_get0_cmsctx(cms)); | |
0f113f3e | 57 | } |
8931b30d | 58 | |
53155f1c SL |
59 | int ossl_cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain, |
60 | int verify) | |
0f113f3e | 61 | { |
bfb0641f | 62 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
0f113f3e MC |
63 | unsigned char md[EVP_MAX_MD_SIZE]; |
64 | unsigned int mdlen; | |
65 | int r = 0; | |
66 | CMS_DigestedData *dd; | |
6e59a892 RL |
67 | |
68 | if (mctx == NULL) { | |
e077455e | 69 | ERR_raise(ERR_LIB_CMS, ERR_R_EVP_LIB); |
6e59a892 RL |
70 | goto err; |
71 | } | |
0f113f3e MC |
72 | |
73 | dd = cms->d.digestedData; | |
74 | ||
53155f1c | 75 | if (!ossl_cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm)) |
0f113f3e MC |
76 | goto err; |
77 | ||
6e59a892 | 78 | if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0) |
0f113f3e MC |
79 | goto err; |
80 | ||
81 | if (verify) { | |
82 | if (mdlen != (unsigned int)dd->digest->length) { | |
9311d0c4 | 83 | ERR_raise(ERR_LIB_CMS, CMS_R_MESSAGEDIGEST_WRONG_LENGTH); |
0f113f3e MC |
84 | goto err; |
85 | } | |
86 | ||
87 | if (memcmp(md, dd->digest->data, mdlen)) | |
9311d0c4 | 88 | ERR_raise(ERR_LIB_CMS, CMS_R_VERIFICATION_FAILURE); |
0f113f3e MC |
89 | else |
90 | r = 1; | |
91 | } else { | |
92 | if (!ASN1_STRING_set(dd->digest, md, mdlen)) | |
93 | goto err; | |
94 | r = 1; | |
95 | } | |
96 | ||
97 | err: | |
bfb0641f | 98 | EVP_MD_CTX_free(mctx); |
0f113f3e MC |
99 | |
100 | return r; | |
101 | ||
102 | } |