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"
44 #include "SecAsn1Item.h"
48 #include <security_asn1/secasn1.h>
49 #include <security_asn1/secerr.h>
50 #include <security_asn1/secport.h>
53 #include <Security/SecKeychain.h>
56 #include <Security/SecIdentity.h>
57 #include <Security/SecCertificateInternal.h>
58 #include <Security/SecInternal.h>
59 #include <Security/SecKeyPriv.h>
60 #include <utilities/SecCFWrappers.h>
61 #include <CoreFoundation/CFTimeZone.h>
64 #define HIDIGIT(v) (((v) / 10) + '0')
65 #define LODIGIT(v) (((v) % 10) + '0')
67 #define ISDIGIT(dig) (((dig) >= '0') && ((dig) <= '9'))
68 #define CAPTURE(var,p,label) \
70 if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1])) goto label; \
71 (var) = ((p)[0] - '0') * 10 + ((p)[1] - '0'); \
76 DER_UTCTimeToCFDate(const SecAsn1Item
* utcTime
, CFAbsoluteTime
*date
)
78 char *string
= (char *)utcTime
->Data
;
79 int year
, month
, mday
, hour
, minute
, second
, hourOff
, minOff
;
81 /* Verify time is formatted properly and capture information */
85 CAPTURE(year
,string
+0,loser
);
87 /* ASSUME that year # is in the 2000's, not the 1900's */
92 CAPTURE(month
,string
+2,loser
);
93 if ((month
== 0) || (month
> 12)) goto loser
;
94 CAPTURE(mday
,string
+4,loser
);
95 if ((mday
== 0) || (mday
> 31)) goto loser
;
96 CAPTURE(hour
,string
+6,loser
);
97 if (hour
> 23) goto loser
;
98 CAPTURE(minute
,string
+8,loser
);
99 if (minute
> 59) goto loser
;
100 if (ISDIGIT(string
[10])) {
101 CAPTURE(second
,string
+10,loser
);
102 if (second
> 59) goto loser
;
105 if (string
[10] == '+') {
106 CAPTURE(hourOff
,string
+11,loser
);
107 if (hourOff
> 23) goto loser
;
108 CAPTURE(minOff
,string
+13,loser
);
109 if (minOff
> 59) goto loser
;
110 } else if (string
[10] == '-') {
111 CAPTURE(hourOff
,string
+11,loser
);
112 if (hourOff
> 23) goto loser
;
114 CAPTURE(minOff
,string
+13,loser
);
115 if (minOff
> 59) goto loser
;
117 } else if (string
[10] != 'Z') {
121 if (hourOff
== 0 && minOff
== 0) {
122 *date
= CFAbsoluteTimeForGregorianZuluMoment(year
, month
, mday
, hour
, minute
, second
);
124 CFTimeZoneRef tz
= CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault
, (hourOff
* 60 + minOff
) * 60);
125 *date
= CFAbsoluteTimeForGregorianMoment(tz
, year
, month
, mday
, hour
, minute
, second
);
136 DER_CFDateToUTCTime(CFAbsoluteTime date
, SecAsn1Item
* utcTime
)
140 utcTime
->Length
= 13;
141 utcTime
->Data
= d
= PORT_Alloc(13);
145 __block
int year
= 0, month
= 0, day
= 0, hour
= 0, minute
= 0, second
= 0;
147 SecCFCalendarDoWithZuluCalendar(^(CFCalendarRef zuluCalendar
) {
148 result
= CFCalendarDecomposeAbsoluteTime(zuluCalendar
, date
, "yMdHms", &year
, &month
, &day
, &hour
, &minute
, &second
);
153 /* UTC time does not handle the years before 1950 */
157 /* remove the century since it's added to the year by the
158 CFAbsoluteTimeGetGregorianDate routine, but is not needed for UTC time */
161 d
[0] = HIDIGIT(year
);
162 d
[1] = LODIGIT(year
);
163 d
[2] = HIDIGIT(month
);
164 d
[3] = LODIGIT(month
);
167 d
[6] = HIDIGIT(hour
);
168 d
[7] = LODIGIT(hour
);
169 d
[8] = HIDIGIT(minute
);
170 d
[9] = LODIGIT(minute
);
171 d
[10] = HIDIGIT(second
);
172 d
[11] = LODIGIT(second
);
177 /* =============================================================================
181 nss_cmssignerinfo_create(SecCmsSignedDataRef sigd
, SecCmsSignerIDSelector type
, SecCertificateRef cert
, const SecAsn1Item
*subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
);
184 SecCmsSignerInfoCreateWithSubjKeyID(SecCmsSignedDataRef sigd
, const SecAsn1Item
*subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
)
186 return nss_cmssignerinfo_create(sigd
, SecCmsSignerIDSubjectKeyID
, NULL
, subjKeyID
, pubKey
, signingKey
, digestalgtag
);
190 SecCmsSignerInfoCreate(SecCmsSignedDataRef sigd
, SecIdentityRef identity
, SECOidTag digestalgtag
)
192 SecCmsSignerInfoRef signerInfo
= NULL
;
193 SecCertificateRef cert
= NULL
;
194 SecPrivateKeyRef signingKey
= NULL
;
196 if (SecIdentityCopyCertificate(identity
, &cert
))
198 if (SecIdentityCopyPrivateKey(identity
, &signingKey
))
201 signerInfo
= nss_cmssignerinfo_create(sigd
, SecCmsSignerIDIssuerSN
, cert
, NULL
, NULL
, signingKey
, digestalgtag
);
207 CFRelease(signingKey
);
213 nss_cmssignerinfo_create(SecCmsSignedDataRef sigd
, SecCmsSignerIDSelector type
, SecCertificateRef cert
, const SecAsn1Item
*subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
)
216 SecCmsSignerInfoRef signerinfo
;
220 poolp
= sigd
->contentInfo
.cmsg
->poolp
;
222 mark
= PORT_ArenaMark(poolp
);
224 signerinfo
= (SecCmsSignerInfoRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsSignerInfo
));
225 if (signerinfo
== NULL
) {
226 PORT_ArenaRelease(poolp
, mark
);
231 signerinfo
->signedData
= sigd
;
234 case SecCmsSignerIDIssuerSN
:
235 signerinfo
->signerIdentifier
.identifierType
= SecCmsSignerIDIssuerSN
;
236 if ((signerinfo
->cert
= CERT_DupCertificate(cert
)) == NULL
)
238 if ((signerinfo
->signerIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
)) == NULL
)
241 case SecCmsSignerIDSubjectKeyID
:
242 signerinfo
->signerIdentifier
.identifierType
= SecCmsSignerIDSubjectKeyID
;
243 PORT_Assert(subjKeyID
);
246 signerinfo
->signerIdentifier
.id
.subjectKeyID
= PORT_ArenaNew(poolp
, SecAsn1Item
);
247 SECITEM_CopyItem(poolp
, signerinfo
->signerIdentifier
.id
.subjectKeyID
,
249 signerinfo
->pubKey
= SECKEY_CopyPublicKey(pubKey
);
250 if (!signerinfo
->pubKey
)
260 signerinfo
->signingKey
= SECKEY_CopyPrivateKey(signingKey
);
261 if (!signerinfo
->signingKey
)
264 /* set version right now */
265 version
= SEC_CMS_SIGNER_INFO_VERSION_ISSUERSN
;
266 /* RFC2630 5.3 "version is the syntax version number. If the .... " */
267 if (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDSubjectKeyID
)
268 version
= SEC_CMS_SIGNER_INFO_VERSION_SUBJKEY
;
269 (void)SEC_ASN1EncodeInteger(poolp
, &(signerinfo
->version
), (long)version
);
271 if (SECOID_SetAlgorithmID(poolp
, &signerinfo
->digestAlg
, digestalgtag
, NULL
) != SECSuccess
)
274 if (SecCmsSignedDataAddSignerInfo(sigd
, signerinfo
))
277 PORT_ArenaUnmark(poolp
, mark
);
281 PORT_ArenaRelease(poolp
, mark
);
286 * SecCmsSignerInfoDestroy - destroy a SignerInfo data structure
289 SecCmsSignerInfoDestroy(SecCmsSignerInfoRef si
)
291 if (si
->cert
!= NULL
)
292 CERT_DestroyCertificate(si
->cert
);
294 if (si
->certList
!= NULL
)
295 CFRelease(si
->certList
);
297 /* XXX storage ??? */
300 static SecAsn1AlgId
SecCertificateGetPublicKeyAlgorithmID(SecCertificateRef cert
)
302 const DERAlgorithmId
*length_data_swapped
= SecCertificateGetPublicKeyAlgorithm(cert
);
303 SecAsn1AlgId temp
= {
304 { length_data_swapped
->oid
.length
, length_data_swapped
->oid
.data
},
305 { length_data_swapped
->params
.length
, length_data_swapped
->params
.data
} };
311 * SecCmsSignerInfoSign - sign something
315 SecCmsSignerInfoSign(SecCmsSignerInfoRef signerinfo
, SecAsn1Item
* digest
, SecAsn1Item
* contentType
)
317 SecCertificateRef cert
;
318 SecPrivateKeyRef privkey
= NULL
;
319 SECOidTag digestalgtag
;
320 SECOidTag pubkAlgTag
;
321 SecAsn1Item signature
= { 0 };
323 PLArenaPool
*poolp
, *tmppoolp
= NULL
;
324 const SECAlgorithmID
*algID
= NULL
;
325 //CERTSubjectPublicKeyInfo *spki;
327 PORT_Assert (digest
!= NULL
);
329 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
331 switch (signerinfo
->signerIdentifier
.identifierType
) {
332 case SecCmsSignerIDIssuerSN
:
333 privkey
= signerinfo
->signingKey
;
334 signerinfo
->signingKey
= NULL
;
335 cert
= signerinfo
->cert
;
337 if (SecCertificateGetAlgorithmID(cert
,&algID
)) {
338 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
342 SecAsn1AlgId _algID
= SecCertificateGetPublicKeyAlgorithmID(cert
);
346 case SecCmsSignerIDSubjectKeyID
:
347 privkey
= signerinfo
->signingKey
;
348 signerinfo
->signingKey
= NULL
;
350 spki
= SECKEY_CreateSubjectPublicKeyInfo(signerinfo
->pubKey
);
351 SECKEY_DestroyPublicKey(signerinfo
->pubKey
);
352 signerinfo
->pubKey
= NULL
;
353 SECOID_CopyAlgorithmID(NULL
, &freeAlgID
, &spki
->algorithm
);
354 SECKEY_DestroySubjectPublicKeyInfo(spki
);
358 if (SecKeyGetAlgorithmID(signerinfo
->pubKey
,&algID
)) {
359 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
364 CFRelease(signerinfo
->pubKey
);
365 signerinfo
->pubKey
= NULL
;
368 PORT_SetError(SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE
);
371 digestalgtag
= SecCmsSignerInfoGetDigestAlgTag(signerinfo
);
372 pubkAlgTag
= SECOID_GetAlgorithmTag(algID
);
374 if (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDSubjectKeyID
) {
375 SECOID_DestroyAlgorithmID(&freeAlgID
, PR_FALSE
);
380 /* Fortezza MISSI have weird signature formats.
381 * Map them to standard DSA formats
383 pubkAlgTag
= PK11_FortezzaMapSig(pubkAlgTag
);
386 if (signerinfo
->authAttr
!= NULL
) {
387 SecAsn1Item encoded_attrs
;
389 /* find and fill in the message digest attribute. */
390 rv
= SecCmsAttributeArraySetAttr(poolp
, &(signerinfo
->authAttr
),
391 SEC_OID_PKCS9_MESSAGE_DIGEST
, digest
, PR_FALSE
);
392 if (rv
!= SECSuccess
)
395 if (contentType
!= NULL
) {
396 /* if the caller wants us to, find and fill in the content type attribute. */
397 rv
= SecCmsAttributeArraySetAttr(poolp
, &(signerinfo
->authAttr
),
398 SEC_OID_PKCS9_CONTENT_TYPE
, contentType
, PR_FALSE
);
399 if (rv
!= SECSuccess
)
403 if ((tmppoolp
= PORT_NewArena (1024)) == NULL
) {
404 PORT_SetError(SEC_ERROR_NO_MEMORY
);
409 * Before encoding, reorder the attributes so that when they
410 * are encoded, they will be conforming DER, which is required
411 * to have a specific order and that is what must be used for
412 * the hash/signature. We do this here, rather than building
413 * it into EncodeAttributes, because we do not want to do
414 * such reordering on incoming messages (which also uses
415 * EncodeAttributes) or our old signatures (and other "broken"
416 * implementations) will not verify. So, we want to guarantee
417 * that we send out good DER encodings of attributes, but not
418 * to expect to receive them.
420 if (SecCmsAttributeArrayReorder(signerinfo
->authAttr
) != SECSuccess
)
423 encoded_attrs
.Data
= NULL
;
424 encoded_attrs
.Length
= 0;
425 if (SecCmsAttributeArrayEncode(tmppoolp
, &(signerinfo
->authAttr
),
426 &encoded_attrs
) == NULL
)
430 rv
= SEC_SignData(&signature
, encoded_attrs
.Data
, encoded_attrs
.Length
,
431 privkey
, digestalgtag
, pubkAlgTag
);
433 signature
.Length
= SecKeyGetSize(privkey
, kSecKeySignatureSize
);
434 signature
.Data
= PORT_ZAlloc(signature
.Length
);
435 if (!signature
.Data
) {
436 signature
.Length
= 0;
439 rv
= SecKeyDigestAndSign(privkey
, &signerinfo
->digestAlg
, encoded_attrs
.Data
, encoded_attrs
.Length
, signature
.Data
, &signature
.Length
);
441 PORT_ZFree(signature
.Data
, signature
.Length
);
442 signature
.Length
= 0;
446 PORT_FreeArena(tmppoolp
, PR_FALSE
); /* awkward memory management :-( */
449 signature
.Length
= SecKeyGetSize(privkey
, kSecKeySignatureSize
);
450 signature
.Data
= PORT_ZAlloc(signature
.Length
);
451 if (!signature
.Data
) {
452 signature
.Length
= 0;
455 rv
= SecKeySignDigest(privkey
, &signerinfo
->digestAlg
, digest
->Data
, digest
->Length
,
456 signature
.Data
, &signature
.Length
);
458 PORT_ZFree(signature
.Data
, signature
.Length
);
459 signature
.Length
= 0;
462 SECKEY_DestroyPrivateKey(privkey
);
465 if (rv
!= SECSuccess
)
468 if (SECITEM_CopyItem(poolp
, &(signerinfo
->encDigest
), &signature
)
472 SECITEM_FreeItem(&signature
, PR_FALSE
);
474 if (SECOID_SetAlgorithmID(poolp
, &(signerinfo
->digestEncAlg
), pubkAlgTag
,
481 if (signature
.Length
!= 0)
482 SECITEM_FreeItem (&signature
, PR_FALSE
);
484 SECKEY_DestroyPrivateKey(privkey
);
486 PORT_FreeArena(tmppoolp
, PR_FALSE
);
492 SecCmsSignerInfoCopySigningCertificates(SecCmsSignerInfoRef signerinfo
)
494 CFMutableArrayRef certs
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
495 SecAsn1Item
**cert_datas
= signerinfo
->signedData
->rawCerts
;
496 SecAsn1Item
*cert_data
;
497 if (cert_datas
) while ((cert_data
= *cert_datas
) != NULL
) {
498 SecCertificateRef cert
= SecCertificateCreateWithBytes(NULL
, cert_data
->Data
, cert_data
->Length
);
500 switch (signerinfo
->signerIdentifier
.identifierType
) {
501 case SecCmsSignerIDIssuerSN
:
502 if (CERT_CheckIssuerAndSerial(cert
,
503 &(signerinfo
->signerIdentifier
.id
.issuerAndSN
->derIssuer
),
504 &(signerinfo
->signerIdentifier
.id
.issuerAndSN
->serialNumber
)))
505 CFArrayInsertValueAtIndex(certs
, 0, cert
);
507 CFArrayAppendValue(certs
, cert
);
509 case SecCmsSignerIDSubjectKeyID
:
511 CFDataRef cert_keyid
= SecCertificateGetSubjectKeyID(cert
);
512 SecAsn1Item
*tbf_keyid
= signerinfo
->signerIdentifier
.id
.subjectKeyID
;
513 if (tbf_keyid
->Length
== (size_t)CFDataGetLength(cert_keyid
) &&
514 !memcmp(tbf_keyid
->Data
, CFDataGetBytePtr(cert_keyid
), tbf_keyid
->Length
))
515 CFArrayInsertValueAtIndex(certs
, 0, cert
);
517 CFArrayAppendValue(certs
, cert
);
526 if ((CFArrayGetCount(certs
) == 0) &&
527 (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDIssuerSN
))
529 SecCertificateRef cert
= CERT_FindCertificateByIssuerAndSN(signerinfo
->signedData
->certs
, signerinfo
->signerIdentifier
.id
.issuerAndSN
);
531 CFArrayAppendValue(certs
, cert
);
540 SecCmsSignerInfoVerifyCertificate(SecCmsSignerInfoRef signerinfo
, SecKeychainRef keychainOrArray
,
541 CFTypeRef policies
, SecTrustRef
*trustRef
)
543 CFAbsoluteTime stime
;
547 SecCertificateRef cert
;
549 if ((cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, keychainOrArray
)) == NULL
) {
553 if ((certs
= SecCmsSignerInfoCopySigningCertificates(signerinfo
)) == NULL
) {
555 signerinfo
->verificationStatus
= SecCmsVSSigningCertNotFound
;
559 * Get and convert the signing time; if available, it will be used
560 * both on the cert verification and for importing the sender
563 if (SecCmsSignerInfoGetSigningTime(signerinfo
, &stime
) != SECSuccess
)
564 stime
= CFAbsoluteTimeGetCurrent();
567 rv
= CERT_VerifyCert(keychainOrArray
, cert
, policies
, stime
, trustRef
);
569 rv
= CERT_VerifyCert(keychainOrArray
, certs
, policies
, stime
, trustRef
);
574 if (PORT_GetError() == SEC_ERROR_UNTRUSTED_CERT
)
576 /* Signature or digest level verificationStatus errors should supercede certificate level errors, so only change the verificationStatus if the status was GoodSignature. */
578 #warning DEBUG - SecCmsSignerInfoVerifyCertificate trusts everything!
579 if (signerinfo
->verificationStatus
== SecCmsVSGoodSignature
) {
580 syslog(LOG_ERR
, "SecCmsSignerInfoVerifyCertificate ignoring SEC_ERROR_UNTRUSTED_CERT");
584 if (signerinfo
->verificationStatus
== SecCmsVSGoodSignature
)
585 signerinfo
->verificationStatus
= SecCmsVSSigningCertNotTrusted
;
594 * SecCmsSignerInfoVerify - verify the signature of a single SignerInfo
596 * Just verifies the signature. The assumption is that verification of the certificate
600 SecCmsSignerInfoVerify(SecCmsSignerInfoRef signerinfo
, SecAsn1Item
* digest
, SecAsn1Item
* contentType
)
602 SecPublicKeyRef publickey
= NULL
;
603 SecCmsAttribute
*attr
;
604 SecAsn1Item encoded_attrs
;
605 SecCertificateRef cert
;
606 SecCmsVerificationStatus vs
= SecCmsVSUnverified
;
608 SECOidTag digestAlgTag
, digestEncAlgTag
;
610 if (signerinfo
== NULL
)
613 /* SecCmsSignerInfoGetSigningCertificate will fail if 2nd parm is NULL and */
614 /* cert has not been verified */
615 if ((cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, NULL
)) == NULL
) {
616 vs
= SecCmsVSSigningCertNotFound
;
621 if (SecCertificateCopyPublicKey(cert
, &publickey
)) {
622 vs
= SecCmsVSProcessingError
;
626 publickey
= SecCertificateCopyPublicKey(cert
);
627 if (publickey
== NULL
)
631 digestAlgTag
= SECOID_GetAlgorithmTag(&(signerinfo
->digestAlg
));
632 digestEncAlgTag
= SECOID_GetAlgorithmTag(&(signerinfo
->digestEncAlg
));
633 if (!SecCmsArrayIsEmpty((void **)signerinfo
->authAttr
)) {
638 * RFC2630 sez that if there are any authenticated attributes,
639 * then there must be one for content type which matches the
640 * content type of the content being signed, and there must
641 * be one for message digest which matches our message digest.
642 * So check these things first.
644 if ((attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
645 SEC_OID_PKCS9_CONTENT_TYPE
, PR_TRUE
)) == NULL
)
647 vs
= SecCmsVSMalformedSignature
;
651 if (SecCmsAttributeCompareValue(attr
, contentType
) == PR_FALSE
) {
652 vs
= SecCmsVSMalformedSignature
;
660 if ((attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
, SEC_OID_PKCS9_MESSAGE_DIGEST
, PR_TRUE
)) == NULL
)
662 vs
= SecCmsVSMalformedSignature
;
665 if (SecCmsAttributeCompareValue(attr
, digest
) == PR_FALSE
) {
666 vs
= SecCmsVSDigestMismatch
;
670 if ((poolp
= PORT_NewArena (1024)) == NULL
) {
671 vs
= SecCmsVSProcessingError
;
678 * The signature is based on a digest of the DER-encoded authenticated
679 * attributes. So, first we encode and then we digest/verify.
680 * we trust the decoder to have the attributes in the right (sorted) order
682 encoded_attrs
.Data
= NULL
;
683 encoded_attrs
.Length
= 0;
685 if (SecCmsAttributeArrayEncode(poolp
, &(signerinfo
->authAttr
), &encoded_attrs
) == NULL
||
686 encoded_attrs
.Data
== NULL
|| encoded_attrs
.Length
== 0)
688 vs
= SecCmsVSProcessingError
;
691 if (errSecSuccess
== SecKeyDigestAndVerify(publickey
, &signerinfo
->digestAlg
, encoded_attrs
.Data
, encoded_attrs
.Length
, signerinfo
->encDigest
.Data
, signerinfo
->encDigest
.Length
))
692 vs
= SecCmsVSGoodSignature
;
694 vs
= SecCmsVSBadSignature
;
696 PORT_FreeArena(poolp
, PR_FALSE
); /* awkward memory management :-( */
701 /* No authenticated attributes. The signature is based on the plain message digest. */
702 sig
= &(signerinfo
->encDigest
);
703 if (sig
->Length
== 0)
706 if (SecKeyVerifyDigest(publickey
, &signerinfo
->digestAlg
, digest
->Data
, digest
->Length
, sig
->Data
, sig
->Length
))
707 vs
= SecCmsVSBadSignature
;
709 vs
= SecCmsVSGoodSignature
;
712 if (vs
== SecCmsVSBadSignature
) {
714 * XXX Change the generic error into our specific one, because
715 * in that case we get a better explanation out of the Security
716 * Advisor. This is really a bug in our error strings (the
717 * "generic" error has a lousy/wrong message associated with it
718 * which assumes the signature verification was done for the
719 * purposes of checking the issuer signature on a certificate)
720 * but this is at least an easy workaround and/or in the
721 * Security Advisor, which specifically checks for the error
722 * SEC_ERROR_PKCS7_BAD_SIGNATURE and gives more explanation
723 * in that case but does not similarly check for
724 * SEC_ERROR_BAD_SIGNATURE. It probably should, but then would
725 * probably say the wrong thing in the case that it *was* the
726 * certificate signature check that failed during the cert
727 * verification done above. Our error handling is really a mess.
729 if (PORT_GetError() == SEC_ERROR_BAD_SIGNATURE
)
730 PORT_SetError(SEC_ERROR_PKCS7_BAD_SIGNATURE
);
733 if (publickey
!= NULL
)
734 CFRelease(publickey
);
736 signerinfo
->verificationStatus
= vs
;
738 return (vs
== SecCmsVSGoodSignature
) ? SECSuccess
: SECFailure
;
741 if (publickey
!= NULL
)
742 SECKEY_DestroyPublicKey (publickey
);
744 signerinfo
->verificationStatus
= vs
;
746 PORT_SetError (SEC_ERROR_PKCS7_BAD_SIGNATURE
);
750 SecCmsVerificationStatus
751 SecCmsSignerInfoGetVerificationStatus(SecCmsSignerInfoRef signerinfo
)
753 return signerinfo
->verificationStatus
;
757 SecCmsSignerInfoGetDigestAlg(SecCmsSignerInfoRef signerinfo
)
759 return SECOID_FindOID (&(signerinfo
->digestAlg
.algorithm
));
763 SecCmsSignerInfoGetDigestAlgTag(SecCmsSignerInfoRef signerinfo
)
767 algdata
= SECOID_FindOID (&(signerinfo
->digestAlg
.algorithm
));
769 return algdata
->offset
;
771 return SEC_OID_UNKNOWN
;
775 SecCmsSignerInfoGetCertList(SecCmsSignerInfoRef signerinfo
)
777 return signerinfo
->certList
;
781 SecCmsSignerInfoGetVersion(SecCmsSignerInfoRef signerinfo
)
783 unsigned long version
;
785 /* always take apart the SecAsn1Item */
786 if (SEC_ASN1DecodeInteger(&(signerinfo
->version
), &version
) != SECSuccess
)
793 * SecCmsSignerInfoGetSigningTime - return the signing time,
794 * in UTCTime format, of a CMS signerInfo.
796 * sinfo - signerInfo data for this signer
798 * Returns a pointer to XXXX (what?)
799 * A return value of NULL is an error.
802 SecCmsSignerInfoGetSigningTime(SecCmsSignerInfoRef sinfo
, CFAbsoluteTime
*stime
)
804 SecCmsAttribute
*attr
;
810 if (sinfo
->signingTime
!= 0) {
811 *stime
= sinfo
->signingTime
; /* cached copy */
815 attr
= SecCmsAttributeArrayFindAttrByOidTag(sinfo
->authAttr
, SEC_OID_PKCS9_SIGNING_TIME
, PR_TRUE
);
816 /* XXXX multi-valued attributes NIH */
817 if (attr
== NULL
|| (value
= SecCmsAttributeGetValue(attr
)) == NULL
)
819 if (DER_UTCTimeToCFDate(value
, stime
) != SECSuccess
)
821 sinfo
->signingTime
= *stime
; /* make cached copy */
826 * Return the signing cert of a CMS signerInfo.
828 * the certs in the enclosing SignedData must have been imported already
831 SecCmsSignerInfoGetSigningCertificate(SecCmsSignerInfoRef signerinfo
, SecKeychainRef keychainOrArray
)
833 SecCertificateRef cert
= NULL
;
835 if (signerinfo
->cert
!= NULL
)
836 return signerinfo
->cert
;
838 /* @@@ Make sure we search though all the certs in the cms message itself as well, it's silly
839 to require them to be added to a keychain first. */
842 SecCmsSignerIdentifier
*sid
;
845 * This cert will also need to be freed, but since we save it
846 * in signerinfo for later, we do not want to destroy it when
847 * we leave this function -- we let the clean-up of the entire
848 * cinfo structure later do the destroy of this cert.
850 sid
= &signerinfo
->signerIdentifier
;
851 switch (sid
->identifierType
) {
852 case SecCmsSignerIDIssuerSN
:
853 cert
= CERT_FindCertByIssuerAndSN(keychainOrArray
, sid
->id
.issuerAndSN
);
855 case SecCmsSignerIDSubjectKeyID
:
856 cert
= CERT_FindCertBySubjectKeyID(keychainOrArray
, sid
->id
.subjectKeyID
);
863 /* cert can be NULL at that point */
864 signerinfo
->cert
= cert
; /* earmark it */
866 SecAsn1Item
**cert_datas
= signerinfo
->signedData
->rawCerts
;
867 SecAsn1Item
*cert_data
;
868 if (cert_datas
) while ((cert_data
= *cert_datas
) != NULL
) {
869 cert
= SecCertificateCreateWithBytes(NULL
, cert_data
->Data
, cert_data
->Length
);
871 switch (signerinfo
->signerIdentifier
.identifierType
) {
872 case SecCmsSignerIDIssuerSN
:
873 if (CERT_CheckIssuerAndSerial(cert
,
874 &(signerinfo
->signerIdentifier
.id
.issuerAndSN
->derIssuer
),
875 &(signerinfo
->signerIdentifier
.id
.issuerAndSN
->serialNumber
)))
876 signerinfo
->cert
= cert
;
878 case SecCmsSignerIDSubjectKeyID
: {
879 CFDataRef cert_keyid
= SecCertificateGetSubjectKeyID(cert
);
880 SecAsn1Item
*tbf_keyid
= signerinfo
->signerIdentifier
.id
.subjectKeyID
;
881 if (tbf_keyid
->Length
== (size_t)CFDataGetLength(cert_keyid
) &&
882 !memcmp(tbf_keyid
->Data
, CFDataGetBytePtr(cert_keyid
), tbf_keyid
->Length
))
883 signerinfo
->cert
= cert
;
886 if (signerinfo
->cert
)
893 if (!signerinfo
->cert
&& (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDIssuerSN
)) {
894 cert
= CERT_FindCertificateByIssuerAndSN(signerinfo
->signedData
->certs
, signerinfo
->signerIdentifier
.id
.issuerAndSN
);
895 signerinfo
->cert
= cert
;
904 * SecCmsSignerInfoGetSignerCommonName - return the common name of the signer
906 * sinfo - signerInfo data for this signer
908 * Returns a CFStringRef containing the common name of the signer.
909 * A return value of NULL is an error.
912 SecCmsSignerInfoGetSignerCommonName(SecCmsSignerInfoRef sinfo
)
914 SecCertificateRef signercert
;
915 CFStringRef commonName
= NULL
;
917 /* will fail if cert is not verified */
918 if ((signercert
= SecCmsSignerInfoGetSigningCertificate(sinfo
, NULL
)) == NULL
)
922 SecCertificateGetCommonName(signercert
, &commonName
);
924 CFArrayRef commonNames
= SecCertificateCopyCommonNames(signercert
);
926 /* SecCertificateCopyCommonNames doesn't return empty arrays */
927 commonName
= (CFStringRef
)CFArrayGetValueAtIndex(commonNames
, CFArrayGetCount(commonNames
) - 1);
928 CFRetain(commonName
);
929 CFRelease(commonNames
);
937 * SecCmsSignerInfoGetSignerEmailAddress - return the email address of the signer
939 * sinfo - signerInfo data for this signer
941 * Returns a CFStringRef containing the name of the signer.
942 * A return value of NULL is an error.
945 SecCmsSignerInfoGetSignerEmailAddress(SecCmsSignerInfoRef sinfo
)
947 SecCertificateRef signercert
;
948 CFStringRef emailAddress
= NULL
;
950 if ((signercert
= SecCmsSignerInfoGetSigningCertificate(sinfo
, NULL
)) == NULL
)
954 SecCertificateGetEmailAddress(signercert
, &emailAddress
);
956 CFArrayRef names
= SecCertificateCopyRFC822Names(signercert
);
958 if (CFArrayGetCount(names
) > 0)
959 emailAddress
= (CFStringRef
)CFArrayGetValueAtIndex(names
, 0);
961 CFRetain(emailAddress
);
970 * SecCmsSignerInfoAddAuthAttr - add an attribute to the
971 * authenticated (i.e. signed) attributes of "signerinfo".
974 SecCmsSignerInfoAddAuthAttr(SecCmsSignerInfoRef signerinfo
, SecCmsAttribute
*attr
)
976 return SecCmsAttributeArrayAddAttr(signerinfo
->signedData
->contentInfo
.cmsg
->poolp
, &(signerinfo
->authAttr
), attr
);
980 * SecCmsSignerInfoAddUnauthAttr - add an attribute to the
981 * unauthenticated attributes of "signerinfo".
984 SecCmsSignerInfoAddUnauthAttr(SecCmsSignerInfoRef signerinfo
, SecCmsAttribute
*attr
)
986 return SecCmsAttributeArrayAddAttr(signerinfo
->signedData
->contentInfo
.cmsg
->poolp
, &(signerinfo
->unAuthAttr
), attr
);
990 * SecCmsSignerInfoAddSigningTime - add the signing time to the
991 * authenticated (i.e. signed) attributes of "signerinfo".
993 * This is expected to be included in outgoing signed
994 * messages for email (S/MIME) but is likely useful in other situations.
996 * This should only be added once; a second call will do nothing.
998 * XXX This will probably just shove the current time into "signerinfo"
999 * but it will not actually get signed until the entire item is
1000 * processed for encoding. Is this (expected to be small) delay okay?
1003 SecCmsSignerInfoAddSigningTime(SecCmsSignerInfoRef signerinfo
, CFAbsoluteTime t
)
1005 SecCmsAttribute
*attr
;
1010 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
1012 mark
= PORT_ArenaMark(poolp
);
1014 /* create new signing time attribute */
1015 if (DER_CFDateToUTCTime(t
, &stime
) != SECSuccess
)
1018 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_SIGNING_TIME
, &stime
, PR_FALSE
)) == NULL
) {
1019 SECITEM_FreeItem (&stime
, PR_FALSE
);
1023 SECITEM_FreeItem (&stime
, PR_FALSE
);
1025 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1028 PORT_ArenaUnmark (poolp
, mark
);
1033 PORT_ArenaRelease (poolp
, mark
);
1038 * SecCmsSignerInfoAddSMIMECaps - add a SMIMECapabilities attribute to the
1039 * authenticated (i.e. signed) attributes of "signerinfo".
1041 * This is expected to be included in outgoing signed
1042 * messages for email (S/MIME).
1045 SecCmsSignerInfoAddSMIMECaps(SecCmsSignerInfoRef signerinfo
)
1047 SecCmsAttribute
*attr
;
1048 SecAsn1Item
* smimecaps
= NULL
;
1052 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
1054 mark
= PORT_ArenaMark(poolp
);
1056 smimecaps
= SECITEM_AllocItem(poolp
, NULL
, 0);
1057 if (smimecaps
== NULL
)
1060 /* create new signing time attribute */
1062 // @@@ We don't do Fortezza yet.
1063 if (SecSMIMECreateSMIMECapabilities(poolp
, smimecaps
, PR_FALSE
) != SECSuccess
)
1065 if (SecSMIMECreateSMIMECapabilities(poolp
, smimecaps
,
1066 PK11_FortezzaHasKEA(signerinfo
->cert
)) != SECSuccess
)
1070 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_SMIME_CAPABILITIES
, smimecaps
, PR_TRUE
)) == NULL
)
1073 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1076 PORT_ArenaUnmark (poolp
, mark
);
1080 PORT_ArenaRelease (poolp
, mark
);
1085 * SecCmsSignerInfoAddSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the
1086 * authenticated (i.e. signed) attributes of "signerinfo".
1088 * This is expected to be included in outgoing signed messages for email (S/MIME).
1091 SecCmsSignerInfoAddSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo
, SecCertificateRef cert
, SecKeychainRef keychainOrArray
)
1093 SecCmsAttribute
*attr
;
1094 SecAsn1Item
* smimeekp
= NULL
;
1101 /* verify this cert for encryption */
1102 policy
= CERT_PolicyForCertUsage(certUsageEmailRecipient
);
1103 if (CERT_VerifyCert(keychainOrArray
, cert
, policy
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1110 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
1111 mark
= PORT_ArenaMark(poolp
);
1113 smimeekp
= SECITEM_AllocItem(poolp
, NULL
, 0);
1114 if (smimeekp
== NULL
)
1117 /* create new signing time attribute */
1118 if (SecSMIMECreateSMIMEEncKeyPrefs(poolp
, smimeekp
, cert
) != SECSuccess
)
1121 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE
, smimeekp
, PR_TRUE
)) == NULL
)
1124 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1127 PORT_ArenaUnmark (poolp
, mark
);
1131 PORT_ArenaRelease (poolp
, mark
);
1136 * SecCmsSignerInfoAddMSSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the
1137 * authenticated (i.e. signed) attributes of "signerinfo", using the OID prefered by Microsoft.
1139 * This is expected to be included in outgoing signed messages for email (S/MIME),
1140 * if compatibility with Microsoft mail clients is wanted.
1143 SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo
, SecCertificateRef cert
, SecKeychainRef keychainOrArray
)
1145 SecCmsAttribute
*attr
;
1146 SecAsn1Item
* smimeekp
= NULL
;
1153 /* verify this cert for encryption */
1154 policy
= CERT_PolicyForCertUsage(certUsageEmailRecipient
);
1155 if (CERT_VerifyCert(keychainOrArray
, cert
, policy
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1162 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
1163 mark
= PORT_ArenaMark(poolp
);
1165 smimeekp
= SECITEM_AllocItem(poolp
, NULL
, 0);
1166 if (smimeekp
== NULL
)
1169 /* create new signing time attribute */
1170 if (SecSMIMECreateMSSMIMEEncKeyPrefs(poolp
, smimeekp
, cert
) != SECSuccess
)
1173 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_MS_SMIME_ENCRYPTION_KEY_PREFERENCE
, smimeekp
, PR_TRUE
)) == NULL
)
1176 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1179 PORT_ArenaUnmark (poolp
, mark
);
1183 PORT_ArenaRelease (poolp
, mark
);
1188 * SecCmsSignerInfoAddCounterSignature - countersign a signerinfo
1190 * 1. digest the DER-encoded signature value of the original signerinfo
1191 * 2. create new signerinfo with correct version, sid, digestAlg
1192 * 3. add message-digest authAttr, but NO content-type
1193 * 4. sign the authAttrs
1194 * 5. DER-encode the new signerInfo
1195 * 6. add the whole thing to original signerInfo's unAuthAttrs
1196 * as a SEC_OID_PKCS9_COUNTER_SIGNATURE attribute
1198 * XXXX give back the new signerinfo?
1201 SecCmsSignerInfoAddCounterSignature(SecCmsSignerInfoRef signerinfo
,
1202 SECOidTag digestalg
, SecIdentityRef identity
)
1209 * XXXX the following needs to be done in the S/MIME layer code
1210 * after signature of a signerinfo is verified
1213 SecCmsSignerInfoSaveSMIMEProfile(SecCmsSignerInfoRef signerinfo
)
1215 return -4 /*unImp*/;
1219 * SecCmsSignerInfoIncludeCerts - set cert chain inclusion mode for this signer
1222 SecCmsSignerInfoIncludeCerts(SecCmsSignerInfoRef signerinfo
, SecCmsCertChainMode cm
, SECCertUsage usage
)
1224 if (signerinfo
->cert
== NULL
)
1227 /* don't leak if we get called twice */
1228 if (signerinfo
->certList
!= NULL
) {
1229 CFRelease(signerinfo
->certList
);
1230 signerinfo
->certList
= NULL
;
1235 signerinfo
->certList
= NULL
;
1237 case SecCmsCMCertOnly
:
1238 signerinfo
->certList
= CERT_CertListFromCert(signerinfo
->cert
);
1240 case SecCmsCMCertChain
:
1241 signerinfo
->certList
= CERT_CertChainFromCert(signerinfo
->cert
, usage
, PR_FALSE
);
1243 case SecCmsCMCertChainWithRoot
:
1244 signerinfo
->certList
= CERT_CertChainFromCert(signerinfo
->cert
, usage
, PR_TRUE
);
1248 if (cm
!= SecCmsCMNone
&& signerinfo
->certList
== NULL
)