| OLD | NEW |
| (Empty) | |
| 1 /* ***** BEGIN LICENSE BLOCK ***** |
| 2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 3 * |
| 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/ |
| 8 * |
| 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. |
| 13 * |
| 14 * The Original Code is the Netscape security libraries. |
| 15 * |
| 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. |
| 20 * |
| 21 * Contributor(s): |
| 22 * |
| 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. |
| 34 * |
| 35 * ***** END LICENSE BLOCK ***** */ |
| 36 |
| 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 */ |
| 40 |
| 41 #ifndef BUILTINS_H |
| 42 #include "builtins.h" |
| 43 #endif /* BUILTINS_H */ |
| 44 |
| 45 /* |
| 46 * builtins/find.c |
| 47 * |
| 48 * This file implements the NSSCKMDFindObjects object for the |
| 49 * "builtin objects" cryptoki module. |
| 50 */ |
| 51 |
| 52 struct builtinsFOStr { |
| 53 NSSArena *arena; |
| 54 CK_ULONG n; |
| 55 CK_ULONG i; |
| 56 builtinsInternalObject **objs; |
| 57 }; |
| 58 |
| 59 static void |
| 60 builtins_mdFindObjects_Final |
| 61 ( |
| 62 NSSCKMDFindObjects *mdFindObjects, |
| 63 NSSCKFWFindObjects *fwFindObjects, |
| 64 NSSCKMDSession *mdSession, |
| 65 NSSCKFWSession *fwSession, |
| 66 NSSCKMDToken *mdToken, |
| 67 NSSCKFWToken *fwToken, |
| 68 NSSCKMDInstance *mdInstance, |
| 69 NSSCKFWInstance *fwInstance |
| 70 ) |
| 71 { |
| 72 struct builtinsFOStr *fo = (struct builtinsFOStr *)mdFindObjects->etc; |
| 73 NSSArena *arena = fo->arena; |
| 74 |
| 75 nss_ZFreeIf(fo->objs); |
| 76 nss_ZFreeIf(fo); |
| 77 nss_ZFreeIf(mdFindObjects); |
| 78 if ((NSSArena *)NULL != arena) { |
| 79 NSSArena_Destroy(arena); |
| 80 } |
| 81 |
| 82 return; |
| 83 } |
| 84 |
| 85 static NSSCKMDObject * |
| 86 builtins_mdFindObjects_Next |
| 87 ( |
| 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 |
| 98 ) |
| 99 { |
| 100 struct builtinsFOStr *fo = (struct builtinsFOStr *)mdFindObjects->etc; |
| 101 builtinsInternalObject *io; |
| 102 |
| 103 if( fo->i == fo->n ) { |
| 104 *pError = CKR_OK; |
| 105 return (NSSCKMDObject *)NULL; |
| 106 } |
| 107 |
| 108 io = fo->objs[ fo->i ]; |
| 109 fo->i++; |
| 110 |
| 111 return nss_builtins_CreateMDObject(arena, io, pError); |
| 112 } |
| 113 |
| 114 static int |
| 115 builtins_derUnwrapInt(unsigned char *src, int size, unsigned char **dest) { |
| 116 unsigned char *start = src; |
| 117 int len = 0; |
| 118 |
| 119 if (*src ++ != 2) { |
| 120 return 0; |
| 121 } |
| 122 len = *src++; |
| 123 if (len & 0x80) { |
| 124 int count = len & 0x7f; |
| 125 len =0; |
| 126 |
| 127 if (count+2 > size) { |
| 128 return 0; |
| 129 } |
| 130 while (count-- > 0) { |
| 131 len = (len << 8) | *src++; |
| 132 } |
| 133 } |
| 134 if (len + (src-start) != size) { |
| 135 return 0; |
| 136 } |
| 137 *dest = src; |
| 138 return len; |
| 139 } |
| 140 |
| 141 static CK_BBOOL |
| 142 builtins_attrmatch |
| 143 ( |
| 144 CK_ATTRIBUTE_PTR a, |
| 145 const NSSItem *b |
| 146 ) |
| 147 { |
| 148 PRBool prb; |
| 149 |
| 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; |
| 155 |
| 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; |
| 160 } |
| 161 } |
| 162 return CK_FALSE; |
| 163 } |
| 164 |
| 165 prb = nsslibc_memequal(a->pValue, b->data, b->size, (PRStatus *)NULL); |
| 166 |
| 167 if( PR_TRUE == prb ) { |
| 168 return CK_TRUE; |
| 169 } else { |
| 170 return CK_FALSE; |
| 171 } |
| 172 } |
| 173 |
| 174 |
| 175 static CK_BBOOL |
| 176 builtins_match |
| 177 ( |
| 178 CK_ATTRIBUTE_PTR pTemplate, |
| 179 CK_ULONG ulAttributeCount, |
| 180 builtinsInternalObject *o |
| 181 ) |
| 182 { |
| 183 CK_ULONG i; |
| 184 |
| 185 for( i = 0; i < ulAttributeCount; i++ ) { |
| 186 CK_ULONG j; |
| 187 |
| 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; |
| 194 } |
| 195 } |
| 196 } |
| 197 |
| 198 if( j == o->n ) { |
| 199 /* Loop ran to the end: no matching attribute */ |
| 200 return CK_FALSE; |
| 201 } |
| 202 } |
| 203 |
| 204 /* Every attribute passed */ |
| 205 return CK_TRUE; |
| 206 } |
| 207 |
| 208 NSS_IMPLEMENT NSSCKMDFindObjects * |
| 209 nss_builtins_FindObjectsInit |
| 210 ( |
| 211 NSSCKFWSession *fwSession, |
| 212 CK_ATTRIBUTE_PTR pTemplate, |
| 213 CK_ULONG ulAttributeCount, |
| 214 CK_RV *pError |
| 215 ) |
| 216 { |
| 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; |
| 223 |
| 224 arena = NSSArena_Create(); |
| 225 if( (NSSArena *)NULL == arena ) { |
| 226 goto loser; |
| 227 } |
| 228 |
| 229 rv = nss_ZNEW(arena, NSSCKMDFindObjects); |
| 230 if( (NSSCKMDFindObjects *)NULL == rv ) { |
| 231 *pError = CKR_HOST_MEMORY; |
| 232 goto loser; |
| 233 } |
| 234 |
| 235 fo = nss_ZNEW(arena, struct builtinsFOStr); |
| 236 if( (struct builtinsFOStr *)NULL == fo ) { |
| 237 *pError = CKR_HOST_MEMORY; |
| 238 goto loser; |
| 239 } |
| 240 |
| 241 fo->arena = arena; |
| 242 /* fo->n and fo->i are already zero */ |
| 243 |
| 244 rv->etc = (void *)fo; |
| 245 rv->Final = builtins_mdFindObjects_Final; |
| 246 rv->Next = builtins_mdFindObjects_Next; |
| 247 rv->null = (void *)NULL; |
| 248 |
| 249 temp = nss_ZNEWARRAY((NSSArena *)NULL, builtinsInternalObject *, |
| 250 nss_builtins_nObjects); |
| 251 if( (builtinsInternalObject **)NULL == temp ) { |
| 252 *pError = CKR_HOST_MEMORY; |
| 253 goto loser; |
| 254 } |
| 255 |
| 256 for( i = 0; i < nss_builtins_nObjects; i++ ) { |
| 257 builtinsInternalObject *o = (builtinsInternalObject *)&nss_builtins_data[i]; |
| 258 |
| 259 if( CK_TRUE == builtins_match(pTemplate, ulAttributeCount, o) ) { |
| 260 temp[ fo->n ] = o; |
| 261 fo->n++; |
| 262 } |
| 263 } |
| 264 |
| 265 fo->objs = nss_ZNEWARRAY(arena, builtinsInternalObject *, fo->n); |
| 266 if( (builtinsInternalObject **)NULL == fo->objs ) { |
| 267 *pError = CKR_HOST_MEMORY; |
| 268 goto loser; |
| 269 } |
| 270 |
| 271 (void)nsslibc_memcpy(fo->objs, temp, sizeof(builtinsInternalObject *) * fo->n)
; |
| 272 nss_ZFreeIf(temp); |
| 273 temp = (builtinsInternalObject **)NULL; |
| 274 |
| 275 return rv; |
| 276 |
| 277 loser: |
| 278 nss_ZFreeIf(temp); |
| 279 nss_ZFreeIf(fo); |
| 280 nss_ZFreeIf(rv); |
| 281 if ((NSSArena *)NULL != arena) { |
| 282 NSSArena_Destroy(arena); |
| 283 } |
| 284 return (NSSCKMDFindObjects *)NULL; |
| 285 } |
| 286 |
| OLD | NEW |