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.
20 // cssmacl - core ACL management interface
26 #include <Security/cssmacl.h>
27 #include <Security/debugging.h>
31 using namespace DataWalkers
;
35 // The static map of available ACL subject makers.
36 // These are the kinds of ACL subjects we can deal with.
38 ModuleNexus
<ObjectAcl::MakerMap
> ObjectAcl::makers
;
42 // Common (basic) features of AclSubjects
44 AclSubject::~AclSubject()
47 AclValidationEnvironment::~AclValidationEnvironment()
50 void AclSubject::exportBlob(Writer::Counter
&, Writer::Counter
&)
53 void AclSubject::exportBlob(Writer
&, Writer
&)
56 void AclSubject::importBlob(Reader
&, Reader
&)
59 AclSubject::Maker::~Maker()
64 // A SimpleAclSubject accepts only a single type of sample, validates
65 // samples independently, and makes no use of certificates.
67 bool SimpleAclSubject::validate(const AclValidationContext
&ctx
) const
69 for (uint32 n
= 0; n
< ctx
.count(); n
++) {
70 const TypedList
&sample
= ctx
[n
];
71 if (!sample
.isProper())
72 CssmError::throwMe(CSSM_ERRCODE_INVALID_SAMPLE_VALUE
);
73 if (sample
.type() == acceptingSamples
&& validate(ctx
, sample
))
74 return true; // matched this sample; validation successful
81 // Create an ObjectAcl
83 ObjectAcl::ObjectAcl(CssmAllocator
&alloc
) : allocator(alloc
), nextHandle(1)
87 ObjectAcl::ObjectAcl(const AclEntryPrototype
&proto
, CssmAllocator
&alloc
)
88 : allocator(alloc
), nextHandle(1)
90 cssmSetInitial(proto
);
93 ObjectAcl::~ObjectAcl()
98 // Set an "initial ACL" from a CSSM-style initial ACL argument.
99 // This will replace the owner, as well as replace the entire ACL
100 // with a single-item slot, as per CSSM specification.
102 void ObjectAcl::cssmSetInitial(const AclEntryPrototype
&proto
)
104 owner
= OwnerEntry(proto
);
105 entries
.insert(EntryMap::value_type("", proto
))->second
.handle
= nextHandle
++;
108 void ObjectAcl::cssmSetInitial(const AclSubjectPointer
&subject
)
110 owner
= OwnerEntry(subject
);
111 entries
.insert(EntryMap::value_type("", subject
))->second
.handle
= nextHandle
++;
114 ObjectAcl::Entry::~Entry()
118 AclValidationContext::~AclValidationContext()
123 // ObjectAcl::validate validates an access authorization claim.
124 // Returns normally if 'auth' is granted to the bearer of 'cred'.
125 // Otherwise, throws a suitable (ACL-related) CssmError exception.
126 // @@@ Should it return a reference to the Entry that granted access?
128 class BaseValidationContext
: public AclValidationContext
{
130 BaseValidationContext(const AccessCredentials
*cred
,
131 AclAuthorization auth
, AclValidationEnvironment
*env
)
132 : AclValidationContext(cred
, auth
, env
) { }
134 uint32
count() const { return mCred
? mCred
->samples().length() : 0; }
135 const TypedList
&sample(uint32 n
) const
136 { assert(n
< count()); return mCred
->samples()[n
]; }
139 void ObjectAcl::validate(AclAuthorization auth
, const AccessCredentials
*cred
,
140 AclValidationEnvironment
*env
) const
142 //@@@ should pre-screen based on requested auth, maybe?
143 BaseValidationContext
ctx(cred
, auth
, env
);
145 #if defined(ACL_OMNIPOTENT_OWNER)
146 // try owner (owner can do anything)
147 if (owner
.validate(ctx
))
149 #endif ACL_OMNIPOTENT_OWNER
151 // try applicable ACLs
152 pair
<ConstIterator
, ConstIterator
> range
;
153 if (getRange(cred
->EntryTag
, range
) == 0) // no such tag
154 CssmError::throwMe(CSSM_ERRCODE_ACL_ENTRY_TAG_NOT_FOUND
);
155 // try entries in turn
156 for (ConstIterator it
= range
.first
; it
!= range
.second
; it
++) {
157 const AclEntry
&slot
= it
->second
;
158 if (slot
.authorizes(ctx
.authorization()) && slot
.validate(ctx
))
161 CssmError::throwMe(CSSM_ERRCODE_OPERATION_AUTH_DENIED
); //@@@ imprecise
164 void ObjectAcl::validateOwner(AclAuthorization authorizationHint
,
165 const AccessCredentials
*cred
, AclValidationEnvironment
*env
) const
167 BaseValidationContext
ctx(cred
, authorizationHint
, env
);
168 if (owner
.validate(ctx
))
170 CssmError::throwMe(CSSM_ERRCODE_OPERATION_AUTH_DENIED
);
175 // Export an ObjectAcl to two memory blobs: public and private data separated.
176 // This is a standard two-pass size+copy operation.
178 void ObjectAcl::exportBlob(CssmData
&publicBlob
, CssmData
&privateBlob
)
180 Writer::Counter pubSize
, privSize
;
181 uint32 entryCount
= entries
.size();
182 owner
.exportBlob(pubSize
, privSize
);
184 for (Iterator it
= begin(); it
!= end(); it
++)
185 it
->second
.exportBlob(pubSize
, privSize
);
186 publicBlob
= CssmData(allocator
.malloc(pubSize
), pubSize
);
187 privateBlob
= CssmData(allocator
.malloc(privSize
), privSize
);
188 Writer
pubWriter(publicBlob
), privWriter(privateBlob
);
189 owner
.exportBlob(pubWriter
, privWriter
);
190 pubWriter(entryCount
);
191 for (Iterator it
= begin(); it
!= end(); it
++)
192 it
->second
.exportBlob(pubWriter
, privWriter
);
197 // Import an ObjectAcl's contents from two memory blobs representing public and
198 // private contents, respectively. These blobs must have been generated by the
200 // Prior contents (if any) are deleted and replaced.
202 void ObjectAcl::importBlob(const void *publicBlob
, const void *privateBlob
)
204 Reader
pubReader(publicBlob
), privReader(privateBlob
);
205 owner
.importBlob(pubReader
, privReader
);
206 uint32 entryCount
; pubReader(entryCount
);
207 entries
.erase(begin(), end());
208 for (uint32 n
= 0; n
< entryCount
; n
++) {
210 newEntry
.importBlob(pubReader
, privReader
);
211 entries
.insert(EntryMap::value_type(newEntry
.tag
, newEntry
))->second
.handle
= nextHandle
++;
213 IFDUMPING("acl", debugDump("imported"));
218 // ACL utility methods
220 unsigned int ObjectAcl::getRange(const char *tag
, pair
<ConstIterator
, ConstIterator
> &range
) const
223 range
= entries
.equal_range(tag
);
224 uint32 count
= entries
.count(tag
);
226 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_ENTRY_TAG
);
229 range
.first
= entries
.begin();
230 range
.second
= entries
.end();
231 return entries
.size();
235 ObjectAcl::Iterator
ObjectAcl::findEntryHandle(CSSM_ACL_HANDLE handle
)
237 for (Iterator it
= entries
.begin(); it
!= entries
.end(); it
++)
238 if (it
->second
.handle
== handle
)
240 CssmError::throwMe(CSSMERR_CSSM_INVALID_HANDLE_USAGE
); //%%% imprecise error code
245 // CSSM style ACL access and modification functions.
247 void ObjectAcl::cssmGetAcl(const char *tag
, uint32
&count
, AclEntryInfo
* &acls
)
249 pair
<ConstIterator
, ConstIterator
> range
;
250 count
= getRange(tag
, range
);
251 acls
= allocator
.alloc
<AclEntryInfo
>(count
);
253 for (ConstIterator it
= range
.first
; it
!= range
.second
; it
++, n
++) {
254 acls
[n
].EntryHandle
= it
->second
.handle
;
255 it
->second
.toEntryInfo(acls
[n
].EntryPublicInfo
, allocator
);
260 void ObjectAcl::cssmChangeAcl(const AclEdit
&edit
,
261 const AccessCredentials
*cred
, AclValidationEnvironment
*env
)
263 IFDUMPING("acl", debugDump("acl-change-from"));
265 // validate access credentials
266 validateOwner(CSSM_ACL_AUTHORIZATION_CHANGE_ACL
, cred
, env
);
268 // what is Thy wish, effendi?
269 switch (edit
.EditMode
) {
270 case CSSM_ACL_EDIT_MODE_ADD
: {
271 AclEntry
ent(Required(edit
.newEntry()).proto()); //@@@ bypassing callback
272 ent
.handle
= nextHandle
++;
273 entries
.insert(EntryMap::value_type(edit
.NewEntry
->Prototype
.EntryTag
, ent
));
276 case CSSM_ACL_EDIT_MODE_REPLACE
: {
277 // keep the handle, and try for some modicum of atomicity
278 Iterator it
= findEntryHandle(edit
.OldEntryHandle
);
279 AclEntry
ent(Required(edit
.newEntry()).proto());
280 ent
.handle
= edit
.OldEntryHandle
;
281 entries
.insert(EntryMap::value_type(edit
.NewEntry
->Prototype
.EntryTag
, ent
));
285 case CSSM_ACL_EDIT_MODE_DELETE
:
286 entries
.erase(findEntryHandle(edit
.OldEntryHandle
));
289 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_EDIT_MODE
);
292 IFDUMPING("acl", debugDump("owner-change-to"));
295 void ObjectAcl::cssmGetOwner(AclOwnerPrototype
&outOwner
)
297 outOwner
.TypedSubject
= owner
.subject
->toList(allocator
);
298 outOwner
.Delegate
= owner
.delegate
;
301 void ObjectAcl::cssmChangeOwner(const AclOwnerPrototype
&newOwner
,
302 const AccessCredentials
*cred
, AclValidationEnvironment
*env
)
304 IFDUMPING("acl", debugDump("owner-change-from"));
306 // only the owner entry can match
307 validateOwner(CSSM_ACL_AUTHORIZATION_CHANGE_OWNER
, cred
, env
);
312 IFDUMPING("acl", debugDump("owner-change-to"));
317 // Common features of ACL entries/owners
319 void ObjectAcl::Entry::init(const AclSubjectPointer
&subject
, bool delegate
)
321 this->subject
= subject
;
322 this->delegate
= delegate
;
325 void ObjectAcl::Entry::importBlob(Reader
&pub
, Reader
&priv
)
327 // delegate is trivial
330 // now reconstruct the (polymorphic) subject
331 CSSM_ACL_SUBJECT_TYPE subjectType
; pub(subjectType
);
332 subject
= make(subjectType
, pub
, priv
);
337 // An OwnerEntry is a restricted EntryPrototype for use as the ACL owner.
339 bool ObjectAcl::OwnerEntry::authorizes(AclAuthorization
) const
341 return true; // owner can do anything
344 bool ObjectAcl::OwnerEntry::validate(const AclValidationContext
&ctx
) const
346 return subject
->validate(ctx
); // simple subject match - no strings attached
351 // An AclEntry has some extra goodies
353 ObjectAcl::AclEntry::AclEntry(const AclEntryPrototype
&proto
) : Entry(proto
)
356 if (proto
.authorization().contains(CSSM_ACL_AUTHORIZATION_ANY
))
357 authorizesAnything
= true; // anything else wouldn't add anything
358 else if (proto
.authorization().empty())
359 authorizesAnything
= true; // not in standard, but common sense
361 authorizesAnything
= false;
362 authorizations
= proto
.authorization();
364 //@@@ not setting time range
365 // handle = not set here. Set by caller when the AclEntry is created.
368 ObjectAcl::AclEntry::AclEntry(const AclSubjectPointer
&subject
) : Entry(subject
)
370 authorizesAnything
= true; // by default, everything
371 //@@@ not setting time range
374 void ObjectAcl::AclEntry::toEntryInfo(CSSM_ACL_ENTRY_PROTOTYPE
&info
, CssmAllocator
&alloc
) const
376 info
.TypedSubject
= subject
->toList(alloc
);
377 info
.Delegate
= delegate
;
378 info
.Authorization
= AuthorizationGroup(authorizations
, alloc
);
379 //@@@ info.TimeRange =
380 assert(tag
.length() <= CSSM_MODULE_STRING_SIZE
);
381 memcpy(info
.EntryTag
, tag
.c_str(), tag
.length() + 1);
384 bool ObjectAcl::AclEntry::authorizes(AclAuthorization auth
) const
386 return authorizesAnything
|| authorizations
.find(auth
) != authorizations
.end();
389 bool ObjectAcl::AclEntry::validate(const AclValidationContext
&ctx
) const
391 //@@@ not checking time ranges
392 return subject
->validate(ctx
);
395 void ObjectAcl::AclEntry::importBlob(Reader
&pub
, Reader
&priv
)
397 Entry::importBlob(pub
, priv
);
398 const char *s
; pub(s
); tag
= s
;
399 pub(authorizesAnything
);
400 authorizations
.erase(authorizations
.begin(), authorizations
.end());
401 if (!authorizesAnything
) {
402 uint32 count
; pub(count
);
403 for (uint32 n
= 0; n
< count
; n
++) {
404 AclAuthorization auth
; pub(auth
);
405 authorizations
.insert(auth
);
408 //@@@ import time range
413 // Subject factory and makers
415 AclSubject::Maker::Maker(CSSM_ACL_SUBJECT_TYPE type
) : myType(type
)
417 ObjectAcl::makers()[type
] = this;
420 AclSubject
*ObjectAcl::make(const TypedList
&list
)
422 if (!list
.isProper())
423 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
424 return makerFor(list
.type()).make(list
);
427 AclSubject
*ObjectAcl::make(CSSM_ACL_SUBJECT_TYPE type
, Reader
&pub
, Reader
&priv
)
429 return makerFor(type
).make(pub
, priv
);
432 AclSubject::Maker
&ObjectAcl::makerFor(CSSM_ACL_SUBJECT_TYPE type
)
434 AclSubject::Maker
*maker
= makers()[type
];
436 CssmError::throwMe(CSSM_ERRCODE_ACL_SUBJECT_TYPE_NOT_SUPPORTED
);
442 // Parsing helper for subject makers.
443 // Note that count/array exclude the first element of list, which is the subject type wordid.
445 void AclSubject::Maker::crack(const CssmList
&list
, uint32 count
, ListElement
**array
, ...)
447 if (count
!= list
.length() - 1)
448 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
451 va_start(args
, array
);
452 ListElement
*elem
= list
.first()->next();
453 for (uint32 n
= 0; n
< count
; n
++, elem
= elem
->next()) {
454 CSSM_LIST_ELEMENT_TYPE expectedType
= va_arg(args
, CSSM_LIST_ELEMENT_TYPE
);
455 if (elem
->type() != expectedType
)
456 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
463 CSSM_WORDID_TYPE
AclSubject::Maker::getWord(const ListElement
&elem
,
464 int min
= 0, int max
= INT_MAX
)
466 if (elem
.type() != CSSM_LIST_ELEMENT_WORDID
)
467 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
468 CSSM_WORDID_TYPE value
= elem
;
469 if (value
< min
|| value
> max
)
470 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
476 // Debug dumping support
478 #if defined(DEBUGDUMP)
480 void ObjectAcl::debugDump(const char *what
) const
484 Debug::dump("%p ACL %s: %d entries\n", this, what
, int(entries
.size()));
485 Debug::dump(" OWNER ["); owner
.debugDump(); Debug::dump("]\n");
486 for (ConstIterator it
= begin(); it
!= end(); it
++) {
487 const AclEntry
&ent
= it
->second
;
488 Debug::dump(" (%ld:%s) [", ent
.handle
, ent
.tag
.c_str());
492 Debug::dump("%p ACL END\n", this);
495 void ObjectAcl::Entry::debugDump() const
497 subject
->debugDump();
499 Debug::dump(" DELEGATE");
502 void ObjectAcl::AclEntry::debugDump() const
505 if (authorizesAnything
) {
506 Debug::dump(" auth(ALL)");
508 Debug::dump(" auth(");
509 for (AclAuthorizationSet::iterator it
= authorizations
.begin();
510 it
!= authorizations
.end(); it
++)
511 Debug::dump(" %ld", *it
);
516 void AclSubject::debugDump() const
519 case CSSM_ACL_SUBJECT_TYPE_ANY
:
523 Debug::dump("subject type=%d", int(type()));