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 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(0))) return false;
33 if (!asn1_start_tag(asn1
, ASN1_SEQUENCE(0))) return false;
35 while (!asn1
->has_error
&& 0 < asn1_tag_remaining(asn1
)) {
39 if (!asn1_peek_uint8(asn1
, &context
)) {
40 asn1
->has_error
= true;
46 case ASN1_CONTEXT(0): {
47 const char **mechTypes
;
49 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(0))) return false;
50 if (!asn1_start_tag(asn1
, ASN1_SEQUENCE(0))) return false;
52 mechTypes
= talloc(mem_ctx
, const char *);
53 if (mechTypes
== NULL
) {
54 asn1
->has_error
= true;
57 for (i
= 0; !asn1
->has_error
&&
58 0 < asn1_tag_remaining(asn1
); i
++) {
61 p
= talloc_realloc(mem_ctx
,
65 talloc_free(mechTypes
);
66 asn1
->has_error
= true;
71 if (!asn1_read_OID(asn1
, mechTypes
, &oid
)) return false;
75 token
->mechTypes
= mechTypes
;
83 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(1))) return false;
84 if (!asn1_read_BitString(asn1
, mem_ctx
, &token
->reqFlags
,
85 &token
->reqFlagsPadding
)) return false;
86 if (!asn1_end_tag(asn1
)) return false;
90 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(2))) return false;
91 if (!asn1_read_OctetString(asn1
, mem_ctx
, &token
->mechToken
)) return false;
92 if (!asn1_end_tag(asn1
)) return false;
98 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(3))) return false;
99 if (!asn1_peek_uint8(asn1
, &type_peek
)) {
100 asn1
->has_error
= true;
103 if (type_peek
== ASN1_OCTET_STRING
) {
104 if (!asn1_read_OctetString(asn1
, mem_ctx
,
105 &token
->mechListMIC
)) return false;
107 /* RFC 2478 says we have an Octet String here,
108 but W2k sends something different... */
110 if (!asn1_start_tag(asn1
, ASN1_SEQUENCE(0))) return false;
111 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(0))) return false;
112 if (!asn1_read_GeneralString(asn1
, mem_ctx
, &mechListMIC
)) return false;
113 if (!asn1_end_tag(asn1
)) return false;
114 if (!asn1_end_tag(asn1
)) return false;
116 token
->targetPrincipal
= mechListMIC
;
118 if (!asn1_end_tag(asn1
)) return false;
122 asn1
->has_error
= true;
127 if (!asn1_end_tag(asn1
)) return false;
128 if (!asn1_end_tag(asn1
)) return false;
130 return !asn1
->has_error
;
133 static bool write_negTokenInit(struct asn1_data
*asn1
, struct spnego_negTokenInit
*token
)
135 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(0))) return false;
136 if (!asn1_push_tag(asn1
, ASN1_SEQUENCE(0))) return false;
138 /* Write mechTypes */
139 if (token
->mechTypes
&& *token
->mechTypes
) {
142 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(0))) return false;
143 if (!asn1_push_tag(asn1
, ASN1_SEQUENCE(0))) return false;
144 for (i
= 0; token
->mechTypes
[i
]; i
++) {
145 if (!asn1_write_OID(asn1
, token
->mechTypes
[i
])) return false;
147 if (!asn1_pop_tag(asn1
)) return false;
148 if (!asn1_pop_tag(asn1
)) return false;
152 if (token
->reqFlags
.length
> 0) {
153 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(1))) return false;
154 if (!asn1_write_BitString(asn1
, token
->reqFlags
.data
,
155 token
->reqFlags
.length
,
156 token
->reqFlagsPadding
)) return false;
157 if (!asn1_pop_tag(asn1
)) return false;
160 /* write mechToken */
161 if (token
->mechToken
.data
) {
162 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(2))) return false;
163 if (!asn1_write_OctetString(asn1
, token
->mechToken
.data
,
164 token
->mechToken
.length
)) return false;
165 if (!asn1_pop_tag(asn1
)) return false;
168 /* write mechListMIC */
169 if (token
->mechListMIC
.data
) {
170 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(3))) return false;
172 /* This is what RFC 2478 says ... */
173 asn1_write_OctetString(asn1
, token
->mechListMIC
.data
,
174 token
->mechListMIC
.length
);
176 /* ... but unfortunately this is what Windows
178 if (!asn1_push_tag(asn1
, ASN1_SEQUENCE(0))) return false;
179 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(0))) return false;
180 if (!asn1_push_tag(asn1
, ASN1_GENERAL_STRING
)) return false;
181 if (!asn1_write(asn1
, token
->mechListMIC
.data
,
182 token
->mechListMIC
.length
)) return false;
183 if (!asn1_pop_tag(asn1
)) return false;
184 if (!asn1_pop_tag(asn1
)) return false;
185 if (!asn1_pop_tag(asn1
)) return false;
187 if (!asn1_pop_tag(asn1
)) return false;
190 if (!asn1_pop_tag(asn1
)) return false;
191 if (!asn1_pop_tag(asn1
)) return false;
193 return !asn1
->has_error
;
196 static bool read_negTokenTarg(struct asn1_data
*asn1
, TALLOC_CTX
*mem_ctx
,
197 struct spnego_negTokenTarg
*token
)
201 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(1))) return false;
202 if (!asn1_start_tag(asn1
, ASN1_SEQUENCE(0))) return false;
204 while (!asn1
->has_error
&& 0 < asn1_tag_remaining(asn1
)) {
207 if (!asn1_peek_uint8(asn1
, &context
)) {
208 asn1
->has_error
= true;
213 case ASN1_CONTEXT(0):
214 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(0))) return false;
215 if (!asn1_start_tag(asn1
, ASN1_ENUMERATED
)) return false;
216 if (!asn1_read_uint8(asn1
, &token
->negResult
)) return false;
217 if (!asn1_end_tag(asn1
)) return false;
218 if (!asn1_end_tag(asn1
)) return false;
220 case ASN1_CONTEXT(1):
221 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(1))) return false;
222 if (!asn1_read_OID(asn1
, mem_ctx
, &oid
)) return false;
223 token
->supportedMech
= oid
;
224 if (!asn1_end_tag(asn1
)) return false;
226 case ASN1_CONTEXT(2):
227 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(2))) return false;
228 if (!asn1_read_OctetString(asn1
, mem_ctx
, &token
->responseToken
)) return false;
229 if (!asn1_end_tag(asn1
)) return false;
231 case ASN1_CONTEXT(3):
232 if (!asn1_start_tag(asn1
, ASN1_CONTEXT(3))) return false;
233 if (!asn1_read_OctetString(asn1
, mem_ctx
, &token
->mechListMIC
)) return false;
234 if (!asn1_end_tag(asn1
)) return false;
237 asn1
->has_error
= true;
242 if (!asn1_end_tag(asn1
)) return false;
243 if (!asn1_end_tag(asn1
)) return false;
245 return !asn1
->has_error
;
248 static bool write_negTokenTarg(struct asn1_data
*asn1
, struct spnego_negTokenTarg
*token
)
250 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(1))) return false;
251 if (!asn1_push_tag(asn1
, ASN1_SEQUENCE(0))) return false;
253 if (token
->negResult
!= SPNEGO_NONE_RESULT
) {
254 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(0))) return false;
255 if (!asn1_write_enumerated(asn1
, token
->negResult
)) return false;
256 if (!asn1_pop_tag(asn1
)) return false;
259 if (token
->supportedMech
) {
260 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(1))) return false;
261 if (!asn1_write_OID(asn1
, token
->supportedMech
)) return false;
262 if (!asn1_pop_tag(asn1
)) return false;
265 if (token
->responseToken
.data
) {
266 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(2))) return false;
267 if (!asn1_write_OctetString(asn1
, token
->responseToken
.data
,
268 token
->responseToken
.length
)) return false;
269 if (!asn1_pop_tag(asn1
)) return false;
272 if (token
->mechListMIC
.data
) {
273 if (!asn1_push_tag(asn1
, ASN1_CONTEXT(3))) return false;
274 if (!asn1_write_OctetString(asn1
, token
->mechListMIC
.data
,
275 token
->mechListMIC
.length
)) return false;
276 if (!asn1_pop_tag(asn1
)) return false;
279 if (!asn1_pop_tag(asn1
)) return false;
280 if (!asn1_pop_tag(asn1
)) return false;
282 return !asn1
->has_error
;
285 ssize_t
spnego_read_data(TALLOC_CTX
*mem_ctx
, DATA_BLOB data
, struct spnego_data
*token
)
287 struct asn1_data
*asn1
;
293 if (data
.length
== 0) {
297 asn1
= asn1_init(mem_ctx
);
302 if (!asn1_load(asn1
, data
)) goto err
;
304 if (!asn1_peek_uint8(asn1
, &context
)) {
305 asn1
->has_error
= true;
308 case ASN1_APPLICATION(0):
309 if (!asn1_start_tag(asn1
, ASN1_APPLICATION(0))) goto err
;
310 if (!asn1_check_OID(asn1
, OID_SPNEGO
)) goto err
;
311 if (read_negTokenInit(asn1
, mem_ctx
, &token
->negTokenInit
)) {
312 token
->type
= SPNEGO_NEG_TOKEN_INIT
;
314 if (!asn1_end_tag(asn1
)) goto err
;
316 case ASN1_CONTEXT(1):
317 if (read_negTokenTarg(asn1
, mem_ctx
, &token
->negTokenTarg
)) {
318 token
->type
= SPNEGO_NEG_TOKEN_TARG
;
322 asn1
->has_error
= true;
327 if (!asn1
->has_error
) ret
= asn1
->ofs
;
336 ssize_t
spnego_write_data(TALLOC_CTX
*mem_ctx
, DATA_BLOB
*blob
, struct spnego_data
*spnego
)
338 struct asn1_data
*asn1
= asn1_init(mem_ctx
);
345 switch (spnego
->type
) {
346 case SPNEGO_NEG_TOKEN_INIT
:
347 if (!asn1_push_tag(asn1
, ASN1_APPLICATION(0))) goto err
;
348 if (!asn1_write_OID(asn1
, OID_SPNEGO
)) goto err
;
349 if (!write_negTokenInit(asn1
, &spnego
->negTokenInit
)) goto err
;
350 if (!asn1_pop_tag(asn1
)) goto err
;
352 case SPNEGO_NEG_TOKEN_TARG
:
353 write_negTokenTarg(asn1
, &spnego
->negTokenTarg
);
356 asn1
->has_error
= true;
360 if (!asn1
->has_error
) {
361 *blob
= data_blob_talloc(mem_ctx
, asn1
->data
, asn1
->length
);
372 bool spnego_free_data(struct spnego_data
*spnego
)
376 if (!spnego
) goto out
;
378 switch(spnego
->type
) {
379 case SPNEGO_NEG_TOKEN_INIT
:
380 if (spnego
->negTokenInit
.mechTypes
) {
381 talloc_free(discard_const(spnego
->negTokenInit
.mechTypes
));
383 data_blob_free(&spnego
->negTokenInit
.reqFlags
);
384 data_blob_free(&spnego
->negTokenInit
.mechToken
);
385 data_blob_free(&spnego
->negTokenInit
.mechListMIC
);
386 talloc_free(spnego
->negTokenInit
.targetPrincipal
);
388 case SPNEGO_NEG_TOKEN_TARG
:
389 if (spnego
->negTokenTarg
.supportedMech
) {
390 talloc_free(discard_const(spnego
->negTokenTarg
.supportedMech
));
392 data_blob_free(&spnego
->negTokenTarg
.responseToken
);
393 data_blob_free(&spnego
->negTokenTarg
.mechListMIC
);
399 ZERO_STRUCTP(spnego
);
404 bool spnego_write_mech_types(TALLOC_CTX
*mem_ctx
,
405 const char * const *mech_types
,
409 struct asn1_data
*asn1
= asn1_init(mem_ctx
);
415 /* Write mechTypes */
416 if (mech_types
&& *mech_types
) {
419 if (!asn1_push_tag(asn1
, ASN1_SEQUENCE(0))) goto err
;
420 for (i
= 0; mech_types
[i
]; i
++) {
421 if (!asn1_write_OID(asn1
, mech_types
[i
])) goto err
;
423 if (!asn1_pop_tag(asn1
)) goto err
;
426 if (asn1
->has_error
) {
430 *blob
= data_blob_talloc(mem_ctx
, asn1
->data
, asn1
->length
);
431 if (blob
->length
!= asn1
->length
) {