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 // acl_threshold - Threshold-based group ACL subjects
23 #define _CPP_ACL_THRESHOLD
26 #include <Security/acl_threshold.h>
28 #include <Security/endian.h>
32 // Validate a credential set against this subject.
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
40 class SublistValidationContext
: public AclValidationContext
{
42 SublistValidationContext(const AclValidationContext
&ctx
, const TypedList
&list
)
43 : AclValidationContext(ctx
), sampleList(list
) { }
45 uint32
count() const { return sampleList
.length() - 1; }
46 const TypedList
&sample(uint32 n
) const
47 { return TypedList::overlay(sampleList
[n
+1].list()); }
49 const TypedList
&sampleList
;
52 bool ThresholdAclSubject::validate(const AclValidationContext
&baseCtx
,
53 const TypedList
&sample
) const
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
67 SublistValidationContext
ctx(baseCtx
, sample
);
69 for (uint32 n
= 0; n
< totalSubjects
; n
++) {
70 if ((matched
+= elements
[n
]->validate(ctx
)) >= minimumNeeded
)
73 else if (matched
+ subSampleCount
- n
<= minimumNeeded
)
74 return false; // can't get there anymore
75 #endif //STRICTCOUNTING
82 // Make a copy of this subject in CSSM_LIST form
84 CssmList
ThresholdAclSubject::toList(CssmAllocator
&alloc
) const
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
));
96 // Create a ThresholdAclSubject
98 ThresholdAclSubject
*ThresholdAclSubject::Maker::make(const TypedList
&list
) const
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
);
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
);
116 ThresholdAclSubject
*ThresholdAclSubject::Maker::make(Version
, Reader
&pub
, Reader
&priv
) const
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
);
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
)
135 // Export the subject to a memory blob
137 template <class Action
>
138 void ThresholdAclSubject::exportBlobForm(Action
&pub
, Action
&priv
)
140 pub(h2n(totalSubjects
));
141 pub(h2n(minimumNeeded
));
142 for (uint32 n
= 0; n
< totalSubjects
; n
++)
143 ObjectAcl::exportSubject(elements
[n
], pub
, priv
);
146 void ThresholdAclSubject::exportBlob(Writer::Counter
&pub
, Writer::Counter
&priv
)
147 { exportBlobForm(pub
, priv
); }
149 void ThresholdAclSubject::exportBlob(Writer
&pub
, Writer
&priv
)
150 { exportBlobForm(pub
, priv
); }
155 void ThresholdAclSubject::debugDump() const
157 Debug::dump("Threshold(%ld of %ld)", minimumNeeded
, totalSubjects
);
158 for (unsigned int n
= 0; n
< elements
.size(); n
++) {
160 if (Version v
= elements
[n
]->version())
161 Debug::dump("V=%d ", v
);
162 elements
[n
]->debugDump();