Search in sources :

Example 31 with IntByReference

use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.

the class Secur32Test method testInitializeSecurityContext.

public void testInitializeSecurityContext() {
    CredHandle phCredential = new CredHandle();
    TimeStamp ptsExpiry = new TimeStamp();
    // acquire a credentials handle
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.AcquireCredentialsHandle(null, "Negotiate", Sspi.SECPKG_CRED_OUTBOUND, null, null, null, null, phCredential, ptsExpiry));
    // initialize security context
    CtxtHandle phNewContext = new CtxtHandle();
    SecBufferDesc pbToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE);
    IntByReference pfContextAttr = new IntByReference();
    int rc = Secur32.INSTANCE.InitializeSecurityContext(phCredential, null, Advapi32Util.getUserName(), Sspi.ISC_REQ_CONNECTION, 0, Sspi.SECURITY_NATIVE_DREP, null, 0, phNewContext, pbToken, pfContextAttr, null);
    assertTrue(rc == W32Errors.SEC_I_CONTINUE_NEEDED || rc == W32Errors.SEC_E_OK);
    assertTrue(phNewContext.dwLower != null);
    assertTrue(phNewContext.dwUpper != null);
    assertTrue(pbToken.pBuffers[0].getBytes().length > 0);
    // release
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.DeleteSecurityContext(phNewContext));
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeCredentialsHandle(phCredential));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) CtxtHandle(com.sun.jna.platform.win32.Sspi.CtxtHandle) CredHandle(com.sun.jna.platform.win32.Sspi.CredHandle) TimeStamp(com.sun.jna.platform.win32.Sspi.TimeStamp) SecBufferDesc(com.sun.jna.platform.win32.Sspi.SecBufferDesc)

Example 32 with IntByReference

use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.

the class Secur32Test method testEnumerateSecurityPackages.

public void testEnumerateSecurityPackages() {
    IntByReference pcPackages = new IntByReference();
    PSecPkgInfo.ByReference pPackageInfo = new PSecPkgInfo.ByReference();
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.EnumerateSecurityPackages(pcPackages, pPackageInfo));
    SecPkgInfo.ByReference[] packagesInfo = pPackageInfo.toArray(pcPackages.getValue());
    for (SecPkgInfo.ByReference packageInfo : packagesInfo) {
        assertTrue(packageInfo.Name.length() > 0);
        assertTrue(packageInfo.Comment.length() >= 0);
    }
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.FreeContextBuffer(pPackageInfo.pPkgInfo.getPointer()));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ByReference(com.sun.jna.platform.win32.Sspi.SecPkgInfo.ByReference) SecPkgInfo(com.sun.jna.platform.win32.Sspi.SecPkgInfo) PSecPkgInfo(com.sun.jna.platform.win32.Sspi.PSecPkgInfo) PSecPkgInfo(com.sun.jna.platform.win32.Sspi.PSecPkgInfo) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) IntByReference(com.sun.jna.ptr.IntByReference) ByReference(com.sun.jna.platform.win32.Sspi.SecPkgInfo.ByReference)

Example 33 with IntByReference

use of com.sun.jna.ptr.IntByReference 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 34 with IntByReference

use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.

the class Secur32Test method testGetUserNameEx.

public void testGetUserNameEx() {
    IntByReference len = new IntByReference();
    Secur32.INSTANCE.GetUserNameEx(Secur32.EXTENDED_NAME_FORMAT.NameSamCompatible, null, len);
    assertTrue(len.getValue() > 0);
    char[] buffer = new char[len.getValue() + 1];
    assertTrue(Secur32.INSTANCE.GetUserNameEx(Secur32.EXTENDED_NAME_FORMAT.NameSamCompatible, buffer, len));
    String username = Native.toString(buffer);
    assertTrue(username.length() > 0);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference)

Example 35 with IntByReference

use of com.sun.jna.ptr.IntByReference in project jna by java-native-access.

the class Secur32Test method testQuerySecurityContextToken.

public void testQuerySecurityContextToken() {
    // 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);
        // client ----------- initialize security context, produce a client 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);
        }
        // server ----------- accept security context, produce a server 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 security context token
    HANDLEByReference phContextToken = new HANDLEByReference();
    assertEquals(W32Errors.SEC_E_OK, Secur32.INSTANCE.QuerySecurityContextToken(phServerContext, phContextToken));
    // release security context token
    Kernel32Util.closeHandleRef(phContextToken);
    // 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) CredHandle(com.sun.jna.platform.win32.Sspi.CredHandle) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) TimeStamp(com.sun.jna.platform.win32.Sspi.TimeStamp) SecBufferDesc(com.sun.jna.platform.win32.Sspi.SecBufferDesc)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)199 PointerByReference (com.sun.jna.ptr.PointerByReference)38 Memory (com.sun.jna.Memory)33 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)26 File (java.io.File)19 Pointer (com.sun.jna.Pointer)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)14 PSID (com.sun.jna.platform.win32.WinNT.PSID)13 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)11 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)11 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)9 ACL (com.sun.jna.platform.win32.WinNT.ACL)8 Advapi32 (com.sun.jna.platform.win32.Advapi32)7 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)7 ACCESS_ALLOWED_ACE (com.sun.jna.platform.win32.WinNT.ACCESS_ALLOWED_ACE)6 SECURITY_DESCRIPTOR (com.sun.jna.platform.win32.WinNT.SECURITY_DESCRIPTOR)6 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)6 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)6 CredHandle (com.sun.jna.platform.win32.Sspi.CredHandle)5