From e1bf0c50498a7161e215669e766a335626b01b5b Mon Sep 17 00:00:00 2001 From: jonathanramirez Date: Fri, 18 Mar 2022 15:32:32 -0600 Subject: [PATCH 1/2] Fix encryption files --- services/jupiterTransactionMessageService.js | 4 +-- services/jupiterTransactionsService.js | 10 +++++-- src/jim/routes/files.js | 15 ++++++++-- src/jim/services/storageService.js | 29 ++++++++++---------- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/services/jupiterTransactionMessageService.js b/services/jupiterTransactionMessageService.js index d347bfd..87db15e 100644 --- a/services/jupiterTransactionMessageService.js +++ b/services/jupiterTransactionMessageService.js @@ -347,7 +347,7 @@ class JupiterTransactionMessageService { logger.warn('???????????????????????????????????????????????\n'); return ''; } - const messageToParse = decryptedMessageContainer.tag.includes(metisConfig.evm) + const messageToParse = decryptedMessageContainer.tag.includes(`.${metisConfig.evm}`) ? crypto.decryptOrNullGCM(decryptedMessageContainer.message) : crypto.decryptOrNull(decryptedMessageContainer.message); if (!messageToParse) { @@ -382,7 +382,7 @@ class JupiterTransactionMessageService { // logger.error('what happens if i try to parse a non JSON String?'); // logger.debug(`decryptedMessage.message= ${decryptedMessage.message}`); // logger.sensitive(`password= ${crypto.decryptionPassword}`); - const messageToParse = messageContainer.tag.includes(metisConfig.evm) + const messageToParse = messageContainer.tag.includes(`.${metisConfig.evm}`) ? crypto.decryptOrPassThroughGCM(messageContainer.message) : crypto.decryptOrPassThrough(messageContainer.message); diff --git a/services/jupiterTransactionsService.js b/services/jupiterTransactionsService.js index bb6d03a..1b227cd 100644 --- a/services/jupiterTransactionsService.js +++ b/services/jupiterTransactionsService.js @@ -9,6 +9,7 @@ const {MetisError} = require("../errors/metisError"); const mError = require("../errors/metisError"); const {validator} = require("./validator"); const {GravityCrypto} = require("./gravityCrypto"); +const {metisConfig} = require("../config/constants"); class JupiterTransactionsService { @@ -129,17 +130,20 @@ class JupiterTransactionsService { * @param transactionId * @param sharedKey * @param isEncrypted + * @param tag * @returns {Object} */ - async getReadableMessageContainersBySharedKey(transactionId, sharedKey, isEncrypted = false){ + async getReadableMessageContainersBySharedKey(transactionId, sharedKey, tag, isEncrypted = false){ console.log(`\n`) logger.verbose(`########################################################################`); logger.verbose(`## getReadableTaggedMessageContainersBySharedKey( transactionId,sharedKey)`); logger.verbose(`########################################################################\n`); if(!gu.isNonEmptyString(transactionId)){throw new MetisError('transactionId is invalid')} if(!gu.isNonEmptyString(sharedKey)){throw new MetisError('sharedKey is invalid')} + if(!gu.isNonEmptyString(tag)){throw new MetisError('tag is invalid')} logger.verbose(`transactionId= ${transactionId}`); logger.verbose(`sharedKey= ${sharedKey}`); + logger.verbose(`tag= ${tag}`); const transaction = await this.jupiterAPIService.getReadableMessageBySharedKey(transactionId, sharedKey); @@ -148,7 +152,9 @@ class JupiterTransactionsService { } const crypto = new GravityCrypto(process.env.ENCRYPT_ALGORITHM, sharedKey); - return crypto.decryptAndParseOrNull(transaction.decryptedMessage); + return tag.includes(`.${metisConfig.evm}`) + ? crypto.decryptAndParseOrNullGCM(transaction.decryptedMessage) + : crypto.decryptAndParseOrNull(transaction.decryptedMessage); } diff --git a/src/jim/routes/files.js b/src/jim/routes/files.js index 82b0283..5a8c8ed 100644 --- a/src/jim/routes/files.js +++ b/src/jim/routes/files.js @@ -4,7 +4,7 @@ import {storageService} from "../services/storageService"; import {localFileCacheService} from "../services/localFileCacheService"; import {chanService} from "../../../services/chanService"; import {jupiterAPIService} from "../../../services/jupiterAPIService"; -import {userConfig} from "../../../config/constants"; +import {metisConfig, userConfig} from "../../../config/constants"; import {FeeManager, feeManagerSingleton} from "../../../services/FeeManager"; import {jupiterTransactionsService} from "../../../services/jupiterTransactionsService"; import {GravityCrypto} from "../../../services/gravityCrypto"; @@ -318,9 +318,18 @@ module.exports = (app, jobs, websocket) => { if(!messageContainers){ return res.status(StatusCode.ClientErrorNotFound).send({message: 'No image found'}); } - const [fileUuid, transactionId, sharedKey] = messageContainers.attachment.message.split('.').slice(-3); + const messageContainerTag = messageContainers.attachment.message; + let fileUuid = ''; + let transactionId = ''; + let sharedKey = ''; + let tag = ''; + if(messageContainerTag.includes(`.${metisConfig.evm}`)){ + [fileUuid, transactionId, sharedKey, tag] = messageContainers.attachment.message.split('.').slice(-4); + } else { + [fileUuid, transactionId, sharedKey] = messageContainers.attachment.message.split('.').slice(-3); + } - const fileInfo = await storageService.fetchFileInfoBySharedKey(transactionId, sharedKey, fileUuid); + const fileInfo = await storageService.fetchFileInfoBySharedKey(transactionId, sharedKey, fileUuid, messageContainerTag); res.setHeader('Content-Type', `${fileInfo.mimeType}`); res.setHeader('Content-Disposition', `inline; filename="${fileInfo.fileName}"`); res.sendFile(fileInfo.bufferDataPath); diff --git a/src/jim/services/storageService.js b/src/jim/services/storageService.js index 84e9114..6ed0d09 100644 --- a/src/jim/services/storageService.js +++ b/src/jim/services/storageService.js @@ -2,6 +2,7 @@ import mError from "../../../errors/metisError"; import {chanService} from "../../../services/chanService"; import {localFileCacheService} from "./localFileCacheService"; import {GravityCrypto} from "../../../services/gravityCrypto"; +import {metisConfig} from "../../../config/constants"; const logger = require('../../../utils/logger')(module); const gu = require('../../../utils/gravityUtils'); const {GravityAccountProperties} = require("../../../gravity/gravityAccountProperties"); @@ -339,7 +340,7 @@ class StorageService { logger.info(` GETTING FILE FROM CACHE`); logger.info(`-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__--\n`); const encryptedFileRecord = this.fileCacheService.getFileRecord(fileUuid); - fileRecord = ownerAccountProperties.crypto.decryptAndParse(encryptedFileRecord); + fileRecord = ownerAccountProperties.crypto.decryptAndParseGCM(encryptedFileRecord); } else { // GETTING FILE FROM BLOCKCHAIN console.log(`\n`); @@ -349,7 +350,7 @@ class StorageService { const fetchFileFromBlockChainResponse = await this.fetchFileFromBlockChain(ownerAccountProperties,fileUuid); bufferData = fetchFileFromBlockChainResponse.bufferData; fileRecord = fetchFileFromBlockChainResponse.fileRecord; - const encryptedFileRecord = ownerAccountProperties.crypto.encryptJson(fileRecord); + const encryptedFileRecord = ownerAccountProperties.crypto.encryptJsonGCM(fileRecord); this.fileCacheService.sendBufferDataToCache(fileUuid,bufferData); this.fileCacheService.sendFileRecordToCache(fileUuid,encryptedFileRecord); } @@ -432,11 +433,12 @@ class StorageService { } - async fetchFileInfoBySharedKey(transactionId, sharedKey, fileUuid){ + async fetchFileInfoBySharedKey(transactionId, sharedKey, fileUuid, tag){ logger.verbose(`#### fetchFileInfoBySharedKey(transactionId, sharedKey, fileUuid)`); if(!gu.isNonEmptyString(transactionId)) throw new mError.MetisErrorBadUuid(`transactionId: ${transactionId}`); if(!gu.isNonEmptyString(sharedKey)) throw new mError.MetisErrorBadUuid(`transactionId: ${sharedKey}`); if(!gu.isWellFormedUuid(fileUuid)) throw new mError.MetisErrorBadUuid(`fileUuid: ${fileUuid}`); + if(!tag) throw new mError.MetisErrorBadUuid(`tag: ${tag}`); let bufferData = null; let fileRecord = null; try { @@ -448,18 +450,17 @@ class StorageService { logger.info(` GETTING FILE FROM CACHE`); logger.info(`-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__--\n`); const encryptedFileRecord = this.fileCacheService.getFileRecord(fileUuid); - fileRecord = crypto.decryptAndParse(encryptedFileRecord); + fileRecord = tag.includes(`.${metisConfig.evm}`) ? crypto.decryptAndParseGCM(encryptedFileRecord) : crypto.decryptAndParse(encryptedFileRecord); } else { // GETTING FILE FROM BLOCKCHAIN console.log(`\n`); logger.info(`-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__--`); logger.info(` GETTING FILE FROM BLOCKCHAIN`); logger.info(`-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__--\n`); - const fetchFileFromBlockChainResponse = await this.fetchFileFromBlockChainBySharedKey(transactionId, sharedKey); + const fetchFileFromBlockChainResponse = await this.fetchFileFromBlockChainBySharedKey(transactionId, sharedKey, tag); bufferData = fetchFileFromBlockChainResponse.bufferData; fileRecord = fetchFileFromBlockChainResponse.fileRecord; - - const encryptedFileRecord = crypto.encryptJson(fileRecord); + const encryptedFileRecord = tag.includes(`.${metisConfig.evm}`) ? crypto.decryptAndParseGCM(fileRecord) : crypto.decryptAndParse(fileRecord); this.fileCacheService.sendBufferDataToCache(fileUuid,bufferData); this.fileCacheService.sendFileRecordToCache(fileUuid,encryptedFileRecord); } @@ -490,12 +491,12 @@ class StorageService { * @param transactionId * @param sharedKey */ - async fetchFileFromBlockChainBySharedKey(transactionId, sharedKey){ + async fetchFileFromBlockChainBySharedKey(transactionId, sharedKey, tag){ logger.verbose(`#### fetchFileFromBlockChainBySharedKey()`); if(!gu.isNonEmptyString(transactionId)) throw new mError.MetisErrorBadUuid(`transactionId is missing`); if(!gu.isNonEmptyString(sharedKey)) throw new mError.MetisErrorBadUuid(`sharedKey is missing`); try { - const fileRecord = await jupiterTransactionsService.getReadableMessageContainersBySharedKey(transactionId, sharedKey); + const fileRecord = await jupiterTransactionsService.getReadableMessageContainersBySharedKey(transactionId, sharedKey, tag); const chunkTransactionIds = fileRecord.chunkTransactionIds; // GET ALL THE CHUNKS @@ -504,7 +505,7 @@ class StorageService { logger.info(` GET ALL THE CHUNKS`); logger.info(`-__-__-__-__-__-__-__-__-__-__-__-__-__-__-__--\n`); const readableMessageContainer$ = chunkTransactionIds.map(chunkTransactionId => - jupiterTransactionsService.getReadableMessageContainersBySharedKey(chunkTransactionId.transactionId, chunkTransactionId.sharedKey) + jupiterTransactionsService.getReadableMessageContainersBySharedKey(chunkTransactionId.transactionId, chunkTransactionId.sharedKey, tag) ); const chunkContainers = await Promise.all(readableMessageContainer$); if(chunkContainers.length < 1) throw new mError.MetisErrorNoBinaryFileFound(`No Chunks found`); @@ -632,7 +633,7 @@ class StorageService { const _fileRecord = (fileCat === FileCategory.PublicProfile || fileCat === FileCategory.ChannelProfile) ? JSON.stringify(fileRecord): - toAccountProperties.crypto.encryptJson(fileRecord) + toAccountProperties.crypto.encryptJsonGCM(fileRecord) if(!this.fileCacheService.bufferDataExists(fileUuid)){ this.fileCacheService.sendBufferDataToCache(fileUuid,bufferData); } @@ -651,20 +652,20 @@ class StorageService { if(fileCat === FileCategory.PublicProfile || fileCat === FileCategory.ChannelProfile){ const crypto = new GravityCrypto(process.env.ENCRYPT_ALGORITHM, xSharedKey); - const encryptedFileRecord = crypto.encryptJson(fileRecord); + const encryptedFileRecord = crypto.encryptJsonGCM(fileRecord); this.fileCacheService.sendFileRecordToCache(fileUuid, encryptedFileRecord); const sendMessageResponsePublicFileSharedKey = await this.jupiterTransactionsService.messageService.sendTaggedAndEncipheredMetisMessage( toAccountProperties.passphrase, toAccountProperties.address, _fileRecord, - `${transactionTags.jimServerTags.binaryFilePublicProfileSharedKey}.${fileUuid}.${xInfo.transactionId}.${xSharedKey}`, + `${transactionTags.jimServerTags.binaryFilePublicProfileSharedKey}.${fileUuid}.${xInfo.transactionId}.${xSharedKey}.${metisConfig.evm}`, FeeManager.feeTypes.metisMessage, toAccountProperties.publicKey ); return {fileRecord: encryptedFileRecord, sharedKey: xSharedKey} } - const encryptedFileRecord = toAccountProperties.crypto.encryptJson(fileRecord); + const encryptedFileRecord = toAccountProperties.crypto.encryptJsonGCM(fileRecord); this.fileCacheService.sendFileRecordToCache(fileUuid, encryptedFileRecord); return {fileRecord: _fileRecord} From e160ccd9e466745eabfed41c92f4a05fded28f45 Mon Sep 17 00:00:00 2001 From: jonathanramirez Date: Fri, 18 Mar 2022 16:05:38 -0600 Subject: [PATCH 2/2] Fix encryption files --- services/gravityCrypto.js | 2 +- src/jim/services/localFileCacheService.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/services/gravityCrypto.js b/services/gravityCrypto.js index da60497..6736130 100644 --- a/services/gravityCrypto.js +++ b/services/gravityCrypto.js @@ -246,7 +246,7 @@ class GravityCrypto { */ decryptAndParseGCM(data){ const decryptedValue = this.decryptOrPassThroughGCM(data); - return JSON.parse(decryptedValue); + return typeof decryptedValue === 'object' ? decryptedValue : JSON.parse(decryptedValue); } diff --git a/src/jim/services/localFileCacheService.js b/src/jim/services/localFileCacheService.js index a7082b2..09d421d 100644 --- a/src/jim/services/localFileCacheService.js +++ b/src/jim/services/localFileCacheService.js @@ -94,7 +94,8 @@ class LocalFileCacheService { if(!gu.isWellFormedUuid(fileUuid)) throw new mError.MetisErrorBadUuid(`fileUuid=${fileUuid}`); if(!encryptedFileRecord) throw new mError.MetisError(`encryptedFileRecord is empty!`); const fileRecordPath = this.generateFileRecordPath(fileUuid); - fs.writeFileSync(fileRecordPath, encryptedFileRecord); + const data = typeof encryptedFileRecord === 'string' ? encryptedFileRecord : JSON.stringify(encryptedFileRecord); + fs.writeFileSync(fileRecordPath, data); } /**