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