1 /* $Id: Support.java,v 1.5 2000/02/12 15:41:33 gelderen Exp $ 2 * 3 * Copyright (C) 1995-1999 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 javax.crypto; 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 /*package*/ final class Support 21 { 22 /*package*/ static Object[] 23 getImplementation(String type, String algorithm) 24 throws NoSuchAlgorithmException 25 { 26 Provider[] providers = Security.getProviders(); 27 if( (providers==null) || (providers.length==0) ) 28 throw new NoSuchAlgorithmException("No providers installed"); 29 30 for(int i=0; i<providers.length; i++) 31 { 32 Object[] res = getImplementation(type, algorithm, providers[i]); 33 if(res != null) 34 return res; 35 } 36 37 throw new NoSuchAlgorithmException( 38 "Algorithm not found. [" + type + "." + algorithm + "]"); 39 } 40 41 42 /*package*/ static Object[] 43 getImplementation(String type, String algorithm, String provider) 44 throws NoSuchAlgorithmException, NoSuchProviderException 45 { 46 Provider p = Security.getProvider(provider); 47 if(p==null) 48 throw new NoSuchProviderException( 49 "Provider not found. [" + provider + "]"); 50 51 Object[] res = getImplementation(type, algorithm, p); 52 if(res != null) 53 return res; 54 55 throw new NoSuchAlgorithmException( 56 "Algorithm not found. [" + type + "." + algorithm + "]"); 57 } 58 59 60 /*package*/ static Object[] 61 getImplementation(String algType, String algName, Provider p) 62 { 63 try 64 { 65 String class_name = 66 p.getProperty("Alg.Alias." + algType + "." + algName); 67 68 if(class_name==null) 69 class_name = p.getProperty(algType + "." + algName); 70 else 71 class_name = p.getProperty(algType + "." + class_name); 72 73 if(class_name == null) 74 return null; 75 76 Object[] res = new Object[2]; 77 res[0] = Class.forName(class_name).newInstance(); 78 res[1] = p; 79 return res; 80 } 81 catch(LinkageError e) 82 { 83 e.printStackTrace(); 84 // FIXME: Throw a RuntimeException with a sensible message???? 85 } 86 catch(ClassNotFoundException e) 87 { 88 e.printStackTrace(); 89 // FIXME: Throw a RuntimeException with a sensible message???? 90 } 91 catch(InstantiationException e) 92 { 93 e.printStackTrace(); 94 // FIXME: Throw a RuntimeException with a sensible message???? 95 } 96 catch(IllegalAccessException e) 97 { 98 e.printStackTrace(); 99 // FIXME: Throw a RuntimeException with a sensible message???? 100 } 101 102 return null; 103 } 104 105 106 /*package*/ static String 107 getClassName(String algType, String algName, Provider p) 108 { 109 String class_name = 110 p.getProperty("Alg.Alias." + algType + "." + algName); 111 112 if(class_name==null) 113 class_name = p.getProperty(algType + "." + algName); 114 else 115 class_name = p.getProperty(algType + "." + class_name); 116 117 return class_name; 118 } 119 }