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

The following examples show how to use java.security.Provider#getServices() . 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: ApnsUtils.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
public static String getAlgorithm() {
    List<String> keys = new LinkedList<>();
    List<String> trusts = new LinkedList<>();
    for (Provider p : Security.getProviders()) {
        if (p.getServices() != null) {
            for (Service s : p.getServices()) {
                if ("KeyManagerFactory".equals(s.getType()) && s.getAlgorithm().endsWith("509")) {
                    keys.add(s.getAlgorithm());
                } else if ("TrustManagerFactory".equals(s.getType()) && s.getAlgorithm().endsWith("509")) {
                    trusts.add(s.getAlgorithm());
                }
            }
        }
    }
    keys.retainAll(trusts);
    return keys.get(0);
}
 
Example 2
Source File: SecurityProcessor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Determine the classes that make up the provider and its services
 *
 * @param providerName - JCA provider name
 * @return class names that make up the provider and its services
 */
private List<String> registerProvider(String providerName) {
    ArrayList<String> providerClasses = new ArrayList<>();
    Provider provider = Security.getProvider(providerName);
    providerClasses.add(provider.getClass().getName());
    Set<Provider.Service> services = provider.getServices();
    for (Provider.Service service : services) {
        String serviceClass = service.getClassName();
        providerClasses.add(serviceClass);
        // Need to pull in the key classes
        String supportedKeyClasses = service.getAttribute("SupportedKeyClasses");
        if (supportedKeyClasses != null) {
            String[] keyClasses = supportedKeyClasses.split("\\|");
            providerClasses.addAll(Arrays.asList(keyClasses));
        }
    }
    return providerClasses;
}
 
Example 3
Source File: TestProcessor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@BuildStep
void registerSUNProvider(BuildProducer<ReflectiveClassBuildItem> classes) {
    Provider provider = Security.getProvider("SUN");
    ArrayList<String> providerClasses = new ArrayList<>();
    providerClasses.add(provider.getClass().getName());
    Set<Provider.Service> services = provider.getServices();
    for (Provider.Service service : services) {
        String serviceClass = service.getClassName();
        providerClasses.add(serviceClass);
        // Need to pull in the key classes
        String supportedKeyClasses = service.getAttribute("SupportedKeyClasses");
        if (supportedKeyClasses != null) {
            String[] keyClasses = supportedKeyClasses.split("\\|");
            providerClasses.addAll(Arrays.asList(keyClasses));
        }
    }
    for (String className : providerClasses) {
        classes.produce(new ReflectiveClassBuildItem(true, true, className));
        log.debugf("Register SUN.provider class: %s", className);
    }
}
 
Example 4
Source File: DSAKeyUtil.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SUN");
    System.out.println(provider.getInfo());
    for (Provider.Service service : provider.getServices()) {
        System.out.println(service);
    }

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
    kpg.initialize(2048);
    System.out.println("DSA.provider: " + kpg.getProvider());
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    System.out.println("DSA.provider: " + keyFactory.getProvider());

    KeyPair pair = kpg.genKeyPair();
    DSAPublicKey publicKey = (DSAPublicKey) pair.getPublic();
    byte[] encoded = publicKey.getEncoded();
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encoded);
    DSAPublicKey publicKey1 = (DSAPublicKey) keyFactory.generatePublic(publicKeySpec);
    System.out.printf("keys are equal: %s%n", publicKey1.equals(publicKey));

    String base64 = Base64.getEncoder().encodeToString(encoded);
    System.out.println(base64);
}
 
Example 5
Source File: FileCache.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private static MessageDigest findAlternativeMessageDigest() {
    if ("MD5".equals(hashAlgorithm)) {
        for (Provider provider : Security.getProviders()) {
            for (Provider.Service service : provider.getServices()) {
                hashAlgorithm = service.getAlgorithm();
                try {
                    MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm);
                    if (messageDigest != null)
                        return messageDigest;
                } catch (NoSuchAlgorithmException ignored) {
                }
            }
        }
    }
    return null;
}
 
Example 6
Source File: Main.java    From secure-tomcat-datasourcefactory with Apache License 2.0 5 votes vote down vote up
private static void listCiphers(Queue<String> arguments) {
	for (Provider provider : Security.getProviders()) {
           for (Provider.Service service : provider.getServices()) {
               if (service.getType().equals("Cipher")) {
               	System.out.println("Provider: " + provider.getName() + "  Algorithm: " + service.getAlgorithm());
               }
           }
       }
}
 
Example 7
Source File: ProviderAttributeDefinition.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void addServices(final ModelNode providerModel, final Provider provider) {
    ModelNode servicesModel = providerModel.get(ElytronDescriptionConstants.SERVICES);

    for (Service current : provider.getServices()) {
        ModelNode serviceModel = new ModelNode();
        serviceModel.get(ElytronDescriptionConstants.TYPE).set(current.getType());
        serviceModel.get(ElytronDescriptionConstants.ALGORITHM).set(current.getAlgorithm());
        serviceModel.get(ElytronDescriptionConstants.CLASS_NAME).set(current.getClassName());

        servicesModel.add(serviceModel);
    }

}
 
Example 8
Source File: DSSUtils.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * This method lists all defined security providers.
 */
public static void printSecurityProviders() {
	final Provider[] providers = Security.getProviders();
	for (final Provider provider : providers) {
		LOG.info("PROVIDER: {}", provider.getName());
		final Set<Provider.Service> services = provider.getServices();
		for (final Provider.Service service : services) {
			LOG.info("\tALGORITHM: {} / {} / {}", service.getAlgorithm(), service.getType(),
					service.getClassName());
		}
	}
}
 
Example 9
Source File: SecureRandomTest.java    From wycheproof with Apache License 2.0 5 votes vote down vote up
/** Returns a list of all implemented services of SecureRandom. */
Collection<Provider.Service> secureRandomServices() {
  // TODO(bleichen): Check if all instances of SecureRandom are actually
  //   listed as services. In particular the default SecureRandom() and
  //   SecureRandom.getInstanceStrong() may not not be registered.
  ArrayList<Provider.Service> result = new ArrayList<Provider.Service>();
  for (Provider p : Security.getProviders()) {
    for (Provider.Service service : p.getServices()) {
      if (service.getType().equals("SecureRandom")) {
        result.add(service);
      }
    }
  }
  return result;
}
 
Example 10
Source File: TraceAndSpanIdGeneratorTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Test
public void getRandomInstance_should_return_requested_instance_if_available() {
    // given: all available SecureRandom algorithm providers
    Provider[] availableProviders = Security.getProviders();
    assertThat(availableProviders).isNotNull();
    assertThat(availableProviders.length).isGreaterThanOrEqualTo(1);

    // Ignore some algorithms because they fail on OSX if you're not an admin.
    // We don't care that *all* algorithms are available at all times on all platforms, just that the code does what it's supposed to do.
    List<String> excludedAlgorithms = Collections.singletonList("NativePRNGNonBlocking");

    Set<String> availableSecureRandomAlgorithms = new HashSet<>();

    for (Provider provider : availableProviders) {
        Set<Provider.Service> services = provider.getServices();
        for (Provider.Service service : services) {
            String type = service.getType();
            if ("SecureRandom".equals(type) && !excludedAlgorithms.contains(service.getAlgorithm()))
                availableSecureRandomAlgorithms.add(service.getAlgorithm());
        }
    }

    assertThat(availableSecureRandomAlgorithms).isNotEmpty();

    for (String algorithm : availableSecureRandomAlgorithms) {
        // when: we ask getRandomInstance for the algorithm
        Random randomGenerator = TraceAndSpanIdGenerator.getRandomInstance(algorithm);

        // then: we get back a valid SecureRandom that uses the requested algorithm
        assertThat(randomGenerator).isNotNull();
        assertThat(randomGenerator).isInstanceOf(SecureRandom.class);
        //noinspection ConstantConditions
        assertThat(((SecureRandom)randomGenerator).getAlgorithm()).isEqualTo(algorithm);
    }
}
 
Example 11
Source File: PKCS11Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns supported algorithms of specified type.
 */
static List<String> getSupportedAlgorithms(String type, String alg,
        Provider p) {
    // prepare a list of supported algorithms
    List<String> algorithms = new ArrayList<>();
    Set<Provider.Service> services = p.getServices();
    for (Provider.Service service : services) {
        if (service.getType().equals(type)
                && service.getAlgorithm().startsWith(alg)) {
            algorithms.add(service.getAlgorithm());
        }
    }
    return algorithms;
}
 
Example 12
Source File: NoSync.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    for (Provider p : Security.getProviders()) {
        for (Provider.Service s : p.getServices()) {
            if (s.getType().equals("SecureRandom") &&
                    !s.getAlgorithm().contains("Block")) {
                test(SecureRandom.getInstance(s.getAlgorithm(), p));
            }
        }
    }
    Security.setProperty("securerandom.drbg.config", "HMAC_DRBG");
    test(SecureRandom.getInstance("DRBG"));
    Security.setProperty("securerandom.drbg.config", "CTR_DRBG");
    test(SecureRandom.getInstance("DRBG"));
}
 
Example 13
Source File: Main.java    From secure-tomcat-datasourcefactory with Apache License 2.0 5 votes vote down vote up
private static void listKeyGenerators(Queue<String> arguments) {
	for (Provider provider : Security.getProviders()) {
           for (Provider.Service service : provider.getServices()) {
               if (service.getType().equals("KeyGenerator")) {
               	System.out.println("Provider: " + provider.getName() + "  Algorithm: " + service.getAlgorithm());
               }
           }
       }
}
 
Example 14
Source File: SystemTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static void SignatureAlgorithms() {
    try {
        for (Provider provider : Security.getProviders()) {
            for (Provider.Service service : provider.getServices()) {
                if (service.getType().equals("Signature")) {
                    logger.debug(service.getAlgorithm());
                }
            }
        }
    } catch (Exception e) {
        logger.debug(e.toString());
    }
}
 
Example 15
Source File: ShowAlgo.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
public static void main(String args[])
{
  Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

  Provider[] p_a = Security.getProviders();
  for(Provider p : p_a)
  {
    System.out.println("Provider: " + p.toString());
    System.out.println("-----------------------------------");
    for(Provider.Service ps : p.getServices())
    {
      System.out.println("  " + ps.toString());
    }
  }
}
 
Example 16
Source File: TimeDigest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static private void testDigest() {
  try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    Provider p = md.getProvider();
    Set<Provider.Service> services = p.getServices();
    for (Provider.Service service : services) {
      System.out.println("Service=" + service);
    }
  } catch (NoSuchAlgorithmException e) {
    e.printStackTrace(); // To change body of catch statement use File | Settings | File Templates.
  }
}
 
Example 17
Source File: ServerPropertiesTest.java    From p4ic4idea with Apache License 2.0 4 votes vote down vote up
/**
 * Test server properties
 * @throws P4JavaException 
 */
@Test
public void testServerProperties() {

	String[] serverUris = {
			"p4java://eng-p4java-vm.das.perforce.com:20121?socketPoolSize=10&testKey1=testVal1",
			"p4javassl://eng-p4java-vm.das.perforce.com:30121?socketPoolSize=10&testKey1=testVal1",
			"p4jrpc://eng-p4java-vm.das.perforce.com:20121?socketPoolSize=10&testKey1=testVal1",
			"p4jrpcssl://eng-p4java-vm.das.perforce.com:30121?socketPoolSize=10&testKey1=testVal1",
			"p4jrpcnts://eng-p4java-vm.das.perforce.com:20121?socketPoolSize=10&testKey1=testVal1",
			"p4jrpcntsssl://eng-p4java-vm.das.perforce.com:30121?socketPoolSize=10&testKey1=testVal1" };

	try {
		// List all the providers and the algorithms supporter
		for (Provider provider : Security.getProviders()) {
		    debugPrint("Provider: " + provider.getName());
			for (Provider.Service service : provider.getServices()) {
			    debugPrint("  Algorithm: " + service.getAlgorithm());
			}
		}

		// List supported protocol versions for this connection
		Socket socket = SSLSocketFactory.getDefault().createSocket();
		String[] supportedProtocols = ((SSLSocket) socket)
				.getSupportedProtocols();
		for (String supportedProtocol : supportedProtocols) {
		    debugPrint("Supported Protocol Version: "
					+ supportedProtocol);
		}
		// List enabled protocol versions for this connection
		String[] enabledProtocols = ((SSLSocket) socket)
				.getEnabledProtocols();
		for (String enabledProtocol : enabledProtocols) {
		    debugPrint("Enabled Protocol Version: "
					+ enabledProtocol);
		}

		Properties props = new Properties();

		// props.put("secureSocketTrustAll", "false");
		// props.put("secureSocketProtocol", "TLS");
		props.put("secureSocketProtocol", "SSL");
		// props.put("secureSocketSetEnabledProptocols", "false");
		// props.put("secureSocketEnabledProtocols", "SSLv3, TLSv1");
		props.put("secureSocketEnabledProtocols", "TLSv1, SSLv3");
		// props.put("secureSocketEnabledProtocols", "TLSv1");

		for (String serverUri : serverUris) {
			server = ServerFactory.getOptionsServer(serverUri, props);
			assertNotNull(server);

			// Register callback
			server.registerCallback(new MockCommandCallback());
			setupServer(p4d.getRSHURL(), userName, password, true, props);
			client = server.getClient("p4TestUserWS20112");
			assertNotNull(client);
			server.setCurrentClient(client);
			IServerInfo serverInfo = server.getServerInfo();
			assertNotNull(serverInfo);
		}

	} catch (Exception e) {
	    StringWriter sw = new StringWriter();
	    e.printStackTrace(new PrintWriter(sw));
		fail("Unexpected exception: " + e.getLocalizedMessage() +
		        "\n" + sw.toString() );
	}
}
 
Example 18
Source File: SignatureUtils.java    From cstc with GNU General Public License v3.0 4 votes vote down vote up
private void getSignatureInfos() {
	for (Provider provider : Security.getProviders())
		for (Service service : provider.getServices())
			if (service.getType().equals("Signature"))
	            algos.add(service.getAlgorithm());
}