Skip to content

Commit cc0ab53

Browse files
committed
first cut of support for FIDO2 keys.
1 parent 7f2e5ca commit cc0ab53

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

core/src/main/java/org/bouncycastle/crypto/util/OpenSSHPublicKeyUtil.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ private OpenSSHPublicKeyUtil()
3030
private static final String ED_25519 = "ssh-ed25519";
3131
private static final String DSS = "ssh-dss";
3232

33+
private static final String FIDO2_EC_P256 = "[email protected]";
34+
private static final String FIDO_ED_25519 = "[email protected]";
35+
3336
/**
3437
* Parse a public key.
3538
* <p>
@@ -167,6 +170,29 @@ else if (magic.startsWith(ECDSA))
167170
curve.decodePoint(pointRaw),
168171
new ECNamedDomainParameters(oid, x9ECParameters));
169172
}
173+
else if (magic.equals(FIDO2_EC_P256))
174+
{
175+
String curveName = buffer.readString();
176+
177+
ASN1ObjectIdentifier oid = SSHNamedCurves.getByName(curveName);
178+
X9ECParameters x9ECParameters = SSHNamedCurves.getParameters(oid);
179+
180+
if (x9ECParameters == null)
181+
{
182+
throw new IllegalStateException("unable to find curve for " + magic + " using curve name " + curveName);
183+
}
184+
185+
ECCurve curve = x9ECParameters.getCurve();
186+
187+
byte[] pointRaw = buffer.readBlock();
188+
189+
// TODO: at the moment we have no use for this, but it's there.
190+
String application = buffer.readString();
191+
192+
result = new ECPublicKeyParameters(
193+
curve.decodePoint(pointRaw),
194+
new ECNamedDomainParameters(oid, x9ECParameters));
195+
}
170196
else if (ED_25519.equals(magic))
171197
{
172198
byte[] pubKeyBytes = buffer.readBlock();
@@ -177,6 +203,19 @@ else if (ED_25519.equals(magic))
177203

178204
result = new Ed25519PublicKeyParameters(pubKeyBytes, 0);
179205
}
206+
else if (FIDO2_EC_P256.equals(magic))
207+
{
208+
byte[] pubKeyBytes = buffer.readBlock();
209+
if (pubKeyBytes.length != Ed25519PublicKeyParameters.KEY_SIZE)
210+
{
211+
throw new IllegalStateException("public key value of wrong length");
212+
}
213+
214+
// TODO: at the moment we have no use for this, but it's there.
215+
String application = buffer.readString();
216+
217+
result = new Ed25519PublicKeyParameters(pubKeyBytes, 0);
218+
}
180219

181220
if (result == null)
182221
{

core/src/test/java/org/bouncycastle/crypto/test/OpenSSHKeyParsingTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ public void testECDSA_curvesFromSSHKeyGen()
294294
doECSigTest(new ECPublicKeyParameters(q, privKey.getParameters()), privKey);
295295
}
296296

297-
298297
for (int i = 0; i != pairs.length; i++)
299298
{
300299
String[] pair = pairs[i];
@@ -335,6 +334,14 @@ public void testECDSA_curvesFromSSHKeyGen()
335334

336335
}
337336

337+
private void testFido2Keys()
338+
{
339+
// P-256 ECDSA Key
340+
byte[] decode = Base64.decode("AAAAInNrLWVjZHNhLXNoYTItbmlzdHAyNTZAb3BlbnNzaC5jb20AAAAIbmlzdHAyNTYAAABBBPnfX2RzzEvD5CEX/0G3LLXrDWjrir9jZ2omSoxNyNT44cSiOP2v/WodnYpQdJsLIZn5bGNI0UxzxTuFzdizrWkAAAAEc3NoOg==");
341+
342+
CipherParameters xpubSpec = OpenSSHPublicKeyUtil.parsePublicKey(decode);
343+
}
344+
338345
private void doECSigTest(CipherParameters pubSpec, CipherParameters privSpec)
339346
{
340347
ECDSASigner signer = new ECDSASigner();
@@ -470,6 +477,7 @@ public void performTest()
470477
testRSA();
471478
testED25519();
472479
testFailures();
480+
testFido2Keys();
473481
}
474482

475483
public void testRSA()

0 commit comments

Comments
 (0)