1 /* 2 * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.security.spec; 27 28 import java.util.Arrays; 29 30 /** 31 * This class represents a public or private key in encoded format. 32 * 33 * @author Jan Luehe 34 * 35 * 36 * @see java.security.Key 37 * @see java.security.KeyFactory 38 * @see KeySpec 39 * @see X509EncodedKeySpec 40 * @see PKCS8EncodedKeySpec 41 * 42 * @since 1.2 43 */ 44 45 public abstract class EncodedKeySpec implements KeySpec { 46 47 private byte[] encodedKey; 48 private String algorithmName; 49 // Android-removed: Unused SharedSecrets. 50 /* 51 static { 52 SharedSecrets.setJavaSecuritySpecAccess( 53 new JavaSecuritySpecAccess() { 54 @Override 55 public void clearEncodedKeySpec(EncodedKeySpec keySpec) { 56 keySpec.clear(); 57 } 58 }); 59 } 60 */ 61 /** 62 * Creates a new {@code EncodedKeySpec} with the given encoded key. 63 * 64 * @param encodedKey the encoded key. The contents of the 65 * array are copied to protect against subsequent modification. 66 * @throws NullPointerException if {@code encodedKey} 67 * is null. 68 */ EncodedKeySpec(byte[] encodedKey)69 public EncodedKeySpec(byte[] encodedKey) { 70 this.encodedKey = encodedKey.clone(); 71 } 72 73 /** 74 * Creates a new {@code EncodedKeySpec} with the given encoded key. 75 * This constructor is useful when subsequent callers of the 76 * {@code EncodedKeySpec} object might not know the algorithm 77 * of the key. 78 * 79 * @param encodedKey the encoded key. The contents of the 80 * array are copied to protect against subsequent modification. 81 * @param algorithm the algorithm name of the encoded key 82 * See the KeyFactory section in the <a href= 83 * "{@docRoot}/../specs/security/standard-names.html#keyfactory-algorithms"> 84 * Java Security Standard Algorithm Names Specification</a> 85 * for information about standard algorithm names. 86 * @throws NullPointerException if {@code encodedKey} 87 * or {@code algorithm} is null. 88 * @throws IllegalArgumentException if {@code algorithm} is 89 * the empty string {@code ""} 90 * @since 9 91 */ EncodedKeySpec(byte[] encodedKey, String algorithm)92 protected EncodedKeySpec(byte[] encodedKey, String algorithm) { 93 if (algorithm == null) { 94 throw new NullPointerException("algorithm name may not be null"); 95 } 96 if (algorithm.isEmpty()) { 97 throw new IllegalArgumentException("algorithm name " 98 + "may not be empty"); 99 } 100 this.encodedKey = encodedKey.clone(); 101 this.algorithmName = algorithm; 102 103 } 104 105 /** 106 * Returns the name of the algorithm of the encoded key. 107 * 108 * @return the name of the algorithm, or null if not specified 109 * @since 9 110 */ getAlgorithm()111 public String getAlgorithm() { 112 return algorithmName; 113 } 114 115 /** 116 * Returns the encoded key. 117 * 118 * @return the encoded key. Returns a new array each time 119 * this method is called. 120 */ getEncoded()121 public byte[] getEncoded() { 122 return this.encodedKey.clone(); 123 } 124 125 /** 126 * Returns the name of the encoding format associated with this 127 * key specification. 128 * 129 * <p>If the opaque representation of a key 130 * (see {@link java.security.Key Key}) can be transformed 131 * (see {@link java.security.KeyFactory KeyFactory}) 132 * into this key specification (or a subclass of it), 133 * {@code getFormat} called 134 * on the opaque key returns the same value as the 135 * {@code getFormat} method 136 * of this key specification. 137 * 138 * @return a string representation of the encoding format. 139 */ getFormat()140 public abstract String getFormat(); 141 142 /** 143 * Clear the encoding inside. 144 */ clear()145 void clear() { 146 Arrays.fill(encodedKey, (byte)0); 147 } 148 } 149