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