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
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.
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 ***** */
38 static const char CVS_ID
[] = "@(#) $RCSfile: bfind.c,v $ $Revision: 1.6 $ $Date: 2005/01/20 02:25:46 $";
43 #endif /* BUILTINS_H */
48 * This file implements the NSSCKMDFindObjects object for the
49 * "builtin objects" cryptoki module.
52 struct builtinsFOStr
{
56 builtinsInternalObject
**objs
;
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
);
77 nss_ZFreeIf(mdFindObjects
);
78 if ((NSSArena
*)NULL
!= arena
) {
79 NSSArena_Destroy(arena
);
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
,
100 struct builtinsFOStr
*fo
= (struct builtinsFOStr
*)mdFindObjects
->etc
;
101 builtinsInternalObject
*io
;
103 if( fo
->i
== fo
->n
) {
105 return (NSSCKMDObject
*)NULL
;
108 io
= fo
->objs
[ fo
->i
];
111 return nss_builtins_CreateMDObject(arena
, io
, pError
);
115 builtins_derUnwrapInt(unsigned char *src
, int size
, unsigned char **dest
) {
116 unsigned char *start
= src
;
124 int count
= len
& 0x7f;
127 if (count
+2 > size
) {
130 while (count
-- > 0) {
131 len
= (len
<< 8) | *src
++;
134 if (len
+ (src
-start
) != size
) {
150 if( a
->ulValueLen
!= b
->size
) {
151 /* match a decoded serial number */
152 if ((a
->type
== CKA_SERIAL_NUMBER
) && (a
->ulValueLen
< b
->size
)) {
156 len
= builtins_derUnwrapInt(b
->data
,b
->size
,&data
);
157 if ((len
== a
->ulValueLen
) &&
158 nsslibc_memequal(a
->pValue
, data
, len
, (PRStatus
*)NULL
)) {
165 prb
= nsslibc_memequal(a
->pValue
, b
->data
, b
->size
, (PRStatus
*)NULL
);
167 if( PR_TRUE
== prb
) {
178 CK_ATTRIBUTE_PTR pTemplate
,
179 CK_ULONG ulAttributeCount
,
180 builtinsInternalObject
*o
185 for( i
= 0; i
< ulAttributeCount
; i
++ ) {
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
]) ) {
199 /* Loop ran to the end: no matching attribute */
204 /* Every attribute passed */
208 NSS_IMPLEMENT NSSCKMDFindObjects
*
209 nss_builtins_FindObjectsInit
211 NSSCKFWSession
*fwSession
,
212 CK_ATTRIBUTE_PTR pTemplate
,
213 CK_ULONG ulAttributeCount
,
217 /* This could be made more efficient. I'm rather rushed. */
219 NSSCKMDFindObjects
*rv
= (NSSCKMDFindObjects
*)NULL
;
220 struct builtinsFOStr
*fo
= (struct builtinsFOStr
*)NULL
;
221 builtinsInternalObject
**temp
= (builtinsInternalObject
**)NULL
;
224 arena
= NSSArena_Create();
225 if( (NSSArena
*)NULL
== arena
) {
229 rv
= nss_ZNEW(arena
, NSSCKMDFindObjects
);
230 if( (NSSCKMDFindObjects
*)NULL
== rv
) {
231 *pError
= CKR_HOST_MEMORY
;
235 fo
= nss_ZNEW(arena
, struct builtinsFOStr
);
236 if( (struct builtinsFOStr
*)NULL
== fo
) {
237 *pError
= CKR_HOST_MEMORY
;
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
;
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
) ) {
265 fo
->objs
= nss_ZNEWARRAY(arena
, builtinsInternalObject
*, fo
->n
);
266 if( (builtinsInternalObject
**)NULL
== fo
->objs
) {
267 *pError
= CKR_HOST_MEMORY
;
271 (void)nsslibc_memcpy(fo
->objs
, temp
, sizeof(builtinsInternalObject
*) * fo
->n
);
273 temp
= (builtinsInternalObject
**)NULL
;
281 if ((NSSArena
*)NULL
!= arena
) {
282 NSSArena_Destroy(arena
);
284 return (NSSCKMDFindObjects
*)NULL
;