Fix attachments

This commit is contained in:
Matt Corallo 2014-05-15 01:02:15 -04:00
parent 3103eaa192
commit 07a23f0759
3 changed files with 19 additions and 7 deletions

View file

@ -192,16 +192,15 @@ var API = new function() {
urlParameters : '/' + id, urlParameters : '/' + id,
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
console.log(response);
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
$.ajax(response.location, { $.ajax(response.location, {
type : "GET", type : "GET",
xhrFields: { xhrFields: {
responseType: "arraybuffer" responseType: "arraybuffer"
}, },
/*headers: { headers: {
"Content-Type": "application/octet-stream" "Content-Type": "application/octet-stream"
},*/ },
success : function(response, textStatus, jqXHR) { success : function(response, textStatus, jqXHR) {
resolve(response); resolve(response);

View file

@ -283,6 +283,19 @@ window.crypto = (function() {
}); });
} }
var calculateMAC = function(data, key) {
return HmacSHA256(key, data);
}
var verifyMAC = function(data, key, mac) {
return calculateMAC(data, key).then(function(calculated_mac) {
var macString = getString(mac);//TODO: Move away from strings for comparison?
if (getString(calculated_mac).substring(0, macString.length) != macString)
throw new Error("Bad MAC");
});
}
/****************************** /******************************
*** Ratchet implementation *** *** Ratchet implementation ***
******************************/ ******************************/
@ -547,8 +560,8 @@ window.crypto = (function() {
}; };
crypto.decryptAttachment = function(encryptedBin, keys) { crypto.decryptAttachment = function(encryptedBin, keys) {
var aes_key = key.slice(0, 32); var aes_key = keys.slice(0, 32);
var mac_key = key.slice(32, 64); var mac_key = keys.slice(32, 64);
var iv = encryptedBin.slice(0, 32); var iv = encryptedBin.slice(0, 32);
var ciphertext = encryptedBin.slice(32, encryptedBin.byteLength - 32); var ciphertext = encryptedBin.slice(32, encryptedBin.byteLength - 32);

View file

@ -321,7 +321,7 @@ function storeMessage(messageObject) {
messageMap[messageObject.pushMessage.source] = conversation; messageMap[messageObject.pushMessage.source] = conversation;
} }
conversation[conversation.length] = { message: getString(messageObject.message.body), conversation[conversation.length] = { message: messageObject.message.body != null && getString(messageObject.message.body),
sender: messageObject.pushMessage.source, sender: messageObject.pushMessage.source,
timestamp: messageObject.pushMessage.timestamp.div(dcodeIO.Long.fromNumber(1000)).toNumber() }; timestamp: messageObject.pushMessage.timestamp.div(dcodeIO.Long.fromNumber(1000)).toNumber() };
storage.putEncrypted("messageMap", messageMap); storage.putEncrypted("messageMap", messageMap);
@ -465,7 +465,7 @@ function subscribeToPush(message_callback) {
return crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) { return crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
var handleAttachment = function(attachment) { var handleAttachment = function(attachment) {
return API.getAttachment(attachment.id).then(function(encryptedBin) { return API.getAttachment(attachment.id).then(function(encryptedBin) {
return crypto.decryptAttachment(encryptedBin, attachment.key).then(function(decryptedBin) { return crypto.decryptAttachment(encryptedBin, toArrayBuffer(attachment.key)).then(function(decryptedBin) {
attachment.decrypted = decryptedBin; attachment.decrypted = decryptedBin;
}); });
}); });