From 17fc32ba519f47aee9159b8884a6c473c9c373ce Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Tue, 9 Jun 2020 18:11:39 +0200 Subject: [PATCH] Appease linter --- .eslintrc.js | 1 + package-lock.json | 31 +++++++++ package.json | 1 + src/crypto/random.js | 98 +++++++++++++------------- src/keyring/keyring.js | 146 +++++++++++++++++++-------------------- src/packet/marker.js | 2 + src/packet/public_key.js | 2 + src/packet/trust.js | 2 + 8 files changed, 161 insertions(+), 122 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 548150e8..c92e05ab 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,6 @@ module.exports = { "extends": "airbnb-base", + "parser": "babel-eslint", "parserOptions": { "sourceType": "module" }, "env": { diff --git a/package-lock.json b/package-lock.json index 2d9dfbd2..c729c550 100644 --- a/package-lock.json +++ b/package-lock.json @@ -479,6 +479,20 @@ "js-tokens": "^3.0.2" } }, + "babel-eslint": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.1.0.tgz", + "integrity": "sha512-ifWaTHQ0ce+448CYop8AdrQiBsGrnC+bMgfyKFdi6EsPLTAWG+QfyDeM6OH+FmWnKvEq5NnBMLvlBUPKQZoDSg==", + "dev": true, + "requires": { + "@babel/code-frame": "^7.0.0", + "@babel/parser": "^7.7.0", + "@babel/traverse": "^7.7.0", + "@babel/types": "^7.7.0", + "eslint-visitor-keys": "^1.0.0", + "resolve": "^1.12.0" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -3063,6 +3077,23 @@ "lodash": "^4.17.14" } }, + "resolve": { + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.17.0.tgz", + "integrity": "sha512-ic+7JYiV8Vi2yzQGFWOkiZD5Z9z7O2Zhm9XMaTxdJExKasieFCr+yXZ/WmXsckHiKl12ar0y6XiXDx3m4RHn1w==", + "dev": true, + "requires": { + "path-parse": "^1.0.6" + }, + "dependencies": { + "path-parse": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "dev": true + } + } + }, "resolve-from": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-1.0.1.tgz", diff --git a/package.json b/package.json index 9fe1ff99..b34b19ae 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@rollup/plugin-node-resolve": "^7.1.3", "@rollup/plugin-replace": "^2.3.2", "asmcrypto.js": "github:openpgpjs/asmcrypto#475cffa5ccb2cf2556427056679414acf3610d1b", + "babel-eslint": "^10.1.0", "bn.js": "^4.11.8", "buffer": "^5.0.8", "chai": "^4.1.2", diff --git a/src/crypto/random.js b/src/crypto/random.js index 021e4e28..0718f94c 100644 --- a/src/crypto/random.js +++ b/src/crypto/random.js @@ -30,55 +30,6 @@ import util from '../util'; // Do not use util.getNodeCrypto because we need this regardless of useNative setting const nodeCrypto = util.detectNode() && require('crypto'); -export default { - /** - * Retrieve secure random byte array of the specified length - * @param {Integer} length Length in bytes to generate - * @returns {Uint8Array} Random byte array - * @async - */ - getRandomBytes: async function(length) { - const buf = new Uint8Array(length); - if (typeof crypto !== 'undefined' && crypto.getRandomValues) { - crypto.getRandomValues(buf); - } else if (typeof globalThis !== 'undefined' && typeof globalThis.msCrypto === 'object' && typeof globalThis.msCrypto.getRandomValues === 'function') { - globalThis.msCrypto.getRandomValues(buf); - } else if (nodeCrypto) { - const bytes = nodeCrypto.randomBytes(buf.length); - buf.set(bytes); - } else if (this.randomBuffer.buffer) { - await this.randomBuffer.get(buf); - } else { - throw new Error('No secure random number generator available.'); - } - return buf; - }, - - /** - * Create a secure random MPI that is greater than or equal to min and less than max. - * @param {module:type/mpi} min Lower bound, included - * @param {module:type/mpi} max Upper bound, excluded - * @returns {module:BN} Random MPI - * @async - */ - getRandomBN: async function(min, max) { - if (max.cmp(min) <= 0) { - throw new Error('Illegal parameter value: max <= min'); - } - - const modulus = max.sub(min); - const bytes = modulus.byteLength(); - - // Using a while loop is necessary to avoid bias introduced by the mod operation. - // However, we request 64 extra random bits so that the bias is negligible. - // Section B.1.1 here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf - const r = new BN(await this.getRandomBytes(bytes + 8)); - return r.mod(modulus).add(min); - }, - - randomBuffer: new RandomBuffer() -}; - /** * Buffer for secure random numbers */ @@ -145,3 +96,52 @@ class RandomBuffer { } } } + +export default { + /** + * Retrieve secure random byte array of the specified length + * @param {Integer} length Length in bytes to generate + * @returns {Uint8Array} Random byte array + * @async + */ + getRandomBytes: async function(length) { + const buf = new Uint8Array(length); + if (typeof crypto !== 'undefined' && crypto.getRandomValues) { + crypto.getRandomValues(buf); + } else if (typeof globalThis !== 'undefined' && typeof globalThis.msCrypto === 'object' && typeof globalThis.msCrypto.getRandomValues === 'function') { + globalThis.msCrypto.getRandomValues(buf); + } else if (nodeCrypto) { + const bytes = nodeCrypto.randomBytes(buf.length); + buf.set(bytes); + } else if (this.randomBuffer.buffer) { + await this.randomBuffer.get(buf); + } else { + throw new Error('No secure random number generator available.'); + } + return buf; + }, + + /** + * Create a secure random MPI that is greater than or equal to min and less than max. + * @param {module:type/mpi} min Lower bound, included + * @param {module:type/mpi} max Upper bound, excluded + * @returns {module:BN} Random MPI + * @async + */ + getRandomBN: async function(min, max) { + if (max.cmp(min) <= 0) { + throw new Error('Illegal parameter value: max <= min'); + } + + const modulus = max.sub(min); + const bytes = modulus.byteLength(); + + // Using a while loop is necessary to avoid bias introduced by the mod operation. + // However, we request 64 extra random bits so that the bias is negligible. + // Section B.1.1 here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf + const r = new BN(await this.getRandomBytes(bytes + 8)); + return r.mod(modulus).add(min); + }, + + randomBuffer: new RandomBuffer() +}; diff --git a/src/keyring/keyring.js b/src/keyring/keyring.js index 7680b070..b4298015 100644 --- a/src/keyring/keyring.js +++ b/src/keyring/keyring.js @@ -25,79 +25,6 @@ import { readAllArmored } from '../key'; import LocalStore from './localstore'; -class Keyring { - /** - * Initialization routine for the keyring. - * @param {keyring/localstore} [storeHandler] class implementing loadPublic(), loadPrivate(), storePublic(), and storePrivate() methods - */ - constructor(storeHandler) { - this.storeHandler = storeHandler || new LocalStore(); - } - - /** - * Calls the storeHandler to load the keys - * @async - */ - async load() { - this.publicKeys = new KeyArray(await this.storeHandler.loadPublic()); - this.privateKeys = new KeyArray(await this.storeHandler.loadPrivate()); - } - - /** - * Calls the storeHandler to save the keys - * @async - */ - async store() { - await Promise.all([ - this.storeHandler.storePublic(this.publicKeys.keys), - this.storeHandler.storePrivate(this.privateKeys.keys) - ]); - } - - /** - * Clear the keyring - erase all the keys - */ - clear() { - this.publicKeys.keys = []; - this.privateKeys.keys = []; - } - - /** - * Searches the keyring for keys having the specified key id - * @param {String} keyId provided as string of lowercase hex number - * withouth 0x prefix (can be 16-character key ID or fingerprint) - * @param {Boolean} deep if true search also in subkeys - * @returns {Array|null} keys found or null - */ - getKeysForId(keyId, deep) { - let result = []; - result = result.concat(this.publicKeys.getForId(keyId, deep) || []); - result = result.concat(this.privateKeys.getForId(keyId, deep) || []); - return result.length ? result : null; - } - - /** - * Removes keys having the specified key id from the keyring - * @param {String} keyId provided as string of lowercase hex number - * withouth 0x prefix (can be 16-character key ID or fingerprint) - * @returns {Array|null} keys found or null - */ - removeKeysForId(keyId) { - let result = []; - result = result.concat(this.publicKeys.removeForId(keyId) || []); - result = result.concat(this.privateKeys.removeForId(keyId) || []); - return result.length ? result : null; - } - - /** - * Get all public and private keys - * @returns {Array} all keys - */ - getAllKeys() { - return this.publicKeys.keys.concat(this.privateKeys.keys); - } -} - /** * Array of keys */ @@ -192,6 +119,79 @@ class KeyArray { } } +class Keyring { + /** + * Initialization routine for the keyring. + * @param {keyring/localstore} [storeHandler] class implementing loadPublic(), loadPrivate(), storePublic(), and storePrivate() methods + */ + constructor(storeHandler) { + this.storeHandler = storeHandler || new LocalStore(); + } + + /** + * Calls the storeHandler to load the keys + * @async + */ + async load() { + this.publicKeys = new KeyArray(await this.storeHandler.loadPublic()); + this.privateKeys = new KeyArray(await this.storeHandler.loadPrivate()); + } + + /** + * Calls the storeHandler to save the keys + * @async + */ + async store() { + await Promise.all([ + this.storeHandler.storePublic(this.publicKeys.keys), + this.storeHandler.storePrivate(this.privateKeys.keys) + ]); + } + + /** + * Clear the keyring - erase all the keys + */ + clear() { + this.publicKeys.keys = []; + this.privateKeys.keys = []; + } + + /** + * Searches the keyring for keys having the specified key id + * @param {String} keyId provided as string of lowercase hex number + * withouth 0x prefix (can be 16-character key ID or fingerprint) + * @param {Boolean} deep if true search also in subkeys + * @returns {Array|null} keys found or null + */ + getKeysForId(keyId, deep) { + let result = []; + result = result.concat(this.publicKeys.getForId(keyId, deep) || []); + result = result.concat(this.privateKeys.getForId(keyId, deep) || []); + return result.length ? result : null; + } + + /** + * Removes keys having the specified key id from the keyring + * @param {String} keyId provided as string of lowercase hex number + * withouth 0x prefix (can be 16-character key ID or fingerprint) + * @returns {Array|null} keys found or null + */ + removeKeysForId(keyId) { + let result = []; + result = result.concat(this.publicKeys.removeForId(keyId) || []); + result = result.concat(this.privateKeys.removeForId(keyId) || []); + return result.length ? result : null; + } + + /** + * Get all public and private keys + * @returns {Array} all keys + */ + getAllKeys() { + return this.publicKeys.keys.concat(this.privateKeys.keys); + } +} + /** * Checks a key to see if it matches the specified email address * @private diff --git a/src/packet/marker.js b/src/packet/marker.js index d0e44f58..4a4ac80e 100644 --- a/src/packet/marker.js +++ b/src/packet/marker.js @@ -15,6 +15,8 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +/* eslint class-methods-use-this: ["error", { "exceptMethods": ["read"] }] */ + /** * @requires enums */ diff --git a/src/packet/public_key.js b/src/packet/public_key.js index 497e78b5..0a8e7faf 100644 --- a/src/packet/public_key.js +++ b/src/packet/public_key.js @@ -15,6 +15,8 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +/* eslint class-methods-use-this: ["error", { "exceptMethods": ["isDecrypted"] }] */ + /** * @requires type/keyid * @requires type/mpi diff --git a/src/packet/trust.js b/src/packet/trust.js index 3d98a3cc..14226bd6 100644 --- a/src/packet/trust.js +++ b/src/packet/trust.js @@ -1,3 +1,5 @@ +/* eslint class-methods-use-this: ["error", { "exceptMethods": ["read"] }] */ + /** * @requires enums */