security/nss/lib/ckfw/dbm/session.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 #include "ckdbm.h"
michael@0 6
michael@0 7 static void
michael@0 8 nss_dbm_mdSession_Close
michael@0 9 (
michael@0 10 NSSCKMDSession *mdSession,
michael@0 11 NSSCKFWSession *fwSession,
michael@0 12 NSSCKMDToken *mdToken,
michael@0 13 NSSCKFWToken *fwToken,
michael@0 14 NSSCKMDInstance *mdInstance,
michael@0 15 NSSCKFWInstance *fwInstance
michael@0 16 )
michael@0 17 {
michael@0 18 nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
michael@0 19
michael@0 20 struct nss_dbm_dbt_node *w;
michael@0 21
michael@0 22 /* Lock */
michael@0 23 {
michael@0 24 if( CKR_OK != NSSCKFWMutex_Lock(session->list_lock) ) {
michael@0 25 return;
michael@0 26 }
michael@0 27
michael@0 28 w = session->session_objects;
michael@0 29 session->session_objects = (struct nss_dbm_dbt_node *)NULL; /* sanity */
michael@0 30
michael@0 31 (void)NSSCKFWMutex_Unlock(session->list_lock);
michael@0 32 }
michael@0 33
michael@0 34 for( ; (struct nss_dbm_dbt_node *)NULL != w; w = w->next ) {
michael@0 35 (void)nss_dbm_db_delete_object(w->dbt);
michael@0 36 }
michael@0 37 }
michael@0 38
michael@0 39 static CK_ULONG
michael@0 40 nss_dbm_mdSession_GetDeviceError
michael@0 41 (
michael@0 42 NSSCKMDSession *mdSession,
michael@0 43 NSSCKFWSession *fwSession,
michael@0 44 NSSCKMDToken *mdToken,
michael@0 45 NSSCKFWToken *fwToken,
michael@0 46 NSSCKMDInstance *mdInstance,
michael@0 47 NSSCKFWInstance *fwInstance
michael@0 48 )
michael@0 49 {
michael@0 50 nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
michael@0 51 return session->deviceError;
michael@0 52 }
michael@0 53
michael@0 54 /* Login isn't needed */
michael@0 55 /* Logout isn't needed */
michael@0 56 /* InitPIN is irrelevant */
michael@0 57 /* SetPIN is irrelevant */
michael@0 58 /* GetOperationStateLen is irrelevant */
michael@0 59 /* GetOperationState is irrelevant */
michael@0 60 /* SetOperationState is irrelevant */
michael@0 61
michael@0 62 static NSSCKMDObject *
michael@0 63 nss_dbm_mdSession_CreateObject
michael@0 64 (
michael@0 65 NSSCKMDSession *mdSession,
michael@0 66 NSSCKFWSession *fwSession,
michael@0 67 NSSCKMDToken *mdToken,
michael@0 68 NSSCKFWToken *fwToken,
michael@0 69 NSSCKMDInstance *mdInstance,
michael@0 70 NSSCKFWInstance *fwInstance,
michael@0 71 NSSArena *handyArenaPointer,
michael@0 72 CK_ATTRIBUTE_PTR pTemplate,
michael@0 73 CK_ULONG ulAttributeCount,
michael@0 74 CK_RV *pError
michael@0 75 )
michael@0 76 {
michael@0 77 nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
michael@0 78 nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
michael@0 79 CK_ULONG i;
michael@0 80 CK_BBOOL isToken = CK_FALSE; /* defaults to false */
michael@0 81 NSSCKMDObject *rv;
michael@0 82 struct nss_dbm_dbt_node *node = (struct nss_dbm_dbt_node *)NULL;
michael@0 83 nss_dbm_object_t *object;
michael@0 84 nss_dbm_db_t *which_db;
michael@0 85
michael@0 86 /* This framework should really pass this to me */
michael@0 87 for( i = 0; i < ulAttributeCount; i++ ) {
michael@0 88 if( CKA_TOKEN == pTemplate[i].type ) {
michael@0 89 isToken = *(CK_BBOOL *)pTemplate[i].pValue;
michael@0 90 break;
michael@0 91 }
michael@0 92 }
michael@0 93
michael@0 94 object = nss_ZNEW(handyArenaPointer, nss_dbm_object_t);
michael@0 95 if( (nss_dbm_object_t *)NULL == object ) {
michael@0 96 *pError = CKR_HOST_MEMORY;
michael@0 97 return (NSSCKMDObject *)NULL;
michael@0 98 }
michael@0 99
michael@0 100 object->arena = handyArenaPointer;
michael@0 101 which_db = isToken ? token->slot->token_db : token->session_db;
michael@0 102
michael@0 103 /* Do this before the actual database call; it's easier to recover from */
michael@0 104 rv = nss_dbm_mdObject_factory(object, pError);
michael@0 105 if( (NSSCKMDObject *)NULL == rv ) {
michael@0 106 return (NSSCKMDObject *)NULL;
michael@0 107 }
michael@0 108
michael@0 109 if( CK_FALSE == isToken ) {
michael@0 110 node = nss_ZNEW(session->arena, struct nss_dbm_dbt_node);
michael@0 111 if( (struct nss_dbm_dbt_node *)NULL == node ) {
michael@0 112 *pError = CKR_HOST_MEMORY;
michael@0 113 return (NSSCKMDObject *)NULL;
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 object->handle = nss_dbm_db_create_object(handyArenaPointer, which_db,
michael@0 118 pTemplate, ulAttributeCount,
michael@0 119 pError, &session->deviceError);
michael@0 120 if( (nss_dbm_dbt_t *)NULL == object->handle ) {
michael@0 121 return (NSSCKMDObject *)NULL;
michael@0 122 }
michael@0 123
michael@0 124 if( CK_FALSE == isToken ) {
michael@0 125 node->dbt = object->handle;
michael@0 126 /* Lock */
michael@0 127 {
michael@0 128 *pError = NSSCKFWMutex_Lock(session->list_lock);
michael@0 129 if( CKR_OK != *pError ) {
michael@0 130 (void)nss_dbm_db_delete_object(object->handle);
michael@0 131 return (NSSCKMDObject *)NULL;
michael@0 132 }
michael@0 133
michael@0 134 node->next = session->session_objects;
michael@0 135 session->session_objects = node;
michael@0 136
michael@0 137 *pError = NSSCKFWMutex_Unlock(session->list_lock);
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 return rv;
michael@0 142 }
michael@0 143
michael@0 144 /* CopyObject isn't needed; the framework will use CreateObject */
michael@0 145
michael@0 146 static NSSCKMDFindObjects *
michael@0 147 nss_dbm_mdSession_FindObjectsInit
michael@0 148 (
michael@0 149 NSSCKMDSession *mdSession,
michael@0 150 NSSCKFWSession *fwSession,
michael@0 151 NSSCKMDToken *mdToken,
michael@0 152 NSSCKFWToken *fwToken,
michael@0 153 NSSCKMDInstance *mdInstance,
michael@0 154 NSSCKFWInstance *fwInstance,
michael@0 155 CK_ATTRIBUTE_PTR pTemplate,
michael@0 156 CK_ULONG ulAttributeCount,
michael@0 157 CK_RV *pError
michael@0 158 )
michael@0 159 {
michael@0 160 nss_dbm_session_t *session = (nss_dbm_session_t *)mdSession->etc;
michael@0 161 nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
michael@0 162 NSSArena *arena;
michael@0 163 nss_dbm_find_t *find;
michael@0 164 NSSCKMDFindObjects *rv;
michael@0 165
michael@0 166 arena = NSSArena_Create();
michael@0 167 if( (NSSArena *)NULL == arena ) {
michael@0 168 *pError = CKR_HOST_MEMORY;
michael@0 169 goto loser;
michael@0 170 }
michael@0 171
michael@0 172 find = nss_ZNEW(arena, nss_dbm_find_t);
michael@0 173 if( (nss_dbm_find_t *)NULL == find ) {
michael@0 174 *pError = CKR_HOST_MEMORY;
michael@0 175 goto loser;
michael@0 176 }
michael@0 177
michael@0 178 find->arena = arena;
michael@0 179 find->list_lock = NSSCKFWInstance_CreateMutex(fwInstance, arena, pError);
michael@0 180 if( (NSSCKFWMutex *)NULL == find->list_lock ) {
michael@0 181 goto loser;
michael@0 182 }
michael@0 183
michael@0 184 *pError = nss_dbm_db_find_objects(find, token->slot->token_db, pTemplate,
michael@0 185 ulAttributeCount, &session->deviceError);
michael@0 186 if( CKR_OK != *pError ) {
michael@0 187 goto loser;
michael@0 188 }
michael@0 189
michael@0 190 *pError = nss_dbm_db_find_objects(find, token->session_db, pTemplate,
michael@0 191 ulAttributeCount, &session->deviceError);
michael@0 192 if( CKR_OK != *pError ) {
michael@0 193 goto loser;
michael@0 194 }
michael@0 195
michael@0 196 rv = nss_dbm_mdFindObjects_factory(find, pError);
michael@0 197 if( (NSSCKMDFindObjects *)NULL == rv ) {
michael@0 198 goto loser;
michael@0 199 }
michael@0 200
michael@0 201 return rv;
michael@0 202
michael@0 203 loser:
michael@0 204 if( (NSSArena *)NULL != arena ) {
michael@0 205 (void)NSSArena_Destroy(arena);
michael@0 206 }
michael@0 207
michael@0 208 return (NSSCKMDFindObjects *)NULL;
michael@0 209 }
michael@0 210
michael@0 211 /* SeedRandom is irrelevant */
michael@0 212 /* GetRandom is irrelevant */
michael@0 213
michael@0 214 NSS_IMPLEMENT NSSCKMDSession *
michael@0 215 nss_dbm_mdSession_factory
michael@0 216 (
michael@0 217 nss_dbm_token_t *token,
michael@0 218 NSSCKFWSession *fwSession,
michael@0 219 NSSCKFWInstance *fwInstance,
michael@0 220 CK_BBOOL rw,
michael@0 221 CK_RV *pError
michael@0 222 )
michael@0 223 {
michael@0 224 NSSArena *arena;
michael@0 225 nss_dbm_session_t *session;
michael@0 226 NSSCKMDSession *rv;
michael@0 227
michael@0 228 arena = NSSCKFWSession_GetArena(fwSession, pError);
michael@0 229
michael@0 230 session = nss_ZNEW(arena, nss_dbm_session_t);
michael@0 231 if( (nss_dbm_session_t *)NULL == session ) {
michael@0 232 *pError = CKR_HOST_MEMORY;
michael@0 233 return (NSSCKMDSession *)NULL;
michael@0 234 }
michael@0 235
michael@0 236 rv = nss_ZNEW(arena, NSSCKMDSession);
michael@0 237 if( (NSSCKMDSession *)NULL == rv ) {
michael@0 238 *pError = CKR_HOST_MEMORY;
michael@0 239 return (NSSCKMDSession *)NULL;
michael@0 240 }
michael@0 241
michael@0 242 session->arena = arena;
michael@0 243 session->token = token;
michael@0 244 session->list_lock = NSSCKFWInstance_CreateMutex(fwInstance, arena, pError);
michael@0 245 if( (NSSCKFWMutex *)NULL == session->list_lock ) {
michael@0 246 return (NSSCKMDSession *)NULL;
michael@0 247 }
michael@0 248
michael@0 249 rv->etc = (void *)session;
michael@0 250 rv->Close = nss_dbm_mdSession_Close;
michael@0 251 rv->GetDeviceError = nss_dbm_mdSession_GetDeviceError;
michael@0 252 /* Login isn't needed */
michael@0 253 /* Logout isn't needed */
michael@0 254 /* InitPIN is irrelevant */
michael@0 255 /* SetPIN is irrelevant */
michael@0 256 /* GetOperationStateLen is irrelevant */
michael@0 257 /* GetOperationState is irrelevant */
michael@0 258 /* SetOperationState is irrelevant */
michael@0 259 rv->CreateObject = nss_dbm_mdSession_CreateObject;
michael@0 260 /* CopyObject isn't needed; the framework will use CreateObject */
michael@0 261 rv->FindObjectsInit = nss_dbm_mdSession_FindObjectsInit;
michael@0 262 rv->null = NULL;
michael@0 263
michael@0 264 return rv;
michael@0 265 }

mercurial