1- import crypto from 'node:crypto' ;
21import is from '@sindresorhus/is' ;
3- import * as openpgp from 'openpgp' ;
42import { logger } from '../logger' ;
53import { maskToken } from '../util/mask' ;
64import { regEx } from '../util/regex' ;
75import { addSecretForSanitizing } from '../util/sanitize' ;
86import { ensureTrailingSlash } from '../util/url' ;
7+ import {
8+ tryDecryptPublicKeyDefault ,
9+ tryDecryptPublicKeyPKCS1 ,
10+ } from './decrypt/legacy' ;
11+ import { tryDecryptOpenPgp } from './decrypt/openpgp' ;
912import { GlobalConfig } from './global' ;
1013import { DecryptedObject } from './schema' ;
1114import type { RenovateConfig } from './types' ;
1215
13- export async function tryDecryptPgp (
14- privateKey : string ,
15- encryptedStr : string ,
16- ) : Promise < string | null > {
17- if ( encryptedStr . length < 500 ) {
18- // optimization during transition of public key -> pgp
19- return null ;
20- }
21- try {
22- const pk = await openpgp . readPrivateKey ( {
23- // prettier-ignore
24- armoredKey : privateKey . replace ( regEx ( / \n [ \t ] + / g) , '\n' ) , // little massage to help a common problem
25- } ) ;
26- const startBlock = '-----BEGIN PGP MESSAGE-----\n\n' ;
27- const endBlock = '\n-----END PGP MESSAGE-----' ;
28- let armoredMessage = encryptedStr . trim ( ) ;
29- if ( ! armoredMessage . startsWith ( startBlock ) ) {
30- armoredMessage = `${ startBlock } ${ armoredMessage } ` ;
31- }
32- if ( ! armoredMessage . endsWith ( endBlock ) ) {
33- armoredMessage = `${ armoredMessage } ${ endBlock } ` ;
34- }
35- const message = await openpgp . readMessage ( {
36- armoredMessage,
37- } ) ;
38- const { data } = await openpgp . decrypt ( {
39- message,
40- decryptionKeys : pk ,
41- } ) ;
42- logger . debug ( 'Decrypted config using openpgp' ) ;
43- return data ;
44- } catch ( err ) {
45- logger . debug ( { err } , 'Could not decrypt using openpgp' ) ;
46- return null ;
47- }
48- }
49-
50- export function tryDecryptPublicKeyDefault (
51- privateKey : string ,
52- encryptedStr : string ,
53- ) : string | null {
54- let decryptedStr : string | null = null ;
55- try {
56- decryptedStr = crypto
57- . privateDecrypt ( privateKey , Buffer . from ( encryptedStr , 'base64' ) )
58- . toString ( ) ;
59- logger . debug ( 'Decrypted config using default padding' ) ;
60- } catch ( err ) {
61- logger . debug ( 'Could not decrypt using default padding' ) ;
62- }
63- return decryptedStr ;
64- }
65-
66- export function tryDecryptPublicKeyPKCS1 (
67- privateKey : string ,
68- encryptedStr : string ,
69- ) : string | null {
70- let decryptedStr : string | null = null ;
71- try {
72- decryptedStr = crypto
73- . privateDecrypt (
74- {
75- key : privateKey ,
76- padding : crypto . constants . RSA_PKCS1_PADDING ,
77- } ,
78- Buffer . from ( encryptedStr , 'base64' ) ,
79- )
80- . toString ( ) ;
81- } catch ( err ) {
82- logger . debug ( 'Could not decrypt using PKCS1 padding' ) ;
83- }
84- return decryptedStr ;
85- }
86-
8716export async function tryDecrypt (
8817 privateKey : string ,
8918 encryptedStr : string ,
@@ -92,7 +21,7 @@ export async function tryDecrypt(
9221) : Promise < string | null > {
9322 let decryptedStr : string | null = null ;
9423 if ( privateKey ?. startsWith ( '-----BEGIN PGP PRIVATE KEY BLOCK-----' ) ) {
95- const decryptedObjStr = await tryDecryptPgp ( privateKey , encryptedStr ) ;
24+ const decryptedObjStr = await tryDecryptOpenPgp ( privateKey , encryptedStr ) ;
9625 if ( decryptedObjStr ) {
9726 decryptedStr = validateDecryptedValue ( decryptedObjStr , repository ) ;
9827 }
0 commit comments