use of java.security.spec.DSAPublicKeySpec in project XobotOS by xamarin.
the class CertPathValidatorUtilities method getNextWorkingKey.
/**
* Return the next working key inheriting DSA parameters if necessary.
* <p>
* This methods inherits DSA parameters from the indexed certificate or
* previous certificates in the certificate chain to the returned
* <code>PublicKey</code>. The list is searched upwards, meaning the end
* certificate is at position 0 and previous certificates are following.
* </p>
* <p>
* If the indexed certificate does not contain a DSA key this method simply
* returns the public key. If the DSA key already contains DSA parameters
* the key is also only returned.
* </p>
*
* @param certs The certification path.
* @param index The index of the certificate which contains the public key
* which should be extended with DSA parameters.
* @return The public key of the certificate in list position
* <code>index</code> extended with DSA parameters if applicable.
* @throws AnnotatedException if DSA parameters cannot be inherited.
*/
protected static PublicKey getNextWorkingKey(List certs, int index) throws CertPathValidatorException {
Certificate cert = (Certificate) certs.get(index);
PublicKey pubKey = cert.getPublicKey();
if (!(pubKey instanceof DSAPublicKey)) {
return pubKey;
}
DSAPublicKey dsaPubKey = (DSAPublicKey) pubKey;
if (dsaPubKey.getParams() != null) {
return dsaPubKey;
}
for (int i = index + 1; i < certs.size(); i++) {
X509Certificate parentCert = (X509Certificate) certs.get(i);
pubKey = parentCert.getPublicKey();
if (!(pubKey instanceof DSAPublicKey)) {
throw new CertPathValidatorException("DSA parameters cannot be inherited from previous certificate.");
}
DSAPublicKey prevDSAPubKey = (DSAPublicKey) pubKey;
if (prevDSAPubKey.getParams() == null) {
continue;
}
DSAParams dsaParams = prevDSAPubKey.getParams();
DSAPublicKeySpec dsaPubKeySpec = new DSAPublicKeySpec(dsaPubKey.getY(), dsaParams.getP(), dsaParams.getQ(), dsaParams.getG());
try {
KeyFactory keyFactory = KeyFactory.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
return keyFactory.generatePublic(dsaPubKeySpec);
} catch (Exception exception) {
throw new RuntimeException(exception.getMessage());
}
}
throw new CertPathValidatorException("DSA parameters cannot be inherited from previous certificate.");
}
use of java.security.spec.DSAPublicKeySpec in project jdk8u_jdk by JetBrains.
the class GenerationTests method getPublicKey.
private static PublicKey getPublicKey(String algo, int keysize) throws Exception {
KeyFactory kf = KeyFactory.getInstance(algo);
KeySpec kspec;
if (algo.equalsIgnoreCase("DSA")) {
if (keysize == 1024) {
kspec = new DSAPublicKeySpec(new BigInteger(DSA_Y), new BigInteger(DSA_P), new BigInteger(DSA_Q), new BigInteger(DSA_G));
} else if (keysize == 2048) {
kspec = new DSAPublicKeySpec(new BigInteger(DSA_2048_Y), new BigInteger(DSA_2048_P), new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
} else
throw new RuntimeException("Unsupported keysize:" + keysize);
} else if (algo.equalsIgnoreCase("RSA")) {
if (keysize == 512) {
kspec = new RSAPublicKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PUB));
} else if (keysize == 1024) {
kspec = new RSAPublicKeySpec(new BigInteger(RSA_1024_MOD), new BigInteger(RSA_PUB));
} else
throw new RuntimeException("Unsupported keysize:" + keysize);
} else
throw new RuntimeException("Unsupported key algorithm " + algo);
return kf.generatePublic(kspec);
}
use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.
the class KeyFactoryTest method testGeneratePublic.
@SuppressWarnings("unchecked")
public void testGeneratePublic() {
KeyFactory factory = null;
try {
factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
assertNotNull(factory);
try {
TestPublicKey key = new TestPublicKey();
TestPublicKeySpec keySpec = new TestPublicKeySpec(key);
PublicKey publicKey = factory.generatePublic(keySpec);
assertNotNull(publicKey);
assertTrue(Arrays.equals(key.encoded, publicKey.getEncoded()));
} catch (InvalidKeySpecException e) {
fail("unexpected exception: " + e);
}
KeySpec[] keySpecs = { new TestPrivateKeySpec(new TestPrivateKey()), null, new DSAPublicKeySpec(null, null, null, null) };
Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class };
for (int i = 0; i < keySpecs.length; i++) {
KeySpec keySpec = keySpecs[i];
String message = "generatePublic(" + (keySpec == null ? "null" : keySpec.toString()) + ")";
try {
PublicKey generatePublic = factory.generatePublic(keySpec);
assertNotNull(generatePublic);
} catch (Exception e) {
checkException(message, e, exceptions[i]);
} finally {
checkException(message, null, exceptions[i]);
}
}
}
use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.
the class SignatureTest method testVerify_SHA1withDSA_Key_Success.
public void testVerify_SHA1withDSA_Key_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("DSA");
DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(DSA_pub, DSA_P, DSA_Q, DSA_G);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
Signature sig = Signature.getInstance("SHA1withDSA");
sig.initVerify(pubKey);
sig.update(Vector2Data);
assertTrue("Signature must verify correctly", sig.verify(SHA1withDSA_Vector2Signature));
}
use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.
the class SignatureTest method testSign_SHA1withDSA_Key_Success.
public void testSign_SHA1withDSA_Key_Success() throws Exception {
KeyFactory kf = KeyFactory.getInstance("DSA");
DSAPrivateKeySpec keySpec = new DSAPrivateKeySpec(DSA_priv, DSA_P, DSA_Q, DSA_G);
PrivateKey privKey = kf.generatePrivate(keySpec);
Signature sig = Signature.getInstance("SHA1withDSA");
sig.initSign(privKey);
sig.update(Vector2Data);
byte[] signature = sig.sign();
assertNotNull("Signature must not be null", signature);
DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(DSA_pub, DSA_P, DSA_Q, DSA_G);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
sig.initVerify(pubKey);
sig.update(Vector2Data);
assertTrue("Signature must verify correctly", sig.verify(signature));
}
Aggregations