1
1
import * as crypto from 'node:crypto' ;
2
2
import type { SignatureHashAlgorithm } from '../types.js' ;
3
3
4
- export class SignatureMissmatchWithProvidedAlgorithmError extends Error {
5
- constructor ( providedAlgorithm : string , detectedAlgorithm : string , realKeyType : string ) {
6
- super ( `Provided algorithm does not match the public key type: provided=${ detectedAlgorithm } (${ providedAlgorithm } }, real=${ realKeyType } ` ) ;
7
- }
4
+ function buildErrorMessage ( providedAlgorithm : string , detectedAlgorithm : string , realKeyType : string ) {
5
+ return `Provided algorithm does not match the public key type: provided=${ detectedAlgorithm } (${ providedAlgorithm } }, real=${ realKeyType } ` ;
8
6
}
9
7
10
8
/**
11
9
* ヘッダーのアルゴリズムから鍵とハッシュアルゴリズムを認識する
12
10
* 提供されたアルゴリズムと呼び出しの公開鍵の種類が一致しない場合はエラーを投げる
13
11
* @param algorithm ヘッダーのアルゴリズム
14
- * @param key 実際の公開鍵
12
+ * @param publicKey 実際の公開鍵
15
13
*/
16
- export function detectAndVerifyAlgorithm ( algorithm : string | undefined , publicKey : crypto . KeyObject ) : { keyAlg : crypto . KeyType , hashAlg : SignatureHashAlgorithm | null } {
14
+ export function detectAndVerifyAlgorithm ( algorithm : string | undefined , publicKey : crypto . KeyObject , errorLogger ?: ( ( message : any ) => any ) ) : { keyAlg : crypto . KeyType , hashAlg : SignatureHashAlgorithm | null } | null {
17
15
algorithm = algorithm ?. toLowerCase ( ) ;
18
16
const realKeyType = publicKey . asymmetricKeyType ;
19
17
@@ -23,7 +21,8 @@ export function detectAndVerifyAlgorithm(algorithm: string | undefined, publicKe
23
21
providedKeyAlgorithm !== realKeyType . toLowerCase ( ) &&
24
22
! ( providedKeyAlgorithm === 'ecdsa' && realKeyType === 'ec' )
25
23
) {
26
- throw new SignatureMissmatchWithProvidedAlgorithmError ( algorithm , providedKeyAlgorithm , realKeyType ) ;
24
+ if ( errorLogger ) errorLogger ( buildErrorMessage ( providedKeyAlgorithm , realKeyType , realKeyType ) ) ;
25
+ return null ;
27
26
}
28
27
}
29
28
@@ -63,5 +62,7 @@ export function detectAndVerifyAlgorithm(algorithm: string | undefined, publicKe
63
62
hashAlg : algoSplitted . length === 1 ? null : algoSplitted [ algoSplitted . length - 1 ] as SignatureHashAlgorithm ,
64
63
} ;
65
64
}
66
- throw new Error ( 'Algorithm not found' ) ;
65
+
66
+ if ( errorLogger ) errorLogger ( 'Algorithm is not detected' ) ;
67
+ return null ;
67
68
}
0 commit comments