1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54:
66: public class ExemptionMechanism
67: {
68:
69:
70:
71:
72: private static final String SERVICE = "ExemptionMechanism";
73: private ExemptionMechanismSpi emSpi;
74: private Provider provider;
75: private String mechanism;
76: private boolean virgin;
77:
78:
79:
80:
81: protected ExemptionMechanism(ExemptionMechanismSpi emSpi, Provider provider,
82: String mechanism)
83: {
84: this.emSpi = emSpi;
85: this.provider = provider;
86: this.mechanism = mechanism;
87: virgin = true;
88: }
89:
90:
91:
92:
93: public static final ExemptionMechanism getInstance(String mechanism)
94: throws NoSuchAlgorithmException
95: {
96: Provider[] provs = Security.getProviders();
97: String msg = "";
98: for (int i = 0; i < provs.length; i++)
99: {
100: try
101: {
102: return getInstance(mechanism, provs[i]);
103: }
104: catch (NoSuchAlgorithmException nsae)
105: {
106: msg = nsae.getMessage();
107: }
108: }
109: throw new NoSuchAlgorithmException(msg);
110: }
111:
112: public static final ExemptionMechanism getInstance(String mechanism,
113: String provider)
114: throws NoSuchAlgorithmException, NoSuchProviderException
115: {
116: Provider p = Security.getProvider(provider);
117: if (p == null)
118: {
119: throw new NoSuchProviderException(provider);
120: }
121: return getInstance(mechanism, p);
122: }
123:
124: public static final ExemptionMechanism getInstance(String mechanism,
125: Provider provider)
126: throws NoSuchAlgorithmException
127: {
128: try
129: {
130: return new ExemptionMechanism((ExemptionMechanismSpi)
131: Engine.getInstance(SERVICE, mechanism, provider),
132: provider, mechanism);
133: }
134: catch (InvocationTargetException ite)
135: {
136: if (ite.getCause() instanceof NoSuchAlgorithmException)
137: throw (NoSuchAlgorithmException) ite.getCause();
138: else
139: throw new NoSuchAlgorithmException(mechanism);
140: }
141: catch (ClassCastException cce)
142: {
143: throw new NoSuchAlgorithmException(mechanism);
144: }
145: }
146:
147:
148:
149:
150: public final byte[] genExemptionBlob()
151: throws IllegalStateException, ExemptionMechanismException
152: {
153: if (virgin)
154: {
155: throw new IllegalStateException("not initialized");
156: }
157: return emSpi.engineGenExemptionBlob();
158: }
159:
160: public final int genExemptionBlob(byte[] output)
161: throws IllegalStateException, ExemptionMechanismException,
162: ShortBufferException
163: {
164: return genExemptionBlob(output, 0);
165: }
166:
167: public final int genExemptionBlob(byte[] output, int outputOffset)
168: throws IllegalStateException, ExemptionMechanismException,
169: ShortBufferException
170: {
171: if (virgin)
172: {
173: throw new IllegalStateException("not initialized");
174: }
175: return emSpi.engineGenExemptionBlob(output, outputOffset);
176: }
177:
178: public final String getName()
179: {
180: return mechanism;
181: }
182:
183: public final int getOutputSize(int inputLength) throws IllegalStateException
184: {
185: if (virgin)
186: {
187: throw new IllegalStateException("not initialized");
188: }
189: return emSpi.engineGetOutputSize(inputLength);
190: }
191:
192: public final Provider getProvider()
193: {
194: return provider;
195: }
196:
197: public final void init(Key key)
198: throws ExemptionMechanismException, InvalidKeyException
199: {
200: emSpi.engineInit(key);
201: virgin = false;
202: }
203:
204: public final void init(Key key, AlgorithmParameters params)
205: throws ExemptionMechanismException, InvalidAlgorithmParameterException,
206: InvalidKeyException
207: {
208: emSpi.engineInit(key, params);
209: virgin = false;
210: }
211:
212: public final void init(Key key, AlgorithmParameterSpec params)
213: throws ExemptionMechanismException, InvalidAlgorithmParameterException,
214: InvalidKeyException
215: {
216: emSpi.engineInit(key, params);
217: virgin = false;
218: }
219:
220: public final boolean isCryptoAllowed(Key key)
221: throws ExemptionMechanismException
222: {
223: return true;
224: }
225:
226: protected void finalize()
227: {
228: }
229: }