/src/openssl111/crypto/cms/cms_dd.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the OpenSSL license (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 *cms_DigestedData_create(const EVP_MD *md) | 
| 21 | 0 | { | 
| 22 | 0 |     CMS_ContentInfo *cms; | 
| 23 | 0 |     CMS_DigestedData *dd; | 
| 24 | 0 |     cms = CMS_ContentInfo_new(); | 
| 25 | 0 |     if (cms == NULL) | 
| 26 | 0 |         return NULL; | 
| 27 |  |  | 
| 28 | 0 |     dd = M_ASN1_new_of(CMS_DigestedData); | 
| 29 |  | 
 | 
| 30 | 0 |     if (dd == NULL) | 
| 31 | 0 |         goto err; | 
| 32 |  |  | 
| 33 | 0 |     cms->contentType = OBJ_nid2obj(NID_pkcs7_digest); | 
| 34 | 0 |     cms->d.digestedData = dd; | 
| 35 |  | 
 | 
| 36 | 0 |     dd->version = 0; | 
| 37 | 0 |     dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data); | 
| 38 |  | 
 | 
| 39 | 0 |     X509_ALGOR_set_md(dd->digestAlgorithm, md); | 
| 40 |  | 
 | 
| 41 | 0 |     return cms; | 
| 42 |  |  | 
| 43 | 0 |  err: | 
| 44 | 0 |     CMS_ContentInfo_free(cms); | 
| 45 | 0 |     return NULL; | 
| 46 | 0 | } | 
| 47 |  |  | 
| 48 |  | BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms) | 
| 49 | 0 | { | 
| 50 | 0 |     CMS_DigestedData *dd; | 
| 51 | 0 |     dd = cms->d.digestedData; | 
| 52 | 0 |     return cms_DigestAlgorithm_init_bio(dd->digestAlgorithm); | 
| 53 | 0 | } | 
| 54 |  |  | 
| 55 |  | int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify) | 
| 56 | 0 | { | 
| 57 | 0 |     EVP_MD_CTX *mctx = EVP_MD_CTX_new(); | 
| 58 | 0 |     unsigned char md[EVP_MAX_MD_SIZE]; | 
| 59 | 0 |     unsigned int mdlen; | 
| 60 | 0 |     int r = 0; | 
| 61 | 0 |     CMS_DigestedData *dd; | 
| 62 |  | 
 | 
| 63 | 0 |     if (mctx == NULL) { | 
| 64 | 0 |         CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE); | 
| 65 | 0 |         goto err; | 
| 66 | 0 |     } | 
| 67 |  |  | 
| 68 | 0 |     dd = cms->d.digestedData; | 
| 69 |  | 
 | 
| 70 | 0 |     if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm)) | 
| 71 | 0 |         goto err; | 
| 72 |  |  | 
| 73 | 0 |     if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0) | 
| 74 | 0 |         goto err; | 
| 75 |  |  | 
| 76 | 0 |     if (verify) { | 
| 77 | 0 |         if (mdlen != (unsigned int)dd->digest->length) { | 
| 78 | 0 |             CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, | 
| 79 | 0 |                    CMS_R_MESSAGEDIGEST_WRONG_LENGTH); | 
| 80 | 0 |             goto err; | 
| 81 | 0 |         } | 
| 82 |  |  | 
| 83 | 0 |         if (memcmp(md, dd->digest->data, mdlen)) | 
| 84 | 0 |             CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, | 
| 85 | 0 |                    CMS_R_VERIFICATION_FAILURE); | 
| 86 | 0 |         else | 
| 87 | 0 |             r = 1; | 
| 88 | 0 |     } else { | 
| 89 | 0 |         if (!ASN1_STRING_set(dd->digest, md, mdlen)) | 
| 90 | 0 |             goto err; | 
| 91 | 0 |         r = 1; | 
| 92 | 0 |     } | 
| 93 |  |  | 
| 94 | 0 |  err: | 
| 95 | 0 |     EVP_MD_CTX_free(mctx); | 
| 96 |  | 
 | 
| 97 | 0 |     return r; | 
| 98 |  | 
 | 
| 99 | 0 | } |