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
}
}
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());
}
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;
}
}
Aggregations