| 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 |
* Rob Crittenden ([email protected]) |
| 23 |
* |
| 24 |
* Alternatively, the contents of this file may be used under the terms of |
| 25 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 26 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 27 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 28 |
* of those above. If you wish to allow use of your version of this file only |
| 29 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 30 |
* use your version of this file under the terms of the MPL, indicate your |
| 31 |
* decision by deleting the provisions above and replace them with the notice |
| 32 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
| 33 |
* the provisions above, a recipient may use your version of this file under |
| 34 |
* the terms of any one of the MPL, the GPL or the LGPL. |
| 35 |
* |
| 36 |
* ***** END LICENSE BLOCK ***** */ |
| 37 |
|
| 38 |
#include "ckpem.h" |
| 39 |
|
| 40 |
/* |
| 41 |
* pfind.c |
| 42 |
* |
| 43 |
* This file implements the NSSCKMDFindObjects object for the |
| 44 |
* "PEM objects" cryptoki module. |
| 45 |
*/ |
| 46 |
|
| 47 |
struct pemFOStr { |
| 48 |
NSSArena *arena; |
| 49 |
CK_ULONG n; |
| 50 |
CK_ULONG i; |
| 51 |
pemInternalObject **objs; |
| 52 |
}; |
| 53 |
|
| 54 |
static void |
| 55 |
pem_mdFindObjects_Final |
| 56 |
( |
| 57 |
NSSCKMDFindObjects * mdFindObjects, |
| 58 |
NSSCKFWFindObjects * fwFindObjects, |
| 59 |
NSSCKMDSession * mdSession, |
| 60 |
NSSCKFWSession * fwSession, |
| 61 |
NSSCKMDToken * mdToken, |
| 62 |
NSSCKFWToken * fwToken, |
| 63 |
NSSCKMDInstance * mdInstance, |
| 64 |
NSSCKFWInstance * fwInstance |
| 65 |
) |
| 66 |
{ |
| 67 |
struct pemFOStr *fo = (struct pemFOStr *) mdFindObjects->etc; |
| 68 |
NSSArena *arena = fo->arena; |
| 69 |
|
| 70 |
NSS_ZFreeIf(fo->objs); |
| 71 |
NSS_ZFreeIf(fo); |
| 72 |
NSS_ZFreeIf(mdFindObjects); |
| 73 |
if ((NSSArena *) NULL != arena) { |
| 74 |
NSSArena_Destroy(arena); |
| 75 |
} |
| 76 |
|
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
static NSSCKMDObject * |
| 81 |
pem_mdFindObjects_Next |
| 82 |
( |
| 83 |
NSSCKMDFindObjects * mdFindObjects, |
| 84 |
NSSCKFWFindObjects * fwFindObjects, |
| 85 |
NSSCKMDSession * mdSession, |
| 86 |
NSSCKFWSession * fwSession, |
| 87 |
NSSCKMDToken * mdToken, |
| 88 |
NSSCKFWToken * fwToken, |
| 89 |
NSSCKMDInstance * mdInstance, |
| 90 |
NSSCKFWInstance * fwInstance, |
| 91 |
NSSArena * arena, |
| 92 |
CK_RV * pError |
| 93 |
) |
| 94 |
{ |
| 95 |
struct pemFOStr *fo = (struct pemFOStr *) mdFindObjects->etc; |
| 96 |
pemInternalObject *io; |
| 97 |
|
| 98 |
plog("pem_FindObjects_Next: "); |
| 99 |
|
| 100 |
if (fo->i == fo->n) { |
| 101 |
plog("Done creating objects\n"); |
| 102 |
*pError = CKR_OK; |
| 103 |
return (NSSCKMDObject *) NULL; |
| 104 |
} |
| 105 |
|
| 106 |
io = fo->objs[fo->i]; |
| 107 |
fo->i++; |
| 108 |
|
| 109 |
plog("Creating object for type %d\n", io->type); |
| 110 |
|
| 111 |
if (!io->extRef) { |
| 112 |
/* increase reference count only once as ckfw will free the found |
| 113 |
* object only once */ |
| 114 |
io->extRef = CK_TRUE; |
| 115 |
io->refCount ++; |
| 116 |
} |
| 117 |
|
| 118 |
return pem_CreateMDObject(arena, io, pError); |
| 119 |
} |
| 120 |
|
| 121 |
static char * pem_attr_name(CK_ATTRIBUTE_TYPE type) { |
| 122 |
switch(type) { |
| 123 |
case CKA_CLASS: |
| 124 |
return "CKA_CLASS"; |
| 125 |
case CKA_TOKEN: |
| 126 |
return "CKA_TOKEN"; |
| 127 |
case CKA_PRIVATE: |
| 128 |
return "CKA_PRIVATE"; |
| 129 |
case CKA_LABEL: |
| 130 |
return "CKA_LABEL"; |
| 131 |
case CKA_APPLICATION: |
| 132 |
return "CKA_APPLICATION"; |
| 133 |
case CKA_VALUE: |
| 134 |
return "CKA_VALUE"; |
| 135 |
case CKA_OBJECT_ID: |
| 136 |
return "CKA_OBJECT_ID"; |
| 137 |
case CKA_CERTIFICATE_TYPE: |
| 138 |
return "CKA_CERTIFICATE_TYPE"; |
| 139 |
case CKA_ISSUER: |
| 140 |
return "CKA_ISSUER"; |
| 141 |
case CKA_SERIAL_NUMBER: |
| 142 |
return "CKA_SERIAL_NUMBER"; |
| 143 |
case CKA_ID: |
| 144 |
return "CKA_ID"; |
| 145 |
default: |
| 146 |
return "unknown"; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
static CK_BBOOL |
| 151 |
pem_attrmatch(CK_ATTRIBUTE_PTR a, pemInternalObject * o) { |
| 152 |
PRBool prb; |
| 153 |
const NSSItem *b; |
| 154 |
CK_RV error = CKR_OK; |
| 155 |
|
| 156 |
b = pem_FetchAttribute(o, a->type, &error); |
| 157 |
if (b == NULL) { |
| 158 |
plog("pem_attrmatch %s %08x: CK_FALSE attr not found\n", pem_attr_name(a->type), a->type); |
| 159 |
return CK_FALSE; |
| 160 |
} |
| 161 |
|
| 162 |
if (a->ulValueLen != b->size) { |
| 163 |
plog("pem_attrmatch %s %08x: CK_FALSE size mismatch %d vs %d\n", pem_attr_name(a->type), a->type, a->ulValueLen, b->size); |
| 164 |
return CK_FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
prb = (0 == memcmp(a->pValue, b->data, b->size)); |
| 168 |
|
| 169 |
if (PR_TRUE == prb) { |
| 170 |
plog("pem_attrmatch %s %08x: CK_TRUE\n", pem_attr_name(a->type), a->type); |
| 171 |
return CK_TRUE; |
| 172 |
} else { |
| 173 |
plog("pem_attrmatch %s %08x: CK_FALSE\n", pem_attr_name(a->type), a->type); |
| 174 |
plog("type: %d, a->pValue %08x, b->data %08x", o->objClass, a->pValue, b->data); |
| 175 |
if (o->objClass == CKO_CERTIFICATE) { |
| 176 |
plog(", label: %s\n", o->u.cert.label.data); |
| 177 |
} |
| 178 |
plog("\n"); |
| 179 |
return CK_FALSE; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
static CK_BBOOL |
| 184 |
pem_match |
| 185 |
( |
| 186 |
CK_ATTRIBUTE_PTR pTemplate, |
| 187 |
CK_ULONG ulAttributeCount, |
| 188 |
pemInternalObject * o |
| 189 |
) |
| 190 |
{ |
| 191 |
CK_ULONG i; |
| 192 |
|
| 193 |
for (i = 0; i < ulAttributeCount; i++) { |
| 194 |
if (CK_FALSE == pem_attrmatch(&pTemplate[i], o)) { |
| 195 |
plog("pem_match: CK_FALSE\n"); |
| 196 |
return CK_FALSE; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/* Every attribute passed */ |
| 201 |
plog("pem_match: CK_TRUE\n"); |
| 202 |
return CK_TRUE; |
| 203 |
} |
| 204 |
|
| 205 |
CK_OBJECT_CLASS |
| 206 |
pem_GetObjectClass(CK_ATTRIBUTE_PTR pTemplate, |
| 207 |
CK_ULONG ulAttributeCount) |
| 208 |
{ |
| 209 |
CK_ULONG i; |
| 210 |
|
| 211 |
for (i = 0; i < ulAttributeCount; i++) { |
| 212 |
if (pTemplate[i].type == CKA_CLASS) { |
| 213 |
return *(CK_OBJECT_CLASS *) pTemplate[i].pValue; |
| 214 |
} |
| 215 |
} |
| 216 |
/* need to return a value that says 'fetch them all' */ |
| 217 |
return CK_INVALID_HANDLE; |
| 218 |
} |
| 219 |
|
| 220 |
static void * |
| 221 |
ensure_array_capacity(void *array, |
| 222 |
size_t *current_items_capacity, |
| 223 |
const size_t required_items_capacity, |
| 224 |
const size_t sizeof_one_item, |
| 225 |
const size_t resize_add_number_of_items) |
| 226 |
{ |
| 227 |
if (required_items_capacity <= *current_items_capacity) { |
| 228 |
return array; |
| 229 |
} else { |
| 230 |
void *new_array; |
| 231 |
size_t new_size_in_bytes = |
| 232 |
sizeof_one_item * ((*current_items_capacity)+resize_add_number_of_items); |
| 233 |
if (array) { |
| 234 |
PORT_Assert(*current_items_capacity > 0); |
| 235 |
new_array = NSS_ZRealloc(array, new_size_in_bytes); |
| 236 |
} else { |
| 237 |
PORT_Assert(*current_items_capacity == 0); |
| 238 |
new_array = NSS_ZAlloc(NULL, new_size_in_bytes); |
| 239 |
} |
| 240 |
(*current_items_capacity) += resize_add_number_of_items; |
| 241 |
return new_array; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
static PRUint32 |
| 246 |
collect_objects(CK_ATTRIBUTE_PTR pTemplate, |
| 247 |
CK_ULONG ulAttributeCount, |
| 248 |
pemInternalObject *** result_array, |
| 249 |
CK_RV * pError, CK_SLOT_ID slotID) |
| 250 |
{ |
| 251 |
size_t result_array_entries = 0; |
| 252 |
size_t result_array_capacity = 0; |
| 253 |
pemObjectType type = pemRaw; |
| 254 |
CK_OBJECT_CLASS objClass = pem_GetObjectClass(pTemplate, ulAttributeCount); |
| 255 |
pemInternalObject *obj = NULL; |
| 256 |
|
| 257 |
*pError = CKR_OK; |
| 258 |
|
| 259 |
plog("collect_objects slot #%ld, ", slotID); |
| 260 |
plog("%d attributes, ", ulAttributeCount); |
| 261 |
plog("%ld objects created in total.\n", pem_nobjs); |
| 262 |
plog("Looking for: "); |
| 263 |
/* |
| 264 |
* now determine type of the object |
| 265 |
*/ |
| 266 |
switch (objClass) { |
| 267 |
case CKO_CERTIFICATE: |
| 268 |
plog("CKO_CERTIFICATE\n"); |
| 269 |
type = pemCert; |
| 270 |
break; |
| 271 |
case CKO_PUBLIC_KEY: |
| 272 |
plog("CKO_PUBLIC_KEY\n"); |
| 273 |
type = pemBareKey; |
| 274 |
break; |
| 275 |
case CKO_PRIVATE_KEY: |
| 276 |
type = pemBareKey; |
| 277 |
plog("CKO_PRIVATE_KEY\n"); |
| 278 |
break; |
| 279 |
case CKO_NSS_TRUST: |
| 280 |
type = pemTrust; |
| 281 |
plog("CKO_NSS_TRUST\n"); |
| 282 |
break; |
| 283 |
case CKO_NSS_CRL: |
| 284 |
plog("CKO_NSS_CRL\n"); |
| 285 |
goto done; |
| 286 |
case CKO_NSS_SMIME: |
| 287 |
plog("CKO_NSS_SMIME\n"); |
| 288 |
goto done; |
| 289 |
case CKO_NSS_BUILTIN_ROOT_LIST: |
| 290 |
plog("CKO_NSS_BUILTIN_ROOT_LIST\n"); |
| 291 |
goto done; |
| 292 |
case CK_INVALID_HANDLE: |
| 293 |
type = pemAll; /* look through all objectclasses - ignore the type field */ |
| 294 |
plog("CK_INVALID_HANDLE\n"); |
| 295 |
break; |
| 296 |
default: |
| 297 |
plog("no other object types %08x\n", objClass); |
| 298 |
goto done; /* no other object types we understand in this module */ |
| 299 |
} |
| 300 |
|
| 301 |
/* find objects */ |
| 302 |
list_for_each_entry(obj, &pem_objs, gl_list) { |
| 303 |
int match = 1; /* matches type if type not specified */ |
| 304 |
plog(" %ld type = %d\n", obj->arrayIdx, obj->type); |
| 305 |
if (type != pemAll) { |
| 306 |
/* type specified - must match given type */ |
| 307 |
match = (type == obj->type); |
| 308 |
} |
| 309 |
if (match) { |
| 310 |
match = (slotID == obj->slotID) && |
| 311 |
(CK_TRUE == pem_match(pTemplate, ulAttributeCount, obj)); |
| 312 |
} |
| 313 |
if (match) { |
| 314 |
pemInternalObject **new_result_array = (pemInternalObject **) |
| 315 |
ensure_array_capacity(*result_array, |
| 316 |
&result_array_capacity, |
| 317 |
result_array_entries+1, |
| 318 |
sizeof(pemInternalObject *), |
| 319 |
512 /*add number of items per resize*/); |
| 320 |
if (!new_result_array) { |
| 321 |
*pError = CKR_HOST_MEMORY; |
| 322 |
goto loser; |
| 323 |
} |
| 324 |
if (*result_array != new_result_array) { |
| 325 |
*result_array = new_result_array; |
| 326 |
} |
| 327 |
(*result_array)[ result_array_entries ] = obj; |
| 328 |
result_array_entries++; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
if (CKR_OK != *pError) { |
| 333 |
goto loser; |
| 334 |
} |
| 335 |
|
| 336 |
done: |
| 337 |
plog("collect_objects: Found %d\n", result_array_entries); |
| 338 |
return result_array_entries; |
| 339 |
loser: |
| 340 |
NSS_ZFreeIf(*result_array); |
| 341 |
return 0; |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
NSS_IMPLEMENT NSSCKMDFindObjects * |
| 346 |
pem_FindObjectsInit |
| 347 |
( |
| 348 |
NSSCKFWSession * fwSession, |
| 349 |
CK_ATTRIBUTE_PTR pTemplate, |
| 350 |
CK_ULONG ulAttributeCount, |
| 351 |
CK_RV * pError |
| 352 |
) |
| 353 |
{ |
| 354 |
NSSArena *arena = NULL; |
| 355 |
NSSCKMDFindObjects *rv = (NSSCKMDFindObjects *) NULL; |
| 356 |
struct pemFOStr *fo = (struct pemFOStr *) NULL; |
| 357 |
pemInternalObject **temp = (pemInternalObject **) NULL; |
| 358 |
NSSCKFWSlot *fwSlot; |
| 359 |
CK_SLOT_ID slotID; |
| 360 |
|
| 361 |
plog("pem_FindObjectsInit\n"); |
| 362 |
fwSlot = NSSCKFWSession_GetFWSlot(fwSession); |
| 363 |
if ((NSSCKFWSlot *) NULL == fwSlot) { |
| 364 |
goto loser; |
| 365 |
} |
| 366 |
slotID = NSSCKFWSlot_GetSlotID(fwSlot); |
| 367 |
|
| 368 |
arena = NSSArena_Create(); |
| 369 |
if ((NSSArena *) NULL == arena) { |
| 370 |
goto loser; |
| 371 |
} |
| 372 |
|
| 373 |
rv = NSS_ZNEW(arena, NSSCKMDFindObjects); |
| 374 |
if ((NSSCKMDFindObjects *) NULL == rv) { |
| 375 |
*pError = CKR_HOST_MEMORY; |
| 376 |
goto loser; |
| 377 |
} |
| 378 |
|
| 379 |
fo = NSS_ZNEW(arena, struct pemFOStr); |
| 380 |
if ((struct pemFOStr *) NULL == fo) { |
| 381 |
*pError = CKR_HOST_MEMORY; |
| 382 |
goto loser; |
| 383 |
} |
| 384 |
|
| 385 |
fo->arena = arena; |
| 386 |
/* fo->n and fo->i are already zero */ |
| 387 |
|
| 388 |
rv->etc = (void *) fo; |
| 389 |
rv->Final = pem_mdFindObjects_Final; |
| 390 |
rv->Next = pem_mdFindObjects_Next; |
| 391 |
rv->null = (void *) NULL; |
| 392 |
|
| 393 |
fo->n = |
| 394 |
collect_objects(pTemplate, ulAttributeCount, &temp, pError, |
| 395 |
slotID); |
| 396 |
if (*pError != CKR_OK) { |
| 397 |
goto loser; |
| 398 |
} |
| 399 |
|
| 400 |
fo->objs = NSS_ZNEWARRAY(arena, pemInternalObject *, fo->n); |
| 401 |
if ((pemInternalObject **) NULL == fo->objs) { |
| 402 |
*pError = CKR_HOST_MEMORY; |
| 403 |
goto loser; |
| 404 |
} |
| 405 |
|
| 406 |
memcpy(fo->objs, temp, sizeof(pemInternalObject *) * fo->n); |
| 407 |
|
| 408 |
NSS_ZFreeIf(temp); |
| 409 |
temp = (pemInternalObject **) NULL; |
| 410 |
|
| 411 |
return rv; |
| 412 |
|
| 413 |
loser: |
| 414 |
NSS_ZFreeIf(temp); |
| 415 |
NSS_ZFreeIf(fo); |
| 416 |
NSS_ZFreeIf(rv); |
| 417 |
if ((NSSArena *) NULL != arena) { |
| 418 |
NSSArena_Destroy(arena); |
| 419 |
} |
| 420 |
return (NSSCKMDFindObjects *) NULL; |
| 421 |
} |