Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/windows/native/sun/net/www/protocol/http/NTLMAuthSequence.c
+++ new/src/windows/native/sun/net/www/protocol/http/ntlm/NTLMAuthSequence.c
1 1 /*
2 2 * Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Sun designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Sun in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 23 * have any questions.
24 24 */
25 25
26 26 #include <jni.h>
27 27 #include <windows.h>
28 28 #include <rpc.h>
29 29 #include <winsock.h>
30 30 #include <lm.h>
31 31
32 32 #include <stdio.h>
33 33 #include <stdarg.h>
34 34 #include <stdlib.h>
35 35 #include <string.h>
36 36 #include <tchar.h>
37 37 #include <fcntl.h>
38 38
39 39 #include "jni_util.h"
40 40
41 41 #define SECURITY_WIN32
42 42 #include "sspi.h"
43 43
44 44
45 45 /*
46 46 * OS calls loaded from DLL on intialization
47 47 */
48 48
49 49 static FREE_CREDENTIALS_HANDLE_FN pFreeCredentialsHandle;
50 50 static ACQUIRE_CREDENTIALS_HANDLE_FN pAcquireCredentialsHandle;
51 51 static FREE_CONTEXT_BUFFER_FN pFreeContextBuffer;
52 52 static INITIALIZE_SECURITY_CONTEXT_FN pInitializeSecurityContext;
↓ open down ↓ |
52 lines elided |
↑ open up ↑ |
53 53 static COMPLETE_AUTH_TOKEN_FN pCompleteAuthToken;
54 54 static DELETE_SECURITY_CONTEXT_FN pDeleteSecurityContext;
55 55
56 56 static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle);
57 57
58 58 static jfieldID ntlm_ctxHandleID;
59 59 static jfieldID ntlm_crdHandleID;
60 60
61 61 static HINSTANCE lib = NULL;
62 62
63 -JNIEXPORT void JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_initFirst
63 +JNIEXPORT void JNICALL Java_sun_net_www_protocol_http_ntlm_NTLMAuthSequence_initFirst
64 64 (JNIEnv *env, jclass clazz)
65 65 {
66 66 OSVERSIONINFO version;
67 67 UCHAR libName[MAX_PATH];
68 68
69 69 ntlm_ctxHandleID = (*env)->GetFieldID(env, clazz, "ctxHandle", "J");
70 70 ntlm_crdHandleID = (*env)->GetFieldID(env, clazz, "crdHandle", "J");
71 71
72 72 version.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
73 73 GetVersionEx (&version);
74 74
75 75 if (version.dwPlatformId == VER_PLATFORM_WIN32_NT) {
76 76 strcpy (libName, "security.dll" );
77 77 }
78 78 else if (version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
79 79 strcpy (libName, "secur32.dll" );
80 80 }
81 81
82 82 lib = LoadLibrary (libName);
83 83
84 84 pFreeCredentialsHandle
85 85 = (FREE_CREDENTIALS_HANDLE_FN) GetProcAddress(
86 86 lib, "FreeCredentialsHandle" );
87 87
88 88 pAcquireCredentialsHandle
89 89 = (ACQUIRE_CREDENTIALS_HANDLE_FN) GetProcAddress(
90 90 lib, "AcquireCredentialsHandleA" );
91 91
92 92 pFreeContextBuffer
93 93 = (FREE_CONTEXT_BUFFER_FN) GetProcAddress(
94 94 lib, "FreeContextBuffer" );
95 95
96 96 pInitializeSecurityContext
97 97 = (INITIALIZE_SECURITY_CONTEXT_FN) GetProcAddress(
98 98 lib, "InitializeSecurityContextA" );
99 99
100 100 pCompleteAuthToken
101 101 = (COMPLETE_AUTH_TOKEN_FN) GetProcAddress(
102 102 lib, "CompleteAuthToken" );
103 103
104 104 pDeleteSecurityContext
105 105 = (DELETE_SECURITY_CONTEXT_FN) GetProcAddress(
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
106 106 lib, "DeleteSecurityContext" );
107 107
108 108 }
109 109
110 110 /*
111 111 * Class: sun_net_www_protocol_http_NTLMAuthSequence
112 112 * Method: getCredentialsHandle
113 113 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)J
114 114 */
115 115
116 -JNIEXPORT jlong JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_getCredentialsHandle
116 +JNIEXPORT jlong JNICALL Java_sun_net_www_protocol_http_ntlm_NTLMAuthSequence_getCredentialsHandle
117 117 (JNIEnv *env, jobject this, jstring user, jstring domain, jstring password)
118 118 {
119 119 SEC_WINNT_AUTH_IDENTITY AuthId;
120 120 SEC_WINNT_AUTH_IDENTITY * pAuthId;
121 121 const CHAR *pUser = 0;
122 122 const CHAR *pDomain = 0;
123 123 const CHAR *pPassword = 0;
124 124 CredHandle *pCred;
125 125 TimeStamp ltime;
126 126 jboolean isCopy;
127 127 SECURITY_STATUS ss;
128 128
129 129 if (user != 0) {
130 130 pUser = JNU_GetStringPlatformChars(env, user, &isCopy);
131 131 if (pUser == NULL)
132 132 return 0; // pending Exception
133 133 }
134 134 if (domain != 0) {
135 135 pDomain = JNU_GetStringPlatformChars(env, domain, &isCopy);
136 136 if (pDomain == NULL) {
137 137 if (pUser != NULL)
138 138 JNU_ReleaseStringPlatformChars(env, user, pUser);
139 139 return 0; // pending Exception
140 140 }
141 141 }
142 142 if (password != 0) {
143 143 pPassword = JNU_GetStringPlatformChars(env, password, &isCopy);
144 144 if (pPassword == NULL) {
145 145 if(pUser != NULL)
146 146 JNU_ReleaseStringPlatformChars(env, user, pUser);
147 147 if(pDomain != NULL)
148 148 JNU_ReleaseStringPlatformChars(env, domain, pDomain);
149 149 return 0; // pending Exception
150 150 }
151 151 }
152 152 pCred = (CredHandle *)malloc(sizeof (CredHandle));
153 153
154 154 if ( ((pUser != NULL) || (pPassword != NULL)) || (pDomain != NULL)) {
155 155 pAuthId = &AuthId;
156 156
157 157 memset( &AuthId, 0, sizeof( AuthId ));
158 158
159 159 if ( pUser != NULL ) {
160 160 AuthId.User = (unsigned char *) pUser;
161 161 AuthId.UserLength = strlen( pUser );
162 162 }
163 163
164 164 if ( pPassword != NULL ) {
165 165 AuthId.Password = (unsigned char *) pPassword;
166 166 AuthId.PasswordLength = strlen( pPassword );
167 167 }
168 168
169 169 if ( pDomain != NULL ) {
170 170 AuthId.Domain = (unsigned char *) pDomain;
171 171 AuthId.DomainLength = strlen( pDomain );
172 172 }
173 173
174 174 AuthId.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
175 175 } else {
176 176 pAuthId = NULL;
177 177 }
178 178
179 179 ss = pAcquireCredentialsHandle(
180 180 NULL, "NTLM", SECPKG_CRED_OUTBOUND,
181 181 NULL, pAuthId, NULL, NULL,
182 182 pCred, <ime
183 183 );
184 184
185 185 /* Release resources held by JNU_GetStringPlatformChars */
186 186 if (pUser != NULL)
187 187 JNU_ReleaseStringPlatformChars(env, user, pUser);
188 188 if (pPassword != NULL)
189 189 JNU_ReleaseStringPlatformChars(env, password, pPassword);
↓ open down ↓ |
63 lines elided |
↑ open up ↑ |
190 190 if (pDomain != NULL)
191 191 JNU_ReleaseStringPlatformChars(env, domain, pDomain);
192 192
193 193 if (ss == 0) {
194 194 return (jlong) pCred;
195 195 } else {
196 196 return 0;
197 197 }
198 198 }
199 199
200 -JNIEXPORT jbyteArray JNICALL Java_sun_net_www_protocol_http_NTLMAuthSequence_getNextToken
200 +JNIEXPORT jbyteArray JNICALL Java_sun_net_www_protocol_http_ntlm_NTLMAuthSequence_getNextToken
201 201 (JNIEnv *env, jobject this, jlong crdHandle, jbyteArray lastToken)
202 202 {
203 203
204 204 VOID *pInput = 0;
205 205 DWORD inputLen;
206 206 CHAR buffOut[512];
207 207 jboolean isCopy;
208 208 SECURITY_STATUS ss;
209 209 SecBufferDesc OutBuffDesc;
210 210 SecBuffer OutSecBuff;
211 211 SecBufferDesc InBuffDesc;
212 212 SecBuffer InSecBuff;
213 213 ULONG ContextAttributes;
214 214 CredHandle *pCred = (CredHandle *)crdHandle;
215 215 CtxtHandle *pCtx;
216 216 CtxtHandle *newContext;
217 217 TimeStamp ltime;
218 218 jbyteArray result;
219 219
220 220
221 221 pCtx = (CtxtHandle *) (*env)->GetLongField (env, this, ntlm_ctxHandleID);
222 222 if (pCtx == 0) { /* first call */
223 223 newContext = (CtxtHandle *)malloc(sizeof(CtxtHandle));
224 224 (*env)->SetLongField (env, this, ntlm_ctxHandleID, (jlong)newContext);
225 225 } else {
226 226 newContext = pCtx;
227 227 }
228 228
229 229 OutBuffDesc.ulVersion = 0;
230 230 OutBuffDesc.cBuffers = 1;
231 231 OutBuffDesc.pBuffers = &OutSecBuff;
232 232
233 233 OutSecBuff.cbBuffer = 512;
234 234 OutSecBuff.BufferType = SECBUFFER_TOKEN;
235 235 OutSecBuff.pvBuffer = buffOut;
236 236
237 237 /*
238 238 * Prepare our Input buffer - Note the server is expecting the client's
239 239 * negotiation packet on the first call
240 240 */
241 241
242 242 if (lastToken != 0)
243 243 {
244 244 pInput = (VOID *)(*env)->GetByteArrayElements(env, lastToken, &isCopy);
245 245 inputLen = (*env)->GetArrayLength(env, lastToken);
246 246
247 247 InBuffDesc.ulVersion = 0;
248 248 InBuffDesc.cBuffers = 1;
249 249 InBuffDesc.pBuffers = &InSecBuff;
250 250
251 251 InSecBuff.cbBuffer = inputLen;
252 252 InSecBuff.BufferType = SECBUFFER_TOKEN;
253 253 InSecBuff.pvBuffer = pInput;
254 254 }
255 255
256 256 /*
257 257 * will return success when its done but we still
258 258 * need to send the out buffer if there are bytes to send
259 259 */
260 260
261 261 ss = pInitializeSecurityContext(
262 262 pCred, pCtx, NULL, 0, 0, SECURITY_NATIVE_DREP,
263 263 lastToken ? &InBuffDesc : NULL, 0, newContext, &OutBuffDesc,
264 264 &ContextAttributes, <ime
265 265 );
266 266
267 267 if (pInput != 0) {
268 268 (*env)->ReleaseByteArrayElements(env, lastToken, pInput, JNI_ABORT);
269 269 }
270 270
271 271 if (ss < 0) {
272 272 endSequence (pCred, pCtx);
273 273 return 0;
274 274 }
275 275
276 276 if ((ss == SEC_I_COMPLETE_NEEDED) || (ss == SEC_I_COMPLETE_AND_CONTINUE) ) {
277 277 ss = pCompleteAuthToken( pCtx, &OutBuffDesc );
278 278
279 279 if (ss < 0) {
280 280 endSequence (pCred, pCtx);
281 281 return 0;
282 282 }
283 283 }
284 284
285 285 if ( OutSecBuff.cbBuffer > 0 ) {
286 286 jbyteArray ret = (*env)->NewByteArray(env, OutSecBuff.cbBuffer);
287 287 (*env)->SetByteArrayRegion(env, ret, 0, OutSecBuff.cbBuffer,
288 288 OutSecBuff.pvBuffer);
289 289 if (lastToken != 0) // 2nd stage
290 290 endSequence (pCred, pCtx);
291 291 result = ret;
292 292 }
293 293
294 294 if ((ss != SEC_I_CONTINUE_NEEDED) && (ss == SEC_I_COMPLETE_AND_CONTINUE)) {
295 295 endSequence (pCred, pCtx);
296 296 }
297 297
298 298 return result;
299 299 }
300 300
301 301 static void endSequence (PCredHandle credHand, PCtxtHandle ctxHandle) {
302 302 if (credHand != 0) {
303 303 pFreeCredentialsHandle (credHand);
304 304 free (credHand);
305 305 }
306 306
307 307 if (ctxHandle != 0) {
308 308 pDeleteSecurityContext(ctxHandle);
309 309 free (ctxHandle);
310 310 }
311 311 }
↓ open down ↓ |
101 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX