node-forge: Fix typings of ed25519 module (#37080)

This change makes the functions actually accept what they are documented
to accept based on the error messages. Specifically this allows to pass
node.js `Buffer`s to `sign` / `verify`, instead of needing to convert them
to a `string`, just for forge to convert them back to a `NativeBuffer`
internally.

This also fixes the type for `ed25519.Key`. `ArrayBuffer` does not appear
in forge's source code at all. Instead all functions accept something
containing bytes (i.e. the new `BinaryBuffer` alias) and return a
`NativeBuffer`. I opted to alias `Key` to `NativeBuffer`, because that's
what is being returned by `publicKeyFromPrivateKey` and `generateKeyPair`,
it's a subset of `BinaryBuffer` and does not cause `forge.pki.Key` (which
is the union of RSA and ed25519 keys) to become a kitchen sink type.
This commit is contained in:
Tim Düsterhus
2019-07-30 10:14:31 -07:00
committed by Jesse Trinity
parent 1c649bd717
commit f3eb9ef31f
2 changed files with 35 additions and 16 deletions
+19 -16
View File
@@ -75,10 +75,6 @@ declare module "node-forge" {
}
var oids: oids;
namespace ed25519 {
type Key = ArrayBuffer;
}
namespace rsa {
type EncryptionScheme = 'RSAES-PKCS1-V1_5' | 'RSA-OAEP' | 'RAW' | 'NONE' | null;
type SignatureScheme = 'RSASSA-PKCS1-V1_5' | pss.PSS | 'NONE' | null;
@@ -125,8 +121,18 @@ declare module "node-forge" {
}
namespace ed25519 {
type NativeBuffer = Buffer | Uint8Array;
type Key = NativeBuffer;
type ToNativeBufferParameters = {
message: NativeBuffer | util.ByteBuffer
} | {
message: string;
encoding: 'binary' | 'utf8';
};
// `string`s will be converted by toNativeBuffer with `encoding: 'binary'`
type BinaryBuffer = NativeBuffer | util.ByteBuffer | string;
namespace constants {
const PUBLIC_KEY_BYTE_LENGTH = 32;
@@ -136,24 +142,21 @@ declare module "node-forge" {
const HASH_BYTE_LENGTH = 64;
}
function generateKeyPair(options?: { seed?: Buffer | Uint8Array | string }): {
// generateKeyPair does not currently accept `util.ByteBuffer` as the seed.
function generateKeyPair(options?: { seed?: NativeBuffer | string }): {
publicKey: NativeBuffer;
privateKey: NativeBuffer;
};
function publicKeyFromPrivateKey(options: { privateKey: NativeBuffer }): NativeBuffer;
function publicKeyFromPrivateKey(options: { privateKey: BinaryBuffer }): NativeBuffer;
function sign(options: {
message: string,
encoding: string,
privateKey: NativeBuffer
function sign(options: ToNativeBufferParameters & {
privateKey: BinaryBuffer
}): NativeBuffer;
function verify(options: {
message: string,
encoding: string,
signature: Buffer | Uint8Array | util.ByteBuffer | string,
publicKey: NativeBuffer
function verify(options: ToNativeBufferParameters & {
signature: BinaryBuffer,
publicKey: BinaryBuffer
}): boolean;
}
+16
View File
@@ -364,3 +364,19 @@ if (forge.util.fillString('1', 5) !== '11111') throw Error('forge.util.fillStrin
console.log('created TLS client and server, doing handshake...');
client.handshake();
}
{
const { privateKey } = forge.pki.ed25519.generateKeyPair();
const toSign = Buffer.from('test', 'utf8');
forge.pki.ed25519.sign({
message: toSign,
privateKey
});
const toSign2 = 'foo';
forge.pki.ed25519.sign({
message: toSign2,
encoding: 'utf8',
privateKey
});
}