Skip to content

Commit 000e133

Browse files
authored
Leave unhashed subpackets as-is when re-serializing signatures (#1561)
When re-serializing a signature packet, don't add Issuer, Issuer Fingerprint, and Embedded Signature subpackets to the unhashed subpackets if they weren't already there. Also, store all unhashed subpackets in `signature.unhashedSubpackets`, not just the "disallowed" ones.
1 parent 5e6dd8b commit 000e133

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

src/packet/signature.js

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

191+
// Set unhashed subpackets for serialization
192+
this.unhashedSubpackets = this.createUnhashedSubPackets();
193+
191194
this.signatureData = util.concat(arr);
192195

193196
const toHash = this.toHash(this.signatureType, data, detached);
@@ -315,26 +318,36 @@ class SignaturePacket {
315318
}
316319

317320
/**
318-
* Creates Uint8Array of bytes of Issuer and Embedded Signature subpackets
319-
* @returns {Uint8Array} Subpacket data.
321+
* Returns the Issuer, Issuer Fingperprint, and Embedded Signature subpacket bodies
322+
* @returns {Array<Uint8Array>} Subpackets.
320323
*/
321-
writeUnhashedSubPackets() {
324+
createUnhashedSubPackets() {
322325
const sub = enums.signatureSubpacket;
323326
const arr = [];
324327
let bytes;
325328
if (!this.issuerKeyID.isNull() && this.issuerKeyVersion !== 5) {
326329
// If the version of [the] key is greater than 4, this subpacket
327330
// MUST NOT be included in the signature.
328-
arr.push(writeSubPacket(sub.issuer, this.issuerKeyID.write()));
331+
arr.push(writeSubPacketBody(sub.issuer, this.issuerKeyID.write()));
329332
}
330333
if (this.embeddedSignature !== null) {
331-
arr.push(writeSubPacket(sub.embeddedSignature, this.embeddedSignature.write()));
334+
arr.push(writeSubPacketBody(sub.embeddedSignature, this.embeddedSignature.write()));
332335
}
333336
if (this.issuerFingerprint !== null) {
334337
bytes = [new Uint8Array([this.issuerKeyVersion]), this.issuerFingerprint];
335338
bytes = util.concat(bytes);
336-
arr.push(writeSubPacket(sub.issuerFingerprint, bytes));
339+
arr.push(writeSubPacketBody(sub.issuerFingerprint, bytes));
337340
}
341+
342+
return arr;
343+
}
344+
345+
/**
346+
* Creates an Uint8Array containing the unhashed subpackets
347+
* @returns {Uint8Array} Subpacket data.
348+
*/
349+
writeUnhashedSubPackets() {
350+
const arr = [];
338351
this.unhashedSubpackets.forEach(data => {
339352
arr.push(writeSimpleLength(data.length));
340353
arr.push(data);
@@ -354,9 +367,11 @@ class SignaturePacket {
354367
const critical = bytes[mypos] & 0x80;
355368
const type = bytes[mypos] & 0x7F;
356369

357-
if (!hashed && !allowedUnhashedSubpackets.has(type)) {
370+
if (!hashed) {
358371
this.unhashedSubpackets.push(bytes.subarray(mypos, bytes.length));
359-
return;
372+
if (!allowedUnhashedSubpackets.has(type)) {
373+
return;
374+
}
360375
}
361376

362377
mypos++;
@@ -762,3 +777,19 @@ function writeSubPacket(type, data) {
762777
arr.push(data);
763778
return util.concat(arr);
764779
}
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)