|
17 | 17 | import com.sun.jna.platform.win32.Sspi.CtxtHandle;
|
18 | 18 | import com.sun.jna.platform.win32.Sspi.PSecPkgInfo;
|
19 | 19 | import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
|
| 20 | +import com.sun.jna.platform.win32.Sspi.SecPkgContext_PackageInfo; |
20 | 21 | import com.sun.jna.platform.win32.Sspi.SecPkgInfo;
|
| 22 | +import com.sun.jna.platform.win32.Sspi.SecPkgInfo.ByReference; |
21 | 23 | import com.sun.jna.platform.win32.Sspi.TimeStamp;
|
22 | 24 | import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
|
23 | 25 | import com.sun.jna.ptr.IntByReference;
|
@@ -341,4 +343,68 @@ public void testCreateEmptyToken() {
|
341 | 343 | assertEquals(Sspi.MAX_TOKEN_SIZE, token.pBuffers[0].cbBuffer);
|
342 | 344 | assertEquals(token.getBytes().length, token.pBuffers[0].getBytes().length);
|
343 | 345 | }
|
| 346 | + |
| 347 | + public void testQueryContextAttributes() { |
| 348 | + // client ----------- acquire outbound credential handle |
| 349 | + CredHandle phClientCredential = new CredHandle(); |
| 350 | + TimeStamp ptsClientExpiry = new TimeStamp(); |
| 351 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle(null, "Negotiate", |
| 352 | + Sspi.SECPKG_CRED_OUTBOUND, null, null, null, null, phClientCredential, ptsClientExpiry)); |
| 353 | + // client ----------- security context |
| 354 | + CtxtHandle phClientContext = new CtxtHandle(); |
| 355 | + IntByReference pfClientContextAttr = new IntByReference(); |
| 356 | + // server ----------- acquire inbound credential handle |
| 357 | + CredHandle phServerCredential = new CredHandle(); |
| 358 | + TimeStamp ptsServerExpiry = new TimeStamp(); |
| 359 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle(null, "Negotiate", |
| 360 | + Sspi.SECPKG_CRED_INBOUND, null, null, null, null, phServerCredential, ptsServerExpiry)); |
| 361 | + // server ----------- security context |
| 362 | + CtxtHandle phServerContext = new CtxtHandle(); |
| 363 | + SecBufferDesc pbServerToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE); |
| 364 | + IntByReference pfServerContextAttr = new IntByReference(); |
| 365 | + int clientRc = W32Errors.SEC_I_CONTINUE_NEEDED; |
| 366 | + int serverRc = W32Errors.SEC_I_CONTINUE_NEEDED; |
| 367 | + do { |
| 368 | + // client token returned is always new |
| 369 | + SecBufferDesc pbClientToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE); |
| 370 | + // client ----------- initialize security context, produce a client |
| 371 | + // token |
| 372 | + if (clientRc == W32Errors.SEC_I_CONTINUE_NEEDED) { |
| 373 | + // server token is empty the first time |
| 374 | + clientRc = Secur32.INSTANCE.InitializeSecurityContext(phClientCredential, |
| 375 | + phClientContext.isNull() ? null : phClientContext, Advapi32Util.getUserName(), |
| 376 | + Sspi.ISC_REQ_CONNECTION, 0, Sspi.SECURITY_NATIVE_DREP, pbServerToken, 0, phClientContext, |
| 377 | + pbClientToken, pfClientContextAttr, null); |
| 378 | + assertTrue(clientRc == W32Errors.SEC_I_CONTINUE_NEEDED || clientRc == W32Errors.SEC_E_OK); |
| 379 | + } |
| 380 | + // server ----------- accept security context, produce a server |
| 381 | + // token |
| 382 | + if (serverRc == W32Errors.SEC_I_CONTINUE_NEEDED) { |
| 383 | + serverRc = Secur32.INSTANCE.AcceptSecurityContext(phServerCredential, phServerContext.isNull() ? null |
| 384 | + : phServerContext, pbClientToken, Sspi.ISC_REQ_CONNECTION, Sspi.SECURITY_NATIVE_DREP, |
| 385 | + phServerContext, pbServerToken, pfServerContextAttr, ptsServerExpiry); |
| 386 | + assertTrue(serverRc == W32Errors.SEC_I_CONTINUE_NEEDED || serverRc == W32Errors.SEC_E_OK); |
| 387 | + } |
| 388 | + } while (serverRc != W32Errors.SEC_E_OK || clientRc != W32Errors.SEC_E_OK); |
| 389 | + // query context attributes |
| 390 | + SecPkgContext_PackageInfo packageinfo = new SecPkgContext_PackageInfo(); |
| 391 | + assertEquals(W32Errors.SEC_E_OK, |
| 392 | + Secur32.INSTANCE.QueryContextAttributes(phServerContext, Sspi.SECPKG_ATTR_PACKAGE_INFO, packageinfo)); |
| 393 | + ByReference info = packageinfo.PackageInfo; |
| 394 | + |
| 395 | + assertNotNull(info.Name); |
| 396 | + assertNotNull(info.Comment); |
| 397 | + |
| 398 | + assertTrue(!info.Name.isEmpty()); |
| 399 | + assertTrue(!info.Comment.isEmpty()); |
| 400 | + |
| 401 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeContextBuffer(info.getPointer())); |
| 402 | + |
| 403 | + // release server context |
| 404 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext(phServerContext)); |
| 405 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle(phServerCredential)); |
| 406 | + // release client context |
| 407 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext(phClientContext)); |
| 408 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle(phClientCredential)); |
| 409 | + } |
344 | 410 | }
|
0 commit comments