summaryrefslogtreecommitdiff
path: root/source3/rpc_server/dcesrv_spnego.c
blob: fb758e338b84ffa970c561db71b3b37ad6d2689f (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *  SPNEGO Encapsulation
 *  DCERPC Server functions
 *  Copyright (C) Simo Sorce 2010.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "includes.h"
#include "../libcli/auth/spnego.h"
#include "dcesrv_ntlmssp.h"
#include "dcesrv_gssapi.h"
#include "dcesrv_spnego.h"

static NTSTATUS spnego_init_server(TALLOC_CTX *mem_ctx,
				   bool do_sign, bool do_seal,
				   bool is_dcerpc,
				   struct spnego_context **spnego_ctx)
{
	struct spnego_context *sp_ctx = NULL;

	sp_ctx = talloc_zero(mem_ctx, struct spnego_context);
	if (!sp_ctx) {
		return NT_STATUS_NO_MEMORY;
	}

	sp_ctx->do_sign = do_sign;
	sp_ctx->do_seal = do_seal;
	sp_ctx->is_dcerpc = is_dcerpc;

	*spnego_ctx = sp_ctx;
	return NT_STATUS_OK;
}

static NTSTATUS spnego_server_mech_init(struct spnego_context *sp_ctx,
					DATA_BLOB *token_in,
					DATA_BLOB *token_out)
{
	struct auth_ntlmssp_state *ntlmssp_ctx;
	struct gse_context *gse_ctx;
	NTSTATUS status;

	switch (sp_ctx->mech) {
	case SPNEGO_KRB5:
		status = gssapi_server_auth_start(sp_ctx,
						  sp_ctx->do_sign,
						  sp_ctx->do_seal,
						  sp_ctx->is_dcerpc,
						  token_in,
						  token_out,
						  &gse_ctx);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0, ("Failed to init gssapi server "
				  "(%s)\n", nt_errstr(status)));
			return status;
		}

		sp_ctx->mech_ctx.gssapi_state = gse_ctx;
		break;

	case SPNEGO_NTLMSSP:
		status = ntlmssp_server_auth_start(sp_ctx,
						   sp_ctx->do_sign,
						   sp_ctx->do_seal,
						   sp_ctx->is_dcerpc,
						   token_in,
						   token_out,
						   &ntlmssp_ctx);
		if (!NT_STATUS_IS_OK(status)) {
			DEBUG(0, ("Failed to init ntlmssp server "
				  "(%s)\n", nt_errstr(status)));
			return status;
		}

		sp_ctx->mech_ctx.ntlmssp_state = ntlmssp_ctx;
		break;

	default:
		DEBUG(3, ("No known mechanisms available\n"));
		return NT_STATUS_INVALID_PARAMETER;
	}

	return NT_STATUS_OK;
}

NTSTATUS spnego_server_step(struct spnego_context *sp_ctx,
			    TALLOC_CTX *mem_ctx,
			    DATA_BLOB *spnego_in,
			    DATA_BLOB *spnego_out)
{
	DATA_BLOB token_in = data_blob_null;
	DATA_BLOB token_out = data_blob_null;
	DATA_BLOB signature = data_blob_null;
	DATA_BLOB MICblob = data_blob_null;
	struct spnego_data sp_in;
	ssize_t len_in = 0;
	NTSTATUS status;
	bool ret;

	len_in = spnego_read_data(mem_ctx, *spnego_in, &sp_in);
	if (len_in == -1) {
		DEBUG(1, (__location__ ": invalid SPNEGO blob.\n"));
		dump_data(10, spnego_in->data, spnego_in->length);
		status = NT_STATUS_INVALID_PARAMETER;
		sp_ctx->state = SPNEGO_CONV_AUTH_DONE;
		goto done;
	}
	if (sp_in.type != SPNEGO_NEG_TOKEN_TARG) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}
	token_in = sp_in.negTokenTarg.responseToken;
	signature = sp_in.negTokenTarg.mechListMIC;

	switch (sp_ctx->state) {
	case SPNEGO_CONV_NEGO:
		/* still to initialize */
		status = spnego_server_mech_init(sp_ctx,
						 &token_in,
						 &token_out);
		if (!NT_STATUS_IS_OK(status)) {
			goto done;
		}
		/* server always need at least one reply from client */
		status = NT_STATUS_MORE_PROCESSING_REQUIRED;
		sp_ctx->state = SPNEGO_CONV_AUTH_MORE;
		break;

	case SPNEGO_CONV_AUTH_MORE:

		switch(sp_ctx->mech) {
		case SPNEGO_KRB5:
			status = gssapi_server_step(
					sp_ctx->mech_ctx.gssapi_state,
					mem_ctx, &token_in, &token_out);
			break;
		case SPNEGO_NTLMSSP:
			status = ntlmssp_server_step(
					sp_ctx->mech_ctx.ntlmssp_state,
					mem_ctx, &token_in, &token_out);
			break;
		default:
			status = NT_STATUS_INVALID_PARAMETER;
			goto done;
		}

		break;

	case SPNEGO_CONV_AUTH_DONE:
		/* we are already done, can't step further */
		/* fall thorugh and return error */
	default:
		/* wrong case */
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (NT_STATUS_IS_OK(status) && signature.length != 0) {
		/* last packet and requires signature check */
		ret = spnego_mech_list_blob(talloc_tos(),
					    sp_ctx->oid_list, &MICblob);
		if (ret) {
			status = spnego_sigcheck(talloc_tos(), sp_ctx,
						 &MICblob, &MICblob,
						 &signature);
		} else {
			status = NT_STATUS_UNSUCCESSFUL;
		}
	}
	if (NT_STATUS_IS_OK(status) && signature.length != 0) {
		/* if signature was good, sign our own packet too */
		status = spnego_sign(talloc_tos(), sp_ctx,
				     &MICblob, &MICblob, &signature);
	}

done:
	*spnego_out = spnego_gen_auth_response_and_mic(mem_ctx, status,
							sp_ctx->mech_oid,
							&token_out,
							&signature);
	if (!spnego_out->data) {
		DEBUG(1, ("SPNEGO wrapping failed!\n"));
		status = NT_STATUS_UNSUCCESSFUL;
	}

	if (NT_STATUS_IS_OK(status) ||
	    !NT_STATUS_EQUAL(status,
			NT_STATUS_MORE_PROCESSING_REQUIRED)) {
		sp_ctx->state = SPNEGO_CONV_AUTH_DONE;
	}

	data_blob_free(&token_in);
	data_blob_free(&token_out);
	return status;
}

NTSTATUS spnego_server_auth_start(TALLOC_CTX *mem_ctx,
				  bool do_sign,
				  bool do_seal,
				  bool is_dcerpc,
				  DATA_BLOB *spnego_in,
				  DATA_BLOB *spnego_out,
				  struct spnego_context **spnego_ctx)
{
	struct spnego_context *sp_ctx;
	DATA_BLOB token_in = data_blob_null;
	DATA_BLOB token_out = data_blob_null;
	unsigned int i;
	NTSTATUS status;
	bool ret;

	if (!spnego_in->length) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	status = spnego_init_server(mem_ctx, do_sign, do_seal, is_dcerpc, &sp_ctx);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	ret = spnego_parse_negTokenInit(sp_ctx, *spnego_in,
					sp_ctx->oid_list, NULL, &token_in);
	if (!ret || sp_ctx->oid_list[0] == NULL) {
		DEBUG(3, ("Invalid SPNEGO message\n"));
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}

	/* try for krb auth first */
	for (i = 0; sp_ctx->oid_list[i] && sp_ctx->mech == SPNEGO_NONE; i++) {
		if (strcmp(OID_KERBEROS5, sp_ctx->oid_list[i]) == 0 ||
		    strcmp(OID_KERBEROS5_OLD, sp_ctx->oid_list[i]) == 0) {

			if (lp_security() == SEC_ADS || USE_KERBEROS_KEYTAB) {
				sp_ctx->mech = SPNEGO_KRB5;
				sp_ctx->mech_oid = sp_ctx->oid_list[i];
			}
		}
	}

	/* if auth type still undetermined, try for NTLMSSP */
	for (i = 0; sp_ctx->oid_list[i] && sp_ctx->mech == SPNEGO_NONE; i++) {
		if (strcmp(OID_NTLMSSP, sp_ctx->oid_list[i]) == 0) {
			sp_ctx->mech = SPNEGO_NTLMSSP;
			sp_ctx->mech_oid = sp_ctx->oid_list[i];
		}
	}

	if (!sp_ctx->mech_oid) {
		DEBUG(3, ("No known mechanisms available\n"));
		status = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}

	if (DEBUGLEVEL >= 10) {
		DEBUG(10, ("Client Provided OIDs:\n"));
		for (i = 0; sp_ctx->oid_list[i]; i++) {
			DEBUG(10, ("  %d: %s\n", i, sp_ctx->oid_list[i]));
		}
		DEBUG(10, ("Chosen OID: %s\n", sp_ctx->mech_oid));
	}

	/* If it is not the first OID, then token_in is not valid for the
	 * choosen mech */
	if (sp_ctx->mech_oid != sp_ctx->oid_list[0]) {
		/* request more and send back empty token */
		status = NT_STATUS_MORE_PROCESSING_REQUIRED;
		sp_ctx->state = SPNEGO_CONV_NEGO;
		goto done;
	}

	status = spnego_server_mech_init(sp_ctx, &token_in, &token_out);
	if (!NT_STATUS_IS_OK(status)) {
		goto done;
	}

	DEBUG(10, ("SPNEGO(%d) auth started\n", sp_ctx->mech));

	/* server always need at least one reply from client */
	status = NT_STATUS_MORE_PROCESSING_REQUIRED;
	sp_ctx->state = SPNEGO_CONV_AUTH_MORE;

done:
	*spnego_out = spnego_gen_auth_response(mem_ctx, &token_out,
						status, sp_ctx->mech_oid);
	if (!spnego_out->data) {
		status = NT_STATUS_INVALID_PARAMETER;
		TALLOC_FREE(sp_ctx);
	} else {
		status = NT_STATUS_OK;
		*spnego_ctx = sp_ctx;
	}

	data_blob_free(&token_in);
	data_blob_free(&token_out);

	return status;
}