• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Waffle / waffle / 6516

11 Mar 2026 12:15AM UTC coverage: 46.217%. Remained the same
6516

push

github

web-flow
Update dependency org.eclipse.jdt:ecj to v3.45.0

276 of 734 branches covered (37.6%)

Branch coverage included in aggregate %.

1019 of 2068 relevant lines covered (49.27%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.65
/Source/JNA/waffle-jna/src/main/java/waffle/windows/auth/impl/WindowsSecurityContextImpl.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6
 */
7
package waffle.windows.auth.impl;
8

9
import com.sun.jna.platform.win32.Secur32;
10
import com.sun.jna.platform.win32.Sspi;
11
import com.sun.jna.platform.win32.Sspi.CtxtHandle;
12
import com.sun.jna.platform.win32.Sspi.SecBufferDesc;
13
import com.sun.jna.platform.win32.SspiUtil.ManagedSecBufferDesc;
14
import com.sun.jna.platform.win32.Win32Exception;
15
import com.sun.jna.platform.win32.WinError;
16
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
17
import com.sun.jna.ptr.IntByReference;
18

19
import waffle.windows.auth.IWindowsCredentialsHandle;
20
import waffle.windows.auth.IWindowsIdentity;
21
import waffle.windows.auth.IWindowsImpersonationContext;
22
import waffle.windows.auth.IWindowsSecurityContext;
23

24
/**
25
 * Windows Security Context.
26
 */
27
public class WindowsSecurityContextImpl implements IWindowsSecurityContext {
2✔
28

29
    /** The principal name. */
30
    private String principalName;
31

32
    /** The security package. */
33
    private String securityPackage;
34

35
    /** The token. */
36
    private ManagedSecBufferDesc token;
37

38
    /** The ctx. */
39
    private CtxtHandle ctx;
40

41
    /** The credentials. */
42
    private IWindowsCredentialsHandle credentials;
43

44
    /** The continue flag. */
45
    private boolean continueFlag;
46

47
    @Override
48
    public IWindowsImpersonationContext impersonate() {
49
        return new WindowsSecurityContextImpersonationContextImpl(this.ctx);
×
50
    }
51

52
    @Override
53
    public IWindowsIdentity getIdentity() {
54
        final HANDLEByReference phContextToken = new HANDLEByReference();
×
55
        final int rc = Secur32.INSTANCE.QuerySecurityContextToken(this.ctx, phContextToken);
×
56
        if (WinError.SEC_E_OK != rc) {
×
57
            throw new Win32Exception(rc);
×
58
        }
59
        return new WindowsIdentityImpl(phContextToken.getValue());
×
60
    }
61

62
    @Override
63
    public String getSecurityPackage() {
64
        return this.securityPackage;
2✔
65
    }
66

67
    @Override
68
    public byte[] getToken() {
69
        return this.token == null || this.token.getBuffer(0).getBytes() == null ? null
2!
70
                : this.token.getBuffer(0).getBytes().clone();
2✔
71
    }
72

73
    /**
74
     * Get the current Windows security context for a given SSPI package.
75
     *
76
     * @param securityPackage
77
     *            SSPI package.
78
     * @param targetName
79
     *            The target of the context. The string contents are security-package specific.
80
     *
81
     * @return Windows security context.
82
     */
83
    public static IWindowsSecurityContext getCurrent(final String securityPackage, final String targetName) {
84
        IWindowsCredentialsHandle credentialsHandle = WindowsCredentialsHandleImpl.getCurrent(securityPackage);
2✔
85
        credentialsHandle.initialize();
2✔
86
        try {
87
            final WindowsSecurityContextImpl ctx = new WindowsSecurityContextImpl();
2✔
88
            ctx.setPrincipalName(WindowsAccountImpl.getCurrentUsername());
2✔
89
            ctx.setCredentialsHandle(credentialsHandle);
2✔
90
            ctx.setSecurityPackage(securityPackage);
2✔
91
            ctx.initialize(null, null, targetName);
2✔
92

93
            // Starting from here ctx 'owns' the credentials handle, so let's null out the
94
            // variable. This will prevent the finally block below from disposing it right away.
95
            credentialsHandle = null;
2✔
96

97
            return ctx;
2✔
98
        } finally {
99
            if (credentialsHandle != null) {
2!
100
                credentialsHandle.dispose();
×
101
            }
102
        }
103
    }
104

105
    @Override
106
    public void initialize(final CtxtHandle continueCtx, final SecBufferDesc continueToken, final String targetName) {
107
        final IntByReference attr = new IntByReference();
2✔
108
        this.ctx = new CtxtHandle();
2✔
109
        int tokenSize = Sspi.MAX_TOKEN_SIZE;
2✔
110
        int rc;
111
        do {
112
            this.token = new ManagedSecBufferDesc(Sspi.SECBUFFER_TOKEN, tokenSize);
2✔
113
            rc = Secur32.INSTANCE.InitializeSecurityContext(this.credentials.getHandle(), continueCtx, targetName,
2✔
114
                    Sspi.ISC_REQ_CONNECTION, 0, Sspi.SECURITY_NATIVE_DREP, continueToken, 0, this.ctx, this.token, attr,
115
                    null);
116
            switch (rc) {
2!
117
                case WinError.SEC_E_INSUFFICIENT_MEMORY:
118
                case WinError.SEC_E_BUFFER_TOO_SMALL:
119
                    tokenSize += Sspi.MAX_TOKEN_SIZE;
×
120
                    break;
×
121
                case WinError.SEC_I_CONTINUE_NEEDED:
122
                    this.continueFlag = true;
2✔
123
                    break;
2✔
124
                case WinError.SEC_E_OK:
125
                    this.continueFlag = false;
×
126
                    break;
×
127
                default:
128
                    throw new Win32Exception(rc);
×
129
            }
130
        } while (rc == WinError.SEC_E_INSUFFICIENT_MEMORY || rc == WinError.SEC_E_BUFFER_TOO_SMALL);
2!
131
    }
2✔
132

133
    @Override
134
    public void dispose() {
135
        WindowsSecurityContextImpl.dispose(this.ctx);
2✔
136

137
        if (this.credentials != null) {
2!
138
            this.credentials.dispose();
2✔
139
        }
140
    }
2✔
141

142
    /**
143
     * Dispose a security context.
144
     *
145
     * @param ctx
146
     *            Security context.
147
     *
148
     * @return True if a context was disposed.
149
     */
150
    public static boolean dispose(final CtxtHandle ctx) {
151
        if (ctx != null && !ctx.isNull()) {
2!
152
            final int rc = Secur32.INSTANCE.DeleteSecurityContext(ctx);
2✔
153
            if (WinError.SEC_E_OK != rc) {
2!
154
                throw new Win32Exception(rc);
×
155
            }
156
            return true;
2✔
157
        }
158
        return false;
×
159
    }
160

161
    @Override
162
    public String getPrincipalName() {
163
        return this.principalName;
×
164
    }
165

166
    /**
167
     * Sets the principal name.
168
     *
169
     * @param value
170
     *            the new principal name
171
     */
172
    public void setPrincipalName(final String value) {
173
        this.principalName = value;
2✔
174
    }
2✔
175

176
    @Override
177
    public CtxtHandle getHandle() {
178
        return this.ctx;
×
179
    }
180

181
    /**
182
     * Sets the credentials handle.
183
     *
184
     * @param handle
185
     *            the new credentials handle
186
     */
187
    public void setCredentialsHandle(final IWindowsCredentialsHandle handle) {
188
        this.credentials = handle;
2✔
189
    }
2✔
190

191
    /**
192
     * Sets the token.
193
     *
194
     * @param bytes
195
     *            the new token
196
     */
197
    public void setToken(final byte[] bytes) {
198
        this.token = new ManagedSecBufferDesc(Sspi.SECBUFFER_TOKEN, bytes);
×
199
    }
×
200

201
    /**
202
     * Sets the security package.
203
     *
204
     * @param value
205
     *            the new security package
206
     */
207
    public void setSecurityPackage(final String value) {
208
        this.securityPackage = value;
2✔
209
    }
2✔
210

211
    /**
212
     * Sets the security context.
213
     *
214
     * @param phNewServerContext
215
     *            the new security context
216
     */
217
    public void setSecurityContext(final CtxtHandle phNewServerContext) {
218
        this.ctx = phNewServerContext;
×
219
    }
×
220

221
    @Override
222
    public boolean isContinue() {
223
        return this.continueFlag;
2✔
224
    }
225

226
    /**
227
     * Sets the continue.
228
     *
229
     * @param b
230
     *            the new continue
231
     */
232
    public void setContinue(final boolean b) {
233
        this.continueFlag = b;
×
234
    }
×
235

236
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc