Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / ckfw / builtins / bfind.c
blob817555f83cd6a646465e2ac296134073fae387a3
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #ifdef DEBUG
38 static const char CVS_ID[] = "@(#) $RCSfile: bfind.c,v $ $Revision: 1.6 $ $Date: 2005/01/20 02:25:46 $";
39 #endif /* DEBUG */
41 #ifndef BUILTINS_H
42 #include "builtins.h"
43 #endif /* BUILTINS_H */
46 * builtins/find.c
48 * This file implements the NSSCKMDFindObjects object for the
49 * "builtin objects" cryptoki module.
52 struct builtinsFOStr {
53 NSSArena *arena;
54 CK_ULONG n;
55 CK_ULONG i;
56 builtinsInternalObject **objs;
59 static void
60 builtins_mdFindObjects_Final
62 NSSCKMDFindObjects *mdFindObjects,
63 NSSCKFWFindObjects *fwFindObjects,
64 NSSCKMDSession *mdSession,
65 NSSCKFWSession *fwSession,
66 NSSCKMDToken *mdToken,
67 NSSCKFWToken *fwToken,
68 NSSCKMDInstance *mdInstance,
69 NSSCKFWInstance *fwInstance
72 struct builtinsFOStr *fo = (struct builtinsFOStr *)mdFindObjects->etc;
73 NSSArena *arena = fo->arena;
75 nss_ZFreeIf(fo->objs);
76 nss_ZFreeIf(fo);
77 nss_ZFreeIf(mdFindObjects);
78 if ((NSSArena *)NULL != arena) {
79 NSSArena_Destroy(arena);
82 return;
85 static NSSCKMDObject *
86 builtins_mdFindObjects_Next
88 NSSCKMDFindObjects *mdFindObjects,
89 NSSCKFWFindObjects *fwFindObjects,
90 NSSCKMDSession *mdSession,
91 NSSCKFWSession *fwSession,
92 NSSCKMDToken *mdToken,
93 NSSCKFWToken *fwToken,
94 NSSCKMDInstance *mdInstance,
95 NSSCKFWInstance *fwInstance,
96 NSSArena *arena,
97 CK_RV *pError
100 struct builtinsFOStr *fo = (struct builtinsFOStr *)mdFindObjects->etc;
101 builtinsInternalObject *io;
103 if( fo->i == fo->n ) {
104 *pError = CKR_OK;
105 return (NSSCKMDObject *)NULL;
108 io = fo->objs[ fo->i ];
109 fo->i++;
111 return nss_builtins_CreateMDObject(arena, io, pError);
114 static int
115 builtins_derUnwrapInt(unsigned char *src, int size, unsigned char **dest) {
116 unsigned char *start = src;
117 int len = 0;
119 if (*src ++ != 2) {
120 return 0;
122 len = *src++;
123 if (len & 0x80) {
124 int count = len & 0x7f;
125 len =0;
127 if (count+2 > size) {
128 return 0;
130 while (count-- > 0) {
131 len = (len << 8) | *src++;
134 if (len + (src-start) != size) {
135 return 0;
137 *dest = src;
138 return len;
141 static CK_BBOOL
142 builtins_attrmatch
144 CK_ATTRIBUTE_PTR a,
145 const NSSItem *b
148 PRBool prb;
150 if( a->ulValueLen != b->size ) {
151 /* match a decoded serial number */
152 if ((a->type == CKA_SERIAL_NUMBER) && (a->ulValueLen < b->size)) {
153 int len;
154 unsigned char *data;
156 len = builtins_derUnwrapInt(b->data,b->size,&data);
157 if ((len == a->ulValueLen) &&
158 nsslibc_memequal(a->pValue, data, len, (PRStatus *)NULL)) {
159 return CK_TRUE;
162 return CK_FALSE;
165 prb = nsslibc_memequal(a->pValue, b->data, b->size, (PRStatus *)NULL);
167 if( PR_TRUE == prb ) {
168 return CK_TRUE;
169 } else {
170 return CK_FALSE;
175 static CK_BBOOL
176 builtins_match
178 CK_ATTRIBUTE_PTR pTemplate,
179 CK_ULONG ulAttributeCount,
180 builtinsInternalObject *o
183 CK_ULONG i;
185 for( i = 0; i < ulAttributeCount; i++ ) {
186 CK_ULONG j;
188 for( j = 0; j < o->n; j++ ) {
189 if( o->types[j] == pTemplate[i].type ) {
190 if( CK_FALSE == builtins_attrmatch(&pTemplate[i], &o->items[j]) ) {
191 return CK_FALSE;
192 } else {
193 break;
198 if( j == o->n ) {
199 /* Loop ran to the end: no matching attribute */
200 return CK_FALSE;
204 /* Every attribute passed */
205 return CK_TRUE;
208 NSS_IMPLEMENT NSSCKMDFindObjects *
209 nss_builtins_FindObjectsInit
211 NSSCKFWSession *fwSession,
212 CK_ATTRIBUTE_PTR pTemplate,
213 CK_ULONG ulAttributeCount,
214 CK_RV *pError
217 /* This could be made more efficient. I'm rather rushed. */
218 NSSArena *arena;
219 NSSCKMDFindObjects *rv = (NSSCKMDFindObjects *)NULL;
220 struct builtinsFOStr *fo = (struct builtinsFOStr *)NULL;
221 builtinsInternalObject **temp = (builtinsInternalObject **)NULL;
222 PRUint32 i;
224 arena = NSSArena_Create();
225 if( (NSSArena *)NULL == arena ) {
226 goto loser;
229 rv = nss_ZNEW(arena, NSSCKMDFindObjects);
230 if( (NSSCKMDFindObjects *)NULL == rv ) {
231 *pError = CKR_HOST_MEMORY;
232 goto loser;
235 fo = nss_ZNEW(arena, struct builtinsFOStr);
236 if( (struct builtinsFOStr *)NULL == fo ) {
237 *pError = CKR_HOST_MEMORY;
238 goto loser;
241 fo->arena = arena;
242 /* fo->n and fo->i are already zero */
244 rv->etc = (void *)fo;
245 rv->Final = builtins_mdFindObjects_Final;
246 rv->Next = builtins_mdFindObjects_Next;
247 rv->null = (void *)NULL;
249 temp = nss_ZNEWARRAY((NSSArena *)NULL, builtinsInternalObject *,
250 nss_builtins_nObjects);
251 if( (builtinsInternalObject **)NULL == temp ) {
252 *pError = CKR_HOST_MEMORY;
253 goto loser;
256 for( i = 0; i < nss_builtins_nObjects; i++ ) {
257 builtinsInternalObject *o = (builtinsInternalObject *)&nss_builtins_data[i];
259 if( CK_TRUE == builtins_match(pTemplate, ulAttributeCount, o) ) {
260 temp[ fo->n ] = o;
261 fo->n++;
265 fo->objs = nss_ZNEWARRAY(arena, builtinsInternalObject *, fo->n);
266 if( (builtinsInternalObject **)NULL == fo->objs ) {
267 *pError = CKR_HOST_MEMORY;
268 goto loser;
271 (void)nsslibc_memcpy(fo->objs, temp, sizeof(builtinsInternalObject *) * fo->n);
272 nss_ZFreeIf(temp);
273 temp = (builtinsInternalObject **)NULL;
275 return rv;
277 loser:
278 nss_ZFreeIf(temp);
279 nss_ZFreeIf(fo);
280 nss_ZFreeIf(rv);
281 if ((NSSArena *)NULL != arena) {
282 NSSArena_Destroy(arena);
284 return (NSSCKMDFindObjects *)NULL;