Skip to content

Commit

Permalink
Allow dead code elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
twiss committed Feb 9, 2021
1 parent c20b752 commit fb3d234
Show file tree
Hide file tree
Showing 44 changed files with 682 additions and 600 deletions.
29 changes: 13 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@
"jsdoc": "github:openpgpjs/jsdoc#0f1816eb4553856647b4ca9561b9307b11ec4f9e",
"mocha": "^5.0.0",
"nyc": "^14.1.1",
"pako": "^1.0.6",
"pako": "github:openpgpjs/pako#f38f7368a5fa511e54b95add2f04444c3a9d803f",
"rollup": "^2.7.2",
"rollup-plugin-terser": "^5.3.0",
"seek-bzip": "github:openpgpjs/seek-bzip#6187fc025851d35c4e104a25ea15a10b9b8d6f7d",
"seek-bzip": "github:openpgpjs/seek-bzip#4b89457f20c0e1921b4689106a31c99782c29829",
"sinon": "^4.3.0",
"text-encoding-utf-8": "^1.0.2",
"tweetnacl": "github:openpgpjs/tweetnacl-js#3dae25bd3eaa77173f3405676b595721dde92eec",
Expand Down
4 changes: 4 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const terserOptions = {
ecma: 2017,
compress: {
unsafe: true
},
output: {
comments: '/^(?:!|#__)/',
preserve_annotations: true
}
};

Expand Down
14 changes: 7 additions & 7 deletions src/cleartext.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import armor from './encoding/armor';
import enums from './enums';
import util from './util';
import packet from './packet';
import { PacketList, LiteralDataPacket, SignaturePacket } from './packet';
import { Signature } from './signature';
import { createVerificationObjects, createSignaturePackets } from './message';

Expand All @@ -47,7 +47,7 @@ export function CleartextMessage(text, signature) {
if (signature && !(signature instanceof Signature)) {
throw new Error('Invalid signature input');
}
this.signature = signature || new Signature(new packet.List());
this.signature = signature || new Signature(new PacketList());
}

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ CleartextMessage.prototype.sign = async function(privateKeys, signature = null,
* @async
*/
CleartextMessage.prototype.signDetached = async function(privateKeys, signature = null, date = new Date(), userIds = []) {
const literalDataPacket = new packet.Literal();
const literalDataPacket = new LiteralDataPacket();
literalDataPacket.setText(this.text);

return new Signature(await createSignaturePackets(literalDataPacket, privateKeys, signature, date, userIds, true));
Expand All @@ -112,7 +112,7 @@ CleartextMessage.prototype.verify = function(keys, date = new Date()) {
*/
CleartextMessage.prototype.verifyDetached = function(signature, keys, date = new Date()) {
const signatureList = signature.packets;
const literalDataPacket = new packet.Literal();
const literalDataPacket = new LiteralDataPacket();
// we assume that cleartext signature is generated based on UTF8 cleartext
literalDataPacket.setText(this.text);
return createVerificationObjects(signatureList, [literalDataPacket], keys, date, true);
Expand Down Expand Up @@ -157,8 +157,8 @@ export async function readArmored(armoredText) {
if (input.type !== enums.armor.signed) {
throw new Error('No cleartext signed message.');
}
const packetlist = new packet.List();
await packetlist.read(input.data);
const packetlist = new PacketList();
await packetlist.read(input.data, { SignaturePacket });
verifyHeaders(input.headers, packetlist);
const signature = new Signature(packetlist);
return new CleartextMessage(input.text, signature);
Expand All @@ -167,7 +167,7 @@ export async function readArmored(armoredText) {
/**
* Compare hash algorithm specified in the armor header with signatures
* @param {Array<String>} headers Armor headers
* @param {module:packet.List} packetlist The packetlist with signature packets
* @param {PacketList} packetlist The packetlist with signature packets
* @private
*/
function verifyHeaders(headers, packetlist) {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/pkcs1.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @fileoverview Provides EME-PKCS1-v1_5 encoding and decoding and EMSA-PKCS1-v1_5 encoding function
* @see module:crypto/public_key/rsa
* @see module:crypto/public_key/elliptic/ecdh
* @see module:packet.PublicKeyEncryptedSessionKey
* @see PublicKeyEncryptedSessionKeyPacket
* @requires crypto/random
* @requires crypto/hash
* @requires util
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/pkcs5.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* @fileoverview Functions to add and remove PKCS5 padding
* @see module:packet.PublicKeyEncryptedSessionKey
* @see PublicKeyEncryptedSessionKeyPacket
* @module crypto/pkcs5
*/

Expand Down
12 changes: 6 additions & 6 deletions src/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,17 @@ export default {
secretKey: 5,
publicKey: 6,
secretSubkey: 7,
compressed: 8,
symmetricallyEncrypted: 9,
compressedData: 8,
symmetricallyEncryptedData: 9,
marker: 10,
literal: 11,
literalData: 11,
trust: 12,
userid: 13,
userID: 13,
publicSubkey: 14,
userAttribute: 17,
symEncryptedIntegrityProtected: 18,
symEncryptedIntegrityProtectedData: 18,
modificationDetectionCode: 19,
symEncryptedAEADProtected: 20 // see IETF draft: https://tools.ietf.org/html/draft-ford-openpgp-format-00#section-2.1
symEncryptedAEADProtectedData: 20 // see IETF draft: https://tools.ietf.org/html/draft-ford-openpgp-format-00#section-2.1
},

/** Data types in the literal packet
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export { default as util } from './util';
* @see module:packet
* @name module:openpgp.packet
*/
export { default as packet } from './packet';
export * from './packet';

/**
* @see module:type/mpi
Expand Down
16 changes: 8 additions & 8 deletions src/key/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* @module key/factory
*/

import packet from '../packet';
import { PacketList, UserIDPacket, SignaturePacket } from '../packet';
import Key from './key';
import * as helper from './helper';
import enums from '../enums';
Expand Down Expand Up @@ -147,7 +147,7 @@ async function wrapKeyObject(secretKeyPacket, secretSubkeyPackets, options) {
}
}));

const packetlist = new packet.List();
const packetlist = new PacketList();

packetlist.push(secretKeyPacket);

Expand All @@ -165,13 +165,13 @@ async function wrapKeyObject(secretKeyPacket, secretSubkeyPackets, options) {
return algos;
}

const userIdPacket = new packet.Userid();
const userIdPacket = new UserIDPacket();
userIdPacket.format(userId);

const dataToSign = {};
dataToSign.userId = userIdPacket;
dataToSign.key = secretKeyPacket;
const signaturePacket = new packet.Signature(options.date);
const signaturePacket = new SignaturePacket(options.date);
signaturePacket.signatureType = enums.signature.certGeneric;
signaturePacket.publicKeyAlgorithm = secretKeyPacket.algorithm;
signaturePacket.hashAlgorithm = await helper.getPreferredHashAlgo(null, secretKeyPacket);
Expand Down Expand Up @@ -270,8 +270,8 @@ async function wrapKeyObject(secretKeyPacket, secretSubkeyPackets, options) {
* @static
*/
export async function read(data) {
const packetlist = new packet.List();
await packetlist.read(data);
const packetlist = new PacketList();
await packetlist.read(data, helper.allowedKeyPackets);
return new Key(packetlist);
}

Expand Down Expand Up @@ -299,8 +299,8 @@ export async function readArmored(armoredKey) {
*/
export async function readAll(data) {
const keys = [];
const packetlist = new packet.List();
await packetlist.read(data);
const packetlist = new PacketList();
await packetlist.read(data, helper.allowedKeyPackets);
const keyIndex = packetlist.indexOfTag(enums.packet.publicKey, enums.packet.secretKey);
if (keyIndex.length === 0) {
throw new Error('No key packet found');
Expand Down
68 changes: 43 additions & 25 deletions src/key/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,40 @@
* @module key/helper
*/

import packet from '../packet';
import {
PublicKeyPacket,
PublicSubkeyPacket,
SecretKeyPacket,
SecretSubkeyPacket,
UserIDPacket,
UserAttributePacket,
SignaturePacket
} from '../packet';
import enums from '../enums';
import config from '../config';
import crypto from '../crypto';
import util from '../util';

export const allowedKeyPackets = {
PublicKeyPacket,
PublicSubkeyPacket,
SecretKeyPacket,
SecretSubkeyPacket,
UserIDPacket,
UserAttributePacket,
SignaturePacket
};

export async function generateSecretSubkey(options) {
const secretSubkeyPacket = new packet.SecretSubkey(options.date);
const secretSubkeyPacket = new SecretSubkeyPacket(options.date);
secretSubkeyPacket.packets = null;
secretSubkeyPacket.algorithm = enums.read(enums.publicKey, options.algorithm);
await secretSubkeyPacket.generate(options.rsaBits, options.curve);
return secretSubkeyPacket;
}

export async function generateSecretKey(options) {
const secretKeyPacket = new packet.SecretKey(options.date);
const secretKeyPacket = new SecretKeyPacket(options.date);
secretKeyPacket.packets = null;
secretKeyPacket.algorithm = enums.read(enums.publicKey, options.algorithm);
await secretKeyPacket.generate(options.rsaBits, options.curve);
Expand All @@ -31,9 +49,9 @@ export async function generateSecretKey(options) {

/**
* Returns the valid and non-expired signature that has the latest creation date, while ignoring signatures created in the future.
* @param {Array<module:packet.Signature>} signatures List of signatures
* @param {Array<SignaturePacket>} signatures List of signatures
* @param {Date} date Use the given date instead of the current time
* @returns {Promise<module:packet.Signature>} The latest valid signature
* @returns {Promise<SignaturePacket>} The latest valid signature
* @async
*/
export async function getLatestValidSignature(signatures, primaryKey, signatureType, dataToVerify, date = new Date()) {
Expand Down Expand Up @@ -76,15 +94,15 @@ export function isDataExpired(keyPacket, signature, date = new Date()) {

/**
* Create Binding signature to the key according to the {@link https://tools.ietf.org/html/rfc4880#section-5.2.1}
* @param {module:packet.SecretSubkey} subkey Subkey key packet
* @param {module:packet.SecretKey} primaryKey Primary key packet
* @param {SecretSubkeyPacket} subkey Subkey key packet
* @param {SecretKeyPacket} primaryKey Primary key packet
* @param {Object} options
*/
export async function createBindingSignature(subkey, primaryKey, options) {
const dataToSign = {};
dataToSign.key = primaryKey;
dataToSign.bind = subkey;
const subkeySignaturePacket = new packet.Signature(options.date);
const subkeySignaturePacket = new SignaturePacket(options.date);
subkeySignaturePacket.signatureType = enums.signature.subkeyBinding;
subkeySignaturePacket.publicKeyAlgorithm = primaryKey.algorithm;
subkeySignaturePacket.hashAlgorithm = await getPreferredHashAlgo(null, subkey);
Expand All @@ -107,7 +125,7 @@ export async function createBindingSignature(subkey, primaryKey, options) {
/**
* Returns the preferred signature hash algorithm of a key
* @param {module:key.Key} key (optional) the key to get preferences from
* @param {module:packet.SecretKey|module:packet.SecretSubkey} keyPacket key packet used for signing
* @param {SecretKeyPacket|SecretSubkeyPacket} keyPacket key packet used for signing
* @param {Date} date (optional) use the given date for verification instead of the current time
* @param {Object} userId (optional) user ID
* @returns {Promise<String>}
Expand All @@ -125,10 +143,10 @@ export async function getPreferredHashAlgo(key, keyPacket, date = new Date(), us
}
}
switch (Object.getPrototypeOf(keyPacket)) {
case packet.SecretKey.prototype:
case packet.PublicKey.prototype:
case packet.SecretSubkey.prototype:
case packet.PublicSubkey.prototype:
case SecretKeyPacket.prototype:
case PublicKeyPacket.prototype:
case SecretSubkeyPacket.prototype:
case PublicSubkeyPacket.prototype:
switch (keyPacket.algorithm) {
case 'ecdh':
case 'ecdsa':
Expand Down Expand Up @@ -182,8 +200,8 @@ export async function getPreferredAlgo(type, keys, date = new Date(), userIds =
/**
* Create signature packet
* @param {Object} dataToSign Contains packets to be signed
* @param {module:packet.SecretKey|
* module:packet.SecretSubkey} signingKeyPacket secret key packet for signing
* @param {SecretKeyPacket|
* SecretSubkeyPacket} signingKeyPacket secret key packet for signing
* @param {Object} signatureProperties (optional) properties to write on the signature packet before signing
* @param {Date} date (optional) override the creationtime of the signature
* @param {Object} userId (optional) user ID
Expand All @@ -195,7 +213,7 @@ export async function createSignaturePacket(dataToSign, privateKey, signingKeyPa
if (!signingKeyPacket.isDecrypted()) {
throw new Error('Private key is not decrypted.');
}
const signaturePacket = new packet.Signature(date);
const signaturePacket = new SignaturePacket(date);
Object.assign(signaturePacket, signatureProperties);
signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm;
signaturePacket.hashAlgorithm = await getPreferredHashAlgo(privateKey, signingKeyPacket, date, userId);
Expand Down Expand Up @@ -231,15 +249,15 @@ export async function mergeSignatures(source, dest, attr, checkFn) {

/**
* Checks if a given certificate or binding signature is revoked
* @param {module:packet.SecretKey|
* module:packet.PublicKey} primaryKey The primary key packet
* @param {Object} dataToVerify The data to check
* @param {Array<module:packet.Signature>} revocations The revocation signatures to check
* @param {module:packet.Signature} signature The certificate or signature to check
* @param {module:packet.PublicSubkey|
* module:packet.SecretSubkey|
* module:packet.PublicKey|
* module:packet.SecretKey} key, optional The key packet to check the signature
* @param {SecretKeyPacket|
* PublicKeyPacket} primaryKey The primary key packet
* @param {Object} dataToVerify The data to check
* @param {Array<SignaturePacket>} revocations The revocation signatures to check
* @param {SignaturePacket} signature The certificate or signature to check
* @param {PublicSubkeyPacket|
* SecretSubkeyPacket|
* PublicKeyPacket|
* SecretKeyPacket} key, optional The key packet to check the signature
* @param {Date} date Use the given date instead of the current time
* @returns {Promise<Boolean>} True if the signature revokes the data
* @async
Expand Down
Loading

0 comments on commit fb3d234

Please sign in to comment.