2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
22 #include <Security/aclclient.h>
23 #include <Security/keychainacl.h>
24 #include <Security/cssmwalkers.h>
25 #include <Security/cssmdata.h>
26 #include <Security/cssmclient.h>
30 namespace CssmClient
{
33 static inline void check(CSSM_RETURN rc
)
35 ObjectImpl::check(rc
);
40 // AclBearer methods (trivial)
42 AclBearer::~AclBearer()
47 // Variant forms of AclBearer implemented in terms of its canonical virtual methods
49 void AclBearer::addAcl(const AclEntryInput
&input
, const CSSM_ACCESS_CREDENTIALS
*cred
)
51 changeAcl(AclEdit(input
), cred
);
54 void AclBearer::changeAcl(CSSM_ACL_HANDLE handle
, const AclEntryInput
&input
,
55 const CSSM_ACCESS_CREDENTIALS
*cred
)
57 changeAcl(AclEdit(handle
, input
), cred
);
60 void AclBearer::deleteAcl(CSSM_ACL_HANDLE handle
, const CSSM_ACCESS_CREDENTIALS
*cred
)
62 changeAcl(AclEdit(handle
), cred
);
65 void AclBearer::deleteAcl(const char *tag
, const CSSM_ACCESS_CREDENTIALS
*cred
)
67 AutoAclEntryInfoList entries
;
69 for (uint32 n
= 0; n
< entries
.count(); n
++)
70 deleteAcl(entries
[n
].handle(), cred
);
75 // KeyAclBearer implementation
77 void KeyAclBearer::getAcl(AutoAclEntryInfoList
&aclInfos
, const char *selectionTag
) const
79 aclInfos
.allocator(allocator
);
80 check(CSSM_GetKeyAcl(csp
, &key
, reinterpret_cast<const CSSM_STRING
*>(selectionTag
), aclInfos
, aclInfos
));
83 void KeyAclBearer::changeAcl(const CSSM_ACL_EDIT
&aclEdit
, const CSSM_ACCESS_CREDENTIALS
*cred
)
85 check(CSSM_ChangeKeyAcl(csp
, AccessCredentials::needed(cred
), &aclEdit
, &key
));
88 void KeyAclBearer::getOwner(AutoAclOwnerPrototype
&owner
) const
90 owner
.allocator(allocator
);
91 check(CSSM_GetKeyOwner(csp
, &key
, owner
));
94 void KeyAclBearer::changeOwner(const CSSM_ACL_OWNER_PROTOTYPE
&newOwner
,
95 const CSSM_ACCESS_CREDENTIALS
*cred
)
97 check(CSSM_ChangeKeyOwner(csp
, AccessCredentials::needed(cred
), &key
, &newOwner
));
102 // A single global structure containing pseudo-static data
106 CssmAllocator
&alloc
;
108 AutoCredentials nullCred
;
109 AutoCredentials promptCred
;
110 AutoCredentials unlockCred
;
114 ModuleNexus
<Statics
> statics
;
119 // Make pseudo-statics.
120 // Note: This is an eternal object. It is not currently destroyed
121 // if the containing code is unloaded. But then, the containing
122 // code is Security.framework, which never unloads anyway.
125 : alloc(CssmAllocator::standard()),
127 promptCred(alloc
, 2),
130 // nullCred: nothing at all
132 // an empty THRESHOLD sample to match threshold subjects with "free" subjects
133 nullCred
.sample(0) = TypedList(alloc
, CSSM_SAMPLE_TYPE_THRESHOLD
);
135 // promptCred: a credential permitting user prompt confirmations
137 // a KEYCHAIN_PROMPT sample, both by itself and in a THRESHOLD
138 promptCred
.sample(0) = TypedList(alloc
, CSSM_SAMPLE_TYPE_KEYCHAIN_PROMPT
);
139 promptCred
.sample(1) = TypedList(alloc
, CSSM_SAMPLE_TYPE_THRESHOLD
,
140 new(alloc
) ListElement(TypedList(alloc
, CSSM_SAMPLE_TYPE_KEYCHAIN_PROMPT
)));
143 unlockCred
.sample(0) = TypedList(alloc
, CSSM_SAMPLE_TYPE_KEYCHAIN_LOCK
,
144 new(alloc
) ListElement(CSSM_SAMPLE_TYPE_KEYCHAIN_PROMPT
));
149 // Make and break AclFactories
151 AclFactory::AclFactory()
154 AclFactory::~AclFactory()
159 // Return basic pseudo-static values
161 const AccessCredentials
*AclFactory::nullCred() const
162 { return &statics().nullCred
; }
164 const AccessCredentials
*AclFactory::promptCred() const
165 { return &statics().promptCred
; }
167 const AccessCredentials
*AclFactory::unlockCred() const
168 { return &statics().unlockCred
; }
172 // Manage the (pseudo) credentials used to explicitly provide a passphrase to a keychain.
173 // Use the eternal unlockCred() for normal (protected prompt) unlocking.
175 AclFactory::KeychainCredentials::~KeychainCredentials ()
177 DataWalkers::chunkFree (mCredentials
, allocator
);
180 AclFactory::PassphraseUnlockCredentials::PassphraseUnlockCredentials (const CssmData
& password
,
181 CssmAllocator
& allocator
) : KeychainCredentials(allocator
)
183 mCredentials
->sample(0) = TypedList(allocator
, CSSM_SAMPLE_TYPE_KEYCHAIN_LOCK
,
184 new (allocator
) ListElement (CSSM_SAMPLE_TYPE_PASSWORD
),
185 new (allocator
) ListElement (CssmAutoData(allocator
, password
).release()));
190 // Manage the (pseudo) credentials used to explicitly change a keychain's passphrase
192 AclFactory::PasswordChangeCredentials::PasswordChangeCredentials (const CssmData
& password
,
193 CssmAllocator
& allocator
) : KeychainCredentials(allocator
)
195 mCredentials
->sample(0) = TypedList(allocator
, CSSM_SAMPLE_TYPE_KEYCHAIN_CHANGE_LOCK
,
196 new (allocator
) ListElement (CSSM_SAMPLE_TYPE_PASSWORD
),
197 new (allocator
) ListElement (CssmAutoData(allocator
, password
).release()));
202 // Create an ANY style AclEntryInput.
203 // This can be used to explicitly request wide-open authorization on a new CSSM object.
205 AclFactory::AnyResourceContext::AnyResourceContext(const CSSM_ACCESS_CREDENTIALS
*cred
)
206 : mAny(CSSM_ACL_SUBJECT_TYPE_ANY
), mTag(CSSM_ACL_AUTHORIZATION_ANY
)
208 // set up an ANY/EVERYTHING AclEntryInput
209 input().proto().subject() += &mAny
;
210 AuthorizationGroup
&authGroup
= input().proto().authorization();
211 authGroup
.NumberOfAuthTags
= 1;
212 authGroup
.AuthTags
= &mTag
;
214 // install the cred (not copied)
219 } // end namespace CssmClient
220 } // end namespace Security