author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 86 | a9644392a939 |
child 910 | 1f53246fb014 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2002-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
#include <jni.h> |
|
27 |
#include <windows.h> |
|
28 |
#include <rpc.h> |
|
29 |
#include <winsock.h> |
|
30 |
#include <lm.h> |
|
31 |
||
32 |
#include <stdio.h> |
|
33 |
#include <stdarg.h> |
|
34 |
#include <stdlib.h> |
|
35 |
#include <string.h> |
|
36 |
#include <tchar.h> |
|
37 |
#include <fcntl.h> |
|
38 |
||
86
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
39 |
#include "jni_util.h" |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
40 |
|
2 | 41 |
#define SECURITY_WIN32 |
42 |
#include "sspi.h" |
|
43 |
#include "issperr.h" |
|
44 |
||
45 |
||
46 |
/* |
|
47 |
* OS calls loaded from DLL on intialization |
|
48 |
*/ |
|
49 |
||
50 |
static FREE_CREDENTIALS_HANDLE_FN pFreeCredentialsHandle; |
|
51 |
static ACQUIRE_CREDENTIALS_HANDLE_FN pAcquireCredentialsHandle; |
|
52 |
static FREE_CONTEXT_BUFFER_FN pFreeContextBuffer; |
|
53 |
static INITIALIZE_SECURITY_CONTEXT_FN pInitializeSecurityContext; |
|
54 |
static COMPLETE_AUTH_TOKEN_FN pCompleteAuthToken; |
|
55 |
static DELETE_SECURITY_CONTEXT_FN pDeleteSecurityContext; |
|
56 |
||
72
f24a98c3df49
6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents:
2
diff
changeset
|
57 |
static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle); |
2 | 58 |
|
59 |
static jfieldID ntlm_ctxHandleID; |
|
60 |
static jfieldID ntlm_crdHandleID; |
|
61 |
||
62 |
static HINSTANCE lib = NULL; |
|
63 |
||
64 |
JNIEXPORT void JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_initFirst |
|
65 |
(JNIEnv *env, jclass clazz) |
|
66 |
{ |
|
67 |
OSVERSIONINFO version; |
|
68 |
UCHAR libName[MAX_PATH]; |
|
69 |
||
70 |
ntlm_ctxHandleID = (*env)->GetFieldID(env, clazz, "ctxHandle", "J"); |
|
71 |
ntlm_crdHandleID = (*env)->GetFieldID(env, clazz, "crdHandle", "J"); |
|
72 |
||
73 |
version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); |
|
74 |
GetVersionEx (&version); |
|
75 |
||
76 |
if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) { |
|
77 |
strcpy (libName, "security.dll" ); |
|
78 |
} |
|
79 |
else if (version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) { |
|
80 |
strcpy (libName, "secur32.dll" ); |
|
81 |
} |
|
82 |
||
83 |
lib = LoadLibrary (libName); |
|
84 |
||
85 |
pFreeCredentialsHandle |
|
86 |
= (FREE_CREDENTIALS_HANDLE_FN) GetProcAddress( |
|
87 |
lib, "FreeCredentialsHandle" ); |
|
88 |
||
89 |
pAcquireCredentialsHandle |
|
90 |
= (ACQUIRE_CREDENTIALS_HANDLE_FN) GetProcAddress( |
|
91 |
lib, "AcquireCredentialsHandleA" ); |
|
92 |
||
93 |
pFreeContextBuffer |
|
94 |
= (FREE_CONTEXT_BUFFER_FN) GetProcAddress( |
|
95 |
lib, "FreeContextBuffer" ); |
|
96 |
||
97 |
pInitializeSecurityContext |
|
98 |
= (INITIALIZE_SECURITY_CONTEXT_FN) GetProcAddress( |
|
99 |
lib, "InitializeSecurityContextA" ); |
|
100 |
||
101 |
pCompleteAuthToken |
|
102 |
= (COMPLETE_AUTH_TOKEN_FN) GetProcAddress( |
|
103 |
lib, "CompleteAuthToken" ); |
|
104 |
||
105 |
pDeleteSecurityContext |
|
106 |
= (DELETE_SECURITY_CONTEXT_FN) GetProcAddress( |
|
107 |
lib, "DeleteSecurityContext" ); |
|
108 |
||
109 |
} |
|
110 |
||
111 |
/* |
|
112 |
* Class: sun_net_www_protocol_http_NTLMAuthSequence |
|
113 |
* Method: getCredentialsHandle |
|
114 |
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J |
|
115 |
*/ |
|
116 |
||
117 |
JNIEXPORT jlong JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_getCredentialsHandle |
|
118 |
(JNIEnv *env, jobject this, jstring user, jstring domain, jstring password) |
|
119 |
{ |
|
120 |
SEC_WINNT_AUTH_IDENTITY AuthId; |
|
121 |
SEC_WINNT_AUTH_IDENTITY * pAuthId; |
|
86
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
122 |
const CHAR *pUser = 0; |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
123 |
const CHAR *pDomain = 0; |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
124 |
const CHAR *pPassword = 0; |
2 | 125 |
CredHandle *pCred; |
126 |
TimeStamp ltime; |
|
127 |
jboolean isCopy; |
|
128 |
SECURITY_STATUS ss; |
|
129 |
||
130 |
if (user != 0) { |
|
86
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
131 |
pUser = JNU_GetStringPlatformChars(env, user, &isCopy); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
132 |
if (pUser == NULL) |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
133 |
return 0; // pending Exception |
2 | 134 |
} |
135 |
if (domain != 0) { |
|
86
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
136 |
pDomain = JNU_GetStringPlatformChars(env, domain, &isCopy); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
137 |
if (pDomain == NULL) { |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
138 |
if (pUser != NULL) |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
139 |
JNU_ReleaseStringPlatformChars(env, user, pUser); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
140 |
return 0; // pending Exception |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
141 |
} |
2 | 142 |
} |
143 |
if (password != 0) { |
|
86
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
144 |
pPassword = JNU_GetStringPlatformChars(env, password, &isCopy); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
145 |
if (pPassword == NULL) { |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
146 |
if(pUser != NULL) |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
147 |
JNU_ReleaseStringPlatformChars(env, user, pUser); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
148 |
if(pDomain != NULL) |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
149 |
JNU_ReleaseStringPlatformChars(env, domain, pDomain); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
150 |
return 0; // pending Exception |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
151 |
} |
2 | 152 |
} |
153 |
pCred = (CredHandle *)malloc(sizeof (CredHandle)); |
|
154 |
||
155 |
if ( ((pUser != NULL) || (pPassword != NULL)) || (pDomain != NULL)) { |
|
156 |
pAuthId = &AuthId; |
|
157 |
||
158 |
memset( &AuthId, 0, sizeof( AuthId )); |
|
159 |
||
160 |
if ( pUser != NULL ) { |
|
161 |
AuthId.User = (unsigned char *) pUser; |
|
162 |
AuthId.UserLength = strlen( pUser ); |
|
163 |
} |
|
164 |
||
165 |
if ( pPassword != NULL ) { |
|
166 |
AuthId.Password = (unsigned char *) pPassword; |
|
167 |
AuthId.PasswordLength = strlen( pPassword ); |
|
168 |
} |
|
169 |
||
170 |
if ( pDomain != NULL ) { |
|
171 |
AuthId.Domain = (unsigned char *) pDomain; |
|
172 |
AuthId.DomainLength = strlen( pDomain ); |
|
173 |
} |
|
174 |
||
175 |
AuthId.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI; |
|
176 |
} else { |
|
177 |
pAuthId = NULL; |
|
178 |
} |
|
179 |
||
180 |
ss = pAcquireCredentialsHandle( |
|
181 |
NULL, "NTLM", SECPKG_CRED_OUTBOUND, |
|
182 |
NULL, pAuthId, NULL, NULL, |
|
183 |
pCred, <ime |
|
184 |
); |
|
185 |
||
86
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
186 |
/* Release resources held by JNU_GetStringPlatformChars */ |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
187 |
if (pUser != NULL) |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
188 |
JNU_ReleaseStringPlatformChars(env, user, pUser); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
189 |
if (pPassword != NULL) |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
190 |
JNU_ReleaseStringPlatformChars(env, password, pPassword); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
191 |
if (pDomain != NULL) |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
192 |
JNU_ReleaseStringPlatformChars(env, domain, pDomain); |
a9644392a939
6628661: NTLM-authentication doesn't work with non-ASCII letters
chegar
parents:
72
diff
changeset
|
193 |
|
2 | 194 |
if (ss == 0) { |
195 |
return (jlong) pCred; |
|
196 |
} else { |
|
197 |
return 0; |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
JNIEXPORT jbyteArray JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_getNextToken |
|
202 |
(JNIEnv *env, jobject this, jlong crdHandle, jbyteArray lastToken) |
|
203 |
{ |
|
204 |
||
205 |
VOID *pInput = 0; |
|
206 |
DWORD inputLen; |
|
207 |
CHAR buffOut[512]; |
|
208 |
jboolean isCopy; |
|
209 |
SECURITY_STATUS ss; |
|
210 |
SecBufferDesc OutBuffDesc; |
|
211 |
SecBuffer OutSecBuff; |
|
212 |
SecBufferDesc InBuffDesc; |
|
213 |
SecBuffer InSecBuff; |
|
214 |
ULONG ContextAttributes; |
|
215 |
CredHandle *pCred = (CredHandle *)crdHandle; |
|
216 |
CtxtHandle *pCtx; |
|
217 |
CtxtHandle *newContext; |
|
218 |
TimeStamp ltime; |
|
219 |
jbyteArray result; |
|
220 |
||
221 |
||
222 |
pCtx = (CtxtHandle *) (*env)->GetLongField (env, this, ntlm_ctxHandleID); |
|
223 |
if (pCtx == 0) { /* first call */ |
|
224 |
newContext = (CtxtHandle *)malloc(sizeof(CtxtHandle)); |
|
225 |
(*env)->SetLongField (env, this, ntlm_ctxHandleID, (jlong)newContext); |
|
226 |
} else { |
|
227 |
newContext = pCtx; |
|
228 |
} |
|
229 |
||
230 |
OutBuffDesc.ulVersion = 0; |
|
231 |
OutBuffDesc.cBuffers = 1; |
|
232 |
OutBuffDesc.pBuffers = &OutSecBuff; |
|
233 |
||
234 |
OutSecBuff.cbBuffer = 512; |
|
235 |
OutSecBuff.BufferType = SECBUFFER_TOKEN; |
|
236 |
OutSecBuff.pvBuffer = buffOut; |
|
237 |
||
238 |
/* |
|
239 |
* Prepare our Input buffer - Note the server is expecting the client's |
|
240 |
* negotiation packet on the first call |
|
241 |
*/ |
|
242 |
||
243 |
if (lastToken != 0) |
|
244 |
{ |
|
245 |
pInput = (VOID *)(*env)->GetByteArrayElements(env, lastToken, &isCopy); |
|
246 |
inputLen = (*env)->GetArrayLength(env, lastToken); |
|
247 |
||
248 |
InBuffDesc.ulVersion = 0; |
|
249 |
InBuffDesc.cBuffers = 1; |
|
250 |
InBuffDesc.pBuffers = &InSecBuff; |
|
251 |
||
252 |
InSecBuff.cbBuffer = inputLen; |
|
253 |
InSecBuff.BufferType = SECBUFFER_TOKEN; |
|
254 |
InSecBuff.pvBuffer = pInput; |
|
255 |
} |
|
256 |
||
257 |
/* |
|
258 |
* will return success when its done but we still |
|
259 |
* need to send the out buffer if there are bytes to send |
|
260 |
*/ |
|
261 |
||
262 |
ss = pInitializeSecurityContext( |
|
263 |
pCred, pCtx, NULL, 0, 0, SECURITY_NATIVE_DREP, |
|
264 |
lastToken ? &InBuffDesc : NULL, 0, newContext, &OutBuffDesc, |
|
265 |
&ContextAttributes, <ime |
|
266 |
); |
|
267 |
||
268 |
if (pInput != 0) { |
|
269 |
(*env)->ReleaseByteArrayElements(env, lastToken, pInput, JNI_ABORT); |
|
270 |
} |
|
271 |
||
272 |
if (ss < 0) { |
|
72
f24a98c3df49
6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents:
2
diff
changeset
|
273 |
endSequence (pCred, pCtx); |
2 | 274 |
return 0; |
275 |
} |
|
276 |
||
277 |
if ((ss == SEC_I_COMPLETE_NEEDED) || (ss == SEC_I_COMPLETE_AND_CONTINUE) ) { |
|
278 |
ss = pCompleteAuthToken( pCtx, &OutBuffDesc ); |
|
279 |
||
280 |
if (ss < 0) { |
|
72
f24a98c3df49
6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents:
2
diff
changeset
|
281 |
endSequence (pCred, pCtx); |
2 | 282 |
return 0; |
283 |
} |
|
284 |
} |
|
285 |
||
286 |
if ( OutSecBuff.cbBuffer > 0 ) { |
|
287 |
jbyteArray ret = (*env)->NewByteArray(env, OutSecBuff.cbBuffer); |
|
288 |
(*env)->SetByteArrayRegion(env, ret, 0, OutSecBuff.cbBuffer, |
|
289 |
OutSecBuff.pvBuffer); |
|
290 |
if (lastToken != 0) // 2nd stage |
|
72
f24a98c3df49
6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents:
2
diff
changeset
|
291 |
endSequence (pCred, pCtx); |
2 | 292 |
result = ret; |
293 |
} |
|
294 |
||
295 |
if ((ss != SEC_I_CONTINUE_NEEDED) && (ss == SEC_I_COMPLETE_AND_CONTINUE)) { |
|
72
f24a98c3df49
6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents:
2
diff
changeset
|
296 |
endSequence (pCred, pCtx); |
2 | 297 |
} |
298 |
||
299 |
return result; |
|
300 |
} |
|
301 |
||
72
f24a98c3df49
6638560: APPCRASH in "SPNEGO_HTTP_AUTH/PROXY_FALLBACK" test case with 64 bit JDK on Win2008 x64, VinVista x64
chegar
parents:
2
diff
changeset
|
302 |
static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle) { |
2 | 303 |
if (credHand != 0) { |
304 |
pFreeCredentialsHandle (credHand); |
|
305 |
free (credHand); |
|
306 |
} |
|
307 |
||
308 |
if (ctxHandle != 0) { |
|
309 |
pDeleteSecurityContext(ctxHandle); |
|
310 |
free (ctxHandle); |
|
311 |
} |
|
312 |
} |