| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
OSToken.cpp |
| 29 |
|
| 30 |
The token class; a token is stored in a directory containing several files. |
| 31 |
Each object is stored in a separate file and a token object is present that |
| 32 |
has the token specific attributes |
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
#include "config.h" |
| 36 |
#include "log.h" |
| 37 |
#include "OSAttributes.h" |
| 38 |
#include "OSAttribute.h" |
| 39 |
#include "ObjectFile.h" |
| 40 |
#include "Directory.h" |
| 41 |
#include "Generation.h" |
| 42 |
#include "UUID.h" |
| 43 |
#include "cryptoki.h" |
| 44 |
#include "OSToken.h" |
| 45 |
#include "OSPathSep.h" |
| 46 |
#include <vector> |
| 47 |
#include <string> |
| 48 |
#include <set> |
| 49 |
#include <map> |
| 50 |
#include <list> |
| 51 |
#include <stdio.h> |
| 52 |
|
| 53 |
// Constructor |
| 54 |
OSToken::OSToken(const std::string inTokenPath) |
| 55 |
{ |
| 56 |
tokenPath = inTokenPath; |
| 57 |
|
| 58 |
tokenDir = new Directory(tokenPath); |
| 59 |
gen = Generation::create(tokenPath + OS_PATHSEP + "generation", true); |
| 60 |
tokenObject = new ObjectFile(this, tokenPath + OS_PATHSEP + "token.object", tokenPath + OS_PATHSEP + "token.lock"); |
| 61 |
tokenMutex = MutexFactory::i()->getMutex(); |
| 62 |
valid = (gen != NULL) && (tokenMutex != NULL) && tokenDir->isValid() && tokenObject->valid; |
| 63 |
|
| 64 |
DEBUG_MSG("Opened token %s", tokenPath.c_str()); |
| 65 |
|
| 66 |
index(true); |
| 67 |
} |
| 68 |
|
| 69 |
// Create a new token |
| 70 |
/*static*/ OSToken* OSToken::createToken(const std::string basePath, const std::string tokenDir, const ByteString& label, const ByteString& serial) |
| 71 |
{ |
| 72 |
Directory baseDir(basePath); |
| 73 |
|
| 74 |
if (!baseDir.isValid()) |
| 75 |
{ |
| 76 |
ERROR_MSG("Could not create the Directory object"); |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
|
| 80 |
// Create the token directory |
| 81 |
if (!baseDir.mkdir(tokenDir)) |
| 82 |
{ |
| 83 |
// Error msg is generated by mkdir |
| 84 |
return NULL; |
| 85 |
} |
| 86 |
|
| 87 |
// Create the token object |
| 88 |
ObjectFile tokenObject(NULL, basePath + OS_PATHSEP + tokenDir + OS_PATHSEP + "token.object", basePath + OS_PATHSEP + tokenDir + OS_PATHSEP + "token.lock", true); |
| 89 |
|
| 90 |
if (!tokenObject.valid) |
| 91 |
{ |
| 92 |
std::string tokenPath = basePath + OS_PATHSEP + tokenDir + OS_PATHSEP + "token.[object|lock]"; |
| 93 |
ERROR_MSG("Failed to create the token object: %s", tokenPath.c_str()); |
| 94 |
|
| 95 |
baseDir.rmdir(tokenDir); |
| 96 |
|
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
// Set the initial attributes |
| 101 |
CK_ULONG flags = |
| 102 |
CKF_RNG | |
| 103 |
CKF_LOGIN_REQUIRED | // FIXME: check |
| 104 |
CKF_RESTORE_KEY_NOT_NEEDED | |
| 105 |
CKF_TOKEN_INITIALIZED | |
| 106 |
CKF_SO_PIN_LOCKED | |
| 107 |
CKF_SO_PIN_TO_BE_CHANGED; |
| 108 |
|
| 109 |
OSAttribute tokenLabel(label); |
| 110 |
OSAttribute tokenSerial(serial); |
| 111 |
OSAttribute tokenFlags(flags); |
| 112 |
|
| 113 |
if (!tokenObject.setAttribute(CKA_OS_TOKENLABEL, tokenLabel) || |
| 114 |
!tokenObject.setAttribute(CKA_OS_TOKENSERIAL, tokenSerial) || |
| 115 |
!tokenObject.setAttribute(CKA_OS_TOKENFLAGS, tokenFlags)) |
| 116 |
{ |
| 117 |
ERROR_MSG("Failed to set the token attributes"); |
| 118 |
|
| 119 |
baseDir.remove(tokenDir + OS_PATHSEP + "token.object"); |
| 120 |
baseDir.remove(tokenDir + OS_PATHSEP + "token.lock"); |
| 121 |
baseDir.rmdir(tokenDir); |
| 122 |
|
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
DEBUG_MSG("Created new token %s", tokenDir.c_str()); |
| 127 |
|
| 128 |
return new OSToken(basePath + OS_PATHSEP + tokenDir); |
| 129 |
} |
| 130 |
|
| 131 |
// Access an existing token |
| 132 |
/*static*/ OSToken *OSToken::accessToken(const std::string &basePath, const std::string &tokenDir) |
| 133 |
{ |
| 134 |
return new OSToken(basePath + OS_PATHSEP + tokenDir); |
| 135 |
} |
| 136 |
|
| 137 |
// Destructor |
| 138 |
OSToken::~OSToken() |
| 139 |
{ |
| 140 |
// Clean up |
| 141 |
std::set<OSObject*> cleanUp = allObjects; |
| 142 |
allObjects.clear(); |
| 143 |
|
| 144 |
for (std::set<OSObject*>::iterator i = cleanUp.begin(); i != cleanUp.end(); i++) |
| 145 |
{ |
| 146 |
delete *i; |
| 147 |
} |
| 148 |
|
| 149 |
delete tokenDir; |
| 150 |
if (gen != NULL) delete gen; |
| 151 |
MutexFactory::i()->recycleMutex(tokenMutex); |
| 152 |
delete tokenObject; |
| 153 |
} |
| 154 |
|
| 155 |
// Set the SO PIN |
| 156 |
bool OSToken::setSOPIN(const ByteString& soPINBlob) |
| 157 |
{ |
| 158 |
if (!valid) return false; |
| 159 |
|
| 160 |
OSAttribute soPIN(soPINBlob); |
| 161 |
|
| 162 |
CK_ULONG flags; |
| 163 |
|
| 164 |
if (tokenObject->setAttribute(CKA_OS_SOPIN, soPIN) && |
| 165 |
getTokenFlags(flags)) |
| 166 |
{ |
| 167 |
flags &= ~CKF_SO_PIN_COUNT_LOW; |
| 168 |
flags &= ~CKF_SO_PIN_FINAL_TRY; |
| 169 |
flags &= ~CKF_SO_PIN_LOCKED; |
| 170 |
flags &= ~CKF_SO_PIN_TO_BE_CHANGED; |
| 171 |
|
| 172 |
return setTokenFlags(flags); |
| 173 |
} |
| 174 |
|
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
// Get the SO PIN |
| 179 |
bool OSToken::getSOPIN(ByteString& soPINBlob) |
| 180 |
{ |
| 181 |
if (!valid || !tokenObject->isValid()) |
| 182 |
{ |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
if (tokenObject->attributeExists(CKA_OS_SOPIN)) |
| 187 |
{ |
| 188 |
soPINBlob = tokenObject->getAttribute(CKA_OS_SOPIN).getByteStringValue(); |
| 189 |
|
| 190 |
return true; |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
return false; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
// Set the user PIN |
| 199 |
bool OSToken::setUserPIN(ByteString userPINBlob) |
| 200 |
{ |
| 201 |
if (!valid) return false; |
| 202 |
|
| 203 |
OSAttribute userPIN(userPINBlob); |
| 204 |
|
| 205 |
CK_ULONG flags; |
| 206 |
|
| 207 |
if (tokenObject->setAttribute(CKA_OS_USERPIN, userPIN) && |
| 208 |
getTokenFlags(flags)) |
| 209 |
{ |
| 210 |
flags |= CKF_USER_PIN_INITIALIZED; |
| 211 |
flags &= ~CKF_USER_PIN_COUNT_LOW; |
| 212 |
flags &= ~CKF_USER_PIN_FINAL_TRY; |
| 213 |
flags &= ~CKF_USER_PIN_LOCKED; |
| 214 |
flags &= ~CKF_USER_PIN_TO_BE_CHANGED; |
| 215 |
|
| 216 |
return setTokenFlags(flags); |
| 217 |
} |
| 218 |
|
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
// Get the user PIN |
| 223 |
bool OSToken::getUserPIN(ByteString& userPINBlob) |
| 224 |
{ |
| 225 |
if (!valid || !tokenObject->isValid()) |
| 226 |
{ |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
if (tokenObject->attributeExists(CKA_OS_USERPIN)) |
| 231 |
{ |
| 232 |
userPINBlob = tokenObject->getAttribute(CKA_OS_USERPIN).getByteStringValue(); |
| 233 |
|
| 234 |
return true; |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
return false; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
// Retrieve the token label |
| 243 |
bool OSToken::getTokenLabel(ByteString& label) |
| 244 |
{ |
| 245 |
if (!valid || !tokenObject->isValid()) |
| 246 |
{ |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
if (tokenObject->attributeExists(CKA_OS_TOKENLABEL)) |
| 251 |
{ |
| 252 |
label = tokenObject->getAttribute(CKA_OS_TOKENLABEL).getByteStringValue(); |
| 253 |
|
| 254 |
return true; |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
return false; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// Retrieve the token serial |
| 263 |
bool OSToken::getTokenSerial(ByteString& serial) |
| 264 |
{ |
| 265 |
if (!valid || !tokenObject->isValid()) |
| 266 |
{ |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
if (tokenObject->attributeExists(CKA_OS_TOKENSERIAL)) |
| 271 |
{ |
| 272 |
serial = tokenObject->getAttribute(CKA_OS_TOKENSERIAL).getByteStringValue(); |
| 273 |
|
| 274 |
return true; |
| 275 |
} |
| 276 |
else |
| 277 |
{ |
| 278 |
return false; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
// Get the token flags |
| 283 |
bool OSToken::getTokenFlags(CK_ULONG& flags) |
| 284 |
{ |
| 285 |
if (!valid || !tokenObject->isValid()) |
| 286 |
{ |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
if (tokenObject->attributeExists(CKA_OS_TOKENFLAGS)) |
| 291 |
{ |
| 292 |
flags = tokenObject->getAttribute(CKA_OS_TOKENFLAGS).getUnsignedLongValue(); |
| 293 |
|
| 294 |
// Check if the user PIN is initialised |
| 295 |
if (tokenObject->attributeExists(CKA_OS_USERPIN)) |
| 296 |
{ |
| 297 |
flags |= CKF_USER_PIN_INITIALIZED; |
| 298 |
} |
| 299 |
|
| 300 |
return true; |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
return false; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// Set the token flags |
| 309 |
bool OSToken::setTokenFlags(const CK_ULONG flags) |
| 310 |
{ |
| 311 |
if (!valid) return false; |
| 312 |
|
| 313 |
OSAttribute tokenFlags(flags); |
| 314 |
|
| 315 |
return tokenObject->setAttribute(CKA_OS_TOKENFLAGS, tokenFlags); |
| 316 |
} |
| 317 |
|
| 318 |
// Retrieve objects |
| 319 |
std::set<OSObject*> OSToken::getObjects() |
| 320 |
{ |
| 321 |
index(); |
| 322 |
|
| 323 |
// Make sure that no other thread is in the process of changing |
| 324 |
// the object list when we return it |
| 325 |
MutexLocker lock(tokenMutex); |
| 326 |
|
| 327 |
return objects; |
| 328 |
} |
| 329 |
|
| 330 |
void OSToken::getObjects(std::set<OSObject*> &inObjects) |
| 331 |
{ |
| 332 |
index(); |
| 333 |
|
| 334 |
// Make sure that no other thread is in the process of changing |
| 335 |
// the object list when we return it |
| 336 |
MutexLocker lock(tokenMutex); |
| 337 |
|
| 338 |
inObjects.insert(objects.begin(),objects.end()); |
| 339 |
} |
| 340 |
|
| 341 |
// Create a new object |
| 342 |
OSObject* OSToken::createObject() |
| 343 |
{ |
| 344 |
if (!valid) return NULL; |
| 345 |
|
| 346 |
// Generate a name for the object |
| 347 |
std::string objectUUID = UUID::newUUID(); |
| 348 |
std::string objectPath = tokenPath + OS_PATHSEP + objectUUID + ".object"; |
| 349 |
std::string lockPath = tokenPath + OS_PATHSEP + objectUUID + ".lock"; |
| 350 |
|
| 351 |
// Create the new object file |
| 352 |
ObjectFile* newObject = new ObjectFile(this, objectPath, lockPath, true); |
| 353 |
|
| 354 |
if (!newObject->valid) |
| 355 |
{ |
| 356 |
ERROR_MSG("Failed to create new object %s", objectPath.c_str()); |
| 357 |
|
| 358 |
delete newObject; |
| 359 |
|
| 360 |
return NULL; |
| 361 |
} |
| 362 |
|
| 363 |
// Now add it to the set of objects |
| 364 |
MutexLocker lock(tokenMutex); |
| 365 |
|
| 366 |
objects.insert(newObject); |
| 367 |
allObjects.insert(newObject); |
| 368 |
currentFiles.insert(newObject->getFilename()); |
| 369 |
|
| 370 |
DEBUG_MSG("(0x%08X) Created new object %s (0x%08X)", this, objectPath.c_str(), newObject); |
| 371 |
|
| 372 |
gen->update(); |
| 373 |
|
| 374 |
gen->commit(); |
| 375 |
|
| 376 |
return newObject; |
| 377 |
} |
| 378 |
|
| 379 |
// Delete an object |
| 380 |
bool OSToken::deleteObject(OSObject* object) |
| 381 |
{ |
| 382 |
if (!valid) return false; |
| 383 |
|
| 384 |
if (objects.find(object) == objects.end()) |
| 385 |
{ |
| 386 |
ERROR_MSG("Cannot delete non-existent object 0x%08X", object); |
| 387 |
|
| 388 |
return false; |
| 389 |
} |
| 390 |
|
| 391 |
MutexLocker lock(tokenMutex); |
| 392 |
|
| 393 |
ObjectFile* fileObject = dynamic_cast<ObjectFile*>(object); |
| 394 |
if (fileObject == NULL) |
| 395 |
{ |
| 396 |
ERROR_MSG("Object type not compatible with this token class 0x%08X", object); |
| 397 |
|
| 398 |
return false; |
| 399 |
} |
| 400 |
|
| 401 |
// Invalidate the object instance |
| 402 |
fileObject->invalidate(); |
| 403 |
|
| 404 |
// Retrieve the filename of the object |
| 405 |
std::string objectFilename = fileObject->getFilename(); |
| 406 |
|
| 407 |
// Attempt to delete the file |
| 408 |
if (!tokenDir->remove(objectFilename)) |
| 409 |
{ |
| 410 |
ERROR_MSG("Failed to delete object file %s", objectFilename.c_str()); |
| 411 |
|
| 412 |
return false; |
| 413 |
} |
| 414 |
|
| 415 |
// Retrieve the filename of the lock |
| 416 |
std::string lockFilename = fileObject->getLockname(); |
| 417 |
|
| 418 |
// Attempt to delete the lock |
| 419 |
if (!tokenDir->remove(lockFilename)) |
| 420 |
{ |
| 421 |
ERROR_MSG("Failed to delete lock file %s", lockFilename.c_str()); |
| 422 |
|
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
objects.erase(object); |
| 427 |
|
| 428 |
DEBUG_MSG("Deleted object %s", objectFilename.c_str()); |
| 429 |
|
| 430 |
gen->update(); |
| 431 |
|
| 432 |
gen->commit(); |
| 433 |
|
| 434 |
return true; |
| 435 |
} |
| 436 |
|
| 437 |
// Checks if the token is consistent |
| 438 |
bool OSToken::isValid() |
| 439 |
{ |
| 440 |
return valid; |
| 441 |
} |
| 442 |
|
| 443 |
// Invalidate the token (for instance if it is deleted) |
| 444 |
void OSToken::invalidate() |
| 445 |
{ |
| 446 |
valid = false; |
| 447 |
} |
| 448 |
|
| 449 |
// Delete the token |
| 450 |
bool OSToken::clearToken() |
| 451 |
{ |
| 452 |
MutexLocker lock(tokenMutex); |
| 453 |
|
| 454 |
// Invalidate the token |
| 455 |
invalidate(); |
| 456 |
|
| 457 |
// First, clear out all objects |
| 458 |
objects.clear(); |
| 459 |
|
| 460 |
// Now, delete all files in the token directory |
| 461 |
if (!tokenDir->refresh()) |
| 462 |
{ |
| 463 |
return false; |
| 464 |
} |
| 465 |
|
| 466 |
std::vector<std::string> tokenFiles = tokenDir->getFiles(); |
| 467 |
|
| 468 |
for (std::vector<std::string>::iterator i = tokenFiles.begin(); i != tokenFiles.end(); i++) |
| 469 |
{ |
| 470 |
if (!tokenDir->remove(*i)) |
| 471 |
{ |
| 472 |
ERROR_MSG("Failed to remove %s from token directory %s", i->c_str(), tokenPath.c_str()); |
| 473 |
|
| 474 |
return false; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
// Now remove the token directory |
| 479 |
if (!tokenDir->rmdir("")) |
| 480 |
{ |
| 481 |
ERROR_MSG("Failed to remove the token directory %s", tokenPath.c_str()); |
| 482 |
|
| 483 |
return false; |
| 484 |
} |
| 485 |
|
| 486 |
DEBUG_MSG("Token instance %s was succesfully cleared", tokenPath.c_str()); |
| 487 |
|
| 488 |
return true; |
| 489 |
} |
| 490 |
|
| 491 |
// Reset the token |
| 492 |
bool OSToken::resetToken(const ByteString& label) |
| 493 |
{ |
| 494 |
CK_ULONG flags; |
| 495 |
|
| 496 |
if (!getTokenFlags(flags)) |
| 497 |
{ |
| 498 |
ERROR_MSG("Failed to get the token attributes"); |
| 499 |
|
| 500 |
return false; |
| 501 |
} |
| 502 |
|
| 503 |
// Clean up |
| 504 |
std::set<OSObject*> cleanUp = getObjects(); |
| 505 |
|
| 506 |
MutexLocker lock(tokenMutex); |
| 507 |
|
| 508 |
for (std::set<OSObject*>::iterator i = cleanUp.begin(); i != cleanUp.end(); i++) |
| 509 |
{ |
| 510 |
ObjectFile* fileObject = dynamic_cast<ObjectFile*>(*i); |
| 511 |
if (fileObject == NULL) |
| 512 |
{ |
| 513 |
ERROR_MSG("Object type not compatible with this token class 0x%08X", *i); |
| 514 |
|
| 515 |
return false; |
| 516 |
} |
| 517 |
|
| 518 |
// Invalidate the object instance |
| 519 |
fileObject->invalidate(); |
| 520 |
|
| 521 |
// Retrieve the filename of the object |
| 522 |
std::string objectFilename = fileObject->getFilename(); |
| 523 |
|
| 524 |
// Attempt to delete the file |
| 525 |
if (!tokenDir->remove(objectFilename)) |
| 526 |
{ |
| 527 |
ERROR_MSG("Failed to delete object file %s", objectFilename.c_str()); |
| 528 |
|
| 529 |
return false; |
| 530 |
} |
| 531 |
|
| 532 |
// Retrieve the filename of the lock |
| 533 |
std::string lockFilename = fileObject->getLockname(); |
| 534 |
|
| 535 |
// Attempt to delete the lock |
| 536 |
if (!tokenDir->remove(lockFilename)) |
| 537 |
{ |
| 538 |
ERROR_MSG("Failed to delete lock file %s", lockFilename.c_str()); |
| 539 |
|
| 540 |
return false; |
| 541 |
} |
| 542 |
|
| 543 |
objects.erase(*i); |
| 544 |
|
| 545 |
DEBUG_MSG("Deleted object %s", objectFilename.c_str()); |
| 546 |
} |
| 547 |
|
| 548 |
// The user PIN has been removed |
| 549 |
flags &= ~CKF_USER_PIN_INITIALIZED; |
| 550 |
flags &= ~CKF_USER_PIN_COUNT_LOW; |
| 551 |
flags &= ~CKF_USER_PIN_FINAL_TRY; |
| 552 |
flags &= ~CKF_USER_PIN_LOCKED; |
| 553 |
flags &= ~CKF_USER_PIN_TO_BE_CHANGED; |
| 554 |
|
| 555 |
// Set new token attributes |
| 556 |
OSAttribute tokenLabel(label); |
| 557 |
OSAttribute tokenFlags(flags); |
| 558 |
|
| 559 |
if (!tokenObject->setAttribute(CKA_OS_TOKENLABEL, tokenLabel) || |
| 560 |
!tokenObject->setAttribute(CKA_OS_TOKENFLAGS, tokenFlags)) |
| 561 |
{ |
| 562 |
ERROR_MSG("Failed to set the token attributes"); |
| 563 |
|
| 564 |
return false; |
| 565 |
} |
| 566 |
|
| 567 |
if (tokenObject->attributeExists(CKA_OS_USERPIN) && |
| 568 |
!tokenObject->deleteAttribute(CKA_OS_USERPIN)) |
| 569 |
{ |
| 570 |
ERROR_MSG("Failed to remove USERPIN"); |
| 571 |
|
| 572 |
return false; |
| 573 |
} |
| 574 |
|
| 575 |
DEBUG_MSG("Token instance %s was succesfully reset", tokenPath.c_str()); |
| 576 |
|
| 577 |
gen->update(); |
| 578 |
gen->commit(); |
| 579 |
|
| 580 |
return true; |
| 581 |
} |
| 582 |
|
| 583 |
// Index the token |
| 584 |
bool OSToken::index(bool isFirstTime /* = false */) |
| 585 |
{ |
| 586 |
// No access to object mutable fields before |
| 587 |
MutexLocker lock(tokenMutex); |
| 588 |
|
| 589 |
// Check if re-indexing is required |
| 590 |
if (!isFirstTime && (!valid || !gen->wasUpdated())) |
| 591 |
{ |
| 592 |
DEBUG_MSG("No re-indexing is required"); |
| 593 |
|
| 594 |
return true; |
| 595 |
} |
| 596 |
|
| 597 |
// Check the integrity |
| 598 |
if (!tokenDir->refresh() || !tokenObject->valid) |
| 599 |
{ |
| 600 |
ERROR_MSG("Token integrity check failed"); |
| 601 |
|
| 602 |
valid = false; |
| 603 |
|
| 604 |
return false; |
| 605 |
} |
| 606 |
|
| 607 |
DEBUG_MSG("Token %s has changed", tokenPath.c_str()); |
| 608 |
|
| 609 |
// Retrieve the directory listing |
| 610 |
std::vector<std::string> tokenFiles = tokenDir->getFiles(); |
| 611 |
|
| 612 |
// Filter out the objects |
| 613 |
std::set<std::string> newSet; |
| 614 |
|
| 615 |
for (std::vector<std::string>::iterator i = tokenFiles.begin(); i != tokenFiles.end(); i++) |
| 616 |
{ |
| 617 |
if ((i->size() > 7) && |
| 618 |
(!(i->substr(i->size() - 7).compare(".object"))) && |
| 619 |
(i->compare("token.object"))) |
| 620 |
{ |
| 621 |
newSet.insert(*i); |
| 622 |
} |
| 623 |
else |
| 624 |
{ |
| 625 |
DEBUG_MSG("Ignored file %s", i->c_str()); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
// Compute the changes compared to the last list of files |
| 630 |
std::set<std::string> addedFiles; |
| 631 |
std::set<std::string> removedFiles; |
| 632 |
|
| 633 |
if (!isFirstTime) |
| 634 |
{ |
| 635 |
// First compute which files were added |
| 636 |
for (std::set<std::string>::iterator i = newSet.begin(); i != newSet.end(); i++) |
| 637 |
{ |
| 638 |
if (currentFiles.find(*i) == currentFiles.end()) |
| 639 |
{ |
| 640 |
addedFiles.insert(*i); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
// Now compute which files were removed |
| 645 |
for (std::set<std::string>::iterator i = currentFiles.begin(); i != currentFiles.end(); i++) |
| 646 |
{ |
| 647 |
if (newSet.find(*i) == newSet.end()) |
| 648 |
{ |
| 649 |
removedFiles.insert(*i); |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
else |
| 654 |
{ |
| 655 |
addedFiles = newSet; |
| 656 |
} |
| 657 |
|
| 658 |
currentFiles = newSet; |
| 659 |
|
| 660 |
DEBUG_MSG("%d objects were added and %d objects were removed", addedFiles.size(), removedFiles.size()); |
| 661 |
DEBUG_MSG("Current directory set contains %d objects", currentFiles.size()); |
| 662 |
|
| 663 |
// Now update the set of objects |
| 664 |
|
| 665 |
// Add new objects |
| 666 |
for (std::set<std::string>::iterator i = addedFiles.begin(); i != addedFiles.end(); i++) |
| 667 |
{ |
| 668 |
if ((i->find_last_of('.') == std::string::npos) || |
| 669 |
(i->substr(i->find_last_of('.')) != ".object")) |
| 670 |
{ |
| 671 |
continue; |
| 672 |
} |
| 673 |
|
| 674 |
std::string lockName(*i); |
| 675 |
lockName.replace(lockName.find_last_of('.'), std::string::npos, ".lock"); |
| 676 |
|
| 677 |
// Create a new token object for the added file |
| 678 |
ObjectFile* newObject = new ObjectFile(this, tokenPath + OS_PATHSEP + *i, tokenPath + OS_PATHSEP + lockName); |
| 679 |
|
| 680 |
// Add the object, even invalid ones. |
| 681 |
// This is so the we can read the attributes once |
| 682 |
// the other process has finished writing to disc. |
| 683 |
DEBUG_MSG("(0x%08X) New object %s (0x%08X) added", this, newObject->getFilename().c_str(), newObject); |
| 684 |
objects.insert(newObject); |
| 685 |
allObjects.insert(newObject); |
| 686 |
} |
| 687 |
|
| 688 |
// Remove deleted objects |
| 689 |
std::set<OSObject*> newObjects; |
| 690 |
|
| 691 |
for (std::set<OSObject*>::iterator i = objects.begin(); i != objects.end(); i++) |
| 692 |
{ |
| 693 |
ObjectFile* fileObject = dynamic_cast<ObjectFile*>((*i)); |
| 694 |
if (fileObject == NULL) |
| 695 |
{ |
| 696 |
ERROR_MSG("Object type not compatible with this token class 0x%08X", (*i)); |
| 697 |
|
| 698 |
return false; |
| 699 |
} |
| 700 |
|
| 701 |
DEBUG_MSG("Processing %s (0x%08X)", fileObject->getFilename().c_str(), *i); |
| 702 |
|
| 703 |
if (removedFiles.find(fileObject->getFilename()) == removedFiles.end()) |
| 704 |
{ |
| 705 |
DEBUG_MSG("Adding object %s", fileObject->getFilename().c_str()); |
| 706 |
// This object gets to stay in the set |
| 707 |
newObjects.insert(*i); |
| 708 |
} |
| 709 |
else |
| 710 |
{ |
| 711 |
fileObject->invalidate(); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
// Set the new objects |
| 716 |
objects = newObjects; |
| 717 |
|
| 718 |
DEBUG_MSG("The token now contains %d objects", objects.size()); |
| 719 |
|
| 720 |
return true; |
| 721 |
} |
| 722 |
|