0N/A/*
2362N/A * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.x509;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.security.PublicKey;
0N/Aimport java.security.MessageDigest;
0N/Aimport java.security.NoSuchAlgorithmException;
0N/A
0N/Aimport sun.misc.HexDumpEncoder;
0N/Aimport sun.security.util.*;
0N/A
0N/A/**
0N/A * Represent the Key Identifier ASN.1 object.
0N/A *
0N/A * @author Amit Kapoor
0N/A * @author Hemma Prafullchandra
0N/A */
0N/Apublic class KeyIdentifier {
0N/A private byte[] octetString;
0N/A
0N/A /**
0N/A * Create a KeyIdentifier with the passed bit settings.
0N/A *
0N/A * @param octetString the octet string identifying the key identifier.
0N/A */
0N/A public KeyIdentifier(byte[] octetString) {
0N/A this.octetString = octetString.clone();
0N/A }
0N/A
0N/A /**
0N/A * Create a KeyIdentifier from the DER encoded value.
0N/A *
0N/A * @param val the DerValue
0N/A */
0N/A public KeyIdentifier(DerValue val) throws IOException {
0N/A octetString = val.getOctetString();
0N/A }
0N/A
0N/A /**
0N/A * Creates a KeyIdentifier from a public-key value.
0N/A *
0N/A * <p>From RFC2459: Two common methods for generating key identifiers from
0N/A * the public key are:
0N/A * <ol>
0N/A * <li>The keyIdentifier is composed of the 160-bit SHA-1 hash of the
0N/A * value of the BIT STRING subjectPublicKey (excluding the tag,
0N/A * length, and number of unused bits).
0N/A * <p>
0N/A * <li>The keyIdentifier is composed of a four bit type field with
0N/A * the value 0100 followed by the least significant 60 bits of the
0N/A * SHA-1 hash of the value of the BIT STRING subjectPublicKey.
0N/A * </ol>
0N/A * <p>This method supports method 1.
0N/A *
0N/A * @param pubKey the public key from which to construct this KeyIdentifier
0N/A * @throws IOException on parsing errors
0N/A */
0N/A public KeyIdentifier(PublicKey pubKey)
0N/A throws IOException
0N/A {
0N/A DerValue algAndKey = new DerValue(pubKey.getEncoded());
0N/A if (algAndKey.tag != DerValue.tag_Sequence)
0N/A throw new IOException("PublicKey value is not a valid "
0N/A + "X.509 public key");
0N/A
0N/A AlgorithmId algid = AlgorithmId.parse(algAndKey.data.getDerValue());
0N/A byte[] key = algAndKey.data.getUnalignedBitString().toByteArray();
0N/A
0N/A MessageDigest md = null;
0N/A try {
0N/A md = MessageDigest.getInstance("SHA1");
0N/A } catch (NoSuchAlgorithmException e3) {
0N/A throw new IOException("SHA1 not supported");
0N/A }
0N/A md.update(key);
0N/A this.octetString = md.digest();
0N/A }
0N/A
0N/A /**
0N/A * Return the value of the KeyIdentifier as byte array.
0N/A */
0N/A public byte[] getIdentifier() {
0N/A return octetString.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns a printable representation of the KeyUsage.
0N/A */
0N/A public String toString() {
0N/A String s = "KeyIdentifier [\n";
0N/A
0N/A HexDumpEncoder encoder = new HexDumpEncoder();
0N/A s += encoder.encodeBuffer(octetString);
0N/A s += "]\n";
0N/A return (s);
0N/A }
0N/A
0N/A /**
0N/A * Write the KeyIdentifier to the DerOutputStream.
0N/A *
0N/A * @param out the DerOutputStream to write the object to.
0N/A * @exception IOException
0N/A */
0N/A void encode(DerOutputStream out) throws IOException {
0N/A out.putOctetString(octetString);
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code value for this object.
0N/A * Objects that are equal will also have the same hashcode.
0N/A */
0N/A public int hashCode () {
0N/A int retval = 0;
0N/A for (int i = 0; i < octetString.length; i++)
0N/A retval += octetString[i] * i;
0N/A return retval;
0N/A }
0N/A
0N/A /**
0N/A * Indicates whether some other object is "equal to" this one.
0N/A */
0N/A public boolean equals(Object other) {
0N/A if (this == other)
0N/A return true;
0N/A if (!(other instanceof KeyIdentifier))
0N/A return false;
0N/A return java.util.Arrays.equals(octetString,
0N/A ((KeyIdentifier)other).getIdentifier());
0N/A }
0N/A}