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"
65 #define HIDIGIT(v) (((v) / 10) + '0')
66 #define LODIGIT(v) (((v) % 10) + '0')
68 #define ISDIGIT(dig) (((dig) >= '0') && ((dig) <= '9'))
69 #define CAPTURE(var,p,label) \
71 if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1])) goto label; \
72 (var) = ((p)[0] - '0') * 10 + ((p)[1] - '0'); \
76 #define SIGINFO_DEBUG 1
80 #define dprintf(args...) fprintf(stderr, args)
82 #define dprintf(args...)
86 #define dprintfRC(args...) dprintf(args)
88 #define dprintfRC(args...)
92 DER_UTCTimeToCFDate(const CSSM_DATA_PTR utcTime
, CFAbsoluteTime
*date
)
94 CFGregorianDate gdate
;
95 char *string
= (char *)utcTime
->Data
;
96 long year
, month
, mday
, hour
, minute
, second
, hourOff
, minOff
;
97 CFTimeZoneRef timeZone
;
99 /* Verify time is formatted properly and capture information */
103 CAPTURE(year
,string
+0,loser
);
105 /* ASSUME that year # is in the 2000's, not the 1900's */
108 CAPTURE(month
,string
+2,loser
);
109 if ((month
== 0) || (month
> 12)) goto loser
;
110 CAPTURE(mday
,string
+4,loser
);
111 if ((mday
== 0) || (mday
> 31)) goto loser
;
112 CAPTURE(hour
,string
+6,loser
);
113 if (hour
> 23) goto loser
;
114 CAPTURE(minute
,string
+8,loser
);
115 if (minute
> 59) goto loser
;
116 if (ISDIGIT(string
[10])) {
117 CAPTURE(second
,string
+10,loser
);
118 if (second
> 59) goto loser
;
121 if (string
[10] == '+') {
122 CAPTURE(hourOff
,string
+11,loser
);
123 if (hourOff
> 23) goto loser
;
124 CAPTURE(minOff
,string
+13,loser
);
125 if (minOff
> 59) goto loser
;
126 } else if (string
[10] == '-') {
127 CAPTURE(hourOff
,string
+11,loser
);
128 if (hourOff
> 23) goto loser
;
130 CAPTURE(minOff
,string
+13,loser
);
131 if (minOff
> 59) goto loser
;
133 } else if (string
[10] != 'Z') {
137 gdate
.year
= (SInt32
)(year
+ 1900);
141 gdate
.minute
= minute
;
142 gdate
.second
= second
;
144 if (hourOff
== 0 && minOff
== 0)
145 timeZone
= NULL
; /* GMT */
148 timeZone
= CFTimeZoneCreateWithTimeIntervalFromGMT(NULL
, (hourOff
* 60 + minOff
) * 60);
151 *date
= CFGregorianDateGetAbsoluteTime(gdate
, timeZone
);
162 DER_CFDateToUTCTime(CFAbsoluteTime date
, CSSM_DATA_PTR utcTime
)
164 CFGregorianDate gdate
= CFAbsoluteTimeGetGregorianDate(date
, NULL
/* GMT */);
168 utcTime
->Length
= 13;
169 utcTime
->Data
= d
= PORT_Alloc(13);
173 /* UTC time does not handle the years before 1950 */
174 if (gdate
.year
< 1950)
177 /* remove the century since it's added to the year by the
178 CFAbsoluteTimeGetGregorianDate routine, but is not needed for UTC time */
180 second
= gdate
.second
+ 0.5;
182 d
[0] = HIDIGIT(gdate
.year
);
183 d
[1] = LODIGIT(gdate
.year
);
184 d
[2] = HIDIGIT(gdate
.month
);
185 d
[3] = LODIGIT(gdate
.month
);
186 d
[4] = HIDIGIT(gdate
.day
);
187 d
[5] = LODIGIT(gdate
.day
);
188 d
[6] = HIDIGIT(gdate
.hour
);
189 d
[7] = LODIGIT(gdate
.hour
);
190 d
[8] = HIDIGIT(gdate
.minute
);
191 d
[9] = LODIGIT(gdate
.minute
);
192 d
[10] = HIDIGIT(second
);
193 d
[11] = LODIGIT(second
);
198 /* =============================================================================
202 nss_cmssignerinfo_create(SecCmsMessageRef cmsg
, SecCmsSignerIDSelector type
, SecCertificateRef cert
, CSSM_DATA_PTR subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
);
205 SecCmsSignerInfoCreateWithSubjKeyID(SecCmsMessageRef cmsg
, CSSM_DATA_PTR subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
)
207 return nss_cmssignerinfo_create(cmsg
, SecCmsSignerIDSubjectKeyID
, NULL
, subjKeyID
, pubKey
, signingKey
, digestalgtag
);
211 SecCmsSignerInfoCreate(SecCmsMessageRef cmsg
, SecIdentityRef identity
, SECOidTag digestalgtag
)
213 SecCmsSignerInfoRef signerInfo
= NULL
;
214 SecCertificateRef cert
= NULL
;
215 SecPrivateKeyRef signingKey
= NULL
;
217 if (SecIdentityCopyCertificate(identity
, &cert
))
219 if (SecIdentityCopyPrivateKey(identity
, &signingKey
))
222 signerInfo
= nss_cmssignerinfo_create(cmsg
, SecCmsSignerIDIssuerSN
, cert
, NULL
, NULL
, signingKey
, digestalgtag
);
228 CFRelease(signingKey
);
234 nss_cmssignerinfo_create(SecCmsMessageRef cmsg
, SecCmsSignerIDSelector type
, SecCertificateRef cert
, CSSM_DATA_PTR subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
)
237 SecCmsSignerInfoRef signerinfo
;
243 mark
= PORT_ArenaMark(poolp
);
245 signerinfo
= (SecCmsSignerInfoRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsSignerInfo
));
246 if (signerinfo
== NULL
) {
247 PORT_ArenaRelease(poolp
, mark
);
252 signerinfo
->cmsg
= cmsg
;
255 case SecCmsSignerIDIssuerSN
:
256 signerinfo
->signerIdentifier
.identifierType
= SecCmsSignerIDIssuerSN
;
257 if ((signerinfo
->cert
= CERT_DupCertificate(cert
)) == NULL
)
259 if ((signerinfo
->signerIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
)) == NULL
)
261 dprintfRC("nss_cmssignerinfo_create: SecCmsSignerIDIssuerSN: cert.rc %d\n",
262 (int)CFGetRetainCount(signerinfo
->cert
));
264 case SecCmsSignerIDSubjectKeyID
:
265 signerinfo
->signerIdentifier
.identifierType
= SecCmsSignerIDSubjectKeyID
;
266 PORT_Assert(subjKeyID
);
269 signerinfo
->signerIdentifier
.id
.subjectKeyID
= PORT_ArenaNew(poolp
, CSSM_DATA
);
270 SECITEM_CopyItem(poolp
, signerinfo
->signerIdentifier
.id
.subjectKeyID
,
272 signerinfo
->pubKey
= SECKEY_CopyPublicKey(pubKey
);
273 if (!signerinfo
->pubKey
)
283 signerinfo
->signingKey
= SECKEY_CopyPrivateKey(signingKey
);
284 if (!signerinfo
->signingKey
)
287 /* set version right now */
288 version
= SEC_CMS_SIGNER_INFO_VERSION_ISSUERSN
;
289 /* RFC2630 5.3 "version is the syntax version number. If the .... " */
290 if (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDSubjectKeyID
)
291 version
= SEC_CMS_SIGNER_INFO_VERSION_SUBJKEY
;
292 (void)SEC_ASN1EncodeInteger(poolp
, &(signerinfo
->version
), (long)version
);
294 if (SECOID_SetAlgorithmID(poolp
, &signerinfo
->digestAlg
, digestalgtag
, NULL
) != SECSuccess
)
297 PORT_ArenaUnmark(poolp
, mark
);
301 PORT_ArenaRelease(poolp
, mark
);
306 * SecCmsSignerInfoDestroy - destroy a SignerInfo data structure
309 SecCmsSignerInfoDestroy(SecCmsSignerInfoRef si
)
311 if (si
->cert
!= NULL
) {
312 dprintfRC("SecCmsSignerInfoDestroy top: certp %p cert.rc %d\n",
313 si
->cert
, (int)CFGetRetainCount(si
->cert
));
314 CERT_DestroyCertificate(si
->cert
);
316 if (si
->certList
!= NULL
) {
317 dprintfRC("SecCmsSignerInfoDestroy top: certList.rc %d\n",
318 (int)CFGetRetainCount(si
->certList
));
319 CFRelease(si
->certList
);
321 if (si
->timestampCertList
!= NULL
) {
322 dprintfRC("SecCmsSignerInfoDestroy top: timestampCertList.rc %d\n",
323 (int)CFGetRetainCount(si
->timestampCertList
));
324 CFRelease(si
->timestampCertList
);
326 /* XXX storage ??? */
330 * SecCmsSignerInfoSign - sign something
334 SecCmsSignerInfoSign(SecCmsSignerInfoRef signerinfo
, CSSM_DATA_PTR digest
, CSSM_DATA_PTR contentType
)
336 SecCertificateRef cert
;
337 SecPrivateKeyRef privkey
= NULL
;
338 SECOidTag digestalgtag
;
339 SECOidTag pubkAlgTag
;
340 CSSM_DATA signature
= { 0 };
342 PLArenaPool
*poolp
, *tmppoolp
= NULL
;
343 const SECAlgorithmID
*algID
;
344 SECAlgorithmID freeAlgID
;
345 //CERTSubjectPublicKeyInfo *spki;
347 PORT_Assert (digest
!= NULL
);
349 poolp
= signerinfo
->cmsg
->poolp
;
351 switch (signerinfo
->signerIdentifier
.identifierType
) {
352 case SecCmsSignerIDIssuerSN
:
353 privkey
= signerinfo
->signingKey
;
354 signerinfo
->signingKey
= NULL
;
355 cert
= signerinfo
->cert
;
356 if (SecCertificateGetAlgorithmID(cert
,&algID
)) {
357 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
361 case SecCmsSignerIDSubjectKeyID
:
362 privkey
= signerinfo
->signingKey
;
363 signerinfo
->signingKey
= NULL
;
365 spki
= SECKEY_CreateSubjectPublicKeyInfo(signerinfo
->pubKey
);
366 SECKEY_DestroyPublicKey(signerinfo
->pubKey
);
367 signerinfo
->pubKey
= NULL
;
368 SECOID_CopyAlgorithmID(NULL
, &freeAlgID
, &spki
->algorithm
);
369 SECKEY_DestroySubjectPublicKeyInfo(spki
);
373 #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR))
374 if (SecKeyGetAlgorithmID(signerinfo
->pubKey
,&algID
)) {
376 /* TBD: Unify this code. Currently, iOS has an incompatible
377 * SecKeyGetAlgorithmID implementation. */
380 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
383 CFRelease(signerinfo
->pubKey
);
384 signerinfo
->pubKey
= NULL
;
388 PORT_SetError(SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE
);
391 digestalgtag
= SecCmsSignerInfoGetDigestAlgTag(signerinfo
);
393 * XXX I think there should be a cert-level interface for this,
394 * so that I do not have to know about subjectPublicKeyInfo...
396 pubkAlgTag
= SECOID_GetAlgorithmTag(algID
);
397 if (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDSubjectKeyID
) {
398 SECOID_DestroyAlgorithmID(&freeAlgID
, PR_FALSE
);
403 /* Fortezza MISSI have weird signature formats.
404 * Map them to standard DSA formats
406 pubkAlgTag
= PK11_FortezzaMapSig(pubkAlgTag
);
409 if (signerinfo
->authAttr
!= NULL
) {
410 CSSM_DATA encoded_attrs
;
412 /* find and fill in the message digest attribute. */
413 rv
= SecCmsAttributeArraySetAttr(poolp
, &(signerinfo
->authAttr
),
414 SEC_OID_PKCS9_MESSAGE_DIGEST
, digest
, PR_FALSE
);
415 if (rv
!= SECSuccess
)
418 if (contentType
!= NULL
) {
419 /* if the caller wants us to, find and fill in the content type attribute. */
420 rv
= SecCmsAttributeArraySetAttr(poolp
, &(signerinfo
->authAttr
),
421 SEC_OID_PKCS9_CONTENT_TYPE
, contentType
, PR_FALSE
);
422 if (rv
!= SECSuccess
)
426 if ((tmppoolp
= PORT_NewArena (1024)) == NULL
) {
427 PORT_SetError(SEC_ERROR_NO_MEMORY
);
432 * Before encoding, reorder the attributes so that when they
433 * are encoded, they will be conforming DER, which is required
434 * to have a specific order and that is what must be used for
435 * the hash/signature. We do this here, rather than building
436 * it into EncodeAttributes, because we do not want to do
437 * such reordering on incoming messages (which also uses
438 * EncodeAttributes) or our old signatures (and other "broken"
439 * implementations) will not verify. So, we want to guarantee
440 * that we send out good DER encodings of attributes, but not
441 * to expect to receive them.
443 if (SecCmsAttributeArrayReorder(signerinfo
->authAttr
) != SECSuccess
)
446 encoded_attrs
.Data
= NULL
;
447 encoded_attrs
.Length
= 0;
448 if (SecCmsAttributeArrayEncode(tmppoolp
, &(signerinfo
->authAttr
),
449 &encoded_attrs
) == NULL
)
452 rv
= SEC_SignData(&signature
, encoded_attrs
.Data
, (int)encoded_attrs
.Length
,
453 privkey
, digestalgtag
, pubkAlgTag
);
454 PORT_FreeArena(tmppoolp
, PR_FALSE
); /* awkward memory management :-( */
457 rv
= SGN_Digest(privkey
, digestalgtag
, pubkAlgTag
, &signature
, digest
);
459 SECKEY_DestroyPrivateKey(privkey
);
462 if (rv
!= SECSuccess
)
465 if (SECITEM_CopyItem(poolp
, &(signerinfo
->encDigest
), &signature
)
469 SECITEM_FreeItem(&signature
, PR_FALSE
);
471 if(pubkAlgTag
== SEC_OID_EC_PUBLIC_KEY
) {
473 * RFC 3278 section section 2.1.1 states that the signatureAlgorithm
474 * field contains the full ecdsa-with-SHA1 OID, not plain old ecPublicKey
475 * as would appear in other forms of signed datas. However Microsoft doesn't
476 * do this, it puts ecPublicKey there, and if we put ecdsa-with-SHA1 there,
477 * MS can't verify - presumably because it takes the digest of the digest
478 * before feeding it to ECDSA.
479 * We handle this with a preference; default if it's not there is
480 * "Microsoft compatibility mode".
482 if(!SecCmsMsEcdsaCompatMode()) {
483 pubkAlgTag
= SEC_OID_ECDSA_WithSHA1
;
485 /* else violating the spec for compatibility */
488 if (SECOID_SetAlgorithmID(poolp
, &(signerinfo
->digestEncAlg
), pubkAlgTag
,
495 if (signature
.Length
!= 0)
496 SECITEM_FreeItem (&signature
, PR_FALSE
);
498 SECKEY_DestroyPrivateKey(privkey
);
499 if((algID
!= NULL
) & (algID
!= &freeAlgID
)) {
500 /* this is dicey - this was actually mallocd by either SecCertificate or
501 * by SecKey...it all boils down to a free() in the end though. */
502 SECOID_DestroyAlgorithmID((SECAlgorithmID
*)algID
, PR_FALSE
);
505 PORT_FreeArena(tmppoolp
, PR_FALSE
);
510 SecCmsSignerInfoVerifyCertificate(SecCmsSignerInfoRef signerinfo
, SecKeychainRef keychainOrArray
,
511 CFTypeRef policies
, SecTrustRef
*trustRef
)
513 SecCertificateRef cert
;
514 CFAbsoluteTime stime
;
516 CSSM_DATA_PTR
*otherCerts
;
518 if ((cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, keychainOrArray
)) == NULL
) {
519 dprintf("SecCmsSignerInfoVerifyCertificate: no signing cert\n");
520 signerinfo
->verificationStatus
= SecCmsVSSigningCertNotFound
;
525 * Get and convert the signing time; if available, it will be used
526 * both on the cert verification and for importing the sender
529 CFTypeRef timeStampPolicies
=SecPolicyCreateAppleTimeStampingAndRevocationPolicies(policies
);
530 if (SecCmsSignerInfoGetTimestampTimeWithPolicy(signerinfo
, timeStampPolicies
, &stime
) != SECSuccess
)
531 if (SecCmsSignerInfoGetSigningTime(signerinfo
, &stime
) != SECSuccess
)
532 stime
= CFAbsoluteTimeGetCurrent();
533 CFReleaseSafe(timeStampPolicies
);
535 rv
= SecCmsSignedDataRawCerts(signerinfo
->sigd
, &otherCerts
);
539 rv
= CERT_VerifyCert(keychainOrArray
, cert
, otherCerts
, policies
, stime
, trustRef
);
540 dprintfRC("SecCmsSignerInfoVerifyCertificate after vfy: certp %p cert.rc %d\n",
541 cert
, (int)CFGetRetainCount(cert
));
544 if (PORT_GetError() == SEC_ERROR_UNTRUSTED_CERT
)
546 /* Signature or digest level verificationStatus errors should supercede certificate level errors, so only change the verificationStatus if the status was GoodSignature. */
547 if (signerinfo
->verificationStatus
== SecCmsVSGoodSignature
)
548 signerinfo
->verificationStatus
= SecCmsVSSigningCertNotTrusted
;
551 /* FIXME isn't this leaking the cert? */
552 dprintf("SecCmsSignerInfoVerifyCertificate: CertVerify rtn %d\n", (int)rv
);
556 static void debugShowSigningCertificate(SecCmsSignerInfoRef signerinfo
)
559 CFStringRef cn
= SecCmsSignerInfoGetSignerCommonName(signerinfo
);
562 char *ccn
= cfStringToChar(cn
);
565 dprintf("SecCmsSignerInfoVerify: cn: %s\n", ccn
);
574 * SecCmsSignerInfoVerify - verify the signature of a single SignerInfo
576 * Just verifies the signature. The assumption is that verification of the certificate
580 SecCmsSignerInfoVerify(SecCmsSignerInfoRef signerinfo
, CSSM_DATA_PTR digest
, CSSM_DATA_PTR contentType
)
582 return SecCmsSignerInfoVerifyWithPolicy(signerinfo
,NULL
, digest
,contentType
);
586 SecCmsSignerInfoVerifyWithPolicy(SecCmsSignerInfoRef signerinfo
,CFTypeRef timeStampPolicy
, CSSM_DATA_PTR digest
, CSSM_DATA_PTR contentType
)
588 SecPublicKeyRef publickey
= NULL
;
589 SecCmsAttribute
*attr
;
590 CSSM_DATA encoded_attrs
;
591 SecCertificateRef cert
;
592 SecCmsVerificationStatus vs
= SecCmsVSUnverified
;
594 SECOidTag digestAlgTag
, digestEncAlgTag
;
596 if (signerinfo
== NULL
)
599 /* SecCmsSignerInfoGetSigningCertificate will fail if 2nd parm is NULL and */
600 /* cert has not been verified */
601 if ((cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, NULL
)) == NULL
) {
602 dprintf("SecCmsSignerInfoVerify: no signing cert\n");
603 vs
= SecCmsVSSigningCertNotFound
;
607 dprintfRC("SecCmsSignerInfoVerify top: cert %p cert.rc %d\n", cert
, (int)CFGetRetainCount(cert
));
609 debugShowSigningCertificate(signerinfo
);
611 if (SecCertificateCopyPublicKey(cert
, &publickey
)) {
612 vs
= SecCmsVSProcessingError
;
616 digestAlgTag
= SECOID_GetAlgorithmTag(&(signerinfo
->digestAlg
));
617 digestEncAlgTag
= SECOID_GetAlgorithmTag(&(signerinfo
->digestEncAlg
));
620 * Gross hack necessitated by RFC 3278 section 2.1.1, which states
621 * that the signature algorithm (here, digestEncAlg) contains ecdsa_with-SHA1,
622 * *not* (as in all other algorithms) the raw signature algorithm, e.g.
623 * pkcs1RSAEncryption.
625 if(digestEncAlgTag
== SEC_OID_ECDSA_WithSHA1
) {
626 digestEncAlgTag
= SEC_OID_EC_PUBLIC_KEY
;
629 if (!SecCmsArrayIsEmpty((void **)signerinfo
->authAttr
)) {
634 * RFC2630 sez that if there are any authenticated attributes,
635 * then there must be one for content type which matches the
636 * content type of the content being signed, and there must
637 * be one for message digest which matches our message digest.
638 * So check these things first.
640 if ((attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
641 SEC_OID_PKCS9_CONTENT_TYPE
, PR_TRUE
)) == NULL
)
643 vs
= SecCmsVSMalformedSignature
;
647 if (SecCmsAttributeCompareValue(attr
, contentType
) == PR_FALSE
) {
648 vs
= SecCmsVSMalformedSignature
;
656 if ((attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
, SEC_OID_PKCS9_MESSAGE_DIGEST
, PR_TRUE
)) == NULL
)
658 vs
= SecCmsVSMalformedSignature
;
661 if (SecCmsAttributeCompareValue(attr
, digest
) == PR_FALSE
) {
662 vs
= SecCmsVSDigestMismatch
;
666 if ((poolp
= PORT_NewArena (1024)) == NULL
) {
667 vs
= SecCmsVSProcessingError
;
674 * The signature is based on a digest of the DER-encoded authenticated
675 * attributes. So, first we encode and then we digest/verify.
676 * we trust the decoder to have the attributes in the right (sorted) order
678 encoded_attrs
.Data
= NULL
;
679 encoded_attrs
.Length
= 0;
681 if (SecCmsAttributeArrayEncode(poolp
, &(signerinfo
->authAttr
), &encoded_attrs
) == NULL
||
682 encoded_attrs
.Data
== NULL
|| encoded_attrs
.Length
== 0)
684 vs
= SecCmsVSProcessingError
;
688 SECStatus err
= SECSuccess
;
689 vs
= ((err
= VFY_VerifyData (encoded_attrs
.Data
, (int)encoded_attrs
.Length
,
690 publickey
, &(signerinfo
->encDigest
),
691 digestAlgTag
, digestEncAlgTag
,
692 signerinfo
->cmsg
->pwfn_arg
)) != SECSuccess
) ? SecCmsVSBadSignature
: SecCmsVSGoodSignature
;
694 dprintf("VFY_VerifyData (authenticated attributes): %s\n",
695 (vs
== SecCmsVSGoodSignature
)?"SecCmsVSGoodSignature":"SecCmsVSBadSignature");
696 if (vs
!= SecCmsVSGoodSignature
) syslog(LOG_ERR
, "VFY_VerifyData (authenticated attributes) failed: %d", err
);
698 PORT_FreeArena(poolp
, PR_FALSE
); /* awkward memory management :-( */
703 /* No authenticated attributes. The signature is based on the plain message digest. */
704 sig
= &(signerinfo
->encDigest
);
705 if (sig
->Length
== 0)
708 SECStatus err
= SECSuccess
;
709 vs
= ((err
= VFY_VerifyDigest(digest
, publickey
, sig
,
710 digestAlgTag
, digestEncAlgTag
,
711 signerinfo
->cmsg
->pwfn_arg
)) != SECSuccess
) ? SecCmsVSBadSignature
: SecCmsVSGoodSignature
;
713 dprintf("VFY_VerifyData (plain message digest): %s\n",
714 (vs
== SecCmsVSGoodSignature
)?"SecCmsVSGoodSignature":"SecCmsVSBadSignature");
715 if (vs
!= SecCmsVSGoodSignature
) syslog(LOG_ERR
, "VFY_VerifyDigest (plain message digest) failed: %d", err
);
718 if (!SecCmsArrayIsEmpty((void **)signerinfo
->unAuthAttr
))
720 dprintf("found an unAuthAttr\n");
721 OSStatus rux
= SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy(signerinfo
,timeStampPolicy
);
722 dprintf("SecCmsSignerInfoVerifyUnAuthAttrs Status: %ld\n", (long)rux
);
724 syslog(LOG_ERR
, "SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy failed: %d", (int)rux
);
729 if (vs
== SecCmsVSBadSignature
) {
731 * XXX Change the generic error into our specific one, because
732 * in that case we get a better explanation out of the Security
733 * Advisor. This is really a bug in our error strings (the
734 * "generic" error has a lousy/wrong message associated with it
735 * which assumes the signature verification was done for the
736 * purposes of checking the issuer signature on a certificate)
737 * but this is at least an easy workaround and/or in the
738 * Security Advisor, which specifically checks for the error
739 * SEC_ERROR_PKCS7_BAD_SIGNATURE and gives more explanation
740 * in that case but does not similarly check for
741 * SEC_ERROR_BAD_SIGNATURE. It probably should, but then would
742 * probably say the wrong thing in the case that it *was* the
743 * certificate signature check that failed during the cert
744 * verification done above. Our error handling is really a mess.
746 syslog(LOG_ERR
, "SecCmsSignerInforVerify bad signature PORT_GetError: %d", PORT_GetError());
747 if (PORT_GetError() == SEC_ERROR_BAD_SIGNATURE
)
748 PORT_SetError(SEC_ERROR_PKCS7_BAD_SIGNATURE
);
751 if (publickey
!= NULL
)
752 CFRelease(publickey
);
754 signerinfo
->verificationStatus
= vs
;
755 dprintfRC("SecCmsSignerInfoVerify end: cerp %p cert.rc %d\n",
756 cert
, (int)CFGetRetainCount(cert
));
758 dprintf("verificationStatus: %d\n", vs
);
760 return (vs
== SecCmsVSGoodSignature
) ? SECSuccess
: SECFailure
;
763 if (publickey
!= NULL
)
764 SECKEY_DestroyPublicKey (publickey
);
766 dprintf("verificationStatus2: %d\n", vs
);
767 signerinfo
->verificationStatus
= vs
;
769 PORT_SetError (SEC_ERROR_PKCS7_BAD_SIGNATURE
);
774 SecCmsSignerInfoVerifyUnAuthAttrs(SecCmsSignerInfoRef signerinfo
) {
775 return SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy(signerinfo
, NULL
);
779 SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy(SecCmsSignerInfoRef signerinfo
,CFTypeRef timeStampPolicy
)
782 unAuthAttr is an array of attributes; we expect to
783 see just one: the timestamp blob. If we have an unAuthAttr,
784 but don't see a timestamp, return an error since we have
785 no other cases where this would be present.
788 SecCmsAttribute
*attr
= NULL
;
789 OSStatus status
= SECFailure
;
791 require(signerinfo
, xit
);
792 attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->unAuthAttr
,
793 SEC_OID_PKCS9_TIMESTAMP_TOKEN
, PR_TRUE
);
796 status
= errSecTimestampMissing
;
800 dprintf("found an id-ct-TSTInfo\n");
801 // Don't check the nonce in this case
802 status
= decodeTimeStampTokenWithPolicy(signerinfo
, timeStampPolicy
, (attr
->values
)[0], &signerinfo
->encDigest
, 0);
808 SecCmsSignerInfoGetEncDigest(SecCmsSignerInfoRef signerinfo
)
810 return &signerinfo
->encDigest
;
813 SecCmsVerificationStatus
814 SecCmsSignerInfoGetVerificationStatus(SecCmsSignerInfoRef signerinfo
)
816 return signerinfo
->verificationStatus
;
820 SecCmsSignerInfoGetDigestAlg(SecCmsSignerInfoRef signerinfo
)
822 return SECOID_FindOID (&(signerinfo
->digestAlg
.algorithm
));
826 SecCmsSignerInfoGetDigestAlgTag(SecCmsSignerInfoRef signerinfo
)
830 algdata
= SECOID_FindOID (&(signerinfo
->digestAlg
.algorithm
));
832 return algdata
->offset
;
834 return SEC_OID_UNKNOWN
;
838 SecCmsSignerInfoGetCertList(SecCmsSignerInfoRef signerinfo
)
840 dprintfRC("SecCmsSignerInfoGetCertList: certList.rc %d\n",
841 (int)CFGetRetainCount(signerinfo
->certList
));
842 return signerinfo
->certList
;
846 SecCmsSignerInfoGetTimestampCertList(SecCmsSignerInfoRef signerinfo
)
848 dprintfRC("SecCmsSignerInfoGetCertList: timestampCertList.rc %d\n",
849 (int)CFGetRetainCount(signerinfo
->timestampCertList
));
850 return signerinfo
->timestampCertList
;
856 SecCmsSignerInfoGetVersion(SecCmsSignerInfoRef signerinfo
)
858 unsigned long version
;
860 /* always take apart the CSSM_DATA */
861 if (SEC_ASN1DecodeInteger(&(signerinfo
->version
), &version
) != SECSuccess
)
868 * SecCmsSignerInfoGetSigningTime - return the signing time,
869 * in UTCTime format, of a CMS signerInfo.
871 * sinfo - signerInfo data for this signer
873 * Returns a pointer to XXXX (what?)
874 * A return value of NULL is an error.
877 SecCmsSignerInfoGetSigningTime(SecCmsSignerInfoRef sinfo
, CFAbsoluteTime
*stime
)
879 SecCmsAttribute
*attr
;
885 if (sinfo
->signingTime
!= 0) {
886 *stime
= sinfo
->signingTime
; /* cached copy */
890 attr
= SecCmsAttributeArrayFindAttrByOidTag(sinfo
->authAttr
, SEC_OID_PKCS9_SIGNING_TIME
, PR_TRUE
);
891 /* XXXX multi-valued attributes NIH */
892 if (attr
== NULL
|| (value
= SecCmsAttributeGetValue(attr
)) == NULL
)
893 return errSecSigningTimeMissing
;
894 if (DER_UTCTimeToCFDate(value
, stime
) != SECSuccess
)
895 return errSecSigningTimeMissing
;
896 sinfo
->signingTime
= *stime
; /* make cached copy */
901 SecCmsSignerInfoGetTimestampTime(SecCmsSignerInfoRef sinfo
, CFAbsoluteTime
*stime
)
903 return SecCmsSignerInfoGetTimestampTimeWithPolicy(sinfo
, NULL
, stime
);
907 SecCmsSignerInfoGetTimestampTimeWithPolicy(SecCmsSignerInfoRef sinfo
, CFTypeRef timeStampPolicy
, CFAbsoluteTime
*stime
)
909 OSStatus status
= paramErr
;
911 require(sinfo
&& stime
, xit
);
913 if (sinfo
->timestampTime
!= 0)
915 *stime
= sinfo
->timestampTime
; /* cached copy */
919 // A bit heavyweight if haven't already called verify
920 status
= SecCmsSignerInfoVerifyUnAuthAttrsWithPolicy(sinfo
,timeStampPolicy
);
921 *stime
= sinfo
->timestampTime
;
927 * Return the signing cert of a CMS signerInfo.
929 * the certs in the enclosing SignedData must have been imported already
932 SecCmsSignerInfoGetSigningCertificate(SecCmsSignerInfoRef signerinfo
, SecKeychainRef keychainOrArray
)
934 SecCertificateRef cert
;
935 SecCmsSignerIdentifier
*sid
;
937 CSSM_DATA_PTR
*rawCerts
;
939 if (signerinfo
->cert
!= NULL
) {
940 dprintfRC("SecCmsSignerInfoGetSigningCertificate top: cert %p cert.rc %d\n",
941 signerinfo
->cert
, (int)CFGetRetainCount(signerinfo
->cert
));
942 return signerinfo
->cert
;
944 ortn
= SecCmsSignedDataRawCerts(signerinfo
->sigd
, &rawCerts
);
948 dprintf("SecCmsSignerInfoGetSigningCertificate: numRawCerts %d\n",
949 SecCmsArrayCount((void **)rawCerts
));
952 * This cert will also need to be freed, but since we save it
953 * in signerinfo for later, we do not want to destroy it when
954 * we leave this function -- we let the clean-up of the entire
955 * cinfo structure later do the destroy of this cert.
957 sid
= &signerinfo
->signerIdentifier
;
958 switch (sid
->identifierType
) {
959 case SecCmsSignerIDIssuerSN
:
960 cert
= CERT_FindCertByIssuerAndSN(keychainOrArray
, rawCerts
, signerinfo
->cmsg
->poolp
,
961 sid
->id
.issuerAndSN
);
963 case SecCmsSignerIDSubjectKeyID
:
964 cert
= CERT_FindCertBySubjectKeyID(keychainOrArray
, rawCerts
, sid
->id
.subjectKeyID
);
971 /* cert can be NULL at that point */
972 signerinfo
->cert
= cert
; /* earmark it */
973 dprintfRC("SecCmsSignerInfoGetSigningCertificate end: certp %p cert.rc %d\n",
974 signerinfo
->cert
, (int)CFGetRetainCount(signerinfo
->cert
));
980 * SecCmsSignerInfoGetSignerCommonName - return the common name of the signer
982 * sinfo - signerInfo data for this signer
984 * Returns a CFStringRef containing the common name of the signer.
985 * A return value of NULL is an error.
988 SecCmsSignerInfoGetSignerCommonName(SecCmsSignerInfoRef sinfo
)
990 SecCertificateRef signercert
;
991 CFStringRef commonName
= NULL
;
993 /* will fail if cert is not verified */
994 if ((signercert
= SecCmsSignerInfoGetSigningCertificate(sinfo
, NULL
)) == NULL
)
997 SecCertificateCopyCommonName(signercert
, &commonName
);
1003 * SecCmsSignerInfoGetSignerEmailAddress - return the email address of the signer
1005 * sinfo - signerInfo data for this signer
1007 * Returns a CFStringRef containing the name of the signer.
1008 * A return value of NULL is an error.
1011 SecCmsSignerInfoGetSignerEmailAddress(SecCmsSignerInfoRef sinfo
)
1013 SecCertificateRef signercert
;
1014 CFStringRef emailAddress
= NULL
;
1016 if ((signercert
= SecCmsSignerInfoGetSigningCertificate(sinfo
, NULL
)) == NULL
)
1019 SecCertificateGetEmailAddress(signercert
, &emailAddress
);
1021 return emailAddress
;
1026 * SecCmsSignerInfoAddAuthAttr - add an attribute to the
1027 * authenticated (i.e. signed) attributes of "signerinfo".
1030 SecCmsSignerInfoAddAuthAttr(SecCmsSignerInfoRef signerinfo
, SecCmsAttribute
*attr
)
1032 return SecCmsAttributeArrayAddAttr(signerinfo
->cmsg
->poolp
, &(signerinfo
->authAttr
), attr
);
1036 * SecCmsSignerInfoAddUnauthAttr - add an attribute to the
1037 * unauthenticated attributes of "signerinfo".
1040 SecCmsSignerInfoAddUnauthAttr(SecCmsSignerInfoRef signerinfo
, SecCmsAttribute
*attr
)
1042 return SecCmsAttributeArrayAddAttr(signerinfo
->cmsg
->poolp
, &(signerinfo
->unAuthAttr
), attr
);
1046 * SecCmsSignerInfoAddSigningTime - add the signing time to the
1047 * authenticated (i.e. signed) attributes of "signerinfo".
1049 * This is expected to be included in outgoing signed
1050 * messages for email (S/MIME) but is likely useful in other situations.
1052 * This should only be added once; a second call will do nothing.
1054 * XXX This will probably just shove the current time into "signerinfo"
1055 * but it will not actually get signed until the entire item is
1056 * processed for encoding. Is this (expected to be small) delay okay?
1059 SecCmsSignerInfoAddSigningTime(SecCmsSignerInfoRef signerinfo
, CFAbsoluteTime t
)
1061 SecCmsAttribute
*attr
;
1066 poolp
= signerinfo
->cmsg
->poolp
;
1068 mark
= PORT_ArenaMark(poolp
);
1070 /* create new signing time attribute */
1071 if (DER_CFDateToUTCTime(t
, &stime
) != SECSuccess
)
1074 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_SIGNING_TIME
, &stime
, PR_FALSE
)) == NULL
) {
1075 SECITEM_FreeItem (&stime
, PR_FALSE
);
1079 SECITEM_FreeItem (&stime
, PR_FALSE
);
1081 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1084 PORT_ArenaUnmark (poolp
, mark
);
1089 PORT_ArenaRelease (poolp
, mark
);
1094 * SecCmsSignerInfoAddSMIMECaps - add a SMIMECapabilities attribute to the
1095 * authenticated (i.e. signed) attributes of "signerinfo".
1097 * This is expected to be included in outgoing signed
1098 * messages for email (S/MIME).
1101 SecCmsSignerInfoAddSMIMECaps(SecCmsSignerInfoRef signerinfo
)
1103 SecCmsAttribute
*attr
;
1104 CSSM_DATA_PTR smimecaps
= NULL
;
1108 poolp
= signerinfo
->cmsg
->poolp
;
1110 mark
= PORT_ArenaMark(poolp
);
1112 smimecaps
= SECITEM_AllocItem(poolp
, NULL
, 0);
1113 if (smimecaps
== NULL
)
1116 /* create new signing time attribute */
1118 // @@@ We don't do Fortezza yet.
1119 if (SecSMIMECreateSMIMECapabilities((SecArenaPoolRef
)poolp
, smimecaps
, PR_FALSE
) != SECSuccess
)
1121 if (SecSMIMECreateSMIMECapabilities(poolp
, smimecaps
,
1122 PK11_FortezzaHasKEA(signerinfo
->cert
)) != SECSuccess
)
1126 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_SMIME_CAPABILITIES
, smimecaps
, PR_TRUE
)) == NULL
)
1129 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1132 PORT_ArenaUnmark (poolp
, mark
);
1136 PORT_ArenaRelease (poolp
, mark
);
1141 * SecCmsSignerInfoAddSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the
1142 * authenticated (i.e. signed) attributes of "signerinfo".
1144 * This is expected to be included in outgoing signed messages for email (S/MIME).
1147 SecCmsSignerInfoAddSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo
, SecCertificateRef cert
, SecKeychainRef keychainOrArray
)
1149 SecCmsAttribute
*attr
;
1150 CSSM_DATA_PTR smimeekp
= NULL
;
1157 /* verify this cert for encryption */
1158 policy
= CERT_PolicyForCertUsage(certUsageEmailRecipient
);
1159 if (CERT_VerifyCert(keychainOrArray
, cert
, policy
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1166 poolp
= signerinfo
->cmsg
->poolp
;
1167 mark
= PORT_ArenaMark(poolp
);
1169 smimeekp
= SECITEM_AllocItem(poolp
, NULL
, 0);
1170 if (smimeekp
== NULL
)
1173 /* create new signing time attribute */
1174 if (SecSMIMECreateSMIMEEncKeyPrefs((SecArenaPoolRef
)poolp
, smimeekp
, cert
) != SECSuccess
)
1177 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE
, smimeekp
, PR_TRUE
)) == NULL
)
1180 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1183 PORT_ArenaUnmark (poolp
, mark
);
1187 PORT_ArenaRelease (poolp
, mark
);
1192 * SecCmsSignerInfoAddMSSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the
1193 * authenticated (i.e. signed) attributes of "signerinfo", using the OID prefered by Microsoft.
1195 * This is expected to be included in outgoing signed messages for email (S/MIME),
1196 * if compatibility with Microsoft mail clients is wanted.
1199 SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo
, SecCertificateRef cert
, SecKeychainRef keychainOrArray
)
1201 SecCmsAttribute
*attr
;
1202 CSSM_DATA_PTR smimeekp
= NULL
;
1209 /* verify this cert for encryption */
1210 policy
= CERT_PolicyForCertUsage(certUsageEmailRecipient
);
1211 if (CERT_VerifyCert(keychainOrArray
, cert
, policy
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1218 poolp
= signerinfo
->cmsg
->poolp
;
1219 mark
= PORT_ArenaMark(poolp
);
1221 smimeekp
= SECITEM_AllocItem(poolp
, NULL
, 0);
1222 if (smimeekp
== NULL
)
1225 /* create new signing time attribute */
1226 if (SecSMIMECreateMSSMIMEEncKeyPrefs((SecArenaPoolRef
)poolp
, smimeekp
, cert
) != SECSuccess
)
1229 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_MS_SMIME_ENCRYPTION_KEY_PREFERENCE
, smimeekp
, PR_TRUE
)) == NULL
)
1232 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1235 PORT_ArenaUnmark (poolp
, mark
);
1239 PORT_ArenaRelease (poolp
, mark
);
1244 * SecCmsSignerInfoAddTimeStamp - add time stamp to the
1245 * unauthenticated (i.e. unsigned) attributes of "signerinfo".
1247 * This will initially be used for time stamping signed applications
1248 * by using a Time Stamping Authority. It may also be included in outgoing signed
1249 * messages for email (S/MIME), and may be useful in other situations.
1251 * This should only be added once; a second call will do nothing.
1256 Countersignature attribute values have ASN.1 type Countersignature:
1257 Countersignature ::= SignerInfo
1258 Countersignature values have the same meaning as SignerInfo values
1259 for ordinary signatures, except that:
1260 1. The signedAttributes field MUST NOT contain a content-type
1261 attribute; there is no content type for countersignatures.
1262 2. The signedAttributes field MUST contain a message-digest
1263 attribute if it contains any other attributes.
1264 3. The input to the message-digesting process is the contents octets
1265 of the DER encoding of the signatureValue field of the SignerInfo
1266 value with which the attribute is associated.
1271 @abstract Create a timestamp unsigned attribute with a TimeStampToken.
1275 SecCmsSignerInfoAddTimeStamp(SecCmsSignerInfoRef signerinfo
, CSSM_DATA
*tstoken
)
1277 SecCmsAttribute
*attr
;
1278 PLArenaPool
*poolp
= signerinfo
->cmsg
->poolp
;
1279 void *mark
= PORT_ArenaMark(poolp
);
1281 // We have already encoded this ourselves, so last param is PR_TRUE
1282 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_TIMESTAMP_TOKEN
, tstoken
, PR_TRUE
)) == NULL
)
1285 if (SecCmsSignerInfoAddUnauthAttr(signerinfo
, attr
) != SECSuccess
)
1288 PORT_ArenaUnmark (poolp
, mark
);
1293 PORT_ArenaRelease (poolp
, mark
);
1298 * SecCmsSignerInfoAddCounterSignature - countersign a signerinfo
1300 * 1. digest the DER-encoded signature value of the original signerinfo
1301 * 2. create new signerinfo with correct version, sid, digestAlg
1302 * 3. add message-digest authAttr, but NO content-type
1303 * 4. sign the authAttrs
1304 * 5. DER-encode the new signerInfo
1305 * 6. add the whole thing to original signerInfo's unAuthAttrs
1306 * as a SEC_OID_PKCS9_COUNTER_SIGNATURE attribute
1308 * XXXX give back the new signerinfo?
1311 SecCmsSignerInfoAddCounterSignature(SecCmsSignerInfoRef signerinfo
,
1312 SECOidTag digestalg
, SecIdentityRef identity
)
1319 * XXXX the following needs to be done in the S/MIME layer code
1320 * after signature of a signerinfo is verified
1323 SecCmsSignerInfoSaveSMIMEProfile(SecCmsSignerInfoRef signerinfo
)
1325 SecCertificateRef cert
= NULL
;
1326 CSSM_DATA_PTR profile
= NULL
;
1327 SecCmsAttribute
*attr
;
1328 CSSM_DATA_PTR utc_stime
= NULL
;
1332 Boolean must_free_cert
= PR_FALSE
;
1334 SecKeychainRef keychainOrArray
;
1336 status
= SecKeychainCopyDefault(&keychainOrArray
);
1338 /* sanity check - see if verification status is ok (unverified does not count...) */
1339 if (signerinfo
->verificationStatus
!= SecCmsVSGoodSignature
)
1342 /* find preferred encryption cert */
1343 if (!SecCmsArrayIsEmpty((void **)signerinfo
->authAttr
) &&
1344 (attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
1345 SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE
, PR_TRUE
)) != NULL
)
1346 { /* we have a SMIME_ENCRYPTION_KEY_PREFERENCE attribute! */
1347 ekp
= SecCmsAttributeGetValue(attr
);
1351 /* we assume that all certs coming with the message have been imported to the */
1352 /* temporary database */
1353 cert
= SecSMIMEGetCertFromEncryptionKeyPreference(keychainOrArray
, ekp
);
1356 must_free_cert
= PR_TRUE
;
1360 /* no preferred cert found?
1361 * find the cert the signerinfo is signed with instead */
1362 CFStringRef emailAddress
=NULL
;
1364 cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, keychainOrArray
);
1367 if (SecCertificateGetEmailAddress(cert
,&emailAddress
))
1371 /* 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.
1372 * that's OK, we can still save the S/MIME profile. The encryption cert
1373 * should have already been saved */
1375 if (CERT_VerifyCert(keychainOrArray
, cert
, certUsageEmailRecipient
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1377 CERT_DestroyCertificate(cert
);
1382 /* XXX store encryption cert permanently? */
1385 * Remember the current error set because we do not care about
1386 * anything set by the functions we are about to call.
1388 save_error
= PORT_GetError();
1390 if (!SecCmsArrayIsEmpty((void **)signerinfo
->authAttr
)) {
1391 attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
1392 SEC_OID_PKCS9_SMIME_CAPABILITIES
,
1394 profile
= SecCmsAttributeGetValue(attr
);
1395 attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
1396 SEC_OID_PKCS9_SIGNING_TIME
,
1398 utc_stime
= SecCmsAttributeGetValue(attr
);
1401 rv
= CERT_SaveSMimeProfile (cert
, profile
, utc_stime
);
1403 CERT_DestroyCertificate(cert
);
1406 * Restore the saved error in case the calls above set a new
1407 * one that we do not actually care about.
1409 PORT_SetError (save_error
);
1415 * SecCmsSignerInfoIncludeCerts - set cert chain inclusion mode for this signer
1418 SecCmsSignerInfoIncludeCerts(SecCmsSignerInfoRef signerinfo
, SecCmsCertChainMode cm
, SECCertUsage usage
)
1420 if (signerinfo
->cert
== NULL
)
1423 /* don't leak if we get called twice */
1424 if (signerinfo
->certList
!= NULL
) {
1425 CFRelease(signerinfo
->certList
);
1426 signerinfo
->certList
= NULL
;
1431 signerinfo
->certList
= NULL
;
1433 case SecCmsCMCertOnly
:
1434 signerinfo
->certList
= CERT_CertListFromCert(signerinfo
->cert
);
1436 case SecCmsCMCertChain
:
1437 signerinfo
->certList
= CERT_CertChainFromCert(signerinfo
->cert
, usage
, PR_FALSE
);
1439 case SecCmsCMCertChainWithRoot
:
1440 signerinfo
->certList
= CERT_CertChainFromCert(signerinfo
->cert
, usage
, PR_TRUE
);
1444 if (cm
!= SecCmsCMNone
&& signerinfo
->certList
== NULL
)