2 Unix SMB/CIFS implementation.
4 RFC2478 Compliant SPNEGO implementation
6 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "../libcli/auth/spnego.h"
25 #include "../lib/util/asn1.h"
27 static bool read_negTokenInit(struct asn1_data
*asn1
, TALLOC_CTX
*mem_ctx
,
28 struct spnego_negTokenInit
*token
)
32 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
33 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
35 while (!asn1
->has_error
&& 0 < asn1_tag_remaining(asn1
)) {
38 if (!asn1_peek_uint8(asn1
, &context
)) {
39 asn1
->has_error
= true;
46 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
47 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
49 token
->mechTypes
= talloc(NULL
, const char *);
50 for (i
= 0; !asn1
->has_error
&&
51 0 < asn1_tag_remaining(asn1
); i
++) {
53 token
->mechTypes
= talloc_realloc(NULL
,
56 asn1_read_OID(asn1
, token
->mechTypes
, &oid
);
57 token
->mechTypes
[i
] = oid
;
59 token
->mechTypes
[i
] = NULL
;
66 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
67 asn1_read_BitString(asn1
, mem_ctx
, &token
->reqFlags
,
68 &token
->reqFlagsPadding
);
73 asn1_start_tag(asn1
, ASN1_CONTEXT(2));
74 asn1_read_OctetString(asn1
, mem_ctx
, &token
->mechToken
);
81 asn1_start_tag(asn1
, ASN1_CONTEXT(3));
82 if (!asn1_peek_uint8(asn1
, &type_peek
)) {
83 asn1
->has_error
= true;
86 if (type_peek
== ASN1_OCTET_STRING
) {
87 asn1_read_OctetString(asn1
, mem_ctx
,
90 /* RFC 2478 says we have an Octet String here,
91 but W2k sends something different... */
93 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
94 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
95 asn1_read_GeneralString(asn1
, mem_ctx
, &mechListMIC
);
99 token
->targetPrincipal
= mechListMIC
;
105 asn1
->has_error
= true;
113 return !asn1
->has_error
;
116 static bool write_negTokenInit(struct asn1_data
*asn1
, struct spnego_negTokenInit
*token
)
118 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
119 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
121 /* Write mechTypes */
122 if (token
->mechTypes
&& *token
->mechTypes
) {
125 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
126 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
127 for (i
= 0; token
->mechTypes
[i
]; i
++) {
128 asn1_write_OID(asn1
, token
->mechTypes
[i
]);
135 if (token
->reqFlags
.length
> 0) {
136 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
137 asn1_write_BitString(asn1
, token
->reqFlags
.data
,
138 token
->reqFlags
.length
,
139 token
->reqFlagsPadding
);
143 /* write mechToken */
144 if (token
->mechToken
.data
) {
145 asn1_push_tag(asn1
, ASN1_CONTEXT(2));
146 asn1_write_OctetString(asn1
, token
->mechToken
.data
,
147 token
->mechToken
.length
);
151 /* write mechListMIC */
152 if (token
->mechListMIC
.data
) {
153 asn1_push_tag(asn1
, ASN1_CONTEXT(3));
155 /* This is what RFC 2478 says ... */
156 asn1_write_OctetString(asn1
, token
->mechListMIC
.data
,
157 token
->mechListMIC
.length
);
159 /* ... but unfortunately this is what Windows
161 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
162 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
163 asn1_push_tag(asn1
, ASN1_GENERAL_STRING
);
164 asn1_write(asn1
, token
->mechListMIC
.data
,
165 token
->mechListMIC
.length
);
176 return !asn1
->has_error
;
179 static bool read_negTokenTarg(struct asn1_data
*asn1
, TALLOC_CTX
*mem_ctx
,
180 struct spnego_negTokenTarg
*token
)
184 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
185 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
187 while (!asn1
->has_error
&& 0 < asn1_tag_remaining(asn1
)) {
190 if (!asn1_peek_uint8(asn1
, &context
)) {
191 asn1
->has_error
= true;
196 case ASN1_CONTEXT(0):
197 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
198 asn1_start_tag(asn1
, ASN1_ENUMERATED
);
199 asn1_read_uint8(asn1
, &token
->negResult
);
203 case ASN1_CONTEXT(1):
204 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
205 asn1_read_OID(asn1
, mem_ctx
, &oid
);
206 token
->supportedMech
= oid
;
209 case ASN1_CONTEXT(2):
210 asn1_start_tag(asn1
, ASN1_CONTEXT(2));
211 asn1_read_OctetString(asn1
, mem_ctx
, &token
->responseToken
);
214 case ASN1_CONTEXT(3):
215 asn1_start_tag(asn1
, ASN1_CONTEXT(3));
216 asn1_read_OctetString(asn1
, mem_ctx
, &token
->mechListMIC
);
220 asn1
->has_error
= true;
228 return !asn1
->has_error
;
231 static bool write_negTokenTarg(struct asn1_data
*asn1
, struct spnego_negTokenTarg
*token
)
233 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
234 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
236 if (token
->negResult
!= SPNEGO_NONE_RESULT
) {
237 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
238 asn1_write_enumerated(asn1
, token
->negResult
);
242 if (token
->supportedMech
) {
243 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
244 asn1_write_OID(asn1
, token
->supportedMech
);
248 if (token
->responseToken
.data
) {
249 asn1_push_tag(asn1
, ASN1_CONTEXT(2));
250 asn1_write_OctetString(asn1
, token
->responseToken
.data
,
251 token
->responseToken
.length
);
255 if (token
->mechListMIC
.data
) {
256 asn1_push_tag(asn1
, ASN1_CONTEXT(3));
257 asn1_write_OctetString(asn1
, token
->mechListMIC
.data
,
258 token
->mechListMIC
.length
);
265 return !asn1
->has_error
;
268 ssize_t
spnego_read_data(TALLOC_CTX
*mem_ctx
, DATA_BLOB data
, struct spnego_data
*token
)
270 struct asn1_data
*asn1
;
276 if (data
.length
== 0) {
280 asn1
= asn1_init(mem_ctx
);
285 asn1_load(asn1
, data
);
287 if (!asn1_peek_uint8(asn1
, &context
)) {
288 asn1
->has_error
= true;
291 case ASN1_APPLICATION(0):
292 asn1_start_tag(asn1
, ASN1_APPLICATION(0));
293 asn1_check_OID(asn1
, OID_SPNEGO
);
294 if (read_negTokenInit(asn1
, mem_ctx
, &token
->negTokenInit
)) {
295 token
->type
= SPNEGO_NEG_TOKEN_INIT
;
299 case ASN1_CONTEXT(1):
300 if (read_negTokenTarg(asn1
, mem_ctx
, &token
->negTokenTarg
)) {
301 token
->type
= SPNEGO_NEG_TOKEN_TARG
;
305 asn1
->has_error
= true;
310 if (!asn1
->has_error
) ret
= asn1
->ofs
;
316 ssize_t
spnego_write_data(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, struct spnego_data
*spnego
)
318 struct asn1_data
*asn1
= asn1_init(mem_ctx
);
325 switch (spnego
->type
) {
326 case SPNEGO_NEG_TOKEN_INIT
:
327 asn1_push_tag(asn1
, ASN1_APPLICATION(0));
328 asn1_write_OID(asn1
, OID_SPNEGO
);
329 write_negTokenInit(asn1
, &spnego
->negTokenInit
);
332 case SPNEGO_NEG_TOKEN_TARG
:
333 write_negTokenTarg(asn1
, &spnego
->negTokenTarg
);
336 asn1
->has_error
= true;
340 if (!asn1
->has_error
) {
341 *blob
= data_blob_talloc(mem_ctx
, asn1
->data
, asn1
->length
);
349 bool spnego_free_data(struct spnego_data
*spnego
)
353 if (!spnego
) goto out
;
355 switch(spnego
->type
) {
356 case SPNEGO_NEG_TOKEN_INIT
:
357 if (spnego
->negTokenInit
.mechTypes
) {
358 talloc_free(spnego
->negTokenInit
.mechTypes
);
360 data_blob_free(&spnego
->negTokenInit
.reqFlags
);
361 data_blob_free(&spnego
->negTokenInit
.mechToken
);
362 data_blob_free(&spnego
->negTokenInit
.mechListMIC
);
363 talloc_free(spnego
->negTokenInit
.targetPrincipal
);
365 case SPNEGO_NEG_TOKEN_TARG
:
366 if (spnego
->negTokenTarg
.supportedMech
) {
367 talloc_free(discard_const(spnego
->negTokenTarg
.supportedMech
));
369 data_blob_free(&spnego
->negTokenTarg
.responseToken
);
370 data_blob_free(&spnego
->negTokenTarg
.mechListMIC
);
376 ZERO_STRUCTP(spnego
);
381 bool spnego_write_mech_types(TALLOC_CTX
*mem_ctx
,
382 const char **mech_types
,
385 struct asn1_data
*asn1
= asn1_init(mem_ctx
);
391 /* Write mechTypes */
392 if (mech_types
&& *mech_types
) {
395 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
396 for (i
= 0; mech_types
[i
]; i
++) {
397 asn1_write_OID(asn1
, mech_types
[i
]);
402 if (asn1
->has_error
) {
407 *blob
= data_blob_talloc(mem_ctx
, asn1
->data
, asn1
->length
);
408 if (blob
->length
!= asn1
->length
) {