Merge pull request #30866 from timhwang21/devs/timothy/node-forge

[node-forge] add types for missing modules; fix incorrect types
This commit is contained in:
Mine Starks
2018-11-28 12:37:08 -08:00
committed by GitHub
2 changed files with 103 additions and 47 deletions
+86 -44
View File
@@ -7,6 +7,7 @@
// Beeno Tung <https://github.com/beenotung>
// Joe Flateau <https://github.com/joeflateau>
// Nikita Koryabkin <https://github.com/Apologiz>
// timhwang21 <https://github.com/timhwang21>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.6
@@ -20,6 +21,15 @@ declare module "node-forge" {
type Utf8 = string;
type OID = string;
namespace jsbn {
class BigInteger {
data: number[];
t: number;
s: number;
toString(): string;
}
}
namespace pem {
interface EncodeOptions {
@@ -40,28 +50,18 @@ declare module "node-forge" {
}
namespace pki {
type PEM = string;
type Key = any;
type PublicKey = rsa.PublicKey | ed25519.Key;
type PrivateKey = rsa.PrivateKey | ed25519.Key;
interface KeyPair {
publicKey: PublicKey;
privateKey: PrivateKey;
}
interface PublicKey {
encrypt(data: string, scheme?: string, schemeOptions?: number): Bytes
verify(digest: string, signature: string, scheme?: string): boolean
}
interface PrivateKey {
decrypt(data: string, scheme?: string, schemeOptions?: number): string
sign(md: string, scheme?: string): Bytes
}
function pemToDer(pem: PEM): util.ByteStringBuffer;
function privateKeyToPem(key: PrivateKey, maxline?: number): PEM;
function privateKeyInfoToPem(key: PrivateKey, maxline?: number): PEM;
function privateKeyInfoToPem(key: Bytes, maxline?: number): PEM;
function publicKeyToPem(key: PublicKey, maxline?: number): PEM;
function publicKeyFromPem(pem: PEM): PublicKey;
function privateKeyFromPem(pem: PEM): PrivateKey;
@@ -73,7 +73,38 @@ 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;
interface PublicKey {
n: jsbn.BigInteger;
e: jsbn.BigInteger;
encrypt(data: Bytes, scheme?: EncryptionScheme, schemeOptions?: any): Bytes;
verify(digest: Bytes, signature: Bytes, scheme?: SignatureScheme): boolean;
}
interface PrivateKey {
n: jsbn.BigInteger;
e: jsbn.BigInteger;
d: jsbn.BigInteger;
p: jsbn.BigInteger;
q: jsbn.BigInteger;
dP: jsbn.BigInteger;
dQ: jsbn.BigInteger;
qInv: jsbn.BigInteger;
decrypt(data: Bytes, scheme?: EncryptionScheme, schemeOptions?: any): Bytes;
sign(md: md.MessageDigest, scheme?: SignatureScheme): Bytes;
}
interface KeyPair {
publicKey: PublicKey;
privateKey: PrivateKey;
}
interface GenerateKeyPairOptions {
bits?: number;
@@ -84,9 +115,9 @@ declare module "node-forge" {
prng?: any;
algorithm?: string;
}
function setPublicKey(n: any, e: any): any;
function generateKeyPair(bits?: number, e?: number, callback?: (err: Error, keypair: KeyPair) => void): KeyPair;
function generateKeyPair(options?: GenerateKeyPairOptions, callback?: (err: Error, keypair: KeyPair) => void): KeyPair;
}
@@ -160,8 +191,8 @@ declare module "node-forge" {
hash: any;
};
extensions: any[];
privateKey: Key;
publicKey: Key;
privateKey: PrivateKey;
publicKey: PublicKey;
md: any;
/**
* Sets the subject of this certificate.
@@ -200,7 +231,7 @@ declare module "node-forge" {
* @param key the private key to sign with.
* @param md the message digest object to use (defaults to forge.md.sha1).
*/
sign(key: pki.Key, md: md.MessageDigest): void;
sign(key: pki.PrivateKey, md?: md.MessageDigest): void;
/**
* Attempts verify the signature on the passed certificate using this
* certificate's public key.
@@ -215,19 +246,30 @@ declare module "node-forge" {
function certificateFromAsn1(obj: asn1.Asn1, computeHash?: boolean): Certificate;
function decryptRsaPrivateKey(pem: PEM, passphrase?: string): Key;
function decryptRsaPrivateKey(pem: PEM, passphrase?: string): PrivateKey;
function createCertificate(): Certificate;
function certificationRequestToPem(cert: Certificate, maxline?: number): PEM;
function certificationRequestFromPem(pem: PEM, computeHash?: boolean, strict?: boolean): Certificate;
function createCertificationRequest(): Certificate;
function publicKeyToAsn1(publicKey: Key): any;
function publicKeyToRSAPublicKey(publicKey: Key): any;
function publicKeyToAsn1(publicKey: PublicKey): any;
function publicKeyToRSAPublicKey(publicKey: PublicKey): any;
}
namespace random {
function getBytes(count: number, callback?: (bytes: Bytes) => any): Bytes;
function getBytesSync(count: number): Bytes;
type CB = (_: any, seed: string) => void;
interface Random {
seedFileSync: (needed: number) => string;
seedFile: (needed: number, cb: CB) => void;
}
function createInstance(): Random;
}
namespace ssh {
@@ -249,22 +291,22 @@ declare module "node-forge" {
/**
* @description Encodes a private RSA key as an OpenSSH file
*/
function privateKeyToOpenSSH(privateKey: pki.Key, passphrase?: string): string;
function privateKeyToOpenSSH(privateKey: pki.PrivateKey, passphrase?: string): string;
/**
* @description Encodes (and optionally encrypts) a private RSA key as a Putty PPK file
*/
function privateKeyToPutty(privateKey: pki.Key, passphrase?: string, comment?: string): string;
function privateKeyToPutty(privateKey: pki.PrivateKey, passphrase?: string, comment?: string): string;
/**
* @description Encodes a public RSA key as an OpenSSH file
*/
function publicKeyToOpenSSH(publicKey: pki.Key, comment?: string): string | pki.PEM;
function publicKeyToOpenSSH(publicKey: pki.PublicKey, comment?: string): string | pki.PEM;
/**
* @description Gets the SSH fingerprint for the given public key
*/
function getPublicKeyFingerprint(publicKey: pki.Key, options?: FingerprintOptions): util.ByteStringBuffer | Hex | string;
function getPublicKeyFingerprint(publicKey: pki.PublicKey, options?: FingerprintOptions): util.ByteStringBuffer | Hex | string;
}
namespace asn1 {
@@ -377,7 +419,7 @@ declare module "node-forge" {
function decodeUtf8(encoded: Utf8): string;
function createBuffer(): ByteBuffer;
function createBuffer(input: string, encode: string): ByteBuffer;
function createBuffer(input: Bytes | ArrayBuffer | ArrayBufferView | ByteStringBuffer, encode: string): ByteBuffer;
namespace binary {
namespace raw {
@@ -418,7 +460,7 @@ declare module "node-forge" {
interface Bag {
type: string;
attributes: any;
key?: pki.Key;
key?: pki.PrivateKey;
cert?: pki.Certificate;
asn1: asn1.Asn1
}
@@ -441,12 +483,12 @@ declare module "node-forge" {
function pkcs12FromAsn1(obj: any, strict?: boolean, password?: string): Pkcs12Pfx;
function pkcs12FromAsn1(obj: any, password?: string): Pkcs12Pfx;
}
namespace pkcs7 {
interface PkcsSignedData {
content?: string | util.ByteBuffer;
contentInfo?: { value: any[] };
addCertificate(certificate: pki.Certificate): void;
addSigner(options: {
key: string;
@@ -459,14 +501,16 @@ declare module "node-forge" {
}): void;
toAsn1(): asn1.Asn1;
}
function createSignedData(): PkcsSignedData;
}
namespace md {
type Encoding = "raw" | "utf8"
interface MessageDigest {
update(msg: string, encoding?: string): MessageDigest;
update(msg: string, encoding?: Encoding): MessageDigest;
digest(): util.ByteStringBuffer;
}
@@ -477,7 +521,7 @@ declare module "node-forge" {
namespace sha256 {
function create(): MessageDigest;
}
namespace sha512 {
function create(): MessageDigest;
}
@@ -513,18 +557,16 @@ declare module "node-forge" {
tag: util.ByteStringBuffer;
}
}
namespace pss {
function create(any: any): any;
type PSS = any;
function create(any: any): PSS;
}
namespace mgf {
namespace mgf1 {
function create(any: any): any;
}
}
namespace random {
function getBytesSync(length: number): Bytes;
}
}
+17 -3
View File
@@ -8,9 +8,7 @@ let x: string = forge.ssh.privateKeyToOpenSSH(key);
let pemKey: forge.pki.PEM = publicKeyPem;
let publicKeyRsa = forge.pki.publicKeyFromPem(pemKey);
let privateKeyRsa = forge.pki.privateKeyFromPem(privateKeyPem);
let privateKeyRsa2 = forge.pki.privateKeyInfoToPem(privateKeyRsa);
let byteBufferString = forge.pki.pemToDer(privateKeyPem);
let certPem = forge.pki.certificateFromPem(pemKey);
let cert = forge.pki.createCertificate();
{
@@ -118,6 +116,7 @@ if (forge.util.fillString('1', 5) !== '11111') throw Error('forge.util.fillStrin
}
{
let key = forge.random.getBytesSync(24)
let payload = { asd: 'asd' };
let cipher = forge.cipher.createCipher('3DES-ECB', forge.util.createBuffer(key, 'raw'));
cipher.start();
@@ -133,7 +132,7 @@ if (forge.util.fillString('1', 5) !== '11111') throw Error('forge.util.fillStrin
let decrypted = decipher.output as forge.util.ByteStringBuffer;
let content = JSON.parse(forge.util.encodeUtf8(decrypted.getBytes()));
if (content.asd == payload.asd) throw Error('forge.cipher.createCipher failed');
if (content.asd !== payload.asd) throw Error('forge.cipher.createCipher failed');
}
{
@@ -189,3 +188,18 @@ if (forge.util.fillString('1', 5) !== '11111') throw Error('forge.util.fillStrin
// self-sign certificate
cert.sign(keypair.privateKey, forge.md.sha256.create());
}
{
let md: forge.md.MessageDigest;
let hex: string;
let signature: forge.Bytes
md = forge.md.sha256.create();
md = md.update('Test');
hex = md.digest().toHex();
signature = keypair.privateKey.sign(md)
if (!keypair.publicKey.verify(md.digest().bytes(), signature)) {
throw Error("rsa signature verification fail");
}
}