Back to index

lightning-sunbird  0.9+nobinonly
bobject.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: bobject.c,v $ $Revision: 1.4 $ $Date: 2005/01/20 02:25:46 $";
00039 #endif /* DEBUG */
00040 
00041 #include "builtins.h"
00042 
00043 /*
00044  * builtins/object.c
00045  *
00046  * This file implements the NSSCKMDObject object for the
00047  * "builtin objects" cryptoki module.
00048  */
00049 
00050 /*
00051  * Finalize - unneeded
00052  * Destroy - CKR_SESSION_READ_ONLY
00053  * IsTokenObject - CK_TRUE
00054  * GetAttributeCount
00055  * GetAttributeTypes
00056  * GetAttributeSize
00057  * GetAttribute
00058  * SetAttribute - unneeded
00059  * GetObjectSize
00060  */
00061 
00062 static CK_RV
00063 builtins_mdObject_Destroy
00064 (
00065   NSSCKMDObject *mdObject,
00066   NSSCKFWObject *fwObject,
00067   NSSCKMDSession *mdSession,
00068   NSSCKFWSession *fwSession,
00069   NSSCKMDToken *mdToken,
00070   NSSCKFWToken *fwToken,
00071   NSSCKMDInstance *mdInstance,
00072   NSSCKFWInstance *fwInstance
00073 )
00074 {
00075   return CKR_SESSION_READ_ONLY;
00076 }
00077 
00078 static CK_BBOOL
00079 builtins_mdObject_IsTokenObject
00080 (
00081   NSSCKMDObject *mdObject,
00082   NSSCKFWObject *fwObject,
00083   NSSCKMDSession *mdSession,
00084   NSSCKFWSession *fwSession,
00085   NSSCKMDToken *mdToken,
00086   NSSCKFWToken *fwToken,
00087   NSSCKMDInstance *mdInstance,
00088   NSSCKFWInstance *fwInstance
00089 )
00090 {
00091   return CK_TRUE;
00092 }
00093 
00094 static CK_ULONG
00095 builtins_mdObject_GetAttributeCount
00096 (
00097   NSSCKMDObject *mdObject,
00098   NSSCKFWObject *fwObject,
00099   NSSCKMDSession *mdSession,
00100   NSSCKFWSession *fwSession,
00101   NSSCKMDToken *mdToken,
00102   NSSCKFWToken *fwToken,
00103   NSSCKMDInstance *mdInstance,
00104   NSSCKFWInstance *fwInstance,
00105   CK_RV *pError
00106 )
00107 {
00108   builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
00109   return io->n;
00110 }
00111 
00112 static CK_RV
00113 builtins_mdObject_GetAttributeTypes
00114 (
00115   NSSCKMDObject *mdObject,
00116   NSSCKFWObject *fwObject,
00117   NSSCKMDSession *mdSession,
00118   NSSCKFWSession *fwSession,
00119   NSSCKMDToken *mdToken,
00120   NSSCKFWToken *fwToken,
00121   NSSCKMDInstance *mdInstance,
00122   NSSCKFWInstance *fwInstance,
00123   CK_ATTRIBUTE_TYPE_PTR typeArray,
00124   CK_ULONG ulCount
00125 )
00126 {
00127   builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
00128   CK_ULONG i;
00129 
00130   if( io->n != ulCount ) {
00131     return CKR_BUFFER_TOO_SMALL;
00132   }
00133 
00134   for( i = 0; i < io->n; i++ ) {
00135     typeArray[i] = io->types[i];
00136   }
00137 
00138   return CKR_OK;
00139 }
00140 
00141 static CK_ULONG
00142 builtins_mdObject_GetAttributeSize
00143 (
00144   NSSCKMDObject *mdObject,
00145   NSSCKFWObject *fwObject,
00146   NSSCKMDSession *mdSession,
00147   NSSCKFWSession *fwSession,
00148   NSSCKMDToken *mdToken,
00149   NSSCKFWToken *fwToken,
00150   NSSCKMDInstance *mdInstance,
00151   NSSCKFWInstance *fwInstance,
00152   CK_ATTRIBUTE_TYPE attribute,
00153   CK_RV *pError
00154 )
00155 {
00156   builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
00157   CK_ULONG i;
00158 
00159   for( i = 0; i < io->n; i++ ) {
00160     if( attribute == io->types[i] ) {
00161       return (CK_ULONG)(io->items[i].size);
00162     }
00163   }
00164 
00165   *pError = CKR_ATTRIBUTE_TYPE_INVALID;
00166   return 0;
00167 }
00168 
00169 static NSSCKFWItem
00170 builtins_mdObject_GetAttribute
00171 (
00172   NSSCKMDObject *mdObject,
00173   NSSCKFWObject *fwObject,
00174   NSSCKMDSession *mdSession,
00175   NSSCKFWSession *fwSession,
00176   NSSCKMDToken *mdToken,
00177   NSSCKFWToken *fwToken,
00178   NSSCKMDInstance *mdInstance,
00179   NSSCKFWInstance *fwInstance,
00180   CK_ATTRIBUTE_TYPE attribute,
00181   CK_RV *pError
00182 )
00183 {
00184   NSSCKFWItem mdItem;
00185   builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
00186   CK_ULONG i;
00187 
00188   mdItem.needsFreeing = PR_FALSE;
00189   mdItem.item = (NSSItem*) NULL;
00190 
00191   for( i = 0; i < io->n; i++ ) {
00192     if( attribute == io->types[i] ) {
00193       mdItem.item = (NSSItem*) &io->items[i];
00194       return mdItem;
00195     }
00196   }
00197 
00198   *pError = CKR_ATTRIBUTE_TYPE_INVALID;
00199   return mdItem;
00200 }
00201 
00202 static CK_ULONG
00203 builtins_mdObject_GetObjectSize
00204 (
00205   NSSCKMDObject *mdObject,
00206   NSSCKFWObject *fwObject,
00207   NSSCKMDSession *mdSession,
00208   NSSCKFWSession *fwSession,
00209   NSSCKMDToken *mdToken,
00210   NSSCKFWToken *fwToken,
00211   NSSCKMDInstance *mdInstance,
00212   NSSCKFWInstance *fwInstance,
00213   CK_RV *pError
00214 )
00215 {
00216   builtinsInternalObject *io = (builtinsInternalObject *)mdObject->etc;
00217   CK_ULONG i;
00218   CK_ULONG rv = sizeof(CK_ULONG);
00219 
00220   for( i = 0; i < io->n; i++ ) {
00221     rv += sizeof(CK_ATTRIBUTE_TYPE) + sizeof(NSSItem) + io->items[i].size;
00222   }
00223 
00224   return rv;
00225 }
00226 
00227 static const NSSCKMDObject
00228 builtins_prototype_mdObject = {
00229   (void *)NULL, /* etc */
00230   NULL, /* Finalize */
00231   builtins_mdObject_Destroy,
00232   builtins_mdObject_IsTokenObject,
00233   builtins_mdObject_GetAttributeCount,
00234   builtins_mdObject_GetAttributeTypes,
00235   builtins_mdObject_GetAttributeSize,
00236   builtins_mdObject_GetAttribute,
00237   NULL, /* FreeAttribute */
00238   NULL, /* SetAttribute */
00239   builtins_mdObject_GetObjectSize,
00240   (void *)NULL /* null terminator */
00241 };
00242 
00243 NSS_IMPLEMENT NSSCKMDObject *
00244 nss_builtins_CreateMDObject
00245 (
00246   NSSArena *arena,
00247   builtinsInternalObject *io,
00248   CK_RV *pError
00249 )
00250 {
00251   if ( (void*)NULL == io->mdObject.etc) {
00252     (void) nsslibc_memcpy(&io->mdObject,&builtins_prototype_mdObject,
00253                                    sizeof(builtins_prototype_mdObject));
00254     io->mdObject.etc = (void *)io;
00255   }
00256 
00257   return &io->mdObject;
00258 }