author | igerasim |
Tue, 10 May 2016 17:16:55 +0300 | |
changeset 37880 | 60ec48925dc6 |
parent 32473 | 09672cd2a4a0 |
permissions | -rw-r--r-- |
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; |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
29 |
import java.util.Objects; |
32032 | 30 |
import java.security.SecureRandom; |
31 |
||
32 |
import sun.security.x509.Extension; |
|
33 |
import sun.security.x509.PKIXExtensions; |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
34 |
import sun.security.util.Debug; |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
35 |
import sun.security.util.DerValue; |
32032 | 36 |
|
37 |
/** |
|
38 |
* Represent the OCSP Nonce Extension. |
|
39 |
* This extension, if present, provides a nonce value in OCSP requests |
|
40 |
* and responses. This will cryptographically bind requests and responses |
|
41 |
* and help to prevent replay attacks (see RFC 6960, section 4.4.1). |
|
42 |
* |
|
43 |
* @see Extension |
|
44 |
*/ |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
45 |
public final class OCSPNonceExtension extends Extension { |
32032 | 46 |
|
47 |
/** |
|
48 |
* Attribute name. |
|
49 |
*/ |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
50 |
private static final String EXTENSION_NAME = "OCSPNonce"; |
32032 | 51 |
private byte[] nonceData = null; |
52 |
||
53 |
/** |
|
37880 | 54 |
* Create an {@code OCSPNonceExtension} by providing the nonce length. |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
55 |
* The criticality is set to false, and the OID for the extension will |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
56 |
* be the value defined by "id-pkix-ocsp-nonce" from RFC 6960. |
32032 | 57 |
* |
58 |
* @param length the number of random bytes composing the nonce |
|
59 |
* |
|
60 |
* @throws IOException if any errors happen during encoding of the |
|
61 |
* extension. |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
62 |
* @throws IllegalArgumentException if length is not a positive integer. |
32032 | 63 |
*/ |
64 |
public OCSPNonceExtension(int length) throws IOException { |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
65 |
this(false, length); |
32032 | 66 |
} |
67 |
||
68 |
/** |
|
37880 | 69 |
* Create an {@code OCSPNonceExtension} by providing the nonce length and |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
70 |
* criticality setting. The OID for the extension will |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
71 |
* be the value defined by "id-pkix-ocsp-nonce" from RFC 6960. |
32032 | 72 |
* |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
73 |
* @param isCritical a boolean flag indicating whether the criticality bit |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
74 |
* is set for this extension |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
75 |
* @param length the number of random bytes composing the nonce |
32032 | 76 |
* |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
77 |
* @throws IOException if any errors happen during encoding of the |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
78 |
* extension. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
79 |
* @throws IllegalArgumentException if length is not a positive integer. |
32032 | 80 |
*/ |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
81 |
public OCSPNonceExtension(boolean isCritical, int length) |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
82 |
throws IOException { |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
83 |
this.extensionId = PKIXExtensions.OCSPNonce_Id; |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
84 |
this.critical = isCritical; |
32032 | 85 |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
86 |
if (length > 0) { |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
87 |
SecureRandom rng = new SecureRandom(); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
88 |
this.nonceData = new byte[length]; |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
89 |
rng.nextBytes(nonceData); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
90 |
this.extensionValue = new DerValue(DerValue.tag_OctetString, |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
91 |
nonceData).toByteArray(); |
32032 | 92 |
} else { |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
93 |
throw new IllegalArgumentException( |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
94 |
"Length must be a positive integer"); |
32032 | 95 |
} |
96 |
} |
|
97 |
||
98 |
/** |
|
37880 | 99 |
* Create an {@code OCSPNonceExtension} by providing a nonce value. |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
100 |
* The criticality is set to false, and the OID for the extension will |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
101 |
* be the value defined by "id-pkix-ocsp-nonce" from RFC 6960. |
32032 | 102 |
* |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
103 |
* @param incomingNonce The nonce data to be set for the extension. This |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
104 |
* must be a non-null array of at least one byte long. |
32032 | 105 |
* |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
106 |
* @throws IOException if any errors happen during encoding of the |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
107 |
* extension. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
108 |
* @throws IllegalArgumentException if the incomingNonce length is not a |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
109 |
* positive integer. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
110 |
* @throws NullPointerException if the incomingNonce is null. |
32032 | 111 |
*/ |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
112 |
public OCSPNonceExtension(byte[] incomingNonce) throws IOException { |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
113 |
this(false, incomingNonce); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
114 |
} |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
115 |
|
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
116 |
/** |
37880 | 117 |
* Create an {@code OCSPNonceExtension} by providing a nonce value and |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
118 |
* criticality setting. The OID for the extension will |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
119 |
* be the value defined by "id-pkix-ocsp-nonce" from RFC 6960. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
120 |
* |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
121 |
* @param isCritical a boolean flag indicating whether the criticality bit |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
122 |
* is set for this extension |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
123 |
* @param incomingNonce The nonce data to be set for the extension. This |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
124 |
* must be a non-null array of at least one byte long. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
125 |
* |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
126 |
* @throws IOException if any errors happen during encoding of the |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
127 |
* extension. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
128 |
* @throws IllegalArgumentException if the incomingNonce length is not a |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
129 |
* positive integer. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
130 |
* @throws NullPointerException if the incomingNonce is null. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
131 |
*/ |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
132 |
public OCSPNonceExtension(boolean isCritical, byte[] incomingNonce) |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
133 |
throws IOException { |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
134 |
this.extensionId = PKIXExtensions.OCSPNonce_Id; |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
135 |
this.critical = isCritical; |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
136 |
|
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
137 |
Objects.requireNonNull(incomingNonce, "Nonce data must be non-null"); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
138 |
if (incomingNonce.length > 0) { |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
139 |
this.nonceData = incomingNonce.clone(); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
140 |
this.extensionValue = new DerValue(DerValue.tag_OctetString, |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
141 |
nonceData).toByteArray(); |
32032 | 142 |
} else { |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
143 |
throw new IllegalArgumentException( |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
144 |
"Nonce data must be at least 1 byte in length"); |
32032 | 145 |
} |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
146 |
} |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
147 |
|
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
148 |
/** |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
149 |
* Return the nonce bytes themselves, without any DER encoding. |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
150 |
* |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
151 |
* @return A copy of the underlying nonce bytes |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
152 |
*/ |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
153 |
public byte[] getNonceValue() { |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
154 |
return nonceData.clone(); |
32032 | 155 |
} |
156 |
||
157 |
/** |
|
158 |
* Returns a printable representation of the {@code OCSPNonceExtension}. |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
159 |
* |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
160 |
* @return a string representation of the extension. |
32032 | 161 |
*/ |
162 |
@Override |
|
163 |
public String toString() { |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
164 |
StringBuilder sb = new StringBuilder(); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
165 |
sb.append(super.toString()).append(EXTENSION_NAME).append(": "); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
166 |
sb.append((nonceData == null) ? "" : Debug.toString(nonceData)); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
167 |
sb.append("\n"); |
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
168 |
return sb.toString(); |
32032 | 169 |
} |
170 |
||
171 |
/** |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
172 |
* Return the name of the extension as a {@code String} |
32032 | 173 |
* |
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
174 |
* @return the name of the extension |
32032 | 175 |
*/ |
176 |
public String getName() { |
|
32473
09672cd2a4a0
8134364: Add defensive copies to get/set methods for OCSPNonceExtension
jnimeh
parents:
32032
diff
changeset
|
177 |
return EXTENSION_NAME; |
32032 | 178 |
} |
179 |
} |