Skip to content

Commit 4a8d584

Browse files
committed
detectAndVerifyAlgorithmの挙動を変更 (エラーにしない)
1 parent 437ee2b commit 4a8d584

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/draft/verify.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export function verifyDraftSignature(parsed: ParsedDraftSignature['value'], publ
66
const publicKey = crypto.createPublicKey(publicKeyPem);
77
try {
88
const detected = detectAndVerifyAlgorithm(parsed.params.algorithm, publicKey);
9+
if (!detected) return false;
910
return crypto.verify(detected.hashAlg, Buffer.from(parsed.signingString), publicKey, Buffer.from(parsed.params.signature, 'base64'));
1011
} catch (e) {
1112
if (errorLogger) errorLogger(e);

src/shared/verify.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import * as crypto from 'node:crypto';
22
import type { SignatureHashAlgorithm } from '../types.js';
33

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}`;
86
}
97

108
/**
119
* ヘッダーのアルゴリズムから鍵とハッシュアルゴリズムを認識する
1210
* 提供されたアルゴリズムと呼び出しの公開鍵の種類が一致しない場合はエラーを投げる
1311
* @param algorithm ヘッダーのアルゴリズム
14-
* @param key 実際の公開鍵
12+
* @param publicKey 実際の公開鍵
1513
*/
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 {
1715
algorithm = algorithm?.toLowerCase();
1816
const realKeyType = publicKey.asymmetricKeyType;
1917

@@ -23,7 +21,8 @@ export function detectAndVerifyAlgorithm(algorithm: string | undefined, publicKe
2321
providedKeyAlgorithm !== realKeyType.toLowerCase() &&
2422
!(providedKeyAlgorithm === 'ecdsa' && realKeyType === 'ec')
2523
) {
26-
throw new SignatureMissmatchWithProvidedAlgorithmError(algorithm, providedKeyAlgorithm, realKeyType);
24+
if (errorLogger) errorLogger(buildErrorMessage(providedKeyAlgorithm, realKeyType, realKeyType));
25+
return null;
2726
}
2827
}
2928

@@ -63,5 +62,7 @@ export function detectAndVerifyAlgorithm(algorithm: string | undefined, publicKe
6362
hashAlg: algoSplitted.length === 1 ? null : algoSplitted[algoSplitted.length - 1] as SignatureHashAlgorithm,
6463
};
6564
}
66-
throw new Error('Algorithm not found');
65+
66+
if (errorLogger) errorLogger('Algorithm is not detected');
67+
return null;
6768
}

0 commit comments

Comments
 (0)