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