001: /*
002: * @(#)KeyIdentifier.java 1.17 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.x509;
029:
030: import java.io.IOException;
031: import java.security.PublicKey;
032: import java.security.MessageDigest;
033: import java.security.NoSuchAlgorithmException;
034:
035: import sun.misc.HexDumpEncoder;
036: import sun.security.util.*;
037:
038: /**
039: * Represent the Key Identifier ASN.1 object.
040: *
041: * @author Amit Kapoor
042: * @author Hemma Prafullchandra
043: * @version 1.10
044: */
045: public class KeyIdentifier {
046: private byte[] octetString;
047:
048: /**
049: * Create a KeyIdentifier with the passed bit settings.
050: *
051: * @param octetString the octet string identifying the key identifier.
052: */
053: public KeyIdentifier(byte[] octetString) {
054: this .octetString = (byte[]) octetString.clone();
055: }
056:
057: /**
058: * Create a KeyIdentifier from the DER encoded value.
059: *
060: * @param val the DerValue
061: */
062: public KeyIdentifier(DerValue val) throws IOException {
063: octetString = val.getOctetString();
064: }
065:
066: /**
067: * Creates a KeyIdentifier from a public-key value.
068: *
069: * <p>From RFC2459: Two common methods for generating key identifiers from
070: * the public key are:
071: * <ol>
072: * <li>The keyIdentifier is composed of the 160-bit SHA-1 hash of the
073: * value of the BIT STRING subjectPublicKey (excluding the tag,
074: * length, and number of unused bits).
075: * <p>
076: * <li>The keyIdentifier is composed of a four bit type field with
077: * the value 0100 followed by the least significant 60 bits of the
078: * SHA-1 hash of the value of the BIT STRING subjectPublicKey.
079: * </ol>
080: * <p>This method supports method 1.
081: *
082: * @param pubKey the public key from which to construct this KeyIdentifier
083: * @throws IOException on parsing errors
084: */
085: public KeyIdentifier(PublicKey pubKey) throws IOException {
086: DerValue algAndKey = new DerValue(pubKey.getEncoded());
087: if (algAndKey.tag != DerValue.tag_Sequence)
088: throw new IOException("PublicKey value is not a valid "
089: + "X.509 public key");
090:
091: AlgorithmId algid = AlgorithmId.parse(algAndKey.data
092: .getDerValue());
093: byte[] key = algAndKey.data.getUnalignedBitString()
094: .toByteArray();
095:
096: MessageDigest md = null;
097: try {
098: md = MessageDigest.getInstance("SHA1");
099: } catch (NoSuchAlgorithmException e3) {
100: throw new IOException("SHA1 not supported");
101: }
102: md.update(key);
103: this .octetString = md.digest();
104: }
105:
106: /**
107: * Return the value of the KeyIdentifier as byte array.
108: */
109: public byte[] getIdentifier() {
110: return ((byte[]) octetString.clone());
111: }
112:
113: /**
114: * Returns a printable representation of the KeyUsage.
115: */
116: public String toString() {
117: String s = "KeyIdentifier [\n";
118:
119: HexDumpEncoder encoder = new HexDumpEncoder();
120: s += encoder.encodeBuffer(octetString);
121: s += "]\n";
122: return (s);
123: }
124:
125: /**
126: * Write the KeyIdentifier to the DerOutputStream.
127: *
128: * @param out the DerOutputStream to write the object to.
129: * @exception IOException
130: */
131: void encode(DerOutputStream out) throws IOException {
132: out.putOctetString(octetString);
133: }
134:
135: /**
136: * Returns a hash code value for this object.
137: * Objects that are equal will also have the same hashcode.
138: */
139: public int hashCode() {
140: int retval = 0;
141: for (int i = 0; i < octetString.length; i++)
142: retval += octetString[i] * i;
143: return retval;
144: }
145:
146: /**
147: * Indicates whether some other object is "equal to" this one.
148: */
149: public boolean equals(Object other) {
150: if (this == other)
151: return true;
152: if (!(other instanceof KeyIdentifier))
153: return false;
154: return java.util.Arrays.equals(octetString,
155: ((KeyIdentifier) other).getIdentifier());
156: }
157: }
|