32032
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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.io.OutputStream;
|
|
30 |
import java.util.Enumeration;
|
|
31 |
import java.security.SecureRandom;
|
|
32 |
|
|
33 |
import sun.security.x509.AttributeNameEnumeration;
|
|
34 |
import sun.security.x509.CertAttrSet;
|
|
35 |
import sun.security.x509.Extension;
|
|
36 |
import sun.security.x509.PKIXExtensions;
|
|
37 |
import sun.security.util.*;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Represent the OCSP Nonce Extension.
|
|
41 |
* This extension, if present, provides a nonce value in OCSP requests
|
|
42 |
* and responses. This will cryptographically bind requests and responses
|
|
43 |
* and help to prevent replay attacks (see RFC 6960, section 4.4.1).
|
|
44 |
*
|
|
45 |
* @see Extension
|
|
46 |
* @see CertAttrSet
|
|
47 |
*/
|
|
48 |
public class OCSPNonceExtension extends Extension
|
|
49 |
implements CertAttrSet<String> {
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Attribute name.
|
|
53 |
*/
|
|
54 |
public static final String NAME = "OCSPNonce";
|
|
55 |
public static final String NONCE = "nonce";
|
|
56 |
|
|
57 |
private byte[] nonceData = null;
|
|
58 |
private String extensionName;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Encode this extension value to DER and assign it to the
|
|
62 |
* {@code extensionName} data member.
|
|
63 |
*
|
|
64 |
* @throws IOException if any errors occur during DER encoding
|
|
65 |
*/
|
|
66 |
private void encodeInternal() throws IOException {
|
|
67 |
if (nonceData == null) {
|
|
68 |
this.extensionValue = null;
|
|
69 |
return;
|
|
70 |
}
|
|
71 |
DerOutputStream os = new DerOutputStream();
|
|
72 |
os.putOctetString(this.nonceData);
|
|
73 |
this.extensionValue = os.toByteArray();
|
|
74 |
}
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Create a {@code OCSPNonceExtension} by providing the nonce length.
|
|
78 |
* The criticality is set to false. The random bytes will be generated
|
|
79 |
* using the SUN provider.
|
|
80 |
*
|
|
81 |
* @param length the number of random bytes composing the nonce
|
|
82 |
*
|
|
83 |
* @throws IOException if any errors happen during encoding of the
|
|
84 |
* extension.
|
|
85 |
*/
|
|
86 |
public OCSPNonceExtension(int length) throws IOException {
|
|
87 |
this(PKIXExtensions.OCSPNonce_Id, false, length, NAME);
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Creates the extension (also called by the subclass).
|
|
92 |
*
|
|
93 |
* @param extensionId the {@code ObjectIdentifier} for the OCSP Nonce
|
|
94 |
* extension
|
|
95 |
* @param isCritical a boolean flag indicating if the criticality bit
|
|
96 |
* is to be set for this extension
|
|
97 |
* @param length the length of the nonce in bytes
|
|
98 |
* @param extensionName the name of the extension
|
|
99 |
*
|
|
100 |
* @throws IOException if any errors happen during encoding of the
|
|
101 |
* extension.
|
|
102 |
*/
|
|
103 |
protected OCSPNonceExtension(ObjectIdentifier extensionId,
|
|
104 |
boolean isCritical, int length, String extensionName)
|
|
105 |
throws IOException {
|
|
106 |
SecureRandom rng = new SecureRandom();
|
|
107 |
this.nonceData = new byte[length];
|
|
108 |
rng.nextBytes(nonceData);
|
|
109 |
this.extensionId = extensionId;
|
|
110 |
this.critical = isCritical;
|
|
111 |
this.extensionName = extensionName;
|
|
112 |
encodeInternal();
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Create the extension using the provided criticality bit setting and
|
|
117 |
* DER encoding.
|
|
118 |
*
|
|
119 |
* @param critical true if the extension is to be treated as critical.
|
|
120 |
* @param value an array of DER encoded bytes of the extnValue for the
|
|
121 |
* extension. It must not include the encapsulating OCTET STRING
|
|
122 |
* tag and length. For an {@code OCSPNonceExtension} the data value
|
|
123 |
* should be a simple OCTET STRING containing random bytes
|
|
124 |
* (see RFC 6960, section 4.4.1).
|
|
125 |
*
|
|
126 |
* @throws ClassCastException if value is not an array of bytes
|
|
127 |
* @throws IOException if any errors happen during encoding of the
|
|
128 |
* extension
|
|
129 |
*/
|
|
130 |
public OCSPNonceExtension(Boolean critical, Object value)
|
|
131 |
throws IOException {
|
|
132 |
this(PKIXExtensions.OCSPNonce_Id, critical, value, NAME);
|
|
133 |
}
|
|
134 |
|
|
135 |
/**
|
|
136 |
* Creates the extension (also called by the subclass).
|
|
137 |
*
|
|
138 |
* @param extensionId the {@code ObjectIdentifier} for the OCSP Nonce
|
|
139 |
* extension
|
|
140 |
* @param critical a boolean flag indicating if the criticality bit
|
|
141 |
* is to be set for this extension
|
|
142 |
* @param value an array of DER encoded bytes of the extnValue for the
|
|
143 |
* extension. It must not include the encapsulating OCTET STRING
|
|
144 |
* tag and length. For an {@code OCSPNonceExtension} the data value
|
|
145 |
* should be a simple OCTET STRING containing random bytes
|
|
146 |
* (see RFC 6960, section 4.4.1).
|
|
147 |
* @param extensionName the name of the extension
|
|
148 |
*
|
|
149 |
* @throws ClassCastException if value is not an array of bytes
|
|
150 |
* @throws IOException if any errors happen during encoding of the
|
|
151 |
* extension
|
|
152 |
*/
|
|
153 |
protected OCSPNonceExtension(ObjectIdentifier extensionId,
|
|
154 |
Boolean critical, Object value, String extensionName)
|
|
155 |
throws IOException {
|
|
156 |
this.extensionId = extensionId;
|
|
157 |
this.critical = critical;
|
|
158 |
this.extensionValue = (byte[]) value;
|
|
159 |
DerValue val = new DerValue(this.extensionValue);
|
|
160 |
this.nonceData = val.getOctetString();
|
|
161 |
this.extensionName = extensionName;
|
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Set the attribute value.
|
|
166 |
*
|
|
167 |
* @param name the name of the attribute.
|
|
168 |
* @param obj an array of nonce bytes for the extension. It must not
|
|
169 |
* contain any DER tags or length.
|
|
170 |
*
|
|
171 |
* @throws IOException if an unsupported name is provided or the supplied
|
|
172 |
* {@code obj} is not a byte array
|
|
173 |
*/
|
|
174 |
@Override
|
|
175 |
public void set(String name, Object obj) throws IOException {
|
|
176 |
if (name.equalsIgnoreCase(NONCE)) {
|
|
177 |
if (!(obj instanceof byte[])) {
|
|
178 |
throw new IOException("Attribute must be of type byte[].");
|
|
179 |
}
|
|
180 |
nonceData = (byte[])obj;
|
|
181 |
} else {
|
|
182 |
throw new IOException("Attribute name not recognized by"
|
|
183 |
+ " CertAttrSet:" + extensionName + ".");
|
|
184 |
}
|
|
185 |
encodeInternal();
|
|
186 |
}
|
|
187 |
|
|
188 |
/**
|
|
189 |
* Get the attribute value.
|
|
190 |
*
|
|
191 |
* @param name the name of the attribute to retrieve. Only "OCSPNonce"
|
|
192 |
* is currently supported.
|
|
193 |
*
|
|
194 |
* @return an array of bytes that are the nonce data. It will not contain
|
|
195 |
* any DER tags or length, only the random nonce bytes.
|
|
196 |
*
|
|
197 |
* @throws IOException if an unsupported name is provided.
|
|
198 |
*/
|
|
199 |
@Override
|
|
200 |
public Object get(String name) throws IOException {
|
|
201 |
if (name.equalsIgnoreCase(NONCE)) {
|
|
202 |
return nonceData;
|
|
203 |
} else {
|
|
204 |
throw new IOException("Attribute name not recognized by"
|
|
205 |
+ " CertAttrSet:" + extensionName + ".");
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Delete the attribute value.
|
|
211 |
*
|
|
212 |
* @param name the name of the attribute to retrieve. Only "OCSPNonce"
|
|
213 |
* is currently supported.
|
|
214 |
*
|
|
215 |
* @throws IOException if an unsupported name is provided or an error
|
|
216 |
* occurs during re-encoding of the extension.
|
|
217 |
*/
|
|
218 |
@Override
|
|
219 |
public void delete(String name) throws IOException {
|
|
220 |
if (name.equalsIgnoreCase(NONCE)) {
|
|
221 |
nonceData = null;
|
|
222 |
} else {
|
|
223 |
throw new IOException("Attribute name not recognized by"
|
|
224 |
+ " CertAttrSet:" + extensionName + ".");
|
|
225 |
}
|
|
226 |
encodeInternal();
|
|
227 |
}
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Returns a printable representation of the {@code OCSPNonceExtension}.
|
|
231 |
*/
|
|
232 |
@Override
|
|
233 |
public String toString() {
|
|
234 |
String s = super.toString() + extensionName + ": " +
|
|
235 |
((nonceData == null) ? "" : Debug.toString(nonceData))
|
|
236 |
+ "\n";
|
|
237 |
return (s);
|
|
238 |
}
|
|
239 |
|
|
240 |
/**
|
|
241 |
* Write the extension to an {@code OutputStream}
|
|
242 |
*
|
|
243 |
* @param out the {@code OutputStream} to write the extension to.
|
|
244 |
*
|
|
245 |
* @throws IOException on encoding errors.
|
|
246 |
*/
|
|
247 |
@Override
|
|
248 |
public void encode(OutputStream out) throws IOException {
|
|
249 |
encode(out, PKIXExtensions.OCSPNonce_Id, this.critical);
|
|
250 |
}
|
|
251 |
|
|
252 |
/**
|
|
253 |
* Write the extension to the DerOutputStream.
|
|
254 |
*
|
|
255 |
* @param out the {@code OutputStream} to write the extension to.
|
|
256 |
* @param extensionId the {@code ObjectIdentifier} used for this extension
|
|
257 |
* @param isCritical a flag indicating if the criticality bit is set for
|
|
258 |
* this extension.
|
|
259 |
*
|
|
260 |
* @throws IOException on encoding errors.
|
|
261 |
*/
|
|
262 |
protected void encode(OutputStream out, ObjectIdentifier extensionId,
|
|
263 |
boolean isCritical) throws IOException {
|
|
264 |
|
|
265 |
DerOutputStream tmp = new DerOutputStream();
|
|
266 |
|
|
267 |
if (this.extensionValue == null) {
|
|
268 |
this.extensionId = extensionId;
|
|
269 |
this.critical = isCritical;
|
|
270 |
encodeInternal();
|
|
271 |
}
|
|
272 |
super.encode(tmp);
|
|
273 |
out.write(tmp.toByteArray());
|
|
274 |
}
|
|
275 |
|
|
276 |
/**
|
|
277 |
* Return an enumeration of names of attributes existing within this
|
|
278 |
* attribute.
|
|
279 |
*/
|
|
280 |
@Override
|
|
281 |
public Enumeration<String> getElements() {
|
|
282 |
AttributeNameEnumeration elements = new AttributeNameEnumeration();
|
|
283 |
elements.addElement(NONCE);
|
|
284 |
return (elements.elements());
|
|
285 |
}
|
|
286 |
|
|
287 |
/**
|
|
288 |
* Return the name of this attribute.
|
|
289 |
*/
|
|
290 |
@Override
|
|
291 |
public String getName() {
|
|
292 |
return (extensionName);
|
|
293 |
}
|
|
294 |
}
|