File: sspi_wrapper.c

package info (click to toggle)
golang-mongodb-mongo-driver 1.8.4%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 18,520 kB
  • sloc: perl: 533; ansic: 491; python: 432; makefile: 187; sh: 72
file content (243 lines) | stat: -rw-r--r-- 7,136 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//+build gssapi,windows

#include "sspi_wrapper.h"

static HINSTANCE sspi_secur32_dll = NULL;
static PSecurityFunctionTable sspi_functions = NULL;
static const LPSTR SSPI_PACKAGE_NAME = "kerberos";

int sspi_init(
)
{
	// Load the secur32.dll library using its exact path. Passing the exact DLL path rather than allowing LoadLibrary to
	// search in different locations removes the possibility of DLL preloading attacks. We use GetSystemDirectoryA and
	// LoadLibraryA rather than the GetSystemDirectory/LoadLibrary aliases to ensure the ANSI versions are used so we
	// don't have to account for variations in char sizes if UNICODE is enabled.

	// Passing a 0 size will return the required buffer length to hold the path, including the null terminator.
	int requiredLen = GetSystemDirectoryA(NULL, 0);
	if (!requiredLen) {
		return GetLastError();
	}

	// Allocate a buffer to hold the system directory + "\secur32.dll" (length 12, not including null terminator).
	int actualLen = requiredLen + 12;
	char *directoryBuffer = (char *) calloc(1, actualLen);
	int directoryLen = GetSystemDirectoryA(directoryBuffer, actualLen);
	if (!directoryLen) {
		free(directoryBuffer);
		return GetLastError();
	}

	// Append the DLL name to the buffer.
	char *dllName = "\\secur32.dll";
	strcpy_s(&(directoryBuffer[directoryLen]), actualLen - directoryLen, dllName);

	sspi_secur32_dll = LoadLibraryA(directoryBuffer);
	free(directoryBuffer);
	if (!sspi_secur32_dll) {
		return GetLastError();
	}

    INIT_SECURITY_INTERFACE init_security_interface = (INIT_SECURITY_INTERFACE)GetProcAddress(sspi_secur32_dll, SECURITY_ENTRYPOINT);
    if (!init_security_interface) {
        return -1;
    }

    sspi_functions = (*init_security_interface)();
    if (!sspi_functions) {
        return -2;
    }

	return SSPI_OK;
}

int sspi_client_init(
    sspi_client_state *client,
    char* username,
    char* password
)
{
	TimeStamp timestamp;

    if (username) {
        if (password) {
            SEC_WINNT_AUTH_IDENTITY auth_identity;
            
        #ifdef _UNICODE
            auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
        #else
            auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
        #endif
            auth_identity.User = (LPSTR) username;
            auth_identity.UserLength = strlen(username);
            auth_identity.Password = (LPSTR) password;
            auth_identity.PasswordLength = strlen(password);
            auth_identity.Domain = NULL;
            auth_identity.DomainLength = 0;
            client->status = sspi_functions->AcquireCredentialsHandle(NULL, SSPI_PACKAGE_NAME, SECPKG_CRED_OUTBOUND, NULL, &auth_identity, NULL, NULL, &client->cred, &timestamp);
        } else {
            client->status = sspi_functions->AcquireCredentialsHandle(username, SSPI_PACKAGE_NAME, SECPKG_CRED_OUTBOUND, NULL, NULL, NULL, NULL, &client->cred, &timestamp);
        }
    } else {
        client->status = sspi_functions->AcquireCredentialsHandle(NULL, SSPI_PACKAGE_NAME, SECPKG_CRED_OUTBOUND, NULL, NULL, NULL, NULL, &client->cred, &timestamp);
    }

    if (client->status != SEC_E_OK) {
        return SSPI_ERROR;
    }

    return SSPI_OK;
}

int sspi_client_username(
    sspi_client_state *client,
    char** username
)
{
    SecPkgCredentials_Names names;
	client->status = sspi_functions->QueryCredentialsAttributes(&client->cred, SECPKG_CRED_ATTR_NAMES, &names);

	if (client->status != SEC_E_OK) {
		return SSPI_ERROR;
	}

	int len = strlen(names.sUserName) + 1;
	*username = malloc(len);
	memcpy(*username, names.sUserName, len);

	sspi_functions->FreeContextBuffer(names.sUserName);

    return SSPI_OK;
}

int sspi_client_negotiate(
    sspi_client_state *client,
    char* spn,
    PVOID input,
    ULONG input_length,
    PVOID* output,
    ULONG* output_length
)
{
    SecBufferDesc inbuf;
	SecBuffer in_bufs[1];
	SecBufferDesc outbuf;
	SecBuffer out_bufs[1];

	if (client->has_ctx > 0) {
		inbuf.ulVersion = SECBUFFER_VERSION;
		inbuf.cBuffers = 1;
		inbuf.pBuffers = in_bufs;
		in_bufs[0].pvBuffer = input;
		in_bufs[0].cbBuffer = input_length;
		in_bufs[0].BufferType = SECBUFFER_TOKEN;
	}

	outbuf.ulVersion = SECBUFFER_VERSION;
	outbuf.cBuffers = 1;
	outbuf.pBuffers = out_bufs;
	out_bufs[0].pvBuffer = NULL;
	out_bufs[0].cbBuffer = 0;
	out_bufs[0].BufferType = SECBUFFER_TOKEN;

	ULONG context_attr = 0;

	client->status = sspi_functions->InitializeSecurityContext(
        &client->cred,
        client->has_ctx > 0 ? &client->ctx : NULL,
        (LPSTR) spn,
        ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_MUTUAL_AUTH,
        0,
        SECURITY_NETWORK_DREP,
        client->has_ctx > 0 ? &inbuf : NULL,
        0,
        &client->ctx,
        &outbuf,
        &context_attr,
        NULL);

    if (client->status != SEC_E_OK && client->status != SEC_I_CONTINUE_NEEDED) {
        return SSPI_ERROR;
    }

    client->has_ctx = 1;

	*output = malloc(out_bufs[0].cbBuffer);
	*output_length = out_bufs[0].cbBuffer;
	memcpy(*output, out_bufs[0].pvBuffer, *output_length);
    sspi_functions->FreeContextBuffer(out_bufs[0].pvBuffer);

    if (client->status == SEC_I_CONTINUE_NEEDED) {
        return SSPI_CONTINUE;
    }

    return SSPI_OK;
}

int sspi_client_wrap_msg(
    sspi_client_state *client,
    PVOID input,
    ULONG input_length,
    PVOID* output,
    ULONG* output_length 
)
{
    SecPkgContext_Sizes sizes;

	client->status = sspi_functions->QueryContextAttributes(&client->ctx, SECPKG_ATTR_SIZES, &sizes);
	if (client->status != SEC_E_OK) {
		return SSPI_ERROR;
	}

	char *msg = malloc((sizes.cbSecurityTrailer + input_length + sizes.cbBlockSize) * sizeof(char));
	memcpy(&msg[sizes.cbSecurityTrailer], input, input_length);

	SecBuffer wrap_bufs[3];
	SecBufferDesc wrap_buf_desc;
	wrap_buf_desc.cBuffers = 3;
	wrap_buf_desc.pBuffers = wrap_bufs;
	wrap_buf_desc.ulVersion = SECBUFFER_VERSION;

	wrap_bufs[0].cbBuffer = sizes.cbSecurityTrailer;
	wrap_bufs[0].BufferType = SECBUFFER_TOKEN;
	wrap_bufs[0].pvBuffer = msg;

	wrap_bufs[1].cbBuffer = input_length;
	wrap_bufs[1].BufferType = SECBUFFER_DATA;
	wrap_bufs[1].pvBuffer = msg + sizes.cbSecurityTrailer;

	wrap_bufs[2].cbBuffer = sizes.cbBlockSize;
	wrap_bufs[2].BufferType = SECBUFFER_PADDING;
	wrap_bufs[2].pvBuffer = msg + sizes.cbSecurityTrailer + input_length;

	client->status = sspi_functions->EncryptMessage(&client->ctx, SECQOP_WRAP_NO_ENCRYPT, &wrap_buf_desc, 0);
	if (client->status != SEC_E_OK) {
		free(msg);
		return SSPI_ERROR;
	}

	*output_length = wrap_bufs[0].cbBuffer + wrap_bufs[1].cbBuffer + wrap_bufs[2].cbBuffer;
	*output = malloc(*output_length);

	memcpy(*output, wrap_bufs[0].pvBuffer, wrap_bufs[0].cbBuffer);
	memcpy(*output + wrap_bufs[0].cbBuffer, wrap_bufs[1].pvBuffer, wrap_bufs[1].cbBuffer);
	memcpy(*output + wrap_bufs[0].cbBuffer + wrap_bufs[1].cbBuffer, wrap_bufs[2].pvBuffer, wrap_bufs[2].cbBuffer);

	free(msg);

	return SSPI_OK;
}

int sspi_client_destroy(
    sspi_client_state *client
)
{
    if (client->has_ctx > 0) {
        sspi_functions->DeleteSecurityContext(&client->ctx);
    }

    sspi_functions->FreeCredentialsHandle(&client->cred);

    return SSPI_OK;
}