Skip to content

Commit 9f3985d

Browse files
authored
Hash all signature subpackets (#1562)
Move the Issuer, Issuer Fingerprint, and Embedded Signature subpackets to the hashed subpackets for new signatures. While we allow these to be unhashed, it's safer to hash them, and this simplifies the code as well.
1 parent 000e133 commit 9f3985d

1 file changed

Lines changed: 17 additions & 43 deletions

File tree

src/packet/signature.js

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ class SignaturePacket {
188188
// Add hashed subpackets
189189
arr.push(this.writeHashedSubPackets());
190190

191-
// Set unhashed subpackets for serialization
192-
this.unhashedSubpackets = this.createUnhashedSubPackets();
191+
// Remove unhashed subpackets, in case some allowed unhashed
192+
// subpackets existed, in order not to duplicate them (in both
193+
// the hashed and unhashed subpackets) when re-signing.
194+
this.unhashedSubpackets = [];
193195

194196
this.signatureData = util.concat(arr);
195197

@@ -253,6 +255,11 @@ class SignaturePacket {
253255
bytes = util.concat([bytes, this.revocationKeyFingerprint]);
254256
arr.push(writeSubPacket(sub.revocationKey, bytes));
255257
}
258+
if (!this.issuerKeyID.isNull() && this.issuerKeyVersion !== 5) {
259+
// If the version of [the] key is greater than 4, this subpacket
260+
// MUST NOT be included in the signature.
261+
arr.push(writeSubPacket(sub.issuer, this.issuerKeyID.write()));
262+
}
256263
this.rawNotations.forEach(([{ name, value, humanReadable }]) => {
257264
bytes = [new Uint8Array([humanReadable ? 0x80 : 0, 0, 0, 0])];
258265
// 2 octets of name length
@@ -306,6 +313,14 @@ class SignaturePacket {
306313
bytes = util.concat(bytes);
307314
arr.push(writeSubPacket(sub.signatureTarget, bytes));
308315
}
316+
if (this.embeddedSignature !== null) {
317+
arr.push(writeSubPacket(sub.embeddedSignature, this.embeddedSignature.write()));
318+
}
319+
if (this.issuerFingerprint !== null) {
320+
bytes = [new Uint8Array([this.issuerKeyVersion]), this.issuerFingerprint];
321+
bytes = util.concat(bytes);
322+
arr.push(writeSubPacket(sub.issuerFingerprint, bytes));
323+
}
309324
if (this.preferredAEADAlgorithms !== null) {
310325
bytes = util.stringToUint8Array(util.uint8ArrayToString(this.preferredAEADAlgorithms));
311326
arr.push(writeSubPacket(sub.preferredAEADAlgorithms, bytes));
@@ -317,31 +332,6 @@ class SignaturePacket {
317332
return util.concat([length, result]);
318333
}
319334

320-
/**
321-
* Returns the Issuer, Issuer Fingperprint, and Embedded Signature subpacket bodies
322-
* @returns {Array<Uint8Array>} Subpackets.
323-
*/
324-
createUnhashedSubPackets() {
325-
const sub = enums.signatureSubpacket;
326-
const arr = [];
327-
let bytes;
328-
if (!this.issuerKeyID.isNull() && this.issuerKeyVersion !== 5) {
329-
// If the version of [the] key is greater than 4, this subpacket
330-
// MUST NOT be included in the signature.
331-
arr.push(writeSubPacketBody(sub.issuer, this.issuerKeyID.write()));
332-
}
333-
if (this.embeddedSignature !== null) {
334-
arr.push(writeSubPacketBody(sub.embeddedSignature, this.embeddedSignature.write()));
335-
}
336-
if (this.issuerFingerprint !== null) {
337-
bytes = [new Uint8Array([this.issuerKeyVersion]), this.issuerFingerprint];
338-
bytes = util.concat(bytes);
339-
arr.push(writeSubPacketBody(sub.issuerFingerprint, bytes));
340-
}
341-
342-
return arr;
343-
}
344-
345335
/**
346336
* Creates an Uint8Array containing the unhashed subpackets
347337
* @returns {Uint8Array} Subpacket data.
@@ -777,19 +767,3 @@ function writeSubPacket(type, data) {
777767
arr.push(data);
778768
return util.concat(arr);
779769
}
780-
781-
/**
782-
* Creates a string representation of the body of a sub-packet (without length)
783-
* @see {@link https://tools.ietf.org/html/rfc4880#section-5.2.3.1|RFC4880 5.2.3.1}
784-
* @see {@link https://tools.ietf.org/html/rfc4880#section-5.2.3.2|RFC4880 5.2.3.2}
785-
* @param {Integer} type - Subpacket signature type.
786-
* @param {String} data - Data to be included
787-
* @returns {Uint8Array} A string-representation of a sub signature packet.
788-
* @private
789-
*/
790-
function writeSubPacketBody(type, data) {
791-
const arr = [];
792-
arr.push(new Uint8Array([type]));
793-
arr.push(data);
794-
return util.concat(arr);
795-
}

0 commit comments

Comments
 (0)