Search in sources :

Example 1 with ByReference

use of com.sun.jna.platform.win32.Sspi.SecPkgInfo.ByReference in project jna by java-native-access.

the class Secur32Test method testQueryContextAttributes.

public void testQueryContextAttributes() {
    // client ----------- acquire outbound credential handle
    CredHandle phClientCredential = new CredHandle();
    TimeStamp ptsClientExpiry = new TimeStamp();
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle(null, "Negotiate", Sspi.SECPKG_CRED_OUTBOUND, null, null, null, null, phClientCredential, ptsClientExpiry));
    // client ----------- security context
    CtxtHandle phClientContext = new CtxtHandle();
    IntByReference pfClientContextAttr = new IntByReference();
    // server ----------- acquire inbound credential handle
    CredHandle phServerCredential = new CredHandle();
    TimeStamp ptsServerExpiry = new TimeStamp();
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle(null, "Negotiate", Sspi.SECPKG_CRED_INBOUND, null, null, null, null, phServerCredential, ptsServerExpiry));
    // server ----------- security context
    CtxtHandle phServerContext = new CtxtHandle();
    SecBufferDesc pbServerToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE);
    IntByReference pfServerContextAttr = new IntByReference();
    int clientRc = W32Errors.SEC_I_CONTINUE_NEEDED;
    int serverRc = W32Errors.SEC_I_CONTINUE_NEEDED;
    do {
        // client token returned is always new
        SecBufferDesc pbClientToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE);
        // token
        if (clientRc == W32Errors.SEC_I_CONTINUE_NEEDED) {
            // server token is empty the first time
            clientRc = Secur32.INSTANCE.InitializeSecurityContext(phClientCredential, phClientContext.isNull() ? null : phClientContext, Advapi32Util.getUserName(), Sspi.ISC_REQ_CONNECTION, 0, Sspi.SECURITY_NATIVE_DREP, pbServerToken, 0, phClientContext, pbClientToken, pfClientContextAttr, null);
            assertTrue(clientRc == W32Errors.SEC_I_CONTINUE_NEEDED || clientRc == W32Errors.SEC_E_OK);
        }
        // token
        if (serverRc == W32Errors.SEC_I_CONTINUE_NEEDED) {
            serverRc = Secur32.INSTANCE.AcceptSecurityContext(phServerCredential, phServerContext.isNull() ? null : phServerContext, pbClientToken, Sspi.ISC_REQ_CONNECTION, Sspi.SECURITY_NATIVE_DREP, phServerContext, pbServerToken, pfServerContextAttr, ptsServerExpiry);
            assertTrue(serverRc == W32Errors.SEC_I_CONTINUE_NEEDED || serverRc == W32Errors.SEC_E_OK);
        }
    } while (serverRc != W32Errors.SEC_E_OK || clientRc != W32Errors.SEC_E_OK);
    // query context attributes
    SecPkgContext_PackageInfo packageinfo = new SecPkgContext_PackageInfo();
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.QueryContextAttributes(phServerContext, Sspi.SECPKG_ATTR_PACKAGE_INFO, packageinfo));
    ByReference info = packageinfo.PackageInfo;
    assertNotNull(info.Name);
    assertNotNull(info.Comment);
    assertTrue(!info.Name.isEmpty());
    assertTrue(!info.Comment.isEmpty());
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeContextBuffer(info.getPointer()));
    // release server context
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext(phServerContext));
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle(phServerCredential));
    // release client context
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext(phClientContext));
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle(phClientCredential));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) CtxtHandle(com.sun.jna.platform.win32.Sspi.CtxtHandle) SecPkgContext_PackageInfo(com.sun.jna.platform.win32.Sspi.SecPkgContext_PackageInfo) CredHandle(com.sun.jna.platform.win32.Sspi.CredHandle) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) IntByReference(com.sun.jna.ptr.IntByReference) ByReference(com.sun.jna.platform.win32.Sspi.SecPkgInfo.ByReference) TimeStamp(com.sun.jna.platform.win32.Sspi.TimeStamp) SecBufferDesc(com.sun.jna.platform.win32.Sspi.SecBufferDesc)

Example 2 with ByReference

use of com.sun.jna.platform.win32.Sspi.SecPkgInfo.ByReference in project jna by java-native-access.

the class Kernel32Test method testGetProcessList.

public void testGetProcessList() throws IOException {
    HANDLE processEnumHandle = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPALL, new WinDef.DWORD(0));
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(processEnumHandle));
    try {
        Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
        assertTrue(Kernel32.INSTANCE.Process32First(processEnumHandle, processEntry));
        List<Long> processIdList = new ArrayList<Long>();
        processIdList.add(processEntry.th32ProcessID.longValue());
        while (Kernel32.INSTANCE.Process32Next(processEnumHandle, processEntry)) {
            processIdList.add(processEntry.th32ProcessID.longValue());
        }
        assertTrue(processIdList.size() > 4);
    } finally {
        Kernel32Util.closeHandle(processEnumHandle);
    }
}
Also used : ArrayList(java.util.ArrayList) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) IntByReference(com.sun.jna.ptr.IntByReference) ShortByReference(com.sun.jna.ptr.ShortByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Aggregations

HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)2 IntByReference (com.sun.jna.ptr.IntByReference)2 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)1 CtxtHandle (com.sun.jna.platform.win32.Sspi.CtxtHandle)1 SecBufferDesc (com.sun.jna.platform.win32.Sspi.SecBufferDesc)1 SecPkgContext_PackageInfo (com.sun.jna.platform.win32.Sspi.SecPkgContext_PackageInfo)1 ByReference (com.sun.jna.platform.win32.Sspi.SecPkgInfo.ByReference)1 TimeStamp (com.sun.jna.platform.win32.Sspi.TimeStamp)1 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)1 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 ShortByReference (com.sun.jna.ptr.ShortByReference)1 ArrayList (java.util.ArrayList)1