Path: blob/master/src/java.base/share/classes/sun/security/x509/KeyIdentifier.java
41159 views
/*1* Copyright (c) 1997, 2018, 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 sun.security.x509;2627import java.io.IOException;28import java.security.PublicKey;29import java.security.MessageDigest;30import java.security.NoSuchAlgorithmException;3132import sun.security.util.HexDumpEncoder;33import sun.security.util.*;3435/**36* Represent the Key Identifier ASN.1 object.37*38* @author Amit Kapoor39* @author Hemma Prafullchandra40*/41public class KeyIdentifier {42private byte[] octetString;4344/**45* Create a KeyIdentifier with the passed bit settings.46*47* @param octetString the octet string identifying the key identifier.48*/49public KeyIdentifier(byte[] octetString) {50this.octetString = octetString.clone();51}5253/**54* Create a KeyIdentifier from the DER encoded value.55*56* @param val the DerValue57*/58public KeyIdentifier(DerValue val) throws IOException {59octetString = val.getOctetString();60}6162/**63* Creates a KeyIdentifier from a public-key value.64*65* <p>From RFC 5280: Two common methods for generating key identifiers from66* the public key are:67* <ol>68* <li>The keyIdentifier is composed of the 160-bit SHA-1 hash of the69* value of the BIT STRING subjectPublicKey (excluding the tag,70* length, and number of unused bits).71*72* <li>The keyIdentifier is composed of a four bit type field with73* the value 0100 followed by the least significant 60 bits of the74* SHA-1 hash of the value of the BIT STRING subjectPublicKey.75* </ol>76* <p>This method supports method 1.77*78* @param pubKey the public key from which to construct this KeyIdentifier79* @throws IOException on parsing errors80*/81public KeyIdentifier(PublicKey pubKey)82throws IOException83{84DerValue algAndKey = new DerValue(pubKey.getEncoded());85if (algAndKey.tag != DerValue.tag_Sequence)86throw new IOException("PublicKey value is not a valid "87+ "X.509 public key");8889AlgorithmId algid = AlgorithmId.parse(algAndKey.data.getDerValue());90byte[] key = algAndKey.data.getUnalignedBitString().toByteArray();9192MessageDigest md = null;93try {94md = MessageDigest.getInstance("SHA1");95} catch (NoSuchAlgorithmException e3) {96throw new IOException("SHA1 not supported");97}98md.update(key);99this.octetString = md.digest();100}101102/**103* Return the value of the KeyIdentifier as byte array.104*/105public byte[] getIdentifier() {106return octetString.clone();107}108109/**110* Returns a printable representation of the KeyUsage.111*/112public String toString() {113String s = "KeyIdentifier [\n";114115HexDumpEncoder encoder = new HexDumpEncoder();116s += encoder.encodeBuffer(octetString);117s += "]\n";118return (s);119}120121/**122* Write the KeyIdentifier to the DerOutputStream.123*124* @param out the DerOutputStream to write the object to.125* @exception IOException126*/127void encode(DerOutputStream out) throws IOException {128out.putOctetString(octetString);129}130131/**132* Returns a hash code value for this object.133* Objects that are equal will also have the same hashcode.134*/135public int hashCode () {136int retval = 0;137for (int i = 0; i < octetString.length; i++)138retval += octetString[i] * i;139return retval;140}141142/**143* Indicates whether some other object is "equal to" this one.144*/145public boolean equals(Object other) {146if (this == other)147return true;148if (!(other instanceof KeyIdentifier))149return false;150byte[] otherString = ((KeyIdentifier)other).octetString;151return java.util.Arrays.equals(octetString, otherString);152}153}154155156