2 * Copyright (c) 2000-2004,2006-2007 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 // cssmaclpod - enhanced PodWrappers for ACL-related CSSM data structures
28 #include <security_cdsa_utilities/cssmaclpod.h>
29 #include <security_cdsa_utilities/cssmwalkers.h>
37 // AclAuthorizationSets
39 AclAuthorizationSet::AclAuthorizationSet(AclAuthorization auth0
, AclAuthorization auth
, ...)
47 auth
= va_arg(args
, AclAuthorization
);
54 // AclAuthorizationGroups
56 AuthorizationGroup::AuthorizationGroup(const AclAuthorizationSet
&auths
, Allocator
&alloc
)
58 NumberOfAuthTags
= auths
.size();
59 AuthTags
= alloc
.alloc
<CSSM_ACL_AUTHORIZATION_TAG
>(NumberOfAuthTags
);
60 copy(auths
.begin(), auths
.end(), AuthTags
); // happens to be sorted
63 AuthorizationGroup::AuthorizationGroup(CSSM_ACL_AUTHORIZATION_TAG tag
, Allocator
&alloc
)
65 AuthTags
= alloc
.alloc
<CSSM_ACL_AUTHORIZATION_TAG
>(1);
70 void AuthorizationGroup::destroy(Allocator
&alloc
)
75 bool AuthorizationGroup::contains(CSSM_ACL_AUTHORIZATION_TAG tag
) const
77 return find(AuthTags
, &AuthTags
[NumberOfAuthTags
], tag
) != &AuthTags
[NumberOfAuthTags
];
81 AuthorizationGroup::operator AclAuthorizationSet() const
83 return AclAuthorizationSet(AuthTags
, &AuthTags
[NumberOfAuthTags
]);
86 AclEntryPrototype::AclEntryPrototype(const AclOwnerPrototype
&proto
)
88 memset(this, 0, sizeof(*this));
89 TypedSubject
= proto
.subject(); Delegate
= proto
.delegate();
90 //@@@ set authorization to "is owner" pseudo-auth? See cssmacl.h
93 void AclEntryPrototype::tag(const char *tagString
)
95 if (tagString
== NULL
)
97 else if (strlen(tagString
) > CSSM_MODULE_STRING_SIZE
)
98 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_ENTRY_TAG
);
100 strcpy(EntryTag
, tagString
);
103 void AclEntryPrototype::tag(const string
&tagString
)
105 if (tagString
.length() > CSSM_MODULE_STRING_SIZE
)
106 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_ENTRY_TAG
);
108 memcpy(EntryTag
, tagString
.c_str(), tagString
.length() + 1);
112 AclOwnerPrototype
*AutoAclOwnerPrototype::make()
114 if (!mAclOwnerPrototype
) {
115 mAclOwnerPrototype
= new AclOwnerPrototype
;
116 mAclOwnerPrototype
->clearPod();
118 return mAclOwnerPrototype
;
121 AutoAclOwnerPrototype::~AutoAclOwnerPrototype()
124 DataWalkers::chunkFree(mAclOwnerPrototype
, *mAllocator
);
128 AutoAclOwnerPrototype::allocator(Allocator
&allocator
)
130 mAllocator
= &allocator
;
134 void AutoAclEntryInfoList::size(uint32 newSize
)
137 mEntries
= mAllocator
->alloc
<AclEntryInfo
>(mEntries
, newSize
);
138 for (uint32 n
= mCount
; n
< newSize
; n
++)
139 mEntries
[n
].clearPod();
144 AclEntryInfo
&AutoAclEntryInfoList::at(uint32 ix
)
147 size(ix
+ 1); // expand vector
152 void AutoAclEntryInfoList::clear()
156 DataWalkers::ChunkFreeWalker
w(*mAllocator
);
157 for (uint32 ix
= 0; ix
< mCount
; ix
++)
158 walk(w
, mEntries
[ix
]);
159 mAllocator
->free(mEntries
);
165 void AutoAclEntryInfoList::allocator(Allocator
&allocator
)
167 mAllocator
= &allocator
;
171 void AutoAclEntryInfoList::add(const TypedList
&subj
, const AclAuthorizationSet
&auths
, const char *tag
/* = NULL */)
173 AclEntryInfo
&info
= at(size());
174 info
.proto() = AclEntryPrototype(subj
);
175 info
.proto().authorization() = AuthorizationGroup(auths
, allocator());
176 info
.proto().tag(tag
);
180 void AutoAclEntryInfoList::addPin(const TypedList
&subj
, uint32 slot
)
183 snprintf(tag
, sizeof(tag
), "PIN%d", slot
);
184 add(subj
, CSSM_ACL_AUTHORIZATION_PREAUTH(slot
), tag
);
187 void AutoAclEntryInfoList::addPinState(uint32 slot
, uint32 status
)
190 snprintf(tag
, sizeof(tag
), "PIN%d?", slot
);
191 TypedList
subj(allocator(), CSSM_WORDID_PIN
,
192 new(allocator()) ListElement(slot
),
193 new(allocator()) ListElement(status
));
194 add(subj
, CSSM_WORDID_PIN
, tag
);
197 void AutoAclEntryInfoList::addPinState(uint32 slot
, uint32 status
, uint32 count
)
200 snprintf(tag
, sizeof(tag
), "PIN%d?", slot
);
201 TypedList
subj(allocator(), CSSM_WORDID_PIN
,
202 new(allocator()) ListElement(slot
),
203 new(allocator()) ListElement(status
),
204 new(allocator()) ListElement(count
));
205 add(subj
, CSSM_WORDID_PIN
, tag
);
208 uint32
pinFromAclTag(const char *tag
, const char *suffix
/* = NULL */)
212 snprintf(format
, sizeof(format
), "PIN%%d%s%%n", suffix
? suffix
: "");
215 sscanf(tag
, format
, &pin
, &consumed
);
216 if (consumed
== strlen(tag
)) // complete and sufficient
222 } // namespace Security