2 * Copyright (c) 2006-2010 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 "reqparser.h"
31 #include "csdatabase.h"
33 #include "csutilities.h"
34 #include <security_utilities/unix++.h>
35 #include <security_utilities/unixchild.h>
36 #include <Security/SecCertificate.h>
41 __SEC_CFTYPE(SecIdentity
)
43 namespace CodeSigning
{
45 using namespace UnixPlusPlus
;
49 // A helper for parsing out a CFDictionary signing-data specification
51 class SecCodeSigner::Parser
: CFDictionary
{
53 Parser(SecCodeSigner
&signer
, CFDictionaryRef parameters
);
55 bool getBool(CFStringRef key
) const
57 if (CFBooleanRef flag
= get
<CFBooleanRef
>(key
))
58 return flag
== kCFBooleanTrue
;
66 // Construct a SecCodeSigner
68 SecCodeSigner::SecCodeSigner(SecCSFlags flags
)
69 : mOpFlags(flags
), mRequirements(NULL
), mDigestAlgorithm(kSecCodeSignatureDefaultDigestAlgorithm
)
75 // Clean up a SecCodeSigner
77 SecCodeSigner::~SecCodeSigner() throw()
79 ::free((Requirements
*)mRequirements
);
86 // Parse an input parameter dictionary and set ready-to-use parameters
88 void SecCodeSigner::parameters(CFDictionaryRef paramDict
)
90 Parser(*this, paramDict
);
92 MacOSError::throwMe(errSecCSInvalidObjectRef
);
97 // Roughly check for validity.
98 // This isn't thorough; it just sees if if looks like we've set up the object appropriately.
100 bool SecCodeSigner::valid() const
102 if (mOpFlags
& kSecCSRemoveSignature
)
111 void SecCodeSigner::sign(SecStaticCode
*code
, SecCSFlags flags
)
113 Signer
operation(*this, code
);
114 if ((flags
| mOpFlags
) & kSecCSRemoveSignature
) {
115 secdebug("signer", "%p will remove signature from %p", this, code
);
116 operation
.remove(flags
);
119 MacOSError::throwMe(errSecCSInvalidObjectRef
);
120 secdebug("signer", "%p will sign %p (flags 0x%x)", this, code
, flags
);
121 operation
.sign(flags
);
123 code
->resetValidity();
128 // ReturnDetachedSignature is called by writers or editors that try to return
129 // detached signature data (rather than annotate the target).
131 void SecCodeSigner::returnDetachedSignature(BlobCore
*blob
, Signer
&signer
)
134 if (CFGetTypeID(mDetached
) == CFURLGetTypeID()) {
135 // URL to destination file
136 AutoFileDesc
fd(cfString(CFURLRef(mDetached
.get())), O_WRONLY
| O_CREAT
| O_TRUNC
);
138 } else if (CFGetTypeID(mDetached
) == CFDataGetTypeID()) {
139 CFDataAppendBytes(CFMutableDataRef(mDetached
.get()),
140 (const UInt8
*)blob
, blob
->length());
141 } else if (CFGetTypeID(mDetached
) == CFNullGetTypeID()) {
142 signatureDatabaseWriter().storeCode(blob
, signer
.path().c_str());
149 // Our DiskRep::signingContext methods communicate with the signing subsystem
150 // in terms those callers can easily understand.
152 string
SecCodeSigner::sdkPath(const std::string
&path
) const
154 assert(path
[0] == '/'); // need absolute path here
156 return cfString(mSDKRoot
) + path
;
161 bool SecCodeSigner::isAdhoc() const
163 return mSigner
== SecIdentityRef(kCFNull
);
168 // The actual parsing operation is done in the Parser class.
170 // Note that we need to copy or retain all incoming data. The caller has no requirement
171 // to keep the parameters dictionary around.
173 SecCodeSigner::Parser::Parser(SecCodeSigner
&state
, CFDictionaryRef parameters
)
174 : CFDictionary(parameters
, errSecCSBadDictionaryFormat
)
176 // the signer may be an identity or null
177 state
.mSigner
= SecIdentityRef(get
<CFTypeRef
>(kSecCodeSignerIdentity
));
179 if (CFGetTypeID(state
.mSigner
) != SecIdentityGetTypeID() && !CFEqual(state
.mSigner
, kCFNull
))
180 MacOSError::throwMe(errSecCSInvalidObjectRef
);
182 // the flags need some augmentation
183 if (CFNumberRef flags
= get
<CFNumberRef
>(kSecCodeSignerFlags
)) {
184 state
.mCdFlagsGiven
= true;
185 state
.mCdFlags
= cfNumber
<uint32_t>(flags
);
187 state
.mCdFlagsGiven
= false;
189 // digest algorithms are specified as a numeric code
190 if (CFNumberRef digestAlgorithm
= get
<CFNumberRef
>(kSecCodeSignerDigestAlgorithm
))
191 state
.mDigestAlgorithm
= cfNumber
<unsigned int>(digestAlgorithm
);
193 if (CFNumberRef cmsSize
= get
<CFNumberRef
>(CFSTR("cmssize")))
194 state
.mCMSSize
= cfNumber
<size_t>(cmsSize
);
196 state
.mCMSSize
= 9000; // likely big enough
198 // signing time can be a CFDateRef or null
199 if (CFTypeRef time
= get
<CFTypeRef
>(kSecCodeSignerSigningTime
)) {
200 if (CFGetTypeID(time
) == CFDateGetTypeID() || time
== kCFNull
)
201 state
.mSigningTime
= CFDateRef(time
);
203 MacOSError::throwMe(errSecCSInvalidObjectRef
);
206 if (CFStringRef ident
= get
<CFStringRef
>(kSecCodeSignerIdentifier
))
207 state
.mIdentifier
= cfString(ident
);
209 if (CFStringRef prefix
= get
<CFStringRef
>(kSecCodeSignerIdentifierPrefix
))
210 state
.mIdentifierPrefix
= cfString(prefix
);
212 // requirements can be binary or string (to be compiled)
213 if (CFTypeRef reqs
= get
<CFTypeRef
>(kSecCodeSignerRequirements
)) {
214 if (CFGetTypeID(reqs
) == CFDataGetTypeID()) { // binary form
215 const Requirements
*rp
= (const Requirements
*)CFDataGetBytePtr(CFDataRef(reqs
));
216 state
.mRequirements
= rp
->clone();
217 } else if (CFGetTypeID(reqs
) == CFStringGetTypeID()) { // text form
218 state
.mRequirements
= parseRequirements(cfString(CFStringRef(reqs
)));
220 MacOSError::throwMe(errSecCSInvalidObjectRef
);
222 state
.mRequirements
= NULL
;
224 state
.mNoMachO
= getBool(CFSTR("no-macho"));
226 state
.mPageSize
= get
<CFNumberRef
>(kSecCodeSignerPageSize
);
228 // detached can be (destination) file URL or (mutable) Data to be appended-to
229 if ((state
.mDetached
= get
<CFTypeRef
>(kSecCodeSignerDetached
))) {
230 CFTypeID type
= CFGetTypeID(state
.mDetached
);
231 if (type
!= CFURLGetTypeID() && type
!= CFDataGetTypeID() && type
!= CFNullGetTypeID())
232 MacOSError::throwMe(errSecCSInvalidObjectRef
);
235 state
.mDryRun
= getBool(kSecCodeSignerDryRun
);
237 state
.mResourceRules
= get
<CFDictionaryRef
>(kSecCodeSignerResourceRules
);
239 state
.mApplicationData
= get
<CFDataRef
>(kSecCodeSignerApplicationData
);
240 state
.mEntitlementData
= get
<CFDataRef
>(kSecCodeSignerEntitlements
);
242 state
.mSDKRoot
= get
<CFURLRef
>(kSecCodeSignerSDKRoot
);
244 if (CFBooleanRef timestampRequest
= get
<CFBooleanRef
>(kSecCodeSignerRequireTimestamp
)) {
245 state
.mWantTimeStamp
= timestampRequest
== kCFBooleanTrue
;
246 } else { // pick default
247 state
.mWantTimeStamp
= false;
248 if (state
.mSigner
&& state
.mSigner
!= SecIdentityRef(kCFNull
)) {
249 CFRef
<SecCertificateRef
> signerCert
;
250 MacOSError::check(SecIdentityCopyCertificate(state
.mSigner
, &signerCert
.aref()));
251 if (certificateHasField(signerCert
, devIdLeafMarkerOID
))
252 state
.mWantTimeStamp
= true;
255 state
.mTimestampAuthentication
= get
<SecIdentityRef
>(kSecCodeSignerTimestampAuthentication
);
256 state
.mTimestampService
= get
<CFURLRef
>(kSecCodeSignerTimestampServer
);
257 state
.mNoTimeStampCerts
= getBool(kSecCodeSignerTimestampOmitCertificates
);
261 } // end namespace CodeSigning
262 } // end namespace Security