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.krb5.internal.rcache;
27
28 import java.util.Objects;
29
30 /**
31 * The class represents a new style replay cache entry. It can be either used
32 * inside memory or in a dfl file.
33 */
34 public class AuthTimeWithHash extends AuthTime
35 implements Comparable<AuthTimeWithHash> {
36
37 final String hash;
38
39 /**
40 * Constructs a new <code>AuthTimeWithHash</code>.
41 */
42 public AuthTimeWithHash(String client, String server,
43 int ctime, int cusec, String hash) {
44 super(client, server, ctime, cusec);
45 this.hash = hash;
46 }
47
48 /**
49 * Compares if an object equals to an <code>AuthTimeWithHash</code> object.
50 * @param o an object.
51 * @return true if two objects are equivalent, otherwise, return false.
52 */
53 @Override
54 public boolean equals(Object o) {
55 if (this == o) return true;
56 if (!(o instanceof AuthTimeWithHash)) return false;
57 AuthTimeWithHash that = (AuthTimeWithHash)o;
58 return Objects.equals(hash, that.hash)
59 && Objects.equals(client, that.client)
60 && Objects.equals(server, that.server)
61 && ctime == that.ctime
62 && cusec == that.cusec;
63 }
64
65 /**
66 * Returns a hash code for this <code>AuthTimeWithHash</code> object.
67 */
68 @Override
69 public int hashCode() {
70 return Objects.hash(hash);
71 }
72
73 @Override
74 public String toString() {
75 return String.format("%d/%06d/%s/%s", ctime, cusec, hash, client);
76 }
77
78 @Override
79 public int compareTo(AuthTimeWithHash other) {
80 int cmp = 0;
81 if (ctime != other.ctime) {
82 cmp = Integer.compare(ctime, other.ctime);
83 } else if (cusec != other.cusec) {
84 cmp = Integer.compare(cusec, other.cusec);
85 } else {
86 cmp = hash.compareTo(other.hash);
87 }
88 return cmp;
89 }
90
91 /**
92 * Compares with a possibly old style object. Used
93 * in DflCache$Storage#loadAndCheck.
94 * @return true if all AuthTime fields are the same
95 */
96 public boolean isSameIgnoresHash(AuthTime old) {
97 return client.equals(old.client) &&
98 server.equals(old.server) &&
99 ctime == old.ctime &&
100 cusec == old.cusec;
101 }
102
103 // Methods used when saved in a dfl file. See DflCache.java
104
105 /**
106 * Encodes to be used in a dfl file
107 * @param withHash write new style if true
108 */
109 @Override
110 public byte[] encode(boolean withHash) {
111 String cstring;
112 String sstring;
113 if (withHash) {
114 cstring = "";
115 sstring = String.format("HASH:%s %d:%s %d:%s", hash,
116 client.length(), client,
117 server.length(), server);
118 } else {
119 cstring = client;
120 sstring = server;
121 }
122 return encode0(cstring, sstring);
123 }
124 }
|
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.krb5.internal.rcache;
27
28 import sun.security.action.GetPropertyAction;
29
30 import java.util.Objects;
31
32 /**
33 * The class represents a new style replay cache entry. It can be either used
34 * inside memory or in a dfl file.
35 */
36 public class AuthTimeWithHash extends AuthTime
37 implements Comparable<AuthTimeWithHash> {
38
39 // The hash algorithm can be "HASH" or "SHA256".
40 public static final String DEFAULT_HASH_ALG;
41
42 static {
43 if (GetPropertyAction.privilegedGetProperty(
44 "jdk.krb5.rcache.usemd5", "false").equals("true")) {
45 DEFAULT_HASH_ALG = "HASH";
46 } else {
47 DEFAULT_HASH_ALG = "SHA256";
48 }
49 }
50
51 public static String realAlg(String alg) {
52 switch (alg) {
53 case "HASH":
54 return "MD5";
55 case "SHA256":
56 return "SHA-256";
57 default:
58 throw new AssertionError(alg + " is not HASH or SHA256");
59 }
60 }
61
62 final String hashAlg;
63 final String hash;
64
65 /**
66 * Constructs a new <code>AuthTimeWithHash</code>.
67 */
68 public AuthTimeWithHash(String client, String server,
69 int ctime, int cusec, String hashAlg, String hash) {
70 super(client, server, ctime, cusec);
71 this.hashAlg = hashAlg;
72 this.hash = hash;
73 }
74
75 /**
76 * Compares if an object equals to an <code>AuthTimeWithHash</code> object.
77 * @param o an object.
78 * @return true if two objects are equivalent, otherwise, return false.
79 */
80 @Override
81 public boolean equals(Object o) {
82 if (this == o) return true;
83 if (!(o instanceof AuthTimeWithHash)) return false;
84 AuthTimeWithHash that = (AuthTimeWithHash)o;
85 return Objects.equals(hash, that.hash)
86 && Objects.equals(hashAlg, that.hashAlg)
87 && Objects.equals(client, that.client)
88 && Objects.equals(server, that.server)
89 && ctime == that.ctime
90 && cusec == that.cusec;
91 }
92
93 /**
94 * Returns a hash code for this <code>AuthTimeWithHash</code> object.
95 */
96 @Override
97 public int hashCode() {
98 return Objects.hash(hash);
99 }
100
101 @Override
102 public String toString() {
103 return String.format("%d/%06d/%s/%s", ctime, cusec, hash, client);
104 }
105
106 @Override
107 public int compareTo(AuthTimeWithHash other) {
108 int cmp = 0;
109 if (ctime != other.ctime) {
110 cmp = Integer.compare(ctime, other.ctime);
111 } else if (cusec != other.cusec) {
112 cmp = Integer.compare(cusec, other.cusec);
113 } else {
114 cmp = hash.compareTo(other.hash);
115 }
116 return cmp;
117 }
118
119 /**
120 * Compares with a possibly old style object. Used
121 * in DflCache$Storage#loadAndCheck.
122 * @return true if all AuthTime fields are the same but different hash
123 */
124 public boolean sameTimeDiffHash(AuthTimeWithHash old) {
125 if (!this.isSameIgnoresHash(old)) {
126 return false;
127 }
128 return this.hashAlg.equals(old.hashAlg) &&
129 !this.hash.equals(old.hash);
130 }
131
132 /**
133 * Compares with a possibly old style object. Used
134 * in DflCache$Storage#loadAndCheck.
135 * @return true if all AuthTime fields are the same
136 */
137 public boolean isSameIgnoresHash(AuthTime old) {
138 return client.equals(old.client) &&
139 server.equals(old.server) &&
140 ctime == old.ctime &&
141 cusec == old.cusec;
142 }
143
144 // Methods used when saved in a dfl file. See DflCache.java
145
146 /**
147 * Encodes to be used in a dfl file
148 * @param withHash write new style if true
149 */
150 @Override
151 public byte[] encode(boolean withHash) {
152 String cstring;
153 String sstring;
154 if (withHash) {
155 cstring = "";
156 sstring = String.format("%s:%s %d:%s %d:%s", hashAlg, hash,
157 client.length(), client,
158 server.length(), server);
159 } else {
160 cstring = client;
161 sstring = server;
162 }
163 return encode0(cstring, sstring);
164 }
165 }
|