rev 15878 : 8168518: rcache interop with krb5-1.15
1 /* 2 * Copyright (c) 2013, 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.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 } --- EOF ---