Save This Page
Home » openjdk-7 » java » security » cert » [javadoc | source]
    1   /* $Id: Support.java,v 1.2 2005/03/13 17:47:10 woudt Exp $
    2    *
    3    * Copyright (C) 1995-2005 The Cryptix Foundation Limited.
    4    * All rights reserved.
    5    *
    6    * Use, modification, copying and distribution of this software is subject
    7    * the terms and conditions of the Cryptix General Licence. You should have
    8    * received a copy of the Cryptix General Licence along with this library;
    9    * if not, you can download a copy from http://www.cryptix.org/ .
   10    */
   11   package java.security.cert;
   12   
   13   
   14   import java.security.NoSuchAlgorithmException;
   15   import java.security.NoSuchProviderException;
   16   import java.security.Provider;
   17   import java.security.Security;
   18   
   19   
   20   /**
   21    * Support class for finding implementations of a particular algorithm or format
   22    * within a provider.
   23    *
   24    * <p>Taken from Cryptix JCE, package javax.crypto, cvs file version 1.5.</p>
   25    *
   26    * @author Jeroen C. van Gelderen <gelderen@cryptix.org>
   27    * @author Edwin Woudt <edwin@cryptix.org>
   28    */
   29   /*package*/ final class Support
   30   {
   31       /*package*/ static Object[]
   32       getImplementation(String type, String algorithm)
   33       throws NoSuchAlgorithmException
   34       {
   35           Provider[] providers = Security.getProviders();
   36           if( (providers==null) || (providers.length==0) )
   37               throw new NoSuchAlgorithmException("No providers installed");
   38   
   39           for(int i=0; i<providers.length; i++)
   40           {
   41               try {
   42                   Object[] res = getImplementation(type, algorithm, providers[i]);
   43                   return res;
   44               } catch (NoSuchAlgorithmException nsae) {
   45                   // try next
   46               }
   47           }
   48   
   49           throw new NoSuchAlgorithmException(
   50               "Algorithm not found. [" + type + "." + algorithm + "]");
   51       }
   52   
   53   
   54       /*package*/ static Object[]
   55       getImplementation(String type, String algorithm, String provider)
   56       throws NoSuchAlgorithmException, NoSuchProviderException
   57       {
   58           Provider p = Security.getProvider(provider);
   59           if(p==null)
   60               throw new NoSuchProviderException(
   61                   "Provider not found. [" + provider + "]");
   62   
   63           Object[] res = getImplementation(type, algorithm, p);
   64           return res;
   65       }
   66   
   67   
   68       /*package*/ static Object[]
   69       getImplementation(String type, String algorithm, Provider provider)
   70       throws NoSuchAlgorithmException
   71       {
   72           try
   73           {
   74               String class_name =
   75                   provider.getProperty("Alg.Alias." + type + "." + algorithm);
   76   
   77               if(class_name==null)
   78                   class_name = provider.getProperty(type + "." + algorithm);
   79               else
   80                   class_name = provider.getProperty(type + "." + class_name);
   81   
   82               if(class_name == null)
   83                   throw new NoSuchAlgorithmException(
   84                       "Algorithm not found. [" + type + "." + algorithm + "]");
   85   
   86               Object[] res = new Object[2];
   87               res[0] = Class.forName(class_name).newInstance();
   88               res[1] = provider;
   89               return res;
   90           }
   91           catch(LinkageError e)
   92           {
   93               throw new NoSuchAlgorithmException(
   94                   "Error initializing class. ["+type+"."+algorithm+"]");
   95           }
   96           catch(ClassNotFoundException e)
   97           {
   98               throw new NoSuchAlgorithmException(
   99                   "Registered class not found. ["+type+"."+algorithm+"]");
  100           }
  101           catch(InstantiationException e)
  102           {
  103               throw new NoSuchAlgorithmException(
  104                   "Error initializing class. ["+type+"."+algorithm+"]");
  105           }
  106           catch(IllegalAccessException e)
  107           {
  108               throw new NoSuchAlgorithmException(
  109                   "Error initializing class. ["+type+"."+algorithm+"]");
  110           }
  111       }
  112   
  113   
  114   
  115       /*package*/ static Object[]
  116       getClassName(String type, String algorithm)
  117       throws NoSuchAlgorithmException
  118       {
  119           Provider[] providers = Security.getProviders();
  120           if( (providers==null) || (providers.length==0) )
  121               throw new NoSuchAlgorithmException("No providers installed");
  122   
  123           for(int i=0; i<providers.length; i++)
  124           {
  125               try {
  126                   Object[] res = getClassName(type, algorithm, providers[i]);
  127                   return res;
  128               } catch (NoSuchAlgorithmException nsae) {
  129                   // try next
  130               }
  131           }
  132   
  133           throw new NoSuchAlgorithmException(
  134               "Algorithm not found. [" + type + "." + algorithm + "]");
  135       }
  136   
  137   
  138       /*package*/ static Object[]
  139       getClassName(String type, String algorithm, String provider)
  140       throws NoSuchAlgorithmException, NoSuchProviderException
  141       {
  142           Provider p = Security.getProvider(provider);
  143           if(p==null)
  144               throw new NoSuchProviderException(
  145                   "Provider not found. [" + provider + "]");
  146   
  147           Object[] res = getClassName(type, algorithm, p);
  148           return res;
  149       }
  150   
  151   
  152       /*package*/ static Object[]
  153       getClassName(String type, String algorithm, Provider provider)
  154       throws NoSuchAlgorithmException
  155       {
  156           String class_name =
  157               provider.getProperty("Alg.Alias." + type + "." + algorithm);
  158   
  159           if(class_name == null)
  160               class_name = provider.getProperty(type + "." + algorithm);
  161           else
  162               class_name = provider.getProperty(type + "." + class_name);
  163   
  164           if(class_name == null)
  165               throw new NoSuchAlgorithmException(
  166                   "Algorithm not found. [" + type + "." + algorithm + "]");
  167   
  168           Object[] res = new Object[2];
  169           res[0] = class_name;
  170           res[1] = provider;
  171           return res;
  172       }
  173   
  174   }

Save This Page
Home » openjdk-7 » java » security » cert » [javadoc | source]