]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/acl_threshold.cpp
Security-177.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / acl_threshold.cpp
1 /*
2 * Copyright (c) 2000-2001 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 //
20 // acl_threshold - Threshold-based group ACL subjects
21 //
22 #ifdef __MWERKS__
23 #define _CPP_ACL_THRESHOLD
24 #endif
25
26 #include <Security/acl_threshold.h>
27 #include <algorithm>
28 #include <Security/endian.h>
29
30
31 //
32 // Validate a credential set against this subject.
33 //
34 // With STRICTCOUNTING set, we assume that every match in the threshold ACL
35 // "consumes" one sample in the corresponding threshold sample. This will not
36 // work as expected for subject types that may succeed without a sample (e.g. ANY)
37 // or subject types that may multiply match against a single sample. You have been
38 // warned.
39 //
40 class SublistValidationContext : public AclValidationContext {
41 public:
42 SublistValidationContext(const AclValidationContext &ctx, const TypedList &list)
43 : AclValidationContext(ctx), sampleList(list) { }
44
45 uint32 count() const { return sampleList.length() - 1; }
46 const TypedList &sample(uint32 n) const
47 { return TypedList::overlay(sampleList[n+1].list()); }
48
49 const TypedList &sampleList;
50 };
51
52 bool ThresholdAclSubject::validate(const AclValidationContext &baseCtx,
53 const TypedList &sample) const
54 {
55 #ifdef STRICTCOUNTING
56 // Pre-screen for reasonable number of subsamples.
57 // We could more strictly require subSampleCount == elements.length();
58 // this is more flexible in that it allows the caller to abbreviate.
59 uint32 subSampleCount = sample.length() - 1; // (drop type header)
60 if (subSampleCount < minimumNeeded) // can't possibly satisfy
61 CssmError::throwMe(CSSM_ERRCODE_INVALID_SAMPLE_VALUE);
62 if (subSampleCount > totalSubjects) // reject attempt at sample stuffing
63 CssmError::throwMe(CSSM_ERRCODE_INVALID_SAMPLE_VALUE);
64 #endif //STRICTCOUNTING
65
66 // evaluate
67 SublistValidationContext ctx(baseCtx, sample);
68 uint32 matched = 0;
69 for (uint32 n = 0; n < totalSubjects; n++) {
70 if ((matched += elements[n]->validate(ctx)) >= minimumNeeded)
71 return true;
72 #ifdef STRICTCOUNTING
73 else if (matched + subSampleCount - n <= minimumNeeded)
74 return false; // can't get there anymore
75 #endif //STRICTCOUNTING
76 }
77 return false;
78 }
79
80
81 //
82 // Make a copy of this subject in CSSM_LIST form
83 //
84 CssmList ThresholdAclSubject::toList(CssmAllocator &alloc) const
85 {
86 TypedList result(alloc, CSSM_ACL_SUBJECT_TYPE_THRESHOLD,
87 new(alloc) ListElement(minimumNeeded),
88 new(alloc) ListElement(totalSubjects));
89 for (uint32 n = 0; n < totalSubjects; n++)
90 result += new(alloc) ListElement(elements[n]->toList(alloc));
91 return result;
92 }
93
94
95 //
96 // Create a ThresholdAclSubject
97 //
98 ThresholdAclSubject *ThresholdAclSubject::Maker::make(const TypedList &list) const
99 {
100 // pick apart the input list
101 if (list.length() < 4) // head + "n" + "k" + at least one subSubject
102 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE);
103 uint32 minimumNeeded = getWord(list[1], 1);
104 uint32 totalSubjects = getWord(list[2], minimumNeeded);
105 if (list.length() != 3 + totalSubjects)
106 CssmError::throwMe(CSSM_ERRCODE_INVALID_ACL_SUBJECT_VALUE);
107
108 // now compile the subSubjects
109 AclSubjectVector elements(totalSubjects);
110 const ListElement *subSubject = &list[3];
111 for (uint32 n = 0; n < totalSubjects; n++, subSubject = subSubject->next())
112 elements[n] = ObjectAcl::make(subSubject->typedList());
113 return new ThresholdAclSubject(totalSubjects, minimumNeeded, elements);
114 }
115
116 ThresholdAclSubject *ThresholdAclSubject::Maker::make(Version, Reader &pub, Reader &priv) const
117 {
118 Endian<uint32> totalSubjects; pub(totalSubjects);
119 Endian<uint32> minimumNeeded; pub(minimumNeeded);
120 AclSubjectVector subSubjects(totalSubjects);
121 for (uint32 n = 0; n < totalSubjects; n++)
122 subSubjects[n] = ObjectAcl::importSubject(pub, priv);
123 return new ThresholdAclSubject(totalSubjects, minimumNeeded, subSubjects);
124 }
125
126 ThresholdAclSubject::ThresholdAclSubject(uint32 n, uint32 k,
127 const AclSubjectVector &subSubjects)
128 : SimpleAclSubject(CSSM_ACL_SUBJECT_TYPE_THRESHOLD, CSSM_SAMPLE_TYPE_THRESHOLD),
129 minimumNeeded(k), totalSubjects(n), elements(subSubjects)
130 {
131 }
132
133
134 //
135 // Export the subject to a memory blob
136 //
137 template <class Action>
138 void ThresholdAclSubject::exportBlobForm(Action &pub, Action &priv)
139 {
140 pub(h2n(totalSubjects));
141 pub(h2n(minimumNeeded));
142 for (uint32 n = 0; n < totalSubjects; n++)
143 ObjectAcl::exportSubject(elements[n], pub, priv);
144 }
145
146 void ThresholdAclSubject::exportBlob(Writer::Counter &pub, Writer::Counter &priv)
147 { exportBlobForm(pub, priv); }
148
149 void ThresholdAclSubject::exportBlob(Writer &pub, Writer &priv)
150 { exportBlobForm(pub, priv); }
151
152
153 #ifdef DEBUGDUMP
154
155 void ThresholdAclSubject::debugDump() const
156 {
157 Debug::dump("Threshold(%ld of %ld)", minimumNeeded, totalSubjects);
158 for (unsigned int n = 0; n < elements.size(); n++) {
159 Debug::dump(" [");
160 if (Version v = elements[n]->version())
161 Debug::dump("V=%d ", v);
162 elements[n]->debugDump();
163 Debug::dump("]");
164 }
165 }
166
167 #endif //DEBUGDUMP