-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathslot.ts
More file actions
155 lines (132 loc) · 3.58 KB
/
slot.ts
File metadata and controls
155 lines (132 loc) · 3.58 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
153
154
155
import * as pkcs11 from "pkcs11js";
import * as core from "./core";
import { Module, SessionFlag, Session, Token, MechanismCollection } from "./";
import { removePadding } from "./core";
/**
* Enumeration specifies slot flags
*/
export enum SlotFlag {
/**
* `true` if a token is present in the slot (e.g., a device is in the reader)
*/
TOKEN_PRESENT = 1,
/**
* `true` if the reader supports removable devices
*/
REMOVABLE_DEVICE = 2,
/**
* `true` if the slot is a hardware slot, as opposed to a software slot implementing a "soft token"
*/
HW_SLOT = 4,
}
/**
* Represents PKCS#11 slot
*/
export class Slot extends core.HandleObject {
/**
* Character-string description of the slot
*/
public slotDescription: string;
/**
* ID of the slot manufacturer
*/
public manufacturerID: string;
/**
* Bits flags that provide capabilities of the slot
*/
public flags: SlotFlag;
/**
* Version number of the slot's hardware
*/
public hardwareVersion: pkcs11.Version;
/**
* Version number of the slot's firmware
*/
public firmwareVersion: pkcs11.Version;
/**
* PKCS#11 module
*/
public module: Module;
/**
* Creates a new instance of {@link Slot}
* @param handle ID of token's slot
* @param module PKCS#11 module
* @param lib PKCS#11 library
*/
constructor(handle: core.Handle, module: Module, lib: pkcs11.PKCS11) {
super(handle, lib);
this.module = module;
this.getInfo();
}
/**
* Returns information about token
* @returns PKCS#11 token structure
*/
public getToken(): Token {
return new Token(this.handle, this.lib);
}
/**
* Obtains a list of mechanism types supported by a token
* @returns The list of {@link Mechanism}
*/
public getMechanisms(): MechanismCollection {
const arr = this.lib.C_GetMechanismList(this.handle);
return new MechanismCollection(arr, this.handle, this.lib);
}
/**
* Initializes a token
* @param pin the SO's initial PIN
* @param label token label
* @returns Token label
*/
public initToken(pin: string, label = ""): string {
const res = this.lib.C_InitToken(this.handle, pin, label);
return removePadding(res);
}
/**
* Opens a session between an application and a token in a particular slot
*
* @param flags Indicates the type of session
* @returns Opened session
*/
public open(flags: SessionFlag = SessionFlag.SERIAL_SESSION): Session {
const hSession = this.lib.C_OpenSession(this.handle, flags);
return new Session(hSession, this, this.lib);
}
/**
* Closes all sessions an application has with a token
*/
public closeAll() {
this.lib.C_CloseAllSessions(this.handle);
}
protected getInfo(): void {
const info = this.lib.C_GetSlotInfo(this.handle);
this.slotDescription = core.removePadding(info.slotDescription);
this.manufacturerID = core.removePadding(info.manufacturerID);
this.flags = info.flags;
this.hardwareVersion = info.hardwareVersion;
this.firmwareVersion = info.firmwareVersion;
}
}
/**
* Collection of slots
*/
export class SlotCollection extends core.Collection<Slot> {
/**
* PKCS#11 module
*/
public module: Module;
/**
* Creates a new instance of {@link SlotCollection}
* @param items The kist of slot handles
* @param module PKCS#11 module
* @param lib PKCS#11 library
*/
constructor(items: Buffer[], module: Module, lib: pkcs11.PKCS11) {
super(items, lib, Slot);
this.module = module;
}
public items(index: number): Slot {
return new Slot(this.innerItems[index], this.module, this.lib);
}
}