Java Code Examples for java.security.Security#getProvider()

The following examples show how to use java.security.Security#getProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestPremaster.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");

    KeyGenerator kg;

    kg = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);

    try {
        kg.generateKey();
        throw new Exception("no exception");
    } catch (IllegalStateException e) {
        System.out.println("OK: " + e);
    }

    int[] protocolVersions = {0x0300, 0x0301, 0x0302, 0x0400};
    for (int clientVersion : protocolVersions) {
        for (int serverVersion : protocolVersions) {
            test(kg, clientVersion, serverVersion);
            if (serverVersion >= clientVersion) {
                break;
            }
        }
    }

    System.out.println("Done.");
}
 
Example 2
Source File: AbstractSignatureInFilter.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected MessageVerifier createMessageVerifier() {
    Properties props = KeyManagementUtils.loadSignatureInProperties();
    if (props == null) {
        throw new SignatureException("Signature properties are not configured correctly");
    }

    Message m = PhaseInterceptorChain.getCurrentMessage();
    PublicKey publicKey = KeyManagementUtils.loadPublicKey(m, props);

    String signatureAlgorithm = (String)m.getContextualProperty(HTTPSignatureConstants.RSSEC_SIGNATURE_ALGORITHM);
    if (signatureAlgorithm == null) {
        signatureAlgorithm = DefaultSignatureConstants.SIGNING_ALGORITHM;
    }

    final String finalSignatureAlgorithm = signatureAlgorithm;
    final Provider provider = Security.getProvider(DefaultSignatureConstants.SECURITY_PROVIDER);
    return new MessageVerifier(keyId -> publicKey, keyId -> provider, keyId -> finalSignatureAlgorithm);
}
 
Example 3
Source File: TestSameLength.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSHA3supported() {
    if (Security.getProvider("SUN") != null) {
        return true;
    }
    if (Security.getProvider("OracleUcrypto") != null
            && "SunOS".equals(System.getProperty("os.name"))
            && System.getProperty("os.version").compareTo("5.12") >= 0) {
        return true;
    }
    return false;
}
 
Example 4
Source File: SameBuffer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider p = Security.getProvider(PROVIDER);
    for (int keyLength : KEY_LENGTHS) {
        for (int textLength : TEXT_LENGTHS) {
            for (int AADLength : AAD_LENGTHS) {
                for (int i = 0; i < OFFSETS; i++) {
                    // try different offsets
                    int offset = i * OFFSET;
                    runTest(p, AES, GCM, PADDING, keyLength, textLength,
                            AADLength, offset);
                }
            }
        }
    }
}
 
Example 5
Source File: TestEC.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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).");
}
 
Example 6
Source File: PBESealedObject.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    PBESealedObject test = new PBESealedObject();
    Provider sunjce = Security.getProvider("SunJCE");

    if (!test.runAll(sunjce, System.out)) {
        throw new RuntimeException("One or more tests have failed....");
    }
}
 
Example 7
Source File: ReadConfInUTF16Env.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String argv[]) {
    Provider p = Security.getProvider("SunPKCS11");
    if (p == null) {
        p = Security.getProvider("SunPKCS11-Solaris");
        if (p == null) {
            System.out.println("Skipping test - no PKCS11 provider available");
            return;
        }
    }

    System.out.println(p.getName());
}
 
Example 8
Source File: Providers.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void setAt(Provider p, int pos) throws Exception {
    if (Security.getProvider(p.getName()) != null) {
        Security.removeProvider(p.getName());
    }
    if (Security.insertProviderAt(p, pos) == -1) {
        throw new Exception("cannot setAt");
    }
}
 
Example 9
Source File: SignUtils.java    From Websocket-Smart-Card-Signer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] calculateHASH(String digestOID, byte[] data) throws Exception{
    String digestName = "";
    
    try{
        if(Security.getProvider("BC") == null)
            Security.addProvider(new BouncyCastleProvider());
        
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_MD5))
            digestName = "MD5";
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA1))
            digestName = "SHA-1";
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA256))
            digestName = "SHA-256";
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA384))
            digestName = "SHA-384";
        if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA512))
            digestName = "SHA-512";
        
        if(digestName.equals(""))
            throw new Exception("Unsupported digestOID");
        
        MessageDigest md = MessageDigest.getInstance(digestName, "BC");
        md.update(data);
        
        byte[] hash = md.digest();

        return hash;
    }catch(Exception e){
        throw new Exception("Error on the generation for the Hash "+digestName+":\n"+e.getMessage());
    }
}
 
Example 10
Source File: ECKeyTest.java    From azure-keyvault-java with MIT License 5 votes vote down vote up
@Test
public void testCreateSECP256K1Key() throws Exception {
    ECGenParameterSpec gps = new ECGenParameterSpec("secp256k1");
    Provider myprov = Security.getProvider("BC");
    final KeyPairGenerator generator = KeyPairGenerator.getInstance("EC");

    generator.initialize(gps);
    EcKey key = new EcKey("akey", JsonWebKeyCurveName.P_256K);
}
 
Example 11
Source File: ReadConfInUTF16Env.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String argv[]) {
    Provider p = Security.getProvider("SunPKCS11");
    if (p == null) {
        p = Security.getProvider("SunPKCS11-Solaris");
        if (p == null) {
            System.out.println("Skipping test - no PKCS11 provider available");
            return;
        }
    }

    System.out.println(p.getName());
}
 
Example 12
Source File: TestPRF12.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");

    InputStream in = new FileInputStream(new File(BASE, "prf12data.txt"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    int n = 0;
    int lineNumber = 0;

    byte[] secret = null;
    String label = null;
    byte[] seed = null;
    int length = 0;
    String prfAlg = null;
    int prfHashLength = 0;
    int prfBlockSize = 0;
    byte[] output = null;

    while (true) {
        String line = reader.readLine();
        lineNumber++;
        if (line == null) {
            break;
        }
        if (line.startsWith("prf-") == false) {
            continue;
        }

        String data = line.substring(PREFIX_LENGTH);
        if (line.startsWith("prf-secret:")) {
            secret = parse(data);
        } else if (line.startsWith("prf-label:")) {
            label = data;
        } else if (line.startsWith("prf-seed:")) {
            seed = parse(data);
        } else if (line.startsWith("prf-length:")) {
            length = Integer.parseInt(data);
        } else if (line.startsWith("prf-alg:")) {
            prfAlg = data;
            switch (prfAlg) {
            case "SHA-224":
                prfHashLength = 28;
                prfBlockSize =  64;
                break;
            case "SHA-256":
                prfHashLength = 32;
                prfBlockSize =  64;
                break;
            case "SHA-384":
                prfHashLength = 48;
                prfBlockSize = 128;
                break;
            case "SHA-512":
                prfHashLength = 64;
                prfBlockSize = 128;
                break;
            default:
                throw new Exception("Unknown Alg in the data.");
            }
        } else if (line.startsWith("prf-output:")) {
            output = parse(data);

            System.out.print(".");
            n++;

            KeyGenerator kg =
                KeyGenerator.getInstance("SunTls12Prf", provider);
            SecretKey inKey;
            if (secret == null) {
                inKey = null;
            } else {
                inKey = new SecretKeySpec(secret, "Generic");
            }
            TlsPrfParameterSpec spec =
                new TlsPrfParameterSpec(inKey, label, seed, length,
                    prfAlg, prfHashLength, prfBlockSize);
            kg.init(spec);
            SecretKey key = kg.generateKey();
            byte[] enc = key.getEncoded();
            if (Arrays.equals(output, enc) == false) {
                throw new Exception("mismatch line: " + lineNumber);
            }
        } else {
            throw new Exception("Unknown line: " + line);
        }
    }
    if (n == 0) {
        throw new Exception("no tests");
    }
    in.close();
    System.out.println();
    System.out.println("OK: " + n + " tests");
}
 
Example 13
Source File: TestMasterSecret.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");

    InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    int n = 0;
    int lineNumber = 0;

    String algorithm = null;
    byte[] premaster = null;
    byte[] clientRandom = null;
    byte[] serverRandom = null;
    int protoMajor = 0;
    int protoMinor = 0;
    int preMajor = 0;
    int preMinor = 0;
    byte[] master = null;

    while (true) {
        String line = reader.readLine();
        lineNumber++;
        if (line == null) {
            break;
        }
        if (line.startsWith("m-") == false) {
            continue;
        }
        String data = line.substring(PREFIX_LENGTH);
        if (line.startsWith("m-algorithm:")) {
            algorithm = data;
        } else if (line.startsWith("m-premaster:")) {
            premaster = parse(data);
        } else if (line.startsWith("m-crandom:")) {
            clientRandom = parse(data);
        } else if (line.startsWith("m-srandom:")) {
            serverRandom = parse(data);
        } else if (line.startsWith("m-protomajor:")) {
            protoMajor = Integer.parseInt(data);
        } else if (line.startsWith("m-protominor:")) {
            protoMinor = Integer.parseInt(data);
        } else if (line.startsWith("m-premajor:")) {
            preMajor = Integer.parseInt(data);
        } else if (line.startsWith("m-preminor:")) {
            preMinor = Integer.parseInt(data);
        } else if (line.startsWith("m-master:")) {
            master = parse(data);

            System.out.print(".");
            n++;

            KeyGenerator kg =
                KeyGenerator.getInstance("SunTlsMasterSecret", provider);
            SecretKey premasterKey =
                new SecretKeySpec(premaster, algorithm);
            TlsMasterSecretParameterSpec spec =
                new TlsMasterSecretParameterSpec(premasterKey, protoMajor,
                    protoMinor, clientRandom, serverRandom,
                    null, -1, -1);
            kg.init(spec);
            TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
            byte[] enc = key.getEncoded();
            if (Arrays.equals(master, enc) == false) {
                throw new Exception("mismatch line: " + lineNumber);
            }
            if ((preMajor != key.getMajorVersion()) ||
                    (preMinor != key.getMinorVersion())) {
                throw new Exception("version mismatch line: " + lineNumber);
            }
        } else {
            throw new Exception("Unknown line: " + line);
        }
    }
    if (n == 0) {
        throw new Exception("no tests");
    }
    in.close();
    System.out.println();
    System.out.println("OK: " + n + " tests");
}
 
Example 14
Source File: TestMasterSecret.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");

    InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    int n = 0;
    int lineNumber = 0;

    String algorithm = null;
    byte[] premaster = null;
    byte[] clientRandom = null;
    byte[] serverRandom = null;
    int protoMajor = 0;
    int protoMinor = 0;
    int preMajor = 0;
    int preMinor = 0;
    byte[] master = null;

    while (true) {
        String line = reader.readLine();
        lineNumber++;
        if (line == null) {
            break;
        }
        if (line.startsWith("m-") == false) {
            continue;
        }
        String data = line.substring(PREFIX_LENGTH);
        if (line.startsWith("m-algorithm:")) {
            algorithm = data;
        } else if (line.startsWith("m-premaster:")) {
            premaster = parse(data);
        } else if (line.startsWith("m-crandom:")) {
            clientRandom = parse(data);
        } else if (line.startsWith("m-srandom:")) {
            serverRandom = parse(data);
        } else if (line.startsWith("m-protomajor:")) {
            protoMajor = Integer.parseInt(data);
        } else if (line.startsWith("m-protominor:")) {
            protoMinor = Integer.parseInt(data);
        } else if (line.startsWith("m-premajor:")) {
            preMajor = Integer.parseInt(data);
        } else if (line.startsWith("m-preminor:")) {
            preMinor = Integer.parseInt(data);
        } else if (line.startsWith("m-master:")) {
            master = parse(data);

            System.out.print(".");
            n++;

            KeyGenerator kg =
                KeyGenerator.getInstance("SunTlsMasterSecret", provider);
            SecretKey premasterKey =
                new SecretKeySpec(premaster, algorithm);
            TlsMasterSecretParameterSpec spec =
                new TlsMasterSecretParameterSpec(premasterKey, protoMajor,
                    protoMinor, clientRandom, serverRandom,
                    null, -1, -1);
            kg.init(spec);
            TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
            byte[] enc = key.getEncoded();
            if (Arrays.equals(master, enc) == false) {
                throw new Exception("mismatch line: " + lineNumber);
            }
            if ((preMajor != key.getMajorVersion()) ||
                    (preMinor != key.getMinorVersion())) {
                throw new Exception("version mismatch line: " + lineNumber);
            }
        } else {
            throw new Exception("Unknown line: " + line);
        }
    }
    if (n == 0) {
        throw new Exception("no tests");
    }
    in.close();
    System.out.println();
    System.out.println("OK: " + n + " tests");
}
 
Example 15
Source File: TestMasterSecret.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");

    InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    int n = 0;
    int lineNumber = 0;

    String algorithm = null;
    byte[] premaster = null;
    byte[] clientRandom = null;
    byte[] serverRandom = null;
    int protoMajor = 0;
    int protoMinor = 0;
    int preMajor = 0;
    int preMinor = 0;
    byte[] master = null;

    while (true) {
        String line = reader.readLine();
        lineNumber++;
        if (line == null) {
            break;
        }
        if (line.startsWith("m-") == false) {
            continue;
        }
        String data = line.substring(PREFIX_LENGTH);
        if (line.startsWith("m-algorithm:")) {
            algorithm = data;
        } else if (line.startsWith("m-premaster:")) {
            premaster = parse(data);
        } else if (line.startsWith("m-crandom:")) {
            clientRandom = parse(data);
        } else if (line.startsWith("m-srandom:")) {
            serverRandom = parse(data);
        } else if (line.startsWith("m-protomajor:")) {
            protoMajor = Integer.parseInt(data);
        } else if (line.startsWith("m-protominor:")) {
            protoMinor = Integer.parseInt(data);
        } else if (line.startsWith("m-premajor:")) {
            preMajor = Integer.parseInt(data);
        } else if (line.startsWith("m-preminor:")) {
            preMinor = Integer.parseInt(data);
        } else if (line.startsWith("m-master:")) {
            master = parse(data);

            System.out.print(".");
            n++;

            KeyGenerator kg =
                KeyGenerator.getInstance("SunTlsMasterSecret", provider);
            SecretKey premasterKey =
                new SecretKeySpec(premaster, algorithm);
            TlsMasterSecretParameterSpec spec =
                new TlsMasterSecretParameterSpec(premasterKey, protoMajor,
                    protoMinor, clientRandom, serverRandom,
                    null, -1, -1);
            kg.init(spec);
            TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
            byte[] enc = key.getEncoded();
            if (Arrays.equals(master, enc) == false) {
                throw new Exception("mismatch line: " + lineNumber);
            }
            if ((preMajor != key.getMajorVersion()) ||
                    (preMinor != key.getMinorVersion())) {
                throw new Exception("version mismatch line: " + lineNumber);
            }
        } else {
            throw new Exception("Unknown line: " + line);
        }
    }
    if (n == 0) {
        throw new Exception("no tests");
    }
    in.close();
    System.out.println();
    System.out.println("OK: " + n + " tests");
}
 
Example 16
Source File: TestMasterSecret.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");

    InputStream in = new FileInputStream(new File(BASE, "masterdata.txt"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    int n = 0;
    int lineNumber = 0;

    String algorithm = null;
    byte[] premaster = null;
    byte[] clientRandom = null;
    byte[] serverRandom = null;
    int protoMajor = 0;
    int protoMinor = 0;
    int preMajor = 0;
    int preMinor = 0;
    byte[] master = null;

    while (true) {
        String line = reader.readLine();
        lineNumber++;
        if (line == null) {
            break;
        }
        if (line.startsWith("m-") == false) {
            continue;
        }
        String data = line.substring(PREFIX_LENGTH);
        if (line.startsWith("m-algorithm:")) {
            algorithm = data;
        } else if (line.startsWith("m-premaster:")) {
            premaster = parse(data);
        } else if (line.startsWith("m-crandom:")) {
            clientRandom = parse(data);
        } else if (line.startsWith("m-srandom:")) {
            serverRandom = parse(data);
        } else if (line.startsWith("m-protomajor:")) {
            protoMajor = Integer.parseInt(data);
        } else if (line.startsWith("m-protominor:")) {
            protoMinor = Integer.parseInt(data);
        } else if (line.startsWith("m-premajor:")) {
            preMajor = Integer.parseInt(data);
        } else if (line.startsWith("m-preminor:")) {
            preMinor = Integer.parseInt(data);
        } else if (line.startsWith("m-master:")) {
            master = parse(data);

            System.out.print(".");
            n++;

            KeyGenerator kg =
                KeyGenerator.getInstance("SunTlsMasterSecret", provider);
            SecretKey premasterKey =
                new SecretKeySpec(premaster, algorithm);
            TlsMasterSecretParameterSpec spec =
                new TlsMasterSecretParameterSpec(premasterKey, protoMajor,
                    protoMinor, clientRandom, serverRandom,
                    null, -1, -1);
            kg.init(spec);
            TlsMasterSecret key = (TlsMasterSecret)kg.generateKey();
            byte[] enc = key.getEncoded();
            if (Arrays.equals(master, enc) == false) {
                throw new Exception("mismatch line: " + lineNumber);
            }
            if ((preMajor != key.getMajorVersion()) ||
                    (preMinor != key.getMinorVersion())) {
                throw new Exception("version mismatch line: " + lineNumber);
            }
        } else {
            throw new Exception("Unknown line: " + line);
        }
    }
    if (n == 0) {
        throw new Exception("no tests");
    }
    in.close();
    System.out.println();
    System.out.println("OK: " + n + " tests");
}
 
Example 17
Source File: TestOAEPPadding.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {
    cp = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + cp.getName() + "...");
    Provider kfp = Security.getProvider("SunRsaSign");
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);
    kpg.initialize(2048);
    KeyPair kp = kpg.generateKeyPair();
    privateKey = (RSAPrivateKey)kp.getPrivate();
    publicKey = (RSAPublicKey)kp.getPublic();

    // Test using a spec with each digest algorithm case
    // MD5
    test(new OAEPParameterSpec("MD5", "MGF1",
            MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("MD5", "MGF1",
            MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("MD5", "MGF1",
            MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("MD5", "MGF1",
            MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("MD5", "MGF1",
            MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
    // SHA1
    test(new OAEPParameterSpec("SHA1", "MGF1",
            MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA1", "MGF1",
            MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA1", "MGF1",
            MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA1", "MGF1",
            MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA1", "MGF1",
            MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
    // For default OAEPParameterSpec case (SHA1)
    test(null);
    // SHA-224
    test(new OAEPParameterSpec("SHA-224", "MGF1",
            MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-224", "MGF1",
            MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-224", "MGF1",
            MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-224", "MGF1",
            MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-224", "MGF1",
            MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
    // SHA-256
    test(new OAEPParameterSpec("SHA-256", "MGF1",
            MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-256", "MGF1",
            MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-256", "MGF1",
            MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-256", "MGF1",
            MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-256", "MGF1",
            MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
    // SHA-384
    test(new OAEPParameterSpec("SHA-384", "MGF1",
            MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-384", "MGF1",
            MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-384", "MGF1",
            MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-384", "MGF1",
            MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-384", "MGF1",
            MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
    // SHA-512
    test(new OAEPParameterSpec("SHA-512", "MGF1",
            MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-512", "MGF1",
            MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-512", "MGF1",
            MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-512", "MGF1",
            MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-512", "MGF1",
            MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));
    // SHA-512/224 and SHA-512/256
    test(new OAEPParameterSpec("SHA-512/224", "MGF1",
            MGF1ParameterSpec.SHA224, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-512/224", "MGF1",
            MGF1ParameterSpec.SHA512_224, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-512/256", "MGF1",
            MGF1ParameterSpec.SHA384, PSource.PSpecified.DEFAULT));
    test(new OAEPParameterSpec("SHA-512/256", "MGF1",
            MGF1ParameterSpec.SHA512, PSource.PSpecified.DEFAULT));

    if (failed) {
        throw new Exception("Test failed");
    }
}
 
Example 18
Source File: TestPRF12.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");

    InputStream in = new FileInputStream(new File(BASE, "prf12data.txt"));
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    int n = 0;
    int lineNumber = 0;

    byte[] secret = null;
    String label = null;
    byte[] seed = null;
    int length = 0;
    String prfAlg = null;
    int prfHashLength = 0;
    int prfBlockSize = 0;
    byte[] output = null;

    while (true) {
        String line = reader.readLine();
        lineNumber++;
        if (line == null) {
            break;
        }
        if (line.startsWith("prf-") == false) {
            continue;
        }

        String data = line.substring(PREFIX_LENGTH);
        if (line.startsWith("prf-secret:")) {
            secret = parse(data);
        } else if (line.startsWith("prf-label:")) {
            label = data;
        } else if (line.startsWith("prf-seed:")) {
            seed = parse(data);
        } else if (line.startsWith("prf-length:")) {
            length = Integer.parseInt(data);
        } else if (line.startsWith("prf-alg:")) {
            prfAlg = data;
            switch (prfAlg) {
            case "SHA-224":
                prfHashLength = 28;
                prfBlockSize =  64;
                break;
            case "SHA-256":
                prfHashLength = 32;
                prfBlockSize =  64;
                break;
            case "SHA-384":
                prfHashLength = 48;
                prfBlockSize = 128;
                break;
            case "SHA-512":
                prfHashLength = 64;
                prfBlockSize = 128;
                break;
            default:
                throw new Exception("Unknown Alg in the data.");
            }
        } else if (line.startsWith("prf-output:")) {
            output = parse(data);

            System.out.print(".");
            n++;

            KeyGenerator kg =
                KeyGenerator.getInstance("SunTls12Prf", provider);
            SecretKey inKey;
            if (secret == null) {
                inKey = null;
            } else {
                inKey = new SecretKeySpec(secret, "Generic");
            }
            TlsPrfParameterSpec spec =
                new TlsPrfParameterSpec(inKey, label, seed, length,
                    prfAlg, prfHashLength, prfBlockSize);
            kg.init(spec);
            SecretKey key = kg.generateKey();
            byte[] enc = key.getEncoded();
            if (Arrays.equals(output, enc) == false) {
                throw new Exception("mismatch line: " + lineNumber);
            }
        } else {
            throw new Exception("Unknown line: " + line);
        }
    }
    if (n == 0) {
        throw new Exception("no tests");
    }
    in.close();
    System.out.println();
    System.out.println("OK: " + n + " tests");
}
 
Example 19
Source File: TestCipherKeyWrapperTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        TestCipherKeyWrapperTest test = new TestCipherKeyWrapperTest();
        // AESWrap and DESedeWrap test
        for (AlgorithmWrapper algoWrapper : AlgorithmWrapper.values()) {
            String algo = algoWrapper.getAlgorithm();
            String wrapper = algoWrapper.getWrapper();
            try {
                int keySize = algoWrapper.getKeySize();
                // only run the tests on longer key lengths if unlimited
                // version of JCE jurisdiction policy files are installed
                if (!(Cipher.getMaxAllowedKeyLength(algo) == Integer.MAX_VALUE)
                        && keySize > LINIMITED_KEYSIZE) {
                    out.println(algo + " will not run if unlimited version of"
                            + " JCE jurisdiction policy files are installed");
                    continue;
                }
                test.wrapperAesDESedeKeyTest(algo, wrapper, keySize);
                if (algoWrapper == AlgorithmWrapper.NegtiveWrap) {
                    throw new RuntimeException("Expected not throw when algo"
                            + " and wrapAlgo are not match:" + algo);
                }
            } catch (InvalidKeyException e) {
                if (algoWrapper == AlgorithmWrapper.NegtiveWrap) {
                    out.println("Expepted exception when algo"
                            + " and wrapAlgo are not match:" + algo);
                } else {
                    throw e;
                }
            }
        }
        test.wrapperBlowfishKeyTest();
        // PBE and public wrapper test.
        String[] publicPrivateAlgos = new String[] { "DiffieHellman", "DSA",
                "RSA" };
        Provider provider = Security.getProvider(SUN_JCE);
        if (provider == null) {
            throw new RuntimeException("SUN_JCE provider not exist");
        }

        test.wrapperPBEKeyTest(provider);
        // Public and private key wrap test
        test.wrapperPublicPriviteKeyTest(provider, publicPrivateAlgos);
    }
 
Example 20
Source File: CrlStreamParserTest.java    From xipki with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void init() {
  if (Security.getProvider("BC") == null) {
    Security.addProvider(new BouncyCastleProvider());
  }
}