History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/security/provider/certpath/CertId.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/security/provider/certpath/CertId.java+226−0
@@ -0,0 +1,226 @@
1+/*
2+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
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
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
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+ *
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.
24+ */
25+
26+package sun.security.provider.certpath;
27+
28+import java.io.IOException;
29+import java.math.BigInteger;
30+import java.security.MessageDigest;
31+import java.security.NoSuchAlgorithmException;
32+import java.security.cert.X509Certificate;
33+import java.util.Arrays;
34+import sun.misc.HexDumpEncoder;
35+import sun.security.x509.*;
36+import sun.security.util.*;
37+
38+/**
39+ * This class corresponds to the CertId field in OCSP Request
40+ * and the OCSP Response. The ASN.1 definition for CertID is defined
41+ * in RFC 2560 as:
42+ * <pre>
43+ *
44+ * CertID ::= SEQUENCE {
45+ * hashAlgorithm AlgorithmIdentifier,
46+ * issuerNameHash OCTET STRING, -- Hash of Issuer's DN
47+ * issuerKeyHash OCTET STRING, -- Hash of Issuers public key
48+ * serialNumber CertificateSerialNumber
49+ * }
50+ *
51+ * </pre>
52+ *
53+ * @author Ram Marti
54+ */
55+
56+public class CertId {
57+
58+ private static final boolean debug = false;
59+ private static final AlgorithmId SHA1_ALGID
60+ = new AlgorithmId(AlgorithmId.SHA_oid);
61+ private final AlgorithmId hashAlgId;
62+ private final byte[] issuerNameHash;
63+ private final byte[] issuerKeyHash;
64+ private final SerialNumber certSerialNumber;
65+ private int myhash = -1; // hashcode for this CertId
66+
67+ /**
68+ * Creates a CertId. The hash algorithm used is SHA-1.
69+ */
70+ public CertId(X509Certificate issuerCert, SerialNumber serialNumber)
71+ throws IOException {
72+
73+ // compute issuerNameHash
74+ MessageDigest md = null;
75+ try {
76+ md = MessageDigest.getInstance("SHA1");
77+ } catch (NoSuchAlgorithmException nsae) {
78+ throw new IOException("Unable to create CertId", nsae);
79+ }
80+ hashAlgId = SHA1_ALGID;
81+ md.update(issuerCert.getSubjectX500Principal().getEncoded());
82+ issuerNameHash = md.digest();
83+
84+ // compute issuerKeyHash (remove the tag and length)
85+ byte[] pubKey = issuerCert.getPublicKey().getEncoded();
86+ DerValue val = new DerValue(pubKey);
87+ DerValue[] seq = new DerValue[2];
88+ seq[0] = val.data.getDerValue(); // AlgorithmID
89+ seq[1] = val.data.getDerValue(); // Key
90+ byte[] keyBytes = seq[1].getBitString();
91+ md.update(keyBytes);
92+ issuerKeyHash = md.digest();
93+ certSerialNumber = serialNumber;
94+
95+ if (debug) {
96+ HexDumpEncoder encoder = new HexDumpEncoder();
97+ System.out.println("Issuer Certificate is " + issuerCert);
98+ System.out.println("issuerNameHash is " +
99+ encoder.encodeBuffer(issuerNameHash));
100+ System.out.println("issuerKeyHash is " +
101+ encoder.encodeBuffer(issuerKeyHash));
102+ System.out.println("SerialNumber is " + serialNumber.getNumber());
103+ }
104+ }
105+
106+ /**
107+ * Creates a CertId from its ASN.1 DER encoding.
108+ */
109+ public CertId(DerInputStream derIn) throws IOException {
110+ hashAlgId = AlgorithmId.parse(derIn.getDerValue());
111+ issuerNameHash = derIn.getOctetString();
112+ issuerKeyHash = derIn.getOctetString();
113+ certSerialNumber = new SerialNumber(derIn);
114+ }
115+
116+ /**
117+ * Return the hash algorithm identifier.
118+ */
119+ public AlgorithmId getHashAlgorithm() {
120+ return hashAlgId;
121+ }
122+
123+ /**
124+ * Return the hash value for the issuer name.
125+ */
126+ public byte[] getIssuerNameHash() {
127+ return issuerNameHash;
128+ }
129+
130+ /**
131+ * Return the hash value for the issuer key.
132+ */
133+ public byte[] getIssuerKeyHash() {
134+ return issuerKeyHash;
135+ }
136+
137+ /**
138+ * Return the serial number.
139+ */
140+ public BigInteger getSerialNumber() {
141+ return certSerialNumber.getNumber();
142+ }
143+
144+ /**
145+ * Encode the CertId using ASN.1 DER.
146+ * The hash algorithm used is SHA-1.
147+ */
148+ public void encode(DerOutputStream out) throws IOException {
149+
150+ DerOutputStream tmp = new DerOutputStream();
151+ hashAlgId.encode(tmp);
152+ tmp.putOctetString(issuerNameHash);
153+ tmp.putOctetString(issuerKeyHash);
154+ certSerialNumber.encode(tmp);
155+ out.write(DerValue.tag_Sequence, tmp);
156+
157+ if (debug) {
158+ HexDumpEncoder encoder = new HexDumpEncoder();
159+ System.out.println("Encoded certId is " +
160+ encoder.encode(out.toByteArray()));
161+ }
162+ }
163+
164+ /**
165+ * Returns a hashcode value for this CertId.
166+ *
167+ * @return the hashcode value.
168+ */
169+ @Override public int hashCode() {
170+ if (myhash == -1) {
171+ myhash = hashAlgId.hashCode();
172+ for (int i = 0; i < issuerNameHash.length; i++) {
173+ myhash += issuerNameHash[i] * i;
174+ }
175+ for (int i = 0; i < issuerKeyHash.length; i++) {
176+ myhash += issuerKeyHash[i] * i;
177+ }
178+ myhash += certSerialNumber.getNumber().hashCode();
179+ }
180+ return myhash;
181+ }
182+
183+ /**
184+ * Compares this CertId for equality with the specified
185+ * object. Two CertId objects are considered equal if their hash algorithms,
186+ * their issuer name and issuer key hash values and their serial numbers
187+ * are equal.
188+ *
189+ * @param other the object to test for equality with this object.
190+ * @return true if the objects are considered equal, false otherwise.
191+ */
192+ @Override public boolean equals(Object other) {
193+ if (this == other) {
194+ return true;
195+ }
196+ if (other == null || (!(other instanceof CertId))) {
197+ return false;
198+ }
199+
200+ CertId that = (CertId) other;
201+ if (hashAlgId.equals(that.getHashAlgorithm()) &&
202+ Arrays.equals(issuerNameHash, that.getIssuerNameHash()) &&
203+ Arrays.equals(issuerKeyHash, that.getIssuerKeyHash()) &&
204+ certSerialNumber.getNumber().equals(that.getSerialNumber())) {
205+ return true;
206+ } else {
207+ return false;
208+ }
209+ }
210+
211+ /**
212+ * Create a string representation of the CertId.
213+ */
214+ @Override public String toString() {
215+ StringBuilder sb = new StringBuilder();
216+ sb.append("CertId \n");
217+ sb.append("Algorithm: " + hashAlgId.toString() +"\n");
218+ sb.append("issuerNameHash \n");
219+ HexDumpEncoder encoder = new HexDumpEncoder();
220+ sb.append(encoder.encode(issuerNameHash));
221+ sb.append("\nissuerKeyHash: \n");
222+ sb.append(encoder.encode(issuerKeyHash));
223+ sb.append("\n" + certSerialNumber.toString());
224+ return sb.toString();
225+ }
226+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t