2 * Copyright (c) 2006-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 // CodeSigner - SecCodeSigner API objects
27 #include "CodeSigner.h"
29 #include "csdatabase.h"
31 #include "csutilities.h"
32 #include <security_utilities/unix++.h>
33 #include <security_utilities/unixchild.h>
34 #include <Security/SecCertificate.h>
35 #include <Security/SecCertificatePriv.h>
40 __SEC_CFTYPE(SecIdentity
)
42 namespace CodeSigning
{
44 using namespace UnixPlusPlus
;
48 // A helper for parsing out a CFDictionary signing-data specification
50 class SecCodeSigner::Parser
: CFDictionary
{
52 Parser(SecCodeSigner
&signer
, CFDictionaryRef parameters
);
54 bool getBool(CFStringRef key
) const
56 if (CFBooleanRef flag
= get
<CFBooleanRef
>(key
))
57 return flag
== kCFBooleanTrue
;
65 // Construct a SecCodeSigner
67 SecCodeSigner::SecCodeSigner(SecCSFlags flags
)
68 : mOpFlags(flags
), mDigestAlgorithm(kSecCodeSignatureDefaultDigestAlgorithm
), mLimitedAsync(NULL
)
74 // Clean up a SecCodeSigner
76 SecCodeSigner::~SecCodeSigner() throw()
85 // Parse an input parameter dictionary and set ready-to-use parameters
87 void SecCodeSigner::parameters(CFDictionaryRef paramDict
)
89 Parser(*this, paramDict
);
91 MacOSError::throwMe(errSecCSInvalidObjectRef
);
95 // Retrieve the team ID from the signing certificate if and only if
96 // it is an apple developer signing cert
98 std::string
SecCodeSigner::getTeamIDFromSigner(CFArrayRef certs
)
100 if (mSigner
&& mSigner
!= SecIdentityRef(kCFNull
)) {
101 CFRef
<SecCertificateRef
> signerCert
;
102 MacOSError::check(SecIdentityCopyCertificate(mSigner
, &signerCert
.aref()));
104 /* Make sure the certificate looks like an Apple certificate, because we do not
105 extract the team ID from a non Apple certificate */
106 if (SecStaticCode::isAppleDeveloperCert(certs
)) {
107 CFRef
<CFStringRef
> teamIDFromCert
;
109 MacOSError::check(SecCertificateCopySubjectComponent(signerCert
.get(), &CSSMOID_OrganizationalUnitName
, &teamIDFromCert
.aref()));
112 return cfString(teamIDFromCert
);
120 // Roughly check for validity.
121 // This isn't thorough; it just sees if if looks like we've set up the object appropriately.
123 bool SecCodeSigner::valid() const
125 if (mOpFlags
& kSecCSRemoveSignature
)
134 void SecCodeSigner::sign(SecStaticCode
*code
, SecCSFlags flags
)
136 code
->setValidationFlags(flags
);
137 if (code
->isSigned() && (flags
& kSecCSSignPreserveSignature
))
139 Signer
operation(*this, code
);
140 if ((flags
| mOpFlags
) & kSecCSRemoveSignature
) {
141 secdebug("signer", "%p will remove signature from %p", this, code
);
142 operation
.remove(flags
);
145 MacOSError::throwMe(errSecCSInvalidObjectRef
);
146 secdebug("signer", "%p will sign %p (flags 0x%x)", this, code
, flags
);
147 operation
.sign(flags
);
149 code
->resetValidity();
154 // ReturnDetachedSignature is called by writers or editors that try to return
155 // detached signature data (rather than annotate the target).
157 void SecCodeSigner::returnDetachedSignature(BlobCore
*blob
, Signer
&signer
)
160 if (CFGetTypeID(mDetached
) == CFURLGetTypeID()) {
161 // URL to destination file
162 AutoFileDesc
fd(cfString(CFURLRef(mDetached
.get())), O_WRONLY
| O_CREAT
| O_TRUNC
);
164 } else if (CFGetTypeID(mDetached
) == CFDataGetTypeID()) {
165 CFDataAppendBytes(CFMutableDataRef(mDetached
.get()),
166 (const UInt8
*)blob
, blob
->length());
167 } else if (CFGetTypeID(mDetached
) == CFNullGetTypeID()) {
168 SignatureDatabaseWriter db
;
169 db
.storeCode(blob
, signer
.path().c_str());
176 // Our DiskRep::signingContext methods communicate with the signing subsystem
177 // in terms those callers can easily understand.
179 string
SecCodeSigner::sdkPath(const std::string
&path
) const
181 assert(path
[0] == '/'); // need absolute path here
183 return cfString(mSDKRoot
) + path
;
188 bool SecCodeSigner::isAdhoc() const
190 return mSigner
== SecIdentityRef(kCFNull
);
193 SecCSFlags
SecCodeSigner::signingFlags() const
200 // The actual parsing operation is done in the Parser class.
202 // Note that we need to copy or retain all incoming data. The caller has no requirement
203 // to keep the parameters dictionary around.
205 SecCodeSigner::Parser::Parser(SecCodeSigner
&state
, CFDictionaryRef parameters
)
206 : CFDictionary(parameters
, errSecCSBadDictionaryFormat
)
208 // the signer may be an identity or null
209 state
.mSigner
= SecIdentityRef(get
<CFTypeRef
>(kSecCodeSignerIdentity
));
211 if (CFGetTypeID(state
.mSigner
) != SecIdentityGetTypeID() && !CFEqual(state
.mSigner
, kCFNull
))
212 MacOSError::throwMe(errSecCSInvalidObjectRef
);
214 // the flags need some augmentation
215 if (CFNumberRef flags
= get
<CFNumberRef
>(kSecCodeSignerFlags
)) {
216 state
.mCdFlagsGiven
= true;
217 state
.mCdFlags
= cfNumber
<uint32_t>(flags
);
219 state
.mCdFlagsGiven
= false;
221 // digest algorithms are specified as a numeric code
222 if (CFNumberRef digestAlgorithm
= get
<CFNumberRef
>(kSecCodeSignerDigestAlgorithm
))
223 state
.mDigestAlgorithm
= cfNumber
<unsigned int>(digestAlgorithm
);
225 if (CFNumberRef cmsSize
= get
<CFNumberRef
>(CFSTR("cmssize")))
226 state
.mCMSSize
= cfNumber
<size_t>(cmsSize
);
228 state
.mCMSSize
= 9000; // likely big enough
230 // metadata preservation options
231 if (CFNumberRef preserve
= get
<CFNumberRef
>(kSecCodeSignerPreserveMetadata
)) {
232 state
.mPreserveMetadata
= cfNumber
<uint32_t>(preserve
);
234 state
.mPreserveMetadata
= 0;
236 // signing time can be a CFDateRef or null
237 if (CFTypeRef time
= get
<CFTypeRef
>(kSecCodeSignerSigningTime
)) {
238 if (CFGetTypeID(time
) == CFDateGetTypeID() || time
== kCFNull
)
239 state
.mSigningTime
= CFDateRef(time
);
241 MacOSError::throwMe(errSecCSInvalidObjectRef
);
244 if (CFStringRef ident
= get
<CFStringRef
>(kSecCodeSignerIdentifier
))
245 state
.mIdentifier
= cfString(ident
);
247 if (CFStringRef teamid
= get
<CFStringRef
>(kSecCodeSignerTeamIdentifier
))
248 state
.mTeamID
= cfString(teamid
);
250 if (CFNumberRef platform
= get
<CFNumberRef
>(kSecCodeSignerPlatformIdentifier
)) {
251 int64_t ident
= cfNumber
<int64_t>(platform
);
252 if (ident
< 0 || ident
> maxPlatform
) // overflow
253 MacOSError::throwMe(errSecCSInvalidPlatform
);
254 state
.mPlatform
= ident
;
257 if (CFStringRef prefix
= get
<CFStringRef
>(kSecCodeSignerIdentifierPrefix
))
258 state
.mIdentifierPrefix
= cfString(prefix
);
260 // Requirements can be binary or string (to be compiled).
261 // We must pass them along to the signer for possible text substitution
262 if (CFTypeRef reqs
= get
<CFTypeRef
>(kSecCodeSignerRequirements
)) {
263 if (CFGetTypeID(reqs
) == CFDataGetTypeID() || CFGetTypeID(reqs
) == CFStringGetTypeID())
264 state
.mRequirements
= reqs
;
266 MacOSError::throwMe(errSecCSInvalidObjectRef
);
268 state
.mRequirements
= NULL
;
270 state
.mNoMachO
= getBool(CFSTR("no-macho"));
272 state
.mPageSize
= get
<CFNumberRef
>(kSecCodeSignerPageSize
);
274 // detached can be (destination) file URL or (mutable) Data to be appended-to
275 if ((state
.mDetached
= get
<CFTypeRef
>(kSecCodeSignerDetached
))) {
276 CFTypeID type
= CFGetTypeID(state
.mDetached
);
277 if (type
!= CFURLGetTypeID() && type
!= CFDataGetTypeID() && type
!= CFNullGetTypeID())
278 MacOSError::throwMe(errSecCSInvalidObjectRef
);
281 state
.mDryRun
= getBool(kSecCodeSignerDryRun
);
283 state
.mResourceRules
= get
<CFDictionaryRef
>(kSecCodeSignerResourceRules
);
285 state
.mApplicationData
= get
<CFDataRef
>(kSecCodeSignerApplicationData
);
286 state
.mEntitlementData
= get
<CFDataRef
>(kSecCodeSignerEntitlements
);
288 state
.mSDKRoot
= get
<CFURLRef
>(kSecCodeSignerSDKRoot
);
290 if (CFBooleanRef timestampRequest
= get
<CFBooleanRef
>(kSecCodeSignerRequireTimestamp
)) {
291 state
.mWantTimeStamp
= timestampRequest
== kCFBooleanTrue
;
292 } else { // pick default
293 state
.mWantTimeStamp
= false;
294 if (state
.mSigner
&& state
.mSigner
!= SecIdentityRef(kCFNull
)) {
295 CFRef
<SecCertificateRef
> signerCert
;
296 MacOSError::check(SecIdentityCopyCertificate(state
.mSigner
, &signerCert
.aref()));
297 if (certificateHasField(signerCert
, devIdLeafMarkerOID
))
298 state
.mWantTimeStamp
= true;
301 state
.mTimestampAuthentication
= get
<SecIdentityRef
>(kSecCodeSignerTimestampAuthentication
);
302 state
.mTimestampService
= get
<CFURLRef
>(kSecCodeSignerTimestampServer
);
303 state
.mNoTimeStampCerts
= getBool(kSecCodeSignerTimestampOmitCertificates
);
307 } // end namespace CodeSigning
308 } // end namespace Security