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