-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.js
141 lines (129 loc) · 3.71 KB
/
utils.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
const jwkToPem = require('jwk-to-pem')
const crypto = require('crypto');
const cbor = require('cbor');
const utils = {};
/**
* Evaluates the sha256 hash of a string
* @param {string} data
* @returns {Buffer} sha256 of the input data
*/
utils.sha256 = data => {
const hash = crypto.createHash('sha256');
hash.update(data);
return hash.digest();
}
/**
* Converts a JWK to a PEM, which is compatible with
* node's crypto verify methods.
* @param {any} jwk json web token
*/
utils.jwkToPem = jwk => {
return jwkToPem(jwk);
}
/**
* Utility function to convert a DER to a PEM
* @param {String} der base64 encoded DER
* @returns {String} PEM
*/
utils.derToPEM = der => {
return "-----BEGIN CERTIFICATE-----\n" +
der +
"\n-----END CERTIFICATE-----";
}
/**
* Turns a cert subject string into a map of its fields
* @param {string} subjectStr cert subject
* @returns {*} map of subject fields
*/
utils.parseCertSubject = (subjectStr) => {
return subjectStr
.slice(1)
.split("/")
.map(i=>i.split("="))
.reduce((a,c)=>{
a[c[0]] = c[1];
return a;
}, {});
}
/**
* Converts a COSE key to a JWK
* @param {Buffer} buffer Buffer containing cbor data with COSE key
* @returns {any} JWK object
*/
utils.coseToJwk = buffer => {
try {
let publicKeyJwk = {};
publicKeyCbor = cbor.decodeAllSync(buffer);
publicKeyCbor = publicKeyCbor[0]; //first element
if (publicKeyCbor.get(3) == -7) {
publicKeyJwk = {
kty: "EC",
crv: "P-256",
x: publicKeyCbor.get(-2).toString('base64'),
y: publicKeyCbor.get(-3).toString('base64')
}
} else if (publicKeyCbor.get(3) == -35) {
publicKeyJwk = {
kty: "EC",
crv: "P-384",
x: publicKeyCbor.get(-2).toString('base64'),
y: publicKeyCbor.get(-3).toString('base64')
}
} else if (publicKeyCbor.get(3) == -36) {
publicKeyJwk = {
kty: "EC",
crv: "P-521",
x: publicKeyCbor.get(-2).toString('base64'),
y: publicKeyCbor.get(-3).toString('base64')
}
} else if (publicKeyCbor.get(3) == -257) {
publicKeyJwk = {
kty: "RSA",
n: publicKeyCbor.get(-1).toString('base64'),
e: publicKeyCbor.get(-2).toString('base64')
}
} else if (publicKeyCbor.get(3) == -8) {
publicKeyJwk = {
key : {
kty: "OKP",
crv: "Ed25519",
x: publicKeyCbor.get(-2).toString('base64')
},
format: 'jwk'
}
} else {
throw new Error("Unknown public key algorithm");
}
return publicKeyJwk;
} catch (e) {
throw new Error("Could not decode COSE Key");
}
}
/**
* Converts a COSE key to hex
* @param {Buffer} buffer Buffer containing cbor data with COSE key
* @returns {String} hex encoded
*/
utils.coseToHex = buffer => {
try {
publicKeyCbor = cbor.decodeAllSync(buffer);
publicKeyCbor = publicKeyCbor[0]; //first element
return cbor.encode(publicKeyCbor).toString('hex').toUpperCase();
} catch (e) {
throw new Error("Could not decode COSE Key");
}
}
/**
* Returns a default value if the provided string is undefined
* @param {string} str
* @param {string} defaultStr
* @returns {string}
*/
utils.defaultTo = (str, defaultStr) => {
if (typeof(str) === 'undefined') {
return defaultStr;
} else {
return str;
}
}
module.exports = utils;