fork-openpgpjs/src/openpgp.msg.privatekey.js
2011-12-09 18:29:47 +01:00

144 lines
5.2 KiB
JavaScript

// 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
function openpgp_msg_privatekey() {
this.subKeys = new Array();
this.privateKeyPacket = null;
this.userIds = new Array();
this.userAttributes = new Array();
this.revocationSignatures = new Array();
this.subKeys = new Array();
/**
*
* @return last position
*/
function read_nodes(parent_node, input, position, len) {
this.privateKeyPacket = parent_node;
var pos = position;
while (input.length > pos) {
var result = openpgp_packet.read_packet(input, pos, input.length - pos);
if (result == null) {
util.print_error("openpgp.msg.messge decrypt:\n"+'[pub/priv_key]parsing ends here @:' + pos + " l:" + len);
break;
} else {
switch (result.tagType) {
case 2: // public key revocation signature
if (result.signatureType == 32)
this.revocationSignatures[this.revocationSignatures.length] = result;
else if (result.signatureType > 15 && result.signatureType < 20) {
if (this.certificationsignatures == null)
this.certificationSignatures = new Array();
this.certificationSignatures[this.certificationSignatures.length] = result;
} else
util.print_error("openpgp.msg.messge decrypt:\n"+"unknown signature type directly on key "+result.signatureType+" @"+pos);
pos += result.packetLength + result.headerLength;
break;
case 7: // PrivateSubkey Packet
this.subKeys[this.subKeys.length] = result;
pos += result.packetLength + result.headerLength;
pos += result.read_nodes(this.privateKeyPacket,input, pos, input.length - pos);
break;
case 17: // User Attribute Packet
this.userAttributes[this.userAttributes.length] = result;
pos += result.packetLength + result.headerLength;
pos += result.read_nodes(this.privateKeyPacket,input, pos, input.length - pos);
break;
case 13: // User ID Packet
this.userIds[this.userIds.length] = result;
pos += result.packetLength + result.headerLength;
pos += result.read_nodes(this.privateKeyPacket, input, pos, input.length - pos);
break;
default:
this.position = position - this.privateKeyPacket.packetLength - this.privateKeyPacket.headerLength;
this.len = pos - position;
return this.len;
}
}
}
this.position = position - this.privateKeyPacket.packetLength - this.privateKeyPacket.headerLength;
this.len = pos - position;
return this.len;
}
function getKeyId() {
return this.privateKeyPacket.publicKey.getKeyId();
}
function getSubKeyIds() {
if (this.privateKeyPacket.publicKey.version == 4) // V3 keys MUST NOT have subkeys.
var result = new Array();
for (var i = 0; i < this.subKeys.length; i++) {
result[i] = str_sha1(this.subKeys[i].publicKey.header+this.subKeys[i].publicKey.data).substring(12,20);
}
return result;
}
function getSigningKey() {
if ((this.privateKeyPacket.publicKey.publicKeyAlgorithm == 17 ||
this.privateKeyPacket.publicKey.publicKeyAlgorithm != 2)
&& this.privateKeyPacket.publicKey.verifyKey() == 3)
return this.privateKeyPacket;
else if (this.privateKeyPacket.publicKey.version == 4) // V3 keys MUST NOT have subkeys.
for (var j = 0; j < this.privateKeyPacket.subKeys.length; j++) {
if ((this.privateKeyPacket.subKeys[j].publicKey.publicKeyAlgorithm == 17 ||
this.privateKeyPacket.subKeys[j].publicKey.publicKeyAlgorithm != 2) &&
this.privateKeyPacket.subKeys[j].publicKey.verifyKey() == 3)
return this.privateKeyPacket.subKeys[j];
}
return null;
}
function getPreferredSignatureHashAlgorithm() {
var pkey = this.getSigningKey();
if (pkey == null) {
util.print_error("private key is for encryption only! Cannot create a signature.")
return null;
}
if (pkey.publicKey.publicKeyAlgorithm == 17) {
var dsa = new DSA();
return dsa.select_hash_algorithm(pkey.publicKey.MPIs[1].toBigInteger()); // q
}
return openpgp.config.config.prefer_hash_algorithm;
}
function decryptSecretMPIs(str_passphrase) {
return this.privateKeyPacket.decryptSecretMPIs(str_passphrase);
}
function getFingerprint() {
return this.privateKeyPacket.publicKey.getFingerprint();
}
// TODO need to implement this
function revoke() {
}
this.getSigningKey = getSigningKey;
this.getFingerprint = getFingerprint;
this.getPreferredSignatureHashAlgorithm = getPreferredSignatureHashAlgorithm;
this.read_nodes = read_nodes;
this.decryptSecretMPIs = decryptSecretMPIs;
this.getSubKeyIds = getSubKeyIds;
this.getKeyId = getKeyId;
}