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

http://github.com/zpao/v8monkey · C · 301 lines · 204 code · 40 blank · 57 comment · 41 complexity · d83ec20cdef400d445af60cbe36290e5 MD5 · raw file

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