Search in sources :

Example 66 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project karaf by apache.

the class PublicKeyLoginModuleTest method testUnknownKeyEC.

@Test
public void testUnknownKeyEC() throws Exception {
    Properties options = getLoginModuleOptions();
    PublickeyLoginModule module = new PublickeyLoginModule();
    Subject subject = new Subject();
    String x = "2145382594999641569030545431803328907795332312211583318014254232969998637145";
    String y = "52282205184471090919696434245736603165041352971927370430120381994413951213993";
    KeyFactory keyFactory = KeyFactory.getInstance("EC");
    AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
    parameters.init(new ECGenParameterSpec("secp256r1"));
    ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
    ECPoint pubPoint = new ECPoint(new BigInteger(x), new BigInteger(y));
    KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
    PublicKey publicKey = keyFactory.generatePublic(keySpec);
    module.initialize(subject, new NamePubkeyCallbackHandler("ec", publicKey), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    try {
        module.login();
        fail("Failure expected on an unknown user");
    } catch (FailedLoginException ex) {
    // expected
    }
}
Also used : PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) Properties(org.apache.felix.utils.properties.Properties) ECPoint(java.security.spec.ECPoint) Subject(javax.security.auth.Subject) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) FailedLoginException(javax.security.auth.login.FailedLoginException) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) KeyFactory(java.security.KeyFactory) AlgorithmParameters(java.security.AlgorithmParameters) Test(org.junit.Test)

Aggregations

ECGenParameterSpec (java.security.spec.ECGenParameterSpec)66 KeyPairGenerator (java.security.KeyPairGenerator)31 KeyPair (java.security.KeyPair)23 AlgorithmParameters (java.security.AlgorithmParameters)22 PublicKey (java.security.PublicKey)19 BigInteger (java.math.BigInteger)18 ECPublicKey (java.security.interfaces.ECPublicKey)17 ECParameterSpec (java.security.spec.ECParameterSpec)14 ECPoint (java.security.spec.ECPoint)14 Test (org.junit.Test)13 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)12 KeySpec (java.security.spec.KeySpec)10 ECPrivateKey (java.security.interfaces.ECPrivateKey)9 PrivateKey (java.security.PrivateKey)8 SecureRandom (java.security.SecureRandom)8 Cipher (javax.crypto.Cipher)8 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)7 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)7