author | igerasim |
Tue, 02 Oct 2018 10:19:07 -0700 | |
changeset 51986 | c1db377f6300 |
parent 47216 | 71c04702a3d5 |
child 53082 | 4c539cb11633 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.x509; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.security.PublicKey; |
|
30 |
import java.security.MessageDigest; |
|
31 |
import java.security.NoSuchAlgorithmException; |
|
32 |
||
34687
d302ed125dc9
8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents:
30374
diff
changeset
|
33 |
import sun.security.util.HexDumpEncoder; |
2 | 34 |
import sun.security.util.*; |
35 |
||
36 |
/** |
|
37 |
* Represent the Key Identifier ASN.1 object. |
|
38 |
* |
|
39 |
* @author Amit Kapoor |
|
40 |
* @author Hemma Prafullchandra |
|
41 |
*/ |
|
42 |
public class KeyIdentifier { |
|
43 |
private byte[] octetString; |
|
44 |
||
45 |
/** |
|
46 |
* Create a KeyIdentifier with the passed bit settings. |
|
47 |
* |
|
48 |
* @param octetString the octet string identifying the key identifier. |
|
49 |
*/ |
|
50 |
public KeyIdentifier(byte[] octetString) { |
|
51 |
this.octetString = octetString.clone(); |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* Create a KeyIdentifier from the DER encoded value. |
|
56 |
* |
|
57 |
* @param val the DerValue |
|
58 |
*/ |
|
59 |
public KeyIdentifier(DerValue val) throws IOException { |
|
60 |
octetString = val.getOctetString(); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Creates a KeyIdentifier from a public-key value. |
|
65 |
* |
|
66 |
* <p>From RFC2459: Two common methods for generating key identifiers from |
|
67 |
* the public key are: |
|
68 |
* <ol> |
|
69 |
* <li>The keyIdentifier is composed of the 160-bit SHA-1 hash of the |
|
70 |
* value of the BIT STRING subjectPublicKey (excluding the tag, |
|
71 |
* length, and number of unused bits). |
|
30374 | 72 |
* |
2 | 73 |
* <li>The keyIdentifier is composed of a four bit type field with |
74 |
* the value 0100 followed by the least significant 60 bits of the |
|
75 |
* SHA-1 hash of the value of the BIT STRING subjectPublicKey. |
|
76 |
* </ol> |
|
77 |
* <p>This method supports method 1. |
|
78 |
* |
|
79 |
* @param pubKey the public key from which to construct this KeyIdentifier |
|
80 |
* @throws IOException on parsing errors |
|
81 |
*/ |
|
82 |
public KeyIdentifier(PublicKey pubKey) |
|
83 |
throws IOException |
|
84 |
{ |
|
85 |
DerValue algAndKey = new DerValue(pubKey.getEncoded()); |
|
86 |
if (algAndKey.tag != DerValue.tag_Sequence) |
|
87 |
throw new IOException("PublicKey value is not a valid " |
|
88 |
+ "X.509 public key"); |
|
89 |
||
90 |
AlgorithmId algid = AlgorithmId.parse(algAndKey.data.getDerValue()); |
|
91 |
byte[] key = algAndKey.data.getUnalignedBitString().toByteArray(); |
|
92 |
||
93 |
MessageDigest md = null; |
|
94 |
try { |
|
95 |
md = MessageDigest.getInstance("SHA1"); |
|
96 |
} catch (NoSuchAlgorithmException e3) { |
|
97 |
throw new IOException("SHA1 not supported"); |
|
98 |
} |
|
99 |
md.update(key); |
|
100 |
this.octetString = md.digest(); |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Return the value of the KeyIdentifier as byte array. |
|
105 |
*/ |
|
106 |
public byte[] getIdentifier() { |
|
107 |
return octetString.clone(); |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Returns a printable representation of the KeyUsage. |
|
112 |
*/ |
|
113 |
public String toString() { |
|
114 |
String s = "KeyIdentifier [\n"; |
|
115 |
||
116 |
HexDumpEncoder encoder = new HexDumpEncoder(); |
|
117 |
s += encoder.encodeBuffer(octetString); |
|
118 |
s += "]\n"; |
|
119 |
return (s); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Write the KeyIdentifier to the DerOutputStream. |
|
124 |
* |
|
125 |
* @param out the DerOutputStream to write the object to. |
|
126 |
* @exception IOException |
|
127 |
*/ |
|
128 |
void encode(DerOutputStream out) throws IOException { |
|
129 |
out.putOctetString(octetString); |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Returns a hash code value for this object. |
|
134 |
* Objects that are equal will also have the same hashcode. |
|
135 |
*/ |
|
136 |
public int hashCode () { |
|
137 |
int retval = 0; |
|
138 |
for (int i = 0; i < octetString.length; i++) |
|
139 |
retval += octetString[i] * i; |
|
140 |
return retval; |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Indicates whether some other object is "equal to" this one. |
|
145 |
*/ |
|
146 |
public boolean equals(Object other) { |
|
147 |
if (this == other) |
|
148 |
return true; |
|
149 |
if (!(other instanceof KeyIdentifier)) |
|
150 |
return false; |
|
35283
c5082624b79f
8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents:
34687
diff
changeset
|
151 |
byte[] otherString = ((KeyIdentifier)other).octetString; |
c5082624b79f
8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents:
34687
diff
changeset
|
152 |
return java.util.Arrays.equals(octetString, otherString); |
2 | 153 |
} |
154 |
} |