Skip to content

Commit a474d13

Browse files
nindanaotoclaude
andcommitted
FIDO2: add regression tests for SK key algorithm dispatch
Add tests that use realistic algorithm names ("Ed25519" and "EC") matching what real SkEd25519PublicKey and SkEcdsaPublicKey return. These reproduce the bug where isEd25519Key() matched SK Ed25519 keys before the instanceof SkPublicKey check, causing DER encoding failure. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 691cd62 commit a474d13

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

src/test/java/com/trilead/ssh2/auth/AuthenticationManagerSkKeyTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ public class AuthenticationManagerSkKeyTest {
5353
*/
5454
static class TestSkPublicKey implements SkPublicKey {
5555
private final String keyType;
56+
private final String algorithm;
5657
private final String application;
5758
private final byte[] keyData;
5859

5960
TestSkPublicKey(String keyType, String application, byte[] keyData) {
61+
this(keyType, keyType, application, keyData);
62+
}
63+
64+
TestSkPublicKey(String keyType, String algorithm, String application, byte[] keyData) {
6065
this.keyType = keyType;
66+
this.algorithm = algorithm;
6167
this.application = application;
6268
this.keyData = keyData.clone();
6369
}
@@ -79,7 +85,7 @@ public byte[] getKeyData() {
7985

8086
@Override
8187
public String getAlgorithm() {
82-
return keyType;
88+
return algorithm;
8389
}
8490

8591
@Override
@@ -238,6 +244,45 @@ public void authenticateSkKey_SendsAuthenticationRequest() throws Exception {
238244
"Authentication should send at least one message");
239245
}
240246

247+
/**
248+
* Regression test: SK Ed25519 key with "Ed25519" algorithm must use the SK
249+
* code path, not the generic Ed25519 path. The real SkEd25519PublicKey returns
250+
* "Ed25519" from getAlgorithm(), which matches isEd25519Key(). Without the
251+
* fix, this causes Ed25519Verify.encodePublicKey() to attempt DER decoding
252+
* and fail with InvalidKeySpecException.
253+
*/
254+
@Test
255+
public void authenticateSkEd25519Key_WithEd25519Algorithm_UsesSkPath() throws Exception {
256+
TestSkPublicKey skKey = new TestSkPublicKey(SK_ED25519_KEY_TYPE, "Ed25519", DEFAULT_APPLICATION, TEST_KEY_DATA);
257+
TestSignatureProxy signatureProxy = new TestSignatureProxy(skKey, TEST_SIGNATURE);
258+
259+
setupMocksForAuthentication();
260+
setupMockForAuthSuccess();
261+
262+
authManager.authenticatePublicKey(TEST_USER, signatureProxy);
263+
264+
assertEquals(SignatureProxy.SHA512, signatureProxy.getLastHashAlgorithm(),
265+
"SK Ed25519 key with Ed25519 algorithm should use SK path with SHA512");
266+
}
267+
268+
/**
269+
* Regression test: SK ECDSA key with "EC" algorithm must use the SK
270+
* code path, not the generic ECDSA path.
271+
*/
272+
@Test
273+
public void authenticateSkEcdsaKey_WithEcAlgorithm_UsesSkPath() throws Exception {
274+
TestSkPublicKey skKey = new TestSkPublicKey(SK_ECDSA_KEY_TYPE, "EC", DEFAULT_APPLICATION, TEST_KEY_DATA);
275+
TestSignatureProxy signatureProxy = new TestSignatureProxy(skKey, TEST_SIGNATURE);
276+
277+
setupMocksForAuthentication();
278+
setupMockForAuthSuccess();
279+
280+
authManager.authenticatePublicKey(TEST_USER, signatureProxy);
281+
282+
assertEquals(SignatureProxy.SHA256, signatureProxy.getLastHashAlgorithm(),
283+
"SK ECDSA key with EC algorithm should use SK path with SHA256");
284+
}
285+
241286
private void setupMocksForAuthentication() throws IOException {
242287
lenient().when(tm.getSessionIdentifier()).thenReturn(TEST_SESSION_ID);
243288
lenient().when(tm.getExtensionInfo()).thenReturn(extensionInfo);

0 commit comments

Comments
 (0)