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: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package javax.crypto;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.IOException;
026: import java.io.ObjectInputStream;
027: import java.io.ObjectOutputStream;
028: import java.io.Serializable;
029: import java.security.AlgorithmParameters;
030: import java.security.InvalidAlgorithmParameterException;
031: import java.security.InvalidKeyException;
032: import java.security.Key;
033: import java.security.NoSuchAlgorithmException;
034: import java.security.NoSuchProviderException;
035: import javax.crypto.IllegalBlockSizeException;
036: import javax.crypto.NoSuchPaddingException;
037:
038: import org.apache.harmony.crypto.internal.nls.Messages;
039:
040: /**
041: * @com.intel.drl.spec_ref
042: */
043: public class SealedObject implements Serializable {
044:
045: // the value of this field was derived by using serialver utility
046: /**
047: * @com.intel.drl.spec_ref
048: */
049: private static final long serialVersionUID = 4482838265551344752L;
050:
051: /**
052: * @com.intel.drl.spec_ref
053: */
054: protected byte[] encodedParams;
055: private byte[] encryptedContent;
056: private String sealAlg;
057: private String paramsAlg;
058:
059: private void readObject(ObjectInputStream s) throws IOException,
060: ClassNotFoundException {
061: encodedParams = (byte[]) s.readUnshared();
062: encryptedContent = (byte[]) s.readUnshared();
063: sealAlg = (String) s.readUnshared();
064: paramsAlg = (String) s.readUnshared();
065: }
066:
067: /**
068: * @com.intel.drl.spec_ref
069: */
070: public SealedObject(Serializable object, Cipher c)
071: throws IOException, IllegalBlockSizeException {
072: if (c == null) {
073: throw new NullPointerException(Messages
074: .getString("crypto.13")); //$NON-NLS-1$
075: }
076: try {
077: ByteArrayOutputStream bos = new ByteArrayOutputStream();
078: ObjectOutputStream oos = new ObjectOutputStream(bos);
079: oos.writeObject(object);
080: oos.flush();
081: AlgorithmParameters ap = c.getParameters();
082: this .encodedParams = (ap == null) ? null : ap.getEncoded();
083: this .paramsAlg = (ap == null) ? null : ap.getAlgorithm();
084: this .sealAlg = c.getAlgorithm();
085: this .encryptedContent = c.doFinal(bos.toByteArray());
086: } catch (BadPaddingException e) {
087: // should be never thrown because the cipher
088: // should be initialized for encryption
089: throw new IOException(e.toString());
090: }
091: }
092:
093: /**
094: * @com.intel.drl.spec_ref
095: */
096: protected SealedObject(SealedObject so) {
097: if (so == null) {
098: throw new NullPointerException(Messages
099: .getString("crypto.14")); //$NON-NLS-1$
100: }
101: this .encryptedContent = so.encryptedContent;
102: this .encodedParams = so.encodedParams;
103: this .sealAlg = so.sealAlg;
104: this .paramsAlg = so.paramsAlg;
105: }
106:
107: /**
108: * @com.intel.drl.spec_ref
109: */
110: public final String getAlgorithm() {
111: return sealAlg;
112: }
113:
114: /**
115: * @com.intel.drl.spec_ref
116: */
117: public final Object getObject(Key key) throws IOException,
118: ClassNotFoundException, NoSuchAlgorithmException,
119: InvalidKeyException {
120: try {
121: Cipher cipher = Cipher.getInstance(sealAlg);
122: if ((paramsAlg != null) && (paramsAlg.length() != 0)) {
123: AlgorithmParameters params = AlgorithmParameters
124: .getInstance(paramsAlg);
125: params.init(encodedParams);
126: cipher.init(Cipher.DECRYPT_MODE, key, params);
127: } else {
128: cipher.init(Cipher.DECRYPT_MODE, key);
129: }
130: byte[] serialized = cipher.doFinal(encryptedContent);
131: ObjectInputStream ois = new ObjectInputStream(
132: new ByteArrayInputStream(serialized));
133: return ois.readObject();
134: } catch (NoSuchPaddingException e) {
135: // should not be thrown because cipher text was made
136: // with existing padding
137: throw new NoSuchAlgorithmException(e.toString());
138: } catch (InvalidAlgorithmParameterException e) {
139: // should not be thrown because cipher text was made
140: // with correct algorithm parameters
141: throw new NoSuchAlgorithmException(e.toString());
142: } catch (IllegalBlockSizeException e) {
143: // should not be thrown because the cipher text
144: // was correctly made
145: throw new NoSuchAlgorithmException(e.toString());
146: } catch (BadPaddingException e) {
147: // should not be thrown because the cipher text
148: // was correctly made
149: throw new NoSuchAlgorithmException(e.toString());
150: } catch (IllegalStateException e) {
151: // should never be thrown because cipher is initialized
152: throw new NoSuchAlgorithmException(e.toString());
153: }
154: }
155:
156: /**
157: * @com.intel.drl.spec_ref
158: */
159: public final Object getObject(Cipher c) throws IOException,
160: ClassNotFoundException, IllegalBlockSizeException,
161: BadPaddingException {
162: if (c == null) {
163: throw new NullPointerException(Messages
164: .getString("crypto.13")); //$NON-NLS-1$
165: }
166: byte[] serialized = c.doFinal(encryptedContent);
167: ObjectInputStream ois = new ObjectInputStream(
168: new ByteArrayInputStream(serialized));
169: return ois.readObject();
170: }
171:
172: /**
173: * @com.intel.drl.spec_ref
174: */
175: public final Object getObject(Key key, String provider)
176: throws IOException, ClassNotFoundException,
177: NoSuchAlgorithmException, NoSuchProviderException,
178: InvalidKeyException {
179: if ((provider == null) || (provider.length() == 0)) {
180: throw new IllegalArgumentException(Messages
181: .getString("crypto.15")); //$NON-NLS-1$
182: }
183: try {
184: Cipher cipher = Cipher.getInstance(sealAlg, provider);
185: if ((paramsAlg != null) && (paramsAlg.length() != 0)) {
186: AlgorithmParameters params = AlgorithmParameters
187: .getInstance(paramsAlg);
188: params.init(encodedParams);
189: cipher.init(Cipher.DECRYPT_MODE, key, params);
190: } else {
191: cipher.init(Cipher.DECRYPT_MODE, key);
192: }
193: byte[] serialized = cipher.doFinal(encryptedContent);
194: ObjectInputStream ois = new ObjectInputStream(
195: new ByteArrayInputStream(serialized));
196: return ois.readObject();
197: } catch (NoSuchPaddingException e) {
198: // should not be thrown because cipher text was made
199: // with existing padding
200: throw new NoSuchAlgorithmException(e.toString());
201: } catch (InvalidAlgorithmParameterException e) {
202: // should not be thrown because cipher text was made
203: // with correct algorithm parameters
204: throw new NoSuchAlgorithmException(e.toString());
205: } catch (IllegalBlockSizeException e) {
206: // should not be thrown because the cipher text
207: // was correctly made
208: throw new NoSuchAlgorithmException(e.toString());
209: } catch (BadPaddingException e) {
210: // should not be thrown because the cipher text
211: // was correctly made
212: throw new NoSuchAlgorithmException(e.toString());
213: } catch (IllegalStateException e) {
214: // should never be thrown because cipher is initialized
215: throw new NoSuchAlgorithmException(e.toString());
216: }
217: }
218: }
|