Skip to content

Commit

Permalink
Fixed missing encryption on recursive messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
diversario committed Jul 17, 2013
1 parent dc2c1ad commit 85b1023
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ coverage:
@mv lcov.info reports
@mv lcov-report reports
@rm -rf lib-cov
@if [ -f "$(which open)" ] && [ $TRAVIS != true ]; then open reports/lcov-report/index.html; fi
@if [ -f "`which open`" ] && [ "${TRAVIS}" != "true" ]; then open reports/lcov-report/index.html; fi

coveralls: test coverage
@cat reports/lcov.info | ./node_modules/coveralls/bin/coveralls.js
Expand Down
60 changes: 20 additions & 40 deletions lib/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var crypto = require('crypto')
, os = require('os')
, assert = require('assert')

, frame = require('./frame')

Expand Down Expand Up @@ -65,7 +66,9 @@ function MessageFactory(parent) {
var header = new Buffer(2)
header.writeUInt16BE(head.metaLength, 0)

return Buffer.concat([header, meta, payload], header.length + meta.length + payload.length)
assert(header.length === 2)

return Buffer.concat([header, meta, payload]/*, header.length + meta.length + payload.length*/)
}


Expand Down Expand Up @@ -112,6 +115,7 @@ function MessageFactory(parent) {

if(this._recursive) {
this._meta.enc = false
if (this.config.encrypt) this._meta.nonce = getNonce()
} else {
this._meta.enc = !!this.config.encrypt
}
Expand Down Expand Up @@ -139,6 +143,7 @@ function MessageFactory(parent) {

if (this.config.encrypt && !this._recursive) {
this._payload = new OutgoingMessage(this.name, this._rawPayload, true).toBuffer()
this._payload = this.encrypt(this._payload)
} else {
this._payload = new Buffer(this._rawPayload ? JSON.stringify(this._rawPayload) : '')
}
Expand All @@ -157,10 +162,7 @@ function MessageFactory(parent) {
OutgoingMessage.prototype.encrypt = function encrypt(str) {
var cipher = crypto.createCipher('aes128', this.config.encrypt.key)

var encrypted = cipher.update(str, 'binary', 'base64')
encrypted += cipher.final('base64')

return encrypted
return Buffer.concat([cipher.update(str), cipher.final()])
}


Expand All @@ -171,12 +173,18 @@ function MessageFactory(parent) {
* @param data
* @constructor
*/
function IncomingMessage(data) {
this.data = data
function IncomingMessage(data, encrypted) {
this.config = parent.config
this._recursive = encrypted

if (encrypted) {
this.data = this.decrypt(data)
} else {
this.data = data
}

if (this.meta().enc) {
return new IncomingMessage(this.rawPayload())
return new IncomingMessage(this.rawPayload(), true)
}
}

Expand Down Expand Up @@ -207,7 +215,7 @@ function MessageFactory(parent) {
if (this._meta) return this._meta

var meta = this.data.slice(frame.META.START, frame.META.START + this.header().metaLength)

this._meta = JSON.parse(meta.toString())

return this._meta
Expand All @@ -222,14 +230,7 @@ function MessageFactory(parent) {
IncomingMessage.prototype.payload = function payload() {
if (this._payload !== undefined) return this._payload

var payload = this.data.slice(frame.HEADER.LENGTH + this.header().metaLength)

if (!payload.length) {
this._payload = null
return this._payload
}

payload = payload.toString()
var payload = this.data.slice(frame.HEADER.LENGTH + this.header().metaLength).toString()

// rawPayload may be an empty string at this point
if (!payload.length) {
Expand Down Expand Up @@ -264,31 +265,10 @@ function MessageFactory(parent) {
IncomingMessage.prototype.decrypt = function decrypt(buf) {
var decipher = crypto.createDecipher('aes128', this.config.encrypt.key)

var decipheredBody = decipher.update(Buffer(buf.toString(), 'base64')) // not sure why I need to recreate a Buffer here
decipheredBody += decipher.final()

return decipheredBody
}


/**
* Adds right padding to the `str` until it's `length` long.
* Optional `char` used for padding defaults to a space character.
*
* @param {*} input Input data. Must have a .toString method
* @param {Number} paddedLength Length of the resulting string
* @param {String} [ch] Character to use for padding. Defaults to a space character.
*
* @returns {String}
*/
function padString(input, paddedLength, ch) {
input = input.toString()

if (!ch) ch = ' '
while (input.length < paddedLength) input += ch
return input
return Buffer.concat([decipher.update(buf), decipher.final()])
}



/**
* Generates a random string and returns its SHA-1 hash.
Expand Down

0 comments on commit 85b1023

Please sign in to comment.