310 lines
12 KiB
HTML
310 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: packet/secret_key.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: packet/secret_key.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source"><code>// GPG4Browsers - An OpenPGP implementation in javascript
|
|
// Copyright (C) 2011 Recurity Labs GmbH
|
|
//
|
|
// This library is free software; you can redistribute it and/or
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
// License as published by the Free Software Foundation; either
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
// Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
// License along with this library; if not, write to the Free Software
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
/** @module packet/secret_key */
|
|
|
|
var publicKey = require('./public_key.js'),
|
|
enums = require('../enums.js'),
|
|
util = require('../util'),
|
|
crypto = require('../crypto'),
|
|
type_mpi = require('../type/mpi.js'),
|
|
type_s2k = require('../type/s2k.js');
|
|
|
|
/**
|
|
* @class
|
|
* @classdesc Implementation of the Key Material Packet (Tag 5,6,7,14)
|
|
* @extends module:packet/public_key
|
|
*
|
|
* RFC4480 5.5:
|
|
* A key material packet contains all the information about a public or
|
|
* private key. There are four variants of this packet type, and two
|
|
* major versions. Consequently, this section is complex.
|
|
*/
|
|
module.exports = function () {
|
|
publicKey.call(this);
|
|
// encrypted secret-key data
|
|
this.encrypted = null;
|
|
// indicator if secret-key data is available in decrypted form
|
|
this.isDecrypted = false;
|
|
|
|
|
|
function get_hash_len(hash) {
|
|
if (hash == 'sha1')
|
|
return 20;
|
|
else
|
|
return 2;
|
|
}
|
|
|
|
function get_hash_fn(hash) {
|
|
if (hash == 'sha1')
|
|
return crypto.hash.sha1;
|
|
else
|
|
return function(c) {
|
|
return util.writeNumber(util.calc_checksum(c), 2);
|
|
};
|
|
}
|
|
|
|
// Helper function
|
|
|
|
function parse_cleartext_mpi(hash_algorithm, cleartext, algorithm) {
|
|
var hashlen = get_hash_len(hash_algorithm),
|
|
hashfn = get_hash_fn(hash_algorithm);
|
|
|
|
var hashtext = cleartext.substr(cleartext.length - hashlen);
|
|
cleartext = cleartext.substr(0, cleartext.length - hashlen);
|
|
|
|
var hash = hashfn(cleartext);
|
|
|
|
if (hash != hashtext)
|
|
return new Error("Hash mismatch.");
|
|
|
|
var mpis = crypto.getPrivateMpiCount(algorithm);
|
|
|
|
var j = 0;
|
|
var mpi = [];
|
|
|
|
for (var i = 0; i < mpis && j < cleartext.length; i++) {
|
|
mpi[i] = new type_mpi();
|
|
j += mpi[i].read(cleartext.substr(j));
|
|
}
|
|
|
|
return mpi;
|
|
}
|
|
|
|
function write_cleartext_mpi(hash_algorithm, algorithm, mpi) {
|
|
var bytes = '';
|
|
var discard = crypto.getPublicMpiCount(algorithm);
|
|
|
|
for (var i = discard; i < mpi.length; i++) {
|
|
bytes += mpi[i].write();
|
|
}
|
|
|
|
|
|
bytes += get_hash_fn(hash_algorithm)(bytes);
|
|
|
|
return bytes;
|
|
}
|
|
|
|
|
|
// 5.5.3. Secret-Key Packet Formats
|
|
|
|
/**
|
|
* Internal parser for private keys as specified in RFC 4880 section 5.5.3
|
|
* @param {String} bytes Input string to read the packet from
|
|
*/
|
|
this.read = function(bytes) {
|
|
// - A Public-Key or Public-Subkey packet, as described above.
|
|
var len = this.readPublicKey(bytes);
|
|
|
|
bytes = bytes.substr(len);
|
|
|
|
|
|
// - One octet indicating string-to-key usage conventions. Zero
|
|
// indicates that the secret-key data is not encrypted. 255 or 254
|
|
// indicates that a string-to-key specifier is being given. Any
|
|
// other value is a symmetric-key encryption algorithm identifier.
|
|
var isEncrypted = bytes.charCodeAt(0);
|
|
|
|
if (isEncrypted) {
|
|
this.encrypted = bytes;
|
|
} else {
|
|
|
|
// - Plain or encrypted multiprecision integers comprising the secret
|
|
// key data. These algorithm-specific fields are as described
|
|
// below.
|
|
var parsedMPI = parse_cleartext_mpi('mod', bytes.substr(1), this.algorithm);
|
|
if (parsedMPI instanceof Error)
|
|
throw parsedMPI;
|
|
this.mpi = this.mpi.concat(parsedMPI);
|
|
this.isDecrypted = true;
|
|
}
|
|
|
|
};
|
|
|
|
/** Creates an OpenPGP key packet for the given key.
|
|
* @return {String} A string of bytes containing the secret key OpenPGP packet
|
|
*/
|
|
this.write = function() {
|
|
var bytes = this.writePublicKey();
|
|
|
|
if (!this.encrypted) {
|
|
bytes += String.fromCharCode(0);
|
|
|
|
bytes += write_cleartext_mpi('mod', this.algorithm, this.mpi);
|
|
} else {
|
|
bytes += this.encrypted;
|
|
}
|
|
|
|
return bytes;
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Encrypt the payload. By default, we use aes256 and iterated, salted string
|
|
* to key specifier
|
|
* @param {String} passphrase
|
|
*/
|
|
this.encrypt = function(passphrase) {
|
|
|
|
var s2k = new type_s2k(),
|
|
symmetric = 'aes256',
|
|
cleartext = write_cleartext_mpi('sha1', this.algorithm, this.mpi),
|
|
key = produceEncryptionKey(s2k, passphrase, symmetric),
|
|
blockLen = crypto.cipher[symmetric].blockSize,
|
|
iv = crypto.random.getRandomBytes(blockLen);
|
|
|
|
|
|
this.encrypted = '';
|
|
this.encrypted += String.fromCharCode(254);
|
|
this.encrypted += String.fromCharCode(enums.write(enums.symmetric, symmetric));
|
|
this.encrypted += s2k.write();
|
|
this.encrypted += iv;
|
|
|
|
this.encrypted += crypto.cfb.normalEncrypt(symmetric, key, cleartext, iv);
|
|
};
|
|
|
|
function produceEncryptionKey(s2k, passphrase, algorithm) {
|
|
return s2k.produce_key(passphrase,
|
|
crypto.cipher[algorithm].keySize);
|
|
}
|
|
|
|
/**
|
|
* Decrypts the private key MPIs which are needed to use the key.
|
|
* openpgp_packet_keymaterial.hasUnencryptedSecretKeyData should be
|
|
* false otherwise
|
|
* a call to this function is not needed
|
|
*
|
|
* @param {String} str_passphrase The passphrase for this private key
|
|
* as string
|
|
* @return {Boolean} True if the passphrase was correct or MPI already
|
|
* decrypted; false if not
|
|
*/
|
|
this.decrypt = function(passphrase) {
|
|
if (this.isDecrypted)
|
|
return true;
|
|
|
|
var i = 0,
|
|
symmetric,
|
|
key;
|
|
|
|
var s2k_usage = this.encrypted.charCodeAt(i++);
|
|
|
|
// - [Optional] If string-to-key usage octet was 255 or 254, a one-
|
|
// octet symmetric encryption algorithm.
|
|
if (s2k_usage == 255 || s2k_usage == 254) {
|
|
symmetric = this.encrypted.charCodeAt(i++);
|
|
symmetric = enums.read(enums.symmetric, symmetric);
|
|
|
|
// - [Optional] If string-to-key usage octet was 255 or 254, a
|
|
// string-to-key specifier. The length of the string-to-key
|
|
// specifier is implied by its type, as described above.
|
|
var s2k = new type_s2k();
|
|
i += s2k.read(this.encrypted.substr(i));
|
|
|
|
key = produceEncryptionKey(s2k, passphrase, symmetric);
|
|
} else {
|
|
symmetric = s2k_usage;
|
|
symmetric = enums.read(enums.symmetric, symmetric);
|
|
key = crypto.hash.md5(passphrase);
|
|
}
|
|
|
|
|
|
// - [Optional] If secret data is encrypted (string-to-key usage octet
|
|
// not zero), an Initial Vector (IV) of the same length as the
|
|
// cipher's block size.
|
|
var iv = this.encrypted.substr(i,
|
|
crypto.cipher[symmetric].blockSize);
|
|
|
|
i += iv.length;
|
|
|
|
var cleartext,
|
|
ciphertext = this.encrypted.substr(i);
|
|
|
|
cleartext = crypto.cfb.normalDecrypt(symmetric, key, ciphertext, iv);
|
|
|
|
var hash = s2k_usage == 254 ?
|
|
'sha1' :
|
|
'mod';
|
|
|
|
var parsedMPI = parse_cleartext_mpi(hash, cleartext, this.algorithm);
|
|
if (parsedMPI instanceof Error)
|
|
return false;
|
|
this.mpi = this.mpi.concat(parsedMPI);
|
|
this.isDecrypted = true;
|
|
return true;
|
|
};
|
|
|
|
this.generate = function(bits) {
|
|
this.mpi = crypto.generateMpi(this.algorithm, bits);
|
|
this.isDecrypted = true;
|
|
};
|
|
|
|
}
|
|
|
|
module.exports.prototype = new publicKey();
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Index</a></h2><h3>Modules</h3><ul><li><a href="module-cleartext.html">cleartext</a></li><li><a href="config.html">config/config</a></li><li><a href="config_localStorage.html">config/config_localStorage</a></li><li><a href="cfb.html">crypto/cfb</a></li><li><a href="aes.html">crypto/cipher/aes</a></li><li><a href="blowfish.html">crypto/cipher/blowfish</a></li><li><a href="cast5.html">crypto/cipher/cast5</a></li><li><a href="des.html">crypto/cipher/des</a></li><li><a href="twofish.html">crypto/cipher/twofish</a></li><li><a href="crypto.html">crypto/crypto</a></li><li><a href="md5.html">crypto/hash/md5</a></li><li><a href="ripe-md.html">crypto/hash/ripe-md</a></li><li><a href="sha.html">crypto/hash/sha</a></li><li><a href="pkcs1.html">crypto/pkcs1</a></li><li><a href="dsa.html">crypto/public_key/dsa</a></li><li><a href="elgamal.html">crypto/public_key/elgamal</a></li><li><a href="jsbn.html">crypto/public_key/jsbn</a></li><li><a href="rsa.html">crypto/public_key/rsa</a></li><li><a href="random.html">crypto/random</a></li><li><a href="signature.html">crypto/signature</a></li><li><a href="armor.html">encoding/armor</a></li><li><a href="base64.html">encoding/base64</a></li><li><a href="module-enums.html">enums</a></li><li><a href="module-key.html">key</a></li><li><a href="keyring.html">keyring/keyring</a></li><li><a href="localstore.html">keyring/localstore</a></li><li><a href="module-message.html">message</a></li><li><a href="module-openpgp.html">openpgp</a></li><li><a href="compressed.html">packet/compressed</a></li><li><a href="literal.html">packet/literal</a></li><li><a href="marker.html">packet/marker</a></li><li><a href="one_pass_signature.html">packet/one_pass_signature</a></li><li><a href="packet.html">packet/packet</a></li><li><a href="packetlist.html">packet/packetlist</a></li><li><a href="public_key.html">packet/public_key</a></li><li><a href="public_key_encrypted_session_key.html">packet/public_key_encrypted_session_key</a></li><li><a href="public_subkey.html">packet/public_subkey</a></li><li><a href="secret_key.html">packet/secret_key</a></li><li><a href="secret_subkey.html">packet/secret_subkey</a></li><li><a href="signature_.html">packet/signature</a></li><li><a href="sym_encrypted_integrity_protected.html">packet/sym_encrypted_integrity_protected</a></li><li><a href="sym_encrypted_session_key.html">packet/sym_encrypted_session_key</a></li><li><a href="symmetrically_encrypted.html">packet/symmetrically_encrypted</a></li><li><a href="user_attribute.html">packet/user_attribute</a></li><li><a href="userid.html">packet/userid</a></li><li><a href="keyid.html">type/keyid</a></li><li><a href="mpi.html">type/mpi</a></li><li><a href="s2k.html">type/s2k</a></li><li><a href="util.html">util/util</a></li></ul><h3>Classes</h3><ul><li><a href="JXG.Util.html">Util</a></li><li><a href="module-cleartext-CleartextMessage.html">CleartextMessage</a></li><li><a href="config-config.html">config</a></li><li><a href="module-key-Key.html">Key</a></li><li><a href="module-key-SubKey.html">SubKey</a></li><li><a href="module-key-User.html">User</a></li><li><a href="module-message-Message.html">Message</a></li><li><a href="util-Util.html">Util</a></li></ul><h3><a href="global.html">Global</a></h3>
|
|
</nav>
|
|
|
|
<br clear="both">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha2</a> on Sun Dec 22 2013 23:43:09 GMT-0800 (PST)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|