]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/cms/cms_ess.c
Fix external symbols for cms.
[thirdparty/openssl.git] / crypto / cms / cms_ess.c
1 /*
2 * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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>
18 #include "crypto/ess.h"
19 #include "crypto/cms.h"
20 #include "crypto/x509.h"
21 #include "cms_local.h"
22
23 IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
24
25 /* ESS services */
26
27 int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
28 {
29 ASN1_STRING *str;
30 CMS_ReceiptRequest *rr;
31 ASN1_OBJECT *obj = OBJ_nid2obj(NID_id_smime_aa_receiptRequest);
32
33 if (prr != NULL)
34 *prr = NULL;
35 str = CMS_signed_get0_data_by_OBJ(si, obj, -3, V_ASN1_SEQUENCE);
36 if (str == NULL)
37 return 0;
38
39 rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
40 if (rr == NULL)
41 return -1;
42 if (prr != NULL)
43 *prr = rr;
44 else
45 CMS_ReceiptRequest_free(rr);
46 return 1;
47 }
48
49 /*
50 First, get the ESS_SIGNING_CERT(V2) signed attribute from |si|.
51 Then check matching of each cert of trust |chain| with one of
52 the |cert_ids|(Hash+IssuerID) list from this ESS_SIGNING_CERT.
53 Derived from ts_check_signing_certs()
54 */
55 int ossl_ess_check_signing_certs(CMS_SignerInfo *si, STACK_OF(X509) *chain)
56 {
57 ESS_SIGNING_CERT *ss = NULL;
58 ESS_SIGNING_CERT_V2 *ssv2 = NULL;
59 X509 *cert;
60 int i = 0, ret = 0;
61
62 if (ossl_cms_signerinfo_get_signing_cert(si, &ss) > 0
63 && ss->cert_ids != NULL) {
64 STACK_OF(ESS_CERT_ID) *cert_ids = ss->cert_ids;
65
66 cert = sk_X509_value(chain, 0);
67 if (ossl_ess_find_cert(cert_ids, cert) != 0)
68 goto err;
69
70 /*
71 * Check the other certificates of the chain.
72 * Fail if no signing certificate ids found for each certificate.
73 */
74 if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
75 /* for each chain cert, try to find its cert id */
76 for (i = 1; i < sk_X509_num(chain); ++i) {
77 cert = sk_X509_value(chain, i);
78 if (ossl_ess_find_cert(cert_ids, cert) < 0)
79 goto err;
80 }
81 }
82 } else if (ossl_cms_signerinfo_get_signing_cert_v2(si, &ssv2) > 0
83 && ssv2->cert_ids!= NULL) {
84 STACK_OF(ESS_CERT_ID_V2) *cert_ids_v2 = ssv2->cert_ids;
85
86 cert = sk_X509_value(chain, 0);
87 if (ossl_ess_find_cert_v2(cert_ids_v2, cert) != 0)
88 goto err;
89
90 /*
91 * Check the other certificates of the chain.
92 * Fail if no signing certificate ids found for each certificate.
93 */
94 if (sk_ESS_CERT_ID_V2_num(cert_ids_v2) > 1) {
95 /* for each chain cert, try to find its cert id */
96 for (i = 1; i < sk_X509_num(chain); ++i) {
97 cert = sk_X509_value(chain, i);
98 if (ossl_ess_find_cert_v2(cert_ids_v2, cert) < 0)
99 goto err;
100 }
101 }
102 } else {
103 ERR_raise(ERR_LIB_CMS, CMS_R_ESS_NO_SIGNING_CERTID_ATTRIBUTE);
104 return 0;
105 }
106 ret = 1;
107 err:
108 if (!ret)
109 ERR_raise(ERR_LIB_CMS, CMS_R_ESS_SIGNING_CERTID_MISMATCH_ERROR);
110
111 ESS_SIGNING_CERT_free(ss);
112 ESS_SIGNING_CERT_V2_free(ssv2);
113 return ret;
114 }
115
116 CMS_ReceiptRequest *CMS_ReceiptRequest_create0_ex(
117 unsigned char *id, int idlen, int allorfirst,
118 STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo,
119 OSSL_LIB_CTX *libctx, const char *propq)
120 {
121 CMS_ReceiptRequest *rr;
122
123 rr = CMS_ReceiptRequest_new();
124 if (rr == NULL)
125 goto merr;
126 if (id)
127 ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
128 else {
129 if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
130 goto merr;
131 if (RAND_bytes_ex(libctx, rr->signedContentIdentifier->data, 32) <= 0)
132 goto err;
133 }
134
135 sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
136 rr->receiptsTo = receiptsTo;
137
138 if (receiptList != NULL) {
139 rr->receiptsFrom->type = 1;
140 rr->receiptsFrom->d.receiptList = receiptList;
141 } else {
142 rr->receiptsFrom->type = 0;
143 rr->receiptsFrom->d.allOrFirstTier = allorfirst;
144 }
145
146 return rr;
147
148 merr:
149 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
150
151 err:
152 CMS_ReceiptRequest_free(rr);
153 return NULL;
154
155 }
156
157 CMS_ReceiptRequest *CMS_ReceiptRequest_create0(
158 unsigned char *id, int idlen, int allorfirst,
159 STACK_OF(GENERAL_NAMES) *receiptList, STACK_OF(GENERAL_NAMES) *receiptsTo)
160 {
161 return CMS_ReceiptRequest_create0_ex(id, idlen, allorfirst, receiptList,
162 receiptsTo, NULL, NULL);
163 }
164
165 int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
166 {
167 unsigned char *rrder = NULL;
168 int rrderlen, r = 0;
169
170 rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
171 if (rrderlen < 0)
172 goto merr;
173
174 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
175 V_ASN1_SEQUENCE, rrder, rrderlen))
176 goto merr;
177
178 r = 1;
179
180 merr:
181 if (!r)
182 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
183
184 OPENSSL_free(rrder);
185
186 return r;
187
188 }
189
190 void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
191 ASN1_STRING **pcid,
192 int *pallorfirst,
193 STACK_OF(GENERAL_NAMES) **plist,
194 STACK_OF(GENERAL_NAMES) **prto)
195 {
196 if (pcid != NULL)
197 *pcid = rr->signedContentIdentifier;
198 if (rr->receiptsFrom->type == 0) {
199 if (pallorfirst != NULL)
200 *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
201 if (plist != NULL)
202 *plist = NULL;
203 } else {
204 if (pallorfirst != NULL)
205 *pallorfirst = -1;
206 if (plist != NULL)
207 *plist = rr->receiptsFrom->d.receiptList;
208 }
209 if (prto != NULL)
210 *prto = rr->receiptsTo;
211 }
212
213 /* Digest a SignerInfo structure for msgSigDigest attribute processing */
214
215 static int cms_msgSigDigest(CMS_SignerInfo *si,
216 unsigned char *dig, unsigned int *diglen)
217 {
218 const EVP_MD *md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
219
220 if (md == NULL)
221 return 0;
222 if (!asn1_item_digest_ex(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
223 si->signedAttrs, dig, diglen,
224 ossl_cms_ctx_get0_libctx(si->cms_ctx),
225 ossl_cms_ctx_get0_propq(si->cms_ctx)))
226 return 0;
227 return 1;
228 }
229
230 /* Add a msgSigDigest attribute to a SignerInfo */
231
232 int ossl_cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
233 {
234 unsigned char dig[EVP_MAX_MD_SIZE];
235 unsigned int diglen;
236
237 if (!cms_msgSigDigest(src, dig, &diglen)) {
238 ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
239 return 0;
240 }
241 if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
242 V_ASN1_OCTET_STRING, dig, diglen)) {
243 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
244 return 0;
245 }
246 return 1;
247 }
248
249 /* Verify signed receipt after it has already passed normal CMS verify */
250
251 int ossl_cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
252 {
253 int r = 0, i;
254 CMS_ReceiptRequest *rr = NULL;
255 CMS_Receipt *rct = NULL;
256 STACK_OF(CMS_SignerInfo) *sis, *osis;
257 CMS_SignerInfo *si, *osi = NULL;
258 ASN1_OCTET_STRING *msig, **pcont;
259 ASN1_OBJECT *octype;
260 unsigned char dig[EVP_MAX_MD_SIZE];
261 unsigned int diglen;
262
263 /* Get SignerInfos, also checks SignedData content type */
264 osis = CMS_get0_SignerInfos(req_cms);
265 sis = CMS_get0_SignerInfos(cms);
266 if (!osis || !sis)
267 goto err;
268
269 if (sk_CMS_SignerInfo_num(sis) != 1) {
270 ERR_raise(ERR_LIB_CMS, CMS_R_NEED_ONE_SIGNER);
271 goto err;
272 }
273
274 /* Check receipt content type */
275 if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
276 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_A_SIGNED_RECEIPT);
277 goto err;
278 }
279
280 /* Extract and decode receipt content */
281 pcont = CMS_get0_content(cms);
282 if (pcont == NULL || *pcont == NULL) {
283 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
284 goto err;
285 }
286
287 rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
288
289 if (!rct) {
290 ERR_raise(ERR_LIB_CMS, CMS_R_RECEIPT_DECODE_ERROR);
291 goto err;
292 }
293
294 /* Locate original request */
295
296 for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
297 osi = sk_CMS_SignerInfo_value(osis, i);
298 if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
299 break;
300 }
301
302 if (i == sk_CMS_SignerInfo_num(osis)) {
303 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_SIGNATURE);
304 goto err;
305 }
306
307 si = sk_CMS_SignerInfo_value(sis, 0);
308
309 /* Get msgSigDigest value and compare */
310
311 msig = CMS_signed_get0_data_by_OBJ(si,
312 OBJ_nid2obj
313 (NID_id_smime_aa_msgSigDigest), -3,
314 V_ASN1_OCTET_STRING);
315
316 if (!msig) {
317 ERR_raise(ERR_LIB_CMS, CMS_R_NO_MSGSIGDIGEST);
318 goto err;
319 }
320
321 if (!cms_msgSigDigest(osi, dig, &diglen)) {
322 ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_ERROR);
323 goto err;
324 }
325
326 if (diglen != (unsigned int)msig->length) {
327 ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
328 goto err;
329 }
330
331 if (memcmp(dig, msig->data, diglen)) {
332 ERR_raise(ERR_LIB_CMS, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
333 goto err;
334 }
335
336 /* Compare content types */
337
338 octype = CMS_signed_get0_data_by_OBJ(osi,
339 OBJ_nid2obj(NID_pkcs9_contentType),
340 -3, V_ASN1_OBJECT);
341 if (!octype) {
342 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
343 goto err;
344 }
345
346 /* Compare details in receipt request */
347
348 if (OBJ_cmp(octype, rct->contentType)) {
349 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_TYPE_MISMATCH);
350 goto err;
351 }
352
353 /* Get original receipt request details */
354
355 if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
356 ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
357 goto err;
358 }
359
360 if (ASN1_STRING_cmp(rr->signedContentIdentifier,
361 rct->signedContentIdentifier)) {
362 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENTIDENTIFIER_MISMATCH);
363 goto err;
364 }
365
366 r = 1;
367
368 err:
369 CMS_ReceiptRequest_free(rr);
370 M_ASN1_free_of(rct, CMS_Receipt);
371 return r;
372
373 }
374
375 /*
376 * Encode a Receipt into an OCTET STRING read for including into content of a
377 * SignedData ContentInfo.
378 */
379
380 ASN1_OCTET_STRING *ossl_cms_encode_Receipt(CMS_SignerInfo *si)
381 {
382 CMS_Receipt rct;
383 CMS_ReceiptRequest *rr = NULL;
384 ASN1_OBJECT *ctype;
385 ASN1_OCTET_STRING *os = NULL;
386
387 /* Get original receipt request */
388
389 /* Get original receipt request details */
390
391 if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
392 ERR_raise(ERR_LIB_CMS, CMS_R_NO_RECEIPT_REQUEST);
393 goto err;
394 }
395
396 /* Get original content type */
397
398 ctype = CMS_signed_get0_data_by_OBJ(si,
399 OBJ_nid2obj(NID_pkcs9_contentType),
400 -3, V_ASN1_OBJECT);
401 if (!ctype) {
402 ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT_TYPE);
403 goto err;
404 }
405
406 rct.version = 1;
407 rct.contentType = ctype;
408 rct.signedContentIdentifier = rr->signedContentIdentifier;
409 rct.originatorSignatureValue = si->signature;
410
411 os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
412
413 err:
414 CMS_ReceiptRequest_free(rr);
415 return os;
416 }
417
418 /*
419 * Add signer certificate's V2 digest |sc| to a SignerInfo structure |si|
420 */
421
422 int ossl_cms_add1_signing_cert_v2(CMS_SignerInfo *si, ESS_SIGNING_CERT_V2 *sc)
423 {
424 ASN1_STRING *seq = NULL;
425 unsigned char *p, *pp = NULL;
426 int len;
427
428 /* Add SigningCertificateV2 signed attribute to the signer info. */
429 len = i2d_ESS_SIGNING_CERT_V2(sc, NULL);
430 if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
431 goto err;
432 p = pp;
433 i2d_ESS_SIGNING_CERT_V2(sc, &p);
434 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
435 goto err;
436 OPENSSL_free(pp);
437 pp = NULL;
438 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificateV2,
439 V_ASN1_SEQUENCE, seq, -1))
440 goto err;
441 ASN1_STRING_free(seq);
442 return 1;
443 err:
444 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
445 ASN1_STRING_free(seq);
446 OPENSSL_free(pp);
447 return 0;
448 }
449
450 /*
451 * Add signer certificate's digest |sc| to a SignerInfo structure |si|
452 */
453
454 int ossl_cms_add1_signing_cert(CMS_SignerInfo *si, ESS_SIGNING_CERT *sc)
455 {
456 ASN1_STRING *seq = NULL;
457 unsigned char *p, *pp = NULL;
458 int len;
459
460 /* Add SigningCertificate signed attribute to the signer info. */
461 len = i2d_ESS_SIGNING_CERT(sc, NULL);
462 if (len <= 0 || (pp = OPENSSL_malloc(len)) == NULL)
463 goto err;
464 p = pp;
465 i2d_ESS_SIGNING_CERT(sc, &p);
466 if (!(seq = ASN1_STRING_new()) || !ASN1_STRING_set(seq, pp, len))
467 goto err;
468 OPENSSL_free(pp);
469 pp = NULL;
470 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_signingCertificate,
471 V_ASN1_SEQUENCE, seq, -1))
472 goto err;
473 ASN1_STRING_free(seq);
474 return 1;
475 err:
476 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
477 ASN1_STRING_free(seq);
478 OPENSSL_free(pp);
479 return 0;
480 }