Back to index

lightning-sunbird  0.9+nobinonly
token.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifdef DEBUG
00038 static const char CVS_ID[] = "@(#) $RCSfile: token.c,v $ $Revision: 1.3.28.1 $ $Date: 2006/03/02 22:45:14 $";
00039 #endif /* DEBUG */
00040 
00041 #include "ckdbm.h"
00042 
00043 static CK_RV
00044 nss_dbm_mdToken_Setup
00045 (
00046   NSSCKMDToken *mdToken,
00047   NSSCKFWToken *fwToken,
00048   NSSCKMDInstance *mdInstance,
00049   NSSCKFWInstance *fwInstance
00050 )
00051 {
00052   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00053   CK_RV rv = CKR_OK;
00054 
00055   token->arena = NSSCKFWToken_GetArena(fwToken, &rv);
00056   token->session_db = nss_dbm_db_open(token->arena, fwInstance, (char *)NULL, 
00057                                       O_RDWR|O_CREAT, &rv);
00058   if( (nss_dbm_db_t *)NULL == token->session_db ) {
00059     return rv;
00060   }
00061 
00062   /* Add a label record if there isn't one? */
00063 
00064   return CKR_OK;
00065 }
00066 
00067 static void
00068 nss_dbm_mdToken_Invalidate
00069 (
00070   NSSCKMDToken *mdToken,
00071   NSSCKFWToken *fwToken,
00072   NSSCKMDInstance *mdInstance,
00073   NSSCKFWInstance *fwInstance
00074 )
00075 {
00076   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00077 
00078   if( (nss_dbm_db_t *)NULL != token->session_db ) {
00079     nss_dbm_db_close(token->session_db);
00080     token->session_db = (nss_dbm_db_t *)NULL;
00081   }
00082 }
00083 
00084 static CK_RV
00085 nss_dbm_mdToken_InitToken
00086 (
00087   NSSCKMDToken *mdToken,
00088   NSSCKFWToken *fwToken,
00089   NSSCKMDInstance *mdInstance,
00090   NSSCKFWInstance *fwInstance,
00091   NSSItem *pin,
00092   NSSUTF8 *label
00093 )
00094 {
00095   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00096   nss_dbm_instance_t *instance = (nss_dbm_instance_t *)mdInstance->etc;
00097   CK_RV rv;
00098 
00099   /* Wipe the session object data */
00100   
00101   if( (nss_dbm_db_t *)NULL != token->session_db ) {
00102     nss_dbm_db_close(token->session_db);
00103   }
00104 
00105   token->session_db = nss_dbm_db_open(token->arena, fwInstance, (char *)NULL, 
00106                                       O_RDWR|O_CREAT, &rv);
00107   if( (nss_dbm_db_t *)NULL == token->session_db ) {
00108     return rv;
00109   }
00110 
00111   /* Wipe the token object data */
00112 
00113   if( token->slot->flags & O_RDWR ) {
00114     if( (nss_dbm_db_t *)NULL != token->slot->token_db ) {
00115       nss_dbm_db_close(token->slot->token_db);
00116     }
00117 
00118     token->slot->token_db = nss_dbm_db_open(instance->arena, fwInstance, 
00119                                             token->slot->filename,
00120                                             token->slot->flags | O_CREAT | O_TRUNC, 
00121                                             &rv);
00122     if( (nss_dbm_db_t *)NULL == token->slot->token_db ) {
00123       return rv;
00124     }
00125 
00126     /* PIN is irrelevant */
00127 
00128     rv = nss_dbm_db_set_label(token->slot->token_db, label);
00129     if( CKR_OK != rv ) {
00130       return rv;
00131     }
00132   }
00133 
00134   return CKR_OK;
00135 }
00136 
00137 static NSSUTF8 *
00138 nss_dbm_mdToken_GetLabel
00139 (
00140   NSSCKMDToken *mdToken,
00141   NSSCKFWToken *fwToken,
00142   NSSCKMDInstance *mdInstance,
00143   NSSCKFWInstance *fwInstance,
00144   CK_RV *pError
00145 )
00146 {
00147   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00148 
00149   if( (NSSUTF8 *)NULL == token->label ) {
00150     token->label = nss_dbm_db_get_label(token->slot->token_db, token->arena, pError);
00151   }
00152 
00153   /* If no label has been set, return *something* */
00154   if( (NSSUTF8 *)NULL == token->label ) {
00155     return token->slot->filename;
00156   }
00157 
00158   return token->label;
00159 }
00160 
00161 static NSSUTF8 *
00162 nss_dbm_mdToken_GetManufacturerID
00163 (
00164   NSSCKMDToken *mdToken,
00165   NSSCKFWToken *fwToken,
00166   NSSCKMDInstance *mdInstance,
00167   NSSCKFWInstance *fwInstance,
00168   CK_RV *pError
00169 )
00170 {
00171   return "mozilla.org NSS";
00172 }
00173 
00174 static NSSUTF8 *
00175 nss_dbm_mdToken_GetModel
00176 (
00177   NSSCKMDToken *mdToken,
00178   NSSCKFWToken *fwToken,
00179   NSSCKMDInstance *mdInstance,
00180   NSSCKFWInstance *fwInstance,
00181   CK_RV *pError
00182 )
00183 {
00184   return "dbm";
00185 }
00186 
00187 /* GetSerialNumber is irrelevant */
00188 /* GetHasRNG defaults to CK_FALSE */
00189 
00190 static CK_BBOOL
00191 nss_dbm_mdToken_GetIsWriteProtected
00192 (
00193   NSSCKMDToken *mdToken,
00194   NSSCKFWToken *fwToken,
00195   NSSCKMDInstance *mdInstance,
00196   NSSCKFWInstance *fwInstance
00197 )
00198 {
00199   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00200 
00201   if( token->slot->flags & O_RDWR ) {
00202     return CK_FALSE;
00203   } else {
00204     return CK_TRUE;
00205   }
00206 }
00207 
00208 /* GetLoginRequired defaults to CK_FALSE */
00209 /* GetUserPinInitialized defaults to CK_FALSE */
00210 /* GetRestoreKeyNotNeeded is irrelevant */
00211 /* GetHasClockOnToken defaults to CK_FALSE */
00212 /* GetHasProtectedAuthenticationPath defaults to CK_FALSE */
00213 /* GetSupportsDualCryptoOperations is irrelevant */
00214 
00215 static CK_ULONG
00216 nss_dbm_mdToken_effectively_infinite
00217 (
00218   NSSCKMDToken *mdToken,
00219   NSSCKFWToken *fwToken,
00220   NSSCKMDInstance *mdInstance,
00221   NSSCKFWInstance *fwInstance
00222 )
00223 {
00224   return CK_EFFECTIVELY_INFINITE;
00225 }
00226 
00227 static CK_VERSION
00228 nss_dbm_mdToken_GetHardwareVersion
00229 (
00230   NSSCKMDToken *mdToken,
00231   NSSCKFWToken *fwToken,
00232   NSSCKMDInstance *mdInstance,
00233   NSSCKFWInstance *fwInstance
00234 )
00235 {
00236   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00237   return nss_dbm_db_get_format_version(token->slot->token_db);
00238 }
00239 
00240 /* GetFirmwareVersion is irrelevant */
00241 /* GetUTCTime is irrelevant */
00242 
00243 static NSSCKMDSession *
00244 nss_dbm_mdToken_OpenSession
00245 (
00246   NSSCKMDToken *mdToken,
00247   NSSCKFWToken *fwToken,
00248   NSSCKMDInstance *mdInstance,
00249   NSSCKFWInstance *fwInstance,
00250   NSSCKFWSession *fwSession,
00251   CK_BBOOL rw,
00252   CK_RV *pError
00253 )
00254 {
00255   nss_dbm_token_t *token = (nss_dbm_token_t *)mdToken->etc;
00256   return nss_dbm_mdSession_factory(token, fwSession, fwInstance, rw, pError);
00257 }
00258 
00259 /* GetMechanismCount defaults to zero */
00260 /* GetMechanismTypes is irrelevant */
00261 /* GetMechanism is irrelevant */
00262 
00263 NSS_IMPLEMENT NSSCKMDToken *
00264 nss_dbm_mdToken_factory
00265 (
00266   nss_dbm_slot_t *slot,
00267   CK_RV *pError
00268 )
00269 {
00270   nss_dbm_token_t *token;
00271   NSSCKMDToken *rv;
00272 
00273   token = nss_ZNEW(slot->instance->arena, nss_dbm_token_t);
00274   if( (nss_dbm_token_t *)NULL == token ) {
00275     *pError = CKR_HOST_MEMORY;
00276     return (NSSCKMDToken *)NULL;
00277   }
00278 
00279   rv = nss_ZNEW(slot->instance->arena, NSSCKMDToken);
00280   if( (NSSCKMDToken *)NULL == rv ) {
00281     *pError = CKR_HOST_MEMORY;
00282     return (NSSCKMDToken *)NULL;
00283   }
00284 
00285   token->slot = slot;
00286 
00287   rv->etc = (void *)token;
00288   rv->Setup = nss_dbm_mdToken_Setup;
00289   rv->Invalidate = nss_dbm_mdToken_Invalidate;
00290   rv->InitToken = nss_dbm_mdToken_InitToken;
00291   rv->GetLabel = nss_dbm_mdToken_GetLabel;
00292   rv->GetManufacturerID = nss_dbm_mdToken_GetManufacturerID;
00293   rv->GetModel = nss_dbm_mdToken_GetModel;
00294   /*  GetSerialNumber is irrelevant */
00295   /*  GetHasRNG defaults to CK_FALSE */
00296   rv->GetIsWriteProtected = nss_dbm_mdToken_GetIsWriteProtected;
00297   /*  GetLoginRequired defaults to CK_FALSE */
00298   /*  GetUserPinInitialized defaults to CK_FALSE */
00299   /*  GetRestoreKeyNotNeeded is irrelevant */
00300   /*  GetHasClockOnToken defaults to CK_FALSE */
00301   /*  GetHasProtectedAuthenticationPath defaults to CK_FALSE */
00302   /*  GetSupportsDualCryptoOperations is irrelevant */
00303   rv->GetMaxSessionCount = nss_dbm_mdToken_effectively_infinite;
00304   rv->GetMaxRwSessionCount = nss_dbm_mdToken_effectively_infinite;
00305   /*  GetMaxPinLen is irrelevant */
00306   /*  GetMinPinLen is irrelevant */
00307   /*  GetTotalPublicMemory defaults to CK_UNAVAILABLE_INFORMATION */
00308   /*  GetFreePublicMemory defaults to CK_UNAVAILABLE_INFORMATION */
00309   /*  GetTotalPrivateMemory defaults to CK_UNAVAILABLE_INFORMATION */
00310   /*  GetFreePrivateMemory defaults to CK_UNAVAILABLE_INFORMATION */
00311   rv->GetHardwareVersion = nss_dbm_mdToken_GetHardwareVersion;
00312   /*  GetFirmwareVersion is irrelevant */
00313   /*  GetUTCTime is irrelevant */
00314   rv->OpenSession = nss_dbm_mdToken_OpenSession;
00315   rv->null = NULL;
00316 
00317   return rv;
00318 }