author | weijun |
Sat, 06 Jul 2019 08:11:19 +0800 | |
changeset 55613 | a55b46a208d1 |
parent 51398 | 3c389a284345 |
child 57487 | 643978a35f6e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55613
a55b46a208d1
8227305: Krb5Util::getTicketFromSubjectAndTgs is useless
weijun
parents:
51398
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, 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.jgss.krb5; |
|
27 |
||
28 |
import javax.security.auth.kerberos.KerberosTicket; |
|
29 |
import javax.security.auth.kerberos.KerberosPrincipal; |
|
9499 | 30 |
import javax.security.auth.kerberos.KeyTab; |
2 | 31 |
import javax.security.auth.Subject; |
32 |
import javax.security.auth.login.LoginException; |
|
33 |
import java.security.AccessControlContext; |
|
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
34 |
|
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
35 |
import sun.security.action.GetBooleanAction; |
2 | 36 |
import sun.security.jgss.GSSUtil; |
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
37 |
import sun.security.jgss.GSSCaller; |
2 | 38 |
|
39 |
import sun.security.krb5.Credentials; |
|
40 |
import sun.security.krb5.EncryptionKey; |
|
41 |
import sun.security.krb5.KrbException; |
|
42 |
import java.io.IOException; |
|
10044
413c0f8ca341
7048466: Move sun.misc.JavaxSecurityAuthKerberosAccess to sun.security.krb5 package
weijun
parents:
9499
diff
changeset
|
43 |
import sun.security.krb5.KerberosSecrets; |
9499 | 44 |
import sun.security.krb5.PrincipalName; |
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
45 |
|
2 | 46 |
/** |
47 |
* Utilities for obtaining and converting Kerberos tickets. |
|
48 |
*/ |
|
49 |
public class Krb5Util { |
|
50 |
||
51398
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
51 |
static final boolean DEBUG = GetBooleanAction |
3c389a284345
8209416: Refactoring GetPropertyAction calls in security libs
weijun
parents:
47216
diff
changeset
|
52 |
.privilegedGetProperty("sun.security.krb5.debug"); |
2 | 53 |
|
54 |
/** |
|
55 |
* Default constructor |
|
56 |
*/ |
|
57 |
private Krb5Util() { // Cannot create one of these |
|
58 |
} |
|
59 |
||
60 |
/** |
|
61 |
* Retrieves the ticket corresponding to the client/server principal |
|
62 |
* pair from the Subject in the specified AccessControlContext. |
|
63 |
* If the ticket can not be found in the Subject, and if |
|
64 |
* useSubjectCredsOnly is false, then obtain ticket from |
|
65 |
* a LoginContext. |
|
66 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
67 |
static KerberosTicket getTicket(GSSCaller caller, |
2 | 68 |
String clientPrincipal, String serverPrincipal, |
69 |
AccessControlContext acc) throws LoginException { |
|
70 |
||
71 |
// Try to get ticket from acc's Subject |
|
72 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 73 |
KerberosTicket ticket = |
2 | 74 |
SubjectComber.find(accSubj, serverPrincipal, clientPrincipal, |
75 |
KerberosTicket.class); |
|
76 |
||
77 |
// Try to get ticket from Subject obtained from GSSUtil |
|
78 |
if (ticket == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
79 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
9499 | 80 |
ticket = SubjectComber.find(subject, |
2 | 81 |
serverPrincipal, clientPrincipal, KerberosTicket.class); |
82 |
} |
|
83 |
return ticket; |
|
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Retrieves the caller's Subject, or Subject obtained by logging in |
|
88 |
* via the specified caller. |
|
89 |
* |
|
90 |
* Caller must have permission to: |
|
91 |
* - access the Subject |
|
92 |
* - create LoginContext |
|
93 |
* - read the auth.login.defaultCallbackHandler security property |
|
94 |
* |
|
95 |
* NOTE: This method is used by JSSE Kerberos Cipher Suites |
|
96 |
*/ |
|
2942
37d9baeb7518
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents:
2
diff
changeset
|
97 |
public static Subject getSubject(GSSCaller caller, |
2 | 98 |
AccessControlContext acc) throws LoginException { |
99 |
||
100 |
// Try to get the Subject from acc |
|
101 |
Subject subject = Subject.getSubject(acc); |
|
102 |
||
103 |
// Try to get Subject obtained from GSSUtil |
|
104 |
if (subject == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
|
105 |
subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
106 |
} |
|
107 |
return subject; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
9499 | 111 |
* Retrieves the ServiceCreds for the specified server principal from |
112 |
* the Subject in the specified AccessControlContext. If not found, and if |
|
113 |
* useSubjectCredsOnly is false, then obtain from a LoginContext. |
|
114 |
* |
|
115 |
* NOTE: This method is also used by JSSE Kerberos Cipher Suites |
|
116 |
*/ |
|
117 |
public static ServiceCreds getServiceCreds(GSSCaller caller, |
|
2 | 118 |
String serverPrincipal, AccessControlContext acc) |
119 |
throws LoginException { |
|
120 |
||
121 |
Subject accSubj = Subject.getSubject(acc); |
|
9499 | 122 |
ServiceCreds sc = null; |
123 |
if (accSubj != null) { |
|
124 |
sc = ServiceCreds.getInstance(accSubj, serverPrincipal); |
|
2 | 125 |
} |
9499 | 126 |
if (sc == null && !GSSUtil.useSubjectCredsOnly(caller)) { |
127 |
Subject subject = GSSUtil.login(caller, GSSUtil.GSS_KRB5_MECH_OID); |
|
128 |
sc = ServiceCreds.getInstance(subject, serverPrincipal); |
|
2 | 129 |
} |
9499 | 130 |
return sc; |
2 | 131 |
} |
132 |
||
133 |
public static KerberosTicket credsToTicket(Credentials serviceCreds) { |
|
134 |
EncryptionKey sessionKey = serviceCreds.getSessionKey(); |
|
135 |
return new KerberosTicket( |
|
136 |
serviceCreds.getEncoded(), |
|
137 |
new KerberosPrincipal(serviceCreds.getClient().getName()), |
|
138 |
new KerberosPrincipal(serviceCreds.getServer().getName(), |
|
139 |
KerberosPrincipal.KRB_NT_SRV_INST), |
|
140 |
sessionKey.getBytes(), |
|
141 |
sessionKey.getEType(), |
|
142 |
serviceCreds.getFlags(), |
|
143 |
serviceCreds.getAuthTime(), |
|
144 |
serviceCreds.getStartTime(), |
|
145 |
serviceCreds.getEndTime(), |
|
146 |
serviceCreds.getRenewTill(), |
|
147 |
serviceCreds.getClientAddresses()); |
|
148 |
}; |
|
149 |
||
150 |
public static Credentials ticketToCreds(KerberosTicket kerbTicket) |
|
14413
e954df027393
6355584: Introduce constrained Kerberos delegation
weijun
parents:
10336
diff
changeset
|
151 |
throws KrbException, IOException { |
2 | 152 |
return new Credentials( |
153 |
kerbTicket.getEncoded(), |
|
154 |
kerbTicket.getClient().getName(), |
|
155 |
kerbTicket.getServer().getName(), |
|
156 |
kerbTicket.getSessionKey().getEncoded(), |
|
157 |
kerbTicket.getSessionKeyType(), |
|
158 |
kerbTicket.getFlags(), |
|
159 |
kerbTicket.getAuthTime(), |
|
160 |
kerbTicket.getStartTime(), |
|
161 |
kerbTicket.getEndTime(), |
|
162 |
kerbTicket.getRenewTill(), |
|
163 |
kerbTicket.getClientAddresses()); |
|
164 |
} |
|
9499 | 165 |
|
166 |
/** |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
167 |
* A helper method to get a sun..KeyTab from a javax..KeyTab |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
168 |
* @param ktab the javax..KeyTab object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
169 |
* @return the sun..KeyTab object |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
170 |
*/ |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
171 |
public static sun.security.krb5.internal.ktab.KeyTab |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
172 |
snapshotFromJavaxKeyTab(KeyTab ktab) { |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
173 |
return KerberosSecrets.getJavaxSecurityAuthKerberosAccess() |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
174 |
.keyTabTakeSnapshot(ktab); |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
175 |
} |
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
176 |
|
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
177 |
/** |
9499 | 178 |
* A helper method to get EncryptionKeys from a javax..KeyTab |
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
179 |
* @param ktab the javax..KeyTab object |
9499 | 180 |
* @param cname the PrincipalName |
181 |
* @return the EKeys, never null, might be empty |
|
182 |
*/ |
|
183 |
public static EncryptionKey[] keysFromJavaxKeyTab( |
|
184 |
KeyTab ktab, PrincipalName cname) { |
|
15649
f6bd3d34f844
8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents:
15006
diff
changeset
|
185 |
return snapshotFromJavaxKeyTab(ktab).readServiceKeys(cname); |
9499 | 186 |
} |
2 | 187 |
} |