-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtoken.ts
More file actions
152 lines (128 loc) · 3.92 KB
/
token.ts
File metadata and controls
152 lines (128 loc) · 3.92 KB
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
import * as pkcs11 from "pkcs11js";
import * as core from "./core";
export enum TokenFlag {
RNG = 0x00000001,
WRITE_PROTECTED = 0x00000002,
LOGIN_REQUIRED = 0x00000004,
USER_PIN_INITIALIZED = 0x00000008,
RESTORE_KEY_NOT_NEEDED = 0x00000020,
CLOCK_ON_TOKEN = 0x00000040,
PROTECTED_AUTHENTICATION_PATH = 0x00000100,
DUAL_CRYPTO_OPERATIONS = 0x00000200,
TOKEN_INITIALIZED = 0x00000400,
SECONDARY_AUTHENTICATION = 0x00000800,
USER_PIN_COUNT_LOW = 0x00010000,
USER_PIN_FINAL_TRY = 0x00020000,
USER_PIN_LOCKED = 0x00040000,
USER_PIN_TO_BE_CHANGED = 0x00080000,
SO_PIN_COUNT_LOW = 0x00100000,
SO_PIN_FINAL_TRY = 0x00200000,
SO_PIN_LOCKED = 0x00400000,
SO_PIN_TO_BE_CHANGED = 0x00800000,
ERROR_STATE = 0x01000000,
}
/**
* Represents PKCS#11 token structure
*/
export class Token extends core.HandleObject {
/**
* Application-defined label, assigned during token initialization
*/
public label: string;
/**
* ID of the device manufacturer
*/
public manufacturerID: string;
/**
* Model of the device
*/
public model: string;
/**
* Character-string serial number of the device.
*/
public serialNumber: string;
/**
* Bit flags indicating capabilities and status of the device
*/
public flags: TokenFlag;
/**
* Maximum number of sessions that can be opened with the token at one time by a single application
*/
public maxSessionCount: number;
/**
* Number of sessions that this application currently has open with the token
*/
public sessionCount: number;
/**
* maximum number of read/write sessions that can be opened
* with the token at one time by a single application
*/
public maxRwSessionCount: number;
/**
* Number of read/write sessions that this application currently has open with the token
*/
public rwSessionCount: number;
/**
* Maximum length in bytes of the PIN
*/
public maxPinLen: number;
/**
* Minimum length in bytes of the PIN
*/
public minPinLen: number;
/**
* The total amount of memory on the token in bytes in which public objects may be stored
*/
public totalPublicMemory: number;
/**
* The amount of free (unused) memory on the token in bytes for public objects
*/
public freePublicMemory: number;
/**
* The total amount of memory on the token in bytes in which private objects may be stored
*/
public totalPrivateMemory: number;
/**
* The amount of free (unused) memory on the token in bytes for private objects
*/
public freePrivateMemory: number;
/**
* Version number of hardware
*/
public hardwareVersion: pkcs11.Version;
/**
* Version number of firmware
*/
public firmwareVersion: pkcs11.Version;
/**
* Current time. Null when a token does not have a clock.
*/
public utcTime: Date | null;
constructor(handle: core.Handle, lib: pkcs11.PKCS11) {
super(handle, lib);
this.getInfo();
}
protected getInfo() {
const info = this.lib.C_GetTokenInfo(this.handle);
this.label = core.removePadding(info.label);
this.manufacturerID = core.removePadding(info.manufacturerID);
this.model = core.removePadding(info.model);
this.serialNumber = core.removePadding(Buffer.from(info.serialNumber).toString());
this.flags = info.flags;
this.maxSessionCount = info.maxSessionCount;
this.sessionCount = info.sessionCount;
this.maxRwSessionCount = info.maxRwSessionCount;
this.rwSessionCount = info.rwSessionCount;
this.maxPinLen = info.maxPinLen;
this.minPinLen = info.minPinLen;
this.totalPublicMemory = info.totalPublicMemory;
this.freePublicMemory = info.freePublicMemory;
this.totalPrivateMemory = info.totalPrivateMemory;
this.freePrivateMemory = info.freePrivateMemory;
this.hardwareVersion = info.hardwareVersion;
this.firmwareVersion = info.firmwareVersion;
this.utcTime = info.flags & TokenFlag.CLOCK_ON_TOKEN
? core.dateFromString(info.utcTime)
: null;
}
}