001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.crypto;
019:
020: import java.security.AlgorithmParameters;
021: import java.security.InvalidAlgorithmParameterException;
022: import java.security.InvalidKeyException;
023: import java.security.Key;
024: import java.security.NoSuchAlgorithmException;
025: import java.security.NoSuchProviderException;
026: import java.security.Provider;
027: import java.security.Security;
028: import java.security.spec.AlgorithmParameterSpec;
029: import java.util.Arrays;
030:
031: import org.apache.harmony.crypto.internal.nls.Messages;
032: import org.apache.harmony.security.fortress.Engine;
033:
034: public class ExemptionMechanism {
035:
036: // Used to access common engine functionality
037: private static final Engine engine = new Engine(
038: "ExemptionMechanism");
039:
040: // Store used provider
041: private final Provider provider;
042:
043: // Store used spi implementation
044: private final ExemptionMechanismSpi spiImpl;
045:
046: // Store mechanism name
047: private final String mechanism;
048:
049: // Store state (initialized or not)
050: private boolean isInit;
051:
052: // Store initKey value
053: private Key initKey;
054:
055: // Indicates if blob generated successfully
056: private boolean generated;
057:
058: protected ExemptionMechanism(ExemptionMechanismSpi exmechSpi,
059: Provider provider, String mechanism) {
060: this .mechanism = mechanism;
061: this .spiImpl = exmechSpi;
062: this .provider = provider;
063: isInit = false;
064: }
065:
066: public final String getName() {
067: return mechanism;
068: }
069:
070: public static final ExemptionMechanism getInstance(String algorithm)
071: throws NoSuchAlgorithmException {
072: if (algorithm == null) {
073: throw new NullPointerException(Messages
074: .getString("crypto.02")); //$NON-NLS-1$
075: }
076: synchronized (engine) {
077: engine.getInstance(algorithm, null);
078: return new ExemptionMechanism(
079: (ExemptionMechanismSpi) engine.spi,
080: engine.provider, algorithm);
081: }
082: }
083:
084: public static final ExemptionMechanism getInstance(
085: String algorithm, String provider)
086: throws NoSuchAlgorithmException, NoSuchProviderException {
087: if (provider == null) {
088: throw new IllegalArgumentException(Messages
089: .getString("crypto.04")); //$NON-NLS-1$
090: }
091: Provider impProvider = Security.getProvider(provider);
092: if (impProvider == null) {
093: throw new NoSuchProviderException(provider);
094: }
095: if (algorithm == null) {
096: throw new NullPointerException(Messages
097: .getString("crypto.02")); //$NON-NLS-1$
098: }
099: return getInstance(algorithm, impProvider);
100: }
101:
102: public static final ExemptionMechanism getInstance(
103: String algorithm, Provider provider)
104: throws NoSuchAlgorithmException {
105: if (algorithm == null) {
106: throw new NullPointerException(Messages
107: .getString("crypto.02")); //$NON-NLS-1$
108: }
109: if (provider == null) {
110: throw new IllegalArgumentException(Messages
111: .getString("crypto.04")); //$NON-NLS-1$
112: }
113: synchronized (engine) {
114: engine.getInstance(algorithm, provider, null);
115: return new ExemptionMechanism(
116: (ExemptionMechanismSpi) engine.spi, provider,
117: algorithm);
118: }
119: }
120:
121: public final Provider getProvider() {
122: return provider;
123: }
124:
125: public final boolean isCryptoAllowed(Key key)
126: throws ExemptionMechanismException {
127:
128: if (generated
129: && (initKey.equals(key) || Arrays.equals(initKey
130: .getEncoded(), key.getEncoded()))) {
131: return true;
132: }
133: return false;
134: }
135:
136: public final int getOutputSize(int inputLen)
137: throws IllegalStateException {
138: if (!isInit) {
139: throw new IllegalStateException(Messages
140: .getString("crypto.2D"));
141: }
142: return spiImpl.engineGetOutputSize(inputLen);
143: }
144:
145: public final void init(Key key) throws InvalidKeyException,
146: ExemptionMechanismException {
147: generated = false;
148: spiImpl.engineInit(key);
149: initKey = key;
150: isInit = true;
151: }
152:
153: public final void init(Key key, AlgorithmParameters param)
154: throws InvalidKeyException,
155: InvalidAlgorithmParameterException,
156: ExemptionMechanismException {
157: generated = false;
158: spiImpl.engineInit(key, param);
159: initKey = key;
160: isInit = true;
161: }
162:
163: public final void init(Key key, AlgorithmParameterSpec param)
164: throws InvalidKeyException,
165: InvalidAlgorithmParameterException,
166: ExemptionMechanismException {
167: generated = false;
168: spiImpl.engineInit(key, param);
169: initKey = key;
170: isInit = true;
171: }
172:
173: public final byte[] genExemptionBlob()
174: throws IllegalStateException, ExemptionMechanismException {
175: if (!isInit) {
176: throw new IllegalStateException(Messages
177: .getString("crypto.2D"));
178: }
179: generated = false;
180: byte[] result = spiImpl.engineGenExemptionBlob();
181: generated = true;
182: return result;
183: }
184:
185: public final int genExemptionBlob(byte[] output)
186: throws IllegalStateException, ShortBufferException,
187: ExemptionMechanismException {
188: return genExemptionBlob(output, 0);
189: }
190:
191: public final int genExemptionBlob(byte[] output, int outputOffset)
192: throws IllegalStateException, ShortBufferException,
193: ExemptionMechanismException {
194: if (!isInit) {
195: throw new IllegalStateException(Messages
196: .getString("crypto.2D"));
197: }
198: generated = false;
199: int len = spiImpl.engineGenExemptionBlob(output, outputOffset);
200: generated = true;
201: return len;
202: }
203:
204: @Override
205: protected void finalize() {
206: initKey = null;
207: }
208: }
|