-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmech.ts
More file actions
274 lines (246 loc) · 7.77 KB
/
mech.ts
File metadata and controls
274 lines (246 loc) · 7.77 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
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
import * as pkcs11 from "pkcs11js";
import * as fs from "fs";
import * as core from "./core";
import { IParams } from "./keys/params";
import { MechanismEnum } from "./mech_enum";
import type { KeyGenMechanism } from "./objects";
import { uint64ToBuffer } from "./core";
/**
* Structure that describes algorithm
*/
export interface IAlgorithm {
/**
* The algorithm name
*/
name: keyof typeof MechanismEnum | string | number;
/**
* The algorithm parameters
*/
params: Buffer | IParams | null;
}
export type MechanismType = MechanismEnum | KeyGenMechanism | IAlgorithm | keyof typeof MechanismEnum | string;
/**
* Bit flags specifying mechanism capabilities
*/
export enum MechanismFlag {
/**
* `true` if the mechanism is performed by the device; `false` if the mechanism is performed in software
*/
HW = pkcs11.CKF_HW,
/**
* `true` if the mechanism can be used with encrypt function
*/
ENCRYPT = pkcs11.CKF_ENCRYPT,
/**
* `true` if the mechanism can be used with decrypt function
*/
DECRYPT = pkcs11.CKF_DECRYPT,
/**
* `true` if the mechanism can be used with digest function
*/
DIGEST = pkcs11.CKF_DIGEST,
/**
* `true` if the mechanism can be used with sign function
*/
SIGN = pkcs11.CKF_SIGN,
/**
* `true` if the mechanism can be used with sign recover function
*/
SIGN_RECOVER = pkcs11.CKF_SIGN_RECOVER,
/**
* `true` if the mechanism can be used with verify function
*/
VERIFY = pkcs11.CKF_VERIFY,
/**
* `true` if the mechanism can be used with verify recover function
*/
VERIFY_RECOVER = pkcs11.CKF_VERIFY_RECOVER,
/**
* `true` if the mechanism can be used with geberate function
*/
GENERATE = pkcs11.CKF_GENERATE,
/**
* `true` if the mechanism can be used with generate key pair function
*/
GENERATE_KEY_PAIR = pkcs11.CKF_GENERATE_KEY_PAIR,
/**
* `true` if the mechanism can be used with wrap function
*/
WRAP = pkcs11.CKF_WRAP,
/**
* `true` if the mechanism can be used with unwrap function
*/
UNWRAP = pkcs11.CKF_UNWRAP,
/**
* `true` if the mechanism can be used with derive function
*/
DERIVE = pkcs11.CKF_DERIVE,
}
/**
* Represents a PKCS#11 mechanism
*/
export class Mechanism extends core.HandleObject {
/**
* Returns string name from {@link MechanismEnum}. For unregistered mechanism returns string `unknown`.
* To register a custom mechanism in {@link MechanismEnum} use {@link Mechanism.vendor} static function
*/
get name(): string {
return MechanismEnum[this.type] || "unknown";
}
/**
* Creates PKCS#11 mechanism structure from {@link MechanismType}
* @param algorithm Mechanism type
*/
public static create(algorithm: MechanismType): pkcs11.Mechanism {
let res: pkcs11.Mechanism;
let alg: IAlgorithm;
if (core.isString(algorithm)) {
alg = { name: algorithm as string, params: null };
} else if (core.isNumber(algorithm)) {
alg = { name: algorithm, params: null };
} else {
alg = algorithm as IAlgorithm;
}
let hAlg: number;
if (core.isNumber(alg.name)) {
hAlg = alg.name;
} else {
hAlg = (MechanismEnum as any)[alg.name.toUpperCase()];
if (core.isEmpty(hAlg)) {
throw new TypeError(`Unknown mechanism name '${alg.name}'`);
}
}
let params: any = null;
if (alg.params) {
if ((alg.params as any).toCKI) {
// Convert object with toCKI to Buffer
params = (alg.params as IParams).toCKI();
} else {
params = alg.params;
}
}
res = {
mechanism: hAlg,
parameter: params,
};
return res;
}
/**
* Adds a vendor mechanisms to {@link MechanismEnum} from the specified file
* @param jsonFile Path to JSON file with vendor mechanisms
*/
public static vendor(jsonFile: string): void;
/**
* Adds a vendor mechanism to {@link MechanismEnum}
* @param name Mechanism name
* @param value Mechanism value
*/
public static vendor(name: string, value: number): void;
public static vendor(name: string, value?: number): void {
const mechs: any = MechanismEnum;
if (core.isEmpty(value)) {
// vendor(jsonFile: string);
const file = fs.readFileSync(name);
const vendor = JSON.parse(file.toString());
for (const i in vendor) {
const newName = i;
const v = vendor[i];
mechs[newName] = v;
mechs[v] = newName;
}
} else {
// vendor(name: string, value: number)
const newName = name;
mechs[newName] = value;
mechs[value as any] = newName;
}
}
/**
* The mechanism type number
*/
public type: MechanismEnum;
/**
* The minimum size of the key for the mechanism
*
* _whether this is measured in bits or in bytes is mechanism-dependent_
*/
public minKeySize: number;
/**
* The maximum size of the key for the mechanism
*
* _whether this is measured in bits or in bytes is mechanism-dependent_
*/
public maxKeySize: number;
/**
* Bit flag specifying mechanism capabilities
*/
public flags: number;
/**
* The ID of the token’s slot
*/
protected slotHandle: core.Handle;
/**
* Initializes the mechanism structure
* @param type The type of mechanism
* @param handle The ID of mechanism
* @param slotHandle The ID of the token’s slot
* @param lib PKCS#11 module
*/
constructor(type: number, handle: pkcs11.Handle, slotHandle: core.Handle, lib: pkcs11.PKCS11) {
super(handle, lib);
this.type = type;
this.slotHandle = slotHandle;
this.getInfo();
}
/**
* Retrieves information about mechanism and fills object fields
*/
protected getInfo(): void {
const info = this.lib.C_GetMechanismInfo(this.slotHandle, this.type);
this.minKeySize = info.minKeySize;
this.maxKeySize = info.maxKeySize;
this.flags = info.flags;
}
}
/**
* Represents a collection of PKCS#11 mechanisms
*/
export class MechanismCollection extends core.Collection<Mechanism> {
/**
* The ID of token's slot
*/
protected slotHandle: core.Handle;
/**
* Initialize a new instance of mechanism collection
* @param items The list of mechanism types
* @param slotHandle The ID of token's slot
* @param lib PKCS#11 module
*/
constructor(items: number[], slotHandle: core.Handle, lib: pkcs11.PKCS11) {
super(items, lib, Mechanism);
this.slotHandle = slotHandle;
}
/**
* Returns item from collection by index
* @param {number} index index of an element in the collection `[0..n]`
*/
public items(index: number): Mechanism {
const type = this.innerItems[index];
// convert type to buffer
const handle = uint64ToBuffer(type, true);
return new Mechanism(type, handle, this.slotHandle, this.lib);
}
/**
* Tries to get Mechanism. Returns `null` if it's impossible to get mechanism
* @param index index of an element in the collection `[0..n]`
*/
public tryGetItem(index: number): Mechanism | null {
// Some providers throw CKR_MECHANISM_INVALID on `C_GetMechanismInfo` method
// https://github.com/PeculiarVentures/node-webcrypto-p11/issues/55
try {
return this.items(index);
} catch {
return null;
}
}
}