Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* |
michael@0 | 6 | * find.c |
michael@0 | 7 | * |
michael@0 | 8 | * This file implements the nssCKFWFindObjects type and methods. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #ifndef CK_H |
michael@0 | 12 | #include "ck.h" |
michael@0 | 13 | #endif /* CK_H */ |
michael@0 | 14 | |
michael@0 | 15 | /* |
michael@0 | 16 | * NSSCKFWFindObjects |
michael@0 | 17 | * |
michael@0 | 18 | * -- create/destroy -- |
michael@0 | 19 | * nssCKFWFindObjects_Create |
michael@0 | 20 | * nssCKFWFindObjects_Destroy |
michael@0 | 21 | * |
michael@0 | 22 | * -- public accessors -- |
michael@0 | 23 | * NSSCKFWFindObjects_GetMDFindObjects |
michael@0 | 24 | * |
michael@0 | 25 | * -- implement public accessors -- |
michael@0 | 26 | * nssCKFWFindObjects_GetMDFindObjects |
michael@0 | 27 | * |
michael@0 | 28 | * -- private accessors -- |
michael@0 | 29 | * |
michael@0 | 30 | * -- module fronts -- |
michael@0 | 31 | * nssCKFWFindObjects_Next |
michael@0 | 32 | */ |
michael@0 | 33 | |
michael@0 | 34 | struct NSSCKFWFindObjectsStr { |
michael@0 | 35 | NSSCKFWMutex *mutex; /* merely to serialise the MDObject calls */ |
michael@0 | 36 | NSSCKMDFindObjects *mdfo1; |
michael@0 | 37 | NSSCKMDFindObjects *mdfo2; |
michael@0 | 38 | NSSCKFWSession *fwSession; |
michael@0 | 39 | NSSCKMDSession *mdSession; |
michael@0 | 40 | NSSCKFWToken *fwToken; |
michael@0 | 41 | NSSCKMDToken *mdToken; |
michael@0 | 42 | NSSCKFWInstance *fwInstance; |
michael@0 | 43 | NSSCKMDInstance *mdInstance; |
michael@0 | 44 | |
michael@0 | 45 | NSSCKMDFindObjects *mdFindObjects; /* varies */ |
michael@0 | 46 | }; |
michael@0 | 47 | |
michael@0 | 48 | #ifdef DEBUG |
michael@0 | 49 | /* |
michael@0 | 50 | * But first, the pointer-tracking stuff. |
michael@0 | 51 | * |
michael@0 | 52 | * NOTE: the pointer-tracking support in NSS/base currently relies |
michael@0 | 53 | * upon NSPR's CallOnce support. That, however, relies upon NSPR's |
michael@0 | 54 | * locking, which is tied into the runtime. We need a pointer-tracker |
michael@0 | 55 | * implementation that uses the locks supplied through C_Initialize. |
michael@0 | 56 | * That support, however, can be filled in later. So for now, I'll |
michael@0 | 57 | * just do these routines as no-ops. |
michael@0 | 58 | */ |
michael@0 | 59 | |
michael@0 | 60 | static CK_RV |
michael@0 | 61 | findObjects_add_pointer |
michael@0 | 62 | ( |
michael@0 | 63 | const NSSCKFWFindObjects *fwFindObjects |
michael@0 | 64 | ) |
michael@0 | 65 | { |
michael@0 | 66 | return CKR_OK; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | static CK_RV |
michael@0 | 70 | findObjects_remove_pointer |
michael@0 | 71 | ( |
michael@0 | 72 | const NSSCKFWFindObjects *fwFindObjects |
michael@0 | 73 | ) |
michael@0 | 74 | { |
michael@0 | 75 | return CKR_OK; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | NSS_IMPLEMENT CK_RV |
michael@0 | 79 | nssCKFWFindObjects_verifyPointer |
michael@0 | 80 | ( |
michael@0 | 81 | const NSSCKFWFindObjects *fwFindObjects |
michael@0 | 82 | ) |
michael@0 | 83 | { |
michael@0 | 84 | return CKR_OK; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | #endif /* DEBUG */ |
michael@0 | 88 | |
michael@0 | 89 | /* |
michael@0 | 90 | * nssCKFWFindObjects_Create |
michael@0 | 91 | * |
michael@0 | 92 | */ |
michael@0 | 93 | NSS_EXTERN NSSCKFWFindObjects * |
michael@0 | 94 | nssCKFWFindObjects_Create |
michael@0 | 95 | ( |
michael@0 | 96 | NSSCKFWSession *fwSession, |
michael@0 | 97 | NSSCKFWToken *fwToken, |
michael@0 | 98 | NSSCKFWInstance *fwInstance, |
michael@0 | 99 | NSSCKMDFindObjects *mdFindObjects1, |
michael@0 | 100 | NSSCKMDFindObjects *mdFindObjects2, |
michael@0 | 101 | CK_RV *pError |
michael@0 | 102 | ) |
michael@0 | 103 | { |
michael@0 | 104 | NSSCKFWFindObjects *fwFindObjects = NULL; |
michael@0 | 105 | NSSCKMDSession *mdSession; |
michael@0 | 106 | NSSCKMDToken *mdToken; |
michael@0 | 107 | NSSCKMDInstance *mdInstance; |
michael@0 | 108 | |
michael@0 | 109 | mdSession = nssCKFWSession_GetMDSession(fwSession); |
michael@0 | 110 | mdToken = nssCKFWToken_GetMDToken(fwToken); |
michael@0 | 111 | mdInstance = nssCKFWInstance_GetMDInstance(fwInstance); |
michael@0 | 112 | |
michael@0 | 113 | fwFindObjects = nss_ZNEW(NULL, NSSCKFWFindObjects); |
michael@0 | 114 | if (!fwFindObjects) { |
michael@0 | 115 | *pError = CKR_HOST_MEMORY; |
michael@0 | 116 | goto loser; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | fwFindObjects->mdfo1 = mdFindObjects1; |
michael@0 | 120 | fwFindObjects->mdfo2 = mdFindObjects2; |
michael@0 | 121 | fwFindObjects->fwSession = fwSession; |
michael@0 | 122 | fwFindObjects->mdSession = mdSession; |
michael@0 | 123 | fwFindObjects->fwToken = fwToken; |
michael@0 | 124 | fwFindObjects->mdToken = mdToken; |
michael@0 | 125 | fwFindObjects->fwInstance = fwInstance; |
michael@0 | 126 | fwFindObjects->mdInstance = mdInstance; |
michael@0 | 127 | |
michael@0 | 128 | fwFindObjects->mutex = nssCKFWInstance_CreateMutex(fwInstance, NULL, pError); |
michael@0 | 129 | if (!fwFindObjects->mutex) { |
michael@0 | 130 | goto loser; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | #ifdef DEBUG |
michael@0 | 134 | *pError = findObjects_add_pointer(fwFindObjects); |
michael@0 | 135 | if( CKR_OK != *pError ) { |
michael@0 | 136 | goto loser; |
michael@0 | 137 | } |
michael@0 | 138 | #endif /* DEBUG */ |
michael@0 | 139 | |
michael@0 | 140 | return fwFindObjects; |
michael@0 | 141 | |
michael@0 | 142 | loser: |
michael@0 | 143 | if( fwFindObjects ) { |
michael@0 | 144 | if( NULL != mdFindObjects1 ) { |
michael@0 | 145 | if( NULL != mdFindObjects1->Final ) { |
michael@0 | 146 | fwFindObjects->mdFindObjects = mdFindObjects1; |
michael@0 | 147 | mdFindObjects1->Final(mdFindObjects1, fwFindObjects, mdSession, |
michael@0 | 148 | fwSession, mdToken, fwToken, mdInstance, fwInstance); |
michael@0 | 149 | } |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | if( NULL != mdFindObjects2 ) { |
michael@0 | 153 | if( NULL != mdFindObjects2->Final ) { |
michael@0 | 154 | fwFindObjects->mdFindObjects = mdFindObjects2; |
michael@0 | 155 | mdFindObjects2->Final(mdFindObjects2, fwFindObjects, mdSession, |
michael@0 | 156 | fwSession, mdToken, fwToken, mdInstance, fwInstance); |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | nss_ZFreeIf(fwFindObjects); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | if( CKR_OK == *pError ) { |
michael@0 | 164 | *pError = CKR_GENERAL_ERROR; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | return (NSSCKFWFindObjects *)NULL; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | |
michael@0 | 171 | /* |
michael@0 | 172 | * nssCKFWFindObjects_Destroy |
michael@0 | 173 | * |
michael@0 | 174 | */ |
michael@0 | 175 | NSS_EXTERN void |
michael@0 | 176 | nssCKFWFindObjects_Destroy |
michael@0 | 177 | ( |
michael@0 | 178 | NSSCKFWFindObjects *fwFindObjects |
michael@0 | 179 | ) |
michael@0 | 180 | { |
michael@0 | 181 | #ifdef NSSDEBUG |
michael@0 | 182 | if( CKR_OK != nssCKFWFindObjects_verifyPointer(fwFindObjects) ) { |
michael@0 | 183 | return; |
michael@0 | 184 | } |
michael@0 | 185 | #endif /* NSSDEBUG */ |
michael@0 | 186 | |
michael@0 | 187 | (void)nssCKFWMutex_Destroy(fwFindObjects->mutex); |
michael@0 | 188 | |
michael@0 | 189 | if (fwFindObjects->mdfo1) { |
michael@0 | 190 | if (fwFindObjects->mdfo1->Final) { |
michael@0 | 191 | fwFindObjects->mdFindObjects = fwFindObjects->mdfo1; |
michael@0 | 192 | fwFindObjects->mdfo1->Final(fwFindObjects->mdfo1, fwFindObjects, |
michael@0 | 193 | fwFindObjects->mdSession, fwFindObjects->fwSession, |
michael@0 | 194 | fwFindObjects->mdToken, fwFindObjects->fwToken, |
michael@0 | 195 | fwFindObjects->mdInstance, fwFindObjects->fwInstance); |
michael@0 | 196 | } |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | if (fwFindObjects->mdfo2) { |
michael@0 | 200 | if (fwFindObjects->mdfo2->Final) { |
michael@0 | 201 | fwFindObjects->mdFindObjects = fwFindObjects->mdfo2; |
michael@0 | 202 | fwFindObjects->mdfo2->Final(fwFindObjects->mdfo2, fwFindObjects, |
michael@0 | 203 | fwFindObjects->mdSession, fwFindObjects->fwSession, |
michael@0 | 204 | fwFindObjects->mdToken, fwFindObjects->fwToken, |
michael@0 | 205 | fwFindObjects->mdInstance, fwFindObjects->fwInstance); |
michael@0 | 206 | } |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | nss_ZFreeIf(fwFindObjects); |
michael@0 | 210 | |
michael@0 | 211 | #ifdef DEBUG |
michael@0 | 212 | (void)findObjects_remove_pointer(fwFindObjects); |
michael@0 | 213 | #endif /* DEBUG */ |
michael@0 | 214 | |
michael@0 | 215 | return; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | /* |
michael@0 | 219 | * nssCKFWFindObjects_GetMDFindObjects |
michael@0 | 220 | * |
michael@0 | 221 | */ |
michael@0 | 222 | NSS_EXTERN NSSCKMDFindObjects * |
michael@0 | 223 | nssCKFWFindObjects_GetMDFindObjects |
michael@0 | 224 | ( |
michael@0 | 225 | NSSCKFWFindObjects *fwFindObjects |
michael@0 | 226 | ) |
michael@0 | 227 | { |
michael@0 | 228 | #ifdef NSSDEBUG |
michael@0 | 229 | if( CKR_OK != nssCKFWFindObjects_verifyPointer(fwFindObjects) ) { |
michael@0 | 230 | return (NSSCKMDFindObjects *)NULL; |
michael@0 | 231 | } |
michael@0 | 232 | #endif /* NSSDEBUG */ |
michael@0 | 233 | |
michael@0 | 234 | return fwFindObjects->mdFindObjects; |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | /* |
michael@0 | 238 | * nssCKFWFindObjects_Next |
michael@0 | 239 | * |
michael@0 | 240 | */ |
michael@0 | 241 | NSS_EXTERN NSSCKFWObject * |
michael@0 | 242 | nssCKFWFindObjects_Next |
michael@0 | 243 | ( |
michael@0 | 244 | NSSCKFWFindObjects *fwFindObjects, |
michael@0 | 245 | NSSArena *arenaOpt, |
michael@0 | 246 | CK_RV *pError |
michael@0 | 247 | ) |
michael@0 | 248 | { |
michael@0 | 249 | NSSCKMDObject *mdObject; |
michael@0 | 250 | NSSCKFWObject *fwObject = (NSSCKFWObject *)NULL; |
michael@0 | 251 | NSSArena *objArena; |
michael@0 | 252 | |
michael@0 | 253 | #ifdef NSSDEBUG |
michael@0 | 254 | if (!pError) { |
michael@0 | 255 | return (NSSCKFWObject *)NULL; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | *pError = nssCKFWFindObjects_verifyPointer(fwFindObjects); |
michael@0 | 259 | if( CKR_OK != *pError ) { |
michael@0 | 260 | return (NSSCKFWObject *)NULL; |
michael@0 | 261 | } |
michael@0 | 262 | #endif /* NSSDEBUG */ |
michael@0 | 263 | |
michael@0 | 264 | *pError = nssCKFWMutex_Lock(fwFindObjects->mutex); |
michael@0 | 265 | if( CKR_OK != *pError ) { |
michael@0 | 266 | return (NSSCKFWObject *)NULL; |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | if (fwFindObjects->mdfo1) { |
michael@0 | 270 | if (fwFindObjects->mdfo1->Next) { |
michael@0 | 271 | fwFindObjects->mdFindObjects = fwFindObjects->mdfo1; |
michael@0 | 272 | mdObject = fwFindObjects->mdfo1->Next(fwFindObjects->mdfo1, |
michael@0 | 273 | fwFindObjects, fwFindObjects->mdSession, fwFindObjects->fwSession, |
michael@0 | 274 | fwFindObjects->mdToken, fwFindObjects->fwToken, |
michael@0 | 275 | fwFindObjects->mdInstance, fwFindObjects->fwInstance, |
michael@0 | 276 | arenaOpt, pError); |
michael@0 | 277 | if (!mdObject) { |
michael@0 | 278 | if( CKR_OK != *pError ) { |
michael@0 | 279 | goto done; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | /* All done. */ |
michael@0 | 283 | fwFindObjects->mdfo1->Final(fwFindObjects->mdfo1, fwFindObjects, |
michael@0 | 284 | fwFindObjects->mdSession, fwFindObjects->fwSession, |
michael@0 | 285 | fwFindObjects->mdToken, fwFindObjects->fwToken, |
michael@0 | 286 | fwFindObjects->mdInstance, fwFindObjects->fwInstance); |
michael@0 | 287 | fwFindObjects->mdfo1 = (NSSCKMDFindObjects *)NULL; |
michael@0 | 288 | } else { |
michael@0 | 289 | goto wrap; |
michael@0 | 290 | } |
michael@0 | 291 | } |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | if (fwFindObjects->mdfo2) { |
michael@0 | 295 | if (fwFindObjects->mdfo2->Next) { |
michael@0 | 296 | fwFindObjects->mdFindObjects = fwFindObjects->mdfo2; |
michael@0 | 297 | mdObject = fwFindObjects->mdfo2->Next(fwFindObjects->mdfo2, |
michael@0 | 298 | fwFindObjects, fwFindObjects->mdSession, fwFindObjects->fwSession, |
michael@0 | 299 | fwFindObjects->mdToken, fwFindObjects->fwToken, |
michael@0 | 300 | fwFindObjects->mdInstance, fwFindObjects->fwInstance, |
michael@0 | 301 | arenaOpt, pError); |
michael@0 | 302 | if (!mdObject) { |
michael@0 | 303 | if( CKR_OK != *pError ) { |
michael@0 | 304 | goto done; |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | /* All done. */ |
michael@0 | 308 | fwFindObjects->mdfo2->Final(fwFindObjects->mdfo2, fwFindObjects, |
michael@0 | 309 | fwFindObjects->mdSession, fwFindObjects->fwSession, |
michael@0 | 310 | fwFindObjects->mdToken, fwFindObjects->fwToken, |
michael@0 | 311 | fwFindObjects->mdInstance, fwFindObjects->fwInstance); |
michael@0 | 312 | fwFindObjects->mdfo2 = (NSSCKMDFindObjects *)NULL; |
michael@0 | 313 | } else { |
michael@0 | 314 | goto wrap; |
michael@0 | 315 | } |
michael@0 | 316 | } |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | /* No more objects */ |
michael@0 | 320 | *pError = CKR_OK; |
michael@0 | 321 | goto done; |
michael@0 | 322 | |
michael@0 | 323 | wrap: |
michael@0 | 324 | /* |
michael@0 | 325 | * This seems is less than ideal-- we should determine if it's a token |
michael@0 | 326 | * object or a session object, and use the appropriate arena. |
michael@0 | 327 | * But that duplicates logic in nssCKFWObject_IsTokenObject. |
michael@0 | 328 | * Also we should lookup the real session the object was created on |
michael@0 | 329 | * if the object was a session object... however this code is actually |
michael@0 | 330 | * correct because nssCKFWObject_Create will return a cached version of |
michael@0 | 331 | * the object from it's hash. This is necessary because 1) we don't want |
michael@0 | 332 | * to create an arena style leak (where our arena grows with every search), |
michael@0 | 333 | * and 2) we want the same object to always have the same ID. This means |
michael@0 | 334 | * the only case the nssCKFWObject_Create() will need the objArena and the |
michael@0 | 335 | * Session is in the case of token objects (session objects should already |
michael@0 | 336 | * exist in the cache from their initial creation). So this code is correct, |
michael@0 | 337 | * but it depends on nssCKFWObject_Create caching all objects. |
michael@0 | 338 | */ |
michael@0 | 339 | objArena = nssCKFWToken_GetArena(fwFindObjects->fwToken, pError); |
michael@0 | 340 | if (!objArena) { |
michael@0 | 341 | if( CKR_OK == *pError ) { |
michael@0 | 342 | *pError = CKR_HOST_MEMORY; |
michael@0 | 343 | } |
michael@0 | 344 | goto done; |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | fwObject = nssCKFWObject_Create(objArena, mdObject, |
michael@0 | 348 | NULL, fwFindObjects->fwToken, |
michael@0 | 349 | fwFindObjects->fwInstance, pError); |
michael@0 | 350 | if (!fwObject) { |
michael@0 | 351 | if( CKR_OK == *pError ) { |
michael@0 | 352 | *pError = CKR_GENERAL_ERROR; |
michael@0 | 353 | } |
michael@0 | 354 | } |
michael@0 | 355 | |
michael@0 | 356 | done: |
michael@0 | 357 | (void)nssCKFWMutex_Unlock(fwFindObjects->mutex); |
michael@0 | 358 | return fwObject; |
michael@0 | 359 | } |
michael@0 | 360 | |
michael@0 | 361 | /* |
michael@0 | 362 | * NSSCKFWFindObjects_GetMDFindObjects |
michael@0 | 363 | * |
michael@0 | 364 | */ |
michael@0 | 365 | |
michael@0 | 366 | NSS_EXTERN NSSCKMDFindObjects * |
michael@0 | 367 | NSSCKFWFindObjects_GetMDFindObjects |
michael@0 | 368 | ( |
michael@0 | 369 | NSSCKFWFindObjects *fwFindObjects |
michael@0 | 370 | ) |
michael@0 | 371 | { |
michael@0 | 372 | #ifdef DEBUG |
michael@0 | 373 | if( CKR_OK != nssCKFWFindObjects_verifyPointer(fwFindObjects) ) { |
michael@0 | 374 | return (NSSCKMDFindObjects *)NULL; |
michael@0 | 375 | } |
michael@0 | 376 | #endif /* DEBUG */ |
michael@0 | 377 | |
michael@0 | 378 | return nssCKFWFindObjects_GetMDFindObjects(fwFindObjects); |
michael@0 | 379 | } |