Search in sources :

Example 16 with NoSuchProviderException

use of java.security.NoSuchProviderException in project jdk8u_jdk by JetBrains.

the class SignatureTest method main.

public static void main(String[] args) throws Exception {
    String testAlg = args[0];
    int testSize = Integer.parseInt(args[1]);
    byte[] data = new byte[100];
    RandomFactory.getRandom().nextBytes(data);
    // create a key pair
    KeyPair kpair = generateKeys(KEYALG, testSize);
    Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());
    Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());
    // For signature algorithm, create and verify a signature
    Arrays.stream(privs).forEach(priv -> Arrays.stream(pubs).forEach(pub -> {
        try {
            checkSignature(data, (PublicKey) pub, (PrivateKey) priv, testAlg);
        } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException | NoSuchProviderException ex) {
            throw new RuntimeException(ex);
        }
    }));
}
Also used : KeyPairGenerator(java.security.KeyPairGenerator) KeyPair(java.security.KeyPair) Arrays(java.util.Arrays) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) Signature(java.security.Signature) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) Key(java.security.Key) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PUBLIC_KEY(javax.crypto.Cipher.PUBLIC_KEY) RSAPublicKey(java.security.interfaces.RSAPublicKey) RandomFactory(jdk.testlibrary.RandomFactory) PrivateKey(java.security.PrivateKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) InvalidKeyException(java.security.InvalidKeyException) PRIVATE_KEY(javax.crypto.Cipher.PRIVATE_KEY) NoSuchProviderException(java.security.NoSuchProviderException) KeyPair(java.security.KeyPair) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) PrivateKey(java.security.PrivateKey)

Example 17 with NoSuchProviderException

use of java.security.NoSuchProviderException 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.");
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 18 with NoSuchProviderException

use of java.security.NoSuchProviderException in project jdk8u_jdk by JetBrains.

the class TestNonexpanding method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        ci.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
        int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
        ci.doFinal(cipherText, offset);
        // Comparison
        if (!(plainText.length == cipherText.length)) {
            // authentication tag and cipher text.
            if (mo.equalsIgnoreCase("GCM")) {
                GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);
                int cipherTextLength = cipherText.length - spec.getTLen() / 8;
                if (plainText.length == cipherTextLength) {
                    return;
                }
            }
            System.out.println("Original length: " + plainText.length);
            System.out.println("Cipher text length: " + cipherText.length);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator)

Example 19 with NoSuchProviderException

use of java.security.NoSuchProviderException 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;
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchProviderException(java.security.NoSuchProviderException) SignedObject(java.security.SignedObject) NoSuchProviderException(java.security.NoSuchProviderException)

Example 20 with NoSuchProviderException

use of java.security.NoSuchProviderException in project jdk8u_jdk by JetBrains.

the class TestEC method main0.

public static void main0(String[] args) throws Exception {
    Provider p = Security.getProvider("SunEC");
    if (p == null) {
        throw new NoSuchProviderException("Can't get SunEC provider");
    }
    System.out.println("Running tests with " + p.getName() + " provider...\n");
    long start = System.currentTimeMillis();
    /*
         * The entry point used for each test is its instance method
         * called main (not its static method called main).
         */
    new TestECDH().main(p);
    new TestECDSA().main(p);
    new TestCurves().main(p);
    new TestKeyFactory().main(p);
    new TestECGenSpec().main(p);
    new ReadPKCS12().main(p);
    new ReadCertificates().main(p);
    // ClientJSSEServerJSSE fails on Solaris 11 when both SunEC and
    // SunPKCS11-Solaris providers are enabled.
    // Workaround:
    // Security.removeProvider("SunPKCS11-Solaris");
    new ClientJSSEServerJSSE().main(p);
    long stop = System.currentTimeMillis();
    System.out.println("\nCompleted tests with " + p.getName() + " provider (" + ((stop - start) / 1000.0) + " seconds).");
}
Also used : NoSuchProviderException(java.security.NoSuchProviderException) Provider(java.security.Provider)

Aggregations

NoSuchProviderException (java.security.NoSuchProviderException)97 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 InvalidKeyException (java.security.InvalidKeyException)31 IOException (java.io.IOException)29 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)20 CertificateException (java.security.cert.CertificateException)19 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)14 Cipher (javax.crypto.Cipher)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 KeyStoreException (java.security.KeyStoreException)12 X509Certificate (java.security.cert.X509Certificate)12 BadPaddingException (javax.crypto.BadPaddingException)12 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)12 SignatureException (java.security.SignatureException)11 SecretKey (javax.crypto.SecretKey)10 CertificateFactory (java.security.cert.CertificateFactory)9 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)8 KeyStore (java.security.KeyStore)7 Provider (java.security.Provider)7