| 1 |
/* |
| 2 |
* Copyright (c) 2013 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 |
DBToken.cpp |
| 29 |
|
| 30 |
The token class; a token is stored in a directory containing a single |
| 31 |
database file. |
| 32 |
Each object is stored in multiple tables with every attribute base type |
| 33 |
stored in a different table. |
| 34 |
*****************************************************************************/ |
| 35 |
|
| 36 |
#include "config.h" |
| 37 |
#include "log.h" |
| 38 |
#include "OSAttributes.h" |
| 39 |
#include "OSAttribute.h" |
| 40 |
#include "OSPathSep.h" |
| 41 |
|
| 42 |
#include "cryptoki.h" |
| 43 |
#include "DBToken.h" |
| 44 |
#include "DBObject.h" |
| 45 |
#include "DB.h" |
| 46 |
|
| 47 |
#include "Directory.h" |
| 48 |
|
| 49 |
#include <vector> |
| 50 |
#include <string> |
| 51 |
#include <set> |
| 52 |
#include <map> |
| 53 |
#include <list> |
| 54 |
#include <cstdio> |
| 55 |
#include <sys/stat.h> |
| 56 |
#include <errno.h> |
| 57 |
|
| 58 |
const char * const DBTOKEN_FILE = "sqlite3.db"; |
| 59 |
const long long DBTOKEN_OBJECT_TOKENINFO = 1; |
| 60 |
|
| 61 |
// Constructor for creating a new token. |
| 62 |
DBToken::DBToken(const std::string &baseDir, const std::string &tokenName, const ByteString &label, const ByteString &serial) |
| 63 |
: _connection(NULL), _tokenMutex(NULL) |
| 64 |
{ |
| 65 |
std::string tokenDir = baseDir + OS_PATHSEP + tokenName; |
| 66 |
std::string tokenPath = tokenDir + OS_PATHSEP + DBTOKEN_FILE; |
| 67 |
|
| 68 |
// Refuse to open an already existing database. |
| 69 |
FILE *f = fopen(tokenPath.c_str(),"r"); |
| 70 |
if (f) |
| 71 |
{ |
| 72 |
fclose(f); |
| 73 |
ERROR_MSG("Refusing to overwrite and existing database at \"%s\"", tokenPath.c_str()); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
// First create the directory for the token, we expect basePath to already exist |
| 78 |
if (mkdir(tokenDir.c_str(), S_IFDIR | S_IRWXU)) |
| 79 |
{ |
| 80 |
// Allow the directory to exists already. |
| 81 |
if (errno != EEXIST) |
| 82 |
{ |
| 83 |
ERROR_MSG("Unable to create directory \"%s\"", tokenDir.c_str()); |
| 84 |
return; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// Create |
| 89 |
_connection = DB::Connection::Create(tokenDir, DBTOKEN_FILE); |
| 90 |
if (_connection == NULL) |
| 91 |
{ |
| 92 |
ERROR_MSG("Failed to create a database connection for \"%s\"", tokenPath.c_str()); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if (!_connection->connect()) |
| 97 |
{ |
| 98 |
delete _connection; |
| 99 |
_connection = NULL; |
| 100 |
|
| 101 |
ERROR_MSG("Failed to connect to the database at \"%s\"", tokenPath.c_str()); |
| 102 |
|
| 103 |
// Now remove the token directory |
| 104 |
if (remove(tokenDir.c_str())) |
| 105 |
{ |
| 106 |
ERROR_MSG("Failed to remove the token directory \"%s\"", tokenDir.c_str()); |
| 107 |
} |
| 108 |
|
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
// Create a DBObject for the established connection to the database. |
| 113 |
DBObject tokenObject(_connection); |
| 114 |
|
| 115 |
// First create the tables that support storage of object attributes and then insert the object containing |
| 116 |
// the token info into the database. |
| 117 |
if (!tokenObject.createTables() || !tokenObject.insert() || tokenObject.objectId()!=DBTOKEN_OBJECT_TOKENINFO) |
| 118 |
{ |
| 119 |
tokenObject.dropConnection(); |
| 120 |
|
| 121 |
_connection->close(); |
| 122 |
delete _connection; |
| 123 |
_connection = NULL; |
| 124 |
|
| 125 |
ERROR_MSG("Failed to create tables for storing objects in database at \"%s\"", tokenPath.c_str()); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// Set the initial attributes |
| 130 |
CK_ULONG flags = |
| 131 |
CKF_RNG | |
| 132 |
CKF_LOGIN_REQUIRED | // FIXME: check |
| 133 |
CKF_RESTORE_KEY_NOT_NEEDED | |
| 134 |
CKF_TOKEN_INITIALIZED | |
| 135 |
CKF_SO_PIN_LOCKED | |
| 136 |
CKF_SO_PIN_TO_BE_CHANGED; |
| 137 |
|
| 138 |
OSAttribute tokenLabel(label); |
| 139 |
OSAttribute tokenSerial(serial); |
| 140 |
OSAttribute tokenFlags(flags); |
| 141 |
|
| 142 |
if (!tokenObject.setAttribute(CKA_OS_TOKENLABEL, tokenLabel) || |
| 143 |
!tokenObject.setAttribute(CKA_OS_TOKENSERIAL, tokenSerial) || |
| 144 |
!tokenObject.setAttribute(CKA_OS_TOKENFLAGS, tokenFlags)) |
| 145 |
{ |
| 146 |
_connection->close(); |
| 147 |
delete _connection; |
| 148 |
_connection = NULL; |
| 149 |
|
| 150 |
// Now remove the token file |
| 151 |
if (remove(tokenPath.c_str())) |
| 152 |
{ |
| 153 |
ERROR_MSG("Failed to remove the token file at \"%s\"", tokenPath.c_str()); |
| 154 |
} |
| 155 |
|
| 156 |
// Now remove the token directory |
| 157 |
if (remove(tokenDir.c_str())) |
| 158 |
{ |
| 159 |
ERROR_MSG("Failed to remove the token directory at \"%s\"", tokenDir.c_str()); |
| 160 |
} |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
_tokenMutex = MutexFactory::i()->getMutex(); |
| 165 |
// Success! |
| 166 |
} |
| 167 |
|
| 168 |
// Constructor for accessing an existing token. |
| 169 |
DBToken::DBToken(const std::string &baseDir, const std::string &tokenName) |
| 170 |
: _connection(NULL), _tokenMutex(NULL) |
| 171 |
{ |
| 172 |
std::string tokenDir = baseDir + OS_PATHSEP + tokenName; |
| 173 |
std::string tokenPath = tokenDir + OS_PATHSEP + DBTOKEN_FILE; |
| 174 |
|
| 175 |
// Refuse to open an already existing database. |
| 176 |
FILE *f = fopen(tokenPath.c_str(),"r"); |
| 177 |
if (f == NULL) |
| 178 |
{ |
| 179 |
ERROR_MSG("Refusing to open a non-existant database at \"%s\"", tokenPath.c_str()); |
| 180 |
return; |
| 181 |
} |
| 182 |
fclose(f); |
| 183 |
|
| 184 |
// Create a database connection. |
| 185 |
_connection = DB::Connection::Create(tokenDir, DBTOKEN_FILE); |
| 186 |
if (_connection == NULL) |
| 187 |
{ |
| 188 |
ERROR_MSG("Failed to create a database connection for \"%s\"", tokenPath.c_str()); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
if (!_connection->connect()) |
| 193 |
{ |
| 194 |
delete _connection; |
| 195 |
_connection = NULL; |
| 196 |
|
| 197 |
ERROR_MSG("Failed to connect to the database at \"%s\"", tokenPath.c_str()); |
| 198 |
|
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
// Find the DBObject for the established connection to the database. |
| 203 |
DBObject tokenObject(_connection); |
| 204 |
|
| 205 |
// First find the token obect that indicates the token is properly initialized. |
| 206 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 207 |
{ |
| 208 |
tokenObject.dropConnection(); |
| 209 |
|
| 210 |
_connection->close(); |
| 211 |
delete _connection; |
| 212 |
_connection = NULL; |
| 213 |
|
| 214 |
ERROR_MSG("Failed to open token object in the token database at \"%s\"", tokenPath.c_str()); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
_tokenMutex = MutexFactory::i()->getMutex(); |
| 219 |
|
| 220 |
// Success! |
| 221 |
} |
| 222 |
|
| 223 |
DBToken *DBToken::createToken(const std::string basePath, const std::string tokenDir, const ByteString &label, const ByteString &serial) |
| 224 |
{ |
| 225 |
Directory baseDir(basePath); |
| 226 |
|
| 227 |
if (!baseDir.isValid()) |
| 228 |
{ |
| 229 |
return NULL; |
| 230 |
} |
| 231 |
|
| 232 |
// Create the token directory |
| 233 |
if (!baseDir.mkdir(tokenDir)) |
| 234 |
{ |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
|
| 238 |
DBToken *token = new DBToken(basePath, tokenDir, label, serial); |
| 239 |
if (!token->isValid()) |
| 240 |
{ |
| 241 |
baseDir.rmdir(tokenDir); |
| 242 |
|
| 243 |
delete token; |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
|
| 247 |
DEBUG_MSG("Created new token %s", tokenDir.c_str()); |
| 248 |
|
| 249 |
return token; |
| 250 |
} |
| 251 |
|
| 252 |
DBToken *DBToken::accessToken(const std::string &basePath, const std::string &tokenDir) |
| 253 |
{ |
| 254 |
return new DBToken(basePath, tokenDir); |
| 255 |
} |
| 256 |
|
| 257 |
// Destructor |
| 258 |
DBToken::~DBToken() |
| 259 |
{ |
| 260 |
if (_tokenMutex) |
| 261 |
{ |
| 262 |
MutexFactory::i()->recycleMutex(_tokenMutex); |
| 263 |
_tokenMutex = NULL; |
| 264 |
} |
| 265 |
|
| 266 |
std::map<long long, OSObject*> cleanUp = _allObjects; |
| 267 |
_allObjects.clear(); |
| 268 |
for (std::map<long long, OSObject*>::iterator i = cleanUp.begin(); i != cleanUp.end(); ++i) |
| 269 |
{ |
| 270 |
delete i->second; |
| 271 |
} |
| 272 |
|
| 273 |
if (_connection) |
| 274 |
{ |
| 275 |
delete _connection; |
| 276 |
_connection = NULL; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
// Set the SO PIN |
| 281 |
bool DBToken::setSOPIN(const ByteString& soPINBlob) |
| 282 |
{ |
| 283 |
if (_connection == NULL) return false; |
| 284 |
|
| 285 |
// Create a DBObject for the established connection to the token object in the database |
| 286 |
DBObject tokenObject(_connection); |
| 287 |
|
| 288 |
if (!tokenObject.startTransaction(DBObject::ReadWrite)) |
| 289 |
{ |
| 290 |
ERROR_MSG("Unable to start a transaction for updating the SOPIN and TOKENFLAGS in token database at \"%s\"", _connection->dbpath().c_str()); |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
// First find the token object in the database. |
| 295 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 296 |
{ |
| 297 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 298 |
tokenObject.abortTransaction(); |
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
OSAttribute soPIN(soPINBlob); |
| 303 |
if (!tokenObject.setAttribute(CKA_OS_SOPIN, soPIN)) |
| 304 |
{ |
| 305 |
ERROR_MSG("Error while setting SOPIN in token database at \"%s\"", _connection->dbpath().c_str()); |
| 306 |
tokenObject.abortTransaction(); |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
if (!tokenObject.attributeExists(CKA_OS_TOKENFLAGS)) |
| 311 |
{ |
| 312 |
ERROR_MSG("Error while getting TOKENFLAGS from token database at \"%s\"", _connection->dbpath().c_str()); |
| 313 |
tokenObject.abortTransaction(); |
| 314 |
return false; |
| 315 |
} |
| 316 |
|
| 317 |
// Retrieve flags from the database and reset flags related to tries and expiration of the SOPIN. |
| 318 |
CK_ULONG flags = tokenObject.getAttribute(CKA_OS_TOKENFLAGS).getUnsignedLongValue() |
| 319 |
& ~(CKF_SO_PIN_COUNT_LOW | CKF_SO_PIN_FINAL_TRY | CKF_SO_PIN_LOCKED | CKF_SO_PIN_TO_BE_CHANGED); |
| 320 |
|
| 321 |
OSAttribute changedTokenFlags(flags); |
| 322 |
if (!tokenObject.setAttribute(CKA_OS_TOKENFLAGS, changedTokenFlags)) |
| 323 |
{ |
| 324 |
ERROR_MSG("Error while setting TOKENFLAGS in token database at \"%s\"", _connection->dbpath().c_str()); |
| 325 |
tokenObject.abortTransaction(); |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
if (!tokenObject.commitTransaction()) |
| 330 |
{ |
| 331 |
ERROR_MSG("Error while committing SOPIN and TOKENFLAGS changes to token database at \"%s\"", _connection->dbpath().c_str()); |
| 332 |
tokenObject.abortTransaction(); |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
return true; |
| 337 |
} |
| 338 |
|
| 339 |
// Get the SO PIN |
| 340 |
bool DBToken::getSOPIN(ByteString& soPINBlob) |
| 341 |
{ |
| 342 |
if (_connection == NULL) return false; |
| 343 |
|
| 344 |
// Create a DBObject for the established connection to the token object in the database |
| 345 |
DBObject tokenObject(_connection); |
| 346 |
|
| 347 |
if (!tokenObject.startTransaction(DBObject::ReadOnly)) |
| 348 |
{ |
| 349 |
ERROR_MSG("Unable to start a transaction for getting the SOPIN from token database at \"%s\"", _connection->dbpath().c_str()); |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
// First find the token object in the database. |
| 354 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 355 |
{ |
| 356 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 357 |
tokenObject.abortTransaction(); |
| 358 |
return false; |
| 359 |
} |
| 360 |
|
| 361 |
if (!tokenObject.attributeExists(CKA_OS_SOPIN)) |
| 362 |
{ |
| 363 |
ERROR_MSG("Error while getting SOPIN from token database at \"%s\"", _connection->dbpath().c_str()); |
| 364 |
tokenObject.abortTransaction(); |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
tokenObject.commitTransaction(); |
| 369 |
soPINBlob = tokenObject.getAttribute(CKA_OS_SOPIN).getByteStringValue(); |
| 370 |
return true; |
| 371 |
} |
| 372 |
|
| 373 |
// Set the user PIN |
| 374 |
bool DBToken::setUserPIN(ByteString userPINBlob) |
| 375 |
{ |
| 376 |
if (_connection == NULL) return false; |
| 377 |
|
| 378 |
// Create a DBObject for the established connection to the token object in the database |
| 379 |
DBObject tokenObject(_connection); |
| 380 |
|
| 381 |
if (!tokenObject.startTransaction(DBObject::ReadWrite)) |
| 382 |
{ |
| 383 |
ERROR_MSG("Unable to start a transaction for updating the USERPIN and TOKENFLAGS in token database at \"%s\"", _connection->dbpath().c_str()); |
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
// First find the token object in the database. |
| 388 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 389 |
{ |
| 390 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 391 |
tokenObject.abortTransaction(); |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
OSAttribute userPIN(userPINBlob); |
| 396 |
if (!tokenObject.setAttribute(CKA_OS_USERPIN, userPIN)) |
| 397 |
{ |
| 398 |
ERROR_MSG("Error while setting USERPIN in token database at \"%s\"", _connection->dbpath().c_str()); |
| 399 |
tokenObject.abortTransaction(); |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
if (!tokenObject.attributeExists(CKA_OS_TOKENFLAGS)) |
| 404 |
{ |
| 405 |
ERROR_MSG("Error while getting TOKENFLAGS from token database at \"%s\"", _connection->dbpath().c_str()); |
| 406 |
tokenObject.abortTransaction(); |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
// Retrieve flags from the database and reset flags related to tries and expiration of the user PIN. |
| 411 |
CK_ULONG flags = tokenObject.getAttribute(CKA_OS_TOKENFLAGS).getUnsignedLongValue(); |
| 412 |
flags |= CKF_USER_PIN_INITIALIZED; |
| 413 |
flags &= ~(CKF_USER_PIN_COUNT_LOW | CKF_USER_PIN_FINAL_TRY | CKF_USER_PIN_LOCKED | CKF_USER_PIN_TO_BE_CHANGED); |
| 414 |
|
| 415 |
OSAttribute changedTokenFlags(flags); |
| 416 |
if (!tokenObject.setAttribute(CKA_OS_TOKENFLAGS, changedTokenFlags)) |
| 417 |
{ |
| 418 |
ERROR_MSG("Error while setting TOKENFLAGS in token database at \"%s\"", _connection->dbpath().c_str()); |
| 419 |
tokenObject.abortTransaction(); |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
if (!tokenObject.commitTransaction()) |
| 424 |
{ |
| 425 |
ERROR_MSG("Error while committing USERPIN and TOKENFLAGS changes to token database at \"%s\"", _connection->dbpath().c_str()); |
| 426 |
tokenObject.abortTransaction(); |
| 427 |
return false; |
| 428 |
} |
| 429 |
|
| 430 |
return true; |
| 431 |
} |
| 432 |
|
| 433 |
// Get the user PIN |
| 434 |
bool DBToken::getUserPIN(ByteString& userPINBlob) |
| 435 |
{ |
| 436 |
if (_connection == NULL) return false; |
| 437 |
|
| 438 |
// Create a DBObject for the established connection to the token object in the database |
| 439 |
DBObject tokenObject(_connection); |
| 440 |
|
| 441 |
if (!tokenObject.startTransaction(DBObject::ReadOnly)) |
| 442 |
{ |
| 443 |
ERROR_MSG("Unable to start a transaction for getting the USERPIN from token database at \"%s\"", _connection->dbpath().c_str()); |
| 444 |
return false; |
| 445 |
} |
| 446 |
|
| 447 |
// First find the token object in the database. |
| 448 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 449 |
{ |
| 450 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 451 |
tokenObject.abortTransaction(); |
| 452 |
return false; |
| 453 |
} |
| 454 |
|
| 455 |
if (!tokenObject.attributeExists(CKA_OS_USERPIN)) |
| 456 |
{ |
| 457 |
ERROR_MSG("Error while getting USERPIN from token database at \"%s\"", _connection->dbpath().c_str()); |
| 458 |
tokenObject.abortTransaction(); |
| 459 |
return false; |
| 460 |
} |
| 461 |
|
| 462 |
tokenObject.commitTransaction(); |
| 463 |
userPINBlob = tokenObject.getAttribute(CKA_OS_USERPIN).getByteStringValue(); |
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
// Retrieve the token label |
| 468 |
bool DBToken::getTokenLabel(ByteString& label) |
| 469 |
{ |
| 470 |
if (_connection == NULL) return false; |
| 471 |
|
| 472 |
// Create a DBObject for the established connection to the token object in the database |
| 473 |
DBObject tokenObject(_connection); |
| 474 |
|
| 475 |
if (!tokenObject.startTransaction(DBObject::ReadOnly)) |
| 476 |
{ |
| 477 |
ERROR_MSG("Unable to start a transaction for getting the TOKENLABEL from token database at \"%s\"", _connection->dbpath().c_str()); |
| 478 |
return false; |
| 479 |
} |
| 480 |
|
| 481 |
// First find the token object in the database. |
| 482 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 483 |
{ |
| 484 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 485 |
tokenObject.abortTransaction(); |
| 486 |
return false; |
| 487 |
} |
| 488 |
|
| 489 |
if (!tokenObject.attributeExists(CKA_OS_TOKENLABEL)) |
| 490 |
{ |
| 491 |
ERROR_MSG("Error while getting TOKENLABEL from token database at \"%s\"", _connection->dbpath().c_str()); |
| 492 |
tokenObject.abortTransaction(); |
| 493 |
return false; |
| 494 |
} |
| 495 |
|
| 496 |
tokenObject.commitTransaction(); |
| 497 |
label = tokenObject.getAttribute(CKA_OS_TOKENLABEL).getByteStringValue(); |
| 498 |
return true; |
| 499 |
} |
| 500 |
|
| 501 |
// Retrieve the token serial |
| 502 |
bool DBToken::getTokenSerial(ByteString& serial) |
| 503 |
{ |
| 504 |
if (_connection == NULL) return false; |
| 505 |
|
| 506 |
// Create a DBObject for the established connection to the token object in the database |
| 507 |
DBObject tokenObject(_connection); |
| 508 |
|
| 509 |
if (!tokenObject.startTransaction(DBObject::ReadOnly)) |
| 510 |
{ |
| 511 |
ERROR_MSG("Unable to start a transaction for getting the TOKENSERIAL from token database at \"%s\"", _connection->dbpath().c_str()); |
| 512 |
return false; |
| 513 |
} |
| 514 |
|
| 515 |
// First find the token object in the database. |
| 516 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 517 |
{ |
| 518 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 519 |
tokenObject.abortTransaction(); |
| 520 |
return false; |
| 521 |
} |
| 522 |
|
| 523 |
if (!tokenObject.attributeExists(CKA_OS_TOKENSERIAL)) |
| 524 |
{ |
| 525 |
ERROR_MSG("Error while getting TOKENSERIAL from token database at \"%s\"", _connection->dbpath().c_str()); |
| 526 |
tokenObject.abortTransaction(); |
| 527 |
return false; |
| 528 |
} |
| 529 |
|
| 530 |
tokenObject.commitTransaction(); |
| 531 |
serial = tokenObject.getAttribute(CKA_OS_TOKENSERIAL).getByteStringValue(); |
| 532 |
return true; |
| 533 |
} |
| 534 |
|
| 535 |
// Get the token flags |
| 536 |
bool DBToken::getTokenFlags(CK_ULONG& flags) |
| 537 |
{ |
| 538 |
if (_connection == NULL) return false; |
| 539 |
|
| 540 |
// Create a DBObject for the established connection to the token object in the database |
| 541 |
DBObject tokenObject(_connection); |
| 542 |
|
| 543 |
if (!tokenObject.startTransaction(DBObject::ReadOnly)) |
| 544 |
{ |
| 545 |
ERROR_MSG("Unable to start a transaction for updating the SOPIN and TOKENFLAGS in token database at \"%s\"", _connection->dbpath().c_str()); |
| 546 |
return false; |
| 547 |
} |
| 548 |
|
| 549 |
// First find the token object in the database. |
| 550 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 551 |
{ |
| 552 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 553 |
tokenObject.abortTransaction(); |
| 554 |
return false; |
| 555 |
} |
| 556 |
|
| 557 |
if (!tokenObject.attributeExists(CKA_OS_TOKENFLAGS)) |
| 558 |
{ |
| 559 |
ERROR_MSG("Error while getting TOKENFLAGS from token database at \"%s\"", _connection->dbpath().c_str()); |
| 560 |
tokenObject.abortTransaction(); |
| 561 |
return false; |
| 562 |
} |
| 563 |
|
| 564 |
tokenObject.commitTransaction(); |
| 565 |
flags = tokenObject.getAttribute(CKA_OS_TOKENFLAGS).getUnsignedLongValue(); |
| 566 |
return true; |
| 567 |
} |
| 568 |
|
| 569 |
// Set the token flags |
| 570 |
bool DBToken::setTokenFlags(const CK_ULONG flags) |
| 571 |
{ |
| 572 |
if (_connection == NULL) return false; |
| 573 |
|
| 574 |
// Create a DBObject for the established connection to the token object in the database |
| 575 |
DBObject tokenObject(_connection); |
| 576 |
|
| 577 |
if (!tokenObject.startTransaction(DBObject::ReadWrite)) |
| 578 |
{ |
| 579 |
ERROR_MSG("Unable to start a transaction for setting the TOKENFLAGS in token database at \"%s\"", _connection->dbpath().c_str()); |
| 580 |
return false; |
| 581 |
} |
| 582 |
|
| 583 |
// First find the token object in the database. |
| 584 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 585 |
{ |
| 586 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 587 |
tokenObject.abortTransaction(); |
| 588 |
return false; |
| 589 |
} |
| 590 |
|
| 591 |
OSAttribute tokenFlags(flags); |
| 592 |
if (!tokenObject.setAttribute(CKA_OS_TOKENFLAGS, tokenFlags)) |
| 593 |
{ |
| 594 |
ERROR_MSG("Error while setting TOKENFLAGS in token database at \"%s\"", _connection->dbpath().c_str()); |
| 595 |
tokenObject.abortTransaction(); |
| 596 |
return false; |
| 597 |
} |
| 598 |
|
| 599 |
if (!tokenObject.commitTransaction()) |
| 600 |
{ |
| 601 |
ERROR_MSG("Error while committing TOKENFLAGS changes to token database at \"%s\"", _connection->dbpath().c_str()); |
| 602 |
tokenObject.abortTransaction(); |
| 603 |
return false; |
| 604 |
} |
| 605 |
|
| 606 |
return true; |
| 607 |
} |
| 608 |
|
| 609 |
// Retrieve objects |
| 610 |
std::set<OSObject *> DBToken::getObjects() |
| 611 |
{ |
| 612 |
std::set<OSObject*> objects; |
| 613 |
getObjects(objects); |
| 614 |
return objects; |
| 615 |
} |
| 616 |
|
| 617 |
void DBToken::getObjects(std::set<OSObject*> &objects) |
| 618 |
{ |
| 619 |
if (_connection == NULL) return; |
| 620 |
|
| 621 |
if (!_connection->beginTransactionRO()) return; |
| 622 |
|
| 623 |
DB::Statement statement = _connection->prepare("select id from object limit -1 offset 1"); |
| 624 |
|
| 625 |
DB::Result result = _connection->perform(statement); |
| 626 |
|
| 627 |
if (result.isValid()) |
| 628 |
{ |
| 629 |
do { |
| 630 |
long long objectId = result.getLongLong(1); |
| 631 |
{ |
| 632 |
MutexLocker lock(_tokenMutex); |
| 633 |
std::map<long long, OSObject*>::iterator it = _allObjects.find(objectId); |
| 634 |
if (it == _allObjects.end()) |
| 635 |
{ |
| 636 |
DBObject *object = new DBObject(_connection, this, objectId); |
| 637 |
_allObjects[objectId] = object; |
| 638 |
objects.insert(object); |
| 639 |
} |
| 640 |
else |
| 641 |
{ |
| 642 |
objects.insert(it->second); |
| 643 |
} |
| 644 |
} |
| 645 |
} while (result.nextRow()); |
| 646 |
} |
| 647 |
|
| 648 |
_connection->endTransactionRO(); |
| 649 |
} |
| 650 |
|
| 651 |
// Create a new object |
| 652 |
OSObject *DBToken::createObject() |
| 653 |
{ |
| 654 |
if (_connection == NULL) return NULL; |
| 655 |
|
| 656 |
DBObject *newObject = new DBObject(_connection, this); |
| 657 |
if (newObject == NULL) |
| 658 |
{ |
| 659 |
ERROR_MSG("Failed to create an object: out of memory"); |
| 660 |
return NULL; |
| 661 |
} |
| 662 |
|
| 663 |
if (!newObject->startTransaction(DBObject::ReadWrite)) |
| 664 |
{ |
| 665 |
delete newObject; |
| 666 |
ERROR_MSG("Unable to start a transaction in token database at \"%s\"", _connection->dbpath().c_str()); |
| 667 |
return NULL; |
| 668 |
} |
| 669 |
|
| 670 |
if (!newObject->insert()) |
| 671 |
{ |
| 672 |
newObject->abortTransaction(); |
| 673 |
delete newObject; |
| 674 |
ERROR_MSG("Unable to insert an object into token database at \"%s\"", _connection->dbpath().c_str()); |
| 675 |
return NULL; |
| 676 |
} |
| 677 |
|
| 678 |
if (!newObject->isValid()) |
| 679 |
{ |
| 680 |
newObject->abortTransaction(); |
| 681 |
delete newObject; |
| 682 |
ERROR_MSG("Object that was inserted in not valid"); |
| 683 |
return NULL; |
| 684 |
} |
| 685 |
|
| 686 |
if (!newObject->commitTransaction()) |
| 687 |
{ |
| 688 |
newObject->abortTransaction(); |
| 689 |
delete newObject; |
| 690 |
ERROR_MSG("Unable to commit a created object to token database at \"%s\"", _connection->dbpath().c_str()); |
| 691 |
return NULL; |
| 692 |
} |
| 693 |
|
| 694 |
// Now add the new object to the list of existing objects. |
| 695 |
{ |
| 696 |
MutexLocker lock(_tokenMutex); |
| 697 |
_allObjects[newObject->objectId()] = newObject; |
| 698 |
} |
| 699 |
|
| 700 |
return newObject; |
| 701 |
} |
| 702 |
|
| 703 |
bool DBToken::deleteObject(OSObject *object) |
| 704 |
{ |
| 705 |
if (_connection == NULL) return false; |
| 706 |
|
| 707 |
if (object == NULL) |
| 708 |
{ |
| 709 |
ERROR_MSG("Object passed in as a parameter is NULL"); |
| 710 |
return false; |
| 711 |
} |
| 712 |
|
| 713 |
if (!object->startTransaction(DBObject::ReadWrite)) |
| 714 |
{ |
| 715 |
ERROR_MSG("Unable to start a transaction for deleting an object in token database at \"%s\"", _connection->dbpath().c_str()); |
| 716 |
return false; |
| 717 |
} |
| 718 |
|
| 719 |
if (!static_cast<DBObject *>(object)->remove()) |
| 720 |
{ |
| 721 |
ERROR_MSG("Error while deleting an existing object from the token database at \"%s\"", _connection->dbpath().c_str()); |
| 722 |
object->abortTransaction(); |
| 723 |
return false; |
| 724 |
} |
| 725 |
|
| 726 |
if (!object->commitTransaction()) |
| 727 |
{ |
| 728 |
ERROR_MSG("Error while committing the deletion of an existing object in token database at \"%s\"", _connection->dbpath().c_str()); |
| 729 |
object->abortTransaction(); |
| 730 |
return false; |
| 731 |
} |
| 732 |
|
| 733 |
return true; |
| 734 |
} |
| 735 |
|
| 736 |
// Checks if the token is consistent |
| 737 |
bool DBToken::isValid() |
| 738 |
{ |
| 739 |
return _connection != NULL && _connection->tableExists("object"); |
| 740 |
} |
| 741 |
|
| 742 |
// Invalidate the token (for instance if it is deleted) |
| 743 |
void DBToken::invalidate() |
| 744 |
{ |
| 745 |
} |
| 746 |
|
| 747 |
// Delete the token. |
| 748 |
bool DBToken::clearToken() |
| 749 |
{ |
| 750 |
if (_connection == NULL) return false; |
| 751 |
|
| 752 |
std::string tokenDir = _connection->dbdir(); |
| 753 |
std::string tokenPath = _connection->dbpath(); |
| 754 |
|
| 755 |
if (!DBObject(_connection).dropTables()) |
| 756 |
{ |
| 757 |
ERROR_MSG("Failed to drop all tables in the token database at \"%s\"", tokenPath.c_str()); |
| 758 |
return false; |
| 759 |
} |
| 760 |
|
| 761 |
_connection->close(); |
| 762 |
delete _connection; |
| 763 |
_connection = NULL; |
| 764 |
|
| 765 |
// Remove all files from the token directory, even ones not placed there by us. |
| 766 |
Directory dir(tokenDir); |
| 767 |
std::vector<std::string> tokenFiles = dir.getFiles(); |
| 768 |
|
| 769 |
for (std::vector<std::string>::iterator i = tokenFiles.begin(); i != tokenFiles.end(); i++) |
| 770 |
{ |
| 771 |
if (!dir.remove(*i)) |
| 772 |
{ |
| 773 |
ERROR_MSG("Failed to remove \"%s\" from token directory \"%s\"", i->c_str(), tokenDir.c_str()); |
| 774 |
|
| 775 |
return false; |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
// Now remove the token directory |
| 780 |
if (!dir.rmdir("")) |
| 781 |
{ |
| 782 |
ERROR_MSG("Failed to remove the token directory \"%s\"", tokenDir.c_str()); |
| 783 |
|
| 784 |
return false; |
| 785 |
} |
| 786 |
|
| 787 |
DEBUG_MSG("Token instance %s was succesfully cleared", tokenDir.c_str()); |
| 788 |
|
| 789 |
return true; |
| 790 |
} |
| 791 |
|
| 792 |
// Reset the token |
| 793 |
bool DBToken::resetToken(const ByteString& label) |
| 794 |
{ |
| 795 |
if (_connection == NULL) return false; |
| 796 |
|
| 797 |
std::string tokenDir = _connection->dbdir(); |
| 798 |
|
| 799 |
// Clean up |
| 800 |
std::set<OSObject*> cleanUp = getObjects(); |
| 801 |
|
| 802 |
for (std::set<OSObject*>::iterator i = cleanUp.begin(); i != cleanUp.end(); i++) |
| 803 |
{ |
| 804 |
if (!deleteObject(*i)) |
| 805 |
{ |
| 806 |
ERROR_MSG("Unable to delete all objects in token database at \"%s\"", _connection->dbpath().c_str()); |
| 807 |
return false; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
// Create a DBObject for the established connection to the token object in the database |
| 812 |
DBObject tokenObject(_connection); |
| 813 |
|
| 814 |
if (!tokenObject.startTransaction(DBObject::ReadWrite)) |
| 815 |
{ |
| 816 |
ERROR_MSG("Unable to start a transaction for setting the TOKENLABEL in token database at \"%s\"", _connection->dbpath().c_str()); |
| 817 |
return false; |
| 818 |
} |
| 819 |
|
| 820 |
// First find the token object in the database. |
| 821 |
if (!tokenObject.find(DBTOKEN_OBJECT_TOKENINFO)) |
| 822 |
{ |
| 823 |
ERROR_MSG("Token object not found in token database at \"%s\"", _connection->dbpath().c_str()); |
| 824 |
tokenObject.abortTransaction(); |
| 825 |
return false; |
| 826 |
} |
| 827 |
|
| 828 |
if (tokenObject.attributeExists(CKA_OS_USERPIN)) |
| 829 |
{ |
| 830 |
if (!tokenObject.deleteAttribute(CKA_OS_USERPIN)) |
| 831 |
{ |
| 832 |
ERROR_MSG("Error while deleting USERPIN in token database at \"%s\"", _connection->dbpath().c_str()); |
| 833 |
tokenObject.abortTransaction(); |
| 834 |
return false; |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
if (!tokenObject.attributeExists(CKA_OS_TOKENFLAGS)) |
| 839 |
{ |
| 840 |
ERROR_MSG("Error while getting TOKENFLAGS from token database at \"%s\"", _connection->dbpath().c_str()); |
| 841 |
tokenObject.abortTransaction(); |
| 842 |
return false; |
| 843 |
} |
| 844 |
|
| 845 |
// Retrieve flags from the database and reset flags related to tries and expiration of the user PIN. |
| 846 |
CK_ULONG flags = tokenObject.getAttribute(CKA_OS_TOKENFLAGS).getUnsignedLongValue() |
| 847 |
& ~(CKF_USER_PIN_INITIALIZED | CKF_USER_PIN_COUNT_LOW | CKF_USER_PIN_FINAL_TRY | CKF_USER_PIN_LOCKED | CKF_USER_PIN_TO_BE_CHANGED); |
| 848 |
|
| 849 |
OSAttribute changedTokenFlags(flags); |
| 850 |
if (!tokenObject.setAttribute(CKA_OS_TOKENFLAGS, changedTokenFlags)) |
| 851 |
{ |
| 852 |
ERROR_MSG("Error while setting TOKENFLAGS in token database at \"%s\"", _connection->dbpath().c_str()); |
| 853 |
tokenObject.abortTransaction(); |
| 854 |
return false; |
| 855 |
} |
| 856 |
|
| 857 |
OSAttribute tokenLabel(label); |
| 858 |
if (!tokenObject.setAttribute(CKA_OS_TOKENLABEL, tokenLabel)) |
| 859 |
{ |
| 860 |
ERROR_MSG("Error while setting TOKENLABEL in token database at \"%s\"", _connection->dbpath().c_str()); |
| 861 |
tokenObject.abortTransaction(); |
| 862 |
return false; |
| 863 |
} |
| 864 |
|
| 865 |
if (!tokenObject.commitTransaction()) |
| 866 |
{ |
| 867 |
ERROR_MSG("Error while committing TOKENLABEL changes to token database at \"%s\"", _connection->dbpath().c_str()); |
| 868 |
tokenObject.abortTransaction(); |
| 869 |
return false; |
| 870 |
} |
| 871 |
|
| 872 |
DEBUG_MSG("Token instance %s was succesfully reset", tokenDir.c_str()); |
| 873 |
|
| 874 |
return true; |
| 875 |
} |