]> git.saurik.com Git - apple/security.git/blob - libsecurity_keychain/lib/TrustedApplication.cpp
Security-55163.44.tar.gz
[apple/security.git] / libsecurity_keychain / lib / TrustedApplication.cpp
1 /*
2 * Copyright (c) 2002-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 //
25 // TrustedApplication.cpp
26 //
27 #include <security_keychain/TrustedApplication.h>
28 #include <security_keychain/ACL.h>
29 #include <security_utilities/osxcode.h>
30 #include <security_utilities/trackingallocator.h>
31 #include <security_cdsa_utilities/acl_codesigning.h>
32 #include <sys/syslimits.h>
33 #include <memory>
34
35 using namespace KeychainCore;
36
37
38 //
39 // Create a TrustedApplication from a code-signing ACL subject.
40 // Throws ACL::ParseError if the subject is unexpected.
41 //
42 TrustedApplication::TrustedApplication(const TypedList &subject)
43 {
44 try {
45 CodeSignatureAclSubject::Maker maker;
46 mForm = maker.make(subject);
47 secdebug("trustedapp", "%p created from list form", this);
48 IFDUMPING("codesign", mForm->AclSubject::dump("STApp created from list"));
49 } catch (...) {
50 throw ACL::ParseError();
51 }
52 }
53
54
55 //
56 // Create a TrustedApplication from a path-to-object-on-disk
57 //
58 TrustedApplication::TrustedApplication(const std::string &path)
59 {
60 RefPointer<OSXCode> code(OSXCode::at(path));
61 mForm = new CodeSignatureAclSubject(OSXVerifier(code));
62 secdebug("trustedapp", "%p created from path %s", this, path.c_str());
63 IFDUMPING("codesign", mForm->AclSubject::dump("STApp created from path"));
64 }
65
66
67 //
68 // Create a TrustedAppliation for the calling process
69 //
70 TrustedApplication::TrustedApplication()
71 {
72 //@@@@ should use CS's idea of "self"
73 RefPointer<OSXCode> me(OSXCode::main());
74 mForm = new CodeSignatureAclSubject(OSXVerifier(me));
75 secdebug("trustedapp", "%p created from self", this);
76 IFDUMPING("codesign", mForm->AclSubject::dump("STApp created from self"));
77 }
78
79
80 //
81 // Create a TrustedApplication from a SecRequirementRef.
82 // Note that the path argument is only stored for documentation;
83 // it is NOT used to denote anything on disk.
84 //
85 TrustedApplication::TrustedApplication(const std::string &path, SecRequirementRef reqRef)
86 {
87 CFRef<CFDataRef> reqData;
88 MacOSError::check(SecRequirementCopyData(reqRef, kSecCSDefaultFlags, &reqData.aref()));
89 mForm = new CodeSignatureAclSubject(NULL, path);
90 mForm->add((const BlobCore *)CFDataGetBytePtr(reqData));
91 secdebug("trustedapp", "%p created from path %s and requirement %p",
92 this, path.c_str(), reqRef);
93 IFDUMPING("codesign", mForm->debugDump());
94 }
95
96
97 TrustedApplication::~TrustedApplication()
98 { /* virtual */ }
99
100
101 //
102 // Convert from/to external data form.
103 //
104 // Since a TrustedApplication's data is essentially a CodeSignatureAclSubject,
105 // we just use the subject's externalizer to produce the data. That requires us
106 // to use the somewhat idiosyncratic linearizer used by CSSM ACL subjects, but
107 // that's a small price to pay for consistency.
108 //
109 TrustedApplication::TrustedApplication(CFDataRef external)
110 {
111 AclSubject::Reader pubReader(CFDataGetBytePtr(external)), privReader;
112 mForm = CodeSignatureAclSubject::Maker().make(0, pubReader, privReader);
113 }
114
115 CFDataRef TrustedApplication::externalForm() const
116 {
117 AclSubject::Writer::Counter pubCounter, privCounter;
118 mForm->exportBlob(pubCounter, privCounter);
119 if (privCounter > 0) // private exported data - format violation
120 CssmError::throwMe(CSSMERR_CSSM_INTERNAL_ERROR);
121 CFRef<CFMutableDataRef> data = CFDataCreateMutable(NULL, pubCounter);
122 CFDataSetLength(data, pubCounter);
123 if (CFDataGetLength(data) < CFIndex(pubCounter))
124 CFError::throwMe();
125 AclSubject::Writer pubWriter(CFDataGetMutableBytePtr(data)), privWriter;
126 mForm->exportBlob(pubWriter, privWriter);
127 return data.yield();
128 }
129
130
131 //
132 // Direct verification interface.
133 // If path == NULL, we verify against the running code itself.
134 //
135 bool TrustedApplication::verifyToDisk(const char *path)
136 {
137 if (SecRequirementRef requirement = mForm->requirement()) {
138 secdebug("trustedapp", "%p validating requirement against path %s", this, path);
139 CFRef<SecStaticCodeRef> ondisk;
140 if (path)
141 MacOSError::check(SecStaticCodeCreateWithPath(CFTempURL(path),
142 kSecCSDefaultFlags, &ondisk.aref()));
143 else
144 MacOSError::check(SecCodeCopySelf(kSecCSDefaultFlags, (SecCodeRef *)&ondisk.aref()));
145 return SecStaticCodeCheckValidity(ondisk, kSecCSDefaultFlags, requirement) == noErr;
146 } else {
147 secdebug("trustedapp", "%p validating hash against path %s", this, path);
148 RefPointer<OSXCode> code = path ? OSXCode::at(path) : OSXCode::main();
149 SHA1::Digest ondiskDigest;
150 OSXVerifier::makeLegacyHash(code, ondiskDigest);
151 return memcmp(ondiskDigest, mForm->legacyHash(), sizeof(ondiskDigest)) == 0;
152 }
153 }
154
155
156 //
157 // Produce a TypedList representing a code-signing ACL subject
158 // for this application.
159 // Memory is allocated from the allocator given, and belongs to
160 // the caller.
161 //
162 CssmList TrustedApplication::makeSubject(Allocator &allocator)
163 {
164 return mForm->toList(allocator);
165 }
166
167
168 //
169 // On a completely different note...
170 // Read a simple text file from disk and cache the lines in a set.
171 // This is used during re-prebinding to cut down on the number of
172 // equivalency records being generated.
173 // This feature is otherwise completely unconnected to anything else here.
174 //
175 PathDatabase::PathDatabase(const char *path)
176 {
177 if (FILE *f = fopen(path, "r")) {
178 mQualifyAll = false;
179 char path[PATH_MAX+1];
180 while (fgets(path, sizeof(path), f)) {
181 path[strlen(path)-1] = '\0'; // strip NL
182 mPaths.insert(path);
183 }
184 fclose(f);
185 secdebug("equivdb", "read %ld paths from %s", mPaths.size(), path);
186 } else {
187 mQualifyAll = true;
188 secdebug("equivdb", "cannot open %s, will qualify all application paths", path);
189 }
190 }
191
192
193 bool PathDatabase::lookup(const string &inPath)
194 {
195 string path = inPath;
196 string::size_type lastSlash = path.rfind('/');
197 string::size_type bundleCore = path.find("/Contents/MacOS/");
198 if (lastSlash != string::npos && bundleCore != string::npos)
199 if (bundleCore + 15 == lastSlash)
200 path = path.substr(0, bundleCore); // @@@ path is being modified here so it can't be const.
201 return mPaths.find(path) != mPaths.end();
202 }