2
|
1 |
/*
|
|
2 |
* Copyright 2007 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.x509;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.OutputStream;
|
|
30 |
import java.util.Date;
|
|
31 |
import java.util.Enumeration;
|
|
32 |
|
|
33 |
import sun.security.util.*;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* From RFC 3280:
|
|
37 |
* <p>
|
|
38 |
* The invalidity date is a non-critical CRL entry extension that
|
|
39 |
* provides the date on which it is known or suspected that the private
|
|
40 |
* key was compromised or that the certificate otherwise became invalid.
|
|
41 |
* This date may be earlier than the revocation date in the CRL entry,
|
|
42 |
* which is the date at which the CA processed the revocation. When a
|
|
43 |
* revocation is first posted by a CRL issuer in a CRL, the invalidity
|
|
44 |
* date may precede the date of issue of earlier CRLs, but the
|
|
45 |
* revocation date SHOULD NOT precede the date of issue of earlier CRLs.
|
|
46 |
* Whenever this information is available, CRL issuers are strongly
|
|
47 |
* encouraged to share it with CRL users.
|
|
48 |
* <p>
|
|
49 |
* The GeneralizedTime values included in this field MUST be expressed
|
|
50 |
* in Greenwich Mean Time (Zulu), and MUST be specified and interpreted
|
|
51 |
* as defined in section 4.1.2.5.2.
|
|
52 |
* <pre>
|
|
53 |
* id-ce-invalidityDate OBJECT IDENTIFIER ::= { id-ce 24 }
|
|
54 |
*
|
|
55 |
* invalidityDate ::= GeneralizedTime
|
|
56 |
* </pre>
|
|
57 |
*
|
|
58 |
* @author Sean Mullan
|
|
59 |
*/
|
|
60 |
public class InvalidityDateExtension extends Extension
|
|
61 |
implements CertAttrSet<String> {
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Attribute name and Reason codes
|
|
65 |
*/
|
|
66 |
public static final String NAME = "InvalidityDate";
|
|
67 |
public static final String DATE = "date";
|
|
68 |
|
|
69 |
private Date date;
|
|
70 |
|
|
71 |
private void encodeThis() throws IOException {
|
|
72 |
if (date == null) {
|
|
73 |
this.extensionValue = null;
|
|
74 |
return;
|
|
75 |
}
|
|
76 |
DerOutputStream dos = new DerOutputStream();
|
|
77 |
dos.putGeneralizedTime(date);
|
|
78 |
this.extensionValue = dos.toByteArray();
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Create a InvalidityDateExtension with the passed in date.
|
|
83 |
* Criticality automatically set to false.
|
|
84 |
*
|
|
85 |
* @param date the invalidity date
|
|
86 |
*/
|
|
87 |
public InvalidityDateExtension(Date date) throws IOException {
|
|
88 |
this(false, date);
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Create a InvalidityDateExtension with the passed in date.
|
|
93 |
*
|
|
94 |
* @param critical true if the extension is to be treated as critical.
|
|
95 |
* @param date the invalidity date
|
|
96 |
*/
|
|
97 |
public InvalidityDateExtension(boolean critical, Date date)
|
|
98 |
throws IOException {
|
|
99 |
this.extensionId = PKIXExtensions.InvalidityDate_Id;
|
|
100 |
this.critical = critical;
|
|
101 |
this.date = date;
|
|
102 |
encodeThis();
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Create the extension from the passed DER encoded value of the same.
|
|
107 |
*
|
|
108 |
* @param critical true if the extension is to be treated as critical.
|
|
109 |
* @param value an array of DER encoded bytes of the actual value.
|
|
110 |
* @exception ClassCastException if value is not an array of bytes
|
|
111 |
* @exception IOException on error.
|
|
112 |
*/
|
|
113 |
public InvalidityDateExtension(Boolean critical, Object value)
|
|
114 |
throws IOException {
|
|
115 |
this.extensionId = PKIXExtensions.InvalidityDate_Id;
|
|
116 |
this.critical = critical.booleanValue();
|
|
117 |
this.extensionValue = (byte[]) value;
|
|
118 |
DerValue val = new DerValue(this.extensionValue);
|
|
119 |
this.date = val.getGeneralizedTime();
|
|
120 |
}
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Set the attribute value.
|
|
124 |
*/
|
|
125 |
public void set(String name, Object obj) throws IOException {
|
|
126 |
if (!(obj instanceof Date)) {
|
|
127 |
throw new IOException("Attribute must be of type Date.");
|
|
128 |
}
|
|
129 |
if (name.equalsIgnoreCase(DATE)) {
|
|
130 |
date = (Date) obj;
|
|
131 |
} else {
|
|
132 |
throw new IOException
|
|
133 |
("Name not supported by InvalidityDateExtension");
|
|
134 |
}
|
|
135 |
encodeThis();
|
|
136 |
}
|
|
137 |
|
|
138 |
/**
|
|
139 |
* Get the attribute value.
|
|
140 |
*/
|
|
141 |
public Object get(String name) throws IOException {
|
|
142 |
if (name.equalsIgnoreCase(DATE)) {
|
|
143 |
return date;
|
|
144 |
} else {
|
|
145 |
throw new IOException
|
|
146 |
("Name not supported by InvalidityDateExtension");
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Delete the attribute value.
|
|
152 |
*/
|
|
153 |
public void delete(String name) throws IOException {
|
|
154 |
if (name.equalsIgnoreCase(DATE)) {
|
|
155 |
date = null;
|
|
156 |
} else {
|
|
157 |
throw new IOException
|
|
158 |
("Name not supported by InvalidityDateExtension");
|
|
159 |
}
|
|
160 |
encodeThis();
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns a printable representation of the Invalidity Date.
|
|
165 |
*/
|
|
166 |
public String toString() {
|
|
167 |
return super.toString() + " Invalidity Date: " + String.valueOf(date);
|
|
168 |
}
|
|
169 |
|
|
170 |
/**
|
|
171 |
* Write the extension to the DerOutputStream.
|
|
172 |
*
|
|
173 |
* @param out the DerOutputStream to write the extension to
|
|
174 |
* @exception IOException on encoding errors
|
|
175 |
*/
|
|
176 |
public void encode(OutputStream out) throws IOException {
|
|
177 |
DerOutputStream tmp = new DerOutputStream();
|
|
178 |
|
|
179 |
if (this.extensionValue == null) {
|
|
180 |
this.extensionId = PKIXExtensions.InvalidityDate_Id;
|
|
181 |
this.critical = false;
|
|
182 |
encodeThis();
|
|
183 |
}
|
|
184 |
super.encode(tmp);
|
|
185 |
out.write(tmp.toByteArray());
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Return an enumeration of names of attributes existing within this
|
|
190 |
* attribute.
|
|
191 |
*/
|
|
192 |
public Enumeration<String> getElements() {
|
|
193 |
AttributeNameEnumeration elements = new AttributeNameEnumeration();
|
|
194 |
elements.addElement(DATE);
|
|
195 |
|
|
196 |
return elements.elements();
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* Return the name of this attribute.
|
|
201 |
*/
|
|
202 |
public String getName() {
|
|
203 |
return NAME;
|
|
204 |
}
|
|
205 |
|
|
206 |
public static InvalidityDateExtension toImpl(java.security.cert.Extension ext)
|
|
207 |
throws IOException {
|
|
208 |
if (ext instanceof InvalidityDateExtension) {
|
|
209 |
return (InvalidityDateExtension) ext;
|
|
210 |
} else {
|
|
211 |
return new InvalidityDateExtension
|
|
212 |
(Boolean.valueOf(ext.isCritical()), ext.getValue());
|
|
213 |
}
|
|
214 |
}
|
|
215 |
}
|