001: /* $Id: AlgorithmParameters.java,v 1.1 2004/01/19 02:03:49 rgrimm Exp $
002: *
003: * Copyright (C) 1995-1999 The Cryptix Foundation Limited.
004: * All rights reserved.
005: *
006: * Use, modification, copying and distribution of this software is subject
007: * the terms and conditions of the Cryptix General Licence. You should have
008: * received a copy of the Cryptix General Licence along with this library;
009: * if not, you can download a copy from http://www.cryptix.org/ .
010: */
011: package java.security;
012:
013: import java.security.NoSuchAlgorithmException;
014: import java.security.NoSuchProviderException;
015:
016: import java.security.Provider;
017: import java.security.Security;
018:
019: import java.security.spec.AlgorithmParameterSpec;
020: import java.security.spec.InvalidParameterSpecException;
021:
022: import java.io.IOException;
023:
024: /**
025: * This is the java.security.AlgorithmParameters class for jdk1.1 compatibility.
026: * Currently only the interfaces are designed, a few lines of code written.
027: *
028: * THIS IS UNDER DEVELOPMENT NOT TESTED YET (1999-24-12)!!!
029: *
030: * @author Josef Hartmann
031: * @version $Revision: 1.1 $
032: */
033: public class AlgorithmParameters {
034: private AlgorithmParametersSpi paramSpi = null;
035: private Provider provider = null;
036: private String algorithm = null;
037: private boolean isInitialized = false;
038:
039: protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
040: Provider provider, String algorithm) {
041: this .paramSpi = paramSpi;
042: this .provider = provider;
043: this .algorithm = algorithm;
044: }
045:
046: public final String getAlgorithm() {
047: return this .algorithm;
048: }
049:
050: // FIXME: Use javax.crypto.Support.getImplementation() instead?
051: public static AlgorithmParameters getInstance(String algorithm)
052: throws NoSuchAlgorithmException {
053: // Walk through installed providers and check for algorithm.
054: // Take the first one which provides the specified algorithm.
055: Provider[] allInstalledProviders = Security.getProviders();
056:
057: for (int i = 0; i < allInstalledProviders.length; i++) {
058: // pass current provider with algorithm to overloaded method.
059: try {
060: // FIXME: Does it make sense to pass the name of the provider
061: // and instanciating one again using the name.
062: AlgorithmParameters ap = getInstance(algorithm,
063: allInstalledProviders[i].getName());
064: return ap;
065: } catch (NoSuchAlgorithmException nsae) {
066: throw new NoSuchAlgorithmException();
067: } catch (NoSuchProviderException nspe) {
068: // Is this the correct exception? Probably yes!
069: throw new NoSuchAlgorithmException();
070: }
071: }
072:
073: // If we get there, no provider with specified algorithm was found.
074: throw new NoSuchAlgorithmException();
075: }
076:
077: public static AlgorithmParameters getInstance(String algorithm,
078: String provider) throws NoSuchAlgorithmException,
079: NoSuchProviderException {
080: AlgorithmParametersSpi algorithmSpi = null;
081: // Provider string is null or empty; take the other method.
082: if ((provider == null) || (provider.length() == 0))
083: return getInstance(algorithm);
084:
085: // Look for the provider.
086: Provider tempProv = Security.getProvider(provider);
087: if (tempProv == null)
088: throw new NoSuchProviderException();
089:
090: // We got the provider, so let's look for the algorithm.
091: String key = "AlgorithmParameters." + algorithm;
092: String prop = tempProv.getProperty(key);
093: if (prop == null) {
094: throw new NoSuchAlgorithmException();
095: }
096:
097: // Here we have a not yet verified property string.
098: // Letīs try to instantiate the class using the name stored in prop.
099: try {
100: algorithmSpi = (AlgorithmParametersSpi) Class.forName(prop)
101: .newInstance();
102: } catch (ClassNotFoundException cnfe) {
103: cnfe.printStackTrace();
104: throw new NoSuchAlgorithmException();
105: } catch (InstantiationException ie) {
106: ie.printStackTrace();
107: // FIXME: what to throw here??
108: } catch (IllegalAccessException iae) {
109: iae.printStackTrace();
110: // FIXME: what to throw here?
111: }
112:
113: // Now we have a provider and the algorithmSpi. So create a new AlgorithmObject and return it.
114: return new AlgorithmParameters(algorithmSpi, tempProv,
115: algorithm);
116: }
117:
118: public final Provider getProvider() {
119: return this .provider;
120: }
121:
122: public final void init(AlgorithmParameterSpec paramSpec)
123: throws InvalidParameterSpecException {
124: this .paramSpi.engineInit(paramSpec);
125: isInitialized = true;
126: // FIXME: What throws the exception?
127: // throw new InvalidParameterSpecException();
128: }
129:
130: public final void init(byte[] params) throws IOException {
131: this .paramSpi.engineInit(params);
132: isInitialized = true;
133: // FIXME: exception?
134: // throw new IOException();
135: }
136:
137: public final void init(byte[] params, String format)
138: throws IOException {
139: this .paramSpi.engineInit(params, format);
140: isInitialized = true;
141: // FIXME: How about the exception?
142: // throw new IOException();
143: }
144:
145: public final AlgorithmParameterSpec getParameterSpec(Class paramSpec)
146: throws InvalidParameterSpecException {
147: return this .paramSpi.engineGetParameterSpec(paramSpec);
148: }
149:
150: public final byte[] getEncoded() throws IOException {
151: if (isInitialized == false)
152: throw new IOException();
153:
154: return this .paramSpi.engineGetEncoded();
155: }
156:
157: public final byte[] getEncoded(String format) throws IOException {
158: if (isInitialized == false)
159: throw new IOException();
160:
161: return this .paramSpi.engineGetEncoded(format);
162: }
163:
164: public final String toString() {
165: // Return the spi. If the object has not been initialized return null.
166: if (isInitialized == false) {
167: return null;
168: } else {
169: return this.paramSpi.engineToString();
170: }
171: }
172: }
|