Java Code Examples for java.security.Provider#getService()

The following examples show how to use java.security.Provider#getService() . 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 openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService(
            "KeyGenerator", "SunTlsRsaPremasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    KeyGenerator kg;
    kg = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);

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

    test(kg, 3, 0);
    test(kg, 3, 1);
    test(kg, 3, 2);
    test(kg, 4, 0);

    System.out.println("Done.");
}
 
Example 2
Source File: DigestKAT.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
void run(Provider p) throws Exception {
    if (p.getService("MessageDigest", alg) == null) {
        System.out.println("Skipped " + alg);
        return;
    }
    MessageDigest md = MessageDigest.getInstance(alg, p);
    md.update(data);
    byte[] myDigest = md.digest();
    if (Arrays.equals(digest, myDigest) == false) {
        System.out.println("Digest test for " + alg + " failed:");
        if (data.length < 256) {
            System.out.println("data: " + DigestKAT.toString(data));
        }
        System.out.println("dig:  " + DigestKAT.toString(digest));
        System.out.println("out:  " + DigestKAT.toString(myDigest));
        throw new Exception("Digest test for " + alg + " failed");
    }
}
 
Example 3
Source File: KeyInfoFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a <code>KeyInfoFactory</code> that supports the
 * requested XML processing mechanism and representation type (ex: "DOM"),
 * as supplied by the specified provider. Note that the specified
 * <code>Provider</code> object does not have to be registered in the
 * provider list.
 *
 * @param mechanismType the type of the XML processing mechanism and
 *    representation.  See the <a href=
 *    "{@docRoot}/../specs/security/standard-names.html#xml-signature-xmlsignaturefactorykeyinfofactorytransformservice-mechanisms">
 *    Java Security Standard Algorithm Names</a> document
 *    for more information.
 * @param provider the <code>Provider</code> object
 * @return a new <code>KeyInfoFactory</code>
 * @throws NullPointerException if <code>mechanismType</code> or
 *    <code>provider</code> are <code>null</code>
 * @throws NoSuchMechanismException if a <code>KeyInfoFactory</code>
 *    implementation for the specified mechanism is not available from the
 *    specified <code>Provider</code> object
 * @see Provider
 */
public static KeyInfoFactory getInstance(String mechanismType,
    Provider provider) {
    if (mechanismType == null) {
        throw new NullPointerException("mechanismType cannot be null");
    } else if (provider == null) {
        throw new NullPointerException("provider cannot be null");
    }

    Service s = provider.getService("KeyInfoFactory", mechanismType);
    if (s != null) {
        Object obj = null;
        try {
            obj = s.newInstance(null);
        } catch (NoSuchAlgorithmException nsae) {
            throw new NoSuchMechanismException(nsae);
        }

        if (obj instanceof KeyInfoFactory) {
            KeyInfoFactory factory = (KeyInfoFactory) obj;
            factory.mechanismType = mechanismType;
            factory.provider = provider;
            return factory;
        }
    }
    throw new NoSuchMechanismException
        ("Mechanism " + mechanismType + " not available from " + provider.getName());
}
 
Example 4
Source File: ClientJSSEServerJSSE.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void main(Provider p) throws Exception {
    if (p.getService("KeyFactory", "EC") == null) {
        System.out.println("Provider does not support EC, skipping");
        return;
    }
    Providers.setAt(p, 1);
    CipherTest.main(new JSSEFactory(), cmdArgs);
    Security.removeProvider(p.getName());
}
 
Example 5
Source File: TestCloning.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void main(Provider p) throws Exception {
    Random r = new Random();
    byte[] data1 = new byte[10];
    byte[] data2 = new byte[2*1024];
    r.nextBytes(data1);
    r.nextBytes(data2);
    System.out.println("Testing against provider " + p.getName());
    for (int i = 0; i < ALGOS.length; i++) {
        if (p.getService("MessageDigest", ALGOS[i]) == null) {
            System.out.println(ALGOS[i] + " is not supported, skipping");
            continue;
        } else {
            System.out.println("Testing " + ALGOS[i] + " of " + p.getName());
            MessageDigest md = MessageDigest.getInstance(ALGOS[i], p);
            try {
                md = testCloning(md, p);
                // repeat the test again after generating digest once
                for (int j = 0; j < 10; j++) {
                    md = testCloning(md, p);
                }
            } catch (Exception ex) {
                if (ALGOS[i] == "MD2" &&
                    p.getName().equalsIgnoreCase("SunPKCS11-NSS")) {
                    // known bug in NSS; ignore for now
                    System.out.println("Ignore Known bug in MD2 of NSS");
                    continue;
                }
                throw ex;
            }
        }
    }
}
 
Example 6
Source File: TestPremaster.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void main(Provider provider) throws Exception {
    if (provider.getService(
            "KeyGenerator", "SunTlsRsaPremasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    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};
    for (int clientVersion : protocolVersions) {
        for (int serverVersion : protocolVersions) {
            test(kg, clientVersion, serverVersion);
            if (serverVersion >= clientVersion) {
                break;
            }
        }
    }

    System.out.println("Done.");
}
 
Example 7
Source File: TransformService.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a <code>TransformService</code> that supports the specified
 * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
 * (ex: DOM) as supplied by the specified provider. Note that the specified
 * <code>Provider</code> object does not have to be registered in the
 * provider list.
 *
 * @param algorithm the URI of the algorithm
 * @param mechanismType the type of the XML processing mechanism and
 *   representation
 * @param provider the <code>Provider</code> object
 * @return a new <code>TransformService</code>
 * @throws NullPointerException if <code>provider</code>,
 *   <code>algorithm</code>, or <code>mechanismType</code> is
 *   <code>null</code>
 * @throws NoSuchAlgorithmException if a <code>TransformService</code>
 *   implementation for the specified algorithm and mechanism type is not
 *   available from the specified <code>Provider</code> object
 * @see Provider
 */
public static TransformService getInstance
    (String algorithm, String mechanismType, Provider provider)
    throws NoSuchAlgorithmException {
    if (mechanismType == null || algorithm == null || provider == null) {
        throw new NullPointerException();
    }

    boolean dom = false;
    if (mechanismType.equals("DOM")) {
        dom = true;
    }
    Service s = provider.getService("TransformService", algorithm);
    if (s != null) {
        String value = s.getAttribute("MechanismType");
        if ((value == null && dom) ||
            (value != null && value.equals(mechanismType))) {
            Object obj = s.newInstance(null);
            if (obj instanceof TransformService) {
                TransformService ts = (TransformService) obj;
                ts.algorithm = algorithm;
                ts.mechanism = mechanismType;
                ts.provider = provider;
                return ts;
            }
        }
    }
    throw new NoSuchAlgorithmException
        (algorithm + " algorithm and " + mechanismType
             + " mechanism not available from " + provider.getName());
}
 
Example 8
Source File: TestPremaster.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService(
            "KeyGenerator", "SunTlsRsaPremasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    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 9
Source File: TestPremaster.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService(
            "KeyGenerator", "SunTlsRsaPremasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    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 10
Source File: TestDSA.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void main(Provider provider) throws Exception {
    long start = System.currentTimeMillis();

    System.out.println("Testing provider " + provider + "...");

    /*
     * Use Solaris SPARC 11.2 or later to avoid an intermittent failure
     * when running SunPKCS11-Solaris (8044554)
     */
    if (provider.getName().equals("SunPKCS11-Solaris") &&
        props.getProperty("os.name").equals("SunOS") &&
        props.getProperty("os.arch").equals("sparcv9") &&
        props.getProperty("os.version").compareTo("5.11") <= 0 &&
        getDistro().compareTo("11.2") < 0) {

        System.out.println("SunPKCS11-Solaris provider requires " +
            "Solaris SPARC 11.2 or later, skipping");
        return;
    }

    if (provider.getService("Signature", "SHA1withDSA") == null) {
        System.out.println("DSA not supported, skipping");
        return;
    }

    KeyFactory kf = KeyFactory.getInstance("DSA", provider);
    DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x, p, q, g);
    DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y, p, q, g);
    PrivateKey privateKey = kf.generatePrivate(privSpec);
    PublicKey publicKey = kf.generatePublic(pubSpec);

    // verify known-good and known-bad signatures using SHA1withDSA and RawDSA
    verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1a, true);
    verify(provider, "SHA1withDSA", publicKey, data1Raw, sig1b, true);
    verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1a, false);
    verify(provider, "SHA1withDSA", publicKey, data2Raw, sig1b, false);

    verify(provider, "RawDSA", publicKey, data1SHA, sig1a, true);
    verify(provider, "RawDSA", publicKey, data1SHA, sig1b, true);
    verify(provider, "RawDSA", publicKey, data2SHA, sig1a, false);
    verify(provider, "RawDSA", publicKey, data2SHA, sig1b, false);

    testSigning(provider, privateKey, publicKey);

    long stop = System.currentTimeMillis();
    System.out.println("All tests passed (" + (stop - start) + " ms).");
}
 
Example 11
Source File: TestMasterSecret.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    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 12
Source File: TestMasterSecret.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    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 13
Source File: ByteBuffers.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void main(Provider p) throws Exception {
    if (p.getService("MessageDigest", "MD5") == null) {
        System.out.println("Provider does not support MD5, skipping");
        return;
    }

    Random random = new Random();
    int n = 10 * 1024;
    byte[] t = new byte[n];
    random.nextBytes(t);

    MessageDigest md = MessageDigest.getInstance("MD5", p);
    byte[] d1 = md.digest(t);

    // test 1: ByteBuffer with an accessible backing array
    ByteBuffer b1 = ByteBuffer.allocate(n + 256);
    b1.position(random.nextInt(256));
    b1.limit(b1.position() + n);
    ByteBuffer b2 = b1.slice();
    b2.put(t);
    b2.clear();
    byte[] d2 = digest(md, b2, random);
    if (Arrays.equals(d1, d2) == false) {
        throw new Exception("Test 1 failed");
    }

    // test 2: direct ByteBuffer
    ByteBuffer b3 = ByteBuffer.allocateDirect(t.length);
    b3.put(t);
    b3.clear();
    byte[] d3 = digest(md, b3, random);
    if (Arrays.equals(d1, d2) == false) {
        throw new Exception("Test 2 failed");
    }

    // test 3: ByteBuffer without an accessible backing array
    b2.clear();
    ByteBuffer b4 = b2.asReadOnlyBuffer();
    byte[] d4 = digest(md, b4, random);
    if (Arrays.equals(d1, d2) == false) {
        throw new Exception("Test 3 failed");
    }
    System.out.println("All tests passed");
}
 
Example 14
Source File: TestPRF.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyGenerator", "SunTlsPrf") == null) {
        System.out.println("Provider does not support algorithm, skipping");
        return;
    }

    InputStream in = new FileInputStream(new File(BASE, "prfdata.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;
    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-output:")) {
            output = parse(data);

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

            KeyGenerator kg =
                KeyGenerator.getInstance("SunTlsPrf", provider);
            SecretKey inKey;
            if (secret == null) {
                inKey = null;
            } else {
                inKey = new SecretKeySpec(secret, "Generic");
            }
            TlsPrfParameterSpec spec =
                new TlsPrfParameterSpec(inKey, label, seed, length,
                    null, -1, -1);
            SecretKey key;
            try {
                kg.init(spec);
                key = kg.generateKey();
            } catch (Exception e) {
                if (secret == null) {
                    // This fails on Solaris, but since we never call this
                    // API for this case in JSSE, ignore the failure.
                    // (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE
                    // mechanism)
                    System.out.print("X");
                    continue;
                }
                System.out.println();
                throw new Exception("Error on line: " + lineNumber, e);
            }
            byte[] enc = key.getEncoded();
            if (Arrays.equals(output, enc) == false) {
                System.out.println();
                System.out.println("expected: " + toString(output));
                System.out.println("actual:   " + toString(enc));
                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 15
Source File: TestMasterSecret.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }

    try (BufferedReader reader = Files.newBufferedReader(
            Paths.get(BASE, "masterdata.txt"))) {

        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);

                try {
                    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);
                    }
                } catch (InvalidAlgorithmParameterException iape) {
                    // SSLv3 support is removed in S12
                    if (preMajor == 3 && preMinor == 0) {
                        System.out.println("Skip testing SSLv3");
                        continue;
                    }
                }
            } else {
                throw new Exception("Unknown line: " + line);
            }
        }
        if (n == 0) {
            throw new Exception("no tests");
        }
        System.out.println();
        System.out.println("OK: " + n + " tests");
    }
}
 
Example 16
Source File: TestMasterSecret.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    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: TestPRF.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyGenerator", "SunTlsPrf") == null) {
        System.out.println("Provider does not support algorithm, skipping");
        return;
    }

    InputStream in = new FileInputStream(new File(BASE, "prfdata.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;
    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-output:")) {
            output = parse(data);

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

            KeyGenerator kg =
                KeyGenerator.getInstance("SunTlsPrf", provider);
            SecretKey inKey;
            if (secret == null) {
                inKey = null;
            } else {
                inKey = new SecretKeySpec(secret, "Generic");
            }
            TlsPrfParameterSpec spec =
                new TlsPrfParameterSpec(inKey, label, seed, length,
                    null, -1, -1);
            SecretKey key;
            try {
                kg.init(spec);
                key = kg.generateKey();
            } catch (Exception e) {
                if (secret == null) {
                    // This fails on Solaris, but since we never call this
                    // API for this case in JSSE, ignore the failure.
                    // (SunJSSE uses the CKM_TLS_KEY_AND_MAC_DERIVE
                    // mechanism)
                    System.out.print("X");
                    continue;
                }
                System.out.println();
                throw new Exception("Error on line: " + lineNumber, e);
            }
            byte[] enc = key.getEncoded();
            if (Arrays.equals(output, enc) == false) {
                System.out.println();
                System.out.println("expected: " + toString(output));
                System.out.println("actual:   " + toString(enc));
                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 18
Source File: TestMasterSecret.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    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 19
Source File: TestMasterSecret.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void main(Provider provider) throws Exception {
    if (provider.getService("KeyGenerator", "SunTlsMasterSecret") == null) {
        System.out.println("Not supported by provider, skipping");
        return;
    }
    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 20
Source File: TransformService.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a <code>TransformService</code> that supports the specified
 * algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type
 * (ex: DOM).
 *
 * <p>This method uses the standard JCA provider lookup mechanism to
 * locate and instantiate a <code>TransformService</code> implementation
 * of the desired algorithm and <code>MechanismType</code> service
 * attribute. It traverses the list of registered security
 * <code>Provider</code>s, starting with the most preferred
 * <code>Provider</code>. A new <code>TransformService</code> object
 * from the first <code>Provider</code> that supports the specified
 * algorithm and mechanism type is returned.
 *
 * <p> Note that the list of registered providers may be retrieved via
 * the {@link Security#getProviders() Security.getProviders()} method.
 *
 * @implNote
 * The JDK Reference Implementation additionally uses the
 * {@code jdk.security.provider.preferred}
 * {@link Security#getProperty(String) Security} property to determine
 * the preferred provider order for the specified algorithm. This
 * may be different than the order of providers returned by
 * {@link Security#getProviders() Security.getProviders()}.
 *
 * @param algorithm the URI of the algorithm
 * @param mechanismType the type of the XML processing mechanism and
 *   representation
 * @return a new <code>TransformService</code>
 * @throws NullPointerException if <code>algorithm</code> or
 *   <code>mechanismType</code> is  <code>null</code>
 * @throws NoSuchAlgorithmException if no <code>Provider</code> supports a
 *   <code>TransformService</code> implementation for the specified
 *   algorithm and mechanism type
 * @see Provider
 */
public static TransformService getInstance
    (String algorithm, String mechanismType)
    throws NoSuchAlgorithmException {
    if (mechanismType == null || algorithm == null) {
        throw new NullPointerException();
    }
    boolean dom = false;
    if (mechanismType.equals("DOM")) {
        dom = true;
    }

    Provider[] provs = Security.getProviders();
    for (Provider p : provs) {
        Service s = p.getService("TransformService", algorithm);
        if (s != null) {
            String value = s.getAttribute("MechanismType");
            if ((value == null && dom) ||
                (value != null && value.equals(mechanismType))) {
                Object obj = s.newInstance(null);
                if (obj instanceof TransformService) {
                    TransformService ts = (TransformService) obj;
                    ts.algorithm = algorithm;
                    ts.mechanism = mechanismType;
                    ts.provider = p;
                    return ts;
                }
            }
        }
    }
    throw new NoSuchAlgorithmException
        (algorithm + " algorithm and " + mechanismType
             + " mechanism not available");
}