Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / smime / cmssigdata.c
blob81ece5473dbe2914b334358d69d50a96adee440d
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * CMS signedData methods.
40 * $Id: cmssigdata.c,v 1.29 2005/06/27 22:21:18 julien.pierre.bugs%sun.com Exp $
43 #include "cmslocal.h"
45 #include "cert.h"
46 /*#include "cdbhdl.h"*/
47 #include "secasn1.h"
48 #include "secitem.h"
49 #include "secoid.h"
50 #include "pk11func.h"
51 #include "secerr.h"
53 NSSCMSSignedData *
54 NSS_CMSSignedData_Create(NSSCMSMessage *cmsg)
56 void *mark;
57 NSSCMSSignedData *sigd;
58 PLArenaPool *poolp;
60 if (!cmsg) {
61 PORT_SetError(SEC_ERROR_INVALID_ARGS);
62 return NULL;
65 poolp = cmsg->poolp;
67 mark = PORT_ArenaMark(poolp);
69 sigd = (NSSCMSSignedData *)PORT_ArenaZAlloc (poolp, sizeof(NSSCMSSignedData));
70 if (sigd == NULL)
71 goto loser;
73 sigd->cmsg = cmsg;
75 /* signerInfos, certs, certlists, crls are all empty */
76 /* version is set in NSS_CMSSignedData_Finalize() */
78 PORT_ArenaUnmark(poolp, mark);
79 return sigd;
81 loser:
82 PORT_ArenaRelease(poolp, mark);
83 return NULL;
86 void
87 NSS_CMSSignedData_Destroy(NSSCMSSignedData *sigd)
89 CERTCertificate **certs, **tempCerts, *cert;
90 CERTCertificateList **certlists, *certlist;
91 NSSCMSSignerInfo **signerinfos, *si;
93 if (sigd == NULL)
94 return;
96 certs = sigd->certs;
97 tempCerts = sigd->tempCerts;
98 certlists = sigd->certLists;
99 signerinfos = sigd->signerInfos;
101 if (certs != NULL) {
102 while ((cert = *certs++) != NULL)
103 CERT_DestroyCertificate (cert);
106 if (tempCerts != NULL) {
107 while ((cert = *tempCerts++) != NULL)
108 CERT_DestroyCertificate (cert);
111 if (certlists != NULL) {
112 while ((certlist = *certlists++) != NULL)
113 CERT_DestroyCertificateList (certlist);
116 if (signerinfos != NULL) {
117 while ((si = *signerinfos++) != NULL)
118 NSS_CMSSignerInfo_Destroy(si);
121 /* everything's in a pool, so don't worry about the storage */
122 NSS_CMSContentInfo_Destroy(&(sigd->contentInfo));
127 * NSS_CMSSignedData_Encode_BeforeStart - do all the necessary things to a SignedData
128 * before start of encoding.
130 * In detail:
131 * - find out about the right value to put into sigd->version
132 * - come up with a list of digestAlgorithms (which should be the union of the algorithms
133 * in the signerinfos).
134 * If we happen to have a pre-set list of algorithms (and digest values!), we
135 * check if we have all the signerinfos' algorithms. If not, this is an error.
137 SECStatus
138 NSS_CMSSignedData_Encode_BeforeStart(NSSCMSSignedData *sigd)
140 NSSCMSSignerInfo *signerinfo;
141 SECOidTag digestalgtag;
142 SECItem *dummy;
143 int version;
144 SECStatus rv;
145 PRBool haveDigests = PR_FALSE;
146 int n, i;
147 PLArenaPool *poolp;
149 if (!sigd) {
150 PORT_SetError(SEC_ERROR_INVALID_ARGS);
151 return SECFailure;
154 poolp = sigd->cmsg->poolp;
156 /* we assume that we have precomputed digests if there is a list of algorithms, and */
157 /* a chunk of data for each of those algorithms */
158 if (sigd->digestAlgorithms != NULL && sigd->digests != NULL) {
159 for (i=0; sigd->digestAlgorithms[i] != NULL; i++) {
160 if (sigd->digests[i] == NULL)
161 break;
163 if (sigd->digestAlgorithms[i] == NULL) /* reached the end of the array? */
164 haveDigests = PR_TRUE; /* yes: we must have all the digests */
167 version = NSS_CMS_SIGNED_DATA_VERSION_BASIC;
169 /* RFC2630 5.1 "version is the syntax version number..." */
170 if (NSS_CMSContentInfo_GetContentTypeTag(&(sigd->contentInfo)) != SEC_OID_PKCS7_DATA)
171 version = NSS_CMS_SIGNED_DATA_VERSION_EXT;
173 /* prepare all the SignerInfos (there may be none) */
174 for (i=0; i < NSS_CMSSignedData_SignerInfoCount(sigd); i++) {
175 signerinfo = NSS_CMSSignedData_GetSignerInfo(sigd, i);
177 /* RFC2630 5.1 "version is the syntax version number..." */
178 if (NSS_CMSSignerInfo_GetVersion(signerinfo) != NSS_CMS_SIGNER_INFO_VERSION_ISSUERSN)
179 version = NSS_CMS_SIGNED_DATA_VERSION_EXT;
181 /* collect digestAlgorithms from SignerInfos */
182 /* (we need to know which algorithms we have when the content comes in) */
183 /* do not overwrite any existing digestAlgorithms (and digest) */
184 digestalgtag = NSS_CMSSignerInfo_GetDigestAlgTag(signerinfo);
185 n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
186 if (n < 0 && haveDigests) {
187 /* oops, there is a digestalg we do not have a digest for */
188 /* but we were supposed to have all the digests already... */
189 goto loser;
190 } else if (n < 0) {
191 /* add the digestAlgorithm & a NULL digest */
192 rv = NSS_CMSSignedData_AddDigest(poolp, sigd, digestalgtag, NULL);
193 if (rv != SECSuccess)
194 goto loser;
195 } else {
196 /* found it, nothing to do */
200 dummy = SEC_ASN1EncodeInteger(poolp, &(sigd->version), (long)version);
201 if (dummy == NULL)
202 return SECFailure;
204 /* this is a SET OF, so we need to sort them guys */
205 rv = NSS_CMSArray_SortByDER((void **)sigd->digestAlgorithms,
206 SEC_ASN1_GET(SECOID_AlgorithmIDTemplate),
207 (void **)sigd->digests);
208 if (rv != SECSuccess)
209 return SECFailure;
211 return SECSuccess;
213 loser:
214 return SECFailure;
217 SECStatus
218 NSS_CMSSignedData_Encode_BeforeData(NSSCMSSignedData *sigd)
220 if (!sigd) {
221 PORT_SetError(SEC_ERROR_INVALID_ARGS);
222 return SECFailure;
224 /* set up the digests */
225 if (sigd->digests && sigd->digests[0]) {
226 sigd->contentInfo.digcx = NULL; /* don't attempt to make new ones. */
227 } else if (sigd->digestAlgorithms != NULL) {
228 sigd->contentInfo.digcx =
229 NSS_CMSDigestContext_StartMultiple(sigd->digestAlgorithms);
230 if (sigd->contentInfo.digcx == NULL)
231 return SECFailure;
233 return SECSuccess;
237 * NSS_CMSSignedData_Encode_AfterData - do all the necessary things to a SignedData
238 * after all the encapsulated data was passed through the encoder.
240 * In detail:
241 * - create the signatures in all the SignerInfos
243 * Please note that nothing is done to the Certificates and CRLs in the message - this
244 * is entirely the responsibility of our callers.
246 SECStatus
247 NSS_CMSSignedData_Encode_AfterData(NSSCMSSignedData *sigd)
249 NSSCMSSignerInfo **signerinfos, *signerinfo;
250 NSSCMSContentInfo *cinfo;
251 SECOidTag digestalgtag;
252 SECStatus ret = SECFailure;
253 SECStatus rv;
254 SECItem *contentType;
255 int certcount;
256 int i, ci, cli, n, rci, si;
257 PLArenaPool *poolp;
258 CERTCertificateList *certlist;
259 extern const SEC_ASN1Template NSSCMSSignerInfoTemplate[];
261 if (!sigd) {
262 PORT_SetError(SEC_ERROR_INVALID_ARGS);
263 return SECFailure;
266 poolp = sigd->cmsg->poolp;
267 cinfo = &(sigd->contentInfo);
269 /* did we have digest calculation going on? */
270 if (cinfo->digcx) {
271 rv = NSS_CMSDigestContext_FinishMultiple(cinfo->digcx, poolp,
272 &(sigd->digests));
273 /* error has been set by NSS_CMSDigestContext_FinishMultiple */
274 cinfo->digcx = NULL;
275 if (rv != SECSuccess)
276 goto loser;
279 signerinfos = sigd->signerInfos;
280 certcount = 0;
282 /* prepare all the SignerInfos (there may be none) */
283 for (i=0; i < NSS_CMSSignedData_SignerInfoCount(sigd); i++) {
284 signerinfo = NSS_CMSSignedData_GetSignerInfo(sigd, i);
286 /* find correct digest for this signerinfo */
287 digestalgtag = NSS_CMSSignerInfo_GetDigestAlgTag(signerinfo);
288 n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
289 if (n < 0 || sigd->digests == NULL || sigd->digests[n] == NULL) {
290 /* oops - digest not found */
291 PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);
292 goto loser;
295 /* XXX if our content is anything else but data, we need to force the
296 * presence of signed attributes (RFC2630 5.3 "signedAttributes is a
297 * collection...") */
299 /* pass contentType here as we want a contentType attribute */
300 if ((contentType = NSS_CMSContentInfo_GetContentTypeOID(cinfo)) == NULL)
301 goto loser;
303 /* sign the thing */
304 rv = NSS_CMSSignerInfo_Sign(signerinfo, sigd->digests[n], contentType);
305 if (rv != SECSuccess)
306 goto loser;
308 /* while we're at it, count number of certs in certLists */
309 certlist = NSS_CMSSignerInfo_GetCertList(signerinfo);
310 if (certlist)
311 certcount += certlist->len;
314 /* this is a SET OF, so we need to sort them guys */
315 rv = NSS_CMSArray_SortByDER((void **)signerinfos, NSSCMSSignerInfoTemplate, NULL);
316 if (rv != SECSuccess)
317 goto loser;
320 * now prepare certs & crls
323 /* count the rest of the certs */
324 if (sigd->certs != NULL) {
325 for (ci = 0; sigd->certs[ci] != NULL; ci++)
326 certcount++;
329 if (sigd->certLists != NULL) {
330 for (cli = 0; sigd->certLists[cli] != NULL; cli++)
331 certcount += sigd->certLists[cli]->len;
334 if (certcount == 0) {
335 sigd->rawCerts = NULL;
336 } else {
338 * Combine all of the certs and cert chains into rawcerts.
339 * Note: certcount is an upper bound; we may not need that many slots
340 * but we will allocate anyway to avoid having to do another pass.
341 * (The temporary space saving is not worth it.)
343 * XXX ARGH - this NEEDS to be fixed. need to come up with a decent
344 * SetOfDERcertficates implementation
346 sigd->rawCerts = (SECItem **)PORT_ArenaAlloc(poolp, (certcount + 1) * sizeof(SECItem *));
347 if (sigd->rawCerts == NULL)
348 return SECFailure;
351 * XXX Want to check for duplicates and not add *any* cert that is
352 * already in the set. This will be more important when we start
353 * dealing with larger sets of certs, dual-key certs (signing and
354 * encryption), etc. For the time being we can slide by...
356 * XXX ARGH - this NEEDS to be fixed. need to come up with a decent
357 * SetOfDERcertficates implementation
359 rci = 0;
360 if (signerinfos != NULL) {
361 for (si = 0; signerinfos[si] != NULL; si++) {
362 signerinfo = signerinfos[si];
363 for (ci = 0; ci < signerinfo->certList->len; ci++)
364 sigd->rawCerts[rci++] = &(signerinfo->certList->certs[ci]);
368 if (sigd->certs != NULL) {
369 for (ci = 0; sigd->certs[ci] != NULL; ci++)
370 sigd->rawCerts[rci++] = &(sigd->certs[ci]->derCert);
373 if (sigd->certLists != NULL) {
374 for (cli = 0; sigd->certLists[cli] != NULL; cli++) {
375 for (ci = 0; ci < sigd->certLists[cli]->len; ci++)
376 sigd->rawCerts[rci++] = &(sigd->certLists[cli]->certs[ci]);
380 sigd->rawCerts[rci] = NULL;
382 /* this is a SET OF, so we need to sort them guys - we have the DER already, though */
383 NSS_CMSArray_Sort((void **)sigd->rawCerts, NSS_CMSUtil_DERCompare, NULL, NULL);
386 ret = SECSuccess;
388 loser:
389 return ret;
392 SECStatus
393 NSS_CMSSignedData_Decode_BeforeData(NSSCMSSignedData *sigd)
395 if (!sigd) {
396 PORT_SetError(SEC_ERROR_INVALID_ARGS);
397 return SECFailure;
399 /* set up the digests */
400 if (sigd->digestAlgorithms != NULL && sigd->digests == NULL) {
401 /* if digests are already there, do nothing */
402 sigd->contentInfo.digcx = NSS_CMSDigestContext_StartMultiple(sigd->digestAlgorithms);
403 if (sigd->contentInfo.digcx == NULL)
404 return SECFailure;
406 return SECSuccess;
410 * NSS_CMSSignedData_Decode_AfterData - do all the necessary things to a
411 * SignedData after all the encapsulated data was passed through the decoder.
413 SECStatus
414 NSS_CMSSignedData_Decode_AfterData(NSSCMSSignedData *sigd)
416 SECStatus rv = SECSuccess;
418 if (!sigd) {
419 PORT_SetError(SEC_ERROR_INVALID_ARGS);
420 return SECFailure;
423 /* did we have digest calculation going on? */
424 if (sigd->contentInfo.digcx) {
425 rv = NSS_CMSDigestContext_FinishMultiple(sigd->contentInfo.digcx,
426 sigd->cmsg->poolp, &(sigd->digests));
427 /* error set by NSS_CMSDigestContext_FinishMultiple */
428 sigd->contentInfo.digcx = NULL;
430 return rv;
434 * NSS_CMSSignedData_Decode_AfterEnd - do all the necessary things to a SignedData
435 * after all decoding is finished.
437 SECStatus
438 NSS_CMSSignedData_Decode_AfterEnd(NSSCMSSignedData *sigd)
440 NSSCMSSignerInfo **signerinfos = NULL;
441 int i;
443 if (!sigd) {
444 PORT_SetError(SEC_ERROR_INVALID_ARGS);
445 return SECFailure;
448 /* set cmsg for all the signerinfos */
449 signerinfos = sigd->signerInfos;
451 /* set cmsg for all the signerinfos */
452 if (signerinfos) {
453 for (i = 0; signerinfos[i] != NULL; i++)
454 signerinfos[i]->cmsg = sigd->cmsg;
457 return SECSuccess;
461 * NSS_CMSSignedData_GetSignerInfos - retrieve the SignedData's signer list
463 NSSCMSSignerInfo **
464 NSS_CMSSignedData_GetSignerInfos(NSSCMSSignedData *sigd)
466 if (!sigd) {
467 PORT_SetError(SEC_ERROR_INVALID_ARGS);
468 return NULL;
470 return sigd->signerInfos;
474 NSS_CMSSignedData_SignerInfoCount(NSSCMSSignedData *sigd)
476 if (!sigd) {
477 PORT_SetError(SEC_ERROR_INVALID_ARGS);
478 return 0;
480 return NSS_CMSArray_Count((void **)sigd->signerInfos);
483 NSSCMSSignerInfo *
484 NSS_CMSSignedData_GetSignerInfo(NSSCMSSignedData *sigd, int i)
486 if (!sigd) {
487 PORT_SetError(SEC_ERROR_INVALID_ARGS);
488 return NULL;
490 return sigd->signerInfos[i];
494 * NSS_CMSSignedData_GetDigestAlgs - retrieve the SignedData's digest algorithm list
496 SECAlgorithmID **
497 NSS_CMSSignedData_GetDigestAlgs(NSSCMSSignedData *sigd)
499 if (!sigd) {
500 PORT_SetError(SEC_ERROR_INVALID_ARGS);
501 return NULL;
503 return sigd->digestAlgorithms;
507 * NSS_CMSSignedData_GetContentInfo - return pointer to this signedData's contentinfo
509 NSSCMSContentInfo *
510 NSS_CMSSignedData_GetContentInfo(NSSCMSSignedData *sigd)
512 if (!sigd) {
513 PORT_SetError(SEC_ERROR_INVALID_ARGS);
514 return NULL;
516 return &(sigd->contentInfo);
520 * NSS_CMSSignedData_GetCertificateList - retrieve the SignedData's certificate list
522 SECItem **
523 NSS_CMSSignedData_GetCertificateList(NSSCMSSignedData *sigd)
525 if (!sigd) {
526 PORT_SetError(SEC_ERROR_INVALID_ARGS);
527 return NULL;
529 return sigd->rawCerts;
532 SECStatus
533 NSS_CMSSignedData_ImportCerts(NSSCMSSignedData *sigd, CERTCertDBHandle *certdb,
534 SECCertUsage certusage, PRBool keepcerts)
536 int certcount;
537 CERTCertificate **certArray = NULL;
538 CERTCertList *certList = NULL;
539 CERTCertListNode *node;
540 SECStatus rv;
541 SECItem **rawArray;
542 int i;
543 PRTime now;
545 if (!sigd) {
546 PORT_SetError(SEC_ERROR_INVALID_ARGS);
547 return SECFailure;
550 certcount = NSS_CMSArray_Count((void **)sigd->rawCerts);
552 /* get the certs in the temp DB */
553 rv = CERT_ImportCerts(certdb, certusage, certcount, sigd->rawCerts,
554 &certArray, PR_FALSE, PR_FALSE, NULL);
555 if (rv != SECSuccess) {
556 goto loser;
559 /* save the certs so they don't get destroyed */
560 for (i=0; i < certcount; i++) {
561 CERTCertificate *cert = certArray[i];
562 if (cert)
563 NSS_CMSSignedData_AddTempCertificate(sigd, cert);
566 if (!keepcerts) {
567 goto done;
570 /* build a CertList for filtering */
571 certList = CERT_NewCertList();
572 if (certList == NULL) {
573 rv = SECFailure;
574 goto loser;
576 for (i=0; i < certcount; i++) {
577 CERTCertificate *cert = certArray[i];
578 if (cert)
579 cert = CERT_DupCertificate(cert);
580 if (cert)
581 CERT_AddCertToListTail(certList,cert);
584 /* filter out the certs we don't want */
585 rv = CERT_FilterCertListByUsage(certList,certusage, PR_FALSE);
586 if (rv != SECSuccess) {
587 goto loser;
590 /* go down the remaining list of certs and verify that they have
591 * valid chains, then import them.
593 now = PR_Now();
594 for (node = CERT_LIST_HEAD(certList) ; !CERT_LIST_END(node,certList);
595 node= CERT_LIST_NEXT(node)) {
596 CERTCertificateList *certChain;
598 if (CERT_VerifyCert(certdb, node->cert,
599 PR_TRUE, certusage, now, NULL, NULL) != SECSuccess) {
600 continue;
603 certChain = CERT_CertChainFromCert(node->cert, certusage, PR_FALSE);
604 if (!certChain) {
605 continue;
609 * CertChain returns an array of SECItems, import expects an array of
610 * SECItem pointers. Create the SECItem Pointers from the array of
611 * SECItems.
613 rawArray = (SECItem **)PORT_Alloc(certChain->len*sizeof (SECItem *));
614 if (!rawArray) {
615 CERT_DestroyCertificateList(certChain);
616 continue;
618 for (i=0; i < certChain->len; i++) {
619 rawArray[i] = &certChain->certs[i];
621 (void )CERT_ImportCerts(certdb, certusage, certChain->len,
622 rawArray, NULL, keepcerts, PR_FALSE, NULL);
623 PORT_Free(rawArray);
624 CERT_DestroyCertificateList(certChain);
627 rv = SECSuccess;
629 /* XXX CRL handling */
631 done:
632 if (sigd->signerInfos != NULL) {
633 /* fill in all signerinfo's certs */
634 for (i = 0; sigd->signerInfos[i] != NULL; i++)
635 (void)NSS_CMSSignerInfo_GetSigningCertificate(
636 sigd->signerInfos[i], certdb);
639 loser:
640 /* now free everything */
641 if (certArray) {
642 CERT_DestroyCertArray(certArray,certcount);
644 if (certList) {
645 CERT_DestroyCertList(certList);
648 return rv;
652 * XXX the digests need to be passed in BETWEEN the decoding and the verification in case
653 * of external signatures!
657 * NSS_CMSSignedData_VerifySignerInfo - check the signatures.
659 * The digests were either calculated during decoding (and are stored in the
660 * signedData itself) or set after decoding using NSS_CMSSignedData_SetDigests.
662 * The verification checks if the signing cert is valid and has a trusted chain
663 * for the purpose specified by "certusage".
665 SECStatus
666 NSS_CMSSignedData_VerifySignerInfo(NSSCMSSignedData *sigd, int i,
667 CERTCertDBHandle *certdb, SECCertUsage certusage)
669 NSSCMSSignerInfo *signerinfo;
670 NSSCMSContentInfo *cinfo;
671 SECOidData *algiddata;
672 SECItem *contentType, *digest;
673 SECOidTag oidTag;
674 SECStatus rv;
676 if (!sigd) {
677 PORT_SetError(SEC_ERROR_INVALID_ARGS);
678 return SECFailure;
681 cinfo = &(sigd->contentInfo);
683 signerinfo = sigd->signerInfos[i];
685 /* verify certificate */
686 rv = NSS_CMSSignerInfo_VerifyCertificate(signerinfo, certdb, certusage);
687 if (rv != SECSuccess)
688 return rv; /* error is set */
690 /* find digest and contentType for signerinfo */
691 algiddata = NSS_CMSSignerInfo_GetDigestAlg(signerinfo);
692 oidTag = algiddata ? algiddata->offset : SEC_OID_UNKNOWN;
693 digest = NSS_CMSSignedData_GetDigestValue(sigd, oidTag);
694 /* NULL digest is acceptable. */
695 contentType = NSS_CMSContentInfo_GetContentTypeOID(cinfo);
696 /* NULL contentType is acceptable. */
698 /* now verify signature */
699 rv = NSS_CMSSignerInfo_Verify(signerinfo, digest, contentType);
700 return rv;
704 * NSS_CMSSignedData_VerifyCertsOnly - verify the certs in a certs-only message
706 SECStatus
707 NSS_CMSSignedData_VerifyCertsOnly(NSSCMSSignedData *sigd,
708 CERTCertDBHandle *certdb,
709 SECCertUsage usage)
711 CERTCertificate *cert;
712 SECStatus rv = SECSuccess;
713 int i;
714 int count;
715 PRTime now;
717 if (!sigd || !certdb || !sigd->rawCerts) {
718 PORT_SetError(SEC_ERROR_INVALID_ARGS);
719 return SECFailure;
722 count = NSS_CMSArray_Count((void**)sigd->rawCerts);
723 now = PR_Now();
724 for (i=0; i < count; i++) {
725 if (sigd->certs && sigd->certs[i]) {
726 cert = CERT_DupCertificate(sigd->certs[i]);
727 } else {
728 cert = CERT_FindCertByDERCert(certdb, sigd->rawCerts[i]);
729 if (!cert) {
730 rv = SECFailure;
731 break;
734 rv |= CERT_VerifyCert(certdb, cert, PR_TRUE, usage, now,
735 NULL, NULL);
736 CERT_DestroyCertificate(cert);
739 return rv;
743 * NSS_CMSSignedData_HasDigests - see if we have digests in place
745 PRBool
746 NSS_CMSSignedData_HasDigests(NSSCMSSignedData *sigd)
748 if (!sigd) {
749 PORT_SetError(SEC_ERROR_INVALID_ARGS);
750 return PR_FALSE;
752 return (sigd->digests != NULL);
755 SECStatus
756 NSS_CMSSignedData_AddCertList(NSSCMSSignedData *sigd, CERTCertificateList *certlist)
758 SECStatus rv;
760 if (!sigd || !certlist) {
761 PORT_SetError(SEC_ERROR_INVALID_ARGS);
762 return SECFailure;
765 /* XXX memory?? a certlist has an arena of its own and is not refcounted!?!? */
766 rv = NSS_CMSArray_Add(sigd->cmsg->poolp, (void ***)&(sigd->certLists), (void *)certlist);
768 return rv;
772 * NSS_CMSSignedData_AddCertChain - add cert and its entire chain to the set of certs
774 SECStatus
775 NSS_CMSSignedData_AddCertChain(NSSCMSSignedData *sigd, CERTCertificate *cert)
777 CERTCertificateList *certlist;
778 SECCertUsage usage;
779 SECStatus rv;
781 usage = certUsageEmailSigner;
783 if (!sigd || !cert) {
784 PORT_SetError(SEC_ERROR_INVALID_ARGS);
785 return SECFailure;
788 /* do not include root */
789 certlist = CERT_CertChainFromCert(cert, usage, PR_FALSE);
790 if (certlist == NULL)
791 return SECFailure;
793 rv = NSS_CMSSignedData_AddCertList(sigd, certlist);
795 return rv;
798 extern SECStatus
799 NSS_CMSSignedData_AddTempCertificate(NSSCMSSignedData *sigd, CERTCertificate *cert)
801 CERTCertificate *c;
802 SECStatus rv;
804 if (!sigd || !cert) {
805 PORT_SetError(SEC_ERROR_INVALID_ARGS);
806 return SECFailure;
809 c = CERT_DupCertificate(cert);
810 rv = NSS_CMSArray_Add(sigd->cmsg->poolp, (void ***)&(sigd->tempCerts), (void *)c);
811 return rv;
814 SECStatus
815 NSS_CMSSignedData_AddCertificate(NSSCMSSignedData *sigd, CERTCertificate *cert)
817 CERTCertificate *c;
818 SECStatus rv;
820 if (!sigd || !cert) {
821 PORT_SetError(SEC_ERROR_INVALID_ARGS);
822 return SECFailure;
825 c = CERT_DupCertificate(cert);
826 rv = NSS_CMSArray_Add(sigd->cmsg->poolp, (void ***)&(sigd->certs), (void *)c);
827 return rv;
830 PRBool
831 NSS_CMSSignedData_ContainsCertsOrCrls(NSSCMSSignedData *sigd)
833 if (!sigd) {
834 PORT_SetError(SEC_ERROR_INVALID_ARGS);
835 return PR_FALSE;
837 if (sigd->rawCerts != NULL && sigd->rawCerts[0] != NULL)
838 return PR_TRUE;
839 else if (sigd->crls != NULL && sigd->crls[0] != NULL)
840 return PR_TRUE;
841 else
842 return PR_FALSE;
845 SECStatus
846 NSS_CMSSignedData_AddSignerInfo(NSSCMSSignedData *sigd,
847 NSSCMSSignerInfo *signerinfo)
849 void *mark;
850 SECStatus rv;
851 SECOidTag digestalgtag;
852 PLArenaPool *poolp;
854 if (!sigd || !signerinfo) {
855 PORT_SetError(SEC_ERROR_INVALID_ARGS);
856 return SECFailure;
859 poolp = sigd->cmsg->poolp;
861 mark = PORT_ArenaMark(poolp);
863 /* add signerinfo */
864 rv = NSS_CMSArray_Add(poolp, (void ***)&(sigd->signerInfos), (void *)signerinfo);
865 if (rv != SECSuccess)
866 goto loser;
869 * add empty digest
870 * Empty because we don't have it yet. Either it gets created during encoding
871 * (if the data is present) or has to be set externally.
872 * XXX maybe pass it in optionally?
874 digestalgtag = NSS_CMSSignerInfo_GetDigestAlgTag(signerinfo);
875 rv = NSS_CMSSignedData_SetDigestValue(sigd, digestalgtag, NULL);
876 if (rv != SECSuccess)
877 goto loser;
880 * The last thing to get consistency would be adding the digest.
883 PORT_ArenaUnmark(poolp, mark);
884 return SECSuccess;
886 loser:
887 PORT_ArenaRelease (poolp, mark);
888 return SECFailure;
892 * NSS_CMSSignedData_SetDigests - set a signedData's digests member
894 * "digestalgs" - array of digest algorithm IDs
895 * "digests" - array of digests corresponding to the digest algorithms
897 SECStatus
898 NSS_CMSSignedData_SetDigests(NSSCMSSignedData *sigd,
899 SECAlgorithmID **digestalgs,
900 SECItem **digests)
902 int cnt, i, idx;
904 if (!sigd || !digestalgs || !digests) {
905 PORT_SetError(SEC_ERROR_INVALID_ARGS);
906 return SECFailure;
909 if (sigd->digestAlgorithms == NULL) {
910 PORT_SetError(SEC_ERROR_INVALID_ARGS);
911 return SECFailure;
914 /* we assume that the digests array is just not there yet */
915 PORT_Assert(sigd->digests == NULL);
916 if (sigd->digests != NULL) {
917 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
918 return SECFailure;
921 /* now allocate one (same size as digestAlgorithms) */
922 cnt = NSS_CMSArray_Count((void **)sigd->digestAlgorithms);
923 sigd->digests = PORT_ArenaZAlloc(sigd->cmsg->poolp, (cnt + 1) * sizeof(SECItem *));
924 if (sigd->digests == NULL) {
925 PORT_SetError(SEC_ERROR_NO_MEMORY);
926 return SECFailure;
929 for (i = 0; sigd->digestAlgorithms[i] != NULL; i++) {
930 /* try to find the sigd's i'th digest algorithm in the array we passed in */
931 idx = NSS_CMSAlgArray_GetIndexByAlgID(digestalgs, sigd->digestAlgorithms[i]);
932 if (idx < 0) {
933 PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);
934 return SECFailure;
936 if (!digests[idx]) {
937 /* We have no digest for this algorithm, probably because it is
938 ** unrecognized or unsupported. We'll ignore this here. If this
939 ** digest is needed later, an error will be be generated then.
941 continue;
944 /* found it - now set it */
945 if ((sigd->digests[i] = SECITEM_AllocItem(sigd->cmsg->poolp, NULL, 0)) == NULL ||
946 SECITEM_CopyItem(sigd->cmsg->poolp, sigd->digests[i], digests[idx]) != SECSuccess)
948 PORT_SetError(SEC_ERROR_NO_MEMORY);
949 return SECFailure;
952 return SECSuccess;
955 SECStatus
956 NSS_CMSSignedData_SetDigestValue(NSSCMSSignedData *sigd,
957 SECOidTag digestalgtag,
958 SECItem *digestdata)
960 SECItem *digest = NULL;
961 PLArenaPool *poolp;
962 void *mark;
963 int n, cnt;
965 if (!sigd) {
966 PORT_SetError(SEC_ERROR_INVALID_ARGS);
967 return SECFailure;
970 poolp = sigd->cmsg->poolp;
972 mark = PORT_ArenaMark(poolp);
975 if (digestdata) {
976 digest = (SECItem *) PORT_ArenaZAlloc(poolp,sizeof(SECItem));
978 /* copy digestdata item to arena (in case we have it and are not only making room) */
979 if (SECITEM_CopyItem(poolp, digest, digestdata) != SECSuccess)
980 goto loser;
983 /* now allocate one (same size as digestAlgorithms) */
984 if (sigd->digests == NULL) {
985 cnt = NSS_CMSArray_Count((void **)sigd->digestAlgorithms);
986 sigd->digests = PORT_ArenaZAlloc(sigd->cmsg->poolp, (cnt + 1) * sizeof(SECItem *));
987 if (sigd->digests == NULL) {
988 PORT_SetError(SEC_ERROR_NO_MEMORY);
989 return SECFailure;
993 n = -1;
994 if (sigd->digestAlgorithms != NULL)
995 n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
997 /* if not found, add a digest */
998 if (n < 0) {
999 if (NSS_CMSSignedData_AddDigest(poolp, sigd, digestalgtag, digest) != SECSuccess)
1000 goto loser;
1001 } else {
1002 /* replace NULL pointer with digest item (and leak previous value) */
1003 sigd->digests[n] = digest;
1006 PORT_ArenaUnmark(poolp, mark);
1007 return SECSuccess;
1009 loser:
1010 PORT_ArenaRelease(poolp, mark);
1011 return SECFailure;
1014 SECStatus
1015 NSS_CMSSignedData_AddDigest(PRArenaPool *poolp,
1016 NSSCMSSignedData *sigd,
1017 SECOidTag digestalgtag,
1018 SECItem *digest)
1020 SECAlgorithmID *digestalg;
1021 void *mark;
1023 if (!sigd || !poolp) {
1024 PORT_SetError(SEC_ERROR_INVALID_ARGS);
1025 return SECFailure;
1028 mark = PORT_ArenaMark(poolp);
1030 digestalg = PORT_ArenaZAlloc(poolp, sizeof(SECAlgorithmID));
1031 if (digestalg == NULL)
1032 goto loser;
1034 if (SECOID_SetAlgorithmID (poolp, digestalg, digestalgtag, NULL) != SECSuccess) /* no params */
1035 goto loser;
1037 if (NSS_CMSArray_Add(poolp, (void ***)&(sigd->digestAlgorithms), (void *)digestalg) != SECSuccess ||
1038 /* even if digest is NULL, add dummy to have same-size array */
1039 NSS_CMSArray_Add(poolp, (void ***)&(sigd->digests), (void *)digest) != SECSuccess)
1041 goto loser;
1044 PORT_ArenaUnmark(poolp, mark);
1045 return SECSuccess;
1047 loser:
1048 PORT_ArenaRelease(poolp, mark);
1049 return SECFailure;
1052 /* XXX This function doesn't set the error code on failure. */
1053 SECItem *
1054 NSS_CMSSignedData_GetDigestValue(NSSCMSSignedData *sigd, SECOidTag digestalgtag)
1056 int n;
1058 if (!sigd) {
1059 PORT_SetError(SEC_ERROR_INVALID_ARGS);
1060 return NULL;
1063 if (sigd->digestAlgorithms == NULL || sigd->digests == NULL) {
1064 PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);
1065 return NULL;
1068 n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
1070 return (n < 0) ? NULL : sigd->digests[n];
1073 /* =============================================================================
1074 * Misc. utility functions
1078 * NSS_CMSSignedData_CreateCertsOnly - create a certs-only SignedData.
1080 * cert - base certificates that will be included
1081 * include_chain - if true, include the complete cert chain for cert
1083 * More certs and chains can be added via AddCertificate and AddCertChain.
1085 * An error results in a return value of NULL and an error set.
1087 * XXXX CRLs
1089 NSSCMSSignedData *
1090 NSS_CMSSignedData_CreateCertsOnly(NSSCMSMessage *cmsg, CERTCertificate *cert, PRBool include_chain)
1092 NSSCMSSignedData *sigd;
1093 void *mark;
1094 PLArenaPool *poolp;
1095 SECStatus rv;
1097 if (!cmsg || !cert) {
1098 PORT_SetError(SEC_ERROR_INVALID_ARGS);
1099 return NULL;
1102 poolp = cmsg->poolp;
1103 mark = PORT_ArenaMark(poolp);
1105 sigd = NSS_CMSSignedData_Create(cmsg);
1106 if (sigd == NULL)
1107 goto loser;
1109 /* no signerinfos, thus no digestAlgorithms */
1111 /* but certs */
1112 if (include_chain) {
1113 rv = NSS_CMSSignedData_AddCertChain(sigd, cert);
1114 } else {
1115 rv = NSS_CMSSignedData_AddCertificate(sigd, cert);
1117 if (rv != SECSuccess)
1118 goto loser;
1120 /* RFC2630 5.2 sez:
1121 * In the degenerate case where there are no signers, the
1122 * EncapsulatedContentInfo value being "signed" is irrelevant. In this
1123 * case, the content type within the EncapsulatedContentInfo value being
1124 * "signed" should be id-data (as defined in section 4), and the content
1125 * field of the EncapsulatedContentInfo value should be omitted.
1127 rv = NSS_CMSContentInfo_SetContent_Data(cmsg, &(sigd->contentInfo), NULL, PR_TRUE);
1128 if (rv != SECSuccess)
1129 goto loser;
1131 PORT_ArenaUnmark(poolp, mark);
1132 return sigd;
1134 loser:
1135 if (sigd)
1136 NSS_CMSSignedData_Destroy(sigd);
1137 PORT_ArenaRelease(poolp, mark);
1138 return NULL;
1141 /* TODO:
1142 * NSS_CMSSignerInfo_GetReceiptRequest()
1143 * NSS_CMSSignedData_HasReceiptRequest()
1144 * easy way to iterate over signers