author | weijun |
Sat, 09 Feb 2013 16:43:49 +0800 | |
changeset 15649 | f6bd3d34f844 |
parent 10044 | 413c0f8ca341 |
child 18830 | 90956ead732f |
permissions | -rw-r--r-- |
9499 | 1 |
/* |
2 |
* Copyright (c) 2011, 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 javax.security.auth.kerberos; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.util.Objects; |
|
30 |
import sun.security.krb5.EncryptionKey; |
|
10044
413c0f8ca341
7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents:
9499
diff
changeset
|
31 |
import sun.security.krb5.KerberosSecrets; |
9499 | 32 |
import sun.security.krb5.PrincipalName; |
33 |
import sun.security.krb5.RealmException; |
|
34 |
||
35 |
/** |
|
36 |
* This class encapsulates a keytab file. |
|
37 |
* <p> |
|
38 |
* A Kerberos JAAS login module that obtains long term secret keys from a |
|
39 |
* keytab file should use this class. The login module will store |
|
40 |
* an instance of this class in the private credential set of a |
|
41 |
* {@link javax.security.auth.Subject Subject} during the commit phase of the |
|
42 |
* authentication process. |
|
43 |
* <p> |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
44 |
* If a {@code KeyTab} object is obtained from {@link #getUnboundInstance()} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
45 |
* or {@link #getUnboundInstance(java.io.File)}, it is unbound and thus can be |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
46 |
* used by any service principal. Otherwise, if it's obtained from |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
47 |
* {@link #getInstance(KerberosPrincipal)} or |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
48 |
* {@link #getInstance(KerberosPrincipal, java.io.File)}, it is bound to the |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
49 |
* specific service principal and can only be used by it. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
50 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
51 |
* Please note the constructors {@link #getInstance()} and |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
52 |
* {@link #getInstance(java.io.File)} were created when there was no support |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
53 |
* for unbound keytabs. These methods should not be used anymore. An object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
54 |
* created with either of these methods are considered to be bound to an |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
55 |
* unknown principal, which means, its {@link #isBound()} returns true and |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
56 |
* {@link #getPrincipal()} returns null. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
57 |
* <p> |
9499 | 58 |
* It might be necessary for the application to be granted a |
59 |
* {@link javax.security.auth.PrivateCredentialPermission |
|
60 |
* PrivateCredentialPermission} if it needs to access the KeyTab |
|
61 |
* instance from a Subject. This permission is not needed when the |
|
62 |
* application depends on the default JGSS Kerberos mechanism to access the |
|
63 |
* KeyTab. In that case, however, the application will need an appropriate |
|
64 |
* {@link javax.security.auth.kerberos.ServicePermission ServicePermission}. |
|
65 |
* <p> |
|
66 |
* The keytab file format is described at |
|
67 |
* <a href="http://www.ioplex.com/utilities/keytab.txt"> |
|
68 |
* http://www.ioplex.com/utilities/keytab.txt</a>. |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
69 |
* <p> |
9499 | 70 |
* @since 1.7 |
71 |
*/ |
|
72 |
public final class KeyTab { |
|
73 |
||
74 |
/* |
|
75 |
* Impl notes: |
|
76 |
* |
|
77 |
* This class is only a name, a permanent link to the keytab source |
|
78 |
* (can be missing). Itself has no content. In order to read content, |
|
79 |
* take a snapshot and read from it. |
|
80 |
* |
|
81 |
* The snapshot is of type sun.security.krb5.internal.ktab.KeyTab, which |
|
82 |
* contains the content of the keytab file when the snapshot is taken. |
|
83 |
* Itself has no refresh function and mostly an immutable class (except |
|
84 |
* for the create/add/save methods only used by the ktab command). |
|
85 |
*/ |
|
86 |
||
87 |
// Source, null if using the default one. Note that the default name |
|
88 |
// is maintained in snapshot, this field is never "resolved". |
|
89 |
private final File file; |
|
90 |
||
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
91 |
// Bound user: normally from the "principal" value in a JAAS krb5 |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
92 |
// login conf. Will be null if it's "*". |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
93 |
private final KerberosPrincipal princ; |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
94 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
95 |
private final boolean bound; |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
96 |
|
10044
413c0f8ca341
7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents:
9499
diff
changeset
|
97 |
// Set up JavaxSecurityAuthKerberosAccess in KerberosSecrets |
9499 | 98 |
static { |
10044
413c0f8ca341
7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents:
9499
diff
changeset
|
99 |
KerberosSecrets.setJavaxSecurityAuthKerberosAccess( |
9499 | 100 |
new JavaxSecurityAuthKerberosAccessImpl()); |
101 |
} |
|
102 |
||
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
103 |
private KeyTab(KerberosPrincipal princ, File file, boolean bound) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
104 |
this.princ = princ; |
9499 | 105 |
this.file = file; |
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
106 |
this.bound = bound; |
9499 | 107 |
} |
108 |
||
109 |
/** |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
110 |
* Returns a {@code KeyTab} instance from a {@code File} object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
111 |
* that is bound to an unknown service principal. |
9499 | 112 |
* <p> |
113 |
* The result of this method is never null. This method only associates |
|
114 |
* the returned {@code KeyTab} object with the file and does not read it. |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
115 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
116 |
* Developers should call {@link #getInstance(KerberosPrincipal,File)} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
117 |
* when the bound service principal is known. |
9499 | 118 |
* @param file the keytab {@code File} object, must not be null |
119 |
* @return the keytab instance |
|
120 |
* @throws NullPointerException if the {@code file} argument is null |
|
121 |
*/ |
|
122 |
public static KeyTab getInstance(File file) { |
|
123 |
if (file == null) { |
|
124 |
throw new NullPointerException("file must be non null"); |
|
125 |
} |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
126 |
return new KeyTab(null, file, true); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
127 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
128 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
129 |
/** |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
130 |
* Returns an unbound {@code KeyTab} instance from a {@code File} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
131 |
* object. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
132 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
133 |
* The result of this method is never null. This method only associates |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
134 |
* the returned {@code KeyTab} object with the file and does not read it. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
135 |
* @param file the keytab {@code File} object, must not be null |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
136 |
* @return the keytab instance |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
137 |
* @throws NullPointerException if the file argument is null |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
138 |
* @since 1.8 |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
139 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
140 |
public static KeyTab getUnboundInstance(File file) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
141 |
if (file == null) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
142 |
throw new NullPointerException("file must be non null"); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
143 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
144 |
return new KeyTab(null, file, false); |
9499 | 145 |
} |
146 |
||
147 |
/** |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
148 |
* Returns a {@code KeyTab} instance from a {@code File} object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
149 |
* that is bound to the specified service principal. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
150 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
151 |
* The result of this method is never null. This method only associates |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
152 |
* the returned {@code KeyTab} object with the file and does not read it. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
153 |
* @param princ the bound service principal, must not be null |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
154 |
* @param file the keytab {@code File} object, must not be null |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
155 |
* @return the keytab instance |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
156 |
* @throws NullPointerException if either of the arguments is null |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
157 |
* @since 1.8 |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
158 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
159 |
public static KeyTab getInstance(KerberosPrincipal princ, File file) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
160 |
if (princ == null) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
161 |
throw new NullPointerException("princ must be non null"); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
162 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
163 |
if (file == null) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
164 |
throw new NullPointerException("file must be non null"); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
165 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
166 |
return new KeyTab(princ, file, true); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
167 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
168 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
169 |
/** |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
170 |
* Returns the default {@code KeyTab} instance that is bound |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
171 |
* to an unknown service principal. |
9499 | 172 |
* <p> |
173 |
* The result of this method is never null. This method only associates |
|
174 |
* the returned {@code KeyTab} object with the default keytab file and |
|
175 |
* does not read it. |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
176 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
177 |
* Developers should call {@link #getInstance(KerberosPrincipal)} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
178 |
* when the bound service principal is known. |
9499 | 179 |
* @return the default keytab instance. |
180 |
*/ |
|
181 |
public static KeyTab getInstance() { |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
182 |
return new KeyTab(null, null, true); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
183 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
184 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
185 |
/** |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
186 |
* Returns the default unbound {@code KeyTab} instance. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
187 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
188 |
* The result of this method is never null. This method only associates |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
189 |
* the returned {@code KeyTab} object with the default keytab file and |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
190 |
* does not read it. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
191 |
* @return the default keytab instance |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
192 |
* @since 1.8 |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
193 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
194 |
public static KeyTab getUnboundInstance() { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
195 |
return new KeyTab(null, null, false); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
196 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
197 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
198 |
/** |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
199 |
* Returns the default {@code KeyTab} instance that is bound |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
200 |
* to the specified service principal. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
201 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
202 |
* The result of this method is never null. This method only associates |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
203 |
* the returned {@code KeyTab} object with the default keytab file and |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
204 |
* does not read it. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
205 |
* @param princ the bound service principal, must not be null |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
206 |
* @return the default keytab instance |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
207 |
* @throws NullPointerException if {@code princ} is null |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
208 |
* @since 1.8 |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
209 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
210 |
public static KeyTab getInstance(KerberosPrincipal princ) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
211 |
if (princ == null) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
212 |
throw new NullPointerException("princ must be non null"); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
213 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
214 |
return new KeyTab(princ, null, true); |
9499 | 215 |
} |
216 |
||
217 |
//Takes a snapshot of the keytab content |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
218 |
sun.security.krb5.internal.ktab.KeyTab takeSnapshot() { |
9499 | 219 |
return sun.security.krb5.internal.ktab.KeyTab.getInstance(file); |
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* Returns fresh keys for the given Kerberos principal. |
|
224 |
* <p> |
|
225 |
* Implementation of this method should make sure the returned keys match |
|
226 |
* the latest content of the keytab file. The result is a newly created |
|
227 |
* copy that can be modified by the caller without modifying the keytab |
|
228 |
* object. The caller should {@link KerberosKey#destroy() destroy} the |
|
229 |
* result keys after they are used. |
|
230 |
* <p> |
|
231 |
* Please note that the keytab file can be created after the |
|
232 |
* {@code KeyTab} object is instantiated and its content may change over |
|
233 |
* time. Therefore, an application should call this method only when it |
|
234 |
* needs to use the keys. Any previous result from an earlier invocation |
|
235 |
* could potentially be expired. |
|
236 |
* <p> |
|
237 |
* If there is any error (say, I/O error or format error) |
|
238 |
* during the reading process of the KeyTab file, a saved result should be |
|
239 |
* returned. If there is no saved result (say, this is the first time this |
|
240 |
* method is called, or, all previous read attempts failed), an empty array |
|
241 |
* should be returned. This can make sure the result is not drastically |
|
242 |
* changed during the (probably slow) update of the keytab file. |
|
243 |
* <p> |
|
244 |
* Each time this method is called and the reading of the file succeeds |
|
245 |
* with no exception (say, I/O error or file format error), |
|
246 |
* the result should be saved for {@code principal}. The implementation can |
|
247 |
* also save keys for other principals having keys in the same keytab object |
|
248 |
* if convenient. |
|
249 |
* <p> |
|
250 |
* Any unsupported key read from the keytab is ignored and not included |
|
251 |
* in the result. |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
252 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
253 |
* If this keytab is bound to a specific principal, calling this method on |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
254 |
* another principal will return an empty array. |
9499 | 255 |
* |
256 |
* @param principal the Kerberos principal, must not be null. |
|
257 |
* @return the keys (never null, may be empty) |
|
258 |
* @throws NullPointerException if the {@code principal} |
|
259 |
* argument is null |
|
260 |
* @throws SecurityException if a security manager exists and the read |
|
261 |
* access to the keytab file is not permitted |
|
262 |
*/ |
|
263 |
public KerberosKey[] getKeys(KerberosPrincipal principal) { |
|
264 |
try { |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
265 |
if (princ != null && !principal.equals(princ)) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
266 |
return new KerberosKey[0]; |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
267 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
268 |
PrincipalName pn = new PrincipalName(principal.getName()); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
269 |
EncryptionKey[] keys = takeSnapshot().readServiceKeys(pn); |
9499 | 270 |
KerberosKey[] kks = new KerberosKey[keys.length]; |
271 |
for (int i=0; i<kks.length; i++) { |
|
272 |
Integer tmp = keys[i].getKeyVersionNumber(); |
|
273 |
kks[i] = new KerberosKey( |
|
274 |
principal, |
|
275 |
keys[i].getBytes(), |
|
276 |
keys[i].getEType(), |
|
277 |
tmp == null ? 0 : tmp.intValue()); |
|
278 |
keys[i].destroy(); |
|
279 |
} |
|
280 |
return kks; |
|
281 |
} catch (RealmException re) { |
|
282 |
return new KerberosKey[0]; |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
EncryptionKey[] getEncryptionKeys(PrincipalName principal) { |
|
287 |
return takeSnapshot().readServiceKeys(principal); |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Checks if the keytab file exists. Implementation of this method |
|
292 |
* should make sure that the result matches the latest status of the |
|
293 |
* keytab file. |
|
294 |
* <p> |
|
295 |
* The caller can use the result to determine if it should fallback to |
|
296 |
* another mechanism to read the keys. |
|
297 |
* @return true if the keytab file exists; false otherwise. |
|
298 |
* @throws SecurityException if a security manager exists and the read |
|
299 |
* access to the keytab file is not permitted |
|
300 |
*/ |
|
301 |
public boolean exists() { |
|
302 |
return !takeSnapshot().isMissing(); |
|
303 |
} |
|
304 |
||
305 |
public String toString() { |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
306 |
String s = (file == null) ? "Default keytab" : file.toString(); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
307 |
if (!bound) return s; |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
308 |
else if (princ == null) return s + " for someone"; |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
309 |
else return s + " for " + princ; |
9499 | 310 |
} |
311 |
||
312 |
/** |
|
313 |
* Returns a hashcode for this KeyTab. |
|
314 |
* |
|
315 |
* @return a hashCode() for the <code>KeyTab</code> |
|
316 |
*/ |
|
317 |
public int hashCode() { |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
318 |
return Objects.hash(file, princ, bound); |
9499 | 319 |
} |
320 |
||
321 |
/** |
|
322 |
* Compares the specified Object with this KeyTab for equality. |
|
323 |
* Returns true if the given object is also a |
|
324 |
* <code>KeyTab</code> and the two |
|
325 |
* <code>KeyTab</code> instances are equivalent. |
|
326 |
* |
|
327 |
* @param other the Object to compare to |
|
328 |
* @return true if the specified object is equal to this KeyTab |
|
329 |
*/ |
|
330 |
public boolean equals(Object other) { |
|
331 |
if (other == this) |
|
332 |
return true; |
|
333 |
||
334 |
if (! (other instanceof KeyTab)) { |
|
335 |
return false; |
|
336 |
} |
|
337 |
||
338 |
KeyTab otherKtab = (KeyTab) other; |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
339 |
return Objects.equals(otherKtab.princ, princ) && |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
340 |
Objects.equals(otherKtab.file, file) && |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
341 |
bound == otherKtab.bound; |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
342 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
343 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
344 |
/** |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
345 |
* Returns the service principal this {@code KeyTab} object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
346 |
* is bound to. Returns {@code null} if it's not bound. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
347 |
* <p> |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
348 |
* Please note the deprecated constructors create a KeyTab object bound for |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
349 |
* some unknown principal. In this case, this method also returns null. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
350 |
* User can call {@link #isBound()} to verify this case. |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
351 |
* @return the service principal |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
352 |
* @since 1.8 |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
353 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
354 |
public KerberosPrincipal getPrincipal() { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
355 |
return princ; |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
356 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
357 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
358 |
/** |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
359 |
* Returns if the keytab is bound to a principal |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
360 |
* @return if the keytab is bound to a principal |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
361 |
* @since 1.8 |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
362 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
363 |
public boolean isBound() { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
10044
diff
changeset
|
364 |
return bound; |
9499 | 365 |
} |
366 |
} |