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/
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.
12 * The Original Code is the Netscape security libraries.
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
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
35 * CMS signedData methods.
38 #include <Security/SecCmsSignedData.h>
40 #include <Security/SecCmsContentInfo.h>
41 #include <Security/SecCmsDigestContext.h>
42 #include <Security/SecCmsSignerInfo.h>
47 #include "SecAsn1Item.h"
50 #include <security_asn1/secasn1.h>
51 #include <security_asn1/secerr.h>
52 #include <security_asn1/secport.h>
55 #include <Security/SecCertificatePriv.h>
59 SecCmsSignedDataCreate(SecCmsMessageRef cmsg
)
62 SecCmsSignedDataRef sigd
;
67 mark
= PORT_ArenaMark(poolp
);
69 sigd
= (SecCmsSignedDataRef
)PORT_ArenaZAlloc (poolp
, sizeof(SecCmsSignedData
));
73 sigd
->contentInfo
.cmsg
= cmsg
;
75 /* signerInfos, certs, certlists, crls are all empty */
76 /* version is set in SecCmsSignedDataFinalize() */
78 PORT_ArenaUnmark(poolp
, mark
);
82 PORT_ArenaRelease(poolp
, mark
);
87 SecCmsSignedDataDestroy(SecCmsSignedDataRef sigd
)
89 SecCmsSignerInfoRef
*signerinfos
, si
;
94 if (sigd
->certs
!= NULL
)
95 CFRelease(sigd
->certs
);
97 signerinfos
= sigd
->signerInfos
;
98 if (signerinfos
!= NULL
) {
99 while ((si
= *signerinfos
++) != NULL
)
100 SecCmsSignerInfoDestroy(si
);
103 /* everything's in a pool, so don't worry about the storage */
104 SecCmsContentInfoDestroy(&(sigd
->contentInfo
));
108 * SecCmsSignedDataEncodeBeforeStart - do all the necessary things to a SignedData
109 * before start of encoding.
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.
119 SecCmsSignedDataEncodeBeforeStart(SecCmsSignedDataRef sigd
)
121 SecCmsSignerInfoRef signerinfo
;
122 SECOidTag digestalgtag
;
126 Boolean haveDigests
= PR_FALSE
;
130 poolp
= sigd
->contentInfo
.cmsg
->poolp
;
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
)
139 if (sigd
->digestAlgorithms
[i
] == NULL
) /* reached the end of the array? */
140 haveDigests
= PR_TRUE
; /* yes: we must have all the digests */
143 version
= SEC_CMS_SIGNED_DATA_VERSION_BASIC
;
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
;
149 /* prepare all the SignerInfos (there may be none) */
150 for (i
=0; i
< SecCmsSignedDataSignerInfoCount(sigd
); i
++) {
151 signerinfo
= SecCmsSignedDataGetSignerInfo(sigd
, i
);
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
;
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
);
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... */
167 /* add the digestAlgorithm & a NULL digest */
168 rv
= SecCmsSignedDataAddDigest(poolp
, sigd
, digestalgtag
, NULL
);
169 if (rv
!= SECSuccess
)
172 /* found it, nothing to do */
176 dummy
= SEC_ASN1EncodeInteger(poolp
, &(sigd
->version
), (long)version
);
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
)
194 SecCmsSignedDataEncodeBeforeData(SecCmsSignedDataRef sigd
)
196 /* set up the digests */
197 if (sigd
->digestAlgorithms
!= NULL
) {
198 sigd
->contentInfo
.digcx
= SecCmsDigestContextStartMultiple(sigd
->digestAlgorithms
);
199 if (sigd
->contentInfo
.digcx
== NULL
)
206 * SecCmsSignedDataEncodeAfterData - do all the necessary things to a SignedData
207 * after all the encapsulated data was passed through the encoder.
210 * - create the signatures in all the SignerInfos
212 * Please note that nothing is done to the Certificates and CRLs in the message - this
213 * is entirely the responsibility of our callers.
216 SecCmsSignedDataEncodeAfterData(SecCmsSignedDataRef sigd
)
218 SecCmsSignerInfoRef
*signerinfos
, signerinfo
;
219 SecCmsContentInfoRef cinfo
;
220 SECOidTag digestalgtag
;
221 OSStatus ret
= SECFailure
;
223 SecAsn1Item
* contentType
;
225 int i
, ci
, n
, rci
, si
;
228 extern const SecAsn1Template SecCmsSignerInfoTemplate
[];
230 cinfo
= &(sigd
->contentInfo
);
231 poolp
= cinfo
->cmsg
->poolp
;
233 /* did we have digest calculation going on? */
235 SecAsn1Item
**digests
= NULL
;
236 SECAlgorithmID
**digestalgs
= NULL
;
237 rv
= SecCmsDigestContextFinishMultiple(cinfo
->digcx
, &digestalgs
, &digests
);
238 if (rv
!= SECSuccess
)
239 goto loser
; /* error has been set by SecCmsDigestContextFinishMultiple */
240 if (digestalgs
&& digests
) {
241 rv
= SecCmsSignedDataSetDigests(sigd
, digestalgs
, digests
);
242 if (rv
!= SECSuccess
)
243 goto loser
; /* error has been set by SecCmsSignedDataSetDigests */
245 SecCmsDigestContextDestroy(cinfo
->digcx
);
249 signerinfos
= sigd
->signerInfos
;
252 /* prepare all the SignerInfos (there may be none) */
253 for (i
=0; i
< SecCmsSignedDataSignerInfoCount(sigd
); i
++) {
254 signerinfo
= SecCmsSignedDataGetSignerInfo(sigd
, i
);
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
);
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
269 /* pass contentType here as we want a contentType attribute */
270 if ((contentType
= SecCmsContentInfoGetContentTypeOID(cinfo
)) == NULL
)
274 rv
= SecCmsSignerInfoSign(signerinfo
, sigd
->digests
[n
], contentType
);
275 if (rv
!= SECSuccess
)
278 /* while we're at it, count number of certs in certLists */
279 certlist
= SecCmsSignerInfoGetCertList(signerinfo
);
281 certcount
+= CFArrayGetCount(certlist
);
284 /* this is a SET OF, so we need to sort them guys */
285 rv
= SecCmsArraySortByDER((void **)signerinfos
, SecCmsSignerInfoTemplate
, NULL
);
286 if (rv
!= SECSuccess
)
290 * now prepare certs & crls
293 /* count the rest of the certs */
294 if (sigd
->certs
!= NULL
)
295 certcount
+= CFArrayGetCount(sigd
->certs
);
297 if (certcount
== 0) {
298 sigd
->rawCerts
= NULL
;
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.)
306 * XXX ARGH - this NEEDS to be fixed. need to come up with a decent
307 * SetOfDERcertficates implementation
309 sigd
->rawCerts
= (SecAsn1Item
* *)PORT_ArenaAlloc(poolp
, (certcount
+ 1) * sizeof(SecAsn1Item
*));
310 if (sigd
->rawCerts
== NULL
)
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...
319 * XXX ARGH - this NEEDS to be fixed. need to come up with a decent
320 * SetOfDERcertficates implementation
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
++) {
327 sigd
->rawCerts
[rci
] = PORT_ArenaZAlloc(poolp
, sizeof(SecAsn1Item
));
328 SecCertificateRef cert
= (SecCertificateRef
)CFArrayGetValueAtIndex(signerinfo
->certList
, ci
);
330 SecCertificateGetData(cert
, sigd
->rawCerts
[rci
++]);
332 SecAsn1Item cert_data
= { SecCertificateGetLength(cert
),
333 (uint8_t *)SecCertificateGetBytePtr(cert
) };
334 *(sigd
->rawCerts
[rci
++]) = cert_data
;
340 if (sigd
->certs
!= NULL
) {
341 for (ci
= 0; ci
< CFArrayGetCount(sigd
->certs
); ci
++) {
342 sigd
->rawCerts
[rci
] = PORT_ArenaZAlloc(poolp
, sizeof(SecAsn1Item
));
343 SecCertificateRef cert
= (SecCertificateRef
)CFArrayGetValueAtIndex(sigd
->certs
, ci
);
345 SecCertificateGetData(cert
, sigd
->rawCerts
[rci
++]);
347 SecAsn1Item cert_data
= { SecCertificateGetLength(cert
),
348 (uint8_t *)SecCertificateGetBytePtr(cert
) };
349 *(sigd
->rawCerts
[rci
++]) = cert_data
;
354 sigd
->rawCerts
[rci
] = NULL
;
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
);
367 SecCmsSignedDataDecodeBeforeData(SecCmsSignedDataRef sigd
)
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*/) {
371 /* if digests are already there, do nothing */
372 sigd
->contentInfo
.digcx
= SecCmsDigestContextStartMultiple(sigd
->digestAlgorithms
);
373 if (sigd
->contentInfo
.digcx
== NULL
)
380 * SecCmsSignedDataDecodeAfterData - do all the necessary things to a SignedData
381 * after all the encapsulated data was passed through the decoder.
384 SecCmsSignedDataDecodeAfterData(SecCmsSignedDataRef sigd
)
386 OSStatus rv
= SECSuccess
;
388 /* did we have digest calculation going on? */
389 if (sigd
->contentInfo
.digcx
) {
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 */
401 SecCmsDigestContextDestroy(sigd
->contentInfo
.digcx
);
402 sigd
->contentInfo
.digcx
= NULL
;
410 * SecCmsSignedDataDecodeAfterEnd - do all the necessary things to a SignedData
411 * after all decoding is finished.
414 SecCmsSignedDataDecodeAfterEnd(SecCmsSignedDataRef sigd
)
416 SecCmsSignerInfoRef
*signerinfos
;
419 /* set cmsg for all the signerinfos */
420 signerinfos
= sigd
->signerInfos
;
422 /* set signedData for all the signerinfos */
424 for (i
= 0; signerinfos
[i
] != NULL
; i
++)
425 signerinfos
[i
]->signedData
= sigd
;
432 * SecCmsSignedDataGetSignerInfos - retrieve the SignedData's signer list
434 SecCmsSignerInfoRef
*
435 SecCmsSignedDataGetSignerInfos(SecCmsSignedDataRef sigd
)
437 return sigd
->signerInfos
;
441 SecCmsSignedDataSignerInfoCount(SecCmsSignedDataRef sigd
)
443 return SecCmsArrayCount((void **)sigd
->signerInfos
);
447 SecCmsSignedDataGetSignerInfo(SecCmsSignedDataRef sigd
, int i
)
449 return sigd
->signerInfos
[i
];
453 * SecCmsSignedDataGetDigestAlgs - retrieve the SignedData's digest algorithm list
456 SecCmsSignedDataGetDigestAlgs(SecCmsSignedDataRef sigd
)
458 return sigd
->digestAlgorithms
;
462 * SecCmsSignedDataGetContentInfo - return pointer to this signedData's contentinfo
465 SecCmsSignedDataGetContentInfo(SecCmsSignedDataRef sigd
)
467 return &(sigd
->contentInfo
);
471 * SecCmsSignedDataGetCertificateList - retrieve the SignedData's certificate list
474 SecCmsSignedDataGetCertificateList(SecCmsSignedDataRef sigd
)
476 return sigd
->rawCerts
;
480 SecCmsSignedDataImportCerts(SecCmsSignedDataRef sigd
, SecKeychainRef keychain
,
481 SECCertUsage certusage
, Boolean keepcerts
)
486 int ix
, certcount
= SecCmsArrayCount((void **)sigd
->rawCerts
);
487 rv
= CERT_ImportCerts(keychain
, certusage
, certcount
, sigd
->rawCerts
, NULL
,
488 keepcerts
, PR_FALSE
, NULL
);
489 /* XXX CRL handling */
491 if (sigd
->signerInfos
!= NULL
) {
492 /* fill in all signerinfo's certs */
493 for (ix
= 0; sigd
->signerInfos
[ix
] != NULL
; i
++)
494 (void)SecCmsSignerInfoGetSigningCertificate(sigd
->signerInfos
[ix
], keychain
);
497 // XXX we should only ever import certs for a cert only data blob
504 * XXX the digests need to be passed in BETWEEN the decoding and the verification in case
505 * of external signatures!
510 * SecCmsSignedDataVerifySignerInfo - check the signatures.
512 * The digests were either calculated during decoding (and are stored in the
513 * signedData itself) or set after decoding using SecCmsSignedDataSetDigests.
515 * The verification checks if the signing cert is valid and has a trusted chain
516 * for the purpose specified by "policies".
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().
522 SecCmsSignedDataVerifySignerInfo(SecCmsSignedDataRef sigd
, int i
,
523 SecKeychainRef keychainOrArray
, CFTypeRef policies
, SecTrustRef
*trustRef
)
525 SecCmsSignerInfoRef signerinfo
;
526 SecCmsContentInfoRef cinfo
;
527 SECOidData
*algiddata
;
528 SecAsn1Item
*contentType
, *digest
;
531 cinfo
= &(sigd
->contentInfo
);
533 signerinfo
= sigd
->signerInfos
[i
];
535 /* Signature or digest level verificationStatus errors should supercede
536 certificate level errors, so check the digest and signature first. */
538 /* Find digest and contentType for signerinfo */
539 algiddata
= SecCmsSignerInfoGetDigestAlg(signerinfo
);
541 if (!sigd
->digests
) {
542 SECAlgorithmID
**digestalgs
= SecCmsSignedDataGetDigestAlgs(sigd
);
543 SecCmsDigestContextRef digcx
= SecCmsDigestContextStartMultiple(digestalgs
);
544 SecCmsSignedDataSetDigestContext(sigd
, digcx
);
545 SecCmsDigestContextDestroy(digcx
);
548 digest
= SecCmsSignedDataGetDigestByAlgTag(sigd
, algiddata
->offset
);
550 contentType
= SecCmsContentInfoGetContentTypeOID(cinfo
);
552 /* verify signature */
553 status
= SecCmsSignerInfoVerify(signerinfo
, digest
, contentType
);
557 /* Now verify the certificate. We do this even if the signature failed to verify so we can
558 return a trustRef to the caller for display purposes. */
559 status
= SecCmsSignerInfoVerifyCertificate(signerinfo
, keychainOrArray
, policies
, trustRef
);
567 * SecCmsSignedDataVerifyCertsOnly - verify the certs in a certs-only message
570 SecCmsSignedDataVerifyCertsOnly(SecCmsSignedDataRef sigd
,
571 SecKeychainRef keychainOrArray
,
574 SecCertificateRef cert
;
575 OSStatus rv
= SECSuccess
;
579 if (!sigd
|| !keychainOrArray
|| !sigd
->rawCerts
) {
580 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
584 count
= SecCmsArrayCount((void**)sigd
->rawCerts
);
585 for (i
=0; i
< count
; i
++) {
586 if (sigd
->certs
&& CFArrayGetCount(sigd
->certs
) > i
) {
587 cert
= (SecCertificateRef
)CFArrayGetValueAtIndex(sigd
->certs
, i
);
590 cert
= CERT_FindCertByDERCert(keychainOrArray
, sigd
->rawCerts
[i
]);
596 rv
|= CERT_VerifyCert(keychainOrArray
, cert
, policies
, CFAbsoluteTimeGetCurrent(), NULL
);
604 SecCmsSignedDataVerifyCertsOnly(SecCmsSignedDataRef sigd
,
605 SecKeychainRef keychainOrArray
,
608 OSStatus rv
= SECSuccess
;
610 if (!sigd
|| !keychainOrArray
|| !sigd
->rawCerts
) {
611 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
615 SecAsn1Item
**cert_datas
= sigd
->rawCerts
;
616 SecAsn1Item
*cert_data
;
617 while ((cert_data
= *cert_datas
++) != NULL
) {
618 SecCertificateRef cert
= SecCertificateCreateWithBytes(NULL
, cert_data
->Data
, cert_data
->Length
);
620 CFArrayRef certs
= CFArrayCreate(kCFAllocatorDefault
, (const void **)&cert
, 1, NULL
);
621 rv
|= CERT_VerifyCert(keychainOrArray
, certs
, policies
, CFAbsoluteTimeGetCurrent(), NULL
);
634 * SecCmsSignedDataHasDigests - see if we have digests in place
637 SecCmsSignedDataHasDigests(SecCmsSignedDataRef sigd
)
639 return (sigd
->digests
!= NULL
);
643 SecCmsSignedDataAddCertList(SecCmsSignedDataRef sigd
, CFArrayRef certlist
)
645 PORT_Assert(certlist
!= NULL
);
647 if (certlist
== NULL
)
651 sigd
->certs
= CFArrayCreateMutableCopy(NULL
, 0, certlist
);
654 CFRange certlistRange
= { 0, CFArrayGetCount(certlist
) };
655 CFArrayAppendArray(sigd
->certs
, certlist
, certlistRange
);
662 * SecCmsSignedDataAddCertChain - add cert and its entire chain to the set of certs
665 SecCmsSignedDataAddCertChain(SecCmsSignedDataRef sigd
, SecCertificateRef cert
)
671 usage
= certUsageEmailSigner
;
673 /* do not include root */
674 certlist
= CERT_CertChainFromCert(cert
, usage
, PR_FALSE
);
675 if (certlist
== NULL
)
678 rv
= SecCmsSignedDataAddCertList(sigd
, certlist
);
685 SecCmsSignedDataAddCertificate(SecCmsSignedDataRef sigd
, SecCertificateRef cert
)
687 PORT_Assert(cert
!= NULL
);
693 sigd
->certs
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
695 CFArrayAppendValue(sigd
->certs
, cert
);
701 SecCmsSignedDataContainsCertsOrCrls(SecCmsSignedDataRef sigd
)
703 if (sigd
->rawCerts
!= NULL
&& sigd
->rawCerts
[0] != NULL
)
705 else if (sigd
->rawCrls
!= NULL
&& sigd
->rawCrls
[0] != NULL
)
712 SecCmsSignedDataAddSignerInfo(SecCmsSignedDataRef sigd
,
713 SecCmsSignerInfoRef signerinfo
)
717 SECOidTag digestalgtag
;
720 poolp
= sigd
->contentInfo
.cmsg
->poolp
;
722 mark
= PORT_ArenaMark(poolp
);
725 rv
= SecCmsArrayAdd(poolp
, (void ***)&(sigd
->signerInfos
), (void *)signerinfo
);
726 if (rv
!= SECSuccess
)
731 * Empty because we don't have it yet. Either it gets created during encoding
732 * (if the data is present) or has to be set externally.
733 * XXX maybe pass it in optionally?
735 digestalgtag
= SecCmsSignerInfoGetDigestAlgTag(signerinfo
);
736 rv
= SecCmsSignedDataSetDigestValue(sigd
, digestalgtag
, NULL
);
737 if (rv
!= SECSuccess
)
741 * The last thing to get consistency would be adding the digest.
744 PORT_ArenaUnmark(poolp
, mark
);
748 PORT_ArenaRelease (poolp
, mark
);
753 SecCmsSignedDataGetDigestByAlgTag(SecCmsSignedDataRef sigd
, SECOidTag algtag
)
757 idx
= SecCmsAlgArrayGetIndexByAlgTag(sigd
->digestAlgorithms
, algtag
);
758 return (idx
>= 0)?(sigd
->digests
)[idx
]:NULL
;
762 SecCmsSignedDataSetDigestContext(SecCmsSignedDataRef sigd
,
763 SecCmsDigestContextRef digestContext
)
765 SECAlgorithmID
**digestalgs
;
766 SecAsn1Item
* *digests
;
768 if (SecCmsDigestContextFinishMultiple(digestContext
, &digestalgs
, &digests
) != SECSuccess
)
770 if (SecCmsSignedDataSetDigests(sigd
, digestalgs
, digests
) != SECSuccess
)
775 return PORT_GetError();
779 * SecCmsSignedDataSetDigests - set a signedData's digests member
781 * "digestalgs" - array of digest algorithm IDs
782 * "digests" - array of digests corresponding to the digest algorithms
785 SecCmsSignedDataSetDigests(SecCmsSignedDataRef sigd
,
786 SECAlgorithmID
**digestalgs
,
787 SecAsn1Item
* *digests
)
791 if (sigd
->digestAlgorithms
== NULL
) {
792 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
796 /* Since we'll generate a empty digest for content-less messages
797 whether or not they're detached, we have to avoid overwriting
798 externally set digest for detached content => return early */
799 if (sigd
->digests
&& sigd
->digests
[0])
802 /* we assume that the digests array is just not there yet */
804 PORT_Assert(sigd->digests == NULL);
805 if (sigd->digests != NULL) {
806 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
810 /* now allocate one (same size as digestAlgorithms) */
811 if (sigd
->digests
== NULL
) {
812 cnt
= SecCmsArrayCount((void **)sigd
->digestAlgorithms
);
813 sigd
->digests
= PORT_ArenaZAlloc(sigd
->contentInfo
.cmsg
->poolp
, (cnt
+ 1) * sizeof(SecAsn1Item
*));
814 if (sigd
->digests
== NULL
) {
815 PORT_SetError(SEC_ERROR_NO_MEMORY
);
820 for (i
= 0; sigd
->digestAlgorithms
[i
] != NULL
; i
++) {
821 /* try to find the sigd's i'th digest algorithm in the array we passed in */
822 idx
= SecCmsAlgArrayGetIndexByAlgID(digestalgs
, sigd
->digestAlgorithms
[i
]);
824 PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND
);
828 /* found it - now set it */
829 if ((sigd
->digests
[i
] = SECITEM_AllocItem(sigd
->contentInfo
.cmsg
->poolp
, NULL
, 0)) == NULL
||
830 SECITEM_CopyItem(sigd
->contentInfo
.cmsg
->poolp
, sigd
->digests
[i
], digests
[idx
]) != SECSuccess
)
832 PORT_SetError(SEC_ERROR_NO_MEMORY
);
840 SecCmsSignedDataSetDigestValue(SecCmsSignedDataRef sigd
,
841 SECOidTag digestalgtag
,
842 SecAsn1Item
* digestdata
)
844 SecAsn1Item
* digest
= NULL
;
849 poolp
= sigd
->contentInfo
.cmsg
->poolp
;
851 mark
= PORT_ArenaMark(poolp
);
855 digest
= (SecAsn1Item
*) PORT_ArenaZAlloc(poolp
,sizeof(SecAsn1Item
));
857 /* copy digestdata item to arena (in case we have it and are not only making room) */
858 if (SECITEM_CopyItem(poolp
, digest
, digestdata
) != SECSuccess
)
862 /* now allocate one (same size as digestAlgorithms) */
863 if (sigd
->digests
== NULL
) {
864 cnt
= SecCmsArrayCount((void **)sigd
->digestAlgorithms
);
865 sigd
->digests
= PORT_ArenaZAlloc(sigd
->contentInfo
.cmsg
->poolp
, (cnt
+ 1) * sizeof(SecAsn1Item
*));
866 if (sigd
->digests
== NULL
) {
867 PORT_SetError(SEC_ERROR_NO_MEMORY
);
873 if (sigd
->digestAlgorithms
!= NULL
)
874 n
= SecCmsAlgArrayGetIndexByAlgTag(sigd
->digestAlgorithms
, digestalgtag
);
876 /* if not found, add a digest */
878 if (SecCmsSignedDataAddDigest(poolp
, sigd
, digestalgtag
, digest
) != SECSuccess
)
881 /* replace NULL pointer with digest item (and leak previous value) */
882 sigd
->digests
[n
] = digest
;
885 PORT_ArenaUnmark(poolp
, mark
);
889 PORT_ArenaRelease(poolp
, mark
);
894 SecCmsSignedDataAddDigest(PRArenaPool
*poolp
,
895 SecCmsSignedDataRef sigd
,
896 SECOidTag digestalgtag
,
897 SecAsn1Item
* digest
)
899 SECAlgorithmID
*digestalg
;
902 mark
= PORT_ArenaMark(poolp
);
904 digestalg
= PORT_ArenaZAlloc(poolp
, sizeof(SECAlgorithmID
));
905 if (digestalg
== NULL
)
908 if (SECOID_SetAlgorithmID (poolp
, digestalg
, digestalgtag
, NULL
) != SECSuccess
) /* no params */
911 if (SecCmsArrayAdd(poolp
, (void ***)&(sigd
->digestAlgorithms
), (void *)digestalg
) != SECSuccess
||
912 /* even if digest is NULL, add dummy to have same-size array */
913 SecCmsArrayAdd(poolp
, (void ***)&(sigd
->digests
), (void *)digest
) != SECSuccess
)
918 PORT_ArenaUnmark(poolp
, mark
);
922 PORT_ArenaRelease(poolp
, mark
);
927 SecCmsSignedDataGetDigestValue(SecCmsSignedDataRef sigd
, SECOidTag digestalgtag
)
931 if (sigd
->digestAlgorithms
== NULL
)
934 n
= SecCmsAlgArrayGetIndexByAlgTag(sigd
->digestAlgorithms
, digestalgtag
);
936 return (n
< 0) ? NULL
: sigd
->digests
[n
];
939 /* =============================================================================
940 * Misc. utility functions
944 * SecCmsSignedDataCreateCertsOnly - create a certs-only SignedData.
946 * cert - base certificates that will be included
947 * include_chain - if true, include the complete cert chain for cert
949 * More certs and chains can be added via AddCertificate and AddCertChain.
951 * An error results in a return value of NULL and an error set.
956 SecCmsSignedDataCreateCertsOnly(SecCmsMessageRef cmsg
, SecCertificateRef cert
, Boolean include_chain
)
958 SecCmsSignedDataRef sigd
;
964 mark
= PORT_ArenaMark(poolp
);
966 sigd
= SecCmsSignedDataCreate(cmsg
);
970 /* no signerinfos, thus no digestAlgorithms */
974 rv
= SecCmsSignedDataAddCertChain(sigd
, cert
);
976 rv
= SecCmsSignedDataAddCertificate(sigd
, cert
);
978 if (rv
!= SECSuccess
)
982 * In the degenerate case where there are no signers, the
983 * EncapsulatedContentInfo value being "signed" is irrelevant. In this
984 * case, the content type within the EncapsulatedContentInfo value being
985 * "signed" should be id-data (as defined in section 4), and the content
986 * field of the EncapsulatedContentInfo value should be omitted.
988 rv
= SecCmsContentInfoSetContentData(&(sigd
->contentInfo
), NULL
, PR_TRUE
);
989 if (rv
!= SECSuccess
)
992 PORT_ArenaUnmark(poolp
, mark
);
997 SecCmsSignedDataDestroy(sigd
);
998 PORT_ArenaRelease(poolp
, mark
);
1003 * SecCmsSignerInfoGetReceiptRequest()
1004 * SecCmsSignedDataHasReceiptRequest()
1005 * easy way to iterate over signers