Skip to content

Commit 6d9129a

Browse files
committed
Revert "Simplify parsing of RSA COSE public keys"
This reverts commit f207b27.
1 parent f207b27 commit 6d9129a

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

webauthn-server-core/src/main/java/com/yubico/webauthn/WebAuthnCodecs.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
import com.yubico.webauthn.data.ByteArray;
3232
import com.yubico.webauthn.data.COSEAlgorithmIdentifier;
3333
import java.io.IOException;
34+
import java.math.BigInteger;
3435
import java.security.KeyFactory;
3536
import java.security.NoSuchAlgorithmException;
3637
import java.security.PublicKey;
3738
import java.security.interfaces.ECPublicKey;
3839
import java.security.spec.InvalidKeySpecException;
40+
import java.security.spec.RSAPublicKeySpec;
3941
import java.security.spec.X509EncodedKeySpec;
4042
import java.util.Arrays;
4143
import java.util.HashMap;
@@ -123,17 +125,29 @@ static PublicKey importCosePublicKey(ByteArray key)
123125
final int kty = cose.get(CBORObject.FromObject(1)).AsInt32();
124126
switch (kty) {
125127
case 1:
126-
// COSE-JAVA is hardcoded to ed25519-java provider ("EdDSA") which would require an
127-
// additional dependency to parse EdDSA keys via the OneKey constructor
128128
return importCoseEdDsaPublicKey(cose);
129-
case 2: // Fall through
129+
case 2:
130+
return importCoseP256PublicKey(cose);
130131
case 3:
131-
return new OneKey(cose).AsPublicKey();
132+
return importCoseRsaPublicKey(cose);
132133
default:
133134
throw new IllegalArgumentException("Unsupported key type: " + kty);
134135
}
135136
}
136137

138+
private static PublicKey importCoseRsaPublicKey(CBORObject cose)
139+
throws NoSuchAlgorithmException, InvalidKeySpecException {
140+
RSAPublicKeySpec spec =
141+
new RSAPublicKeySpec(
142+
new BigInteger(1, cose.get(CBORObject.FromObject(-1)).GetByteString()),
143+
new BigInteger(1, cose.get(CBORObject.FromObject(-2)).GetByteString()));
144+
return KeyFactory.getInstance("RSA").generatePublic(spec);
145+
}
146+
147+
private static ECPublicKey importCoseP256PublicKey(CBORObject cose) throws CoseException {
148+
return (ECPublicKey) new OneKey(cose).AsPublicKey();
149+
}
150+
137151
private static PublicKey importCoseEdDsaPublicKey(CBORObject cose)
138152
throws InvalidKeySpecException, NoSuchAlgorithmException {
139153
final int curveId = cose.get(CBORObject.FromObject(-1)).AsInt32();

0 commit comments

Comments
 (0)