Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/security/KeyRep.java
38829 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.security;2627import java.io.*;28import java.util.Locale;2930import java.security.spec.PKCS8EncodedKeySpec;31import java.security.spec.X509EncodedKeySpec;32import java.security.spec.InvalidKeySpecException;3334import javax.crypto.SecretKeyFactory;35import javax.crypto.spec.SecretKeySpec;3637/**38* Standardized representation for serialized Key objects.39*40* <p>41*42* Note that a serialized Key may contain sensitive information43* which should not be exposed in untrusted environments. See the44* <a href="../../../platform/serialization/spec/security.html">45* Security Appendix</a>46* of the Serialization Specification for more information.47*48* @see Key49* @see KeyFactory50* @see javax.crypto.spec.SecretKeySpec51* @see java.security.spec.X509EncodedKeySpec52* @see java.security.spec.PKCS8EncodedKeySpec53*54* @since 1.555*/5657public class KeyRep implements Serializable {5859private static final long serialVersionUID = -4757683898830641853L;6061/**62* Key type.63*64* @since 1.565*/66public static enum Type {6768/** Type for secret keys. */69SECRET,7071/** Type for public keys. */72PUBLIC,7374/** Type for private keys. */75PRIVATE,7677}7879private static final String PKCS8 = "PKCS#8";80private static final String X509 = "X.509";81private static final String RAW = "RAW";8283/**84* Either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE85*86* @serial87*/88private Type type;8990/**91* The Key algorithm92*93* @serial94*/95private String algorithm;9697/**98* The Key encoding format99*100* @serial101*/102private String format;103104/**105* The encoded Key bytes106*107* @serial108*/109private byte[] encoded;110111/**112* Construct the alternate Key class.113*114* <p>115*116* @param type either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE117* @param algorithm the algorithm returned from118* {@code Key.getAlgorithm()}119* @param format the encoding format returned from120* {@code Key.getFormat()}121* @param encoded the encoded bytes returned from122* {@code Key.getEncoded()}123*124* @exception NullPointerException125* if type is {@code null},126* if algorithm is {@code null},127* if format is {@code null},128* or if encoded is {@code null}129*/130public KeyRep(Type type, String algorithm,131String format, byte[] encoded) {132133if (type == null || algorithm == null ||134format == null || encoded == null) {135throw new NullPointerException("invalid null input(s)");136}137138this.type = type;139this.algorithm = algorithm;140this.format = format.toUpperCase(Locale.ENGLISH);141this.encoded = encoded.clone();142}143144/**145* Resolve the Key object.146*147* <p> This method supports three Type/format combinations:148* <ul>149* <li> Type.SECRET/"RAW" - returns a SecretKeySpec object150* constructed using encoded key bytes and algorithm151* <li> Type.PUBLIC/"X.509" - gets a KeyFactory instance for152* the key algorithm, constructs an X509EncodedKeySpec with the153* encoded key bytes, and generates a public key from the spec154* <li> Type.PRIVATE/"PKCS#8" - gets a KeyFactory instance for155* the key algorithm, constructs a PKCS8EncodedKeySpec with the156* encoded key bytes, and generates a private key from the spec157* </ul>158*159* <p>160*161* @return the resolved Key object162*163* @exception ObjectStreamException if the Type/format164* combination is unrecognized, if the algorithm, key format, or165* encoded key bytes are unrecognized/invalid, of if the166* resolution of the key fails for any reason167*/168protected Object readResolve() throws ObjectStreamException {169try {170if (type == Type.SECRET && RAW.equals(format)) {171return new SecretKeySpec(encoded, algorithm);172} else if (type == Type.PUBLIC && X509.equals(format)) {173KeyFactory f = KeyFactory.getInstance(algorithm);174return f.generatePublic(new X509EncodedKeySpec(encoded));175} else if (type == Type.PRIVATE && PKCS8.equals(format)) {176KeyFactory f = KeyFactory.getInstance(algorithm);177return f.generatePrivate(new PKCS8EncodedKeySpec(encoded));178} else {179throw new NotSerializableException180("unrecognized type/format combination: " +181type + "/" + format);182}183} catch (NotSerializableException nse) {184throw nse;185} catch (Exception e) {186NotSerializableException nse = new NotSerializableException187("java.security.Key: " +188"[" + type + "] " +189"[" + algorithm + "] " +190"[" + format + "]");191nse.initCause(e);192throw nse;193}194}195}196197198