]> git.saurik.com Git - apple/security.git/blame - libsecurity_smime/lib/cmssigdata.c
Security-57337.60.2.tar.gz
[apple/security.git] / libsecurity_smime / lib / cmssigdata.c
CommitLineData
b1ab9ed8
A
1/*
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above. If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL. If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
31 * GPL.
32 */
33
34/*
35 * CMS signedData methods.
36 */
37
38#include <Security/SecCmsSignedData.h>
39
40#include <Security/SecCmsContentInfo.h>
41#include <Security/SecCmsDigestContext.h>
42#include <Security/SecCmsSignerInfo.h>
43
44#include "cmslocal.h"
45
46#include "cert.h"
d8f41ccd 47#include "SecAsn1Item.h"
b1ab9ed8 48#include "secoid.h"
b1ab9ed8
A
49
50#include <security_asn1/secasn1.h>
51#include <security_asn1/secerr.h>
d8f41ccd 52#include <security_asn1/secport.h>
b1ab9ed8 53
d8f41ccd
A
54#if !USE_CDSA_CRYPTO
55#include <Security/SecCertificatePriv.h>
b1ab9ed8
A
56#endif
57
58SecCmsSignedDataRef
59SecCmsSignedDataCreate(SecCmsMessageRef cmsg)
60{
61 void *mark;
62 SecCmsSignedDataRef sigd;
63 PLArenaPool *poolp;
64
65 poolp = cmsg->poolp;
66
67 mark = PORT_ArenaMark(poolp);
68
69 sigd = (SecCmsSignedDataRef)PORT_ArenaZAlloc (poolp, sizeof(SecCmsSignedData));
70 if (sigd == NULL)
71 goto loser;
72
d8f41ccd 73 sigd->contentInfo.cmsg = cmsg;
b1ab9ed8
A
74
75 /* signerInfos, certs, certlists, crls are all empty */
76 /* version is set in SecCmsSignedDataFinalize() */
77
78 PORT_ArenaUnmark(poolp, mark);
79 return sigd;
80
81loser:
82 PORT_ArenaRelease(poolp, mark);
83 return NULL;
84}
85
86void
87SecCmsSignedDataDestroy(SecCmsSignedDataRef sigd)
88{
89 SecCmsSignerInfoRef *signerinfos, si;
90
91 if (sigd == NULL)
92 return;
93
94 if (sigd->certs != NULL)
95 CFRelease(sigd->certs);
96
97 signerinfos = sigd->signerInfos;
98 if (signerinfos != NULL) {
99 while ((si = *signerinfos++) != NULL)
100 SecCmsSignerInfoDestroy(si);
101 }
102
103 /* everything's in a pool, so don't worry about the storage */
104 SecCmsContentInfoDestroy(&(sigd->contentInfo));
105}
106
107/*
108 * SecCmsSignedDataEncodeBeforeStart - do all the necessary things to a SignedData
109 * before start of encoding.
110 *
111 * In detail:
112 * - find out about the right value to put into sigd->version
113 * - come up with a list of digestAlgorithms (which should be the union of the algorithms
114 * in the signerinfos).
115 * If we happen to have a pre-set list of algorithms (and digest values!), we
116 * check if we have all the signerinfos' algorithms. If not, this is an error.
117 */
118OSStatus
119SecCmsSignedDataEncodeBeforeStart(SecCmsSignedDataRef sigd)
120{
121 SecCmsSignerInfoRef signerinfo;
122 SECOidTag digestalgtag;
d8f41ccd 123 SecAsn1Item * dummy;
b1ab9ed8
A
124 int version;
125 OSStatus rv;
126 Boolean haveDigests = PR_FALSE;
127 int n, i;
128 PLArenaPool *poolp;
129
d8f41ccd 130 poolp = sigd->contentInfo.cmsg->poolp;
b1ab9ed8
A
131
132 /* we assume that we have precomputed digests if there is a list of algorithms, and */
133 /* a chunk of data for each of those algorithms */
134 if (sigd->digestAlgorithms != NULL && sigd->digests != NULL) {
135 for (i=0; sigd->digestAlgorithms[i] != NULL; i++) {
136 if (sigd->digests[i] == NULL)
137 break;
138 }
139 if (sigd->digestAlgorithms[i] == NULL) /* reached the end of the array? */
140 haveDigests = PR_TRUE; /* yes: we must have all the digests */
141 }
142
143 version = SEC_CMS_SIGNED_DATA_VERSION_BASIC;
144
145 /* RFC2630 5.1 "version is the syntax version number..." */
146 if (SecCmsContentInfoGetContentTypeTag(&(sigd->contentInfo)) != SEC_OID_PKCS7_DATA)
147 version = SEC_CMS_SIGNED_DATA_VERSION_EXT;
148
149 /* prepare all the SignerInfos (there may be none) */
150 for (i=0; i < SecCmsSignedDataSignerInfoCount(sigd); i++) {
151 signerinfo = SecCmsSignedDataGetSignerInfo(sigd, i);
152
153 /* RFC2630 5.1 "version is the syntax version number..." */
154 if (SecCmsSignerInfoGetVersion(signerinfo) != SEC_CMS_SIGNER_INFO_VERSION_ISSUERSN)
155 version = SEC_CMS_SIGNED_DATA_VERSION_EXT;
156
157 /* collect digestAlgorithms from SignerInfos */
158 /* (we need to know which algorithms we have when the content comes in) */
159 /* do not overwrite any existing digestAlgorithms (and digest) */
160 digestalgtag = SecCmsSignerInfoGetDigestAlgTag(signerinfo);
b1ab9ed8
A
161 n = SecCmsAlgArrayGetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
162 if (n < 0 && haveDigests) {
163 /* oops, there is a digestalg we do not have a digest for */
164 /* but we were supposed to have all the digests already... */
165 goto loser;
166 } else if (n < 0) {
167 /* add the digestAlgorithm & a NULL digest */
d8f41ccd 168 rv = SecCmsSignedDataAddDigest(poolp, sigd, digestalgtag, NULL);
b1ab9ed8
A
169 if (rv != SECSuccess)
170 goto loser;
171 } else {
172 /* found it, nothing to do */
173 }
174 }
175
176 dummy = SEC_ASN1EncodeInteger(poolp, &(sigd->version), (long)version);
177 if (dummy == NULL)
178 return SECFailure;
179
180 /* this is a SET OF, so we need to sort them guys */
181 rv = SecCmsArraySortByDER((void **)sigd->digestAlgorithms,
182 SEC_ASN1_GET(SECOID_AlgorithmIDTemplate),
183 (void **)sigd->digests);
184 if (rv != SECSuccess)
185 return SECFailure;
186
187 return SECSuccess;
188
189loser:
190 return SECFailure;
191}
192
193OSStatus
194SecCmsSignedDataEncodeBeforeData(SecCmsSignedDataRef sigd)
195{
196 /* set up the digests */
197 if (sigd->digestAlgorithms != NULL) {
198 sigd->contentInfo.digcx = SecCmsDigestContextStartMultiple(sigd->digestAlgorithms);
199 if (sigd->contentInfo.digcx == NULL)
200 return SECFailure;
201 }
202 return SECSuccess;
203}
204
b1ab9ed8
A
205/*
206 * SecCmsSignedDataEncodeAfterData - do all the necessary things to a SignedData
207 * after all the encapsulated data was passed through the encoder.
208 *
209 * In detail:
210 * - create the signatures in all the SignerInfos
211 *
212 * Please note that nothing is done to the Certificates and CRLs in the message - this
213 * is entirely the responsibility of our callers.
214 */
215OSStatus
216SecCmsSignedDataEncodeAfterData(SecCmsSignedDataRef sigd)
217{
218 SecCmsSignerInfoRef *signerinfos, signerinfo;
219 SecCmsContentInfoRef cinfo;
220 SECOidTag digestalgtag;
221 OSStatus ret = SECFailure;
222 OSStatus rv;
d8f41ccd
A
223 SecAsn1Item * contentType;
224 CFIndex certcount;
b1ab9ed8
A
225 int i, ci, n, rci, si;
226 PLArenaPool *poolp;
227 CFArrayRef certlist;
228 extern const SecAsn1Template SecCmsSignerInfoTemplate[];
229
b1ab9ed8 230 cinfo = &(sigd->contentInfo);
d8f41ccd 231 poolp = cinfo->cmsg->poolp;
b1ab9ed8
A
232
233 /* did we have digest calculation going on? */
234 if (cinfo->digcx) {
d8f41ccd
A
235 SecAsn1Item **digests = NULL;
236 SECAlgorithmID **digestalgs = NULL;
237 rv = SecCmsDigestContextFinishMultiple(cinfo->digcx, &digestalgs, &digests);
b1ab9ed8
A
238 if (rv != SECSuccess)
239 goto loser; /* error has been set by SecCmsDigestContextFinishMultiple */
d8f41ccd
A
240 if (digestalgs && digests) {
241 rv = SecCmsSignedDataSetDigests(sigd, digestalgs, digests);
242 if (rv != SECSuccess)
243 goto loser; /* error has been set by SecCmsSignedDataSetDigests */
244 }
245 SecCmsDigestContextDestroy(cinfo->digcx);
b1ab9ed8
A
246 cinfo->digcx = NULL;
247 }
248
249 signerinfos = sigd->signerInfos;
250 certcount = 0;
251
252 /* prepare all the SignerInfos (there may be none) */
253 for (i=0; i < SecCmsSignedDataSignerInfoCount(sigd); i++) {
254 signerinfo = SecCmsSignedDataGetSignerInfo(sigd, i);
d8f41ccd 255
b1ab9ed8
A
256 /* find correct digest for this signerinfo */
257 digestalgtag = SecCmsSignerInfoGetDigestAlgTag(signerinfo);
258 n = SecCmsAlgArrayGetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
259 if (n < 0 || sigd->digests == NULL || sigd->digests[n] == NULL) {
260 /* oops - digest not found */
261 PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);
262 goto loser;
263 }
264
265 /* XXX if our content is anything else but data, we need to force the
266 * presence of signed attributes (RFC2630 5.3 "signedAttributes is a
267 * collection...") */
268
269 /* pass contentType here as we want a contentType attribute */
270 if ((contentType = SecCmsContentInfoGetContentTypeOID(cinfo)) == NULL)
271 goto loser;
272
273 /* sign the thing */
274 rv = SecCmsSignerInfoSign(signerinfo, sigd->digests[n], contentType);
275 if (rv != SECSuccess)
276 goto loser;
277
278 /* while we're at it, count number of certs in certLists */
279 certlist = SecCmsSignerInfoGetCertList(signerinfo);
280 if (certlist)
281 certcount += CFArrayGetCount(certlist);
282 }
283
b1ab9ed8
A
284 /* this is a SET OF, so we need to sort them guys */
285 rv = SecCmsArraySortByDER((void **)signerinfos, SecCmsSignerInfoTemplate, NULL);
286 if (rv != SECSuccess)
287 goto loser;
288
289 /*
290 * now prepare certs & crls
291 */
292
293 /* count the rest of the certs */
294 if (sigd->certs != NULL)
295 certcount += CFArrayGetCount(sigd->certs);
296
297 if (certcount == 0) {
298 sigd->rawCerts = NULL;
299 } else {
300 /*
301 * Combine all of the certs and cert chains into rawcerts.
302 * Note: certcount is an upper bound; we may not need that many slots
303 * but we will allocate anyway to avoid having to do another pass.
304 * (The temporary space saving is not worth it.)
305 *
306 * XXX ARGH - this NEEDS to be fixed. need to come up with a decent
307 * SetOfDERcertficates implementation
308 */
d8f41ccd 309 sigd->rawCerts = (SecAsn1Item * *)PORT_ArenaAlloc(poolp, (certcount + 1) * sizeof(SecAsn1Item *));
b1ab9ed8
A
310 if (sigd->rawCerts == NULL)
311 return SECFailure;
312
313 /*
314 * XXX Want to check for duplicates and not add *any* cert that is
315 * already in the set. This will be more important when we start
316 * dealing with larger sets of certs, dual-key certs (signing and
317 * encryption), etc. For the time being we can slide by...
318 *
319 * XXX ARGH - this NEEDS to be fixed. need to come up with a decent
320 * SetOfDERcertficates implementation
321 */
322 rci = 0;
323 if (signerinfos != NULL) {
324 for (si = 0; signerinfos[si] != NULL; si++) {
325 signerinfo = signerinfos[si];
326 for (ci = 0; ci < CFArrayGetCount(signerinfo->certList); ci++) {
d8f41ccd 327 sigd->rawCerts[rci] = PORT_ArenaZAlloc(poolp, sizeof(SecAsn1Item));
b1ab9ed8 328 SecCertificateRef cert = (SecCertificateRef)CFArrayGetValueAtIndex(signerinfo->certList, ci);
d8f41ccd 329#if USE_CDSA_CRYPTO
b1ab9ed8 330 SecCertificateGetData(cert, sigd->rawCerts[rci++]);
d8f41ccd
A
331#else
332 SecAsn1Item cert_data = { SecCertificateGetLength(cert),
333 (uint8_t *)SecCertificateGetBytePtr(cert) };
334 *(sigd->rawCerts[rci++]) = cert_data;
335#endif
b1ab9ed8
A
336 }
337 }
338 }
339
340 if (sigd->certs != NULL) {
341 for (ci = 0; ci < CFArrayGetCount(sigd->certs); ci++) {
d8f41ccd 342 sigd->rawCerts[rci] = PORT_ArenaZAlloc(poolp, sizeof(SecAsn1Item));
b1ab9ed8 343 SecCertificateRef cert = (SecCertificateRef)CFArrayGetValueAtIndex(sigd->certs, ci);
d8f41ccd
A
344#if USE_CDSA_CRYPTO
345 SecCertificateGetData(cert, sigd->rawCerts[rci++]);
346#else
347 SecAsn1Item cert_data = { SecCertificateGetLength(cert),
348 (uint8_t *)SecCertificateGetBytePtr(cert) };
349 *(sigd->rawCerts[rci++]) = cert_data;
350#endif
b1ab9ed8
A
351 }
352 }
353
354 sigd->rawCerts[rci] = NULL;
355
356 /* this is a SET OF, so we need to sort them guys - we have the DER already, though */
357 SecCmsArraySort((void **)sigd->rawCerts, SecCmsUtilDERCompare, NULL, NULL);
358 }
359
360 ret = SECSuccess;
361
362loser:
b1ab9ed8
A
363 return ret;
364}
365
366OSStatus
367SecCmsSignedDataDecodeBeforeData(SecCmsSignedDataRef sigd)
368{
d8f41ccd
A
369 /* set up the digests, if we have digest algorithms, no digests yet, and content is attached */
370 if (sigd->digestAlgorithms != NULL && sigd->digests == NULL /* && sigd->contentInfo.content.pointer != NULL*/) {
b1ab9ed8
A
371 /* if digests are already there, do nothing */
372 sigd->contentInfo.digcx = SecCmsDigestContextStartMultiple(sigd->digestAlgorithms);
373 if (sigd->contentInfo.digcx == NULL)
374 return SECFailure;
375 }
376 return SECSuccess;
377}
378
379/*
380 * SecCmsSignedDataDecodeAfterData - do all the necessary things to a SignedData
381 * after all the encapsulated data was passed through the decoder.
382 */
383OSStatus
384SecCmsSignedDataDecodeAfterData(SecCmsSignedDataRef sigd)
385{
d8f41ccd
A
386 OSStatus rv = SECSuccess;
387
b1ab9ed8
A
388 /* did we have digest calculation going on? */
389 if (sigd->contentInfo.digcx) {
d8f41ccd
A
390 /* @@@ we should see if data was absent vs. zero length */
391 if (sigd->contentInfo.content.data && sigd->contentInfo.content.data->Length) {
392 SecAsn1Item * *digests = NULL;
393 SECAlgorithmID **digestalgs = NULL;
394 rv = SecCmsDigestContextFinishMultiple(sigd->contentInfo.digcx, &digestalgs, &digests);
395 if (rv != SECSuccess)
396 goto loser; /* error has been set by SecCmsDigestContextFinishMultiple */
397 rv = SecCmsSignedDataSetDigests(sigd, digestalgs, digests);
398 if (rv != SECSuccess)
399 goto loser; /* error has been set by SecCmsSignedDataSetDigests */
400 }
401 SecCmsDigestContextDestroy(sigd->contentInfo.digcx);
b1ab9ed8
A
402 sigd->contentInfo.digcx = NULL;
403 }
d8f41ccd
A
404
405loser:
406 return rv;
b1ab9ed8
A
407}
408
409/*
410 * SecCmsSignedDataDecodeAfterEnd - do all the necessary things to a SignedData
411 * after all decoding is finished.
412 */
413OSStatus
414SecCmsSignedDataDecodeAfterEnd(SecCmsSignedDataRef sigd)
415{
416 SecCmsSignerInfoRef *signerinfos;
417 int i;
418
d8f41ccd 419 /* set cmsg for all the signerinfos */
b1ab9ed8
A
420 signerinfos = sigd->signerInfos;
421
d8f41ccd 422 /* set signedData for all the signerinfos */
b1ab9ed8 423 if (signerinfos) {
d8f41ccd
A
424 for (i = 0; signerinfos[i] != NULL; i++)
425 signerinfos[i]->signedData = sigd;
b1ab9ed8
A
426 }
427
428 return SECSuccess;
429}
430
431/*
432 * SecCmsSignedDataGetSignerInfos - retrieve the SignedData's signer list
433 */
434SecCmsSignerInfoRef *
435SecCmsSignedDataGetSignerInfos(SecCmsSignedDataRef sigd)
436{
437 return sigd->signerInfos;
438}
439
440int
441SecCmsSignedDataSignerInfoCount(SecCmsSignedDataRef sigd)
442{
443 return SecCmsArrayCount((void **)sigd->signerInfos);
444}
445
446SecCmsSignerInfoRef
447SecCmsSignedDataGetSignerInfo(SecCmsSignedDataRef sigd, int i)
448{
449 return sigd->signerInfos[i];
450}
451
452/*
453 * SecCmsSignedDataGetDigestAlgs - retrieve the SignedData's digest algorithm list
454 */
455SECAlgorithmID **
456SecCmsSignedDataGetDigestAlgs(SecCmsSignedDataRef sigd)
457{
458 return sigd->digestAlgorithms;
459}
460
461/*
462 * SecCmsSignedDataGetContentInfo - return pointer to this signedData's contentinfo
463 */
464SecCmsContentInfoRef
465SecCmsSignedDataGetContentInfo(SecCmsSignedDataRef sigd)
466{
467 return &(sigd->contentInfo);
468}
469
470/*
471 * SecCmsSignedDataGetCertificateList - retrieve the SignedData's certificate list
472 */
d8f41ccd 473SecAsn1Item * *
b1ab9ed8
A
474SecCmsSignedDataGetCertificateList(SecCmsSignedDataRef sigd)
475{
476 return sigd->rawCerts;
477}
478
479OSStatus
480SecCmsSignedDataImportCerts(SecCmsSignedDataRef sigd, SecKeychainRef keychain,
481 SECCertUsage certusage, Boolean keepcerts)
482{
d8f41ccd 483 OSStatus rv = -1;
b1ab9ed8 484
d8f41ccd
A
485#if USE_CDSA_CRYPTO
486 int ix, certcount = SecCmsArrayCount((void **)sigd->rawCerts);
b1ab9ed8
A
487 rv = CERT_ImportCerts(keychain, certusage, certcount, sigd->rawCerts, NULL,
488 keepcerts, PR_FALSE, NULL);
b1ab9ed8
A
489 /* XXX CRL handling */
490
491 if (sigd->signerInfos != NULL) {
492 /* fill in all signerinfo's certs */
d8f41ccd
A
493 for (ix = 0; sigd->signerInfos[ix] != NULL; i++)
494 (void)SecCmsSignerInfoGetSigningCertificate(sigd->signerInfos[ix], keychain);
b1ab9ed8 495 }
d8f41ccd
A
496#else
497 // XXX we should only ever import certs for a cert only data blob
498#endif
b1ab9ed8
A
499
500 return rv;
501}
502
503/*
504 * XXX the digests need to be passed in BETWEEN the decoding and the verification in case
505 * of external signatures!
506 */
507
d8f41ccd 508
b1ab9ed8
A
509/*
510 * SecCmsSignedDataVerifySignerInfo - check the signatures.
511 *
512 * The digests were either calculated during decoding (and are stored in the
513 * signedData itself) or set after decoding using SecCmsSignedDataSetDigests.
514 *
515 * The verification checks if the signing cert is valid and has a trusted chain
516 * for the purpose specified by "policies".
517 *
518 * If trustRef is NULL the cert chain is verified and the VerificationStatus is set accordingly.
519 * Otherwise a SecTrust object is returned for the caller to evaluate using SecTrustEvaluate().
520 */
521OSStatus
522SecCmsSignedDataVerifySignerInfo(SecCmsSignedDataRef sigd, int i,
523 SecKeychainRef keychainOrArray, CFTypeRef policies, SecTrustRef *trustRef)
524{
525 SecCmsSignerInfoRef signerinfo;
526 SecCmsContentInfoRef cinfo;
527 SECOidData *algiddata;
d8f41ccd
A
528 SecAsn1Item *contentType, *digest;
529 OSStatus status;
b1ab9ed8
A
530
531 cinfo = &(sigd->contentInfo);
532
533 signerinfo = sigd->signerInfos[i];
534
535 /* Signature or digest level verificationStatus errors should supercede
536 certificate level errors, so check the digest and signature first. */
537
538 /* Find digest and contentType for signerinfo */
539 algiddata = SecCmsSignerInfoGetDigestAlg(signerinfo);
d8f41ccd
A
540
541 if (!sigd->digests) {
542 SECAlgorithmID **digestalgs = SecCmsSignedDataGetDigestAlgs(sigd);
543 SecCmsDigestContextRef digcx = SecCmsDigestContextStartMultiple(digestalgs);
544 SecCmsSignedDataSetDigestContext(sigd, digcx);
545 SecCmsDigestContextDestroy(digcx);
b1ab9ed8 546 }
d8f41ccd 547
b1ab9ed8 548 digest = SecCmsSignedDataGetDigestByAlgTag(sigd, algiddata->offset);
d8f41ccd 549
b1ab9ed8
A
550 contentType = SecCmsContentInfoGetContentTypeOID(cinfo);
551
552 /* verify signature */
553 status = SecCmsSignerInfoVerify(signerinfo, digest, contentType);
5c19dc3a
A
554#if SECTRUST_VERBOSE_DEBUG
555 syslog(LOG_ERR, "SecCmsSignedDataVerifySignerInfo: SecCmsSignerInfoVerify returned %d, will %sverify cert",
556 (int)status, (status) ? "NOT " : "");
557#endif
558 if (status) {
559 return status;
560 }
b1ab9ed8 561
d8f41ccd
A
562 /* Now verify the certificate. We do this even if the signature failed to verify so we can
563 return a trustRef to the caller for display purposes. */
564 status = SecCmsSignerInfoVerifyCertificate(signerinfo, keychainOrArray, policies, trustRef);
5c19dc3a
A
565#if SECTRUST_VERBOSE_DEBUG
566 syslog(LOG_ERR, "SecCmsSignedDataVerifySignerInfo: SecCmsSignerInfoVerifyCertificate returned %d", (int)status);
567#endif
d8f41ccd
A
568
569 return status;
b1ab9ed8
A
570}
571
d8f41ccd
A
572#if USE_CDSA_CRYPTO
573
b1ab9ed8
A
574/*
575 * SecCmsSignedDataVerifyCertsOnly - verify the certs in a certs-only message
576 */
577OSStatus
578SecCmsSignedDataVerifyCertsOnly(SecCmsSignedDataRef sigd,
579 SecKeychainRef keychainOrArray,
580 CFTypeRef policies)
581{
582 SecCertificateRef cert;
583 OSStatus rv = SECSuccess;
584 int i;
585 int count;
586
587 if (!sigd || !keychainOrArray || !sigd->rawCerts) {
588 PORT_SetError(SEC_ERROR_INVALID_ARGS);
589 return SECFailure;
590 }
591
592 count = SecCmsArrayCount((void**)sigd->rawCerts);
593 for (i=0; i < count; i++) {
594 if (sigd->certs && CFArrayGetCount(sigd->certs) > i) {
595 cert = (SecCertificateRef)CFArrayGetValueAtIndex(sigd->certs, i);
596 CFRetain(cert);
597 } else {
598 cert = CERT_FindCertByDERCert(keychainOrArray, sigd->rawCerts[i]);
599 if (!cert) {
600 rv = SECFailure;
601 break;
602 }
603 }
d8f41ccd 604 rv |= CERT_VerifyCert(keychainOrArray, cert, policies, CFAbsoluteTimeGetCurrent(), NULL);
b1ab9ed8
A
605 CFRelease(cert);
606 }
607
608 return rv;
609}
d8f41ccd
A
610#else
611OSStatus
612SecCmsSignedDataVerifyCertsOnly(SecCmsSignedDataRef sigd,
613 SecKeychainRef keychainOrArray,
614 CFTypeRef policies)
615{
616 OSStatus rv = SECSuccess;
617
618 if (!sigd || !keychainOrArray || !sigd->rawCerts) {
619 PORT_SetError(SEC_ERROR_INVALID_ARGS);
620 return SECFailure;
621 }
622
623 SecAsn1Item **cert_datas = sigd->rawCerts;
624 SecAsn1Item *cert_data;
625 while ((cert_data = *cert_datas++) != NULL) {
626 SecCertificateRef cert = SecCertificateCreateWithBytes(NULL, cert_data->Data, cert_data->Length);
627 if (cert) {
628 CFArrayRef certs = CFArrayCreate(kCFAllocatorDefault, (const void **)&cert, 1, NULL);
629 rv |= CERT_VerifyCert(keychainOrArray, certs, policies, CFAbsoluteTimeGetCurrent(), NULL);
630 CFRelease(certs);
631 CFRelease(cert);
632 }
633 else
634 rv |= SECFailure;
635 }
636
637 return rv;
638}
639#endif
b1ab9ed8
A
640
641/*
642 * SecCmsSignedDataHasDigests - see if we have digests in place
643 */
644Boolean
645SecCmsSignedDataHasDigests(SecCmsSignedDataRef sigd)
646{
647 return (sigd->digests != NULL);
648}
649
650OSStatus
651SecCmsSignedDataAddCertList(SecCmsSignedDataRef sigd, CFArrayRef certlist)
652{
653 PORT_Assert(certlist != NULL);
654
655 if (certlist == NULL)
656 return SECFailure;
657
658 if (!sigd->certs)
659 sigd->certs = CFArrayCreateMutableCopy(NULL, 0, certlist);
660 else
661 {
662 CFRange certlistRange = { 0, CFArrayGetCount(certlist) };
663 CFArrayAppendArray(sigd->certs, certlist, certlistRange);
664 }
665
666 return SECSuccess;
667}
668
669/*
670 * SecCmsSignedDataAddCertChain - add cert and its entire chain to the set of certs
671 */
672OSStatus
673SecCmsSignedDataAddCertChain(SecCmsSignedDataRef sigd, SecCertificateRef cert)
674{
675 CFArrayRef certlist;
676 SECCertUsage usage;
677 OSStatus rv;
678
679 usage = certUsageEmailSigner;
680
681 /* do not include root */
682 certlist = CERT_CertChainFromCert(cert, usage, PR_FALSE);
683 if (certlist == NULL)
684 return SECFailure;
685
686 rv = SecCmsSignedDataAddCertList(sigd, certlist);
687 CFRelease(certlist);
688
689 return rv;
690}
691
692OSStatus
693SecCmsSignedDataAddCertificate(SecCmsSignedDataRef sigd, SecCertificateRef cert)
694{
695 PORT_Assert(cert != NULL);
696
697 if (cert == NULL)
698 return SECFailure;
699
700 if (!sigd->certs)
701 sigd->certs = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
702
703 CFArrayAppendValue(sigd->certs, cert);
704
705 return SECSuccess;
706}
707
708Boolean
709SecCmsSignedDataContainsCertsOrCrls(SecCmsSignedDataRef sigd)
710{
711 if (sigd->rawCerts != NULL && sigd->rawCerts[0] != NULL)
712 return PR_TRUE;
713 else if (sigd->rawCrls != NULL && sigd->rawCrls[0] != NULL)
714 return PR_TRUE;
715 else
716 return PR_FALSE;
717}
718
719OSStatus
720SecCmsSignedDataAddSignerInfo(SecCmsSignedDataRef sigd,
d8f41ccd 721 SecCmsSignerInfoRef signerinfo)
b1ab9ed8
A
722{
723 void *mark;
724 OSStatus rv;
725 SECOidTag digestalgtag;
726 PLArenaPool *poolp;
727
d8f41ccd 728 poolp = sigd->contentInfo.cmsg->poolp;
b1ab9ed8
A
729
730 mark = PORT_ArenaMark(poolp);
731
732 /* add signerinfo */
733 rv = SecCmsArrayAdd(poolp, (void ***)&(sigd->signerInfos), (void *)signerinfo);
734 if (rv != SECSuccess)
735 goto loser;
736
b1ab9ed8
A
737 /*
738 * add empty digest
739 * Empty because we don't have it yet. Either it gets created during encoding
740 * (if the data is present) or has to be set externally.
741 * XXX maybe pass it in optionally?
742 */
743 digestalgtag = SecCmsSignerInfoGetDigestAlgTag(signerinfo);
744 rv = SecCmsSignedDataSetDigestValue(sigd, digestalgtag, NULL);
745 if (rv != SECSuccess)
746 goto loser;
747
748 /*
749 * The last thing to get consistency would be adding the digest.
750 */
751
752 PORT_ArenaUnmark(poolp, mark);
753 return SECSuccess;
754
755loser:
756 PORT_ArenaRelease (poolp, mark);
757 return SECFailure;
758}
759
d8f41ccd 760SecAsn1Item *
b1ab9ed8
A
761SecCmsSignedDataGetDigestByAlgTag(SecCmsSignedDataRef sigd, SECOidTag algtag)
762{
763 int idx;
764
b1ab9ed8 765 idx = SecCmsAlgArrayGetIndexByAlgTag(sigd->digestAlgorithms, algtag);
d8f41ccd
A
766 return (idx >= 0)?(sigd->digests)[idx]:NULL;
767}
768
769OSStatus
770SecCmsSignedDataSetDigestContext(SecCmsSignedDataRef sigd,
771 SecCmsDigestContextRef digestContext)
772{
773 SECAlgorithmID **digestalgs;
774 SecAsn1Item * *digests;
775
776 if (SecCmsDigestContextFinishMultiple(digestContext, &digestalgs, &digests) != SECSuccess)
777 goto loser;
778 if (SecCmsSignedDataSetDigests(sigd, digestalgs, digests) != SECSuccess)
779 goto loser;
780
781 return 0;
782loser:
783 return PORT_GetError();
b1ab9ed8
A
784}
785
786/*
787 * SecCmsSignedDataSetDigests - set a signedData's digests member
788 *
789 * "digestalgs" - array of digest algorithm IDs
790 * "digests" - array of digests corresponding to the digest algorithms
791 */
792OSStatus
793SecCmsSignedDataSetDigests(SecCmsSignedDataRef sigd,
794 SECAlgorithmID **digestalgs,
d8f41ccd 795 SecAsn1Item * *digests)
b1ab9ed8
A
796{
797 int cnt, i, idx;
798
799 if (sigd->digestAlgorithms == NULL) {
800 PORT_SetError(SEC_ERROR_INVALID_ARGS);
801 return SECFailure;
802 }
803
d8f41ccd
A
804 /* Since we'll generate a empty digest for content-less messages
805 whether or not they're detached, we have to avoid overwriting
806 externally set digest for detached content => return early */
807 if (sigd->digests && sigd->digests[0])
808 return 0;
809
b1ab9ed8 810 /* we assume that the digests array is just not there yet */
d8f41ccd 811/*
b1ab9ed8
A
812 PORT_Assert(sigd->digests == NULL);
813 if (sigd->digests != NULL) {
814 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
815 return SECFailure;
816 }
d8f41ccd 817*/
b1ab9ed8 818 /* now allocate one (same size as digestAlgorithms) */
b1ab9ed8 819 if (sigd->digests == NULL) {
d8f41ccd
A
820 cnt = SecCmsArrayCount((void **)sigd->digestAlgorithms);
821 sigd->digests = PORT_ArenaZAlloc(sigd->contentInfo.cmsg->poolp, (cnt + 1) * sizeof(SecAsn1Item *));
822 if (sigd->digests == NULL) {
823 PORT_SetError(SEC_ERROR_NO_MEMORY);
824 return SECFailure;
825 }
b1ab9ed8 826 }
d8f41ccd 827
b1ab9ed8
A
828 for (i = 0; sigd->digestAlgorithms[i] != NULL; i++) {
829 /* try to find the sigd's i'th digest algorithm in the array we passed in */
830 idx = SecCmsAlgArrayGetIndexByAlgID(digestalgs, sigd->digestAlgorithms[i]);
831 if (idx < 0) {
832 PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);
833 return SECFailure;
834 }
835
836 /* found it - now set it */
d8f41ccd
A
837 if ((sigd->digests[i] = SECITEM_AllocItem(sigd->contentInfo.cmsg->poolp, NULL, 0)) == NULL ||
838 SECITEM_CopyItem(sigd->contentInfo.cmsg->poolp, sigd->digests[i], digests[idx]) != SECSuccess)
b1ab9ed8
A
839 {
840 PORT_SetError(SEC_ERROR_NO_MEMORY);
841 return SECFailure;
842 }
843 }
844 return SECSuccess;
845}
846
847OSStatus
848SecCmsSignedDataSetDigestValue(SecCmsSignedDataRef sigd,
849 SECOidTag digestalgtag,
d8f41ccd 850 SecAsn1Item * digestdata)
b1ab9ed8 851{
d8f41ccd 852 SecAsn1Item * digest = NULL;
b1ab9ed8
A
853 PLArenaPool *poolp;
854 void *mark;
855 int n, cnt;
856
d8f41ccd 857 poolp = sigd->contentInfo.cmsg->poolp;
b1ab9ed8
A
858
859 mark = PORT_ArenaMark(poolp);
860
861
862 if (digestdata) {
d8f41ccd 863 digest = (SecAsn1Item *) PORT_ArenaZAlloc(poolp,sizeof(SecAsn1Item));
b1ab9ed8
A
864
865 /* copy digestdata item to arena (in case we have it and are not only making room) */
866 if (SECITEM_CopyItem(poolp, digest, digestdata) != SECSuccess)
867 goto loser;
868 }
869
870 /* now allocate one (same size as digestAlgorithms) */
871 if (sigd->digests == NULL) {
872 cnt = SecCmsArrayCount((void **)sigd->digestAlgorithms);
d8f41ccd 873 sigd->digests = PORT_ArenaZAlloc(sigd->contentInfo.cmsg->poolp, (cnt + 1) * sizeof(SecAsn1Item *));
b1ab9ed8
A
874 if (sigd->digests == NULL) {
875 PORT_SetError(SEC_ERROR_NO_MEMORY);
876 return SECFailure;
877 }
878 }
879
880 n = -1;
881 if (sigd->digestAlgorithms != NULL)
882 n = SecCmsAlgArrayGetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
883
884 /* if not found, add a digest */
885 if (n < 0) {
d8f41ccd 886 if (SecCmsSignedDataAddDigest(poolp, sigd, digestalgtag, digest) != SECSuccess)
b1ab9ed8
A
887 goto loser;
888 } else {
889 /* replace NULL pointer with digest item (and leak previous value) */
890 sigd->digests[n] = digest;
891 }
892
893 PORT_ArenaUnmark(poolp, mark);
894 return SECSuccess;
895
896loser:
897 PORT_ArenaRelease(poolp, mark);
898 return SECFailure;
899}
900
901OSStatus
d8f41ccd 902SecCmsSignedDataAddDigest(PRArenaPool *poolp,
b1ab9ed8
A
903 SecCmsSignedDataRef sigd,
904 SECOidTag digestalgtag,
d8f41ccd 905 SecAsn1Item * digest)
b1ab9ed8 906{
b1ab9ed8
A
907 SECAlgorithmID *digestalg;
908 void *mark;
909
910 mark = PORT_ArenaMark(poolp);
911
912 digestalg = PORT_ArenaZAlloc(poolp, sizeof(SECAlgorithmID));
913 if (digestalg == NULL)
914 goto loser;
915
916 if (SECOID_SetAlgorithmID (poolp, digestalg, digestalgtag, NULL) != SECSuccess) /* no params */
917 goto loser;
918
919 if (SecCmsArrayAdd(poolp, (void ***)&(sigd->digestAlgorithms), (void *)digestalg) != SECSuccess ||
920 /* even if digest is NULL, add dummy to have same-size array */
921 SecCmsArrayAdd(poolp, (void ***)&(sigd->digests), (void *)digest) != SECSuccess)
922 {
923 goto loser;
924 }
925
926 PORT_ArenaUnmark(poolp, mark);
927 return SECSuccess;
928
929loser:
930 PORT_ArenaRelease(poolp, mark);
931 return SECFailure;
932}
933
d8f41ccd 934SecAsn1Item *
b1ab9ed8
A
935SecCmsSignedDataGetDigestValue(SecCmsSignedDataRef sigd, SECOidTag digestalgtag)
936{
937 int n;
938
939 if (sigd->digestAlgorithms == NULL)
940 return NULL;
941
942 n = SecCmsAlgArrayGetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
943
944 return (n < 0) ? NULL : sigd->digests[n];
945}
946
947/* =============================================================================
948 * Misc. utility functions
949 */
950
951/*
952 * SecCmsSignedDataCreateCertsOnly - create a certs-only SignedData.
953 *
954 * cert - base certificates that will be included
955 * include_chain - if true, include the complete cert chain for cert
956 *
957 * More certs and chains can be added via AddCertificate and AddCertChain.
958 *
959 * An error results in a return value of NULL and an error set.
960 *
961 * XXXX CRLs
962 */
963SecCmsSignedDataRef
964SecCmsSignedDataCreateCertsOnly(SecCmsMessageRef cmsg, SecCertificateRef cert, Boolean include_chain)
965{
966 SecCmsSignedDataRef sigd;
967 void *mark;
968 PLArenaPool *poolp;
969 OSStatus rv;
970
971 poolp = cmsg->poolp;
972 mark = PORT_ArenaMark(poolp);
973
974 sigd = SecCmsSignedDataCreate(cmsg);
975 if (sigd == NULL)
976 goto loser;
977
978 /* no signerinfos, thus no digestAlgorithms */
979
980 /* but certs */
981 if (include_chain) {
982 rv = SecCmsSignedDataAddCertChain(sigd, cert);
983 } else {
984 rv = SecCmsSignedDataAddCertificate(sigd, cert);
985 }
986 if (rv != SECSuccess)
987 goto loser;
988
989 /* RFC2630 5.2 sez:
990 * In the degenerate case where there are no signers, the
991 * EncapsulatedContentInfo value being "signed" is irrelevant. In this
992 * case, the content type within the EncapsulatedContentInfo value being
993 * "signed" should be id-data (as defined in section 4), and the content
994 * field of the EncapsulatedContentInfo value should be omitted.
995 */
d8f41ccd 996 rv = SecCmsContentInfoSetContentData(&(sigd->contentInfo), NULL, PR_TRUE);
b1ab9ed8
A
997 if (rv != SECSuccess)
998 goto loser;
999
1000 PORT_ArenaUnmark(poolp, mark);
1001 return sigd;
1002
1003loser:
1004 if (sigd)
1005 SecCmsSignedDataDestroy(sigd);
1006 PORT_ArenaRelease(poolp, mark);
1007 return NULL;
1008}
1009
b1ab9ed8
A
1010/* TODO:
1011 * SecCmsSignerInfoGetReceiptRequest()
1012 * SecCmsSignedDataHasReceiptRequest()
1013 * easy way to iterate over signers
1014 */
1015