mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-27 12:36:14 +01:00
extract public from private key
This commit is contained in:
parent
e22915818c
commit
2bc7ae0da2
@ -161,6 +161,15 @@ describe('NodeCrypto Function Service', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('rsaExtractPublicKey', () => {
|
||||||
|
it('should successfully extract key', async () => {
|
||||||
|
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
|
||||||
|
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
|
||||||
|
const publicKey = await nodeCryptoFunctionService.rsaExtractPublicKey(privKey.buffer);
|
||||||
|
expect(Utils.fromBufferToB64(publicKey)).toBe(RsaPublicKey);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('randomBytes', () => {
|
describe('randomBytes', () => {
|
||||||
it('should make a value of the correct length', async () => {
|
it('should make a value of the correct length', async () => {
|
||||||
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
|
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
|
||||||
|
@ -247,6 +247,15 @@ describe('WebCrypto Function Service', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('rsaExtractPublicKey', () => {
|
||||||
|
it('should successfully extract key', async () => {
|
||||||
|
const cryptoFunctionService = getWebCryptoFunctionService();
|
||||||
|
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
|
||||||
|
const publicKey = await cryptoFunctionService.rsaExtractPublicKey(privKey.buffer);
|
||||||
|
expect(Utils.fromBufferToB64(publicKey)).toBe(RsaPublicKey);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('randomBytes', () => {
|
describe('randomBytes', () => {
|
||||||
it('should make a value of the correct length', async () => {
|
it('should make a value of the correct length', async () => {
|
||||||
const cryptoFunctionService = getWebCryptoFunctionService();
|
const cryptoFunctionService = getWebCryptoFunctionService();
|
||||||
|
@ -16,6 +16,7 @@ export abstract class CryptoFunctionService {
|
|||||||
aesDecryptFast: (parameters: DecryptParameters<ArrayBuffer | string>) => Promise<string>;
|
aesDecryptFast: (parameters: DecryptParameters<ArrayBuffer | string>) => Promise<string>;
|
||||||
aesDecrypt: (data: ArrayBuffer, iv: ArrayBuffer, key: ArrayBuffer) => Promise<ArrayBuffer>;
|
aesDecrypt: (data: ArrayBuffer, iv: ArrayBuffer, key: ArrayBuffer) => Promise<ArrayBuffer>;
|
||||||
rsaEncrypt: (data: ArrayBuffer, publicKey: ArrayBuffer, algorithm: 'sha1' | 'sha256') => Promise<ArrayBuffer>;
|
rsaEncrypt: (data: ArrayBuffer, publicKey: ArrayBuffer, algorithm: 'sha1' | 'sha256') => Promise<ArrayBuffer>;
|
||||||
rsaDecrypt: (data: ArrayBuffer, key: ArrayBuffer, algorithm: 'sha1' | 'sha256') => Promise<ArrayBuffer>;
|
rsaDecrypt: (data: ArrayBuffer, privateKey: ArrayBuffer, algorithm: 'sha1' | 'sha256') => Promise<ArrayBuffer>;
|
||||||
|
rsaExtractPublicKey: (privateKey: ArrayBuffer) => Promise<ArrayBuffer>;
|
||||||
randomBytes: (length: number) => Promise<ArrayBuffer>;
|
randomBytes: (length: number) => Promise<ArrayBuffer>;
|
||||||
}
|
}
|
||||||
|
@ -133,6 +133,16 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
|
|||||||
return Promise.resolve(this.toArrayBuffer(decipher));
|
return Promise.resolve(this.toArrayBuffer(decipher));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async rsaExtractPublicKey(privateKey: ArrayBuffer): Promise<ArrayBuffer> {
|
||||||
|
const privateKeyByteString = Utils.fromBufferToByteString(privateKey);
|
||||||
|
const privateKeyAsn1 = forge.asn1.fromDer(privateKeyByteString);
|
||||||
|
const forgePrivateKey = (forge as any).pki.privateKeyFromAsn1(privateKeyAsn1);
|
||||||
|
const forgePublicKey = (forge.pki as any).setRsaPublicKey(forgePrivateKey.n, forgePrivateKey.e);
|
||||||
|
const publicKeyAsn1 = (forge.pki as any).publicKeyToAsn1(forgePublicKey);
|
||||||
|
const publicKeyByteString = forge.asn1.toDer(publicKeyAsn1).data;
|
||||||
|
return Utils.fromByteStringToArray(publicKeyByteString).buffer;
|
||||||
|
}
|
||||||
|
|
||||||
randomBytes(length: number): Promise<ArrayBuffer> {
|
randomBytes(length: number): Promise<ArrayBuffer> {
|
||||||
return new Promise<ArrayBuffer>((resolve, reject) => {
|
return new Promise<ArrayBuffer>((resolve, reject) => {
|
||||||
crypto.randomBytes(length, (error, bytes) => {
|
crypto.randomBytes(length, (error, bytes) => {
|
||||||
|
@ -204,6 +204,25 @@ export class WebCryptoFunctionService implements CryptoFunctionService {
|
|||||||
return await this.subtle.decrypt(rsaParams, impKey, data);
|
return await this.subtle.decrypt(rsaParams, impKey, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async rsaExtractPublicKey(privateKey: ArrayBuffer): Promise<ArrayBuffer> {
|
||||||
|
const rsaParams = {
|
||||||
|
name: 'RSA-OAEP',
|
||||||
|
// Have to specify some algorithm
|
||||||
|
hash: { name: this.toWebCryptoAlgorithm('sha1') },
|
||||||
|
};
|
||||||
|
const impPrivateKey = await this.subtle.importKey('pkcs8', privateKey, rsaParams, true, ['decrypt']);
|
||||||
|
const jwkPrivateKey = await this.subtle.exportKey('jwk', impPrivateKey);
|
||||||
|
const jwkPublicKeyParams = {
|
||||||
|
kty: 'RSA',
|
||||||
|
e: jwkPrivateKey.e,
|
||||||
|
n: jwkPrivateKey.n,
|
||||||
|
alg: 'RSA-OAEP',
|
||||||
|
ext: true,
|
||||||
|
};
|
||||||
|
const impPublicKey = await this.subtle.importKey('jwk', jwkPublicKeyParams, rsaParams, true, ['encrypt']);
|
||||||
|
return await this.subtle.exportKey('spki', impPublicKey);
|
||||||
|
}
|
||||||
|
|
||||||
randomBytes(length: number): Promise<ArrayBuffer> {
|
randomBytes(length: number): Promise<ArrayBuffer> {
|
||||||
const arr = new Uint8Array(length);
|
const arr = new Uint8Array(length);
|
||||||
this.crypto.getRandomValues(arr);
|
this.crypto.getRandomValues(arr);
|
||||||
|
Loading…
Reference in New Issue
Block a user