2 * Copyright (c) 2006-2007 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 // SecStaticCode - API frame for SecStaticCode objects
28 #include "StaticCode.h"
29 #include <security_utilities/cfmunge.h>
33 using namespace CodeSigning
;
37 // CF-standard type code function
39 CFTypeID
SecStaticCodeGetTypeID(void)
42 return gCFObjects().StaticCode
.typeID
;
43 END_CSAPI1(_kCFRuntimeNotATypeID
)
48 // Create an StaticCode directly from disk path.
50 OSStatus
SecStaticCodeCreateWithPath(CFURLRef path
, SecCSFlags flags
, SecStaticCodeRef
*staticCodeRef
)
55 CodeSigning::Required(staticCodeRef
) = (new SecStaticCode(DiskRep::bestGuess(cfString(path
).c_str())))->handle();
60 const CFStringRef kSecCodeAttributeArchitecture
= CFSTR("architecture");
61 const CFStringRef kSecCodeAttributeSubarchitecture
=CFSTR("subarchitecture");
62 const CFStringRef kSecCodeAttributeBundleVersion
= CFSTR("bundleversion");
63 const CFStringRef kSecCodeAttributeUniversalFileOffset
= CFSTR("UniversalFileOffset");
65 OSStatus
SecStaticCodeCreateWithPathAndAttributes(CFURLRef path
, SecCSFlags flags
, CFDictionaryRef attributes
,
66 SecStaticCodeRef
*staticCodeRef
)
72 std::string version
; // holds memory placed into ctx
75 int archNumber
, subarchNumber
, offset
;
76 if (cfscan(attributes
, "{%O=%d}", kSecCodeAttributeUniversalFileOffset
, &offset
)) {
78 } else if (cfscan(attributes
, "{%O=%s}", kSecCodeAttributeArchitecture
, &archName
)) {
79 ctx
.arch
= Architecture(archName
.c_str());
80 } else if (cfscan(attributes
, "{%O=%d,%O=%d}",
81 kSecCodeAttributeArchitecture
, &archNumber
, kSecCodeAttributeSubarchitecture
, &subarchNumber
))
82 ctx
.arch
= Architecture(archNumber
, subarchNumber
);
83 else if (cfscan(attributes
, "{%O=%d}", kSecCodeAttributeArchitecture
, &archNumber
))
84 ctx
.arch
= Architecture(archNumber
);
85 if (cfscan(attributes
, "{%O=%s}", kSecCodeAttributeBundleVersion
, &version
))
86 ctx
.version
= version
.c_str();
89 CodeSigning::Required(staticCodeRef
) = (new SecStaticCode(DiskRep::bestGuess(cfString(path
).c_str(), &ctx
)))->handle();
96 // Check static validity of a StaticCode
98 OSStatus
SecStaticCodeCheckValidity(SecStaticCodeRef staticCodeRef
, SecCSFlags flags
,
99 SecRequirementRef requirementRef
)
101 return SecStaticCodeCheckValidityWithErrors(staticCodeRef
, flags
, requirementRef
, NULL
);
104 OSStatus
SecStaticCodeCheckValidityWithErrors(SecStaticCodeRef staticCodeRef
, SecCSFlags flags
,
105 SecRequirementRef requirementRef
, CFErrorRef
*errors
)
110 kSecCSCheckAllArchitectures
111 | kSecCSDoNotValidateExecutable
112 | kSecCSDoNotValidateResources
113 | kSecCSConsiderExpiration
114 | kSecCSEnforceRevocationChecks
115 | kSecCSCheckNestedCode
);
117 SecPointer
<SecStaticCode
> code
= SecStaticCode::requiredStatic(staticCodeRef
);
118 const SecRequirement
*req
= SecRequirement::optional(requirementRef
);
119 DTRACK(CODESIGN_EVAL_STATIC
, code
, (char*)code
->mainExecutablePath().c_str());
120 code
->staticValidate(flags
, req
);
127 // ====================================================================================
129 // The following API functions are called SecCode* but accept both SecCodeRef and
130 // SecStaticCodeRef arguments, operating on the implied SecStaticCodeRef as appropriate.
131 // Hence they're here, rather than in SecCode.cpp.
136 // Retrieve location information for an StaticCode.
138 OSStatus
SecCodeCopyPath(SecStaticCodeRef staticCodeRef
, SecCSFlags flags
, CFURLRef
*path
)
143 SecPointer
<SecStaticCode
> staticCode
= SecStaticCode::requiredStatic(staticCodeRef
);
144 CodeSigning::Required(path
) = staticCode
->canonicalPath();
151 // Fetch or make up a designated requirement
153 OSStatus
SecCodeCopyDesignatedRequirement(SecStaticCodeRef staticCodeRef
, SecCSFlags flags
,
154 SecRequirementRef
*requirementRef
)
159 const Requirement
*req
=
160 SecStaticCode::requiredStatic(staticCodeRef
)->designatedRequirement();
161 CodeSigning::Required(requirementRef
) = (new SecRequirement(req
))->handle();
168 // Fetch a particular internal requirement, if present
170 OSStatus
SecCodeCopyInternalRequirement(SecStaticCodeRef staticCodeRef
, SecRequirementType type
,
171 SecCSFlags flags
, SecRequirementRef
*requirementRef
)
176 const Requirement
*req
=
177 SecStaticCode::requiredStatic(staticCodeRef
)->internalRequirement(type
);
178 CodeSigning::Required(requirementRef
) = req
? (new SecRequirement(req
))->handle() : NULL
;
185 // Record for future use a detached code signature.
187 OSStatus
SecCodeSetDetachedSignature(SecStaticCodeRef codeRef
, CFDataRef signature
,
193 SecPointer
<SecStaticCode
> code
= SecStaticCode::requiredStatic(codeRef
);
195 code
->detachedSignature(signature
); // ... and pass it to the code
196 code
->resetValidity();
203 // Attach a code signature to a kernel memory mapping for page-in validation.
205 OSStatus
SecCodeMapMemory(SecStaticCodeRef codeRef
, SecCSFlags flags
)
210 SecPointer
<SecStaticCode
> code
= SecStaticCode::requiredStatic(codeRef
);
211 if (const CodeDirectory
*cd
= code
->codeDirectory(false)) {
212 fsignatures args
= { code
->diskRep()->signingBase(), (void *)cd
, cd
->length() };
213 UnixError::check(::fcntl(code
->diskRep()->fd(), F_ADDSIGS
, &args
));
215 MacOSError::throwMe(errSecCSUnsigned
);
222 // Attach a callback block to a code object
224 OSStatus
SecStaticCodeSetCallback(SecStaticCodeRef codeRef
, SecCSFlags flags
, SecCodeCallback
*old
, SecCodeCallback monitor
)
229 SecStaticCode
*code
= SecStaticCode::requiredStatic(codeRef
);
231 *old
= code
->monitor();
232 code
->setMonitor(monitor
);