1183234Ssimon/* crypto/cms/cms_dd.c */
2296465Sdelphij/*
3296465Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4183234Ssimon * project.
5183234Ssimon */
6183234Ssimon/* ====================================================================
7183234Ssimon * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
8183234Ssimon *
9183234Ssimon * Redistribution and use in source and binary forms, with or without
10183234Ssimon * modification, are permitted provided that the following conditions
11183234Ssimon * are met:
12183234Ssimon *
13183234Ssimon * 1. Redistributions of source code must retain the above copyright
14296465Sdelphij *    notice, this list of conditions and the following disclaimer.
15183234Ssimon *
16183234Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17183234Ssimon *    notice, this list of conditions and the following disclaimer in
18183234Ssimon *    the documentation and/or other materials provided with the
19183234Ssimon *    distribution.
20183234Ssimon *
21183234Ssimon * 3. All advertising materials mentioning features or use of this
22183234Ssimon *    software must display the following acknowledgment:
23183234Ssimon *    "This product includes software developed by the OpenSSL Project
24183234Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25183234Ssimon *
26183234Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27183234Ssimon *    endorse or promote products derived from this software without
28183234Ssimon *    prior written permission. For written permission, please contact
29183234Ssimon *    licensing@OpenSSL.org.
30183234Ssimon *
31183234Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32183234Ssimon *    nor may "OpenSSL" appear in their names without prior written
33183234Ssimon *    permission of the OpenSSL Project.
34183234Ssimon *
35183234Ssimon * 6. Redistributions of any form whatsoever must retain the following
36183234Ssimon *    acknowledgment:
37183234Ssimon *    "This product includes software developed by the OpenSSL Project
38183234Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39183234Ssimon *
40183234Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41183234Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42183234Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43183234Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44183234Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45183234Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46183234Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47183234Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48183234Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49183234Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50183234Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51183234Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52183234Ssimon * ====================================================================
53183234Ssimon */
54183234Ssimon
55183234Ssimon#include "cryptlib.h"
56183234Ssimon#include <openssl/asn1t.h>
57183234Ssimon#include <openssl/pem.h>
58183234Ssimon#include <openssl/x509v3.h>
59183234Ssimon#include <openssl/err.h>
60183234Ssimon#include <openssl/cms.h>
61183234Ssimon#include "cms_lcl.h"
62183234Ssimon
63183234SsimonDECLARE_ASN1_ITEM(CMS_DigestedData)
64183234Ssimon
65183234Ssimon/* CMS DigestedData Utilities */
66183234Ssimon
67183234SsimonCMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md)
68296465Sdelphij{
69296465Sdelphij    CMS_ContentInfo *cms;
70296465Sdelphij    CMS_DigestedData *dd;
71296465Sdelphij    cms = CMS_ContentInfo_new();
72296465Sdelphij    if (!cms)
73296465Sdelphij        return NULL;
74183234Ssimon
75296465Sdelphij    dd = M_ASN1_new_of(CMS_DigestedData);
76183234Ssimon
77296465Sdelphij    if (!dd)
78296465Sdelphij        goto err;
79183234Ssimon
80296465Sdelphij    cms->contentType = OBJ_nid2obj(NID_pkcs7_digest);
81296465Sdelphij    cms->d.digestedData = dd;
82183234Ssimon
83296465Sdelphij    dd->version = 0;
84296465Sdelphij    dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
85183234Ssimon
86296465Sdelphij    cms_DigestAlgorithm_set(dd->digestAlgorithm, md);
87183234Ssimon
88296465Sdelphij    return cms;
89183234Ssimon
90296465Sdelphij err:
91183234Ssimon
92296465Sdelphij    if (cms)
93296465Sdelphij        CMS_ContentInfo_free(cms);
94183234Ssimon
95296465Sdelphij    return NULL;
96296465Sdelphij}
97183234Ssimon
98183234SsimonBIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms)
99296465Sdelphij{
100296465Sdelphij    CMS_DigestedData *dd;
101296465Sdelphij    dd = cms->d.digestedData;
102296465Sdelphij    return cms_DigestAlgorithm_init_bio(dd->digestAlgorithm);
103296465Sdelphij}
104183234Ssimon
105183234Ssimonint cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify)
106296465Sdelphij{
107296465Sdelphij    EVP_MD_CTX mctx;
108296465Sdelphij    unsigned char md[EVP_MAX_MD_SIZE];
109296465Sdelphij    unsigned int mdlen;
110296465Sdelphij    int r = 0;
111296465Sdelphij    CMS_DigestedData *dd;
112296465Sdelphij    EVP_MD_CTX_init(&mctx);
113183234Ssimon
114296465Sdelphij    dd = cms->d.digestedData;
115183234Ssimon
116296465Sdelphij    if (!cms_DigestAlgorithm_find_ctx(&mctx, chain, dd->digestAlgorithm))
117296465Sdelphij        goto err;
118183234Ssimon
119296465Sdelphij    if (EVP_DigestFinal_ex(&mctx, md, &mdlen) <= 0)
120296465Sdelphij        goto err;
121183234Ssimon
122296465Sdelphij    if (verify) {
123296465Sdelphij        if (mdlen != (unsigned int)dd->digest->length) {
124296465Sdelphij            CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
125296465Sdelphij                   CMS_R_MESSAGEDIGEST_WRONG_LENGTH);
126296465Sdelphij            goto err;
127296465Sdelphij        }
128183234Ssimon
129296465Sdelphij        if (memcmp(md, dd->digest->data, mdlen))
130296465Sdelphij            CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
131296465Sdelphij                   CMS_R_VERIFICATION_FAILURE);
132296465Sdelphij        else
133296465Sdelphij            r = 1;
134296465Sdelphij    } else {
135296465Sdelphij        if (!ASN1_STRING_set(dd->digest, md, mdlen))
136296465Sdelphij            goto err;
137296465Sdelphij        r = 1;
138296465Sdelphij    }
139183234Ssimon
140296465Sdelphij err:
141296465Sdelphij    EVP_MD_CTX_cleanup(&mctx);
142183234Ssimon
143296465Sdelphij    return r;
144183234Ssimon
145296465Sdelphij}
146