author | jjg |
Mon, 15 Aug 2011 11:48:20 -0700 | |
changeset 10336 | 0bb1999251f8 |
parent 6517 | 151856936fd8 |
child 10348 | 7d1a82029332 |
permissions | -rw-r--r-- |
6517 | 1 |
/* |
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
2 |
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
6517 | 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 com.sun.security.sasl.ntlm; |
|
27 |
||
28 |
import com.sun.security.ntlm.NTLMException; |
|
29 |
import com.sun.security.ntlm.Server; |
|
30 |
import java.io.IOException; |
|
31 |
import java.security.GeneralSecurityException; |
|
32 |
import java.util.Map; |
|
33 |
import java.util.Random; |
|
34 |
import javax.security.auth.callback.Callback; |
|
35 |
import javax.security.auth.callback.CallbackHandler; |
|
36 |
import javax.security.auth.callback.NameCallback; |
|
37 |
import javax.security.auth.callback.PasswordCallback; |
|
38 |
import javax.security.auth.callback.UnsupportedCallbackException; |
|
39 |
import javax.security.sasl.*; |
|
40 |
||
41 |
/** |
|
42 |
* Required callbacks: |
|
43 |
* - RealmCallback |
|
44 |
* used as key by handler to fetch password, optional |
|
45 |
* - NameCallback |
|
46 |
* used as key by handler to fetch password |
|
47 |
* - PasswordCallback |
|
48 |
* handler must enter password for username/realm supplied |
|
49 |
* |
|
50 |
* Environment properties that affect the implementation: |
|
51 |
* |
|
52 |
* javax.security.sasl.qop |
|
53 |
* String, quality of protection; only "auth" is accepted, default "auth" |
|
54 |
* |
|
55 |
* com.sun.security.sasl.ntlm.version |
|
56 |
* String, name a specific version to accept: |
|
57 |
* LM/NTLM: Original NTLM v1 |
|
58 |
* LM: Original NTLM v1, LM only |
|
59 |
* NTLM: Original NTLM v1, NTLM only |
|
60 |
* NTLM2: NTLM v1 with Client Challenge |
|
61 |
* LMv2/NTLMv2: NTLM v2 |
|
62 |
* LMv2: NTLM v2, LM only |
|
63 |
* NTLMv2: NTLM v2, NTLM only |
|
64 |
* If not specified, use system property "ntlm.version". If also |
|
65 |
* not specfied, all versions are accepted. |
|
66 |
* |
|
67 |
* com.sun.security.sasl.ntlm.domain |
|
68 |
* String, the domain of the server, default is server name (fqdn parameter) |
|
69 |
* |
|
70 |
* com.sun.security.sasl.ntlm.random |
|
71 |
* java.util.Random, the nonce source. Default null, an internal |
|
72 |
* java.util.Random object will be used |
|
73 |
* |
|
74 |
* Negotiated Properties: |
|
75 |
* |
|
76 |
* javax.security.sasl.qop |
|
77 |
* Always "auth" |
|
78 |
* |
|
79 |
* com.sun.security.sasl.ntlm.hostname |
|
80 |
* The hostname for the user, provided by the client |
|
81 |
* |
|
82 |
*/ |
|
83 |
||
84 |
final class NTLMServer implements SaslServer { |
|
85 |
||
86 |
private final static String NTLM_VERSION = |
|
87 |
"com.sun.security.sasl.ntlm.version"; |
|
88 |
private final static String NTLM_DOMAIN = |
|
89 |
"com.sun.security.sasl.ntlm.domain"; |
|
90 |
private final static String NTLM_HOSTNAME = |
|
91 |
"com.sun.security.sasl.ntlm.hostname"; |
|
92 |
private static final String NTLM_RANDOM = |
|
93 |
"com.sun.security.sasl.ntlm.random"; |
|
94 |
||
95 |
private final Random random; |
|
96 |
private final Server server; |
|
97 |
private byte[] nonce; |
|
98 |
private int step = 0; |
|
99 |
private String authzId; |
|
100 |
private final String mech; |
|
101 |
private String hostname; |
|
102 |
||
103 |
/** |
|
104 |
* @param mech not null |
|
105 |
* @param protocol not null for Sasl, ignored in NTLM |
|
106 |
* @param serverName not null for Sasl, can be null in NTLM. If non-null, |
|
107 |
* might be used as domain if not provided in props |
|
108 |
* @param props can be null |
|
109 |
* @param cbh can be null for Sasl, but will throw NPE in auth for NTLM |
|
110 |
* @throws SaslException |
|
111 |
*/ |
|
112 |
NTLMServer(String mech, String protocol, String serverName, |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
113 |
Map<String, ?> props, final CallbackHandler cbh) |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
114 |
throws SaslException { |
6517 | 115 |
|
116 |
this.mech = mech; |
|
117 |
String version = null; |
|
118 |
String domain = null; |
|
119 |
Random rtmp = null; |
|
120 |
||
121 |
if (props != null) { |
|
122 |
domain = (String) props.get(NTLM_DOMAIN); |
|
123 |
version = (String)props.get(NTLM_VERSION); |
|
124 |
rtmp = (Random)props.get(NTLM_RANDOM); |
|
125 |
} |
|
126 |
random = rtmp != null ? rtmp : new Random(); |
|
127 |
||
128 |
if (version == null) { |
|
129 |
version = System.getProperty("ntlm.version"); |
|
130 |
} |
|
131 |
if (domain == null) { |
|
132 |
domain = serverName; |
|
133 |
} |
|
134 |
if (domain == null) { |
|
135 |
throw new NullPointerException("Domain must be provided as" |
|
136 |
+ " the serverName argument or in props"); |
|
137 |
} |
|
138 |
||
139 |
try { |
|
140 |
server = new Server(version, domain) { |
|
141 |
public char[] getPassword(String ntdomain, String username) { |
|
142 |
try { |
|
143 |
RealmCallback rcb = new RealmCallback( |
|
144 |
"Domain: ", ntdomain); |
|
145 |
NameCallback ncb = new NameCallback( |
|
146 |
"Name: ", username); |
|
147 |
PasswordCallback pcb = new PasswordCallback( |
|
148 |
"Password: ", false); |
|
149 |
cbh.handle(new Callback[] { rcb, ncb, pcb }); |
|
150 |
char[] passwd = pcb.getPassword(); |
|
151 |
pcb.clearPassword(); |
|
152 |
return passwd; |
|
153 |
} catch (IOException ioe) { |
|
154 |
return null; |
|
155 |
} catch (UnsupportedCallbackException uce) { |
|
156 |
return null; |
|
157 |
} |
|
158 |
} |
|
159 |
}; |
|
160 |
} catch (NTLMException ne) { |
|
161 |
throw new SaslException( |
|
162 |
"NTLM: Invalid version string: " + version, ne); |
|
163 |
} |
|
164 |
nonce = new byte[8]; |
|
165 |
} |
|
166 |
||
167 |
@Override |
|
168 |
public String getMechanismName() { |
|
169 |
return mech; |
|
170 |
} |
|
171 |
||
172 |
@Override |
|
173 |
public byte[] evaluateResponse(byte[] response) throws SaslException { |
|
174 |
try { |
|
175 |
step++; |
|
176 |
if (step == 1) { |
|
177 |
random.nextBytes(nonce); |
|
178 |
return server.type2(response, nonce); |
|
179 |
} else { |
|
180 |
String[] out = server.verify(response, nonce); |
|
181 |
authzId = out[0]; |
|
182 |
hostname = out[1]; |
|
183 |
return null; |
|
184 |
} |
|
185 |
} catch (GeneralSecurityException ex) { |
|
186 |
throw new SaslException("", ex); |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
@Override |
|
191 |
public boolean isComplete() { |
|
192 |
return step >= 2; |
|
193 |
} |
|
194 |
||
195 |
@Override |
|
196 |
public String getAuthorizationID() { |
|
197 |
return authzId; |
|
198 |
} |
|
199 |
||
200 |
@Override |
|
201 |
public byte[] unwrap(byte[] incoming, int offset, int len) |
|
202 |
throws SaslException { |
|
203 |
throw new UnsupportedOperationException("Not supported yet."); |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public byte[] wrap(byte[] outgoing, int offset, int len) |
|
208 |
throws SaslException { |
|
209 |
throw new UnsupportedOperationException("Not supported yet."); |
|
210 |
} |
|
211 |
||
212 |
@Override |
|
213 |
public Object getNegotiatedProperty(String propName) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
214 |
switch (propName) { |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
215 |
case Sasl.QOP: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
216 |
return "auth"; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
217 |
case NTLM_HOSTNAME: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
218 |
return hostname; |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
219 |
default: |
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
6517
diff
changeset
|
220 |
return null; |
6517 | 221 |
} |
222 |
} |
|
223 |
||
224 |
@Override |
|
225 |
public void dispose() throws SaslException { |
|
226 |
return; |
|
227 |
} |
|
228 |
} |