From dc7feaa80af1d00b6999c154bf4185e05b55de0e Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Sun, 15 Sep 2013 19:00:35 +0200 Subject: [PATCH] fix and cleanup encrypted message parsing --- src/js/app-config.js | 7 ++++++- src/js/dao/email-dao.js | 27 ++++++++++++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/js/app-config.js b/src/js/app-config.js index 851f275..d833e86 100644 --- a/src/js/app-config.js +++ b/src/js/app-config.js @@ -34,7 +34,12 @@ define([], function() { * Strings are maintained here */ app.string = { - signature: 'Sent securely from whiteout mail' + subject: '[whiteout] Encrypted message', + message: 'this is a private conversation. To read my encrypted message below, simply install Whiteout Mail for Chrome. The app is really easy to use and automatically encrypts sent emails, so that only the two of us can read them: https://chrome.google.com/webstore/detail/whiteout-mail/jjgghafhamholjigjoghcfcekhkonijg', + cryptPrefix: '-----BEGIN ENCRYPTED MESSAGE-----', + cryptSuffix: '-----END ENCRYPTED MESSAGE-----', + signature: 'Sent securely from whiteout mail', + webSite: 'http://whiteout.io' }; return app; diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index a16ff7d..f5957a5 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -5,13 +5,8 @@ define(function(require) { util = require('cryptoLib/util'), crypto = require('js/crypto/crypto'), jsonDB = require('js/dao/lawnchair-dao'), - devicestorage = require('js/dao/devicestorage-dao'); - - var SUBJECT = '[whiteout] Encrypted message', - MESSAGE = 'this is a private conversation. To read my encrypted message below, simply install Whiteout Mail for Chrome. The app is really easy to use and automatically encrypts sent emails, so that only the two of us can read them: https://chrome.google.com/webstore/detail/whiteout-mail/jjgghafhamholjigjoghcfcekhkonijg\n\n\n', - PREFIX = '-----BEGIN ENCRYPTED MESSAGE-----\n', - SUFFIX = '\n-----END ENCRYPTED MESSAGE-----', - SIGNATURE = '\n\n\nSent securely from whiteout mail\nhttp://whiteout.io\n\n'; + devicestorage = require('js/dao/devicestorage-dao'), + str = require('js/app-config').string; /** * A high-level Data-Access Api for handling Email synchronization @@ -256,6 +251,12 @@ define(function(require) { function frameEncryptedMessage(email, ct) { var to, greeting, ctBase64; + var SUBJECT = str.subject, + MESSAGE = str.message + '\n\n\n', + PREFIX = str.cryptPrefix + '\n', + SUFFIX = '\n' + str.cryptSuffix, + SIGNATURE = '\n\n\n' + str.signature + '\n' + str.webSite + '\n\n'; + // get first name of recipient to = (email.to[0].name || email.to[0].address).split('@')[0].split('.')[0].split(' ')[0]; greeting = 'Hi ' + to + ',\n\n'; @@ -350,7 +351,7 @@ define(function(require) { } // decrypt Message body - if (message.body.indexOf(PREFIX) !== -1 && message.body.indexOf(SUFFIX) !== -1) { + if (message.body.indexOf(str.cryptPrefix) !== -1 && message.body.indexOf(str.cryptSuffix) !== -1) { decryptBody(message, function(err, ptMessage) { message = ptMessage; // return decrypted message @@ -366,12 +367,16 @@ define(function(require) { } function decryptBody(email, callback) { - var ctMessageBase64, ctMessage, pubkeyIds; + var ctMessageBase64, ctMessageJson, ctMessage, pubkeyIds; // parse email body for encrypted message block try { - ctMessageBase64 = email.body.split(PREFIX)[1].split(SUFFIX)[0]; - ctMessage = JSON.parse(atob(ctMessageBase64)); + // get base64 encoded message block + ctMessageBase64 = email.body.split(str.cryptPrefix)[1].split(str.cryptSuffix)[0].trim(); + // decode bae64 + ctMessageJson = atob(ctMessageBase64); + // parse json string to get ciphertext object + ctMessage = JSON.parse(ctMessageJson); } catch (e) { callback({ errMsg: 'Error parsing encrypted message block!'