Skip to content

Commit

Permalink
pgp: parse s2k gnu extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Feb 20, 2020
1 parent 76266c9 commit 2a1d2e2
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions lib/pgp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,7 @@ class S2K extends bio.Struct {
this.hash = 0;
this.count = 0;
this.salt = EMPTY;
this.serial = EMPTY;
}

derive(passphrase, size) {
Expand Down Expand Up @@ -1511,6 +1512,16 @@ class S2K extends bio.Struct {
size += 8;
size += 1;
break;
case 1001:
size += 3;
size += 1;
break;
case 1002:
size += 3;
size += 1;
size += 1;
size += this.serial.length;
break;
default:
throw new Error('Unknown S2K function.');
}
Expand All @@ -1519,7 +1530,7 @@ class S2K extends bio.Struct {
}

write(bw) {
bw.writeU8(this.mode);
bw.writeU8(this.mode >= 1000 ? 101 : this.mode);
bw.writeU8(this.hash);

switch (this.mode) {
Expand All @@ -1532,6 +1543,16 @@ class S2K extends bio.Struct {
bw.writeBytes(this.salt);
bw.writeU8(encodeCount(this.count));
break;
case 1001:
bw.writeString('GNU', 'binary');
bw.writeU8(1);
break;
case 1002:
bw.writeString('GNU', 'binary');
bw.writeU8(2);
bw.writeU8(this.serial.length);
bw.writeBytes(this.serial);
break;
default:
throw new Error('Unknown S2K function.');
}
Expand All @@ -1544,17 +1565,49 @@ class S2K extends bio.Struct {
this.hash = br.readU8();

switch (this.mode) {
case 0:
case 0: {
break;
case 1:
}

case 1: {
this.salt = br.readBytes(8);
break;
case 3:
}

case 3: {
this.salt = br.readBytes(8);
this.count = decodeCount(br.readU8());
break;
default:
}

case 101: {
// GNU extensions.
// See: https://github.com/handshake-org/hs-airdrop/issues/44
const tag = br.readString(3, 'binary');

if (tag !== 'GNU')
throw new Error('Unknown S2K function.');

this.mode = 1000 + br.readU8();

switch (this.mode) {
case 1001:
// gnu-dummy
break;
case 1002:
// gnu-divert-to-card
this.serial = br.readBytes(Math.max(br.readU8(), 16));
break;
default:
throw new Error('Unknown S2K function.');
}

break;
}

default: {
throw new Error('Unknown S2K function.');
}
}

return this;
Expand All @@ -1565,7 +1618,8 @@ class S2K extends bio.Struct {
mode: this.mode,
hash: hashTypesByVal[this.hash] || 'UNKNOWN',
count: this.count,
salt: this.salt.toString('hex')
salt: this.salt.toString('hex'),
serial: this.serial.toString('hex')
};
}
}
Expand Down

0 comments on commit 2a1d2e2

Please sign in to comment.