diff --git a/js/cryptoLib.js b/js/cryptoLib.js index d5285cf8..15a41105 100644 --- a/js/cryptoLib.js +++ b/js/cryptoLib.js @@ -10,7 +10,6 @@ const logLevels = require('./enums/logLevels.js'); const { isMac, isDevEnv } = require('../js/utils/misc'); const TAG_LENGTH = 16; -const KEY_LENGTH = 32; const arch = process.arch === 'ia32'; const winLibraryPath = isDevEnv ? path.join(__dirname, '..', 'library') : path.join(execPath, 'library'); const macLibraryPath = isDevEnv ? path.join(__dirname, '..', 'library') : path.join(execPath, '..', 'library'); @@ -19,12 +18,6 @@ const cryptoLibPath = isMac ? path.join(macLibraryPath, 'cryptoLib.dylib') : (arch ? path.join(winLibraryPath, 'libsymphonysearch-x86.dll') : path.join(winLibraryPath, 'libsymphonysearch-x64.dll')); -const voidPtr = ref.refType(ref.types.void); -const RSAKeyPair = exports.RSAKeyPair = voidPtr; -const RSAKeyPairPtr = exports.RSAKeyPairPtr = ref.refType(RSAKeyPair); -const RSAPubKey = exports.RSAPubKey = voidPtr; -const RSAPubKeyPtr = exports.RSAPubKeyPtr = ref.refType(RSAPubKey); - const library = new ffi.Library((cryptoLibPath), { AESEncryptGCM: [ref.types.int32, [ @@ -53,37 +46,6 @@ const library = new ffi.Library((cryptoLibPath), { ref.refType(ref.types.uchar), ]], - encryptRSA: [ref.types.uint32, [ - RSAPubKeyPtr, - ref.types.int32, - ref.refType(ref.types.uchar), - ref.types.uint32, - ref.refType(ref.types.uchar), - ref.types.uint32, - ]], - - decryptRSA: [ref.types.uint32, [ - RSAPubKeyPtr, - ref.types.int32, - ref.refType(ref.types.uchar), - ref.types.uint32, - ref.refType(ref.types.uchar), - ref.types.uint32, - ]], - - deserializeRSAPubKey: [RSAPubKey, [ - ref.refType(ref.types.uchar), - ref.types.uint32, - ]], - deserializeRSAKeyPair: [RSAKeyPairPtr, [ - ref.refType(ref.types.uchar), - ref.types.uint32, - ]], - - getRSAKeySize: [ref.types.uint32, [ - RSAKeyPairPtr - ]], - getVersion: [ref.types.CString, []], }); @@ -166,82 +128,7 @@ const EncryptDecrypt = function (name, Base64IV, Base64AAD, Base64Key, Base64In) return null; }; -/** - * Decrypt RSA - * @param pemKey - * @param input - * @return {*} - * @constructor - */ -const RSADecrypt = function (pemKey, input) { - return RSAEncryptDecrypt("RSADecrypt", pemKey, input); -}; - -/** - * Encrypt / Decrypt RSA - * @param action - * @param pemKey - * @param inputStr - * @return {String} - * @constructor - */ -const RSAEncryptDecrypt = function (action, pemKey, inputStr) { - - let rsaKey = getRSAKeyFromPEM(pemKey); - - if (!rsaKey) { - log.send(logLevels.ERROR, `Failed to parse formatted RSA PEM key`); - } - - let input = Buffer.from(inputStr, 'base64'); - let outLen = library.getRSAKeySize(rsaKey); - - let outPtr = Buffer.alloc(KEY_LENGTH); - - let ret = 0; - - if (action === 'RSAEncrypt') { - ret = library.encryptRSA(rsaKey, 0, input, input.length, outPtr, outLen); - } else { - outLen = library.decryptRSA(rsaKey, 0, input, input.length, outPtr, outLen); - - if (outLen < 0) { - ret = outLen; - } - } - - if (ret !== 0) { - log.send(logLevels.ERROR, `${action} failed due to -> ${ret}`); - } - return Buffer.from(outPtr.toString('hex'), 'hex').toString('base64'); -}; - -/** - * Get RSA key from PEM key - * @param pemKey - * @return {*} - */ -const getRSAKeyFromPEM = function (pemKey) { - - let pemKeyBytes = Buffer.from(pemKey, 'utf-8'); - - let rsaKey; - - if (pemKey.startsWith("-----BEGIN PUBLIC KEY-----")) { - rsaKey = library.deserializeRSAPubKey(pemKeyBytes, pemKey.length); - } else { - rsaKey = library.deserializeRSAKeyPair(pemKeyBytes, pemKey.length); - } - - if (rsaKey === 0) { - log.send(logLevels.ERROR, 'RSAKey is 0!!'); - } - return rsaKey; -}; - - module.exports = { AESGCMEncrypt: AESGCMEncrypt, AESGCMDecrypt: AESGCMDecrypt, - RSADecrypt: RSADecrypt, };