Skip to content

Commit a2c70ec

Browse files
author
Ross Nicoll
committed
Add handling of X509Key wrapped EdDSA keys
1 parent 0c5b1b6 commit a2c70ec

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/net/i2p/crypto/eddsa/EdDSAEngine.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
import java.security.Signature;
2323
import java.security.SignatureException;
2424
import java.security.spec.AlgorithmParameterSpec;
25+
import java.security.spec.InvalidKeySpecException;
26+
import java.security.spec.X509EncodedKeySpec;
2527
import java.util.Arrays;
2628

2729
import net.i2p.crypto.eddsa.math.Curve;
2830
import net.i2p.crypto.eddsa.math.GroupElement;
2931
import net.i2p.crypto.eddsa.math.ScalarOps;
32+
import sun.security.x509.X509Key;
3033

3134
/**
3235
* Signing and verification for EdDSA.
@@ -154,6 +157,16 @@ protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException
154157
}
155158
} else if (!key.getParams().getHashAlgorithm().equals(digest.getAlgorithm()))
156159
throw new InvalidKeyException("Key hash algorithm does not match chosen digest");
160+
} else if (publicKey instanceof X509Key) {
161+
// X509Certificate will sometimes contain an X509Key rather than the EdDSAPublicKey itself; the contained
162+
// key is valid but needs to be instanced as an EdDSAPublicKey before it can be used.
163+
EdDSAPublicKey parsedPublicKey;
164+
try {
165+
parsedPublicKey = new EdDSAPublicKey(new X509EncodedKeySpec(publicKey.getEncoded()));
166+
} catch (InvalidKeySpecException ex) {
167+
throw new InvalidKeyException("cannot handle X.509 EdDSA public key: " + publicKey.getAlgorithm());
168+
}
169+
engineInitVerify(parsedPublicKey);
157170
} else {
158171
throw new InvalidKeyException("cannot identify EdDSA public key: " + publicKey.getClass());
159172
}

0 commit comments

Comments
 (0)