]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/SampleGroup.cpp
Security-54.1.3.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / SampleGroup.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 // SampleGroup.cpp
20 //
21 // CSSM_SAMPLE POD routines
22
23 #ifdef __MWERKS__
24 #define _CPP_UTILITIES
25 #endif
26
27 #include <cdsa_utilities/cssmlist.h>
28 #include <cdsa_utilities/SampleGroup.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 CssmSample::CssmSample()
33 {
34 Verifier = NULL;
35 TypedSample.ListType = CSSM_LIST_TYPE_UNKNOWN;
36 TypedSample.Head = NULL;
37 TypedSample.Tail = NULL;
38 }
39
40 CssmSample::CssmSample( CSSM_LIST &list, CSSM_SUBSERVICE_UID *verifier)
41 {
42 Verifier = verifier;
43 TypedSample.ListType = list.ListType;
44 TypedSample.Head = list.Head;
45 TypedSample.Tail = list.Tail;
46 }
47
48 CssmSample::~CssmSample()
49 {
50 }
51
52
53 CssmSample* CssmSample::operator = (CssmSample& sample)
54 {
55 if( this == &sample )
56 return NULL;
57
58 this->Verifier = sample.Verifier;
59
60 this->TypedSample = sample.TypedSample;
61
62 return this;
63 }
64
65 CSSM_RETURN CssmSample::AddPasswordImmediate( char* password, CSSM_SUBSERVICE_UID *optionalVerifier )
66 {
67 CSSM_RETURN result = CSSM_OK;
68
69 CSSM_LIST_ELEMENT* passwordTypeElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_PASSWORD ); // declares the type to be password
70 (CssmList::overlay(TypedSample)).append( ListElement::overlay(passwordTypeElement) );
71
72 char* permanentPasswordData = (char*)malloc( strlen(password) + 1 ); // need error handling. Going to assume these succeed for now
73 strcpy( permanentPasswordData, password );
74 CSSM_LIST_ELEMENT* passwordElement = MakeDatumElement( (void*)permanentPasswordData, strlen(password) ); // has the password CSSM_DATA in it
75 (CssmList::overlay(TypedSample)).append( ListElement::overlay(passwordElement) );
76
77 Verifier = optionalVerifier;
78
79 return result;
80 }
81
82
83 CSSM_RETURN CssmSample::AddPasswordCallback( )
84 {
85 CSSM_RETURN result = CSSM_OK;
86
87 CSSM_LIST_ELEMENT* passwordCallbackElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_PASSWORD ); // declares the type to be password
88 (CssmList::overlay(TypedSample)).append( ListElement::overlay(passwordCallbackElement) );
89
90 Verifier = NULL;
91
92 return result;
93
94 }
95
96 CSSM_RETURN CssmSample::AddHashedPassword( char* password, CSSM_SUBSERVICE_UID *optionalVerifier )
97 {
98 CSSM_RETURN result = CSSM_OK;
99
100 CSSM_LIST_ELEMENT* passwordTypeElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_HASHED_PASSWORD ); // declares the type to be password
101 (CssmList::overlay(TypedSample)).append( ListElement::overlay(passwordTypeElement) );
102
103 char* permanentPasswordData = (char*)malloc( strlen(password) + 1 ); // need error handling. Going to assume these succeed for now
104 strcpy( permanentPasswordData, password );
105 CSSM_LIST_ELEMENT* passwordElement = MakeDatumElement( (void*)permanentPasswordData, strlen(password) ); // has the password CSSM_DATA in it
106 (CssmList::overlay(TypedSample)).append( ListElement::overlay(passwordElement) );
107
108 Verifier = optionalVerifier;
109
110 return result;
111
112 }
113
114
115
116 CSSM_RETURN CssmSample::AddProtectedPasword(CSSM_SUBSERVICE_UID *optionalVerifier)
117 {
118 CSSM_RETURN result = CSSM_OK;
119
120 CSSM_LIST_ELEMENT* passwordCallbackElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_PROTECTED_PASSWORD ); // declares the type to be password
121 (CssmList::overlay(TypedSample)).append( ListElement::overlay(passwordCallbackElement) );
122
123 Verifier = optionalVerifier;
124
125 return result;
126 }
127
128
129 CSSM_RETURN CssmSample::AddPromptedPassword( char* promptedPassword, CSSM_SUBSERVICE_UID *optionalVerifier )
130 {
131 CSSM_RETURN result = CSSM_OK;
132
133 CSSM_LIST_ELEMENT* passwordTypeElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_PROMPTED_PASSWORD ); // declares the type to be password
134 (CssmList::overlay(TypedSample)).append( ListElement::overlay(passwordTypeElement) );
135
136 char* permanentPasswordData = (char*)malloc( strlen(promptedPassword) + 1 ); // need error handling. Going to assume these succeed for now
137 strcpy( permanentPasswordData, promptedPassword );
138 CSSM_LIST_ELEMENT* passwordPromptElement = MakeDatumElement( (void*)permanentPasswordData, strlen(promptedPassword) ); // has the password CSSM_DATA in it
139 (CssmList::overlay(TypedSample)).append( ListElement::overlay(passwordPromptElement) );
140
141 Verifier = optionalVerifier;
142
143 return result;
144 }
145
146
147 CSSM_RETURN CssmSample::AddSignedNonceForCallback( CSSM_SUBSERVICE_UID *requiredVerifier )
148 {
149 CSSM_RETURN result = CSSM_OK;
150
151 CSSM_LIST_ELEMENT* signedNonceCallbackElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_SIGNED_NONCE );
152 (CssmList::overlay(TypedSample)).append( ListElement::overlay(signedNonceCallbackElement) );
153
154 Verifier = requiredVerifier;
155
156 return result;
157 }
158
159 CSSM_RETURN CssmSample::AddSignedNonceReply( CSSM_DATA_PTR signedNonce, CSSM_SUBSERVICE_UID *requiredVerifier )
160 {
161 CSSM_RETURN result = CSSM_OK;
162
163 CSSM_LIST_ELEMENT* signedNonceTypeElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_SIGNED_NONCE );
164 (CssmList::overlay(TypedSample)).append( ListElement::overlay(signedNonceTypeElement) );
165
166 CSSM_LIST_ELEMENT* signedNonceDataElement = MakeDatumElement( (void*)signedNonce->Data, signedNonce->Length );
167 (CssmList::overlay(TypedSample)).append( ListElement::overlay(signedNonceDataElement) );
168
169 Verifier = requiredVerifier;
170
171 return result;
172 }
173
174 CSSM_RETURN CssmSample::AddSignedSecretForCallback( CSSM_SUBSERVICE_UID *requiredVerifier )
175 {
176 CSSM_RETURN result = CSSM_OK;
177 CSSM_LIST_ELEMENT* signedSecretCallbackElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_SIGNED_SECRET );
178 (CssmList::overlay(TypedSample)).append( ListElement::overlay(signedSecretCallbackElement) );
179
180 Verifier = requiredVerifier;
181
182 return result;
183 }
184
185 CSSM_RETURN CssmSample::AddSignedSecretImmediate( CSSM_DATA_PTR signedSecret, CSSM_SUBSERVICE_UID *requiredVerifier )
186 {
187 CSSM_RETURN result = CSSM_OK;
188
189 CSSM_LIST_ELEMENT* signedSecretTypeElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_SIGNED_SECRET );
190 (CssmList::overlay(TypedSample)).append( ListElement::overlay(signedSecretTypeElement) );
191
192 CSSM_LIST_ELEMENT* signedSecretElement = MakeDatumElement( (void*)signedSecret->Data, signedSecret->Length );
193 (CssmList::overlay(TypedSample)).append( ListElement::overlay(signedSecretElement) );
194
195 Verifier = requiredVerifier;
196 return result;
197
198 }
199
200 CSSM_RETURN CssmSample::AddBiometricCallback( CSSM_SUBSERVICE_UID *requiredVerifier )
201 {
202 CSSM_RETURN result = CSSM_OK;
203
204 CSSM_LIST_ELEMENT* callbackElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_BIOMETRIC );
205 (CssmList::overlay(TypedSample)).append( ListElement::overlay(callbackElement) );
206
207 Verifier = requiredVerifier;
208
209 return result;
210 }
211
212
213 CSSM_RETURN CssmSample::AddBiometricImmediate( CSSM_DATA_PTR biometricData, CSSM_SUBSERVICE_UID *requiredVerifier )
214 {
215 CSSM_RETURN result = CSSM_OK;
216
217 CSSM_LIST_ELEMENT* typeElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_BIOMETRIC );
218 (CssmList::overlay(TypedSample)).append( ListElement::overlay(typeElement) );
219
220 CSSM_LIST_ELEMENT* dataElement = MakeDatumElement( (void*)biometricData->Data, biometricData->Length );
221 (CssmList::overlay(TypedSample)).append( ListElement::overlay(dataElement) );
222
223 Verifier = requiredVerifier;
224
225 return result;
226 }
227
228
229 CSSM_RETURN CssmSample::AddProtectedBiometric( CSSM_SUBSERVICE_UID *requiredVerifier )
230 {
231 CSSM_RETURN result = CSSM_OK;
232
233 CSSM_LIST_ELEMENT* callbackElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_PROTECTED_BIOMETRIC );
234 (CssmList::overlay(TypedSample)).append( ListElement::overlay(callbackElement) );
235
236 Verifier = requiredVerifier;
237 return result;
238 }
239
240 CSSM_RETURN CssmSample::AddPromptedBiometric( CSSM_DATA_PTR biometricData, CSSM_SUBSERVICE_UID *requiredVerifier )
241 {
242 CSSM_RETURN result = CSSM_OK;
243
244 CSSM_LIST_ELEMENT* callbackElement = MakeWordIDElement( CSSM_SAMPLE_TYPE_PROMPTED_BIOMETRIC );
245 (CssmList::overlay(TypedSample)).append( ListElement::overlay(callbackElement) );
246
247
248 CSSM_LIST_ELEMENT* dataElement = MakeDatumElement( (void*)biometricData->Data, biometricData->Length );
249 (CssmList::overlay(TypedSample)).append( ListElement::overlay(dataElement) );
250
251 Verifier = requiredVerifier;
252 return result;
253 }
254
255 // CssmSampleGroup
256
257 CssmSampleGroup::CssmSampleGroup()
258 { // creates the nothing sample group
259 NumberOfSamples = 0;
260 Samples = NULL;
261 }
262
263 CSSM_RETURN CssmSampleGroup::AddSample(CSSM_SAMPLE* sample)
264 {
265 CSSM_RETURN result = CSSM_OK;
266 CSSM_SAMPLE* sampleBase;
267 if( NumberOfSamples == 0 )
268 { // malloc to create the first item
269 sampleBase = (CSSM_SAMPLE*)malloc( sizeof(CSSM_SAMPLE) );
270 Samples = sampleBase;
271 }
272 else
273 { // realloc to add the next item
274 sampleBase = (CSSM_SAMPLE*)realloc( (void*)Samples, sizeof(CSSM_SAMPLE) * (NumberOfSamples + 1) );
275 Samples = sampleBase;
276 }
277
278 sampleBase[NumberOfSamples].TypedSample.ListType = sample->TypedSample.ListType;
279 sampleBase[NumberOfSamples].TypedSample.Head = sample->TypedSample.Head;
280 sampleBase[NumberOfSamples].TypedSample.Tail = sample->TypedSample.Tail;
281 sampleBase[NumberOfSamples].Verifier = sample->Verifier;
282
283 NumberOfSamples++;
284
285 return result;
286 }
287
288 CSSM_SAMPLE* CssmSampleGroup::GetIthSample(uint32 sampleIndex)
289 {
290 if( (0 != NumberOfSamples) && (sampleIndex < NumberOfSamples-1) )
291 return (CSSM_SAMPLE*)&Samples[sampleIndex];
292 else
293 return NULL;
294 }