-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(config): split decryption functions (#28571)
- Loading branch information
Showing
7 changed files
with
99 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** istanbul ignore file */ | ||
import crypto from 'node:crypto'; | ||
import { logger } from '../../logger'; | ||
|
||
export function tryDecryptPublicKeyPKCS1( | ||
privateKey: string, | ||
encryptedStr: string, | ||
): string | null { | ||
let decryptedStr: string | null = null; | ||
try { | ||
decryptedStr = crypto | ||
.privateDecrypt( | ||
{ | ||
key: privateKey, | ||
padding: crypto.constants.RSA_PKCS1_PADDING, | ||
}, | ||
Buffer.from(encryptedStr, 'base64'), | ||
) | ||
.toString(); | ||
} catch (err) { | ||
logger.debug('Could not decrypt using PKCS1 padding'); | ||
} | ||
return decryptedStr; | ||
} | ||
|
||
export function tryDecryptPublicKeyDefault( | ||
privateKey: string, | ||
encryptedStr: string, | ||
): string | null { | ||
let decryptedStr: string | null = null; | ||
try { | ||
decryptedStr = crypto | ||
.privateDecrypt(privateKey, Buffer.from(encryptedStr, 'base64')) | ||
.toString(); | ||
logger.debug('Decrypted config using default padding'); | ||
} catch (err) { | ||
logger.debug('Could not decrypt using default padding'); | ||
} | ||
return decryptedStr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as openpgp from 'openpgp'; | ||
import { logger } from '../../logger'; | ||
import { regEx } from '../../util/regex'; | ||
|
||
export async function tryDecryptOpenPgp( | ||
privateKey: string, | ||
encryptedStr: string, | ||
): Promise<string | null> { | ||
if (encryptedStr.length < 500) { | ||
// optimization during transition of public key -> pgp | ||
return null; | ||
} | ||
try { | ||
const pk = await openpgp.readPrivateKey({ | ||
// prettier-ignore | ||
armoredKey: privateKey.replace(regEx(/\n[ \t]+/g), '\n'), // little massage to help a common problem | ||
}); | ||
const startBlock = '-----BEGIN PGP MESSAGE-----\n\n'; | ||
const endBlock = '\n-----END PGP MESSAGE-----'; | ||
let armoredMessage = encryptedStr.trim(); | ||
if (!armoredMessage.startsWith(startBlock)) { | ||
armoredMessage = `${startBlock}${armoredMessage}`; | ||
} | ||
if (!armoredMessage.endsWith(endBlock)) { | ||
armoredMessage = `${armoredMessage}${endBlock}`; | ||
} | ||
const message = await openpgp.readMessage({ | ||
armoredMessage, | ||
}); | ||
const { data } = await openpgp.decrypt({ | ||
message, | ||
decryptionKeys: pk, | ||
}); | ||
logger.debug('Decrypted config using openpgp'); | ||
return data; | ||
} catch (err) { | ||
logger.debug({ err }, 'Could not decrypt using openpgp'); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.