|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.client.swing; |
|
27 |
|
28 import java.security.*; |
|
29 import java.security.cert.*; |
|
30 import java.security.cert.Certificate; |
|
31 import javax.swing.JEditorPane; |
|
32 import com.oracle.solaris.vp.util.misc.TextUtil; |
|
33 import com.oracle.solaris.vp.util.misc.finder.Finder; |
|
34 |
|
35 @SuppressWarnings({"serial"}) |
|
36 public class CertificateEditorPane extends JEditorPane { |
|
37 // |
|
38 // Instance data |
|
39 // |
|
40 |
|
41 private Certificate certificate; |
|
42 |
|
43 // |
|
44 // Constructors |
|
45 // |
|
46 |
|
47 public CertificateEditorPane() { |
|
48 setContentType("text/html"); |
|
49 setEditable(false); |
|
50 } |
|
51 |
|
52 public CertificateEditorPane(Certificate certificate) { |
|
53 this(); |
|
54 setCertificate(certificate); |
|
55 } |
|
56 |
|
57 // |
|
58 // JTextComponent methods |
|
59 // |
|
60 |
|
61 @Override |
|
62 public void setText(String text) { |
|
63 super.setText(text); |
|
64 |
|
65 // Scroll to top |
|
66 setCaretPosition(0); |
|
67 } |
|
68 |
|
69 // |
|
70 // CertificateEditorPane methods |
|
71 // |
|
72 |
|
73 public Certificate getCertificate() { |
|
74 return certificate; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Sets the {@code Certificate} to display in this {@code |
|
79 * ExpandableCertificatePanel}. |
|
80 * |
|
81 * @param certificate |
|
82 * the {@code Certificate} object to display in this {@code |
|
83 * ExpandableCertificatePanel} |
|
84 */ |
|
85 public void setCertificate(Certificate certificate) { |
|
86 this.certificate = certificate; |
|
87 |
|
88 String html; |
|
89 if (certificate instanceof X509Certificate) { |
|
90 X509Certificate x509 = (X509Certificate)certificate; |
|
91 |
|
92 Object[] args = { |
|
93 TextUtil.escapeHTMLChars(x509.getSubjectDN().toString()), |
|
94 TextUtil.escapeHTMLChars(x509.getIssuerDN().toString()), |
|
95 TextUtil.escapeHTMLChars(x509.getSerialNumber().toString(16)), |
|
96 x509.getNotBefore(), |
|
97 x509.getNotAfter(), |
|
98 TextUtil.escapeHTMLChars(getCertFingerPrint("MD5", x509)), |
|
99 TextUtil.escapeHTMLChars(getCertFingerPrint("SHA1", x509)), |
|
100 TextUtil.escapeHTMLChars(x509.getSigAlgName()), |
|
101 (Integer)x509.getVersion() |
|
102 }; |
|
103 |
|
104 html = Finder.getString("certificate.view.x509", args); |
|
105 } else { |
|
106 html = Finder.getString("certificate.view.unknown", |
|
107 TextUtil.escapeHTMLChars(certificate.getType()), |
|
108 TextUtil.escapeHTMLChars(certificate.toString())); |
|
109 } |
|
110 |
|
111 setText(html); |
|
112 } |
|
113 |
|
114 // |
|
115 // Private methods |
|
116 // |
|
117 |
|
118 /** |
|
119 * Gets the requested fingerprint of the certificate. |
|
120 */ |
|
121 private String getCertFingerPrint(String mdAlg, Certificate cert) { |
|
122 try { |
|
123 byte[] encCertInfo = cert.getEncoded(); |
|
124 MessageDigest md = MessageDigest.getInstance(mdAlg); |
|
125 byte[] digest = md.digest(encCertInfo); |
|
126 |
|
127 char[] hexChars = { |
|
128 '0', '1', '2', '3', '4', '5', '6', '7', '8', |
|
129 '9', 'A', 'B', 'C', 'D', 'E', 'F' |
|
130 }; |
|
131 |
|
132 StringBuilder buffer = new StringBuilder(); |
|
133 for (int i = 0; i < digest.length; i++) { |
|
134 if (i != 0) { |
|
135 buffer.append(':'); |
|
136 } |
|
137 int high = (digest[i] & 0xf0) >> 4; |
|
138 int low = digest[i] & 0x0f; |
|
139 buffer.append(hexChars[high]).append(hexChars[low]); |
|
140 } |
|
141 |
|
142 return buffer.toString(); |
|
143 } catch (CertificateEncodingException e) { |
|
144 return e.getMessage(); |
|
145 |
|
146 } catch (NoSuchAlgorithmException e) { |
|
147 return e.getMessage(); |
|
148 } |
|
149 } |
|
150 } |