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: token.c,v $ $Revision: 1.4 $ $Date: 2006/03/02 22:48:54 $";
46 NSSCKMDToken
*mdToken
,
47 NSSCKFWToken
*fwToken
,
48 NSSCKMDInstance
*mdInstance
,
49 NSSCKFWInstance
*fwInstance
52 nss_dbm_token_t
*token
= (nss_dbm_token_t
*)mdToken
->etc
;
55 token
->arena
= NSSCKFWToken_GetArena(fwToken
, &rv
);
56 token
->session_db
= nss_dbm_db_open(token
->arena
, fwInstance
, (char *)NULL
,
58 if( (nss_dbm_db_t
*)NULL
== token
->session_db
) {
62 /* Add a label record if there isn't one? */
68 nss_dbm_mdToken_Invalidate
70 NSSCKMDToken
*mdToken
,
71 NSSCKFWToken
*fwToken
,
72 NSSCKMDInstance
*mdInstance
,
73 NSSCKFWInstance
*fwInstance
76 nss_dbm_token_t
*token
= (nss_dbm_token_t
*)mdToken
->etc
;
78 if( (nss_dbm_db_t
*)NULL
!= token
->session_db
) {
79 nss_dbm_db_close(token
->session_db
);
80 token
->session_db
= (nss_dbm_db_t
*)NULL
;
85 nss_dbm_mdToken_InitToken
87 NSSCKMDToken
*mdToken
,
88 NSSCKFWToken
*fwToken
,
89 NSSCKMDInstance
*mdInstance
,
90 NSSCKFWInstance
*fwInstance
,
95 nss_dbm_token_t
*token
= (nss_dbm_token_t
*)mdToken
->etc
;
96 nss_dbm_instance_t
*instance
= (nss_dbm_instance_t
*)mdInstance
->etc
;
99 /* Wipe the session object data */
101 if( (nss_dbm_db_t
*)NULL
!= token
->session_db
) {
102 nss_dbm_db_close(token
->session_db
);
105 token
->session_db
= nss_dbm_db_open(token
->arena
, fwInstance
, (char *)NULL
,
106 O_RDWR
|O_CREAT
, &rv
);
107 if( (nss_dbm_db_t
*)NULL
== token
->session_db
) {
111 /* Wipe the token object data */
113 if( token
->slot
->flags
& O_RDWR
) {
114 if( (nss_dbm_db_t
*)NULL
!= token
->slot
->token_db
) {
115 nss_dbm_db_close(token
->slot
->token_db
);
118 token
->slot
->token_db
= nss_dbm_db_open(instance
->arena
, fwInstance
,
119 token
->slot
->filename
,
120 token
->slot
->flags
| O_CREAT
| O_TRUNC
,
122 if( (nss_dbm_db_t
*)NULL
== token
->slot
->token_db
) {
126 /* PIN is irrelevant */
128 rv
= nss_dbm_db_set_label(token
->slot
->token_db
, label
);
138 nss_dbm_mdToken_GetLabel
140 NSSCKMDToken
*mdToken
,
141 NSSCKFWToken
*fwToken
,
142 NSSCKMDInstance
*mdInstance
,
143 NSSCKFWInstance
*fwInstance
,
147 nss_dbm_token_t
*token
= (nss_dbm_token_t
*)mdToken
->etc
;
149 if( (NSSUTF8
*)NULL
== token
->label
) {
150 token
->label
= nss_dbm_db_get_label(token
->slot
->token_db
, token
->arena
, pError
);
153 /* If no label has been set, return *something* */
154 if( (NSSUTF8
*)NULL
== token
->label
) {
155 return token
->slot
->filename
;
162 nss_dbm_mdToken_GetManufacturerID
164 NSSCKMDToken
*mdToken
,
165 NSSCKFWToken
*fwToken
,
166 NSSCKMDInstance
*mdInstance
,
167 NSSCKFWInstance
*fwInstance
,
171 return "mozilla.org NSS";
175 nss_dbm_mdToken_GetModel
177 NSSCKMDToken
*mdToken
,
178 NSSCKFWToken
*fwToken
,
179 NSSCKMDInstance
*mdInstance
,
180 NSSCKFWInstance
*fwInstance
,
187 /* GetSerialNumber is irrelevant */
188 /* GetHasRNG defaults to CK_FALSE */
191 nss_dbm_mdToken_GetIsWriteProtected
193 NSSCKMDToken
*mdToken
,
194 NSSCKFWToken
*fwToken
,
195 NSSCKMDInstance
*mdInstance
,
196 NSSCKFWInstance
*fwInstance
199 nss_dbm_token_t
*token
= (nss_dbm_token_t
*)mdToken
->etc
;
201 if( token
->slot
->flags
& O_RDWR
) {
208 /* GetLoginRequired defaults to CK_FALSE */
209 /* GetUserPinInitialized defaults to CK_FALSE */
210 /* GetRestoreKeyNotNeeded is irrelevant */
211 /* GetHasClockOnToken defaults to CK_FALSE */
212 /* GetHasProtectedAuthenticationPath defaults to CK_FALSE */
213 /* GetSupportsDualCryptoOperations is irrelevant */
216 nss_dbm_mdToken_effectively_infinite
218 NSSCKMDToken
*mdToken
,
219 NSSCKFWToken
*fwToken
,
220 NSSCKMDInstance
*mdInstance
,
221 NSSCKFWInstance
*fwInstance
224 return CK_EFFECTIVELY_INFINITE
;
228 nss_dbm_mdToken_GetHardwareVersion
230 NSSCKMDToken
*mdToken
,
231 NSSCKFWToken
*fwToken
,
232 NSSCKMDInstance
*mdInstance
,
233 NSSCKFWInstance
*fwInstance
236 nss_dbm_token_t
*token
= (nss_dbm_token_t
*)mdToken
->etc
;
237 return nss_dbm_db_get_format_version(token
->slot
->token_db
);
240 /* GetFirmwareVersion is irrelevant */
241 /* GetUTCTime is irrelevant */
243 static NSSCKMDSession
*
244 nss_dbm_mdToken_OpenSession
246 NSSCKMDToken
*mdToken
,
247 NSSCKFWToken
*fwToken
,
248 NSSCKMDInstance
*mdInstance
,
249 NSSCKFWInstance
*fwInstance
,
250 NSSCKFWSession
*fwSession
,
255 nss_dbm_token_t
*token
= (nss_dbm_token_t
*)mdToken
->etc
;
256 return nss_dbm_mdSession_factory(token
, fwSession
, fwInstance
, rw
, pError
);
259 /* GetMechanismCount defaults to zero */
260 /* GetMechanismTypes is irrelevant */
261 /* GetMechanism is irrelevant */
263 NSS_IMPLEMENT NSSCKMDToken
*
264 nss_dbm_mdToken_factory
266 nss_dbm_slot_t
*slot
,
270 nss_dbm_token_t
*token
;
273 token
= nss_ZNEW(slot
->instance
->arena
, nss_dbm_token_t
);
274 if( (nss_dbm_token_t
*)NULL
== token
) {
275 *pError
= CKR_HOST_MEMORY
;
276 return (NSSCKMDToken
*)NULL
;
279 rv
= nss_ZNEW(slot
->instance
->arena
, NSSCKMDToken
);
280 if( (NSSCKMDToken
*)NULL
== rv
) {
281 *pError
= CKR_HOST_MEMORY
;
282 return (NSSCKMDToken
*)NULL
;
287 rv
->etc
= (void *)token
;
288 rv
->Setup
= nss_dbm_mdToken_Setup
;
289 rv
->Invalidate
= nss_dbm_mdToken_Invalidate
;
290 rv
->InitToken
= nss_dbm_mdToken_InitToken
;
291 rv
->GetLabel
= nss_dbm_mdToken_GetLabel
;
292 rv
->GetManufacturerID
= nss_dbm_mdToken_GetManufacturerID
;
293 rv
->GetModel
= nss_dbm_mdToken_GetModel
;
294 /* GetSerialNumber is irrelevant */
295 /* GetHasRNG defaults to CK_FALSE */
296 rv
->GetIsWriteProtected
= nss_dbm_mdToken_GetIsWriteProtected
;
297 /* GetLoginRequired defaults to CK_FALSE */
298 /* GetUserPinInitialized defaults to CK_FALSE */
299 /* GetRestoreKeyNotNeeded is irrelevant */
300 /* GetHasClockOnToken defaults to CK_FALSE */
301 /* GetHasProtectedAuthenticationPath defaults to CK_FALSE */
302 /* GetSupportsDualCryptoOperations is irrelevant */
303 rv
->GetMaxSessionCount
= nss_dbm_mdToken_effectively_infinite
;
304 rv
->GetMaxRwSessionCount
= nss_dbm_mdToken_effectively_infinite
;
305 /* GetMaxPinLen is irrelevant */
306 /* GetMinPinLen is irrelevant */
307 /* GetTotalPublicMemory defaults to CK_UNAVAILABLE_INFORMATION */
308 /* GetFreePublicMemory defaults to CK_UNAVAILABLE_INFORMATION */
309 /* GetTotalPrivateMemory defaults to CK_UNAVAILABLE_INFORMATION */
310 /* GetFreePrivateMemory defaults to CK_UNAVAILABLE_INFORMATION */
311 rv
->GetHardwareVersion
= nss_dbm_mdToken_GetHardwareVersion
;
312 /* GetFirmwareVersion is irrelevant */
313 /* GetUTCTime is irrelevant */
314 rv
->OpenSession
= nss_dbm_mdToken_OpenSession
;