Пример #1
0
        /*
         * Returns the service with the specified {@code type} implementing the
         * specified {@code algorithm}, or {@code null} if no such implementation
         * exists.
         * <p/>
         * If two services match the requested type and algorithm, the one added
         * with the {@link #putService(Service)} is returned (as opposed to the one
         * added via {@link #put(Object, Object)}.
         *
         * @param type
         *            the type of the service (for example {@code KeyPairGenerator})
         * @param algorithm
         *            the algorithm name (case insensitive)
         * @return the requested service, or {@code null} if no such implementation
         *         exists
         */
        public Provider.Service getService(String type,
                                           String algorithm)
        {
            lock (this)
            {
                if (type == null || algorithm == null)
                {
                    throw new java.lang.NullPointerException();
                }

                if (type.equals(lastServiceName) &&
                    algorithm.equalsIgnoreCase(lastAlgorithm))
                {
                    return(returnedService);
                }

                String alg = algorithm.toUpperCase();
                Object o   = null;
                if (serviceTable != null)
                {
                    o = serviceTable.get(type, alg);
                }
                if (o == null && aliasTable != null)
                {
                    o = aliasTable.get(type, alg);
                }
                if (o == null)
                {
                    updatePropertyServiceTable();
                }
                if (o == null && propertyServiceTable != null)
                {
                    o = propertyServiceTable.get(type, alg);
                }
                if (o == null && propertyAliasTable != null)
                {
                    o = propertyAliasTable.get(type, alg);
                }

                if (o != null)
                {
                    lastServiceName = type;
                    lastAlgorithm   = algorithm;
                    returnedService = (Provider.Service)o;
                    return(returnedService);
                }
                return(null);
            }
        }
Пример #2
0
 /**
  * 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 java.util.Set <String> getAlgorithms(String serviceName)
 {
     java.util.HashSet <String> result = new java.util.HashSet <String>();
     Provider[] p = getProviders();
     for (int i = 0; i < p.Length; i++)
     {
         for (java.util.Iterator <Provider.Service> it = p[i].getServices().iterator(); it.hasNext();)
         {
             Provider.Service s = (Provider.Service)it.next();
             if (Util.equalsIgnoreCase(s.getType(), serviceName))
             {
                 result.add(s.getAlgorithm());
             }
         }
     }
     return(result);
 }
Пример #3
0
 /*
  * Constructs a new instance of {@code SecureRandom}. An implementation for
  * the highest-priority provider is returned. The constructed instance will
  * not have been seeded.
  */
 public SecureRandom() : base(0)
 {
     Provider.Service service = findService();
     if (service == null)
     {
         this.provider        = null;
         this.secureRandomSpi = new SHA1PRNG_SecureRandomImpl();
         this.algorithm       = "SHA1PRNG"; //$NON-NLS-1$
     }
     else
     {
         try {
             this.provider        = service.getProvider();
             this.secureRandomSpi = (SecureRandomSpi)service.newInstance(null);
             this.algorithm       = service.getAlgorithm();
         } catch (java.lang.Exception e) {
             throw new RuntimeException(e);
         }
     }
 }
Пример #4
0
 /*
  * Get the service of the specified type
  *
  */
 internal Provider.Service getService(String type)
 {
     lock (this)
     {
         updatePropertyServiceTable();
         if (lastServicesByType != null && type.equals(lastType))
         {
             return(lastServicesByType);
         }
         Provider.Service service;
         for (java.util.Iterator <Service> it = getServices().iterator(); it.hasNext();)
         {
             service = it.next();
             if (type.equals(service.type))
             {
                 lastType           = type;
                 lastServicesByType = service;
                 return(service);
             }
         }
         return(null);
     }
 }
Пример #5
0
        // Update provider Services if the properties was changed
        private void updatePropertyServiceTable()
        {
            Object _key;
            Object _value;
            Provider.Service s;
            String serviceName;
            String algorithm;
            if (changedProperties == null || changedProperties.isEmpty())
            {
                return;
            }
            java.util.Iterator<java.util.MapNS.Entry<String, String>> it = changedProperties.entrySet().iterator();
            for (; it.hasNext(); )
            {
                java.util.MapNS.Entry<String, String> entry = it.next();
                _key = entry.getKey();
                _value = entry.getValue();
                if (_key == null || _value == null || !(_key is String)
                        || !(_value is String))
                {
                    continue;
                }
                String key = (String)_key;
                String value = (String)_value;
                if (key.startsWith("Provider"))
                { // Provider service type is reserved //$NON-NLS-1$
                    continue;
                }
                int i;
                if (key.startsWith("Alg.Alias."))
                { // Alg.Alias.<crypto_service>.<aliasName>=<stanbdardName> //$NON-NLS-1$
                    String aliasName;
                    String service_alias = key.substring(10);
                    i = service_alias.indexOf('.');
                    serviceName = service_alias.substring(0, i);
                    aliasName = service_alias.substring(i + 1);
                    algorithm = value;
                    String algUp = algorithm.toUpperCase();
                    Object o = null;
                    if (propertyServiceTable == null)
                    {
                        propertyServiceTable = new TwoKeyHashMap<String, String, Service>(128);
                    }
                    else
                    {
                        o = propertyServiceTable.get(serviceName, algUp);
                    }
                    if (o != null)
                    {
                        s = (Provider.Service)o;
                        s.aliases.add(aliasName);
                        if (propertyAliasTable == null)
                        {
                            propertyAliasTable = new TwoKeyHashMap<String, String, Service>(256);
                        }
                        propertyAliasTable.put(serviceName,
                                aliasName.toUpperCase(), s);
                    }
                    else
                    {
                        String className = (String)changedProperties
                                .get(serviceName + "." + algorithm); //$NON-NLS-1$
                        if (className != null)
                        {
                            java.util.ArrayList<String> l = new java.util.ArrayList<String>();
                            l.add(aliasName);
                            s = new Provider.Service(this, serviceName, algorithm,
                                    className, l, new java.util.HashMap<String, String>());
                            propertyServiceTable.put(serviceName, algUp, s);
                            if (propertyAliasTable == null)
                            {
                                propertyAliasTable = new TwoKeyHashMap<String, String, Service>(256);
                            }
                            propertyAliasTable.put(serviceName, aliasName
                                    .toUpperCase(), s);
                        }
                    }
                    continue;
                }
                int j = key.indexOf('.');
                if (j == -1)
                { // unknown format
                    continue;
                }
                i = key.indexOf(' ');
                if (i == -1)
                { // <crypto_service>.<algorithm_or_type>=<className>
                    serviceName = key.substring(0, j);
                    algorithm = key.substring(j + 1);
                    String alg = algorithm.toUpperCase();
                    Object o = null;
                    if (propertyServiceTable != null)
                    {
                        o = propertyServiceTable.get(serviceName, alg);
                    }
                    if (o != null)
                    {
                        s = (Provider.Service)o;
                        s.className = value;
                    }
                    else
                    {
                        s = new Provider.Service(this, serviceName, algorithm,
                                value, new java.util.ArrayList<String>(), new java.util.HashMap<String, String>());
                        if (propertyServiceTable == null)
                        {
                            propertyServiceTable = new TwoKeyHashMap<String, String, Service>(128);
                        }
                        propertyServiceTable.put(serviceName, alg, s);

                    }
                }
                else
                { // <crypto_service>.<algorithm_or_type>
                    // <attribute_name>=<attrValue>
                    serviceName = key.substring(0, j);
                    algorithm = key.substring(j + 1, i);
                    String attribute = key.substring(i + 1);
                    String alg = algorithm.toUpperCase();
                    Object o = null;
                    if (propertyServiceTable != null)
                    {
                        o = propertyServiceTable.get(serviceName, alg);
                    }
                    if (o != null)
                    {
                        s = (Provider.Service)o;
                        s.attributes.put(attribute, value);
                    }
                    else
                    {
                        String className = (String)changedProperties
                                .get(serviceName + "." + algorithm); //$NON-NLS-1$
                        if (className != null)
                        {
                            java.util.HashMap<String, String> m = new java.util.HashMap<String, String>();
                            m.put(attribute, value);
                            s = new Provider.Service(this, serviceName, algorithm,
                                    className, new java.util.ArrayList<String>(), m);
                            if (propertyServiceTable == null)
                            {
                                propertyServiceTable = new TwoKeyHashMap<String, String, Service>(128);
                            }
                            propertyServiceTable.put(serviceName, alg, s);
                        }
                    }
                }
            }
            servicesChanged();
            changedProperties.clear();
        }
Пример #6
0
 private void servicesChanged()
 {
     lastServicesByType = null;
     lastServiceName = null;
     lastServicesSet = null;
 }
Пример #7
0
 /**
  * Get the service of the specified type
  *
  */
 internal Provider.Service getService(String type)
 {
     lock (this)
     {
         updatePropertyServiceTable();
         if (lastServicesByType != null && type.equals(lastType))
         {
             return lastServicesByType;
         }
         Provider.Service service;
         for (java.util.Iterator<Service> it = getServices().iterator(); it.hasNext(); )
         {
             service = it.next();
             if (type.equals(service.type))
             {
                 lastType = type;
                 lastServicesByType = service;
                 return service;
             }
         }
         return null;
     }
 }
Пример #8
0
        /**
         * Returns the service with the specified {@code type} implementing the
         * specified {@code algorithm}, or {@code null} if no such implementation
         * exists.
         * <p/>
         * If two services match the requested type and algorithm, the one added
         * with the {@link #putService(Service)} is returned (as opposed to the one
         * added via {@link #put(Object, Object)}.
         *
         * @param type
         *            the type of the service (for example {@code KeyPairGenerator})
         * @param algorithm
         *            the algorithm name (case insensitive)
         * @return the requested service, or {@code null} if no such implementation
         *         exists
         */
        public Provider.Service getService(String type,
                String algorithm)
        {
            lock (this)
            {
                if (type == null || algorithm == null)
                {
                    throw new java.lang.NullPointerException();
                }

                if (type.equals(lastServiceName)
                        && algorithm.equalsIgnoreCase(lastAlgorithm))
                {
                    return returnedService;
                }

                String alg = algorithm.toUpperCase();
                Object o = null;
                if (serviceTable != null)
                {
                    o = serviceTable.get(type, alg);
                }
                if (o == null && aliasTable != null)
                {
                    o = aliasTable.get(type, alg);
                }
                if (o == null)
                {
                    updatePropertyServiceTable();
                }
                if (o == null && propertyServiceTable != null)
                {
                    o = propertyServiceTable.get(type, alg);
                }
                if (o == null && propertyAliasTable != null)
                {
                    o = propertyAliasTable.get(type, alg);
                }

                if (o != null)
                {
                    lastServiceName = type;
                    lastAlgorithm = algorithm;
                    returnedService = (Provider.Service)o;
                    return returnedService;
                }
                return null;
            }
        }
Пример #9
0
 private void servicesChanged()
 {
     lastServicesByType = null;
     lastServiceName    = null;
     lastServicesSet    = null;
 }
Пример #10
0
        // Update provider Services if the properties was changed
        private void updatePropertyServiceTable()
        {
            Object _key;
            Object _value;

            Provider.Service s;
            String           serviceName;
            String           algorithm;

            if (changedProperties == null || changedProperties.isEmpty())
            {
                return;
            }
            java.util.Iterator <java.util.MapNS.Entry <String, String> > it = changedProperties.entrySet().iterator();
            for (; it.hasNext();)
            {
                java.util.MapNS.Entry <String, String> entry = it.next();
                _key   = entry.getKey();
                _value = entry.getValue();
                if (_key == null || _value == null || !(_key is String) ||
                    !(_value is String))
                {
                    continue;
                }
                String key   = (String)_key;
                String value = (String)_value;
                if (key.startsWith("Provider"))
                { // Provider service type is reserved //$NON-NLS-1$
                    continue;
                }
                int i;
                if (key.startsWith("Alg.Alias."))
                { // Alg.Alias.<crypto_service>.<aliasName>=<stanbdardName> //$NON-NLS-1$
                    String aliasName;
                    String service_alias = key.substring(10);
                    i           = service_alias.indexOf('.');
                    serviceName = service_alias.substring(0, i);
                    aliasName   = service_alias.substring(i + 1);
                    algorithm   = value;
                    String algUp = algorithm.toUpperCase();
                    Object o     = null;
                    if (propertyServiceTable == null)
                    {
                        propertyServiceTable = new TwoKeyHashMap <String, String, Service>(128);
                    }
                    else
                    {
                        o = propertyServiceTable.get(serviceName, algUp);
                    }
                    if (o != null)
                    {
                        s = (Provider.Service)o;
                        s.aliases.add(aliasName);
                        if (propertyAliasTable == null)
                        {
                            propertyAliasTable = new TwoKeyHashMap <String, String, Service>(256);
                        }
                        propertyAliasTable.put(serviceName,
                                               aliasName.toUpperCase(), s);
                    }
                    else
                    {
                        String className = (String)changedProperties
                                           .get(serviceName + "." + algorithm); //$NON-NLS-1$
                        if (className != null)
                        {
                            java.util.ArrayList <String> l = new java.util.ArrayList <String>();
                            l.add(aliasName);
                            s = new Provider.Service(this, serviceName, algorithm,
                                                     className, l, new java.util.HashMap <String, String>());
                            propertyServiceTable.put(serviceName, algUp, s);
                            if (propertyAliasTable == null)
                            {
                                propertyAliasTable = new TwoKeyHashMap <String, String, Service>(256);
                            }
                            propertyAliasTable.put(serviceName, aliasName
                                                   .toUpperCase(), s);
                        }
                    }
                    continue;
                }
                int j = key.indexOf('.');
                if (j == -1)
                { // unknown format
                    continue;
                }
                i = key.indexOf(' ');
                if (i == -1)
                { // <crypto_service>.<algorithm_or_type>=<className>
                    serviceName = key.substring(0, j);
                    algorithm   = key.substring(j + 1);
                    String alg = algorithm.toUpperCase();
                    Object o   = null;
                    if (propertyServiceTable != null)
                    {
                        o = propertyServiceTable.get(serviceName, alg);
                    }
                    if (o != null)
                    {
                        s           = (Provider.Service)o;
                        s.className = value;
                    }
                    else
                    {
                        s = new Provider.Service(this, serviceName, algorithm,
                                                 value, new java.util.ArrayList <String>(), new java.util.HashMap <String, String>());
                        if (propertyServiceTable == null)
                        {
                            propertyServiceTable = new TwoKeyHashMap <String, String, Service>(128);
                        }
                        propertyServiceTable.put(serviceName, alg, s);
                    }
                }
                else
                { // <crypto_service>.<algorithm_or_type>
                    // <attribute_name>=<attrValue>
                    serviceName = key.substring(0, j);
                    algorithm   = key.substring(j + 1, i);
                    String attribute = key.substring(i + 1);
                    String alg       = algorithm.toUpperCase();
                    Object o         = null;
                    if (propertyServiceTable != null)
                    {
                        o = propertyServiceTable.get(serviceName, alg);
                    }
                    if (o != null)
                    {
                        s = (Provider.Service)o;
                        s.attributes.put(attribute, value);
                    }
                    else
                    {
                        String className = (String)changedProperties
                                           .get(serviceName + "." + algorithm); //$NON-NLS-1$
                        if (className != null)
                        {
                            java.util.HashMap <String, String> m = new java.util.HashMap <String, String>();
                            m.put(attribute, value);
                            s = new Provider.Service(this, serviceName, algorithm,
                                                     className, new java.util.ArrayList <String>(), m);
                            if (propertyServiceTable == null)
                            {
                                propertyServiceTable = new TwoKeyHashMap <String, String, Service>(128);
                            }
                            propertyServiceTable.put(serviceName, alg, s);
                        }
                    }
                }
            }
            servicesChanged();
            changedProperties.clear();
        }
Пример #11
0
 //  Access to Security.getAliases()
 public java.util.Iterator <String> getAliases(Provider.Service s)
 {
     return(s.getAliases());
 }