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