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 signerInfo methods.
38 #include <Security/SecCmsSignerInfo.h>
39 #include "SecSMIMEPriv.h"
48 #include <security_asn1/secasn1.h>
49 #include <security_asn1/secerr.h>
50 #include <Security/SecKeychain.h>
51 #include <Security/SecIdentity.h>
52 #include <Security/SecCertificatePriv.h>
53 #include <Security/SecKeyPriv.h>
54 #include <CoreFoundation/CFTimeZone.h>
55 #include <utilities/SecCFWrappers.h>
56 #include <AssertMacros.h>
57 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
58 #include <Security/SecPolicyPriv.h>
60 #include "tsaSupport.h"
61 #include "tsaSupportPriv.h"
63 #define HIDIGIT(v) (((v) / 10) + '0')
64 #define LODIGIT(v) (((v) % 10) + '0')
66 #define ISDIGIT(dig) (((dig) >= '0') && ((dig) <= '9'))
67 #define CAPTURE(var,p,label) \
69 if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1])) goto label; \
70 (var) = ((p)[0] - '0') * 10 + ((p)[1] - '0'); \
74 #define SIGINFO_DEBUG 1
78 #define dprintf(args...) fprintf(stderr, args)
80 #define dprintf(args...)
84 #define dprintfRC(args...) dprintf(args)
86 #define dprintfRC(args...)
90 DER_UTCTimeToCFDate(const CSSM_DATA_PTR utcTime
, CFAbsoluteTime
*date
)
92 CFGregorianDate gdate
;
93 char *string
= (char *)utcTime
->Data
;
94 long year
, month
, mday
, hour
, minute
, second
, hourOff
, minOff
;
95 CFTimeZoneRef timeZone
;
97 /* Verify time is formatted properly and capture information */
101 CAPTURE(year
,string
+0,loser
);
103 /* ASSUME that year # is in the 2000's, not the 1900's */
106 CAPTURE(month
,string
+2,loser
);
107 if ((month
== 0) || (month
> 12)) goto loser
;
108 CAPTURE(mday
,string
+4,loser
);
109 if ((mday
== 0) || (mday
> 31)) goto loser
;
110 CAPTURE(hour
,string
+6,loser
);
111 if (hour
> 23) goto loser
;
112 CAPTURE(minute
,string
+8,loser
);
113 if (minute
> 59) goto loser
;
114 if (ISDIGIT(string
[10])) {
115 CAPTURE(second
,string
+10,loser
);
116 if (second
> 59) goto loser
;
119 if (string
[10] == '+') {
120 CAPTURE(hourOff
,string
+11,loser
);
121 if (hourOff
> 23) goto loser
;
122 CAPTURE(minOff
,string
+13,loser
);
123 if (minOff
> 59) goto loser
;
124 } else if (string
[10] == '-') {
125 CAPTURE(hourOff
,string
+11,loser
);
126 if (hourOff
> 23) goto loser
;
128 CAPTURE(minOff
,string
+13,loser
);
129 if (minOff
> 59) goto loser
;
131 } else if (string
[10] != 'Z') {
135 gdate
.year
= (SInt32
)(year
+ 1900);
139 gdate
.minute
= minute
;
140 gdate
.second
= second
;
142 if (hourOff
== 0 && minOff
== 0)
143 timeZone
= NULL
; /* GMT */
146 timeZone
= CFTimeZoneCreateWithTimeIntervalFromGMT(NULL
, (hourOff
* 60 + minOff
) * 60);
149 *date
= CFGregorianDateGetAbsoluteTime(gdate
, timeZone
);
160 DER_CFDateToUTCTime(CFAbsoluteTime date
, CSSM_DATA_PTR utcTime
)
162 CFGregorianDate gdate
= CFAbsoluteTimeGetGregorianDate(date
, NULL
/* GMT */);
166 utcTime
->Length
= 13;
167 utcTime
->Data
= d
= PORT_Alloc(13);
171 /* UTC time does not handle the years before 1950 */
172 if (gdate
.year
< 1950)
175 /* remove the century since it's added to the year by the
176 CFAbsoluteTimeGetGregorianDate routine, but is not needed for UTC time */
178 second
= gdate
.second
+ 0.5;
180 d
[0] = HIDIGIT(gdate
.year
);
181 d
[1] = LODIGIT(gdate
.year
);
182 d
[2] = HIDIGIT(gdate
.month
);
183 d
[3] = LODIGIT(gdate
.month
);
184 d
[4] = HIDIGIT(gdate
.day
);
185 d
[5] = LODIGIT(gdate
.day
);
186 d
[6] = HIDIGIT(gdate
.hour
);
187 d
[7] = LODIGIT(gdate
.hour
);
188 d
[8] = HIDIGIT(gdate
.minute
);
189 d
[9] = LODIGIT(gdate
.minute
);
190 d
[10] = HIDIGIT(second
);
191 d
[11] = LODIGIT(second
);
196 /* =============================================================================
200 nss_cmssignerinfo_create(SecCmsMessageRef cmsg
, SecCmsSignerIDSelector type
, SecCertificateRef cert
, CSSM_DATA_PTR subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
);
203 SecCmsSignerInfoCreateWithSubjKeyID(SecCmsMessageRef cmsg
, CSSM_DATA_PTR subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
)
205 return nss_cmssignerinfo_create(cmsg
, SecCmsSignerIDSubjectKeyID
, NULL
, subjKeyID
, pubKey
, signingKey
, digestalgtag
);
209 SecCmsSignerInfoCreate(SecCmsMessageRef cmsg
, SecIdentityRef identity
, SECOidTag digestalgtag
)
211 SecCmsSignerInfoRef signerInfo
= NULL
;
212 SecCertificateRef cert
= NULL
;
213 SecPrivateKeyRef signingKey
= NULL
;
215 if (SecIdentityCopyCertificate(identity
, &cert
))
217 if (SecIdentityCopyPrivateKey(identity
, &signingKey
))
220 signerInfo
= nss_cmssignerinfo_create(cmsg
, SecCmsSignerIDIssuerSN
, cert
, NULL
, NULL
, signingKey
, digestalgtag
);
226 CFRelease(signingKey
);
232 nss_cmssignerinfo_create(SecCmsMessageRef cmsg
, SecCmsSignerIDSelector type
, SecCertificateRef cert
, CSSM_DATA_PTR subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
)
235 SecCmsSignerInfoRef signerinfo
;
241 mark
= PORT_ArenaMark(poolp
);
243 signerinfo
= (SecCmsSignerInfoRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsSignerInfo
));
244 if (signerinfo
== NULL
) {
245 PORT_ArenaRelease(poolp
, mark
);
250 signerinfo
->cmsg
= cmsg
;
253 case SecCmsSignerIDIssuerSN
:
254 signerinfo
->signerIdentifier
.identifierType
= SecCmsSignerIDIssuerSN
;
255 if ((signerinfo
->cert
= CERT_DupCertificate(cert
)) == NULL
)
257 if ((signerinfo
->signerIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
)) == NULL
)
259 dprintfRC("nss_cmssignerinfo_create: SecCmsSignerIDIssuerSN: cert.rc %d\n",
260 (int)CFGetRetainCount(signerinfo
->cert
));
262 case SecCmsSignerIDSubjectKeyID
:
263 signerinfo
->signerIdentifier
.identifierType
= SecCmsSignerIDSubjectKeyID
;
264 PORT_Assert(subjKeyID
);
267 signerinfo
->signerIdentifier
.id
.subjectKeyID
= PORT_ArenaNew(poolp
, CSSM_DATA
);
268 SECITEM_CopyItem(poolp
, signerinfo
->signerIdentifier
.id
.subjectKeyID
,
270 signerinfo
->pubKey
= SECKEY_CopyPublicKey(pubKey
);
271 if (!signerinfo
->pubKey
)
281 signerinfo
->signingKey
= SECKEY_CopyPrivateKey(signingKey
);
282 if (!signerinfo
->signingKey
)
285 /* set version right now */
286 version
= SEC_CMS_SIGNER_INFO_VERSION_ISSUERSN
;
287 /* RFC2630 5.3 "version is the syntax version number. If the .... " */
288 if (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDSubjectKeyID
)
289 version
= SEC_CMS_SIGNER_INFO_VERSION_SUBJKEY
;
290 (void)SEC_ASN1EncodeInteger(poolp
, &(signerinfo
->version
), (long)version
);
292 if (SECOID_SetAlgorithmID(poolp
, &signerinfo
->digestAlg
, digestalgtag
, NULL
) != SECSuccess
)
295 PORT_ArenaUnmark(poolp
, mark
);
299 PORT_ArenaRelease(poolp
, mark
);
304 * SecCmsSignerInfoDestroy - destroy a SignerInfo data structure
307 SecCmsSignerInfoDestroy(SecCmsSignerInfoRef si
)
309 if (si
->cert
!= NULL
) {
310 dprintfRC("SecCmsSignerInfoDestroy top: certp %p cert.rc %d\n",
311 si
->cert
, (int)CFGetRetainCount(si
->cert
));
312 CERT_DestroyCertificate(si
->cert
);
314 if (si
->certList
!= NULL
) {
315 dprintfRC("SecCmsSignerInfoDestroy top: certList.rc %d\n",
316 (int)CFGetRetainCount(si
->certList
));
317 CFRelease(si
->certList
);
319 if (si
->timestampCertList
!= NULL
) {
320 dprintfRC("SecCmsSignerInfoDestroy top: timestampCertList.rc %d\n",
321 (int)CFGetRetainCount(si
->timestampCertList
));
322 CFRelease(si
->timestampCertList
);
324 /* XXX storage ??? */
328 * SecCmsSignerInfoSign - sign something
332 SecCmsSignerInfoSign(SecCmsSignerInfoRef signerinfo
, CSSM_DATA_PTR digest
, CSSM_DATA_PTR contentType
)
334 SecCertificateRef cert
;
335 SecPrivateKeyRef privkey
= NULL
;
336 SECOidTag digestalgtag
;
337 SECOidTag pubkAlgTag
;
338 CSSM_DATA signature
= { 0 };
340 PLArenaPool
*poolp
, *tmppoolp
= NULL
;
341 const SECAlgorithmID
*algID
;
342 SECAlgorithmID freeAlgID
;
343 //CERTSubjectPublicKeyInfo *spki;
345 PORT_Assert (digest
!= NULL
);
347 poolp
= signerinfo
->cmsg
->poolp
;
349 switch (signerinfo
->signerIdentifier
.identifierType
) {
350 case SecCmsSignerIDIssuerSN
:
351 privkey
= signerinfo
->signingKey
;
352 signerinfo
->signingKey
= NULL
;
353 cert
= signerinfo
->cert
;
354 if (SecCertificateGetAlgorithmID(cert
,&algID
)) {
355 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
359 case SecCmsSignerIDSubjectKeyID
:
360 privkey
= signerinfo
->signingKey
;
361 signerinfo
->signingKey
= NULL
;
363 spki
= SECKEY_CreateSubjectPublicKeyInfo(signerinfo
->pubKey
);
364 SECKEY_DestroyPublicKey(signerinfo
->pubKey
);
365 signerinfo
->pubKey
= NULL
;
366 SECOID_CopyAlgorithmID(NULL
, &freeAlgID
, &spki
->algorithm
);
367 SECKEY_DestroySubjectPublicKeyInfo(spki
);
371 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
372 if (SecKeyGetAlgorithmID(signerinfo
->pubKey
,&algID
)) {
374 /* TBD: Unify this code. Currently, iOS has an incompatible
375 * SecKeyGetAlgorithmID implementation. */
378 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
381 CFRelease(signerinfo
->pubKey
);
382 signerinfo
->pubKey
= NULL
;
386 PORT_SetError(SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE
);
389 digestalgtag
= SecCmsSignerInfoGetDigestAlgTag(signerinfo
);
391 * XXX I think there should be a cert-level interface for this,
392 * so that I do not have to know about subjectPublicKeyInfo...
394 pubkAlgTag
= SECOID_GetAlgorithmTag(algID
);
395 if (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDSubjectKeyID
) {
396 SECOID_DestroyAlgorithmID(&freeAlgID
, PR_FALSE
);
401 /* Fortezza MISSI have weird signature formats.
402 * Map them to standard DSA formats
404 pubkAlgTag
= PK11_FortezzaMapSig(pubkAlgTag
);
407 if (signerinfo
->authAttr
!= NULL
) {
408 CSSM_DATA encoded_attrs
;
410 /* find and fill in the message digest attribute. */
411 rv
= SecCmsAttributeArraySetAttr(poolp
, &(signerinfo
->authAttr
),
412 SEC_OID_PKCS9_MESSAGE_DIGEST
, digest
, PR_FALSE
);
413 if (rv
!= SECSuccess
)
416 if (contentType
!= NULL
) {
417 /* if the caller wants us to, find and fill in the content type attribute. */
418 rv
= SecCmsAttributeArraySetAttr(poolp
, &(signerinfo
->authAttr
),
419 SEC_OID_PKCS9_CONTENT_TYPE
, contentType
, PR_FALSE
);
420 if (rv
!= SECSuccess
)
424 if ((tmppoolp
= PORT_NewArena (1024)) == NULL
) {
425 PORT_SetError(SEC_ERROR_NO_MEMORY
);
430 * Before encoding, reorder the attributes so that when they
431 * are encoded, they will be conforming DER, which is required
432 * to have a specific order and that is what must be used for
433 * the hash/signature. We do this here, rather than building
434 * it into EncodeAttributes, because we do not want to do
435 * such reordering on incoming messages (which also uses
436 * EncodeAttributes) or our old signatures (and other "broken"
437 * implementations) will not verify. So, we want to guarantee
438 * that we send out good DER encodings of attributes, but not
439 * to expect to receive them.
441 if (SecCmsAttributeArrayReorder(signerinfo
->authAttr
) != SECSuccess
)
444 encoded_attrs
.Data
= NULL
;
445 encoded_attrs
.Length
= 0;
446 if (SecCmsAttributeArrayEncode(tmppoolp
, &(signerinfo
->authAttr
),
447 &encoded_attrs
) == NULL
)
450 rv
= SEC_SignData(&signature
, encoded_attrs
.Data
, (int)encoded_attrs
.Length
,
451 privkey
, digestalgtag
, pubkAlgTag
);
452 PORT_FreeArena(tmppoolp
, PR_FALSE
); /* awkward memory management :-( */
455 rv
= SGN_Digest(privkey
, digestalgtag
, pubkAlgTag
, &signature
, digest
);
457 SECKEY_DestroyPrivateKey(privkey
);
460 if (rv
!= SECSuccess
)
463 if (SECITEM_CopyItem(poolp
, &(signerinfo
->encDigest
), &signature
)
467 SECITEM_FreeItem(&signature
, PR_FALSE
);
469 if(pubkAlgTag
== SEC_OID_EC_PUBLIC_KEY
) {
471 * RFC 3278 section section 2.1.1 states that the signatureAlgorithm
472 * field contains the full ecdsa-with-SHA1 OID, not plain old ecPublicKey
473 * as would appear in other forms of signed datas. However Microsoft doesn't
474 * do this, it puts ecPublicKey there, and if we put ecdsa-with-SHA1 there,
475 * MS can't verify - presumably because it takes the digest of the digest
476 * before feeding it to ECDSA.
477 * We handle this with a preference; default if it's not there is
478 * "Microsoft compatibility mode".
480 if(!SecCmsMsEcdsaCompatMode()) {
481 pubkAlgTag
= SEC_OID_ECDSA_WithSHA1
;
483 /* else violating the spec for compatibility */
486 if (SECOID_SetAlgorithmID(poolp
, &(signerinfo
->digestEncAlg
), pubkAlgTag
,
493 if (signature
.Length
!= 0)
494 SECITEM_FreeItem (&signature
, PR_FALSE
);
496 SECKEY_DestroyPrivateKey(privkey
);
497 if((algID
!= NULL
) & (algID
!= &freeAlgID
)) {
498 /* this is dicey - this was actually mallocd by either SecCertificate or
499 * by SecKey...it all boils down to a free() in the end though. */
500 SECOID_DestroyAlgorithmID((SECAlgorithmID
*)algID
, PR_FALSE
);
503 PORT_FreeArena(tmppoolp
, PR_FALSE
);
508 SecCmsSignerInfoVerifyCertificate(SecCmsSignerInfoRef signerinfo
, SecKeychainRef keychainOrArray
,
509 CFTypeRef policies
, SecTrustRef
*trustRef
)
511 SecCertificateRef cert
;
512 CFAbsoluteTime stime
;
514 CSSM_DATA_PTR
*otherCerts
;
516 if ((cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, keychainOrArray
)) == NULL
) {
517 dprintf("SecCmsSignerInfoVerifyCertificate: no signing cert\n");
518 signerinfo
->verificationStatus
= SecCmsVSSigningCertNotFound
;
523 * Get and convert the signing time; if available, it will be used
524 * both on the cert verification and for importing the sender
527 CFTypeRef timeStampPolicies
=SecPolicyCreateAppleTimeStampingAndRevocationPolicies(policies
);
528 if (SecCmsSignerInfoGetTimestampTimeWithPolicy(signerinfo
, timeStampPolicies
, &stime
) != SECSuccess
)
529 if (SecCmsSignerInfoGetSigningTime(signerinfo
, &stime
) != SECSuccess
)
530 stime
= CFAbsoluteTimeGetCurrent();
531 CFReleaseSafe(timeStampPolicies
);
533 rv
= SecCmsSignedDataRawCerts(signerinfo
->sigd
, &otherCerts
);
537 rv
= CERT_VerifyCert(keychainOrArray
, cert
, otherCerts
, policies
, stime
, trustRef
);
538 dprintfRC("SecCmsSignerInfoVerifyCertificate after vfy: certp %p cert.rc %d\n",
539 cert
, (int)CFGetRetainCount(cert
));
542 if (PORT_GetError() == SEC_ERROR_UNTRUSTED_CERT
)
544 /* Signature or digest level verificationStatus errors should supercede certificate level errors, so only change the verificationStatus if the status was GoodSignature. */
545 if (signerinfo
->verificationStatus
== SecCmsVSGoodSignature
)
546 signerinfo
->verificationStatus
= SecCmsVSSigningCertNotTrusted
;
549 /* FIXME isn't this leaking the cert? */
550 dprintf("SecCmsSignerInfoVerifyCertificate: CertVerify rtn %d\n", (int)rv
);
554 static void debugShowSigningCertificate(SecCmsSignerInfoRef signerinfo
)
557 CFStringRef cn
= SecCmsSignerInfoGetSignerCommonName(signerinfo
);
560 char *ccn
= cfStringToChar(cn
);
563 dprintf("SecCmsSignerInfoVerify: cn: %s\n", ccn
);
572 * SecCmsSignerInfoVerify - verify the signature of a single SignerInfo
574 * Just verifies the signature. The assumption is that verification of the certificate
578 SecCmsSignerInfoVerify(SecCmsSignerInfoRef signerinfo
, CSSM_DATA_PTR digest
, CSSM_DATA_PTR contentType
)
580 return SecCmsSignerInfoVerifyWithPolicy(signerinfo
,NULL
, digest
,contentType
);
584 SecCmsSignerInfoVerifyWithPolicy(SecCmsSignerInfoRef signerinfo
,CFTypeRef timeStampPolicy
, CSSM_DATA_PTR digest
, CSSM_DATA_PTR contentType
)
586 SecPublicKeyRef publickey
= NULL
;
587 SecCmsAttribute
*attr
;
588 CSSM_DATA encoded_attrs
;
589 SecCertificateRef cert
;
590 SecCmsVerificationStatus vs
= SecCmsVSUnverified
;
592 SECOidTag digestAlgTag
, digestEncAlgTag
;
594 if (signerinfo
== NULL
)
597 /* SecCmsSignerInfoGetSigningCertificate will fail if 2nd parm is NULL and */
598 /* cert has not been verified */
599 if ((cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, NULL
)) == NULL
) {
600 dprintf("SecCmsSignerInfoVerify: no signing cert\n");
601 vs
= SecCmsVSSigningCertNotFound
;
605 dprintfRC("SecCmsSignerInfoVerify top: cert %p cert.rc %d\n", cert
, (int)CFGetRetainCount(cert
));
607 debugShowSigningCertificate(signerinfo
);
609 if (SecCertificateCopyPublicKey(cert
, &publickey
)) {
610 vs
= SecCmsVSProcessingError
;
614 digestAlgTag
= SECOID_GetAlgorithmTag(&(signerinfo
->digestAlg
));
615 digestEncAlgTag
= SECOID_GetAlgorithmTag(&(signerinfo
->digestEncAlg
));
618 * Gross hack necessitated by RFC 3278 section 2.1.1, which states
619 * that the signature algorithm (here, digestEncAlg) contains ecdsa_with-SHA1,
620 * *not* (as in all other algorithms) the raw signature algorithm, e.g.
621 * pkcs1RSAEncryption.
623 if(digestEncAlgTag
== SEC_OID_ECDSA_WithSHA1
) {
624 digestEncAlgTag
= SEC_OID_EC_PUBLIC_KEY
;
627 if (!SecCmsArrayIsEmpty((void **)signerinfo
->authAttr
)) {
632 * RFC2630 sez that if there are any authenticated attributes,
633 * then there must be one for content type which matches the
634 * content type of the content being signed, and there must
635 * be one for message digest which matches our message digest.
636 * So check these things first.
638 if ((attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
639 SEC_OID_PKCS9_CONTENT_TYPE
, PR_TRUE
)) == NULL
)
641 vs
= SecCmsVSMalformedSignature
;
645 if (SecCmsAttributeCompareValue(attr
, contentType
) == PR_FALSE
) {
646 vs
= SecCmsVSMalformedSignature
;
654 if ((attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
, SEC_OID_PKCS9_MESSAGE_DIGEST
, PR_TRUE
)) == NULL
)
656 vs
= SecCmsVSMalformedSignature
;
659 if (SecCmsAttributeCompareValue(attr
, digest
) == PR_FALSE
) {
660 vs
= SecCmsVSDigestMismatch
;
664 if ((poolp
= PORT_NewArena (1024)) == NULL
) {
665 vs
= SecCmsVSProcessingError
;
672 * The signature is based on a digest of the DER-encoded authenticated
673 * attributes. So, first we encode and then we digest/verify.
674 * we trust the decoder to have the attributes in the right (sorted) order
676 encoded_attrs
.Data
= NULL
;
677 encoded_attrs
.Length
= 0;
679 if (SecCmsAttributeArrayEncode(poolp
, &(signerinfo
->authAttr
), &encoded_attrs
) == NULL
||
680 encoded_attrs
.Data
== NULL
|| encoded_attrs
.Length
== 0)
682 vs
= SecCmsVSProcessingError
;
686 vs
= (VFY_VerifyData (encoded_attrs
.Data
, (int)encoded_attrs
.Length
,
687 publickey
, &(signerinfo
->encDigest
),
688 digestAlgTag
, digestEncAlgTag
,
689 signerinfo
->cmsg
->pwfn_arg
) != SECSuccess
) ? SecCmsVSBadSignature
: SecCmsVSGoodSignature
;
691 dprintf("VFY_VerifyData (authenticated attributes): %s\n",
692 (vs
== SecCmsVSGoodSignature
)?"SecCmsVSGoodSignature":"SecCmsVSBadSignature");
694 PORT_FreeArena(poolp
, PR_FALSE
); /* awkward memory management :-( */
699 /* No authenticated attributes. The signature is based on the plain message digest. */
700 sig
= &(signerinfo
->encDigest
);
701 if (sig
->Length
== 0)
704 vs
= (VFY_VerifyDigest(digest
, publickey
, sig
,
705 digestAlgTag
, digestEncAlgTag
,
706 signerinfo
->cmsg
->pwfn_arg
) != SECSuccess
) ? SecCmsVSBadSignature
: SecCmsVSGoodSignature
;
708 dprintf("VFY_VerifyData (plain message digest): %s\n",
709 (vs
== SecCmsVSGoodSignature
)?"SecCmsVSGoodSignature":"SecCmsVSBadSignature");
712 if (!SecCmsArrayIsEmpty((void **)signerinfo
->unAuthAttr
))
714 dprintf("found an unAuthAttr\n");
715 OSStatus rux
= SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy(signerinfo
,timeStampPolicy
);
716 dprintf("SecCmsSignerInfoVerifyUnAuthAttrs Status: %ld\n", (long)rux
);
721 if (vs
== SecCmsVSBadSignature
) {
723 * XXX Change the generic error into our specific one, because
724 * in that case we get a better explanation out of the Security
725 * Advisor. This is really a bug in our error strings (the
726 * "generic" error has a lousy/wrong message associated with it
727 * which assumes the signature verification was done for the
728 * purposes of checking the issuer signature on a certificate)
729 * but this is at least an easy workaround and/or in the
730 * Security Advisor, which specifically checks for the error
731 * SEC_ERROR_PKCS7_BAD_SIGNATURE and gives more explanation
732 * in that case but does not similarly check for
733 * SEC_ERROR_BAD_SIGNATURE. It probably should, but then would
734 * probably say the wrong thing in the case that it *was* the
735 * certificate signature check that failed during the cert
736 * verification done above. Our error handling is really a mess.
738 if (PORT_GetError() == SEC_ERROR_BAD_SIGNATURE
)
739 PORT_SetError(SEC_ERROR_PKCS7_BAD_SIGNATURE
);
742 if (publickey
!= NULL
)
743 CFRelease(publickey
);
745 signerinfo
->verificationStatus
= vs
;
746 dprintfRC("SecCmsSignerInfoVerify end: cerp %p cert.rc %d\n",
747 cert
, (int)CFGetRetainCount(cert
));
749 dprintf("verificationStatus: %d\n", vs
);
751 return (vs
== SecCmsVSGoodSignature
) ? SECSuccess
: SECFailure
;
754 if (publickey
!= NULL
)
755 SECKEY_DestroyPublicKey (publickey
);
757 dprintf("verificationStatus2: %d\n", vs
);
758 signerinfo
->verificationStatus
= vs
;
760 PORT_SetError (SEC_ERROR_PKCS7_BAD_SIGNATURE
);
765 SecCmsSignerInfoVerifyUnAuthAttrs(SecCmsSignerInfoRef signerinfo
) {
766 return SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy(signerinfo
, NULL
);
770 SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy(SecCmsSignerInfoRef signerinfo
,CFTypeRef timeStampPolicy
)
773 unAuthAttr is an array of attributes; we expect to
774 see just one: the timestamp blob. If we have an unAuthAttr,
775 but don't see a timestamp, return an error since we have
776 no other cases where this would be present.
779 SecCmsAttribute
*attr
= NULL
;
780 OSStatus status
= SECFailure
;
782 require(signerinfo
, xit
);
783 attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->unAuthAttr
,
784 SEC_OID_PKCS9_TIMESTAMP_TOKEN
, PR_TRUE
);
787 status
= errSecTimestampMissing
;
791 dprintf("found an id-ct-TSTInfo\n");
792 // Don't check the nonce in this case
793 status
= decodeTimeStampTokenWithPolicy(signerinfo
, timeStampPolicy
, (attr
->values
)[0], &signerinfo
->encDigest
, 0);
799 SecCmsSignerInfoGetEncDigest(SecCmsSignerInfoRef signerinfo
)
801 return &signerinfo
->encDigest
;
804 SecCmsVerificationStatus
805 SecCmsSignerInfoGetVerificationStatus(SecCmsSignerInfoRef signerinfo
)
807 return signerinfo
->verificationStatus
;
811 SecCmsSignerInfoGetDigestAlg(SecCmsSignerInfoRef signerinfo
)
813 return SECOID_FindOID (&(signerinfo
->digestAlg
.algorithm
));
817 SecCmsSignerInfoGetDigestAlgTag(SecCmsSignerInfoRef signerinfo
)
821 algdata
= SECOID_FindOID (&(signerinfo
->digestAlg
.algorithm
));
823 return algdata
->offset
;
825 return SEC_OID_UNKNOWN
;
829 SecCmsSignerInfoGetCertList(SecCmsSignerInfoRef signerinfo
)
831 dprintfRC("SecCmsSignerInfoGetCertList: certList.rc %d\n",
832 (int)CFGetRetainCount(signerinfo
->certList
));
833 return signerinfo
->certList
;
837 SecCmsSignerInfoGetTimestampCertList(SecCmsSignerInfoRef signerinfo
)
839 dprintfRC("SecCmsSignerInfoGetCertList: timestampCertList.rc %d\n",
840 (int)CFGetRetainCount(signerinfo
->timestampCertList
));
841 return signerinfo
->timestampCertList
;
847 SecCmsSignerInfoGetVersion(SecCmsSignerInfoRef signerinfo
)
849 unsigned long version
;
851 /* always take apart the CSSM_DATA */
852 if (SEC_ASN1DecodeInteger(&(signerinfo
->version
), &version
) != SECSuccess
)
859 * SecCmsSignerInfoGetSigningTime - return the signing time,
860 * in UTCTime format, of a CMS signerInfo.
862 * sinfo - signerInfo data for this signer
864 * Returns a pointer to XXXX (what?)
865 * A return value of NULL is an error.
868 SecCmsSignerInfoGetSigningTime(SecCmsSignerInfoRef sinfo
, CFAbsoluteTime
*stime
)
870 SecCmsAttribute
*attr
;
876 if (sinfo
->signingTime
!= 0) {
877 *stime
= sinfo
->signingTime
; /* cached copy */
881 attr
= SecCmsAttributeArrayFindAttrByOidTag(sinfo
->authAttr
, SEC_OID_PKCS9_SIGNING_TIME
, PR_TRUE
);
882 /* XXXX multi-valued attributes NIH */
883 if (attr
== NULL
|| (value
= SecCmsAttributeGetValue(attr
)) == NULL
)
884 return errSecSigningTimeMissing
;
885 if (DER_UTCTimeToCFDate(value
, stime
) != SECSuccess
)
886 return errSecSigningTimeMissing
;
887 sinfo
->signingTime
= *stime
; /* make cached copy */
892 SecCmsSignerInfoGetTimestampTime(SecCmsSignerInfoRef sinfo
, CFAbsoluteTime
*stime
)
894 return SecCmsSignerInfoGetTimestampTimeWithPolicy(sinfo
, NULL
, stime
);
898 SecCmsSignerInfoGetTimestampTimeWithPolicy(SecCmsSignerInfoRef sinfo
, CFTypeRef timeStampPolicy
, CFAbsoluteTime
*stime
)
900 OSStatus status
= paramErr
;
902 require(sinfo
&& stime
, xit
);
904 if (sinfo
->timestampTime
!= 0)
906 *stime
= sinfo
->timestampTime
; /* cached copy */
910 // A bit heavyweight if haven't already called verify
911 status
= SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy(sinfo
,timeStampPolicy
);
912 *stime
= sinfo
->timestampTime
;
918 * Return the signing cert of a CMS signerInfo.
920 * the certs in the enclosing SignedData must have been imported already
923 SecCmsSignerInfoGetSigningCertificate(SecCmsSignerInfoRef signerinfo
, SecKeychainRef keychainOrArray
)
925 SecCertificateRef cert
;
926 SecCmsSignerIdentifier
*sid
;
928 CSSM_DATA_PTR
*rawCerts
;
930 if (signerinfo
->cert
!= NULL
) {
931 dprintfRC("SecCmsSignerInfoGetSigningCertificate top: cert %p cert.rc %d\n",
932 signerinfo
->cert
, (int)CFGetRetainCount(signerinfo
->cert
));
933 return signerinfo
->cert
;
935 ortn
= SecCmsSignedDataRawCerts(signerinfo
->sigd
, &rawCerts
);
939 dprintf("SecCmsSignerInfoGetSigningCertificate: numRawCerts %d\n",
940 SecCmsArrayCount((void **)rawCerts
));
943 * This cert will also need to be freed, but since we save it
944 * in signerinfo for later, we do not want to destroy it when
945 * we leave this function -- we let the clean-up of the entire
946 * cinfo structure later do the destroy of this cert.
948 sid
= &signerinfo
->signerIdentifier
;
949 switch (sid
->identifierType
) {
950 case SecCmsSignerIDIssuerSN
:
951 cert
= CERT_FindCertByIssuerAndSN(keychainOrArray
, rawCerts
, signerinfo
->cmsg
->poolp
,
952 sid
->id
.issuerAndSN
);
954 case SecCmsSignerIDSubjectKeyID
:
955 cert
= CERT_FindCertBySubjectKeyID(keychainOrArray
, rawCerts
, sid
->id
.subjectKeyID
);
962 /* cert can be NULL at that point */
963 signerinfo
->cert
= cert
; /* earmark it */
964 dprintfRC("SecCmsSignerInfoGetSigningCertificate end: certp %p cert.rc %d\n",
965 signerinfo
->cert
, (int)CFGetRetainCount(signerinfo
->cert
));
971 * SecCmsSignerInfoGetSignerCommonName - return the common name of the signer
973 * sinfo - signerInfo data for this signer
975 * Returns a CFStringRef containing the common name of the signer.
976 * A return value of NULL is an error.
979 SecCmsSignerInfoGetSignerCommonName(SecCmsSignerInfoRef sinfo
)
981 SecCertificateRef signercert
;
982 CFStringRef commonName
= NULL
;
984 /* will fail if cert is not verified */
985 if ((signercert
= SecCmsSignerInfoGetSigningCertificate(sinfo
, NULL
)) == NULL
)
988 SecCertificateCopyCommonName(signercert
, &commonName
);
994 * SecCmsSignerInfoGetSignerEmailAddress - return the email address of the signer
996 * sinfo - signerInfo data for this signer
998 * Returns a CFStringRef containing the name of the signer.
999 * A return value of NULL is an error.
1002 SecCmsSignerInfoGetSignerEmailAddress(SecCmsSignerInfoRef sinfo
)
1004 SecCertificateRef signercert
;
1005 CFStringRef emailAddress
= NULL
;
1007 if ((signercert
= SecCmsSignerInfoGetSigningCertificate(sinfo
, NULL
)) == NULL
)
1010 SecCertificateGetEmailAddress(signercert
, &emailAddress
);
1012 return emailAddress
;
1017 * SecCmsSignerInfoAddAuthAttr - add an attribute to the
1018 * authenticated (i.e. signed) attributes of "signerinfo".
1021 SecCmsSignerInfoAddAuthAttr(SecCmsSignerInfoRef signerinfo
, SecCmsAttribute
*attr
)
1023 return SecCmsAttributeArrayAddAttr(signerinfo
->cmsg
->poolp
, &(signerinfo
->authAttr
), attr
);
1027 * SecCmsSignerInfoAddUnauthAttr - add an attribute to the
1028 * unauthenticated attributes of "signerinfo".
1031 SecCmsSignerInfoAddUnauthAttr(SecCmsSignerInfoRef signerinfo
, SecCmsAttribute
*attr
)
1033 return SecCmsAttributeArrayAddAttr(signerinfo
->cmsg
->poolp
, &(signerinfo
->unAuthAttr
), attr
);
1037 * SecCmsSignerInfoAddSigningTime - add the signing time to the
1038 * authenticated (i.e. signed) attributes of "signerinfo".
1040 * This is expected to be included in outgoing signed
1041 * messages for email (S/MIME) but is likely useful in other situations.
1043 * This should only be added once; a second call will do nothing.
1045 * XXX This will probably just shove the current time into "signerinfo"
1046 * but it will not actually get signed until the entire item is
1047 * processed for encoding. Is this (expected to be small) delay okay?
1050 SecCmsSignerInfoAddSigningTime(SecCmsSignerInfoRef signerinfo
, CFAbsoluteTime t
)
1052 SecCmsAttribute
*attr
;
1057 poolp
= signerinfo
->cmsg
->poolp
;
1059 mark
= PORT_ArenaMark(poolp
);
1061 /* create new signing time attribute */
1062 if (DER_CFDateToUTCTime(t
, &stime
) != SECSuccess
)
1065 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_SIGNING_TIME
, &stime
, PR_FALSE
)) == NULL
) {
1066 SECITEM_FreeItem (&stime
, PR_FALSE
);
1070 SECITEM_FreeItem (&stime
, PR_FALSE
);
1072 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1075 PORT_ArenaUnmark (poolp
, mark
);
1080 PORT_ArenaRelease (poolp
, mark
);
1085 * SecCmsSignerInfoAddSMIMECaps - add a SMIMECapabilities attribute to the
1086 * authenticated (i.e. signed) attributes of "signerinfo".
1088 * This is expected to be included in outgoing signed
1089 * messages for email (S/MIME).
1092 SecCmsSignerInfoAddSMIMECaps(SecCmsSignerInfoRef signerinfo
)
1094 SecCmsAttribute
*attr
;
1095 CSSM_DATA_PTR smimecaps
= NULL
;
1099 poolp
= signerinfo
->cmsg
->poolp
;
1101 mark
= PORT_ArenaMark(poolp
);
1103 smimecaps
= SECITEM_AllocItem(poolp
, NULL
, 0);
1104 if (smimecaps
== NULL
)
1107 /* create new signing time attribute */
1109 // @@@ We don't do Fortezza yet.
1110 if (SecSMIMECreateSMIMECapabilities((SecArenaPoolRef
)poolp
, smimecaps
, PR_FALSE
) != SECSuccess
)
1112 if (SecSMIMECreateSMIMECapabilities(poolp
, smimecaps
,
1113 PK11_FortezzaHasKEA(signerinfo
->cert
)) != SECSuccess
)
1117 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_SMIME_CAPABILITIES
, smimecaps
, PR_TRUE
)) == NULL
)
1120 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1123 PORT_ArenaUnmark (poolp
, mark
);
1127 PORT_ArenaRelease (poolp
, mark
);
1132 * SecCmsSignerInfoAddSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the
1133 * authenticated (i.e. signed) attributes of "signerinfo".
1135 * This is expected to be included in outgoing signed messages for email (S/MIME).
1138 SecCmsSignerInfoAddSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo
, SecCertificateRef cert
, SecKeychainRef keychainOrArray
)
1140 SecCmsAttribute
*attr
;
1141 CSSM_DATA_PTR smimeekp
= NULL
;
1148 /* verify this cert for encryption */
1149 policy
= CERT_PolicyForCertUsage(certUsageEmailRecipient
);
1150 if (CERT_VerifyCert(keychainOrArray
, cert
, policy
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1157 poolp
= signerinfo
->cmsg
->poolp
;
1158 mark
= PORT_ArenaMark(poolp
);
1160 smimeekp
= SECITEM_AllocItem(poolp
, NULL
, 0);
1161 if (smimeekp
== NULL
)
1164 /* create new signing time attribute */
1165 if (SecSMIMECreateSMIMEEncKeyPrefs((SecArenaPoolRef
)poolp
, smimeekp
, cert
) != SECSuccess
)
1168 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE
, smimeekp
, PR_TRUE
)) == NULL
)
1171 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1174 PORT_ArenaUnmark (poolp
, mark
);
1178 PORT_ArenaRelease (poolp
, mark
);
1183 * SecCmsSignerInfoAddMSSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the
1184 * authenticated (i.e. signed) attributes of "signerinfo", using the OID prefered by Microsoft.
1186 * This is expected to be included in outgoing signed messages for email (S/MIME),
1187 * if compatibility with Microsoft mail clients is wanted.
1190 SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo
, SecCertificateRef cert
, SecKeychainRef keychainOrArray
)
1192 SecCmsAttribute
*attr
;
1193 CSSM_DATA_PTR smimeekp
= NULL
;
1200 /* verify this cert for encryption */
1201 policy
= CERT_PolicyForCertUsage(certUsageEmailRecipient
);
1202 if (CERT_VerifyCert(keychainOrArray
, cert
, policy
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1209 poolp
= signerinfo
->cmsg
->poolp
;
1210 mark
= PORT_ArenaMark(poolp
);
1212 smimeekp
= SECITEM_AllocItem(poolp
, NULL
, 0);
1213 if (smimeekp
== NULL
)
1216 /* create new signing time attribute */
1217 if (SecSMIMECreateMSSMIMEEncKeyPrefs((SecArenaPoolRef
)poolp
, smimeekp
, cert
) != SECSuccess
)
1220 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_MS_SMIME_ENCRYPTION_KEY_PREFERENCE
, smimeekp
, PR_TRUE
)) == NULL
)
1223 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1226 PORT_ArenaUnmark (poolp
, mark
);
1230 PORT_ArenaRelease (poolp
, mark
);
1235 * SecCmsSignerInfoAddTimeStamp - add time stamp to the
1236 * unauthenticated (i.e. unsigned) attributes of "signerinfo".
1238 * This will initially be used for time stamping signed applications
1239 * by using a Time Stamping Authority. It may also be included in outgoing signed
1240 * messages for email (S/MIME), and may be useful in other situations.
1242 * This should only be added once; a second call will do nothing.
1247 Countersignature attribute values have ASN.1 type Countersignature:
1248 Countersignature ::= SignerInfo
1249 Countersignature values have the same meaning as SignerInfo values
1250 for ordinary signatures, except that:
1251 1. The signedAttributes field MUST NOT contain a content-type
1252 attribute; there is no content type for countersignatures.
1253 2. The signedAttributes field MUST contain a message-digest
1254 attribute if it contains any other attributes.
1255 3. The input to the message-digesting process is the contents octets
1256 of the DER encoding of the signatureValue field of the SignerInfo
1257 value with which the attribute is associated.
1262 @abstract Create a timestamp unsigned attribute with a TimeStampToken.
1266 SecCmsSignerInfoAddTimeStamp(SecCmsSignerInfoRef signerinfo
, CSSM_DATA
*tstoken
)
1268 SecCmsAttribute
*attr
;
1269 PLArenaPool
*poolp
= signerinfo
->cmsg
->poolp
;
1270 void *mark
= PORT_ArenaMark(poolp
);
1272 // We have already encoded this ourselves, so last param is PR_TRUE
1273 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_TIMESTAMP_TOKEN
, tstoken
, PR_TRUE
)) == NULL
)
1276 if (SecCmsSignerInfoAddUnauthAttr(signerinfo
, attr
) != SECSuccess
)
1279 PORT_ArenaUnmark (poolp
, mark
);
1284 PORT_ArenaRelease (poolp
, mark
);
1289 * SecCmsSignerInfoAddCounterSignature - countersign a signerinfo
1291 * 1. digest the DER-encoded signature value of the original signerinfo
1292 * 2. create new signerinfo with correct version, sid, digestAlg
1293 * 3. add message-digest authAttr, but NO content-type
1294 * 4. sign the authAttrs
1295 * 5. DER-encode the new signerInfo
1296 * 6. add the whole thing to original signerInfo's unAuthAttrs
1297 * as a SEC_OID_PKCS9_COUNTER_SIGNATURE attribute
1299 * XXXX give back the new signerinfo?
1302 SecCmsSignerInfoAddCounterSignature(SecCmsSignerInfoRef signerinfo
,
1303 SECOidTag digestalg
, SecIdentityRef identity
)
1310 * XXXX the following needs to be done in the S/MIME layer code
1311 * after signature of a signerinfo is verified
1314 SecCmsSignerInfoSaveSMIMEProfile(SecCmsSignerInfoRef signerinfo
)
1316 SecCertificateRef cert
= NULL
;
1317 CSSM_DATA_PTR profile
= NULL
;
1318 SecCmsAttribute
*attr
;
1319 CSSM_DATA_PTR utc_stime
= NULL
;
1323 Boolean must_free_cert
= PR_FALSE
;
1325 SecKeychainRef keychainOrArray
;
1327 status
= SecKeychainCopyDefault(&keychainOrArray
);
1329 /* sanity check - see if verification status is ok (unverified does not count...) */
1330 if (signerinfo
->verificationStatus
!= SecCmsVSGoodSignature
)
1333 /* find preferred encryption cert */
1334 if (!SecCmsArrayIsEmpty((void **)signerinfo
->authAttr
) &&
1335 (attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
1336 SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE
, PR_TRUE
)) != NULL
)
1337 { /* we have a SMIME_ENCRYPTION_KEY_PREFERENCE attribute! */
1338 ekp
= SecCmsAttributeGetValue(attr
);
1342 /* we assume that all certs coming with the message have been imported to the */
1343 /* temporary database */
1344 cert
= SecSMIMEGetCertFromEncryptionKeyPreference(keychainOrArray
, ekp
);
1347 must_free_cert
= PR_TRUE
;
1351 /* no preferred cert found?
1352 * find the cert the signerinfo is signed with instead */
1353 CFStringRef emailAddress
=NULL
;
1355 cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, keychainOrArray
);
1358 if (SecCertificateGetEmailAddress(cert
,&emailAddress
))
1362 /* verify this cert for encryption (has been verified for signing so far) */ /* don't verify this cert for encryption. It may just be a signing cert.
1363 * that's OK, we can still save the S/MIME profile. The encryption cert
1364 * should have already been saved */
1366 if (CERT_VerifyCert(keychainOrArray
, cert
, certUsageEmailRecipient
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1368 CERT_DestroyCertificate(cert
);
1373 /* XXX store encryption cert permanently? */
1376 * Remember the current error set because we do not care about
1377 * anything set by the functions we are about to call.
1379 save_error
= PORT_GetError();
1381 if (!SecCmsArrayIsEmpty((void **)signerinfo
->authAttr
)) {
1382 attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
1383 SEC_OID_PKCS9_SMIME_CAPABILITIES
,
1385 profile
= SecCmsAttributeGetValue(attr
);
1386 attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
1387 SEC_OID_PKCS9_SIGNING_TIME
,
1389 utc_stime
= SecCmsAttributeGetValue(attr
);
1392 rv
= CERT_SaveSMimeProfile (cert
, profile
, utc_stime
);
1394 CERT_DestroyCertificate(cert
);
1397 * Restore the saved error in case the calls above set a new
1398 * one that we do not actually care about.
1400 PORT_SetError (save_error
);
1406 * SecCmsSignerInfoIncludeCerts - set cert chain inclusion mode for this signer
1409 SecCmsSignerInfoIncludeCerts(SecCmsSignerInfoRef signerinfo
, SecCmsCertChainMode cm
, SECCertUsage usage
)
1411 if (signerinfo
->cert
== NULL
)
1414 /* don't leak if we get called twice */
1415 if (signerinfo
->certList
!= NULL
) {
1416 CFRelease(signerinfo
->certList
);
1417 signerinfo
->certList
= NULL
;
1422 signerinfo
->certList
= NULL
;
1424 case SecCmsCMCertOnly
:
1425 signerinfo
->certList
= CERT_CertListFromCert(signerinfo
->cert
);
1427 case SecCmsCMCertChain
:
1428 signerinfo
->certList
= CERT_CertChainFromCert(signerinfo
->cert
, usage
, PR_FALSE
);
1430 case SecCmsCMCertChainWithRoot
:
1431 signerinfo
->certList
= CERT_CertChainFromCert(signerinfo
->cert
, usage
, PR_TRUE
);
1435 if (cm
!= SecCmsCMNone
&& signerinfo
->certList
== NULL
)