Skip to content

Move CRX2 logic to separate file #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 4 additions & 55 deletions src/crx.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var crypto = require("crypto");
var RSA = require("node-rsa");
var archiver = require("archiver");
var resolve = require("./resolver.js");
var crx2 = require("./crx2.js");
var crx3 = require("./crx3.js");

const DEFAULTS = {
Expand Down Expand Up @@ -56,13 +57,11 @@ class ChromeExtension {

selfie.publicKey = publicKey;

if (selfie.version === 3) {
return crx3(selfie.privateKey, publicKey, contents);
if (selfie.version === 2) {
return crx2(selfie.privateKey, publicKey, contents);
}

var signature = selfie.generateSignature(contents);

return selfie.generatePackage(signature, publicKey, contents);
return crx3(selfie.privateKey, publicKey, contents);
});
}

Expand All @@ -89,7 +88,6 @@ class ChromeExtension {
});
}


/**
* Generates a public key.
*
Expand Down Expand Up @@ -119,24 +117,6 @@ class ChromeExtension {
});
}

/**
* Generates a SHA1 package signature.
*
* BC BREAK `this.signature` is not stored anymore (since 1.0.0)
*
* @param {Buffer} contents
* @returns {Buffer}
*/
generateSignature (contents) {
return Buffer.from(
crypto
.createSign("sha1")
.update(contents)
.sign(this.privateKey),
"binary"
);
}

/**
*
* BC BREAK `this.contents` is not stored anymore (since 1.0.0)
Expand Down Expand Up @@ -183,37 +163,6 @@ class ChromeExtension {
});
}

/**
* Generates and returns a signed package from extension content.
*
* BC BREAK `this.package` is not stored anymore (since 1.0.0)
*
* @param {Buffer} signature
* @param {Buffer} publicKey
* @param {Buffer} contents
* @returns {Buffer}
*/
generatePackage (signature, publicKey, contents) {
var keyLength = publicKey.length;
var sigLength = signature.length;
var zipLength = contents.length;
var length = 16 + keyLength + sigLength + zipLength;

var crx = Buffer.alloc(length);

crx.write("Cr24" + new Array(13).join("\x00"), "binary");

crx[4] = 2;
crx.writeUInt32LE(keyLength, 8);
crx.writeUInt32LE(sigLength, 12);

publicKey.copy(crx, 16);
signature.copy(crx, 16 + keyLength);
contents.copy(crx, 16 + keyLength + sigLength);

return crx;
}

/**
* Generates an appId from the publicKey.
* Public key has to be set for this to work, otherwise an error is thrown.
Expand Down
55 changes: 55 additions & 0 deletions src/crx2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

var crypto = require("crypto");

/**
* Generates and returns a signed package from extension content.
*
* BC BREAK `this.package` is not stored anymore (since 1.0.0)
*
* @param {Buffer} signature
* @param {Buffer} publicKey
* @param {Buffer} contents
* @returns {Buffer}
*/
module.exports = function generatePackage (privateKey, publicKey, contents) {
var signature = generateSignature(privateKey, contents);

var keyLength = publicKey.length;
var sigLength = signature.length;
var zipLength = contents.length;
var length = 16 + keyLength + sigLength + zipLength;

var crx = Buffer.alloc(length);

crx.write("Cr24" + new Array(13).join("\x00"), "binary");

crx[4] = 2;
crx.writeUInt32LE(keyLength, 8);
crx.writeUInt32LE(sigLength, 12);

publicKey.copy(crx, 16);
signature.copy(crx, 16 + keyLength);
contents.copy(crx, 16 + keyLength + sigLength);

return crx;
};

/**
* Generates a SHA1 package signature.
*
* BC BREAK `this.signature` is not stored anymore (since 1.0.0)
*
* @param {Buffer} privateKey
* @param {Buffer} contents
* @returns {Buffer}
*/
function generateSignature (privateKey, contents) {
return Buffer.from(
crypto
.createSign("sha1")
.update(contents)
.sign(privateKey),
"binary"
);
}