import java.security.Provider; import java.security.Security; public class SecurityListings { public static void main(String[] args) { for (Provider provider : Security.getProviders()) { System.out.println("Provider: " + provider.getName()); for (Provider.Service service : provider.getServices()) { System.out.println(" Algorithm: " + service.getAlgorithm()); } } } }
/** * Returns a {@code Set} of all registered algorithms for the specified * cryptographic service. {@code "Signature"}, {@code "Cipher"} and {@code * "KeyStore"} are examples for such kind of services. * * @param serviceName * the case-insensitive name of the service. * @return a {@code Set} of all registered algorithms for the specified * cryptographic service, or an empty {@code Set} if {@code * serviceName} is {@code null} or if no registered provider * provides the requested service. */ public static Set<String> getAlgorithms(String serviceName) { Set<String> result = new HashSet<String>(); // compatibility with RI if (serviceName == null) { return result; } for (Provider provider : getProviders()) { for (Provider.Service service: provider.getServices()) { if (service.getType().equalsIgnoreCase(serviceName)) { result.add(service.getAlgorithm()); } } } return result; }
import java.security.Provider; import java.security.Security; public class WindowsSecureRandom extends Provider { private static final String MSCAPI = "sun.security.mscapi.PRNG"; private WindowsSecureRandom() { super("WindowsSecureRandom Provider", 1.0, null); putService(new Service(this, "SecureRandom", "Windows-PRNG", MSCAPI, null, null)); } public static void register() { if (System.getProperty("os.name").contains("Windows")) { try { Class.forName(MSCAPI); Security.insertProviderAt(new WindowsSecureRandom(), 1); } catch (ClassNotFoundException e) { // Fallback to default implementation } } } }
final List <Provider> aSortedProviders = CollectionHelper.getSorted (Security.getProviders (), Comparator.comparing (Provider::getName) .thenComparing (Comparator.comparingDouble (Provider::getVersion))); aRow.addCell (aSecurityProvider.getName ()); aRow.addCell (Double.toString (aSecurityProvider.getVersion ())); aRow.addCell (aSecurityProvider.getInfo ()); aRow.addCell (aService.getType ()); aRow.addCell (aService.getAlgorithm ()); aRow.addCell (aService.getClassName ()); aRow.addCell (aService.getType ()); aRow.addCell (aService.getAlgorithm ()); aRow.addCell (aService.getClassName ()); for (final Service aService : aSecurityProvider.getServices ()) if ("SSLContext".equals (aService.getType ())) aRow.addCell (aService.getType ()); aRow.addCell (aService.getAlgorithm ()); try final SSLContext aSSLCtx = SSLContext.getInstance (aService.getAlgorithm ()); if (!"Default".equals (aService.getAlgorithm ()))
public class Sample { static { Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1); } public void printProviders() { for (Provider provider : Security.getProviders()) { System.out.println("Provider: " + provider.getName()); for (Provider.Service service : provider.getServices()) { System.out.println(" Algorithm: " + service.getAlgorithm()); } } } }
for (Provider provider : Security.getProviders()) { System.out.println("Provider: " + provider.getName() + " version: " + provider.getVersion()); for (Provider.Service service : provider.getServices()) { System.out.printf(" Type : %-30s Algorithm: %-30s\n", service.getType(), service.getAlgorithm()); } }
import java.security.Provider; import java.security.Provider.Service; import java.security.Security; import java.util.Set; public class MainActivity { public static void main(String[] args) { Provider [] providerList = Security.getProviders(); for (Provider provider : providerList) { System.out.println("Name: " + provider.getName()); System.out.println("Information:\n" + provider.getInfo()); Set<Service> serviceList = provider.getServices(); for (Service service : serviceList) { System.out.println("Service Type: " + service.getType() + " Algorithm " + service.getAlgorithm()); } } } }
if (providerName != null && ! providerName.equals(provider.getName())) { continue; Set<Service> services = provider.getServices(); if (services != null) { for (Provider.Service service : services) { if (SERVICE_TYPE.equals(service.getType())) { String protocolName = service.getAlgorithm(); List<Provider> providerList = preferredProviderByAlgorithm.computeIfAbsent(protocolName.toUpperCase(Locale.ENGLISH), s -> new ArrayList<>()); providerList.add(provider);
private static String asString(Service s) { String str = "Provider = " + s.getProvider().getName() + SEP; str += "Type = " + s.getType() + SEP; str += "Algorithm = " + s.getAlgorithm() + SEP; str += "Class = " + s.getClassName(); if (isSignature(s) || isCipher(s) || isKeyAgreement(s)) { str += SEP + "SupportedKeyClasses = " + Arrays.toString(getSupportedKeyClasses(s)); } return str; }
/** * Adds information about provider services into HashMap. */ public static synchronized void initServiceInfo(Provider p) { for (Provider.Service service : p.getServices()) { String type = service.getType(); if (cachedSecureRandomService == null && type.equals("SecureRandom")) { cachedSecureRandomService = service; } String key = type + "." + service.getAlgorithm().toUpperCase(Locale.US); if (!services.containsKey(key)) { services.put(key, service); } for (String alias : Engine.door.getAliases(service)) { key = type + "." + alias.toUpperCase(Locale.US); if (!services.containsKey(key)) { services.put(key, service); } } } }
/** * Get a {@code CredentialStore} instance. The returned CredentialStore object will implement the given algorithm. * * @param algorithm the name of the algorithm * @param provider the provider to use * @return a {@code CredentialStore} instance * @throws NoSuchAlgorithmException if the given algorithm has no available implementations */ public static CredentialStore getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException { final Provider.Service service = provider.getService(CREDENTIAL_STORE_TYPE, algorithm); if (service == null) throw new NoSuchAlgorithmException(algorithm); return new CredentialStore(provider, (CredentialStoreSpi) service.newInstance(null), algorithm); }
s.addAlias(aliasName); if (propertyAliasTable == null) { propertyAliasTable = new LinkedHashMap<String, Service>(256); List<String> l = new ArrayList<String>(); l.add(aliasName); s = new Provider.Service(this, serviceName, algorithm, className, l, new HashMap<String, String>()); propertyServiceTable.put(propertyServiceTableKey, s); propertyAliasTable = new LinkedHashMap<String, Service>(256); s.className = value; } else { s = new Provider.Service(this, serviceName, algorithm, value, Collections.<String>emptyList(), Collections.<String,String>emptyMap()); s.putAttribute(attribute, value); } else { String className = (String) changedProperties Map<String, String> m = new HashMap<String, String>(); m.put(attribute, value); s = new Provider.Service(this, serviceName, algorithm, className, new ArrayList<String>(), m); if (propertyServiceTable == null) {
/** * Adds a {@code Service} to this {@code Provider}. If a service with the * same name was registered via this method, it is replace. * * @param s * the {@code Service} to register */ protected synchronized void putService(Provider.Service s) { if (s == null) { throw new NullPointerException("s == null"); } if ("Provider".equals(s.getType())) { // Provider service type cannot be added return; } servicesChanged(); if (serviceTable == null) { serviceTable = new LinkedHashMap<String, Service>(128); } serviceTable.put(key(s.type, s.algorithm), s); if (s.aliases != null) { if (aliasTable == null) { aliasTable = new LinkedHashMap<String, Service>(256); } for (String alias : s.getAliases()) { aliasTable.put(key(s.type, alias), s); } } serviceInfoToProperties(s); }
private void putCredentialStoreProviderImplementations() { final List<String> emptyList = Collections.emptyList(); final Map<String, String> emptyMap = Collections.emptyMap(); putService(new Service(this, CredentialStore.CREDENTIAL_STORE_TYPE, KeyStoreCredentialStore.KEY_STORE_CREDENTIAL_STORE, "org.wildfly.security.credential.store.impl.KeyStoreCredentialStore", emptyList, emptyMap)); putService(new Service(this, CredentialStore.CREDENTIAL_STORE_TYPE, VaultCredentialStore.VAULT_CREDENTIAL_STORE, "org.wildfly.security.credential.store.impl.VaultCredentialStore", emptyList, emptyMap)); putService(new Service(this, CredentialStore.CREDENTIAL_STORE_TYPE, MapCredentialStore.MAP_CREDENTIAL_STORE, "org.wildfly.security.credential.store.impl.MapCredentialStore", emptyList, emptyMap)); }
private void putKeyStoreImplementations() { final List<String> emptyList = Collections.emptyList(); final Map<String, String> emptyMap = Collections.emptyMap(); putService(new Service(this, "KeyStore", "PasswordFile", "org.wildfly.security.keystore.PasswordKeyStoreSpi", emptyList, emptyMap)); }
/** * Default constructor for this security provider. */ public WildFlyElytronProvider() { super("WildFlyElytron", "1.0", "WildFly Elytron Provider"); putHttpAuthenticationMechanismImplementations(); putKeyStoreImplementations(); putPasswordImplementations(); putSaslMechanismImplementations(); putCredentialStoreProviderImplementations(); putAlgorithmParametersImplementations(); put("Alg.Alias.Data.OID.1.2.840.113549.1.7.1", "Data"); putService(new Service(this, "SecretKeyFactory", "1.2.840.113549.1.7.1", "org.wildfly.security.key.RawSecretKeyFactory", Collections.emptyList(), Collections.emptyMap())); putService(new Service(this, "MessageDigest", "SHA-512-256", "org.wildfly.security.digest.SHA512_256MessageDigest", Collections.emptyList(), Collections.emptyMap())); }
/** * Find the first provider from the supplier which provides the given service type and algorithm name. * * If a providerName is specified the match will only be tested against providers with the name specified. * * @param providerSupplier the provider supplier (must not be {@code null}) * @param providerName the name of the provider, can be {@code null} * @param serviceType the service type (must not be {@code null}) * @param algorithm the algorithm name (must not be {@code null}) * @return the provider, or {@code null} if none is found */ public static Provider findProvider(Supplier<Provider[]> providerSupplier, String providerName, String serviceType, String algorithm) { final Provider.Service service = findProviderService(providerSupplier, providerName, serviceType, algorithm); return service == null ? null : service.getProvider(); }
/** * Constructs a new {@code SecureRandom} that uses the default algorithm. */ public SecureRandom() { super(0); Provider.Service service = Services.getSecureRandomService(); if (service == null) { this.provider = null; this.secureRandomSpi = new SHA1PRNG_SecureRandomImpl(); this.algorithm = "SHA1PRNG"; } else { try { this.provider = service.getProvider(); this.secureRandomSpi = (SecureRandomSpi)service.newInstance(null); this.algorithm = service.getAlgorithm(); } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Finds the appropriate service implementation and returns an * {@code SpiAndProvider} instance containing a reference to SPI * and its {@code Provider} */ public SpiAndProvider getInstance(String algorithm, Object param) throws NoSuchAlgorithmException { if (algorithm == null) { throw new NoSuchAlgorithmException("Null algorithm name"); } int newCacheVersion = Services.getCacheVersion(); Provider.Service service; ServiceCacheEntry cacheEntry = this.serviceCache; if (cacheEntry != null && cacheEntry.algorithm.equalsIgnoreCase(algorithm) && newCacheVersion == cacheEntry.cacheVersion) { service = cacheEntry.service; } else { if (Services.isEmpty()) { throw notFound(serviceName, algorithm); } String name = this.serviceName + "." + algorithm.toUpperCase(Locale.US); service = Services.getService(name); if (service == null) { throw notFound(serviceName, algorithm); } this.serviceCache = new ServiceCacheEntry(algorithm, newCacheVersion, service); } return new SpiAndProvider(service.newInstance(param), service.getProvider()); }
putService(new Service(this, ALG_PARAMS_TYPE, "RSA", "org.wildfly.security.key.RSAParameterSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_CRYPT_MD5, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SUN_CRYPT_MD5, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SUN_CRYPT_MD5_BARE_SALT, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_CRYPT_SHA_256, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_CRYPT_SHA_512, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_DIGEST_MD5, "org.wildfly.security.password.impl.DigestPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_DIGEST_SHA, "org.wildfly.security.password.impl.DigestPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_DIGEST_SHA_256, "org.wildfly.security.password.impl.DigestPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_DIGEST_SHA_384, "org.wildfly.security.password.impl.DigestPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_DIGEST_SHA_512, "org.wildfly.security.password.impl.DigestPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_DIGEST_SHA_512_256, "org.wildfly.security.password.impl.DigestPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_PASSWORD_SALT_DIGEST_MD5, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_PASSWORD_SALT_DIGEST_SHA_1, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_PASSWORD_SALT_DIGEST_SHA_256, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_PASSWORD_SALT_DIGEST_SHA_384, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_PASSWORD_SALT_DIGEST_SHA_512, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SALT_PASSWORD_DIGEST_MD5, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SALT_PASSWORD_DIGEST_SHA_1, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SALT_PASSWORD_DIGEST_SHA_256, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SALT_PASSWORD_DIGEST_SHA_384, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SALT_PASSWORD_DIGEST_SHA_512, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_CRYPT_DES, "org.wildfly.security.password.impl.SaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_BSD_CRYPT_DES, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_BCRYPT, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SCRAM_SHA_1, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SCRAM_SHA_256, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap)); putService(new Service(this, ALG_PARAMS_TYPE, ALGORITHM_SCRAM_SHA_384, "org.wildfly.security.password.impl.IteratedSaltedPasswordAlgorithmParametersSpiImpl", emptyList, emptyMap));