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 Vladimir N. Molotkov
020: * @version $Revision$
021: */package java.security;
022:
023: import java.io.IOException;
024: import java.io.NotSerializableException;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectStreamException;
027: import java.io.Serializable;
028: import java.security.spec.X509EncodedKeySpec;
029: import java.security.spec.PKCS8EncodedKeySpec;
030: import java.security.spec.InvalidKeySpecException;
031:
032: import javax.crypto.spec.SecretKeySpec;
033:
034: import org.apache.harmony.security.internal.nls.Messages;
035:
036: /**
037: * @com.intel.drl.spec_ref
038: *
039: */
040: public class KeyRep implements Serializable {
041: /**
042: * @com.intel.drl.spec_ref
043: */
044: private static final long serialVersionUID = -4757683898830641853L;
045: // Key type
046: private final Type type;
047: // Key algorithm name
048: private final String algorithm;
049: // Key encoding format
050: private final String format;
051: // Key encoding
052: private byte[] encoded;
053:
054: /**
055: * @com.intel.drl.spec_ref
056: */
057: public KeyRep(Type type, String algorithm, String format,
058: byte[] encoded) {
059: this .type = type;
060: this .algorithm = algorithm;
061: this .format = format;
062: this .encoded = encoded;
063: if (this .type == null) {
064: throw new NullPointerException(Messages
065: .getString("security.07")); //$NON-NLS-1$
066: }
067: if (this .algorithm == null) {
068: throw new NullPointerException(Messages
069: .getString("security.08")); //$NON-NLS-1$
070: }
071: if (this .format == null) {
072: throw new NullPointerException(Messages
073: .getString("security.09")); //$NON-NLS-1$
074: }
075: if (this .encoded == null) {
076: throw new NullPointerException(Messages
077: .getString("security.0A")); //$NON-NLS-1$
078: }
079: }
080:
081: /**
082: * @com.intel.drl.spec_ref
083: */
084: protected Object readResolve() throws ObjectStreamException {
085: switch (type) {
086: case SECRET:
087: if ("RAW".equals(format)) { //$NON-NLS-1$
088: try {
089: return new SecretKeySpec(encoded, algorithm);
090: } catch (IllegalArgumentException e) {
091: throw new NotSerializableException(Messages
092: .getString("security.0B", e)); //$NON-NLS-1$
093: }
094: }
095: throw new NotSerializableException(Messages.getString(
096: "security.0C", type, format)); //$NON-NLS-1$
097: case PUBLIC:
098: if ("X.509".equals(format)) { //$NON-NLS-1$
099: try {
100: KeyFactory kf = KeyFactory.getInstance(algorithm);
101: return kf.generatePublic(new X509EncodedKeySpec(
102: encoded));
103: } catch (NoSuchAlgorithmException e) {
104: throw new NotSerializableException(Messages
105: .getString("security.0D", e)); //$NON-NLS-1$
106: } catch (InvalidKeySpecException e) {
107: throw new NotSerializableException(Messages
108: .getString("security.0D", e)); //$NON-NLS-1$
109: }
110: }
111: throw new NotSerializableException(Messages.getString(
112: "security.0C", type, format)); //$NON-NLS-1$
113: case PRIVATE:
114: if ("PKCS#8".equals(format)) { //$NON-NLS-1$
115: try {
116: KeyFactory kf = KeyFactory.getInstance(algorithm);
117: return kf.generatePrivate(new PKCS8EncodedKeySpec(
118: encoded));
119: } catch (NoSuchAlgorithmException e) {
120: throw new NotSerializableException(Messages
121: .getString("security.0D", e)); //$NON-NLS-1$
122: } catch (InvalidKeySpecException e) {
123: throw new NotSerializableException(Messages
124: .getString("security.0D", e)); //$NON-NLS-1$
125: }
126: }
127: throw new NotSerializableException(Messages.getString(
128: "security.0C", type, format)); //$NON-NLS-1$
129: }
130: throw new NotSerializableException(Messages.getString(
131: "security.0E", type)); //$NON-NLS-1$
132: }
133:
134: // Makes defensive copy of key encoding
135: private void readObject(ObjectInputStream is) throws IOException,
136: ClassNotFoundException {
137: is.defaultReadObject();
138: byte[] new_encoded = new byte[encoded.length];
139: System
140: .arraycopy(encoded, 0, new_encoded, 0,
141: new_encoded.length);
142: encoded = new_encoded;
143: }
144:
145: /**
146: * Supported key types
147: */
148: public static enum Type {
149: SECRET, PUBLIC, PRIVATE
150: }
151: }
|