use of java.security.interfaces.DSAParams in project robovm by robovm.
the class DSAKeyPairGeneratorTest method test_DSAKeyPairGenerator01.
/**
* java.security.interfaces.DSAKeyPairGenerator
* #initialize(DSAParams params, SecureRandom random)
*/
public void test_DSAKeyPairGenerator01() {
DSAParams dsaParams = new DSAParameterSpec(p, q, g);
SecureRandom random = null;
MyDSA dsa = new MyDSA(dsaParams);
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
fail("Unexpected exception for SecureRandom: " + e);
}
try {
dsa.initialize(dsaParams, random);
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
try {
dsa.initialize(dsaParams, null);
fail("InvalidParameterException was not thrown");
} catch (InvalidParameterException ipe) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of InvalidParameterException");
}
try {
dsa.initialize(null, random);
fail("InvalidParameterException was not thrown");
} catch (InvalidParameterException ipe) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of InvalidParameterException");
}
}
use of java.security.interfaces.DSAParams in project robovm by robovm.
the class DSAKeyPairGeneratorTest method test_DSAKeyPairGenerator02.
/**
* java.security.interfaces.DSAKeyPairGenerator
* #initialize(int modlen, boolean genParams, SecureRandom randomm)
*/
public void test_DSAKeyPairGenerator02() {
int[] invalidLen = { -1, 0, 511, 513, 650, 1023, 1025 };
DSAParams dsaParams = new DSAParameterSpec(p, q, g);
SecureRandom random = null;
MyDSA dsa = new MyDSA(null);
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
fail("Unexpected exception for SecureRandom: " + e);
}
//exception case
try {
dsa.initialize(520, false, random);
fail("InvalidParameterException was not thrown");
} catch (InvalidParameterException ipe) {
String str = ipe.getMessage();
if (!str.equals("there are not precomputed parameters")) {
fail("Incorrect exception's message: " + str);
}
} catch (Exception e) {
fail(e + " was thrown instead of InvalidParameterException");
}
//exception case
for (int i = 0; i < invalidLen.length; i++) {
try {
dsa.initialize(invalidLen[i], true, random);
fail("InvalidParameterException was not thrown");
} catch (InvalidParameterException ipe) {
String str = ipe.getMessage();
if (!str.equals("Incorrect modlen")) {
fail("Incorrect exception's message: " + str);
}
} catch (Exception e) {
fail(e + " was thrown instead of InvalidParameterException");
}
}
//positive case
dsa = new MyDSA(dsaParams);
try {
dsa.initialize(520, true, random);
} catch (Exception e) {
fail(e + " was thrown for subcase 1");
}
//positive case
try {
dsa.initialize(520, false, random);
} catch (Exception e) {
fail(e + " was thrown for subcase 1");
}
}
use of java.security.interfaces.DSAParams in project robovm by robovm.
the class DSAKeyTest method test_getParams.
/**
* java.security.interfaces.DSAKey
* #getParams()
* test covers following use cases
* Case 1: check private key
* Case 2: check public key
*/
public void test_getParams() throws Exception {
DSAParams param = new DSAParameterSpec(Util.P, Util.Q, Util.G);
KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
gen.initialize((DSAParameterSpec) param);
DSAKey key = null;
// Case 1: check private key
key = (DSAKey) gen.generateKeyPair().getPrivate();
assertDSAParamsEquals(param, key.getParams());
// Case 2: check public key
key = (DSAKey) gen.generateKeyPair().getPublic();
assertDSAParamsEquals(param, key.getParams());
}
use of java.security.interfaces.DSAParams in project robovm by robovm.
the class DSAParamsTest method test_getG.
/**
* java.security.interfaces.DSAParams
* #getG()
*/
public void test_getG() {
DSAParams params = new DSAParameterSpec(p, q, g);
assertEquals("Invalid G", g, params.getG());
}
use of java.security.interfaces.DSAParams in project robovm by robovm.
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.");
}
Aggregations