/
utils.ts
161 lines (147 loc) · 4.24 KB
/
utils.ts
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
import * as crypto from "crypto";
import { Session, Slot, SlotFlag } from "graphene-pk11";
import { Convert } from "pvtsutils";
import { BufferSourceConverter } from "webcrypto-core";
import { ID_DIGEST } from "./const";
export interface HashedAlgorithm extends Algorithm {
hash: AlgorithmIdentifier;
}
export function GUID(session: Session): Buffer {
return crypto.randomBytes(20);
}
export function b64UrlDecode(b64url: string): Buffer {
return Buffer.from(Convert.FromBase64Url(b64url));
}
/**
* Converts BufferSource to Buffer
* @param data Array which must be prepared
*/
export function prepareData(data: BufferSource): Buffer {
return Buffer.from(BufferSourceConverter.toArrayBuffer(data));
}
export function isHashedAlgorithm(data: any): data is HashedAlgorithm {
return data instanceof Object
&& "name" in data
&& "hash" in data;
}
export function isCryptoKeyPair(data: any): data is CryptoKeyPair {
return data && data.privateKey && data.publicKey;
}
export function prepareAlgorithm(algorithm: AlgorithmIdentifier): Algorithm {
if (typeof algorithm === "string") {
return {
name: algorithm,
} as Algorithm;
}
if (isHashedAlgorithm(algorithm)) {
const preparedAlgorithm = { ...algorithm };
preparedAlgorithm.hash = this.prepareAlgorithm(algorithm.hash);
return preparedAlgorithm as HashedAlgorithm;
}
return { ...algorithm };
}
/**
* Calculates digest for given data
* @param algorithm
* @param data
*/
export function digest(algorithm: string, data: BufferSource): Buffer {
const hash = crypto.createHash(algorithm.replace("-", ""));
hash.update(prepareData(Buffer.from(BufferSourceConverter.toArrayBuffer(data))));
return hash.digest();
}
function calculateProviderID(slot: Slot) {
const str = slot.manufacturerID + slot.slotDescription + slot.getToken().serialNumber + slot.handle.toString("hex");
return digest(ID_DIGEST, Buffer.from(str)).toString("hex");
}
export function getProviderInfo(slot: Slot) {
// get index of slot
const slots = slot.module.getSlots(true);
let index = -1;
for (let i = 0; i < slots.length; i++) {
if (slots.items(i).handle.equals(slot.handle)) {
index = i;
break;
}
}
const token = slot.getToken();
const provider: ProviderInfo = {
id: calculateProviderID(slot),
slot: index,
name: token.label,
reader: slot.slotDescription,
serialNumber: slot.getToken().serialNumber,
algorithms: [],
isRemovable: !!(slot.flags & SlotFlag.REMOVABLE_DEVICE),
isHardware: !!(slot.flags & SlotFlag.HW_SLOT),
};
const algorithms = slot.getMechanisms();
for (let i = 0; i < algorithms.length; i++) {
const algorithm = algorithms.items(i);
let algName = "";
switch (algorithm.name) {
case "SHA_1":
algName = "SHA-1";
break;
case "SHA256":
algName = "SHA-256";
break;
case "SHA384":
algName = "SHA-384";
break;
case "SHA512":
algName = "SHA-512";
break;
case "RSA_PKCS":
case "SHA1_RSA_PKCS":
case "SHA256_RSA_PKCS":
case "SHA384_RSA_PKCS":
case "SHA512_RSA_PKCS":
algName = "RSASSA-PKCS1-v1_5";
break;
case "SHA1_RSA_PSS":
case "SHA256_RSA_PSS":
case "SHA384_RSA_PSS":
case "SHA512_RSA_PSS":
algName = "RSA-PSS";
break;
case "SHA1_RSA_PKCS_PSS":
case "SHA256_RSA_PKCS_PSS":
case "SHA384_RSA_PKCS_PSS":
case "SHA512_RSA_PKCS_PSS":
algName = "RSA-PSS";
break;
case "RSA_PKCS_OAEP":
algName = "RSA-OAEP";
break;
case "ECDSA":
case "ECDSA_SHA1":
case "ECDSA_SHA256":
case "ECDSA_SHA384":
case "ECDSA_SHA512":
algName = "ECDSA";
break;
case "ECDH1_DERIVE":
algName = "ECDH";
break;
case "AES_CBC_PAD":
algName = "AES-CBC";
break;
case "AES_ECB":
case "AES_ECB_PAD":
algName = "AES-ECB";
break;
case "AES_GCM_PAD":
algName = "AES-GCM";
break;
case "AES_KEY_WRAP_PAD":
algName = "AES-KW";
break;
default:
}
if (algName && !provider.algorithms.some((alg) => alg === algName)) {
provider.algorithms.push(algName);
}
}
return provider;
}