( data: Buffer, description: string = '' )
| 22 | |
| 23 | // Exported for testing. |
| 24 | export async function parseMaybeEncryptedKey( |
| 25 | data: Buffer, |
| 26 | description: string = '' |
| 27 | ): Promise<KeyObject> { |
| 28 | // Read unencrypted private key. |
| 29 | try { |
| 30 | return parsePemKey(data); |
| 31 | } catch { |
| 32 | infoLog('The provided key is probably encrypted. Decrypting attempt...'); |
| 33 | } |
| 34 | |
| 35 | const hasEnvVarSet = |
| 36 | process.env.WEB_BUNDLE_SIGNING_PASSPHRASE && |
| 37 | process.env.WEB_BUNDLE_SIGNING_PASSPHRASE !== ''; |
| 38 | |
| 39 | // Read encrypted private key. |
| 40 | try { |
| 41 | return parsePemKey( |
| 42 | data, |
| 43 | hasEnvVarSet |
| 44 | ? process.env.WEB_BUNDLE_SIGNING_PASSPHRASE |
| 45 | : await readPassphrase(description) |
| 46 | ); |
| 47 | } catch (err) { |
| 48 | throw Error( |
| 49 | `Failed decrypting encrypted private key with passphrase read from ${ |
| 50 | hasEnvVarSet |
| 51 | ? '`WEB_BUNDLE_SIGNING_PASSPHRASE` environment variable' |
| 52 | : 'prompt' |
| 53 | }`, |
| 54 | { cause: err } |
| 55 | ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // A helper function that can be used to get the passphrase to decrypt a |
| 60 | // password-decrypted private key from user. |
no test coverage detected