Search in sources :

Example 61 with DSAPublicKeySpec

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

the class PublicKeyLoginModuleTest method testUnknownKeyDSA.

@Test
public void testUnknownKeyDSA() throws Exception {
    Properties options = getLoginModuleOptions();
    PublickeyLoginModule module = new PublickeyLoginModule();
    Subject subject = new Subject();
    String p = "1076175237625726563105954460741409330556298182412863930703571469202992312952487088821612089126846931217220" + "139938550642040962241586994856559462488140821681403960733982209827487135132210000913512532065787125116985685638" + "40437219296134522589816052156357553531846010339651017908589163855315552516201352809575855397";
    String q = "918380515194943729419256231914804453973955269349";
    String g = "5928865413019314795162062081939159959737363875586187627523617102819491716184351195073908492559564825805562" + "104476892066919492044841627907376461274343797017375757242038772707578284292374846844427026690399002493750530347" + "2378225083646830569532678306021077676137269211638266431262139218141967811197461432032698462";
    String y = "3780682190459260799543888842390974417268312111951424991203659597814001671832656608276823896973755971735795" + "130565245682634187551545737028902938478313465290457154458005480679650487421678748598551351730312164280338152996" + "0448119336850459047721615478019482431582683540283279032651976075781966545889409150149549267";
    // Generate a PublicKey using the known values
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec publicKeySpec = new DSAPublicKeySpec(new BigInteger(y), new BigInteger(p), new BigInteger(q), new BigInteger(g));
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
    module.initialize(subject, new NamePubkeyCallbackHandler("dsa", 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 : FailedLoginException(javax.security.auth.login.FailedLoginException) PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) BigInteger(java.math.BigInteger) Properties(org.apache.felix.utils.properties.Properties) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) Subject(javax.security.auth.Subject) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) Test(org.junit.Test)

Example 62 with DSAPublicKeySpec

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

the class PublicKeyLoginModuleTest method testDSALogin.

@Test
public void testDSALogin() throws Exception {
    Properties options = getLoginModuleOptions();
    PublickeyLoginModule module = new PublickeyLoginModule();
    Subject subject = new Subject();
    String p = "1076175237625726563105954460741409330556298182412863930703571469202992312952487088821612089126846931217220" + "139938550642040962241586994856559462488140821681403960733982209827487135132210000913512532065787125116985685638" + "40437219296134522589816052156357553531846010339651017908589163855315552516201352809575855397";
    String q = "918380515194943729419256231914804453973955269349";
    String g = "5928865413019314795162062081939159959737363875586187627523617102819491716184351195073908492559564825805562" + "104476892066919492044841627907376461274343797017375757242038772707578284292374846844427026690399002493750530347" + "2378225083646830569532678306021077676137269211638266431262139218141967811197461432032698462";
    String y = "3780682190459260799543888842390974417268312111951424991203659597814001671832656608276823896973755971735795" + "130565245682634187551545737028902938478313465290457154458005480679650487421678748598551351730312164280338152996" + "0448119336850459047721615478019482431582683540283279032651976075781966545889409150149549269";
    // Generate a PublicKey using the known values
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec publicKeySpec = new DSAPublicKeySpec(new BigInteger(y), new BigInteger(p), new BigInteger(q), new BigInteger(g));
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
    module.initialize(subject, new NamePubkeyCallbackHandler("dsa", publicKey), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertTrue(module.login());
    assertTrue(module.commit());
    assertFalse(subject.getPrincipals().isEmpty());
    assertThat("dsa", isIn(names(subject.getPrincipals(UserPrincipal.class))));
    // We didn't configure any roles
    assertTrue(names(subject.getPrincipals(RolePrincipal.class)).isEmpty());
    assertTrue(module.logout());
    assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
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) BigInteger(java.math.BigInteger) Properties(org.apache.felix.utils.properties.Properties) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Subject(javax.security.auth.Subject) KeyFactory(java.security.KeyFactory) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) Test(org.junit.Test)

Example 63 with DSAPublicKeySpec

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

the class KeyPairLoader method convertPrivateToPublicKey.

private static PublicKey convertPrivateToPublicKey(PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    if (privateKey instanceof RSAPrivateCrtKey) {
        KeySpec keySpec = new RSAPublicKeySpec(((RSAPrivateCrtKey) privateKey).getModulus(), ((RSAPrivateCrtKey) privateKey).getPublicExponent());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    } else if (privateKey instanceof ECPrivateKey) {
        ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
        // Derive the public point by multiplying the generator by the private value
        ECParameterSpec paramSpec = EC5Util.convertSpec(ecPrivateKey.getParams());
        ECPoint q = paramSpec.getG().multiply(ecPrivateKey.getS());
        KeySpec keySpec = new ECPublicKeySpec(q, paramSpec);
        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        return keyFactory.generatePublic(keySpec);
    } else if (privateKey instanceof DSAPrivateKey) {
        DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
        BigInteger q = dsaPrivateKey.getParams().getQ();
        BigInteger p = dsaPrivateKey.getParams().getP();
        KeySpec keySpec = new DSAPublicKeySpec(q.modPow(dsaPrivateKey.getX(), p), p, q, dsaPrivateKey.getParams().getG());
        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        return keyFactory.generatePublic(keySpec);
    } else {
        LOGGER.warn("Unable to convert private key to public key. Only RSA, DSA + ECDSA supported");
        return null;
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(org.bouncycastle.math.ec.ECPoint) KeyFactory(java.security.KeyFactory) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)63 BigInteger (java.math.BigInteger)45 KeyFactory (java.security.KeyFactory)37 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)23 PublicKey (java.security.PublicKey)22 DSAPublicKey (java.security.interfaces.DSAPublicKey)21 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)19 KeySpec (java.security.spec.KeySpec)19 DSAParams (java.security.interfaces.DSAParams)17 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)17 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 PrivateKey (java.security.PrivateKey)10 GeneralSecurityException (java.security.GeneralSecurityException)9 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)9 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)9 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)9 IOException (java.io.IOException)7 CertPathValidatorException (java.security.cert.CertPathValidatorException)7 InvalidKeyException (java.security.InvalidKeyException)5 KeyPair (java.security.KeyPair)5