]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/cms/cms_dd.c
Run the withlibctx.pl script
[thirdparty/openssl.git] / crypto / cms / cms_dd.c
CommitLineData
0f113f3e 1/*
b1322259 2 * Copyright 2008-2016 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
c1669f41
SL
20CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md,
21 OPENSSL_CTX *libctx, const char *propq)
0f113f3e
MC
22{
23 CMS_ContentInfo *cms;
24 CMS_DigestedData *dd;
c1669f41 25
d8652be0 26 cms = CMS_ContentInfo_new_ex(libctx, propq);
90945fa3 27 if (cms == NULL)
0f113f3e 28 return NULL;
8931b30d 29
0f113f3e 30 dd = M_ASN1_new_of(CMS_DigestedData);
8931b30d 31
90945fa3 32 if (dd == NULL)
0f113f3e 33 goto err;
8931b30d 34
0f113f3e
MC
35 cms->contentType = OBJ_nid2obj(NID_pkcs7_digest);
36 cms->d.digestedData = dd;
8931b30d 37
0f113f3e
MC
38 dd->version = 0;
39 dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
8931b30d 40
0f113f3e 41 X509_ALGOR_set_md(dd->digestAlgorithm, md);
8931b30d 42
0f113f3e 43 return cms;
8931b30d 44
0f113f3e 45 err:
25aaa98a 46 CMS_ContentInfo_free(cms);
0f113f3e
MC
47 return NULL;
48}
8931b30d 49
9fdcc21f 50BIO *cms_DigestedData_init_bio(const CMS_ContentInfo *cms)
0f113f3e 51{
c1669f41
SL
52 CMS_DigestedData *dd = cms->d.digestedData;
53
54 return cms_DigestAlgorithm_init_bio(dd->digestAlgorithm, cms_get0_cmsctx(cms));
0f113f3e 55}
8931b30d 56
9fdcc21f 57int cms_DigestedData_do_final(const CMS_ContentInfo *cms, BIO *chain, int verify)
0f113f3e 58{
bfb0641f 59 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
0f113f3e
MC
60 unsigned char md[EVP_MAX_MD_SIZE];
61 unsigned int mdlen;
62 int r = 0;
63 CMS_DigestedData *dd;
6e59a892
RL
64
65 if (mctx == NULL) {
66 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE);
67 goto err;
68 }
0f113f3e
MC
69
70 dd = cms->d.digestedData;
71
6e59a892 72 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))
0f113f3e
MC
73 goto err;
74
6e59a892 75 if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)
0f113f3e
MC
76 goto err;
77
78 if (verify) {
79 if (mdlen != (unsigned int)dd->digest->length) {
80 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
81 CMS_R_MESSAGEDIGEST_WRONG_LENGTH);
82 goto err;
83 }
84
85 if (memcmp(md, dd->digest->data, mdlen))
86 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
87 CMS_R_VERIFICATION_FAILURE);
88 else
89 r = 1;
90 } else {
91 if (!ASN1_STRING_set(dd->digest, md, mdlen))
92 goto err;
93 r = 1;
94 }
95
96 err:
bfb0641f 97 EVP_MD_CTX_free(mctx);
0f113f3e
MC
98
99 return r;
100
101}