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);
152 if (!CFCalendarDecomposeAbsoluteTime(SecCFCalendarGetZulu(), date
, "yMdHms", &year
, &month
, &day
, &hour
, &minute
, &second
))
156 /* UTC time does not handle the years before 1950 */
160 /* remove the century since it's added to the year by the
161 CFAbsoluteTimeGetGregorianDate routine, but is not needed for UTC time */
164 d
[0] = HIDIGIT(year
);
165 d
[1] = LODIGIT(year
);
166 d
[2] = HIDIGIT(month
);
167 d
[3] = LODIGIT(month
);
170 d
[6] = HIDIGIT(hour
);
171 d
[7] = LODIGIT(hour
);
172 d
[8] = HIDIGIT(minute
);
173 d
[9] = LODIGIT(minute
);
174 d
[10] = HIDIGIT(second
);
175 d
[11] = LODIGIT(second
);
180 /* =============================================================================
184 nss_cmssignerinfo_create(SecCmsSignedDataRef sigd
, SecCmsSignerIDSelector type
, SecCertificateRef cert
, const SecAsn1Item
*subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
);
187 SecCmsSignerInfoCreateWithSubjKeyID(SecCmsSignedDataRef sigd
, const SecAsn1Item
*subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
)
189 return nss_cmssignerinfo_create(sigd
, SecCmsSignerIDSubjectKeyID
, NULL
, subjKeyID
, pubKey
, signingKey
, digestalgtag
);
193 SecCmsSignerInfoCreate(SecCmsSignedDataRef sigd
, SecIdentityRef identity
, SECOidTag digestalgtag
)
195 SecCmsSignerInfoRef signerInfo
= NULL
;
196 SecCertificateRef cert
= NULL
;
197 SecPrivateKeyRef signingKey
= NULL
;
199 if (SecIdentityCopyCertificate(identity
, &cert
))
201 if (SecIdentityCopyPrivateKey(identity
, &signingKey
))
204 signerInfo
= nss_cmssignerinfo_create(sigd
, SecCmsSignerIDIssuerSN
, cert
, NULL
, NULL
, signingKey
, digestalgtag
);
210 CFRelease(signingKey
);
216 nss_cmssignerinfo_create(SecCmsSignedDataRef sigd
, SecCmsSignerIDSelector type
, SecCertificateRef cert
, const SecAsn1Item
*subjKeyID
, SecPublicKeyRef pubKey
, SecPrivateKeyRef signingKey
, SECOidTag digestalgtag
)
219 SecCmsSignerInfoRef signerinfo
;
223 poolp
= sigd
->contentInfo
.cmsg
->poolp
;
225 mark
= PORT_ArenaMark(poolp
);
227 signerinfo
= (SecCmsSignerInfoRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsSignerInfo
));
228 if (signerinfo
== NULL
) {
229 PORT_ArenaRelease(poolp
, mark
);
234 signerinfo
->signedData
= sigd
;
237 case SecCmsSignerIDIssuerSN
:
238 signerinfo
->signerIdentifier
.identifierType
= SecCmsSignerIDIssuerSN
;
239 if ((signerinfo
->cert
= CERT_DupCertificate(cert
)) == NULL
)
241 if ((signerinfo
->signerIdentifier
.id
.issuerAndSN
= CERT_GetCertIssuerAndSN(poolp
, cert
)) == NULL
)
244 case SecCmsSignerIDSubjectKeyID
:
245 signerinfo
->signerIdentifier
.identifierType
= SecCmsSignerIDSubjectKeyID
;
246 PORT_Assert(subjKeyID
);
249 signerinfo
->signerIdentifier
.id
.subjectKeyID
= PORT_ArenaNew(poolp
, SecAsn1Item
);
250 SECITEM_CopyItem(poolp
, signerinfo
->signerIdentifier
.id
.subjectKeyID
,
252 signerinfo
->pubKey
= SECKEY_CopyPublicKey(pubKey
);
253 if (!signerinfo
->pubKey
)
263 signerinfo
->signingKey
= SECKEY_CopyPrivateKey(signingKey
);
264 if (!signerinfo
->signingKey
)
267 /* set version right now */
268 version
= SEC_CMS_SIGNER_INFO_VERSION_ISSUERSN
;
269 /* RFC2630 5.3 "version is the syntax version number. If the .... " */
270 if (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDSubjectKeyID
)
271 version
= SEC_CMS_SIGNER_INFO_VERSION_SUBJKEY
;
272 (void)SEC_ASN1EncodeInteger(poolp
, &(signerinfo
->version
), (long)version
);
274 if (SECOID_SetAlgorithmID(poolp
, &signerinfo
->digestAlg
, digestalgtag
, NULL
) != SECSuccess
)
277 if (SecCmsSignedDataAddSignerInfo(sigd
, signerinfo
))
280 PORT_ArenaUnmark(poolp
, mark
);
284 PORT_ArenaRelease(poolp
, mark
);
289 * SecCmsSignerInfoDestroy - destroy a SignerInfo data structure
292 SecCmsSignerInfoDestroy(SecCmsSignerInfoRef si
)
294 if (si
->cert
!= NULL
)
295 CERT_DestroyCertificate(si
->cert
);
297 if (si
->certList
!= NULL
)
298 CFRelease(si
->certList
);
300 /* XXX storage ??? */
303 static SecAsn1AlgId
SecCertificateGetPublicKeyAlgorithmID(SecCertificateRef cert
)
305 const DERAlgorithmId
*length_data_swapped
= SecCertificateGetPublicKeyAlgorithm(cert
);
306 SecAsn1AlgId temp
= {
307 { length_data_swapped
->oid
.length
, length_data_swapped
->oid
.data
},
308 { length_data_swapped
->params
.length
, length_data_swapped
->params
.data
} };
314 * SecCmsSignerInfoSign - sign something
318 SecCmsSignerInfoSign(SecCmsSignerInfoRef signerinfo
, SecAsn1Item
* digest
, SecAsn1Item
* contentType
)
320 SecCertificateRef cert
;
321 SecPrivateKeyRef privkey
= NULL
;
322 SECOidTag digestalgtag
;
323 SECOidTag pubkAlgTag
;
324 SecAsn1Item signature
= { 0 };
326 PLArenaPool
*poolp
, *tmppoolp
;
327 const SECAlgorithmID
*algID
= NULL
;
328 //CERTSubjectPublicKeyInfo *spki;
330 PORT_Assert (digest
!= NULL
);
332 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
334 switch (signerinfo
->signerIdentifier
.identifierType
) {
335 case SecCmsSignerIDIssuerSN
:
336 privkey
= signerinfo
->signingKey
;
337 signerinfo
->signingKey
= NULL
;
338 cert
= signerinfo
->cert
;
340 if (SecCertificateGetAlgorithmID(cert
,&algID
)) {
341 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
345 SecAsn1AlgId _algID
= SecCertificateGetPublicKeyAlgorithmID(cert
);
349 case SecCmsSignerIDSubjectKeyID
:
350 privkey
= signerinfo
->signingKey
;
351 signerinfo
->signingKey
= NULL
;
353 spki
= SECKEY_CreateSubjectPublicKeyInfo(signerinfo
->pubKey
);
354 SECKEY_DestroyPublicKey(signerinfo
->pubKey
);
355 signerinfo
->pubKey
= NULL
;
356 SECOID_CopyAlgorithmID(NULL
, &freeAlgID
, &spki
->algorithm
);
357 SECKEY_DestroySubjectPublicKeyInfo(spki
);
361 if (SecKeyGetAlgorithmID(signerinfo
->pubKey
,&algID
)) {
362 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
367 CFRelease(signerinfo
->pubKey
);
368 signerinfo
->pubKey
= NULL
;
371 PORT_SetError(SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE
);
374 digestalgtag
= SecCmsSignerInfoGetDigestAlgTag(signerinfo
);
375 pubkAlgTag
= SECOID_GetAlgorithmTag(algID
);
377 if (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDSubjectKeyID
) {
378 SECOID_DestroyAlgorithmID(&freeAlgID
, PR_FALSE
);
383 /* Fortezza MISSI have weird signature formats.
384 * Map them to standard DSA formats
386 pubkAlgTag
= PK11_FortezzaMapSig(pubkAlgTag
);
389 if (signerinfo
->authAttr
!= NULL
) {
390 SecAsn1Item encoded_attrs
;
392 /* find and fill in the message digest attribute. */
393 rv
= SecCmsAttributeArraySetAttr(poolp
, &(signerinfo
->authAttr
),
394 SEC_OID_PKCS9_MESSAGE_DIGEST
, digest
, PR_FALSE
);
395 if (rv
!= SECSuccess
)
398 if (contentType
!= NULL
) {
399 /* if the caller wants us to, find and fill in the content type attribute. */
400 rv
= SecCmsAttributeArraySetAttr(poolp
, &(signerinfo
->authAttr
),
401 SEC_OID_PKCS9_CONTENT_TYPE
, contentType
, PR_FALSE
);
402 if (rv
!= SECSuccess
)
406 if ((tmppoolp
= PORT_NewArena (1024)) == NULL
) {
407 PORT_SetError(SEC_ERROR_NO_MEMORY
);
412 * Before encoding, reorder the attributes so that when they
413 * are encoded, they will be conforming DER, which is required
414 * to have a specific order and that is what must be used for
415 * the hash/signature. We do this here, rather than building
416 * it into EncodeAttributes, because we do not want to do
417 * such reordering on incoming messages (which also uses
418 * EncodeAttributes) or our old signatures (and other "broken"
419 * implementations) will not verify. So, we want to guarantee
420 * that we send out good DER encodings of attributes, but not
421 * to expect to receive them.
423 if (SecCmsAttributeArrayReorder(signerinfo
->authAttr
) != SECSuccess
)
426 encoded_attrs
.Data
= NULL
;
427 encoded_attrs
.Length
= 0;
428 if (SecCmsAttributeArrayEncode(tmppoolp
, &(signerinfo
->authAttr
),
429 &encoded_attrs
) == NULL
)
433 rv
= SEC_SignData(&signature
, encoded_attrs
.Data
, encoded_attrs
.Length
,
434 privkey
, digestalgtag
, pubkAlgTag
);
436 signature
.Length
= SecKeyGetSize(privkey
, kSecKeySignatureSize
);
437 signature
.Data
= PORT_ZAlloc(signature
.Length
);
438 if (!signature
.Data
) {
439 signature
.Length
= 0;
442 rv
= SecKeyDigestAndSign(privkey
, &signerinfo
->digestAlg
, encoded_attrs
.Data
, encoded_attrs
.Length
, signature
.Data
, &signature
.Length
);
444 PORT_ZFree(signature
.Data
, signature
.Length
);
445 signature
.Length
= 0;
449 PORT_FreeArena(tmppoolp
, PR_FALSE
); /* awkward memory management :-( */
451 signature
.Length
= SecKeyGetSize(privkey
, kSecKeySignatureSize
);
452 signature
.Data
= PORT_ZAlloc(signature
.Length
);
453 if (!signature
.Data
) {
454 signature
.Length
= 0;
457 rv
= SecKeySignDigest(privkey
, &signerinfo
->digestAlg
, digest
->Data
, digest
->Length
,
458 signature
.Data
, &signature
.Length
);
460 PORT_ZFree(signature
.Data
, signature
.Length
);
461 signature
.Length
= 0;
464 SECKEY_DestroyPrivateKey(privkey
);
467 if (rv
!= SECSuccess
)
470 if (SECITEM_CopyItem(poolp
, &(signerinfo
->encDigest
), &signature
)
474 SECITEM_FreeItem(&signature
, PR_FALSE
);
476 if (SECOID_SetAlgorithmID(poolp
, &(signerinfo
->digestEncAlg
), pubkAlgTag
,
483 if (signature
.Length
!= 0)
484 SECITEM_FreeItem (&signature
, PR_FALSE
);
486 SECKEY_DestroyPrivateKey(privkey
);
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. */
577 if (signerinfo
->verificationStatus
== SecCmsVSGoodSignature
)
578 signerinfo
->verificationStatus
= SecCmsVSSigningCertNotTrusted
;
586 * SecCmsSignerInfoVerify - verify the signature of a single SignerInfo
588 * Just verifies the signature. The assumption is that verification of the certificate
592 SecCmsSignerInfoVerify(SecCmsSignerInfoRef signerinfo
, SecAsn1Item
* digest
, SecAsn1Item
* contentType
)
594 SecPublicKeyRef publickey
= NULL
;
595 SecCmsAttribute
*attr
;
596 SecAsn1Item encoded_attrs
;
597 SecCertificateRef cert
;
598 SecCmsVerificationStatus vs
= SecCmsVSUnverified
;
600 SECOidTag digestAlgTag
, digestEncAlgTag
;
602 if (signerinfo
== NULL
)
605 /* SecCmsSignerInfoGetSigningCertificate will fail if 2nd parm is NULL and */
606 /* cert has not been verified */
607 if ((cert
= SecCmsSignerInfoGetSigningCertificate(signerinfo
, NULL
)) == NULL
) {
608 vs
= SecCmsVSSigningCertNotFound
;
613 if (SecCertificateCopyPublicKey(cert
, &publickey
)) {
614 vs
= SecCmsVSProcessingError
;
618 publickey
= SecCertificateCopyPublicKey(cert
);
619 if (publickey
== NULL
)
623 digestAlgTag
= SECOID_GetAlgorithmTag(&(signerinfo
->digestAlg
));
624 digestEncAlgTag
= SECOID_GetAlgorithmTag(&(signerinfo
->digestEncAlg
));
625 if (!SecCmsArrayIsEmpty((void **)signerinfo
->authAttr
)) {
630 * RFC2630 sez that if there are any authenticated attributes,
631 * then there must be one for content type which matches the
632 * content type of the content being signed, and there must
633 * be one for message digest which matches our message digest.
634 * So check these things first.
636 if ((attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
,
637 SEC_OID_PKCS9_CONTENT_TYPE
, PR_TRUE
)) == NULL
)
639 vs
= SecCmsVSMalformedSignature
;
643 if (SecCmsAttributeCompareValue(attr
, contentType
) == PR_FALSE
) {
644 vs
= SecCmsVSMalformedSignature
;
652 if ((attr
= SecCmsAttributeArrayFindAttrByOidTag(signerinfo
->authAttr
, SEC_OID_PKCS9_MESSAGE_DIGEST
, PR_TRUE
)) == NULL
)
654 vs
= SecCmsVSMalformedSignature
;
657 if (SecCmsAttributeCompareValue(attr
, digest
) == PR_FALSE
) {
658 vs
= SecCmsVSDigestMismatch
;
662 if ((poolp
= PORT_NewArena (1024)) == NULL
) {
663 vs
= SecCmsVSProcessingError
;
670 * The signature is based on a digest of the DER-encoded authenticated
671 * attributes. So, first we encode and then we digest/verify.
672 * we trust the decoder to have the attributes in the right (sorted) order
674 encoded_attrs
.Data
= NULL
;
675 encoded_attrs
.Length
= 0;
677 if (SecCmsAttributeArrayEncode(poolp
, &(signerinfo
->authAttr
), &encoded_attrs
) == NULL
||
678 encoded_attrs
.Data
== NULL
|| encoded_attrs
.Length
== 0)
680 vs
= SecCmsVSProcessingError
;
683 if (errSecSuccess
== SecKeyDigestAndVerify(publickey
, &signerinfo
->digestAlg
, encoded_attrs
.Data
, encoded_attrs
.Length
, signerinfo
->encDigest
.Data
, signerinfo
->encDigest
.Length
))
684 vs
= SecCmsVSGoodSignature
;
686 vs
= SecCmsVSBadSignature
;
688 PORT_FreeArena(poolp
, PR_FALSE
); /* awkward memory management :-( */
693 /* No authenticated attributes. The signature is based on the plain message digest. */
694 sig
= &(signerinfo
->encDigest
);
695 if (sig
->Length
== 0)
698 if (SecKeyVerifyDigest(publickey
, &signerinfo
->digestAlg
, digest
->Data
, digest
->Length
, sig
->Data
, sig
->Length
))
699 vs
= SecCmsVSBadSignature
;
701 vs
= SecCmsVSGoodSignature
;
704 if (vs
== SecCmsVSBadSignature
) {
706 * XXX Change the generic error into our specific one, because
707 * in that case we get a better explanation out of the Security
708 * Advisor. This is really a bug in our error strings (the
709 * "generic" error has a lousy/wrong message associated with it
710 * which assumes the signature verification was done for the
711 * purposes of checking the issuer signature on a certificate)
712 * but this is at least an easy workaround and/or in the
713 * Security Advisor, which specifically checks for the error
714 * SEC_ERROR_PKCS7_BAD_SIGNATURE and gives more explanation
715 * in that case but does not similarly check for
716 * SEC_ERROR_BAD_SIGNATURE. It probably should, but then would
717 * probably say the wrong thing in the case that it *was* the
718 * certificate signature check that failed during the cert
719 * verification done above. Our error handling is really a mess.
721 if (PORT_GetError() == SEC_ERROR_BAD_SIGNATURE
)
722 PORT_SetError(SEC_ERROR_PKCS7_BAD_SIGNATURE
);
725 if (publickey
!= NULL
)
726 CFRelease(publickey
);
728 signerinfo
->verificationStatus
= vs
;
730 return (vs
== SecCmsVSGoodSignature
) ? SECSuccess
: SECFailure
;
733 if (publickey
!= NULL
)
734 SECKEY_DestroyPublicKey (publickey
);
736 signerinfo
->verificationStatus
= vs
;
738 PORT_SetError (SEC_ERROR_PKCS7_BAD_SIGNATURE
);
742 SecCmsVerificationStatus
743 SecCmsSignerInfoGetVerificationStatus(SecCmsSignerInfoRef signerinfo
)
745 return signerinfo
->verificationStatus
;
749 SecCmsSignerInfoGetDigestAlg(SecCmsSignerInfoRef signerinfo
)
751 return SECOID_FindOID (&(signerinfo
->digestAlg
.algorithm
));
755 SecCmsSignerInfoGetDigestAlgTag(SecCmsSignerInfoRef signerinfo
)
759 algdata
= SECOID_FindOID (&(signerinfo
->digestAlg
.algorithm
));
761 return algdata
->offset
;
763 return SEC_OID_UNKNOWN
;
767 SecCmsSignerInfoGetCertList(SecCmsSignerInfoRef signerinfo
)
769 return signerinfo
->certList
;
773 SecCmsSignerInfoGetVersion(SecCmsSignerInfoRef signerinfo
)
775 unsigned long version
;
777 /* always take apart the SecAsn1Item */
778 if (SEC_ASN1DecodeInteger(&(signerinfo
->version
), &version
) != SECSuccess
)
785 * SecCmsSignerInfoGetSigningTime - return the signing time,
786 * in UTCTime format, of a CMS signerInfo.
788 * sinfo - signerInfo data for this signer
790 * Returns a pointer to XXXX (what?)
791 * A return value of NULL is an error.
794 SecCmsSignerInfoGetSigningTime(SecCmsSignerInfoRef sinfo
, CFAbsoluteTime
*stime
)
796 SecCmsAttribute
*attr
;
802 if (sinfo
->signingTime
!= 0) {
803 *stime
= sinfo
->signingTime
; /* cached copy */
807 attr
= SecCmsAttributeArrayFindAttrByOidTag(sinfo
->authAttr
, SEC_OID_PKCS9_SIGNING_TIME
, PR_TRUE
);
808 /* XXXX multi-valued attributes NIH */
809 if (attr
== NULL
|| (value
= SecCmsAttributeGetValue(attr
)) == NULL
)
811 if (DER_UTCTimeToCFDate(value
, stime
) != SECSuccess
)
813 sinfo
->signingTime
= *stime
; /* make cached copy */
818 * Return the signing cert of a CMS signerInfo.
820 * the certs in the enclosing SignedData must have been imported already
823 SecCmsSignerInfoGetSigningCertificate(SecCmsSignerInfoRef signerinfo
, SecKeychainRef keychainOrArray
)
825 SecCertificateRef cert
= NULL
;
827 if (signerinfo
->cert
!= NULL
)
828 return signerinfo
->cert
;
830 /* @@@ Make sure we search though all the certs in the cms message itself as well, it's silly
831 to require them to be added to a keychain first. */
834 SecCmsSignerIdentifier
*sid
;
837 * This cert will also need to be freed, but since we save it
838 * in signerinfo for later, we do not want to destroy it when
839 * we leave this function -- we let the clean-up of the entire
840 * cinfo structure later do the destroy of this cert.
842 sid
= &signerinfo
->signerIdentifier
;
843 switch (sid
->identifierType
) {
844 case SecCmsSignerIDIssuerSN
:
845 cert
= CERT_FindCertByIssuerAndSN(keychainOrArray
, sid
->id
.issuerAndSN
);
847 case SecCmsSignerIDSubjectKeyID
:
848 cert
= CERT_FindCertBySubjectKeyID(keychainOrArray
, sid
->id
.subjectKeyID
);
855 /* cert can be NULL at that point */
856 signerinfo
->cert
= cert
; /* earmark it */
858 SecAsn1Item
**cert_datas
= signerinfo
->signedData
->rawCerts
;
859 SecAsn1Item
*cert_data
;
860 if (cert_datas
) while ((cert_data
= *cert_datas
) != NULL
) {
861 cert
= SecCertificateCreateWithBytes(NULL
, cert_data
->Data
, cert_data
->Length
);
863 switch (signerinfo
->signerIdentifier
.identifierType
) {
864 case SecCmsSignerIDIssuerSN
:
865 if (CERT_CheckIssuerAndSerial(cert
,
866 &(signerinfo
->signerIdentifier
.id
.issuerAndSN
->derIssuer
),
867 &(signerinfo
->signerIdentifier
.id
.issuerAndSN
->serialNumber
)))
868 signerinfo
->cert
= cert
;
870 case SecCmsSignerIDSubjectKeyID
: {
871 CFDataRef cert_keyid
= SecCertificateGetSubjectKeyID(cert
);
872 SecAsn1Item
*tbf_keyid
= signerinfo
->signerIdentifier
.id
.subjectKeyID
;
873 if (tbf_keyid
->Length
== (size_t)CFDataGetLength(cert_keyid
) &&
874 !memcmp(tbf_keyid
->Data
, CFDataGetBytePtr(cert_keyid
), tbf_keyid
->Length
))
875 signerinfo
->cert
= cert
;
878 if (signerinfo
->cert
)
885 if (!signerinfo
->cert
&& (signerinfo
->signerIdentifier
.identifierType
== SecCmsSignerIDIssuerSN
)) {
886 cert
= CERT_FindCertificateByIssuerAndSN(signerinfo
->signedData
->certs
, signerinfo
->signerIdentifier
.id
.issuerAndSN
);
887 signerinfo
->cert
= cert
;
896 * SecCmsSignerInfoGetSignerCommonName - return the common name of the signer
898 * sinfo - signerInfo data for this signer
900 * Returns a CFStringRef containing the common name of the signer.
901 * A return value of NULL is an error.
904 SecCmsSignerInfoGetSignerCommonName(SecCmsSignerInfoRef sinfo
)
906 SecCertificateRef signercert
;
907 CFStringRef commonName
= NULL
;
909 /* will fail if cert is not verified */
910 if ((signercert
= SecCmsSignerInfoGetSigningCertificate(sinfo
, NULL
)) == NULL
)
914 SecCertificateGetCommonName(signercert
, &commonName
);
916 CFArrayRef commonNames
= SecCertificateCopyCommonNames(signercert
);
918 /* SecCertificateCopyCommonNames doesn't return empty arrays */
919 commonName
= (CFStringRef
)CFArrayGetValueAtIndex(commonNames
, CFArrayGetCount(commonNames
) - 1);
920 CFRetain(commonName
);
921 CFRelease(commonNames
);
929 * SecCmsSignerInfoGetSignerEmailAddress - return the email address of the signer
931 * sinfo - signerInfo data for this signer
933 * Returns a CFStringRef containing the name of the signer.
934 * A return value of NULL is an error.
937 SecCmsSignerInfoGetSignerEmailAddress(SecCmsSignerInfoRef sinfo
)
939 SecCertificateRef signercert
;
940 CFStringRef emailAddress
= NULL
;
942 if ((signercert
= SecCmsSignerInfoGetSigningCertificate(sinfo
, NULL
)) == NULL
)
946 SecCertificateGetEmailAddress(signercert
, &emailAddress
);
948 CFArrayRef names
= SecCertificateCopyRFC822Names(signercert
);
950 if (CFArrayGetCount(names
) > 0)
951 emailAddress
= (CFStringRef
)CFArrayGetValueAtIndex(names
, 0);
953 CFRetain(emailAddress
);
962 * SecCmsSignerInfoAddAuthAttr - add an attribute to the
963 * authenticated (i.e. signed) attributes of "signerinfo".
966 SecCmsSignerInfoAddAuthAttr(SecCmsSignerInfoRef signerinfo
, SecCmsAttribute
*attr
)
968 return SecCmsAttributeArrayAddAttr(signerinfo
->signedData
->contentInfo
.cmsg
->poolp
, &(signerinfo
->authAttr
), attr
);
972 * SecCmsSignerInfoAddUnauthAttr - add an attribute to the
973 * unauthenticated attributes of "signerinfo".
976 SecCmsSignerInfoAddUnauthAttr(SecCmsSignerInfoRef signerinfo
, SecCmsAttribute
*attr
)
978 return SecCmsAttributeArrayAddAttr(signerinfo
->signedData
->contentInfo
.cmsg
->poolp
, &(signerinfo
->unAuthAttr
), attr
);
982 * SecCmsSignerInfoAddSigningTime - add the signing time to the
983 * authenticated (i.e. signed) attributes of "signerinfo".
985 * This is expected to be included in outgoing signed
986 * messages for email (S/MIME) but is likely useful in other situations.
988 * This should only be added once; a second call will do nothing.
990 * XXX This will probably just shove the current time into "signerinfo"
991 * but it will not actually get signed until the entire item is
992 * processed for encoding. Is this (expected to be small) delay okay?
995 SecCmsSignerInfoAddSigningTime(SecCmsSignerInfoRef signerinfo
, CFAbsoluteTime t
)
997 SecCmsAttribute
*attr
;
1002 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
1004 mark
= PORT_ArenaMark(poolp
);
1006 /* create new signing time attribute */
1007 if (DER_CFDateToUTCTime(t
, &stime
) != SECSuccess
)
1010 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_SIGNING_TIME
, &stime
, PR_FALSE
)) == NULL
) {
1011 SECITEM_FreeItem (&stime
, PR_FALSE
);
1015 SECITEM_FreeItem (&stime
, PR_FALSE
);
1017 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1020 PORT_ArenaUnmark (poolp
, mark
);
1025 PORT_ArenaRelease (poolp
, mark
);
1030 * SecCmsSignerInfoAddSMIMECaps - add a SMIMECapabilities attribute to the
1031 * authenticated (i.e. signed) attributes of "signerinfo".
1033 * This is expected to be included in outgoing signed
1034 * messages for email (S/MIME).
1037 SecCmsSignerInfoAddSMIMECaps(SecCmsSignerInfoRef signerinfo
)
1039 SecCmsAttribute
*attr
;
1040 SecAsn1Item
* smimecaps
= NULL
;
1044 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
1046 mark
= PORT_ArenaMark(poolp
);
1048 smimecaps
= SECITEM_AllocItem(poolp
, NULL
, 0);
1049 if (smimecaps
== NULL
)
1052 /* create new signing time attribute */
1054 // @@@ We don't do Fortezza yet.
1055 if (SecSMIMECreateSMIMECapabilities(poolp
, smimecaps
, PR_FALSE
) != SECSuccess
)
1057 if (SecSMIMECreateSMIMECapabilities(poolp
, smimecaps
,
1058 PK11_FortezzaHasKEA(signerinfo
->cert
)) != SECSuccess
)
1062 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_PKCS9_SMIME_CAPABILITIES
, smimecaps
, PR_TRUE
)) == NULL
)
1065 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1068 PORT_ArenaUnmark (poolp
, mark
);
1072 PORT_ArenaRelease (poolp
, mark
);
1077 * SecCmsSignerInfoAddSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the
1078 * authenticated (i.e. signed) attributes of "signerinfo".
1080 * This is expected to be included in outgoing signed messages for email (S/MIME).
1083 SecCmsSignerInfoAddSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo
, SecCertificateRef cert
, SecKeychainRef keychainOrArray
)
1085 SecCmsAttribute
*attr
;
1086 SecAsn1Item
* smimeekp
= NULL
;
1093 /* verify this cert for encryption */
1094 policy
= CERT_PolicyForCertUsage(certUsageEmailRecipient
);
1095 if (CERT_VerifyCert(keychainOrArray
, cert
, policy
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1102 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
1103 mark
= PORT_ArenaMark(poolp
);
1105 smimeekp
= SECITEM_AllocItem(poolp
, NULL
, 0);
1106 if (smimeekp
== NULL
)
1109 /* create new signing time attribute */
1110 if (SecSMIMECreateSMIMEEncKeyPrefs(poolp
, smimeekp
, cert
) != SECSuccess
)
1113 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE
, smimeekp
, PR_TRUE
)) == NULL
)
1116 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1119 PORT_ArenaUnmark (poolp
, mark
);
1123 PORT_ArenaRelease (poolp
, mark
);
1128 * SecCmsSignerInfoAddMSSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the
1129 * authenticated (i.e. signed) attributes of "signerinfo", using the OID prefered by Microsoft.
1131 * This is expected to be included in outgoing signed messages for email (S/MIME),
1132 * if compatibility with Microsoft mail clients is wanted.
1135 SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo
, SecCertificateRef cert
, SecKeychainRef keychainOrArray
)
1137 SecCmsAttribute
*attr
;
1138 SecAsn1Item
* smimeekp
= NULL
;
1145 /* verify this cert for encryption */
1146 policy
= CERT_PolicyForCertUsage(certUsageEmailRecipient
);
1147 if (CERT_VerifyCert(keychainOrArray
, cert
, policy
, CFAbsoluteTimeGetCurrent(), NULL
) != SECSuccess
) {
1154 poolp
= signerinfo
->signedData
->contentInfo
.cmsg
->poolp
;
1155 mark
= PORT_ArenaMark(poolp
);
1157 smimeekp
= SECITEM_AllocItem(poolp
, NULL
, 0);
1158 if (smimeekp
== NULL
)
1161 /* create new signing time attribute */
1162 if (SecSMIMECreateMSSMIMEEncKeyPrefs(poolp
, smimeekp
, cert
) != SECSuccess
)
1165 if ((attr
= SecCmsAttributeCreate(poolp
, SEC_OID_MS_SMIME_ENCRYPTION_KEY_PREFERENCE
, smimeekp
, PR_TRUE
)) == NULL
)
1168 if (SecCmsSignerInfoAddAuthAttr(signerinfo
, attr
) != SECSuccess
)
1171 PORT_ArenaUnmark (poolp
, mark
);
1175 PORT_ArenaRelease (poolp
, mark
);
1180 * SecCmsSignerInfoAddCounterSignature - countersign a signerinfo
1182 * 1. digest the DER-encoded signature value of the original signerinfo
1183 * 2. create new signerinfo with correct version, sid, digestAlg
1184 * 3. add message-digest authAttr, but NO content-type
1185 * 4. sign the authAttrs
1186 * 5. DER-encode the new signerInfo
1187 * 6. add the whole thing to original signerInfo's unAuthAttrs
1188 * as a SEC_OID_PKCS9_COUNTER_SIGNATURE attribute
1190 * XXXX give back the new signerinfo?
1193 SecCmsSignerInfoAddCounterSignature(SecCmsSignerInfoRef signerinfo
,
1194 SECOidTag digestalg
, SecIdentityRef identity
)
1201 * XXXX the following needs to be done in the S/MIME layer code
1202 * after signature of a signerinfo is verified
1205 SecCmsSignerInfoSaveSMIMEProfile(SecCmsSignerInfoRef signerinfo
)
1207 return -4 /*unImp*/;
1211 * SecCmsSignerInfoIncludeCerts - set cert chain inclusion mode for this signer
1214 SecCmsSignerInfoIncludeCerts(SecCmsSignerInfoRef signerinfo
, SecCmsCertChainMode cm
, SECCertUsage usage
)
1216 if (signerinfo
->cert
== NULL
)
1219 /* don't leak if we get called twice */
1220 if (signerinfo
->certList
!= NULL
) {
1221 CFRelease(signerinfo
->certList
);
1222 signerinfo
->certList
= NULL
;
1227 signerinfo
->certList
= NULL
;
1229 case SecCmsCMCertOnly
:
1230 signerinfo
->certList
= CERT_CertListFromCert(signerinfo
->cert
);
1232 case SecCmsCMCertChain
:
1233 signerinfo
->certList
= CERT_CertChainFromCert(signerinfo
->cert
, usage
, PR_FALSE
);
1235 case SecCmsCMCertChainWithRoot
:
1236 signerinfo
->certList
= CERT_CertChainFromCert(signerinfo
->cert
, usage
, PR_TRUE
);
1240 if (cm
!= SecCmsCMNone
&& signerinfo
->certList
== NULL
)