X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/5c19dc3ae3bd8e40a9c028b0deddd50ff337692c..7e6b461318c8a779d91381531435a68ee4e8b6ed:/OSX/libsecurity_cms/lib/CMSEncoder.cpp?ds=sidebyside diff --git a/OSX/libsecurity_cms/lib/CMSEncoder.cpp b/OSX/libsecurity_cms/lib/CMSEncoder.cpp index c60110c7..27d6810d 100644 --- a/OSX/libsecurity_cms/lib/CMSEncoder.cpp +++ b/OSX/libsecurity_cms/lib/CMSEncoder.cpp @@ -25,8 +25,8 @@ * CMSEncoder.cpp - encode, sign, and/or encrypt CMS messages. */ -#include "CMSEncoder.h" -#include "CMSPrivate.h" +#include +#include #include "CMSUtils.h" #include #include @@ -47,6 +47,7 @@ #include #include #include +#include #include #include @@ -96,6 +97,9 @@ struct _CMSEncoder { SECOidTag digestalgtag; CMSCertificateChainMode chainMode; + CFDataRef hashAgilityAttrValue; + CFDictionaryRef hashAgilityV2AttrValues; + CFAbsoluteTime expirationTime; }; static void cmsEncoderInit(CFTypeRef enc); @@ -243,12 +247,12 @@ static int encodeOid( for(digit=0; digitotherCerts); if(cmsEncoder->cmsMsg != NULL) { SecCmsMessageDestroy(cmsEncoder->cmsMsg); + cmsEncoder->cmsMsg = NULL; } if(cmsEncoder->arena != NULL) { SecArenaPoolFree(cmsEncoder->arena, false); @@ -442,7 +455,6 @@ static OSStatus cmsSetupForSignedData( numSigners = CFArrayGetCount(cmsEncoder->signers); } CFIndex dex; - SecKeychainRef ourKc = NULL; SecCertificateRef ourCert = NULL; SecCmsCertChainMode chainMode = SecCmsCMCertChain; @@ -456,6 +468,9 @@ static OSStatus cmsSetupForSignedData( case kCMSCertificateChainWithRoot: chainMode = SecCmsCMCertChainWithRoot; break; + case kCMSCertificateChainWithRootOrFail: + chainMode = SecCmsCMCertChainWithRootOrFail; + break; default: break; } @@ -469,11 +484,6 @@ static OSStatus cmsSetupForSignedData( CSSM_PERROR("SecIdentityCopyCertificate", ortn); break; } - ortn = SecKeychainItemCopyKeychain((SecKeychainItemRef)ourCert, &ourKc); - if(ortn) { - CSSM_PERROR("SecKeychainItemCopyKeychain", ortn); - break; - } signerInfo = SecCmsSignerInfoCreate(cmsEncoder->cmsMsg, ourId, cmsEncoder->digestalgtag); if (signerInfo == NULL) { ortn = errSecInternalComponent; @@ -500,7 +510,7 @@ static OSStatus cmsSetupForSignedData( } } if(cmsEncoder->signedAttributes & kCMSAttrSmimeEncryptionKeyPrefs) { - ortn = SecCmsSignerInfoAddSMIMEEncKeyPrefs(signerInfo, ourCert, ourKc); + ortn = SecCmsSignerInfoAddSMIMEEncKeyPrefs(signerInfo, ourCert, NULL); if(ortn) { ortn = cmsRtnToOSStatus(ortn); CSSM_PERROR("SecCmsSignerInfoAddSMIMEEncKeyPrefs", ortn); @@ -508,7 +518,7 @@ static OSStatus cmsSetupForSignedData( } } if(cmsEncoder->signedAttributes & kCMSAttrSmimeMSEncryptionKeyPrefs) { - ortn = SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(signerInfo, ourCert, ourKc); + ortn = SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(signerInfo, ourCert, NULL); if(ortn) { ortn = cmsRtnToOSStatus(ortn); CSSM_PERROR("SecCmsSignerInfoAddMSSMIMEEncKeyPrefs", ortn); @@ -525,6 +535,34 @@ static OSStatus cmsSetupForSignedData( break; } } + if(cmsEncoder->signedAttributes & kCMSAttrAppleCodesigningHashAgility) { + ortn = SecCmsSignerInfoAddAppleCodesigningHashAgility(signerInfo, cmsEncoder->hashAgilityAttrValue); + /* libsecurity_smime made a copy of the attribute value. We don't need it anymore. */ + CFReleaseNull(cmsEncoder->hashAgilityAttrValue); + if(ortn) { + ortn = cmsRtnToOSStatus(ortn); + CSSM_PERROR("SecCmsSignerInfoAddAppleCodesigningHashAgility", ortn); + break; + } + } + if(cmsEncoder->signedAttributes & kCMSAttrAppleCodesigningHashAgilityV2) { + ortn = SecCmsSignerInfoAddAppleCodesigningHashAgilityV2(signerInfo, cmsEncoder->hashAgilityV2AttrValues); + /* libsecurity_smime made a copy of the attribute value. We don't need it anymore. */ + CFReleaseNull(cmsEncoder->hashAgilityV2AttrValues); + if(ortn) { + ortn = cmsRtnToOSStatus(ortn); + CSSM_PERROR("SecCmsSignerInfoAddAppleCodesigningHashAgilityV2", ortn); + break; + } + } + if (cmsEncoder->signedAttributes & kCMSAttrAppleExpirationTime) { + ortn = SecCmsSignerInfoAddAppleExpirationTime(signerInfo, cmsEncoder->expirationTime); + if(ortn) { + ortn = cmsRtnToOSStatus(ortn); + CSSM_PERROR("SecCmsSignerInfoAddAppleExpirationTime", ortn); + break; + } + } ortn = SecCmsSignedDataAddSignerInfo(signedData, signerInfo); if(ortn) { @@ -533,13 +571,10 @@ static OSStatus cmsSetupForSignedData( break; } - CFRELEASE(ourKc); CFRELEASE(ourCert); - ourKc = NULL; ourCert = NULL; } if(ortn) { - CFRELEASE(ourKc); CFRELEASE(ourCert); } return ortn; @@ -982,7 +1017,7 @@ OSStatus CMSEncoderAddSignedAttributes( if(cmsEncoder->encState != ES_Init) { return errSecParam; } - cmsEncoder->signedAttributes = signedAttributes; + cmsEncoder->signedAttributes |= signedAttributes; return errSecSuccess; } @@ -1004,6 +1039,55 @@ OSStatus CMSEncoderSetSigningTime( return errSecSuccess; } +/* + * Set the hash agility attribute for a CMSEncoder. + * This is only used if the kCMSAttrAppleCodesigningHashAgility attribute + * is included. + */ +OSStatus CMSEncoderSetAppleCodesigningHashAgility( + CMSEncoderRef cmsEncoder, + CFDataRef hashAgilityAttrValue) +{ + if (cmsEncoder == NULL || cmsEncoder->encState != ES_Init) { + return errSecParam; + } + cmsEncoder->hashAgilityAttrValue = CFRetainSafe(hashAgilityAttrValue); + return errSecSuccess; +} + +/* + * Set the hash agility attribute for a CMSEncoder. + * This is only used if the kCMSAttrAppleCodesigningHashAgilityV2 attribute + * is included. + */ +OSStatus CMSEncoderSetAppleCodesigningHashAgilityV2( + CMSEncoderRef cmsEncoder, + CFDictionaryRef hashAgilityV2AttrValues) +{ + if (cmsEncoder == NULL || cmsEncoder->encState != ES_Init) { + return errSecParam; + } + cmsEncoder->hashAgilityV2AttrValues = CFRetainSafe(hashAgilityV2AttrValues); + return errSecSuccess; +} + +/* + * Set the expiration time for a CMSEncoder. + * This is only used if the kCMSAttrAppleExpirationTime attribute is included. + */ +OSStatus CMSEncoderSetAppleExpirationTime( + CMSEncoderRef cmsEncoder, + CFAbsoluteTime time) +{ + if(cmsEncoder == NULL) { + return errSecParam; + } + if(cmsEncoder->encState != ES_Init) { + return errSecParam; + } + cmsEncoder->expirationTime = time; + return errSecSuccess; +} OSStatus CMSEncoderSetCertificateChainMode( CMSEncoderRef cmsEncoder, @@ -1020,6 +1104,7 @@ OSStatus CMSEncoderSetCertificateChainMode( case kCMSCertificateSignerOnly: case kCMSCertificateChain: case kCMSCertificateChainWithRoot: + case kCMSCertificateChainWithRootOrFail: break; default: return errSecParam;