Path: blob/master/src/java.base/share/classes/java/security/KeyRep.java
41152 views
/*1* Copyright (c) 2003, 2019, 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="{@docRoot}/../specs/serialization/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 {5859@java.io.Serial60private static final long serialVersionUID = -4757683898830641853L;6162/**63* Key type.64*65* @since 1.566*/67public static enum Type {6869/** Type for secret keys. */70SECRET,7172/** Type for public keys. */73PUBLIC,7475/** Type for private keys. */76PRIVATE,7778}7980private static final String PKCS8 = "PKCS#8";81private static final String X509 = "X.509";82private static final String RAW = "RAW";8384/**85* Either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE86*87* @serial88*/89private Type type;9091/**92* The Key algorithm93*94* @serial95*/96private String algorithm;9798/**99* The Key encoding format100*101* @serial102*/103private String format;104105/**106* The encoded Key bytes107*108* @serial109*/110private byte[] encoded;111112/**113* Construct the alternate Key class.114*115* @param type either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE116* @param algorithm the algorithm returned from117* {@code Key.getAlgorithm()}118* @param format the encoding format returned from119* {@code Key.getFormat()}120* @param encoded the encoded bytes returned from121* {@code Key.getEncoded()}122*123* @throws NullPointerException124* if type is {@code null},125* if algorithm is {@code null},126* if format is {@code null},127* or if encoded is {@code null}128*/129public KeyRep(Type type, String algorithm,130String format, byte[] encoded) {131132if (type == null || algorithm == null ||133format == null || encoded == null) {134throw new NullPointerException("invalid null input(s)");135}136137this.type = type;138this.algorithm = algorithm;139this.format = format.toUpperCase(Locale.ENGLISH);140this.encoded = encoded.clone();141}142143/**144* Resolve the Key object.145*146* <p> This method supports three Type/format combinations:147* <ul>148* <li> Type.SECRET/"RAW" - returns a SecretKeySpec object149* constructed using encoded key bytes and algorithm150* <li> Type.PUBLIC/"X.509" - gets a KeyFactory instance for151* the key algorithm, constructs an X509EncodedKeySpec with the152* encoded key bytes, and generates a public key from the spec153* <li> Type.PRIVATE/"PKCS#8" - gets a KeyFactory instance for154* the key algorithm, constructs a PKCS8EncodedKeySpec with the155* encoded key bytes, and generates a private key from the spec156* </ul>157*158* @return the resolved Key object159*160* @throws ObjectStreamException if the Type/format161* combination is unrecognized, if the algorithm, key format, or162* encoded key bytes are unrecognized/invalid, of if the163* resolution of the key fails for any reason164*/165@java.io.Serial166protected Object readResolve() throws ObjectStreamException {167try {168if (type == Type.SECRET && RAW.equals(format)) {169return new SecretKeySpec(encoded, algorithm);170} else if (type == Type.PUBLIC && X509.equals(format)) {171KeyFactory f = KeyFactory.getInstance(algorithm);172return f.generatePublic(new X509EncodedKeySpec(encoded));173} else if (type == Type.PRIVATE && PKCS8.equals(format)) {174KeyFactory f = KeyFactory.getInstance(algorithm);175return f.generatePrivate(new PKCS8EncodedKeySpec(encoded));176} else {177throw new NotSerializableException178("unrecognized type/format combination: " +179type + "/" + format);180}181} catch (NotSerializableException nse) {182throw nse;183} catch (Exception e) {184NotSerializableException nse = new NotSerializableException185("java.security.Key: " +186"[" + type + "] " +187"[" + algorithm + "] " +188"[" + format + "]");189nse.initCause(e);190throw nse;191}192}193}194195196