private static Signature getInstanceRSA(Provider p) throws NoSuchAlgorithmException { // try Signature first Service s = p.getService("Signature", RSA_SIGNATURE); if (s != null) { Instance instance = GetInstance.getInstance(s, SignatureSpi.class); return getInstance(instance, RSA_SIGNATURE); } // check Cipher try { Cipher c = Cipher.getInstance(RSA_CIPHER, p); return new Delegate(new CipherAdapter(c), RSA_SIGNATURE); } catch (GeneralSecurityException e) { // throw Signature style exception message to avoid confusion, // but append Cipher exception as cause throw new NoSuchAlgorithmException("no such algorithm: " + RSA_SIGNATURE + " for provider " + p.getName(), e); } }
private static SignatureSpi newInstance(Service s) throws NoSuchAlgorithmException { if (s.getType().equals("Cipher")) { // must be NONEwithRSA try { Cipher c = Cipher.getInstance(RSA_CIPHER, s.getProvider()); return new CipherAdapter(c); } catch (NoSuchPaddingException e) { throw new NoSuchAlgorithmException(e); } } else { Object o = s.newInstance(null); if (o instanceof SignatureSpi == false) { throw new NoSuchAlgorithmException ("Not a SignatureSpi: " + o.getClass().getName()); } return (SignatureSpi)o; } }
private static Set<Object> getFactories(String serviceName) { HashSet<Object> result = new HashSet<Object>(); if ((serviceName == null) || (serviceName.length() == 0) || (serviceName.endsWith("."))) { return result; } Provider[] provs = Security.getProviders(); Object fac; for (Provider p : provs) { Iterator<Service> iter = p.getServices().iterator(); while (iter.hasNext()) { Service s = iter.next(); if (s.getType().equals(serviceName)) { try { fac = loadFactory(s); if (fac != null) { result.add(fac); } } catch (Exception ignore) { } } } } return Collections.unmodifiableSet(result); }
private static Object loadFactory(Service service) throws SaslException { try { /* * Load the implementation class with the same class loader * that was used to load the provider. * In order to get the class loader of a class, the * caller's class loader must be the same as or an ancestor of * the class loader being returned. Otherwise, the caller must * have "getClassLoader" permission, or a SecurityException * will be thrown. */ return service.newInstance(null); } catch (InvalidParameterException | NoSuchAlgorithmException e) { throw new SaslException("Cannot instantiate service " + service, e); } }
public Iterator<Service> iterator() { return new Iterator<Service>() { int index; public boolean hasNext() { return tryGet(index) != null; } public Service next() { Service s = tryGet(index); if (s == null) { throw new NoSuchElementException(); } index++; return s; } public void remove() { throw new UnsupportedOperationException(); } }; }
public static Instance getInstance(String type, Class<?> clazz, String algorithm, Object param) throws NoSuchAlgorithmException { List<Service> services = getServices(type, algorithm); NoSuchAlgorithmException failure = null; for (Service s : services) { try { return getInstance(s, clazz, param); } catch (NoSuchAlgorithmException e) { failure = e; } } if (failure != null) { throw failure; } else { throw new NoSuchAlgorithmException (algorithm + " " + type + " not available"); } }
/** * Returns an array of objects: the first object in the array is * an instance of an implementation of the requested algorithm * and type, and the second object in the array identifies the provider * of that implementation. * The <code>provName</code> argument can be null, in which case all * configured providers will be searched in order of preference. */ static Object[] getImpl(String algName, String engineType, String provName) throws NoSuchAlgorithmException, NoSuchProviderException { Service service; if (provName != null) { ProviderList list = Providers.getProviderList(); Provider prov = list.getProvider(provName); if (prov == null) { throw new NoSuchProviderException("No such provider: " + provName); } service = prov.getService(engineType, algName); } else { service = getService(engineType, algName); } if (service == null) { throw new NoSuchAlgorithmException("Algorithm " + algName + " not available"); } return getImpl1(algName, engineType, service); }
public static Service getService(String type, String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { if ((provider == null) || (provider.length() == 0)) { throw new IllegalArgumentException("missing provider"); } Provider p = Providers.getProviderList().getProvider(provider); if (p == null) { throw new NoSuchProviderException("no such provider: " + provider); } Service s = p.getService(type, algorithm); if (s == null) { throw new NoSuchAlgorithmException("no such algorithm: " + algorithm + " for provider " + provider); } return s; }
static Instance getInstance(String type, Class<?> clazz, String algorithm) throws NoSuchAlgorithmException { List<Service> services = GetInstance.getServices(type, algorithm); NoSuchAlgorithmException failure = null; for (Service s : services) { if (canUseProvider(s.getProvider()) == false) { // allow only signed providers continue; } try { Instance instance = GetInstance.getInstance(s, clazz); return instance; } catch (NoSuchAlgorithmException e) { failure = e; } } throw new NoSuchAlgorithmException("Algorithm " + algorithm + " not available", failure); }
private KeyGenerator(String algorithm) throws NoSuchAlgorithmException { this.algorithm = algorithm; List<Service> list = GetInstance.getServices("KeyGenerator", algorithm); serviceIterator = list.iterator(); initType = I_NONE; // fetch and instantiate initial spi if (nextSpi(null, false) == null) { throw new NoSuchAlgorithmException (algorithm + " KeyGenerator not available"); } if (!skipDebug && pdebug != null) { pdebug.println("KeyGenerator." + algorithm + " algorithm from: " + getProviderName()); } }
/** * 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()); }
private KeyGenerator(String algorithm) throws NoSuchAlgorithmException { this.algorithm = algorithm; List<Service> list = GetInstance.getServices("KeyGenerator", algorithm); serviceIterator = list.iterator(); initType = I_NONE; // fetch and instantiate initial spi if (nextSpi(null, false) == null) { throw new NoSuchAlgorithmException (algorithm + " KeyGenerator not available"); } if (!skipDebug && pdebug != null) { pdebug.println("KeyGenerator." + algorithm + " algorithm from: " + this.provider.getName()); } }
private static boolean isSpi(Service s) { if (s.getType().equals("Cipher")) { // must be a CipherSpi, which we can wrap with the CipherAdapter return true; } String className = s.getClassName(); Boolean result = signatureInfo.get(className); if (result == null) { try { Object instance = s.newInstance(null); // Signature extends SignatureSpi // so it is a "real" Spi if it is an // instance of SignatureSpi but not Signature boolean r = (instance instanceof SignatureSpi) && (instance instanceof Signature == false); if ((debug != null) && (r == false)) { debug.println("Not a SignatureSpi " + className); debug.println("Delayed provider selection may not be " + "available for algorithm " + s.getAlgorithm()); } result = Boolean.valueOf(r); signatureInfo.put(className, result); } catch (Exception e) { // something is wrong, assume not an SPI return false; } } return result.booleanValue(); }
private KeyFactory(String algorithm) throws NoSuchAlgorithmException { this.algorithm = algorithm; List<Service> list = GetInstance.getServices("KeyFactory", algorithm); serviceIterator = list.iterator(); // fetch and instantiate initial spi if (nextSpi(null) == null) { throw new NoSuchAlgorithmException (algorithm + " KeyFactory not available"); } }
/** * Gets a default PRNG algorithm by looking through all registered * providers. Returns the first PRNG algorithm of the first provider that * has registered a SecureRandom implementation, or null if none of the * registered providers supplies a SecureRandom implementation. */ private static String getPrngAlgorithm() { for (Provider p : Providers.getProviderList().providers()) { for (Service s : p.getServices()) { if (s.getType().equals("SecureRandom")) { return s.getAlgorithm(); } } } return null; }
Delegate(Instance instance, Iterator<Service> serviceIterator, String algorithm) { super(algorithm); spi = (KeyPairGeneratorSpi)instance.impl; provider = instance.provider; this.serviceIterator = serviceIterator; initType = I_NONE; if (!skipDebug && pdebug != null) { pdebug.println("KeyPairGenerator." + algorithm + " algorithm from: " + provider.getName()); } }
/** * Return a Service describing an implementation of the specified * algorithm from the Provider with the highest precedence that * supports that algorithm. Return null if no Provider supports this * algorithm. */ public Service getService(String type, String name) { for (int i = 0; i < configs.length; i++) { Provider p = getProvider(i); Service s = p.getService(type, name); if (s != null) { return s; } } return null; }
/** * This method exists for compatibility with JCE only. It will be removed * once JCE has been changed to use the replacement method. * @deprecated use getServices(List<ServiceId>) instead */ @Deprecated public List<Service> getServices(String type, List<String> algorithms) { List<ServiceId> ids = new ArrayList<>(); for (String alg : algorithms) { ids.add(new ServiceId(type, alg)); } return getServices(ids); }
private void addService(Service s) { if (firstService == null) { firstService = s; } else { if (services == null) { services = new ArrayList<Service>(4); services.add(firstService); } services.add(s); } }
public static Service getService(String type, String algorithm) throws NoSuchAlgorithmException { ProviderList list = Providers.getProviderList(); Service s = list.getService(type, algorithm); if (s == null) { throw new NoSuchAlgorithmException (algorithm + " " + type + " not available"); } return s; }
public static Service getService(String type, String algorithm, Provider provider) throws NoSuchAlgorithmException { if (provider == null) { throw new IllegalArgumentException("missing provider"); } Service s = provider.getService(type, algorithm); if (s == null) { throw new NoSuchAlgorithmException("no such algorithm: " + algorithm + " for provider " + provider.getName()); } return s; }
/** * Check is subClass is a subclass of superClass. If not, * throw a NoSuchAlgorithmException. */ public static void checkSuperClass(Service s, Class<?> subClass, Class<?> superClass) throws NoSuchAlgorithmException { if (superClass == null) { return; } if (superClass.isAssignableFrom(subClass) == false) { throw new NoSuchAlgorithmException ("class configured for " + s.getType() + ": " + s.getClassName() + " not a " + s.getType()); } }
/** * Returns an array of objects: the first object in the array is * an instance of an implementation of the requested algorithm * and type, and the second object in the array identifies the provider * of that implementation. * The <code>prov</code> argument can be null, in which case all * configured providers will be searched in order of preference. */ static Object[] getImpl(String algName, String engineType, Provider prov) throws NoSuchAlgorithmException { Service service = prov.getService(engineType, algName); if (service == null) { throw new NoSuchAlgorithmException("No such algorithm: " + algName); } return getImpl1(algName, engineType, service); }
/** * 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. * * @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; } List<Service> services = GetInstance.getServices("TransformService", algorithm); for (Iterator<Service> t = services.iterator(); t.hasNext(); ) { Service s = t.next(); String value = s.getAttribute("MechanismType"); if ((value == null && dom) || (value != null && value.equals(mechanismType))) { Instance instance = GetInstance.getInstance(s, null); TransformService ts = (TransformService) instance.impl; ts.algorithm = algorithm; ts.mechanism = mechanismType; ts.provider = instance.provider; return ts; } } throw new NoSuchAlgorithmException (algorithm + " algorithm and " + mechanismType + " mechanism not available"); }
/** * 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 = GetInstance.getService ("TransformService", algorithm, provider); String value = s.getAttribute("MechanismType"); if ((value == null && dom) || (value != null && value.equals(mechanismType))) { Instance instance = GetInstance.getInstance(s, null); TransformService ts = (TransformService) instance.impl; ts.algorithm = algorithm; ts.mechanism = mechanismType; ts.provider = instance.provider; return ts; } throw new NoSuchAlgorithmException (algorithm + " algorithm and " + mechanismType + " mechanism not available"); }
/** * 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. The specified provider * must be registered in the security provider list. * * <p>Note that the list of registered providers may be retrieved via * the {@link Security#getProviders() Security.getProviders()} method. * * @param algorithm the URI of the algorithm * @param mechanismType the type of the XML processing mechanism and * representation * @param provider the string name of the provider * @return a new <code>TransformService</code> * @throws NoSuchProviderException if the specified provider is not * registered in the security provider list * @throws NullPointerException if <code>provider</code>, * <code>mechanismType</code>, or <code>algorithm</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 provider * @see Provider */ public static TransformService getInstance (String algorithm, String mechanismType, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { if (mechanismType == null || algorithm == null || provider == null) { throw new NullPointerException(); } else if (provider.length() == 0) { throw new NoSuchProviderException(); } boolean dom = false; if (mechanismType.equals("DOM")) { dom = true; } Service s = GetInstance.getService ("TransformService", algorithm, provider); String value = s.getAttribute("MechanismType"); if ((value == null && dom) || (value != null && value.equals(mechanismType))) { Instance instance = GetInstance.getInstance(s, null); TransformService ts = (TransformService) instance.impl; ts.algorithm = algorithm; ts.mechanism = mechanismType; ts.provider = instance.provider; return ts; } throw new NoSuchAlgorithmException (algorithm + " algorithm and " + mechanismType + " mechanism not available"); }
int supportsModePadding(Service s) { int smode = supportsMode(s); if (smode == S_NO) { return smode; } int spad = supportsPadding(s); // our constants are defined so that Math.min() is a tri-valued AND return Math.min(smode, spad); }
private static int supports(Service s, String attrName, String value) { if (value == null) { return S_YES; } String regexp = s.getAttribute(attrName); if (regexp == null) { return S_MAYBE; } return matches(regexp, value) ? S_YES : S_NO; }
private SecretKeyFactory(String algorithm) throws NoSuchAlgorithmException { this.algorithm = algorithm; List<Service> list = GetInstance.getServices("SecretKeyFactory", algorithm); serviceIterator = list.iterator(); // fetch and instantiate initial spi if (nextSpi(null) == null) { throw new NoSuchAlgorithmException (algorithm + " SecretKeyFactory not available"); } }
/** * Returns an <code>XMLSignatureFactory</code> that supports the * requested XML processing mechanism and representation type (ex: "DOM"), * as supplied by the specified provider. The specified provider must be * registered in the security provider list. * * <p>Note that the list of registered providers may be retrieved via * the {@link Security#getProviders() Security.getProviders()} method. * * @param mechanismType the type of the XML processing mechanism and * representation. See the {@extLink security_guide_xmldsig_provider * Service Providers} section of the API overview for a list of * standard mechanism types. * @param provider the string name of the provider * @return a new <code>XMLSignatureFactory</code> * @throws NoSuchProviderException if the specified provider is not * registered in the security provider list * @throws NullPointerException if <code>provider</code> or * <code>mechanismType</code> is <code>null</code> * @throws NoSuchMechanismException if an <code>XMLSignatureFactory</code> * implementation for the specified mechanism is not * available from the specified provider * @see Provider */ public static XMLSignatureFactory getInstance(String mechanismType, String provider) throws NoSuchProviderException { if (mechanismType == null) { throw new NullPointerException("mechanismType cannot be null"); } else if (provider == null) { throw new NullPointerException("provider cannot be null"); } else if (provider.length() == 0) { throw new NoSuchProviderException(); } Provider p = Security.getProvider(provider); if (p == null) { throw new NoSuchProviderException("No such provider: " + provider); } Service s = p.getService("XMLSignatureFactory", mechanismType); if (s != null) { Object obj = null; try { obj = s.newInstance(null); } catch (NoSuchAlgorithmException nsae) { throw new NoSuchMechanismException(nsae); } if (obj instanceof XMLSignatureFactory) { XMLSignatureFactory factory = (XMLSignatureFactory) obj; factory.mechanismType = mechanismType; factory.provider = p; return factory; } } throw new NoSuchMechanismException ("Mechanism " + mechanismType + " not available from " + provider); }
static Instance getInstance(String type, Class<?> clazz, String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException { Service s = GetInstance.getService(type, algorithm, provider); Exception ve = getVerificationResult(s.getProvider()); if (ve != null) { String msg = "JCE cannot authenticate the provider " + provider; throw (NoSuchProviderException) new NoSuchProviderException(msg).initCause(ve); } return GetInstance.getInstance(s, clazz); }