2 * Copyright 2008-2019 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/pem.h>
13 #include <openssl/rand.h>
14 #include <openssl/x509v3.h>
15 #include <openssl/err.h>
16 #include <openssl/cms.h>
17 #include <openssl/ess.h>
19 #include "internal/ess_int.h"
21 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
25 int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
28 CMS_ReceiptRequest *rr = NULL;
31 str = CMS_signed_get0_data_by_OBJ(si,
33 (NID_id_smime_aa_receiptRequest), -3,
38 rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
44 CMS_ReceiptRequest_free(rr);
48 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
50 STACK_OF(GENERAL_NAMES)
51 *receiptList, STACK_OF(GENERAL_NAMES)
54 CMS_ReceiptRequest *rr = NULL;
56 rr = CMS_ReceiptRequest_new();
60 ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
62 if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
64 if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0)
68 sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
69 rr->receiptsTo = receiptsTo;
72 rr->receiptsFrom->type = 1;
73 rr->receiptsFrom->d.receiptList = receiptList;
75 rr->receiptsFrom->type = 0;
76 rr->receiptsFrom->d.allOrFirstTier = allorfirst;
82 CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
85 CMS_ReceiptRequest_free(rr);
90 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
92 unsigned char *rrder = NULL;
95 rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
99 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
100 V_ASN1_SEQUENCE, rrder, rrderlen))
107 CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
115 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
118 STACK_OF(GENERAL_NAMES) **plist,
119 STACK_OF(GENERAL_NAMES) **prto)
122 *pcid = rr->signedContentIdentifier;
123 if (rr->receiptsFrom->type == 0) {
125 *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
132 *plist = rr->receiptsFrom->d.receiptList;
135 *prto = rr->receiptsTo;
138 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
140 static int cms_msgSigDigest(CMS_SignerInfo *si,
141 unsigned char *dig, unsigned int *diglen)
144 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
147 if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
148 si->signedAttrs, dig, diglen))
153 /* Add a msgSigDigest attribute to a SignerInfo */
155 int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
157 unsigned char dig[EVP_MAX_MD_SIZE];
159 if (!cms_msgSigDigest(src, dig, &diglen)) {
160 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
163 if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
164 V_ASN1_OCTET_STRING, dig, diglen)) {
165 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
171 /* Verify signed receipt after it has already passed normal CMS verify */
173 int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
176 CMS_ReceiptRequest *rr = NULL;
177 CMS_Receipt *rct = NULL;
178 STACK_OF(CMS_SignerInfo) *sis, *osis;
179 CMS_SignerInfo *si, *osi = NULL;
180 ASN1_OCTET_STRING *msig, **pcont;
182 unsigned char dig[EVP_MAX_MD_SIZE];
185 /* Get SignerInfos, also checks SignedData content type */
186 osis = CMS_get0_SignerInfos(req_cms);
187 sis = CMS_get0_SignerInfos(cms);
191 if (sk_CMS_SignerInfo_num(sis) != 1) {
192 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
196 /* Check receipt content type */
197 if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
198 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
202 /* Extract and decode receipt content */
203 pcont = CMS_get0_content(cms);
204 if (!pcont || !*pcont) {
205 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
209 rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
212 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
216 /* Locate original request */
218 for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
219 osi = sk_CMS_SignerInfo_value(osis, i);
220 if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
224 if (i == sk_CMS_SignerInfo_num(osis)) {
225 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
229 si = sk_CMS_SignerInfo_value(sis, 0);
231 /* Get msgSigDigest value and compare */
233 msig = CMS_signed_get0_data_by_OBJ(si,
235 (NID_id_smime_aa_msgSigDigest), -3,
236 V_ASN1_OCTET_STRING);
239 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
243 if (!cms_msgSigDigest(osi, dig, &diglen)) {
244 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
248 if (diglen != (unsigned int)msig->length) {
249 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
253 if (memcmp(dig, msig->data, diglen)) {
254 CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
255 CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
259 /* Compare content types */
261 octype = CMS_signed_get0_data_by_OBJ(osi,
262 OBJ_nid2obj(NID_pkcs9_contentType),
265 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
269 /* Compare details in receipt request */
271 if (OBJ_cmp(octype, rct->contentType)) {
272 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
276 /* Get original receipt request details */
278 if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
279 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
283 if (ASN1_STRING_cmp(rr->signedContentIdentifier,
284 rct->signedContentIdentifier)) {
285 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
292 CMS_ReceiptRequest_free(rr);
293 M_ASN1_free_of(rct, CMS_Receipt);
299 * Encode a Receipt into an OCTET STRING read for including into content of a
300 * SignedData ContentInfo.
303 ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
306 CMS_ReceiptRequest *rr = NULL;
308 ASN1_OCTET_STRING *os = NULL;
310 /* Get original receipt request */
312 /* Get original receipt request details */
314 if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
315 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
319 /* Get original content type */
321 ctype = CMS_signed_get0_data_by_OBJ(si,
322 OBJ_nid2obj(NID_pkcs9_contentType),
325 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
330 rct.contentType = ctype;
331 rct.signedContentIdentifier = rr->signedContentIdentifier;
332 rct.originatorSignatureValue = si->signature;
334 os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
337 CMS_ReceiptRequest_free(rr);
342 * Add signer certificate's V2 digest to a SignerInfo
346 int CMS_add1_signing_cert_v2(CMS_SignerInfo *si,
347 ESS_SIGNING_CERT_V2 *sc)
349 ASN1_STRING *seq = NULL;
350 unsigned char *p, *pp;
353 /* Add SigningCertificateV2 signed attribute to the signer info. */
354 len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
355 if ((pp = OPENSSL_malloc(len)) == NULL)
358 i2d_ESS_SIGNING_CERT_V2(sc, &p);
359 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
363 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
364 V_ASN1_SEQUENCE, seq, -1))
366 ASN1_STRING_free(seq);
369 CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT_V2, ERR_R_MALLOC_FAILURE);
370 ASN1_STRING_free(seq);
376 * Add signer certificate's digest to a SignerInfo
380 int CMS_add1_signing_cert(CMS_SignerInfo *si, ESS_SIGNING_CERT *sc)
382 ASN1_STRING *seq = NULL;
383 unsigned char *p, *pp;
386 /* Add SigningCertificate signed attribute to the signer info. */
387 len = i2d_ESS_SIGNING_CERT(sc, NULL);
388 if ((pp = OPENSSL_malloc(len)) == NULL)
391 i2d_ESS_SIGNING_CERT(sc, &p);
392 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
396 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
397 V_ASN1_SEQUENCE, seq, -1))
399 ASN1_STRING_free(seq);
402 CMSerr(CMS_F_CMS_ADD1_SIGNING_CERT, ERR_R_MALLOC_FAILURE);
403 ASN1_STRING_free(seq);