Skip to content

Commit bb9ac20

Browse files
committed
Further edDSA clean-up
Remove unused things, and give the EdDSASecurityProviderRegistrar a PublicKeyFactory.
1 parent 34ccc31 commit bb9ac20

6 files changed

Lines changed: 67 additions & 245 deletions

File tree

sshd-common/src/main/java/org/apache/sshd/common/util/security/bouncycastle/BouncyCastlePublicKeyFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import org.apache.sshd.common.util.security.SecurityUtils;
2626
import org.bouncycastle.jcajce.interfaces.EdDSAPrivateKey;
2727

28-
public class BouncyCastlePublicKeyFactory implements PublicKeyFactory {
28+
public final class BouncyCastlePublicKeyFactory implements PublicKeyFactory {
2929

30-
public BouncyCastlePublicKeyFactory() {
30+
public static final PublicKeyFactory INSTANCE = new BouncyCastlePublicKeyFactory();
31+
32+
private BouncyCastlePublicKeyFactory() {
3133
super();
3234
}
3335

sshd-common/src/main/java/org/apache/sshd/common/util/security/bouncycastle/BouncyCastleSecurityProviderRegistrar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public boolean isSupported() {
172172
@Override
173173
public PublicKey getPublicKey(PrivateKey key) {
174174
if (isEnabled() && isEdDSASupported() && key.getClass().getPackage().getName().startsWith("org.bouncycastle.")) {
175-
return new BouncyCastlePublicKeyFactory().getPublicKey(key);
175+
return BouncyCastlePublicKeyFactory.INSTANCE.getPublicKey(key);
176176
}
177177
return super.getPublicKey(key);
178178
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sshd.common.util.security.eddsa;
20+
21+
import java.security.GeneralSecurityException;
22+
import java.security.KeyFactory;
23+
import java.security.PrivateKey;
24+
import java.security.PublicKey;
25+
26+
import net.i2p.crypto.eddsa.EdDSAPrivateKey;
27+
import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
28+
import org.apache.sshd.common.util.ValidateUtils;
29+
import org.apache.sshd.common.util.security.PublicKeyFactory;
30+
import org.apache.sshd.common.util.security.SecurityUtils;
31+
32+
/**
33+
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
34+
*/
35+
public final class EdDSAPublicKeyFactory implements PublicKeyFactory {
36+
37+
public static final PublicKeyFactory INSTANCE = new EdDSAPublicKeyFactory();
38+
39+
private EdDSAPublicKeyFactory() {
40+
super();
41+
}
42+
43+
@Override
44+
public PublicKey getPublicKey(PrivateKey key) {
45+
ValidateUtils.checkTrue(SecurityUtils.isEDDSACurveSupported(), SecurityUtils.EDDSA + " not supported");
46+
if (!(key instanceof EdDSAPrivateKey)) {
47+
return null;
48+
}
49+
50+
EdDSAPrivateKey prvKey = (EdDSAPrivateKey) key;
51+
EdDSAPublicKeySpec keySpec = new EdDSAPublicKeySpec(prvKey.getAbyte(), prvKey.getParams());
52+
KeyFactory factory;
53+
try {
54+
factory = SecurityUtils.getKeyFactory(SecurityUtils.ED25519);
55+
return factory.generatePublic(keySpec);
56+
} catch (GeneralSecurityException e) {
57+
return null;
58+
}
59+
}
60+
61+
}

sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderRegistrar.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,7 @@ protected String effectiveAlgorithm(String originalAlgorithm) {
134134
public PublicKey getPublicKey(PrivateKey key) {
135135
if (isEnabled() && isSupported() && "EdDSA".equals(key.getAlgorithm())
136136
&& key.getClass().getPackage().getName().startsWith("net.i2p.")) {
137-
try {
138-
return EdDSASecurityProviderUtils.recoverEDDSAPublicKey(key);
139-
} catch (GeneralSecurityException e) {
140-
return null;
141-
}
137+
return EdDSAPublicKeyFactory.INSTANCE.getPublicKey(key);
142138
}
143139
return super.getPublicKey(key);
144140
}

sshd-common/src/main/java/org/apache/sshd/common/util/security/eddsa/EdDSASecurityProviderUtils.java

Lines changed: 0 additions & 154 deletions
This file was deleted.

sshd-common/src/test/java/org/apache/sshd/common/signature/SignaturesDevelopment.java

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)