Back to index

lightning-sunbird  0.9+nobinonly
bfind.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: bfind.c,v $ $Revision: 1.6 $ $Date: 2005/01/20 02:25:46 $";
00039 #endif /* DEBUG */
00040 
00041 #ifndef BUILTINS_H
00042 #include "builtins.h"
00043 #endif /* BUILTINS_H */
00044 
00045 /*
00046  * builtins/find.c
00047  *
00048  * This file implements the NSSCKMDFindObjects object for the
00049  * "builtin objects" cryptoki module.
00050  */
00051 
00052 struct builtinsFOStr {
00053   NSSArena *arena;
00054   CK_ULONG n;
00055   CK_ULONG i;
00056   builtinsInternalObject **objs;
00057 };
00058 
00059 static void
00060 builtins_mdFindObjects_Final
00061 (
00062   NSSCKMDFindObjects *mdFindObjects,
00063   NSSCKFWFindObjects *fwFindObjects,
00064   NSSCKMDSession *mdSession,
00065   NSSCKFWSession *fwSession,
00066   NSSCKMDToken *mdToken,
00067   NSSCKFWToken *fwToken,
00068   NSSCKMDInstance *mdInstance,
00069   NSSCKFWInstance *fwInstance
00070 )
00071 {
00072   struct builtinsFOStr *fo = (struct builtinsFOStr *)mdFindObjects->etc;
00073   NSSArena *arena = fo->arena;
00074 
00075   nss_ZFreeIf(fo->objs);
00076   nss_ZFreeIf(fo);
00077   nss_ZFreeIf(mdFindObjects);
00078   if ((NSSArena *)NULL != arena) {
00079     NSSArena_Destroy(arena);
00080   }
00081 
00082   return;
00083 }
00084 
00085 static NSSCKMDObject *
00086 builtins_mdFindObjects_Next
00087 (
00088   NSSCKMDFindObjects *mdFindObjects,
00089   NSSCKFWFindObjects *fwFindObjects,
00090   NSSCKMDSession *mdSession,
00091   NSSCKFWSession *fwSession,
00092   NSSCKMDToken *mdToken,
00093   NSSCKFWToken *fwToken,
00094   NSSCKMDInstance *mdInstance,
00095   NSSCKFWInstance *fwInstance,
00096   NSSArena *arena,
00097   CK_RV *pError
00098 )
00099 {
00100   struct builtinsFOStr *fo = (struct builtinsFOStr *)mdFindObjects->etc;
00101   builtinsInternalObject *io;
00102 
00103   if( fo->i == fo->n ) {
00104     *pError = CKR_OK;
00105     return (NSSCKMDObject *)NULL;
00106   }
00107 
00108   io = fo->objs[ fo->i ];
00109   fo->i++;
00110 
00111   return nss_builtins_CreateMDObject(arena, io, pError);
00112 }
00113 
00114 static int
00115 builtins_derUnwrapInt(unsigned char *src, int size, unsigned char **dest) {
00116     unsigned char *start = src;
00117     int len = 0;
00118 
00119     if (*src ++ != 2) {
00120        return 0;
00121     }
00122     len = *src++;
00123     if (len & 0x80) {
00124        int count = len & 0x7f;
00125        len =0;
00126 
00127        if (count+2 > size) {
00128            return 0;
00129        }
00130        while (count-- > 0) {
00131            len = (len << 8) | *src++;
00132        }
00133     }
00134     if (len + (src-start) != size) {
00135        return 0;
00136     }
00137     *dest = src;
00138     return len;
00139 }
00140 
00141 static CK_BBOOL
00142 builtins_attrmatch
00143 (
00144   CK_ATTRIBUTE_PTR a,
00145   const NSSItem *b
00146 )
00147 {
00148   PRBool prb;
00149 
00150   if( a->ulValueLen != b->size ) {
00151     /* match a decoded serial number */
00152     if ((a->type == CKA_SERIAL_NUMBER) && (a->ulValueLen < b->size)) {
00153        int len;
00154        unsigned char *data;
00155 
00156        len = builtins_derUnwrapInt(b->data,b->size,&data);
00157        if ((len == a->ulValueLen) && 
00158               nsslibc_memequal(a->pValue, data, len, (PRStatus *)NULL)) {
00159            return CK_TRUE;
00160        }
00161     }
00162     return CK_FALSE;
00163   }
00164 
00165   prb = nsslibc_memequal(a->pValue, b->data, b->size, (PRStatus *)NULL);
00166 
00167   if( PR_TRUE == prb ) {
00168     return CK_TRUE;
00169   } else {
00170     return CK_FALSE;
00171   }
00172 }
00173 
00174 
00175 static CK_BBOOL
00176 builtins_match
00177 (
00178   CK_ATTRIBUTE_PTR pTemplate,
00179   CK_ULONG ulAttributeCount,
00180   builtinsInternalObject *o
00181 )
00182 {
00183   CK_ULONG i;
00184 
00185   for( i = 0; i < ulAttributeCount; i++ ) {
00186     CK_ULONG j;
00187 
00188     for( j = 0; j < o->n; j++ ) {
00189       if( o->types[j] == pTemplate[i].type ) {
00190         if( CK_FALSE == builtins_attrmatch(&pTemplate[i], &o->items[j]) ) {
00191           return CK_FALSE;
00192         } else {
00193           break;
00194         }
00195       }
00196     }
00197 
00198     if( j == o->n ) {
00199       /* Loop ran to the end: no matching attribute */
00200       return CK_FALSE;
00201     }
00202   }
00203 
00204   /* Every attribute passed */
00205   return CK_TRUE;
00206 }
00207 
00208 NSS_IMPLEMENT NSSCKMDFindObjects *
00209 nss_builtins_FindObjectsInit
00210 (
00211   NSSCKFWSession *fwSession,
00212   CK_ATTRIBUTE_PTR pTemplate,
00213   CK_ULONG ulAttributeCount,
00214   CK_RV *pError
00215 )
00216 {
00217   /* This could be made more efficient.  I'm rather rushed. */
00218   NSSArena *arena;
00219   NSSCKMDFindObjects *rv = (NSSCKMDFindObjects *)NULL;
00220   struct builtinsFOStr *fo = (struct builtinsFOStr *)NULL;
00221   builtinsInternalObject **temp = (builtinsInternalObject **)NULL;
00222   PRUint32 i;
00223 
00224   arena = NSSArena_Create();
00225   if( (NSSArena *)NULL == arena ) {
00226     goto loser;
00227   }
00228 
00229   rv = nss_ZNEW(arena, NSSCKMDFindObjects);
00230   if( (NSSCKMDFindObjects *)NULL == rv ) {
00231     *pError = CKR_HOST_MEMORY;
00232     goto loser;
00233   }
00234 
00235   fo = nss_ZNEW(arena, struct builtinsFOStr);
00236   if( (struct builtinsFOStr *)NULL == fo ) {
00237     *pError = CKR_HOST_MEMORY;
00238     goto loser;
00239   }
00240 
00241   fo->arena = arena;
00242   /* fo->n and fo->i are already zero */
00243 
00244   rv->etc = (void *)fo;
00245   rv->Final = builtins_mdFindObjects_Final;
00246   rv->Next = builtins_mdFindObjects_Next;
00247   rv->null = (void *)NULL;
00248 
00249   temp = nss_ZNEWARRAY((NSSArena *)NULL, builtinsInternalObject *, 
00250                        nss_builtins_nObjects);
00251   if( (builtinsInternalObject **)NULL == temp ) {
00252     *pError = CKR_HOST_MEMORY;
00253     goto loser;
00254   }
00255 
00256   for( i = 0; i < nss_builtins_nObjects; i++ ) {
00257     builtinsInternalObject *o = (builtinsInternalObject *)&nss_builtins_data[i];
00258 
00259     if( CK_TRUE == builtins_match(pTemplate, ulAttributeCount, o) ) {
00260       temp[ fo->n ] = o;
00261       fo->n++;
00262     }
00263   }
00264 
00265   fo->objs = nss_ZNEWARRAY(arena, builtinsInternalObject *, fo->n);
00266   if( (builtinsInternalObject **)NULL == fo->objs ) {
00267     *pError = CKR_HOST_MEMORY;
00268     goto loser;
00269   }
00270 
00271   (void)nsslibc_memcpy(fo->objs, temp, sizeof(builtinsInternalObject *) * fo->n);
00272   nss_ZFreeIf(temp);
00273   temp = (builtinsInternalObject **)NULL;
00274 
00275   return rv;
00276 
00277  loser:
00278   nss_ZFreeIf(temp);
00279   nss_ZFreeIf(fo);
00280   nss_ZFreeIf(rv);
00281   if ((NSSArena *)NULL != arena) {
00282      NSSArena_Destroy(arena);
00283   }
00284   return (NSSCKMDFindObjects *)NULL;
00285 }
00286