-
Notifications
You must be signed in to change notification settings - Fork 3
/
common-ecc.js
227 lines (194 loc) · 6.96 KB
/
common-ecc.js
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
const crypto = require('crypto')
const ecKeyUtils = require('eckey-utils')
const base64 = require('./base64')
/**
* Generate a key identifier suitable for use as a `kid` in a JSON Web Key.
*
* @param {Object} jwk - The JSON Web Key for which a `kid` is going to be generated.
* @returns {Promise<string>} The key identifier.
*/
const generateKeyIdentifier = (jwk) => {
return new Promise(async (resolve, reject) => {
if (jwk && jwk.publicKey && jwk.publicKey.x) {
let buffer = Buffer.from((await base64.urlDecode(jwk.publicKey.x)))
return resolve(buffer.slice(0, 4).toString('hex'))
} else {
return reject(new TypeError('Invalid JSON Web Key supplied.'))
}
})
}
module.exports = {
/**
* Convert a PEM certificate to a JSON Web Key.
*
* @param {string} curveName - The type of ECC curve.
* @param {string|Buffer} privateOrPublicPem - The private or public PEM key.
* @param {Array} privateKeyOps - The operations intended for the private key.
* @param {Array} publicKeyOps - The operations intended for the public key.
* @returns {Promise<Object>} The converted certificate.
*/
convertPemToJwk: (curveName, privateOrPublicPem, privateKeyOps = [], publicKeyOps = []) => {
return new Promise(async (resolve, reject) => {
if (privateKeyOps.length === 0) {
privateKeyOps = ['deriveKey', 'sign']
}
if (publicKeyOps.length === 0) {
publicKeyOps = ['verify']
}
try {
let parsed = ecKeyUtils.parsePem(privateOrPublicPem)
let params = {}
if (parsed.privateKey !== void 0 && parsed.privateKey !== null) {
params.privateKey = parsed.privateKey
}
if (parsed.publicKey !== void 0 && parsed.publicKey !== null) {
params.publicKey = parsed.publicKey
}
let jwk = ecKeyUtils.generateJwk(curveName, parsed)
let kid = (await generateKeyIdentifier(jwk))
if (jwk.privateKey !== void 0) {
jwk.privateKey.kid = kid
jwk.privateKey.key_ops = privateKeyOps
}
if (jwk.publicKey !== void 0) {
jwk.publicKey.kid = kid
jwk.publicKey.key_ops = publicKeyOps
}
return resolve(jwk)
} catch (err) {
return reject(err)
}
})
},
/**
* Convert a JSON Web Key to a PEM certificate.
*
* @param {string} curveName - The type of ECC curve.
* @param {string|Buffer} privateOrPublicJwk - The private or public JSON Web Key.
* @returns {Promise<Object>} The converted certificate.
*/
convertJwkToPem: (curveName, privateOrPublicJwk) => {
return new Promise((resolve, reject) => {
try {
let parsed = ecKeyUtils.parseJwk(privateOrPublicJwk)
let params = {}
if (parsed.privateKey !== void 0 && parsed.privateKey !== null) {
params.privateKey = parsed.privateKey
}
if (parsed.publicKey !== void 0 && parsed.publicKey !== null) {
params.publicKey = parsed.publicKey
}
return resolve(ecKeyUtils.generatePem(curveName, params))
} catch (err) {
return reject(err)
}
})
},
/**
* Generate an ECDH key pair as a JSON Web Key.
*
* @param {string} curveName - The type of ECC curve.
* @param {Array} privateKeyOps - The operations intended for the private key.
* @param {Array} publicKeyOps - The operations intended for the public key.
* @returns {Promise<Object>} The ECDH key pair.
*/
generateJwkKeyPair: (curveName, privateKeyOps = [], publicKeyOps = []) => {
if (privateKeyOps.length === 0) {
privateKeyOps = ['deriveKey', 'sign']
}
if (publicKeyOps.length === 0) {
publicKeyOps = ['verify']
}
return new Promise(async (resolve, reject) => {
let ecdh = crypto.createECDH(curveName)
ecdh.generateKeys()
let params = {
privateKey: ecdh.getPrivateKey(),
publicKey: ecdh.getPublicKey()
}
let jwk = ecKeyUtils.generateJwk(curveName, params)
let kid = await generateKeyIdentifier(jwk)
jwk.privateKey.kid = kid
jwk.privateKey.key_ops = privateKeyOps
jwk.publicKey.kid = kid
jwk.publicKey.key_ops = publicKeyOps
return resolve(jwk)
})
},
/**
* Generate an ECDH key pair as PEM certificates.
*
* @param {string} curveName - The type of ECC curve.
* @returns {Promise<Object>} The PEM certificates.
*/
generatePemKeyPair: (curveName) => {
return new Promise((resolve, reject) => {
let ecdh = crypto.createECDH(curveName)
ecdh.generateKeys()
let params = {
privateKey: ecdh.getPrivateKey(),
publicKey: ecdh.getPublicKey()
}
return resolve(ecKeyUtils.generatePem(curveName, params))
})
},
/**
* Sign a payload to prevent it from tamper.
*
* @param {string} payload - The payload to sign.
* @param {string} privateKeyPem - The private key in PEM format.
* @param {string} hashType - The type of SHA2 digest to use. Defaults to SHA-256.
* @returns {Promise<Buffer>} The signature for the payload.
*/
signPayload: (payload, privateKeyPem, hashType = 'sha256') => {
return new Promise((resolve, reject) => {
if (typeof payload !== 'string') {
return reject(new TypeError('Payload must be a string.'))
}
let signer = crypto.createSign(hashType)
let message = Buffer.from(payload)
signer.update(message)
return resolve(signer.sign(privateKeyPem))
})
},
/**
* Verify the signature of a given payload.
*
* @param {string} payload - The payload against which the signature will be checked.
* @param {string|Buffer} signature - The signature of the payload.
* @param {Object} publicKeyPem - The public ECDH key in PEM format.
* @param {string} hashType - The type of SHA2 digest to use. Defaults to SHA-256.
* @returns {Promise<boolean>}
*/
verifyPayloadSignature: (payload, signature, publicKeyPem, hashType = 'sha256') => {
return new Promise((resolve, reject) => {
if (typeof payload !== 'string') {
return reject(new TypeError('Payload must be a string.'))
}
let verifier = crypto.createVerify(hashType)
let message = Buffer.from(payload)
verifier.update(message)
return resolve(verifier.verify(publicKeyPem, signature))
})
},
/**
* Compute an ECDH shared secret.
*
* @param {string} curveName - The type of ECC curve.
* @param {Object} privatePemKey - The private ECDH key in PEM format.
* @param {Object} otherPublicPemKey - The other public ECDH key in PEM format.
* @returns {Promise<string>}
*/
computeSecret: (curveName, privatePemKey, otherPublicPemKey) => {
return new Promise((resolve, reject) => {
try {
let ecdh = crypto.createECDH(curveName)
ecdh.setPrivateKey((ecKeyUtils.parsePem(privatePemKey)).privateKey)
return resolve(ecdh.computeSecret(
(ecKeyUtils.parsePem(otherPublicPemKey)).publicKey))
} catch (err) {
return reject(err)
}
})
}
}