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
22 #include <Security/cssmacl.h>
23 #include <Security/debugging.h>
27 using namespace DataWalkers
;
31 // The static map of available ACL subject makers.
32 // These are the kinds of ACL subjects we can deal with.
34 ModuleNexus
<ObjectAcl::MakerMap
> ObjectAcl::makers
;
38 // Common (basic) features of AclSubjects
40 AclSubject::~AclSubject()
43 AclValidationEnvironment::~AclValidationEnvironment()
46 void AclSubject::exportBlob(Writer::Counter
&, Writer::Counter
&)
49 void AclSubject::exportBlob(Writer
&, Writer
&)
52 void AclSubject::importBlob(Reader
&, Reader
&)
55 AclSubject::Maker::~Maker()
60 // A SimpleAclSubject accepts only a single type of sample, validates
61 // samples independently, and makes no use of certificates.
63 bool SimpleAclSubject::validate(const AclValidationContext
&ctx
) const
65 for (uint32 n
= 0; n
< ctx
.count(); n
++) {
66 const TypedList
&sample
= ctx
[n
];
67 if (!sample
.isProper())
68 CssmError::throwMe(CSSM_ERRCODE_INVALID_SAMPLE_VALUE
);
69 if (sample
.type() == acceptingSamples
&& validate(ctx
, sample
))
70 return true; // matched this sample; validation successful
77 // Create an ObjectAcl
79 ObjectAcl::ObjectAcl(CssmAllocator
&alloc
) : allocator(alloc
), nextHandle(1)
83 ObjectAcl::ObjectAcl(const AclEntryPrototype
&proto
, CssmAllocator
&alloc
)
84 : allocator(alloc
), nextHandle(1)
86 cssmSetInitial(proto
);
89 ObjectAcl::~ObjectAcl()
94 // Set an "initial ACL" from a CSSM-style initial ACL argument.
95 // This will replace the owner, as well as replace the entire ACL
96 // with a single-item slot, as per CSSM specification.
98 void ObjectAcl::cssmSetInitial(const AclEntryPrototype
&proto
)
100 owner
= OwnerEntry(proto
);
101 entries
.insert(EntryMap::value_type(proto
.tag(), proto
))->second
.handle
= nextHandle
++;
102 IFDUMPING("acl", debugDump("create/proto"));
105 void ObjectAcl::cssmSetInitial(const AclSubjectPointer
&subject
)
107 owner
= OwnerEntry(subject
);
108 entries
.insert(EntryMap::value_type("", subject
))->second
.handle
= nextHandle
++;
109 IFDUMPING("acl", debugDump("create/subject"));
112 ObjectAcl::Entry::~Entry()
116 AclValidationContext::~AclValidationContext()
121 // ObjectAcl::validate validates an access authorization claim.
122 // Returns normally if 'auth' is granted to the bearer of 'cred'.
123 // Otherwise, throws a suitable (ACL-related) CssmError exception.
124 // @@@ Should it return a reference to the Entry that granted access?
126 class BaseValidationContext
: public AclValidationContext
{
128 BaseValidationContext(const AccessCredentials
*cred
,
129 AclAuthorization auth
, AclValidationEnvironment
*env
)
130 : AclValidationContext(cred
, auth
, env
) { }
132 uint32
count() const { return mCred
? mCred
->samples().length() : 0; }
133 const TypedList
&sample(uint32 n
) const
134 { assert(n
< count()); return mCred
->samples()[n
]; }
137 void ObjectAcl::validate(AclAuthorization auth
, const AccessCredentials
*cred
,
138 AclValidationEnvironment
*env
) const
140 //@@@ should pre-screen based on requested auth, maybe?
141 BaseValidationContext
ctx(cred
, auth
, env
);
143 #if defined(ACL_OMNIPOTENT_OWNER)
144 // try owner (owner can do anything)
145 if (owner
.validate(ctx
))
147 #endif ACL_OMNIPOTENT_OWNER
149 // try applicable ACLs
150 pair
<ConstIterator
, ConstIterator
> range
;
151 if (getRange(cred
->EntryTag
, range
) == 0) // no such tag
152 CssmError::throwMe(CSSM_ERRCODE_ACL_ENTRY_TAG_NOT_FOUND
);
153 // try entries in turn
154 for (ConstIterator it
= range
.first
; it
!= range
.second
; it
++) {
155 const AclEntry
&slot
= it
->second
;
156 if (slot
.authorizes(ctx
.authorization()) && slot
.validate(ctx
))
159 CssmError::throwMe(CSSM_ERRCODE_OPERATION_AUTH_DENIED
); //@@@ imprecise
162 void ObjectAcl::validateOwner(AclAuthorization authorizationHint
,
163 const AccessCredentials
*cred
, AclValidationEnvironment
*env
) const
165 BaseValidationContext
ctx(cred
, authorizationHint
, env
);
166 if (owner
.validate(ctx
))
168 CssmError::throwMe(CSSM_ERRCODE_OPERATION_AUTH_DENIED
);
173 // Export an ObjectAcl to two memory blobs: public and private data separated.
174 // This is a standard two-pass size+copy operation.
176 void ObjectAcl::exportBlob(CssmData
&publicBlob
, CssmData
&privateBlob
)
178 Writer::Counter pubSize
, privSize
;
179 uint32 entryCount
= entries
.size();
180 owner
.exportBlob(pubSize
, privSize
);
182 for (Iterator it
= begin(); it
!= end(); it
++)
183 it
->second
.exportBlob(pubSize
, privSize
);
184 publicBlob
= CssmData(allocator
.malloc(pubSize
), pubSize
);
185 privateBlob
= CssmData(allocator
.malloc(privSize
), privSize
);
186 Writer
pubWriter(publicBlob
), privWriter(privateBlob
);
187 owner
.exportBlob(pubWriter
, privWriter
);
188 pubWriter(entryCount
);
189 for (Iterator it
= begin(); it
!= end(); it
++)
190 it
->second
.exportBlob(pubWriter
, privWriter
);
191 IFDUMPING("acl", debugDump("exported"));
196 // Import an ObjectAcl's contents from two memory blobs representing public and
197 // private contents, respectively. These blobs must have been generated by the
199 // Prior contents (if any) are deleted and replaced.
201 void ObjectAcl::importBlob(const void *publicBlob
, const void *privateBlob
)
203 Reader
pubReader(publicBlob
), privReader(privateBlob
);
204 owner
.importBlob(pubReader
, privReader
);
205 uint32 entryCount
; pubReader(entryCount
);
206 entries
.erase(begin(), end());
207 for (uint32 n
= 0; n
< entryCount
; n
++) {
209 newEntry
.importBlob(pubReader
, privReader
);
210 entries
.insert(EntryMap::value_type(newEntry
.tag
, newEntry
))->second
.handle
= nextHandle
++;
212 IFDUMPING("acl", debugDump("imported"));
217 // Import/export helpers for subjects.
218 // This is exported to (subject implementation) callers to maintain consistency
219 // in binary format handling.
221 AclSubject
*ObjectAcl::importSubject(Reader
&pub
, Reader
&priv
)
223 uint32 typeAndVersion
; pub(typeAndVersion
);
224 return make(typeAndVersion
, pub
, priv
);
229 // ACL utility methods
231 unsigned int ObjectAcl::getRange(const char *tag
, pair
<ConstIterator
, ConstIterator
> &range
) const
233 if (tag
&& tag
[0]) { // tag restriction in effect
234 range
= entries
.equal_range(tag
);
235 uint32 count
= entries
.count(tag
);
237 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_ENTRY_TAG
);
239 } else { // try all tags
240 range
.first
= entries
.begin();
241 range
.second
= entries
.end();
242 return entries
.size();
246 ObjectAcl::Iterator
ObjectAcl::findEntryHandle(CSSM_ACL_HANDLE handle
)
248 for (Iterator it
= entries
.begin(); it
!= entries
.end(); it
++)
249 if (it
->second
.handle
== handle
)
251 CssmError::throwMe(CSSMERR_CSSM_INVALID_HANDLE_USAGE
); //%%% imprecise error code
256 // CSSM style ACL access and modification functions.
258 void ObjectAcl::cssmGetAcl(const char *tag
, uint32
&count
, AclEntryInfo
* &acls
)
260 pair
<ConstIterator
, ConstIterator
> range
;
261 count
= getRange(tag
, range
);
262 acls
= allocator
.alloc
<AclEntryInfo
>(count
);
264 for (ConstIterator it
= range
.first
; it
!= range
.second
; it
++, n
++) {
265 acls
[n
].EntryHandle
= it
->second
.handle
;
266 it
->second
.toEntryInfo(acls
[n
].EntryPublicInfo
, allocator
);
271 void ObjectAcl::cssmChangeAcl(const AclEdit
&edit
,
272 const AccessCredentials
*cred
, AclValidationEnvironment
*env
)
274 IFDUMPING("acl", debugDump("acl-change-from"));
276 // validate access credentials
277 validateOwner(CSSM_ACL_AUTHORIZATION_CHANGE_ACL
, cred
, env
);
279 // what is Thy wish, effendi?
280 switch (edit
.EditMode
) {
281 case CSSM_ACL_EDIT_MODE_ADD
: {
282 AclEntry
ent(Required(edit
.newEntry()).proto()); //@@@ bypassing callback
283 ent
.handle
= nextHandle
++;
284 entries
.insert(EntryMap::value_type(edit
.NewEntry
->Prototype
.EntryTag
, ent
));
287 case CSSM_ACL_EDIT_MODE_REPLACE
: {
288 // keep the handle, and try for some modicum of atomicity
289 Iterator it
= findEntryHandle(edit
.OldEntryHandle
);
290 AclEntry
ent(Required(edit
.newEntry()).proto());
291 ent
.handle
= edit
.OldEntryHandle
;
292 entries
.insert(EntryMap::value_type(edit
.NewEntry
->Prototype
.EntryTag
, ent
));
296 case CSSM_ACL_EDIT_MODE_DELETE
:
297 entries
.erase(findEntryHandle(edit
.OldEntryHandle
));
300 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_EDIT_MODE
);
303 IFDUMPING("acl", debugDump("acl-change-to"));
306 void ObjectAcl::cssmGetOwner(AclOwnerPrototype
&outOwner
)
308 outOwner
.TypedSubject
= owner
.subject
->toList(allocator
);
309 outOwner
.Delegate
= owner
.delegate
;
312 void ObjectAcl::cssmChangeOwner(const AclOwnerPrototype
&newOwner
,
313 const AccessCredentials
*cred
, AclValidationEnvironment
*env
)
315 IFDUMPING("acl", debugDump("owner-change-from"));
317 // only the owner entry can match
318 validateOwner(CSSM_ACL_AUTHORIZATION_CHANGE_OWNER
, cred
, env
);
323 IFDUMPING("acl", debugDump("owner-change-to"));
328 // Common features of ACL entries/owners
330 void ObjectAcl::Entry::init(const AclSubjectPointer
&subject
, bool delegate
)
332 this->subject
= subject
;
333 this->delegate
= delegate
;
336 void ObjectAcl::Entry::importBlob(Reader
&pub
, Reader
&priv
)
338 uint32 del
; pub(del
); delegate
= del
; // 4 bytes delegate flag
339 subject
= importSubject(pub
, priv
);
344 // An OwnerEntry is a restricted EntryPrototype for use as the ACL owner.
346 bool ObjectAcl::OwnerEntry::authorizes(AclAuthorization
) const
348 return true; // owner can do anything
351 bool ObjectAcl::OwnerEntry::validate(const AclValidationContext
&ctx
) const
353 return subject
->validate(ctx
); // simple subject match - no strings attached
358 // An AclEntry has some extra goodies
360 ObjectAcl::AclEntry::AclEntry(const AclEntryPrototype
&proto
) : Entry(proto
)
363 if (proto
.authorization().contains(CSSM_ACL_AUTHORIZATION_ANY
))
364 authorizesAnything
= true; // anything else wouldn't add anything
365 else if (proto
.authorization().empty())
366 authorizesAnything
= true; // not in standard, but common sense
368 authorizesAnything
= false;
369 authorizations
= proto
.authorization();
371 //@@@ not setting time range
372 // handle = not set here. Set by caller when the AclEntry is created.
375 ObjectAcl::AclEntry::AclEntry(const AclSubjectPointer
&subject
) : Entry(subject
)
377 authorizesAnything
= true; // by default, everything
378 //@@@ not setting time range
381 void ObjectAcl::AclEntry::toEntryInfo(CSSM_ACL_ENTRY_PROTOTYPE
&info
, CssmAllocator
&alloc
) const
383 info
.TypedSubject
= subject
->toList(alloc
);
384 info
.Delegate
= delegate
;
385 info
.Authorization
= AuthorizationGroup(authorizations
, alloc
);
386 //@@@ info.TimeRange =
387 assert(tag
.length() <= CSSM_MODULE_STRING_SIZE
);
388 memcpy(info
.EntryTag
, tag
.c_str(), tag
.length() + 1);
391 bool ObjectAcl::AclEntry::authorizes(AclAuthorization auth
) const
393 return authorizesAnything
|| authorizations
.find(auth
) != authorizations
.end();
396 bool ObjectAcl::AclEntry::validate(const AclValidationContext
&ctx
) const
398 //@@@ not checking time ranges
399 return subject
->validate(ctx
);
402 void ObjectAcl::AclEntry::importBlob(Reader
&pub
, Reader
&priv
)
404 Entry::importBlob(pub
, priv
);
405 const char *s
; pub(s
); tag
= s
;
407 // authorizesAnything is on disk as a 4-byte flag
408 uint32 tmpAuthorizesAnything
;
409 pub(tmpAuthorizesAnything
);
410 authorizesAnything
= tmpAuthorizesAnything
;
412 authorizations
.erase(authorizations
.begin(), authorizations
.end());
413 if (!authorizesAnything
) {
414 uint32 count
; pub(count
);
415 for (uint32 n
= 0; n
< count
; n
++) {
416 AclAuthorization auth
; pub(auth
);
417 authorizations
.insert(auth
);
420 //@@@ import time range
425 // Subject factory and makers
427 AclSubject::Maker::Maker(CSSM_ACL_SUBJECT_TYPE type
) : myType(type
)
429 ObjectAcl::makers()[type
] = this;
432 AclSubject
*ObjectAcl::make(const TypedList
&list
)
434 if (!list
.isProper())
435 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
436 return makerFor(list
.type()).make(list
);
439 AclSubject
*ObjectAcl::make(uint32 typeAndVersion
, Reader
&pub
, Reader
&priv
)
441 // this type is encode as (version << 24) | type
442 return makerFor(typeAndVersion
& ~AclSubject::versionMask
).make(typeAndVersion
>> AclSubject::versionShift
, pub
, priv
);
445 AclSubject::Maker
&ObjectAcl::makerFor(CSSM_ACL_SUBJECT_TYPE type
)
447 AclSubject::Maker
*maker
= makers()[type
];
449 CssmError::throwMe(CSSM_ERRCODE_ACL_SUBJECT_TYPE_NOT_SUPPORTED
);
455 // Parsing helper for subject makers.
456 // Note that count/array exclude the first element of list, which is the subject type wordid.
458 void AclSubject::Maker::crack(const CssmList
&list
, uint32 count
, ListElement
**array
, ...)
460 if (count
!= list
.length() - 1)
461 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
464 va_start(args
, array
);
465 ListElement
*elem
= list
.first()->next();
466 for (uint32 n
= 0; n
< count
; n
++, elem
= elem
->next()) {
467 CSSM_LIST_ELEMENT_TYPE expectedType
= va_arg(args
, CSSM_LIST_ELEMENT_TYPE
);
468 if (elem
->type() != expectedType
)
469 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
476 CSSM_WORDID_TYPE
AclSubject::Maker::getWord(const ListElement
&elem
,
477 int min
= 0, int max
= INT_MAX
)
479 if (elem
.type() != CSSM_LIST_ELEMENT_WORDID
)
480 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
481 CSSM_WORDID_TYPE value
= elem
;
482 if (value
< min
|| value
> max
)
483 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE
);
489 // Debug dumping support.
490 // Leave the ObjectAcl::debugDump method in (stubbed out)
491 // to keep the virtual table layout stable, and to allow
492 // proper linking in weird mix-and-match scenarios.
494 void ObjectAcl::debugDump(const char *what
) const
496 #if defined(DEBUGDUMP)
499 Debug::dump("%p ACL %s: %d entries\n", this, what
, int(entries
.size()));
500 Debug::dump(" OWNER ["); owner
.debugDump(); Debug::dump("]\n");
501 for (ConstIterator it
= begin(); it
!= end(); it
++) {
502 const AclEntry
&ent
= it
->second
;
503 Debug::dump(" (%ld:%s) [", ent
.handle
, ent
.tag
.c_str());
507 Debug::dump("%p ACL END\n", this);
511 void AclSubject::debugDump() const
513 #if defined(DEBUGDUMP)
515 case CSSM_ACL_SUBJECT_TYPE_ANY
:
519 Debug::dump("subject type=%d", int(type()));
525 #if defined(DEBUGDUMP)
527 void ObjectAcl::Entry::debugDump() const
529 if (AclSubject::Version v
= subject
->version())
530 Debug::dump("V=%d ", v
);
531 subject
->debugDump();
533 Debug::dump(" DELEGATE");
536 void ObjectAcl::AclEntry::debugDump() const
539 if (authorizesAnything
) {
540 Debug::dump(" auth(ALL)");
542 Debug::dump(" auth(");
543 for (AclAuthorizationSet::iterator it
= authorizations
.begin();
544 it
!= authorizations
.end(); it
++)
545 Debug::dump(" %ld", *it
);