use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.
the class VerifyRangeCheckOverflow method main.
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
keyPairGenerator.initialize(1024);
KeyPair keys = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keys.getPublic();
byte[] sigBytes = new byte[100];
Signature signature = Signature.getInstance("SHA1withDSA");
signature.initVerify(publicKey);
try {
signature.verify(sigBytes, Integer.MAX_VALUE, 1);
} catch (IllegalArgumentException ex) {
// Expected
}
}
use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.
the class Chain method runTest.
static boolean runTest(Test test) {
System.out.format("Test: provider = %s, signature algorithm = %s, " + "key algorithm = %s\n", test.provider, test.sigAlg, test.keyAlg);
try {
// Generate all private/public key pairs
PrivateKey[] privKeys = new PrivateKey[N];
PublicKey[] pubKeys = new PublicKey[N];
PublicKey[] anotherPubKeys = new PublicKey[N];
KeyPairGenerator kpg = KeyPairGenerator.getInstance(test.keyAlg.name);
for (int j = 0; j < N; j++) {
KeyPair kp = kpg.genKeyPair();
KeyPair anotherKp = kpg.genKeyPair();
privKeys[j] = kp.getPrivate();
pubKeys[j] = kp.getPublic();
anotherPubKeys[j] = anotherKp.getPublic();
if (Arrays.equals(pubKeys[j].getEncoded(), anotherPubKeys[j].getEncoded())) {
System.out.println("Failed: it should not get " + "the same pair of public key");
return false;
}
}
Signature signature;
if (test.provider != Provider.Default) {
signature = Signature.getInstance(test.sigAlg.name, test.provider.name);
} else {
signature = Signature.getInstance(test.sigAlg.name);
}
// Create a chain of signed objects
SignedObject[] objects = new SignedObject[N];
objects[0] = new SignedObject(str, privKeys[0], signature);
for (int j = 1; j < N; j++) {
objects[j] = new SignedObject(objects[j - 1], privKeys[j], signature);
}
// Verify the chain
int n = objects.length - 1;
SignedObject object = objects[n];
do {
if (!object.verify(pubKeys[n], signature)) {
System.out.println("Failed: verification failed, n = " + n);
return false;
}
if (object.verify(anotherPubKeys[n], signature)) {
System.out.println("Failed: verification should not " + "succeed with wrong public key, n = " + n);
return false;
}
object = (SignedObject) object.getObject();
n--;
} while (n > 0);
System.out.println("signed data: " + object.getObject());
if (!str.equals(object.getObject())) {
System.out.println("Failed: signed data is not equal to " + "original one");
return false;
}
System.out.println("Test passed");
return true;
} catch (NoSuchProviderException nspe) {
if (test.provider == Provider.SunMSCAPI && !System.getProperty("os.name").startsWith("Windows")) {
System.out.println("SunMSCAPI is available only on Windows: " + nspe);
return true;
}
System.out.println("Unexpected exception: " + nspe);
return false;
} catch (Exception e) {
System.out.println("Unexpected exception: " + e);
e.printStackTrace(System.out);
return false;
}
}
use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.
the class Copy method main.
public static void main(String[] args) throws Exception {
KeyPairGenerator kg = KeyPairGenerator.getInstance(DSA);
kg.initialize(KEY_SIZE);
KeyPair kp = kg.genKeyPair();
Signature signature = Signature.getInstance(DSA);
Test original = new Test();
SignedObject so = new SignedObject(original, kp.getPrivate(), signature);
System.out.println("Signature algorithm: " + so.getAlgorithm());
signature = Signature.getInstance(DSA, "SUN");
if (!so.verify(kp.getPublic(), signature)) {
throw new RuntimeException("Verification failed");
}
kg = KeyPairGenerator.getInstance(DSA);
kg.initialize(KEY_SIZE);
kp = kg.genKeyPair();
if (so.verify(kp.getPublic(), signature)) {
throw new RuntimeException("Unexpected success");
}
Object copy = so.getObject();
if (!original.equals(copy)) {
throw new RuntimeException("Signed object is not equal " + "to original one: " + copy);
}
/*
* The signed object is a copy of an original one.
* Once the copy is made, further manipulation
* of the original object shouldn't has any effect on the copy.
*/
original.set(MAGIC - 1);
copy = so.getObject();
if (original.equals(copy)) {
throw new RuntimeException("Signed object is not a copy " + "of original one: " + copy);
}
System.out.println("Test passed");
}
use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.
the class TestDSAGenParameterSpec method checkParam.
private static void checkParam(AlgorithmParameters param, DSAGenParameterSpec genParam) throws InvalidParameterSpecException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
String algorithm = param.getAlgorithm();
if (!algorithm.equalsIgnoreCase(ALGORITHM_NAME)) {
throw new RuntimeException("Unexpected type of parameters: " + algorithm);
}
DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
int valueL = spec.getP().bitLength();
int strengthP = genParam.getPrimePLength();
if (strengthP != valueL) {
System.out.printf("P: Expected %d but actual %d%n", strengthP, valueL);
throw new RuntimeException("Wrong P strength");
}
int valueN = spec.getQ().bitLength();
int strengthQ = genParam.getSubprimeQLength();
if (strengthQ != valueN) {
System.out.printf("Q: Expected %d but actual %d%n", strengthQ, valueN);
throw new RuntimeException("Wrong Q strength");
}
if (genParam.getSubprimeQLength() != genParam.getSeedLength()) {
System.out.println("Defaut seed length should be the same as Q.");
throw new RuntimeException("Wrong seed length");
}
// use the parameters to generate real DSA keys
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_NAME, PROVIDER_NAME);
keyGen.initialize(spec);
keyGen.generateKeyPair();
}
use of java.security.KeyPairGenerator in project jdk8u_jdk by JetBrains.
the class SpecTest method main.
public static void main(String[] args) {
int failCount = 0;
// Test key size.
int size = Integer.parseInt(args[0]);
try {
KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
kpg1.initialize(new RSAKeyGenParameterSpec(size, RSAKeyGenParameterSpec.F4));
if (!specTest(kpg1.generateKeyPair(), RSAKeyGenParameterSpec.F4)) {
failCount++;
}
KeyPairGenerator kpg2 = KeyPairGenerator.getInstance(KEYALG, PROVIDER);
kpg2.initialize(new RSAKeyGenParameterSpec(size, RSAKeyGenParameterSpec.F0));
if (!specTest(kpg2.generateKeyPair(), RSAKeyGenParameterSpec.F0)) {
failCount++;
}
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException ex) {
ex.printStackTrace(System.err);
failCount++;
}
if (failCount != 0) {
throw new RuntimeException("There are " + failCount + " tests failed.");
}
}
Aggregations