|
31 | 31 | import com.yubico.webauthn.data.ByteArray;
|
32 | 32 | import com.yubico.webauthn.data.COSEAlgorithmIdentifier;
|
33 | 33 | import java.io.IOException;
|
| 34 | +import java.math.BigInteger; |
34 | 35 | import java.security.KeyFactory;
|
35 | 36 | import java.security.NoSuchAlgorithmException;
|
36 | 37 | import java.security.PublicKey;
|
37 | 38 | import java.security.interfaces.ECPublicKey;
|
38 | 39 | import java.security.spec.InvalidKeySpecException;
|
| 40 | +import java.security.spec.RSAPublicKeySpec; |
39 | 41 | import java.security.spec.X509EncodedKeySpec;
|
40 | 42 | import java.util.Arrays;
|
41 | 43 | import java.util.HashMap;
|
@@ -123,17 +125,29 @@ static PublicKey importCosePublicKey(ByteArray key)
|
123 | 125 | final int kty = cose.get(CBORObject.FromObject(1)).AsInt32();
|
124 | 126 | switch (kty) {
|
125 | 127 | 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 |
128 | 128 | return importCoseEdDsaPublicKey(cose);
|
129 |
| - case 2: // Fall through |
| 129 | + case 2: |
| 130 | + return importCoseP256PublicKey(cose); |
130 | 131 | case 3:
|
131 |
| - return new OneKey(cose).AsPublicKey(); |
| 132 | + return importCoseRsaPublicKey(cose); |
132 | 133 | default:
|
133 | 134 | throw new IllegalArgumentException("Unsupported key type: " + kty);
|
134 | 135 | }
|
135 | 136 | }
|
136 | 137 |
|
| 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 | + |
137 | 151 | private static PublicKey importCoseEdDsaPublicKey(CBORObject cose)
|
138 | 152 | throws InvalidKeySpecException, NoSuchAlgorithmException {
|
139 | 153 | final int curveId = cose.get(CBORObject.FromObject(-1)).AsInt32();
|
|
0 commit comments