/** * Get a password factory instance. The returned password factory object will implement the given * algorithm. * * @param algorithm the name of the algorithm * @param providerSupplier the provider supplier to search * @return a password factory instance * @throws NoSuchAlgorithmException if the given algorithm has no available implementations */ public static PasswordFactory getInstance(String algorithm, Supplier<Provider[]> providerSupplier) throws NoSuchAlgorithmException { for (Provider provider : providerSupplier.get()) { final Provider.Service service = provider.getService("PasswordFactory", algorithm); if (service != null) { return new PasswordFactory( (PasswordFactorySpi) service.newInstance(null), provider, algorithm); } } throw log.noSuchAlgorithmInvalidAlgorithm(algorithm); }
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."); }
@Override public void main(Provider p) throws Exception { if (p.getService("KeyFactory", "EC") == null) { System.out.println("Provider does not support EC, skipping"); return; } int[] keyLengths = {192, 163, 409, 521}; int len = 0; if (getNSSECC() == ECCState.Basic) { System.out.println("NSS Basic ECC only. Skipping 192, 163, & 409"); len = 3; } KeyFactory kf = KeyFactory.getInstance("EC", p); for (; keyLengths.length > len; len++) { System.out.println("Length " + keyLengths[len]); KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p); kpg.initialize(keyLengths[len]); KeyPair kp = kpg.generateKeyPair(); test(kf, kp.getPrivate()); test(kf, kp.getPublic()); } }
/** * Get a password factory instance. The returned password factory object will implement the given * algorithm. * * @param algorithm the name of the algorithm * @param provider the provider to use * @return a password factory instance * @throws NoSuchAlgorithmException if the given algorithm has no available implementations */ public static PasswordFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException { final Provider.Service service = provider.getService("PasswordFactory", algorithm); if (service == null) throw log.noSuchAlgorithmInvalidAlgorithm(algorithm); return new PasswordFactory((PasswordFactorySpi) service.newInstance(null), provider, algorithm); }
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"); }