]> git.saurik.com Git - apple/security.git/blob - Keychain/TrustedApplication.cpp
b2a532cdf5afaedade44be4cab6bfeb09e17920b
[apple/security.git] / Keychain / TrustedApplication.cpp
1 /*
2 * Copyright (c) 2002 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18 //
19 // TrustedApplication.cpp
20 //
21 #include <Security/TrustedApplication.h>
22 #include <Security/ACL.h>
23 #include <Security/osxsigning.h>
24 #include <Security/osxsigner.h>
25 #include <Security/trackingallocator.h>
26 #include <memory>
27
28 using namespace KeychainCore;
29 using namespace CodeSigning;
30
31
32 //
33 // Create a TrustedApplication from a code-signing ACL subject.
34 // Throws ACL::ParseError if the subject is unexpected.
35 //
36 TrustedApplication::TrustedApplication(const TypedList &subject)
37 : mSignature(CssmAllocator::standard()),
38 mData(CssmAllocator::standard())
39 {
40 if (subject.type() != CSSM_ACL_SUBJECT_TYPE_CODE_SIGNATURE)
41 throw ACL::ParseError();
42 if (subject[1] != CSSM_ACL_CODE_SIGNATURE_OSX)
43 throw ACL::ParseError();
44 mSignature = subject[2].data();
45 mData = subject[3].data();
46 }
47
48
49 TrustedApplication::TrustedApplication(const CssmData &signature, const CssmData &data) :
50 mSignature(CssmAllocator::standard(), signature),
51 mData(CssmAllocator::standard(), data)
52 {
53 }
54
55 TrustedApplication::TrustedApplication(const char *path)
56 : mSignature(CssmAllocator::standard()),
57 mData(CssmAllocator::standard())
58 {
59 OSXSigner signer;
60 RefPointer<OSXCode> object(OSXCode::at(path));
61 auto_ptr<OSXSigner::OSXSignature> signature(signer.sign(*object));
62 mSignature = *signature;
63 mData = CssmData(const_cast<char *>(path), strlen(path) + 1);
64 }
65
66 TrustedApplication::TrustedApplication()
67 : mSignature(CssmAllocator::standard()),
68 mData(CssmAllocator::standard())
69 {
70 OSXSigner signer;
71 RefPointer<OSXCode> object(OSXCode::main());
72 auto_ptr<OSXSigner::OSXSignature> signature(signer.sign(*object));
73 mSignature = *signature;
74 string path = object->canonicalPath();
75 mData.copy(path.c_str(), path.length() + 1); // including trailing null
76 }
77
78 TrustedApplication::~TrustedApplication()
79 {
80 }
81
82 const CssmData &
83 TrustedApplication::signature() const
84 {
85 return mSignature;
86 }
87
88 bool
89 TrustedApplication::sameSignature(const char *path)
90 {
91 // return true if object at given path has same signature
92 CssmAutoData otherSignature(CssmAllocator::standard());
93 calcSignature(path, otherSignature);
94 return (mSignature.get() == otherSignature);
95 }
96
97 void
98 TrustedApplication::calcSignature(const char *path, CssmOwnedData &signature)
99 {
100 // generate a signature for the given object
101 RefPointer<CodeSigning::OSXCode> objToVerify(CodeSigning::OSXCode::at(path));
102 CodeSigning::OSXSigner signer;
103 auto_ptr<CodeSigning::OSXSigner::OSXSignature> osxSignature(signer.sign(*objToVerify));
104 signature.copy(osxSignature->data(), osxSignature->length());
105 }
106
107
108 //
109 // Produce a TypedList representing a code-signing ACL subject
110 // for this application.
111 // Memory is allocated from the allocator given, and belongs to
112 // the caller.
113 //
114 TypedList TrustedApplication::makeSubject(CssmAllocator &allocator)
115 {
116 return TypedList(allocator,
117 CSSM_ACL_SUBJECT_TYPE_CODE_SIGNATURE,
118 new(allocator) ListElement(CSSM_ACL_CODE_SIGNATURE_OSX),
119 new(allocator) ListElement(mSignature.get()),
120 new(allocator) ListElement(mData.get()));
121 }