forked from oseme-techguy/coralpay-c-connect-node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keymanager.js
executable file
·364 lines (308 loc) · 11.1 KB
/
keymanager.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* Created by osemeodigie on 13/05/2019.
* objective: building to scale
*
* @author (Author): Oseme Odigie @oseme-techguy
*
* @package CoralPayPGPLibrary
*/
const fs = require('fs');
const GPG = require('gpg');
const shelljs = require('shelljs');
const misc = require('locutus/php/misc');
/**
* GPG status output file descriptor. The status file descriptor outputs
* detailed information for many GPG commands. See the second section of
* the file <b>doc/DETAILS</b> in the
* {@link http://www.gnupg.org/download/ GPG package} for a detailed
* description of GPG's status output.
*/
const FD_STATUS = 3;
/**
* Command input file descriptor. This is used for methods requiring
* passphrases.
*/
const FD_COMMAND = 4;
const defaultArguments = [
// '--status-fd ' + FD_STATUS,
// '--command-fd ' + FD_COMMAND,
'--exit-on-status-write-error',
'--no-permission-warning',
'--no-secmem-warning',
'--no-tty',
'--no-default-keyring', // ignored if keying files are not specified
'--no-options' // prevent creation of ~/.gnupg directory
];
/**
* Used to import a key into the GPG keychain.
*
* @param {*} keyContentToImport
* @param {*} importOptions
*/
const importKeys = async (keyContentToImport, importOptions = {}) => {
const argOptions = importOptions || {
debug: false
};
return new Promise((resolve, reject) => {
GPG.importKey(keyContentToImport, function(importError, result, fingerprint) {
if(!importError) {
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`\n\nSuccesfully imported the key!!! \n\nImported Key Data: \n${result} \n\n`);
}
const keyId = (fingerprint != null && fingerprint != undefined) ? fingerprint : null;
return resolve(keyId);
}
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`\nError occurred while importing key!\n`);
}
return reject(new Error(`Error occurred while importing key!`));
});
});
};
exports.importKeys = importKeys;
/**
* This method is used to encrypt the request to be sent to Cgate.
*
* @param {*} plainMessage - the plain request to encrypt
* @param {*} keyIdForPublicKey - the Long/Short ID for the imported public key
* @param {*} encryptOptions - options to control how the output is generated
*/
const _encryptRequest = async (plainMessage, keyIdForPublicKey, encryptOptions = {}) => {
const argOptions = encryptOptions || {
homedir: null,
armor: true,
debug: false,
format: 'binary', // binary , hex or armor
showVersion: false
};
if(keyIdForPublicKey == undefined || keyIdForPublicKey == null) {
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`Error: Key Not Found with Id`);
}
throw new Error(`Error: Key Not Found with Id`);
return;
}
console.log(keyIdForPublicKey)
let optionArgs = [
'--default-key', keyIdForPublicKey,
'--recipient', keyIdForPublicKey,
'--trust-model', 'always' // so we don't get "no assurance this key belongs to the given user"
];
if((argOptions.homedir != undefined) && (argOptions.homedir != null) && argOptions.homedir) {
optionArgs.push('--homedir').push(argOptions.homedir);
}
if(((argOptions.armor != undefined) && argOptions.armor) ||
((argOptions.format != undefined) && argOptions.format.toLowerCase() === 'armor')) {
optionArgs.push('--armor');
}
// Add the default options
optionArgs = optionArgs.concat(defaultArguments);
console.log('please see this',optionArgs)
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`\nCalling GPG with options: \n${JSON.stringify(optionArgs)}\n`);
}
return new Promise((resolve, reject) => {
GPG.encrypt(plainMessage, optionArgs, function(error, encryptedBuffer) {
if(error) {
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`\nError occurred while encrypting request with GPG. Error: \n${error}\n`);
}
reject(error);
}
const outputString = ((argOptions.format != undefined) && argOptions.format.toLowerCase() === 'hex') ?
encryptedBuffer.toString("hex") :
encryptedBuffer.toString();
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`\nSuccessfully Encrypted With GPG: \n\n${outputString}\n`);
}
resolve(outputString);
});
})
};
exports.encryptRequest = _encryptRequest;
/**
* This method is used to decrypt the response that is received from Cgate.
*
* @param {*} encryptedResponse - the encrypted response to decrypt
* @param {*} keyIdForPrivateKey - the Long/Short ID for the imported public key
* @param {*} passphrase - the passphrase associated with the Private key
* @param {*} decryptOptions - options to control how the decryption process flows
*/
const _decryptRequest = async (encryptedResponse, keyIdForPrivateKey, passphrase, decryptOptions = {}) => {
const argOptions = decryptOptions || {
homedir: null,
armor: true,
debug: false,
format: 'binary', // plain, hex, or armor
showVersion: false
};
if(keyIdForPrivateKey == undefined || keyIdForPrivateKey == null) {
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`Error: Key Not Found with Id`);
}
throw new Error(`Error: Key Not Found with Id`);
return;
}
let optionArgs = [
'--skip-verify',
'--ignore-mdc-error',
// '--options /dev/null', // ignore any saved options
//'--pinentry-mode loopback', // Since 2.1.13 we can use "loopback mode" instead of gpg-agent
'--default-key', keyIdForPrivateKey,
'-u', keyIdForPrivateKey,
'--trust-model', 'always', // so we don't get "no assurance this key belongs to the given user"
// 'echo your_password | gpg --batch --yes --passphrase-fd 0',
'--batch',// '--quiet',
'--yes',
// '--passphrase-file <(echo password)',
'--passphrase', passphrase
];
if((argOptions.homedir != undefined) && (argOptions.homedir != null) && argOptions.homedir) {
optionArgs.push('--homedir').push(argOptions.homedir);
}
if(((argOptions.armor != undefined) && argOptions.armor) ||
((argOptions.format != undefined) && argOptions.format.toLowerCase() === 'armor')) {
optionArgs.push('--armor');
}
// Add the default options
optionArgs = optionArgs.concat(defaultArguments);
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`\nCalling GPG with options: \n${JSON.stringify(optionArgs)}\n`);
}
const binaryEncryptedResponse = Buffer.from(encryptedResponse, 'hex').toString('binary');
const armoredBinMessage = enarmor(binaryEncryptedResponse, 'PGP MESSAGE');
// console.log("\n\nThe Armored Message:\n\n", armoredBinMessage, '\n\n')
// return;
// -u ${keyIdForPrivateKey} --batch --passphrase-fd 0`
// const decryptCommand = `echo "password" | gpg --decrypt`;
// shelljs.exec(decryptCommand, function(code, stdout, stderr) {
// console.log('Exit Code: ', code);
// console.log('Program Output: ', stdout);
// console.log('Program stderr: ', stderr);
// });
// return;
return new Promise((resolve, reject)=> {
GPG.decrypt(armoredBinMessage, optionArgs, function(error, decryptedBuffer) {
if(error) {
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`\nError occurred while decrypting response with GPG.\n${error}\n`);
}
reject(error);
}
const outputString = ((argOptions.format != undefined) && argOptions.format.toLowerCase() === 'hex') ?
decryptedBuffer.toString("hex") :
decryptedBuffer.toString();
if((argOptions.debug != undefined) && argOptions.debug) {
console.log(`\nSuccessfully Decrypted With GPG: \n\n${outputString}\n`);
}
resolve(outputString);
});
})
};
exports.decryptRequest = _decryptRequest;
/**
* Utility function to convert Uint8 array to a binary string.
*
* @param {*} u8Array
*/
const convertUint8ArrayToBinaryString = (u8Array) => {
var i, len = u8Array.length, binaryString = "";
for (i=0; i<len; i++) {
binaryString += String.fromCharCode(u8Array[i]);
}
return binaryString;
}
/**
* Utility function to convert from a binary string to an Uint8 array.
*
* @param {*} binaryString
*/
const convertBinaryStringToUint8Array = (binaryString) => {
var i, len = binaryString.length, u8_array = new Uint8Array(len);
for (var i = 0; i < len; i++) {
u8_array[i] = binaryString.charCodeAt(i);
}
return u8_array;
}
/**
* Ord function
* @param {*} str
*/
const ord = (str) => {
return str.charCodeAt(0);
}
/**
* @see http://tools.ietf.org/html/rfc4880#section-6.2
*/
const header = (marker) => {
return '-----BEGIN ' + marker.toUpperCase() + '-----';
}
/**
* @see http://tools.ietf.org/html/rfc4880#section-6.2
*/
const footer = (marker) => {
return '-----END ' + marker.toUpperCase() + '-----';
}
/** This is used to calculate Cyclic Redundancy Checksum on a string
*
* @see http://tools.ietf.org/html/rfc4880#section-6
* @see http://tools.ietf.org/html/rfc4880#section-6.1
*/
const crc24 = (data) => {
crc = 0x00b704ce;
for (i = 0; i < data.length; i += 1) {
crc ^= (ord(data[i]) & 255) << 16;
for (j = 0; j < 8; j += 1) {
crc <<= 1;
if (crc & 0x01000000) {
crc ^= 0x01864cfb;
}
}
}
return crc & 0x00ffffff;
}
/**
* Method to perform word wrap operation on a string
*
* @param {*} word
* @param {*} width
* @param {*} breakChar
* @param {*} shouldCut
*/
const wordwrap = (word, width, breakChar, shouldCut) => {
breakChar = breakChar || 'n';
width = width || 76;
shouldCut = shouldCut || false;
if(!word) {
return word;
}
const wordLength = word.length;
let startIndex = 0;
let wrappedWordLines = [];
while(startIndex < wordLength) {
const acceptableWidth = ((startIndex + width) > wordLength) ? (wordLength - startIndex) : width;
wrappedWordLines.push(word.substr(startIndex, acceptableWidth));
startIndex += width;
}
return wrappedWordLines.join(breakChar);
}
/** Used to ernamor a PGP message
* @see http://tools.ietf.org/html/rfc4880#section-6
* @see http://tools.ietf.org/html/rfc4880#section-6.2
* @see http://tools.ietf.org/html/rfc2045
*/
const enarmor = (data, marker = 'MESSAGE', headers = {}) => {
text = header(marker) + "\n";
const headerKeys = Object.keys(headers);
for (i = 0; i < headerKeys.length; i += 1) {
text += headerKeys[i] + ': ' + headers[headerKeys[i]] + "\n";
}
const base64Data = Buffer.from(data, 'binary').toString('base64');
const packDataToUInt32BE = misc.pack('N', crc24(data)); // pack line here
const base64PackedData = Buffer.from(packDataToUInt32BE.substr(1), 'binary').toString('base64');
text += "\n" + wordwrap(base64Data, 76, "\n", true);
text += "\n" + '=' + base64PackedData + "\n";
text += footer(marker) + "\n";
return text;
}
exports.enarmor = enarmor;