author | igerasim |
Tue, 02 Oct 2018 10:19:07 -0700 | |
changeset 51986 | c1db377f6300 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.x509; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
||
30 |
import sun.security.util.*; |
|
31 |
||
32 |
/** |
|
33 |
* Represent the GeneralSubtree ASN.1 object, whose syntax is: |
|
34 |
* <pre> |
|
35 |
* GeneralSubtree ::= SEQUENCE { |
|
36 |
* base GeneralName, |
|
37 |
* minimum [0] BaseDistance DEFAULT 0, |
|
38 |
* maximum [1] BaseDistance OPTIONAL |
|
39 |
* } |
|
40 |
* BaseDistance ::= INTEGER (0..MAX) |
|
41 |
* </pre> |
|
42 |
* @author Amit Kapoor |
|
43 |
* @author Hemma Prafullchandra |
|
44 |
*/ |
|
45 |
public class GeneralSubtree { |
|
46 |
private static final byte TAG_MIN = 0; |
|
47 |
private static final byte TAG_MAX = 1; |
|
48 |
private static final int MIN_DEFAULT = 0; |
|
49 |
||
50 |
private GeneralName name; |
|
51 |
private int minimum = MIN_DEFAULT; |
|
52 |
private int maximum = -1; |
|
53 |
||
54 |
private int myhash = -1; |
|
55 |
||
56 |
/** |
|
57 |
* The default constructor for the class. |
|
58 |
* |
|
30374 | 59 |
* @param name the GeneralName |
60 |
* @param min the minimum BaseDistance |
|
61 |
* @param max the maximum BaseDistance |
|
2 | 62 |
*/ |
63 |
public GeneralSubtree(GeneralName name, int min, int max) { |
|
64 |
this.name = name; |
|
65 |
this.minimum = min; |
|
66 |
this.maximum = max; |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Create the object from its DER encoded form. |
|
71 |
* |
|
72 |
* @param val the DER encoded from of the same. |
|
73 |
*/ |
|
74 |
public GeneralSubtree(DerValue val) throws IOException { |
|
75 |
if (val.tag != DerValue.tag_Sequence) { |
|
76 |
throw new IOException("Invalid encoding for GeneralSubtree."); |
|
77 |
} |
|
78 |
name = new GeneralName(val.data.getDerValue(), true); |
|
79 |
||
80 |
// NB. this is always encoded with the IMPLICIT tag |
|
81 |
// The checks only make sense if we assume implicit tagging, |
|
82 |
// with explicit tagging the form is always constructed. |
|
83 |
while (val.data.available() != 0) { |
|
84 |
DerValue opt = val.data.getDerValue(); |
|
85 |
||
86 |
if (opt.isContextSpecific(TAG_MIN) && !opt.isConstructed()) { |
|
87 |
opt.resetTag(DerValue.tag_Integer); |
|
88 |
minimum = opt.getInteger(); |
|
89 |
||
90 |
} else if (opt.isContextSpecific(TAG_MAX) && !opt.isConstructed()) { |
|
91 |
opt.resetTag(DerValue.tag_Integer); |
|
92 |
maximum = opt.getInteger(); |
|
93 |
} else |
|
94 |
throw new IOException("Invalid encoding of GeneralSubtree."); |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
/** |
|
99 |
* Return the GeneralName. |
|
100 |
* |
|
101 |
* @return the GeneralName |
|
102 |
*/ |
|
103 |
public GeneralName getName() { |
|
104 |
//XXXX May want to consider cloning this |
|
105 |
return name; |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Return the minimum BaseDistance. |
|
110 |
* |
|
111 |
* @return the minimum BaseDistance. Default is 0 if not set. |
|
112 |
*/ |
|
113 |
public int getMinimum() { |
|
114 |
return minimum; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Return the maximum BaseDistance. |
|
119 |
* |
|
120 |
* @return the maximum BaseDistance, or -1 if not set. |
|
121 |
*/ |
|
122 |
public int getMaximum() { |
|
123 |
return maximum; |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Return a printable string of the GeneralSubtree. |
|
128 |
*/ |
|
129 |
public String toString() { |
|
30649
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
130 |
StringBuilder sb = new StringBuilder(); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
131 |
sb.append("\n GeneralSubtree: [") |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
132 |
.append("\n GeneralName: "); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
133 |
if (name != null) { |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
134 |
sb.append(name); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
135 |
} |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
136 |
sb.append("\n Minimum: ") |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
137 |
.append(minimum) |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
138 |
.append("\n Maximum: "); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
139 |
if (maximum == -1) { |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
140 |
sb.append("undefined"); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
141 |
} else { |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
142 |
sb.append(maximum); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
143 |
} |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
144 |
sb.append(" ]\n"); |
e7cc8f48f616
8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents:
30374
diff
changeset
|
145 |
return sb.toString(); |
2 | 146 |
} |
147 |
||
148 |
/** |
|
149 |
* Compare this GeneralSubtree with another |
|
150 |
* |
|
151 |
* @param other GeneralSubtree to compare to this |
|
30374 | 152 |
* @return true if match |
2 | 153 |
*/ |
154 |
public boolean equals(Object other) { |
|
155 |
if (!(other instanceof GeneralSubtree)) |
|
156 |
return false; |
|
157 |
GeneralSubtree otherGS = (GeneralSubtree)other; |
|
158 |
if (this.name == null) { |
|
159 |
if (otherGS.name != null) { |
|
160 |
return false; |
|
161 |
} |
|
162 |
} else { |
|
163 |
if (!((this.name).equals(otherGS.name))) |
|
164 |
return false; |
|
165 |
} |
|
166 |
if (this.minimum != otherGS.minimum) |
|
167 |
return false; |
|
168 |
if (this.maximum != otherGS.maximum) |
|
169 |
return false; |
|
170 |
return true; |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Returns the hash code for this GeneralSubtree. |
|
175 |
* |
|
176 |
* @return a hash code value. |
|
177 |
*/ |
|
178 |
public int hashCode() { |
|
179 |
if (myhash == -1) { |
|
180 |
myhash = 17; |
|
181 |
if (name != null) { |
|
182 |
myhash = 37 * myhash + name.hashCode(); |
|
183 |
} |
|
184 |
if (minimum != MIN_DEFAULT) { |
|
185 |
myhash = 37 * myhash + minimum; |
|
186 |
} |
|
187 |
if (maximum != -1) { |
|
188 |
myhash = 37 * myhash + maximum; |
|
189 |
} |
|
190 |
} |
|
191 |
return myhash; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Encode the GeneralSubtree. |
|
196 |
* |
|
30374 | 197 |
* @param out the DerOutputStream to encode this object to. |
2 | 198 |
*/ |
199 |
public void encode(DerOutputStream out) throws IOException { |
|
200 |
DerOutputStream seq = new DerOutputStream(); |
|
201 |
||
202 |
name.encode(seq); |
|
203 |
||
204 |
if (minimum != MIN_DEFAULT) { |
|
205 |
DerOutputStream tmp = new DerOutputStream(); |
|
206 |
tmp.putInteger(minimum); |
|
207 |
seq.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
208 |
false, TAG_MIN), tmp); |
|
209 |
} |
|
210 |
if (maximum != -1) { |
|
211 |
DerOutputStream tmp = new DerOutputStream(); |
|
212 |
tmp.putInteger(maximum); |
|
213 |
seq.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, |
|
214 |
false, TAG_MAX), tmp); |
|
215 |
} |
|
216 |
out.write(DerValue.tag_Sequence, seq); |
|
217 |
} |
|
218 |
} |