jdk/src/share/classes/sun/security/x509/KeyIdentifier.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-1999 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PublicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.misc.HexDumpEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Represent the Key Identifier ASN.1 object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class KeyIdentifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private byte[] octetString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Create a KeyIdentifier with the passed bit settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @param octetString the octet string identifying the key identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public KeyIdentifier(byte[] octetString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this.octetString = octetString.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Create a KeyIdentifier from the DER encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param val the DerValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public KeyIdentifier(DerValue val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        octetString = val.getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates a KeyIdentifier from a public-key value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <p>From RFC2459: Two common methods for generating key identifiers from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * the public key are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <li>The keyIdentifier is composed of the 160-bit SHA-1 hash of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * value of the BIT STRING subjectPublicKey (excluding the tag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * length, and number of unused bits).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <li>The keyIdentifier is composed of a four bit type field with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * the value 0100 followed by the least significant 60 bits of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * SHA-1 hash of the value of the BIT STRING subjectPublicKey.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <p>This method supports method 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param pubKey the public key from which to construct this KeyIdentifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @throws IOException on parsing errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public KeyIdentifier(PublicKey pubKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        DerValue algAndKey = new DerValue(pubKey.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (algAndKey.tag != DerValue.tag_Sequence)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new IOException("PublicKey value is not a valid "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                  + "X.509 public key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        AlgorithmId algid = AlgorithmId.parse(algAndKey.data.getDerValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        byte[] key = algAndKey.data.getUnalignedBitString().toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        MessageDigest md = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            md = MessageDigest.getInstance("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (NoSuchAlgorithmException e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IOException("SHA1 not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        md.update(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.octetString = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Return the value of the KeyIdentifier as byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public byte[] getIdentifier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return octetString.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Returns a printable representation of the KeyUsage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String s = "KeyIdentifier [\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        HexDumpEncoder encoder = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        s += encoder.encodeBuffer(octetString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        s += "]\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Write the KeyIdentifier to the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param out the DerOutputStream to write the object to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        out.putOctetString(octetString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Objects that are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public int hashCode () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        for (int i = 0; i < octetString.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            retval += octetString[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Indicates whether some other object is "equal to" this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (this == other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (!(other instanceof KeyIdentifier))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return java.util.Arrays.equals(octetString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                       ((KeyIdentifier)other).getIdentifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}