|
| 1 | +/* Copyright (c) 2010 Daniel Doubrovkine, All Rights Reserved |
| 2 | + * |
| 3 | + * The contents of this file is dual-licensed under 2 |
| 4 | + * alternative Open Source/Free licenses: LGPL 2.1 or later and |
| 5 | + * Apache License 2.0. (starting with JNA version 4.0.0). |
| 6 | + * |
| 7 | + * You can freely decide which license you want to apply to |
| 8 | + * the project. |
| 9 | + * |
| 10 | + * You may obtain a copy of the LGPL License at: |
| 11 | + * |
| 12 | + * http://www.gnu.org/licenses/licenses.html |
| 13 | + * |
| 14 | + * A copy is also included in the downloadable source code package |
| 15 | + * containing JNA, in file "LGPL2.1". |
| 16 | + * |
| 17 | + * You may obtain a copy of the Apache License at: |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/ |
| 20 | + * |
| 21 | + * A copy is also included in the downloadable source code package |
| 22 | + * containing JNA, in file "AL2.0". |
| 23 | + */ |
| 24 | +package com.sun.jna.platform.win32; |
| 25 | + |
| 26 | +import com.sun.jna.ptr.IntByReference; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import static junit.framework.TestCase.assertEquals; |
| 30 | +import static junit.framework.TestCase.assertTrue; |
| 31 | + |
| 32 | + |
| 33 | +public class Secur32_Impersonate_Test { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testImpersonateRevertSecurityContext() { |
| 37 | + // client ----------- acquire outbound credential handle |
| 38 | + Sspi.CredHandle phClientCredential = new Sspi.CredHandle(); |
| 39 | + Sspi.TimeStamp ptsClientExpiry = new Sspi.TimeStamp(); |
| 40 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle( |
| 41 | + null, "Negotiate", Sspi.SECPKG_CRED_OUTBOUND, null, null, null, |
| 42 | + null, phClientCredential, ptsClientExpiry)); |
| 43 | + // client ----------- security context |
| 44 | + Sspi.CtxtHandle phClientContext = new Sspi.CtxtHandle(); |
| 45 | + IntByReference pfClientContextAttr = new IntByReference(); |
| 46 | + // server ----------- acquire inbound credential handle |
| 47 | + Sspi.CredHandle phServerCredential = new Sspi.CredHandle(); |
| 48 | + Sspi.TimeStamp ptsServerExpiry = new Sspi.TimeStamp(); |
| 49 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle( |
| 50 | + null, "Negotiate", Sspi.SECPKG_CRED_INBOUND, null, null, null, |
| 51 | + null, phServerCredential, ptsServerExpiry)); |
| 52 | + // server ----------- security context |
| 53 | + Sspi.CtxtHandle phServerContext = new Sspi.CtxtHandle(); |
| 54 | + SspiUtil.ManagedSecBufferDesc pbServerToken = null; |
| 55 | + IntByReference pfServerContextAttr = new IntByReference(); |
| 56 | + int clientRc = W32Errors.SEC_I_CONTINUE_NEEDED; |
| 57 | + int serverRc = W32Errors.SEC_I_CONTINUE_NEEDED; |
| 58 | + do { |
| 59 | + // client ----------- initialize security context, produce a client token |
| 60 | + // client token returned is always new |
| 61 | + SspiUtil.ManagedSecBufferDesc pbClientToken = new SspiUtil.ManagedSecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE); |
| 62 | + if (clientRc == W32Errors.SEC_I_CONTINUE_NEEDED) { |
| 63 | + // server token is empty the first time |
| 64 | + SspiUtil.ManagedSecBufferDesc pbServerTokenCopy = pbServerToken == null |
| 65 | + ? null : new SspiUtil.ManagedSecBufferDesc(Sspi.SECBUFFER_TOKEN, pbServerToken.getBuffer(0).getBytes()); |
| 66 | + clientRc = Secur32.INSTANCE.InitializeSecurityContext( |
| 67 | + phClientCredential, |
| 68 | + phClientContext.isNull() ? null : phClientContext, |
| 69 | + Advapi32Util.getUserName(), |
| 70 | + Sspi.ISC_REQ_CONNECTION, |
| 71 | + 0, |
| 72 | + Sspi.SECURITY_NATIVE_DREP, |
| 73 | + pbServerTokenCopy, |
| 74 | + 0, |
| 75 | + phClientContext, |
| 76 | + pbClientToken, |
| 77 | + pfClientContextAttr, |
| 78 | + null); |
| 79 | + assertTrue(clientRc == W32Errors.SEC_I_CONTINUE_NEEDED || clientRc == W32Errors.SEC_E_OK); |
| 80 | + } |
| 81 | + // server ----------- accept security context, produce a server token |
| 82 | + if (serverRc == W32Errors.SEC_I_CONTINUE_NEEDED) { |
| 83 | + pbServerToken = new SspiUtil.ManagedSecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE); |
| 84 | + SspiUtil.ManagedSecBufferDesc pbClientTokenByValue = new SspiUtil.ManagedSecBufferDesc(Sspi.SECBUFFER_TOKEN, pbClientToken.getBuffer(0).getBytes()); |
| 85 | + serverRc = Secur32.INSTANCE.AcceptSecurityContext(phServerCredential, |
| 86 | + phServerContext.isNull() ? null : phServerContext, |
| 87 | + pbClientTokenByValue, |
| 88 | + Sspi.ISC_REQ_CONNECTION, |
| 89 | + Sspi.SECURITY_NATIVE_DREP, |
| 90 | + phServerContext, |
| 91 | + pbServerToken, |
| 92 | + pfServerContextAttr, |
| 93 | + ptsServerExpiry); |
| 94 | + assertTrue(serverRc == W32Errors.SEC_I_CONTINUE_NEEDED || serverRc == W32Errors.SEC_E_OK); |
| 95 | + } |
| 96 | + } while (serverRc != W32Errors.SEC_E_OK || clientRc != W32Errors.SEC_E_OK); |
| 97 | + // impersonate |
| 98 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.ImpersonateSecurityContext( |
| 99 | + phServerContext)); |
| 100 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.RevertSecurityContext( |
| 101 | + phServerContext)); |
| 102 | + // release server context |
| 103 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext( |
| 104 | + phServerContext)); |
| 105 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle( |
| 106 | + phServerCredential)); |
| 107 | + // release client context |
| 108 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext( |
| 109 | + phClientContext)); |
| 110 | + assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle( |
| 111 | + phClientCredential)); |
| 112 | + } |
| 113 | +} |
0 commit comments