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/>.
26 #define DBGC_CLASS DBGC_AUTH
28 static bool read_negTokenInit(ASN1_DATA
*asn1
, negTokenInit_t
*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 switch (asn1
->data
[asn1
->ofs
]) {
41 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
42 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
44 token
->mechTypes
= SMB_MALLOC_P(const char *);
45 for (i
= 0; !asn1
->has_error
&&
46 0 < asn1_tag_remaining(asn1
); i
++) {
49 SMB_REALLOC_ARRAY(token
->mechTypes
, const char *, i
+ 2);
50 if (!token
->mechTypes
) {
51 asn1
->has_error
= True
;
54 asn1_read_OID(asn1
, &p_oid
);
55 token
->mechTypes
[i
] = p_oid
;
57 token
->mechTypes
[i
] = NULL
;
64 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
65 asn1_read_Integer(asn1
, &token
->reqFlags
);
66 token
->reqFlags
|= SPNEGO_REQ_FLAG
;
71 asn1_start_tag(asn1
, ASN1_CONTEXT(2));
72 asn1_read_OctetString(asn1
, &token
->mechToken
);
77 asn1_start_tag(asn1
, ASN1_CONTEXT(3));
78 if (asn1
->data
[asn1
->ofs
] == ASN1_OCTET_STRING
) {
79 asn1_read_OctetString(asn1
,
82 /* RFC 2478 says we have an Octet String here,
83 but W2k sends something different... */
85 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
86 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
87 asn1_read_GeneralString(asn1
, &mechListMIC
);
92 data_blob(mechListMIC
, strlen(mechListMIC
));
93 SAFE_FREE(mechListMIC
);
98 asn1
->has_error
= True
;
106 return !asn1
->has_error
;
109 static bool write_negTokenInit(ASN1_DATA
*asn1
, negTokenInit_t
*token
)
111 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
112 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
114 /* Write mechTypes */
115 if (token
->mechTypes
&& *token
->mechTypes
) {
118 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
119 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
120 for (i
= 0; token
->mechTypes
[i
]; i
++) {
121 asn1_write_OID(asn1
, token
->mechTypes
[i
]);
128 if (token
->reqFlags
& SPNEGO_REQ_FLAG
) {
129 int flags
= token
->reqFlags
& ~SPNEGO_REQ_FLAG
;
131 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
132 asn1_write_Integer(asn1
, flags
);
136 /* write mechToken */
137 if (token
->mechToken
.data
) {
138 asn1_push_tag(asn1
, ASN1_CONTEXT(2));
139 asn1_write_OctetString(asn1
, token
->mechToken
.data
,
140 token
->mechToken
.length
);
144 /* write mechListMIC */
145 if (token
->mechListMIC
.data
) {
146 asn1_push_tag(asn1
, ASN1_CONTEXT(3));
148 /* This is what RFC 2478 says ... */
149 asn1_write_OctetString(asn1
, token
->mechListMIC
.data
,
150 token
->mechListMIC
.length
);
152 /* ... but unfortunately this is what Windows
154 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
155 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
156 asn1_push_tag(asn1
, ASN1_GENERAL_STRING
);
157 asn1_write(asn1
, token
->mechListMIC
.data
,
158 token
->mechListMIC
.length
);
169 return !asn1
->has_error
;
172 static bool read_negTokenTarg(ASN1_DATA
*asn1
, negTokenTarg_t
*token
)
176 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
177 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
179 while (!asn1
->has_error
&& 0 < asn1_tag_remaining(asn1
)) {
180 switch (asn1
->data
[asn1
->ofs
]) {
181 case ASN1_CONTEXT(0):
182 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
183 asn1_start_tag(asn1
, ASN1_ENUMERATED
);
184 asn1_read_uint8(asn1
, &token
->negResult
);
188 case ASN1_CONTEXT(1):
189 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
190 asn1_read_OID(asn1
, &token
->supportedMech
);
193 case ASN1_CONTEXT(2):
194 asn1_start_tag(asn1
, ASN1_CONTEXT(2));
195 asn1_read_OctetString(asn1
, &token
->responseToken
);
198 case ASN1_CONTEXT(3):
199 asn1_start_tag(asn1
, ASN1_CONTEXT(3));
200 asn1_read_OctetString(asn1
, &token
->mechListMIC
);
204 asn1
->has_error
= True
;
212 return !asn1
->has_error
;
215 static bool write_negTokenTarg(ASN1_DATA
*asn1
, negTokenTarg_t
*token
)
217 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
218 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
220 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
221 asn1_write_enumerated(asn1
, token
->negResult
);
224 if (token
->supportedMech
) {
225 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
226 asn1_write_OID(asn1
, token
->supportedMech
);
230 if (token
->responseToken
.data
) {
231 asn1_push_tag(asn1
, ASN1_CONTEXT(2));
232 asn1_write_OctetString(asn1
, token
->responseToken
.data
,
233 token
->responseToken
.length
);
237 if (token
->mechListMIC
.data
) {
238 asn1_push_tag(asn1
, ASN1_CONTEXT(3));
239 asn1_write_OctetString(asn1
, token
->mechListMIC
.data
,
240 token
->mechListMIC
.length
);
247 return !asn1
->has_error
;
250 ssize_t
read_spnego_data(DATA_BLOB data
, SPNEGO_DATA
*token
)
257 asn1_load(&asn1
, data
);
259 switch (asn1
.data
[asn1
.ofs
]) {
260 case ASN1_APPLICATION(0):
261 asn1_start_tag(&asn1
, ASN1_APPLICATION(0));
262 asn1_check_OID(&asn1
, OID_SPNEGO
);
263 if (read_negTokenInit(&asn1
, &token
->negTokenInit
)) {
264 token
->type
= SPNEGO_NEG_TOKEN_INIT
;
268 case ASN1_CONTEXT(1):
269 if (read_negTokenTarg(&asn1
, &token
->negTokenTarg
)) {
270 token
->type
= SPNEGO_NEG_TOKEN_TARG
;
277 if (!asn1
.has_error
) ret
= asn1
.ofs
;
283 ssize_t
write_spnego_data(DATA_BLOB
*blob
, SPNEGO_DATA
*spnego
)
290 switch (spnego
->type
) {
291 case SPNEGO_NEG_TOKEN_INIT
:
292 asn1_push_tag(&asn1
, ASN1_APPLICATION(0));
293 asn1_write_OID(&asn1
, OID_SPNEGO
);
294 write_negTokenInit(&asn1
, &spnego
->negTokenInit
);
297 case SPNEGO_NEG_TOKEN_TARG
:
298 write_negTokenTarg(&asn1
, &spnego
->negTokenTarg
);
301 asn1
.has_error
= True
;
305 if (!asn1
.has_error
) {
306 *blob
= data_blob(asn1
.data
, asn1
.length
);
314 bool free_spnego_data(SPNEGO_DATA
*spnego
)
318 if (!spnego
) goto out
;
320 switch(spnego
->type
) {
321 case SPNEGO_NEG_TOKEN_INIT
:
322 if (spnego
->negTokenInit
.mechTypes
) {
324 for (i
= 0; spnego
->negTokenInit
.mechTypes
[i
]; i
++) {
325 free(CONST_DISCARD(char *,spnego
->negTokenInit
.mechTypes
[i
]));
327 free(spnego
->negTokenInit
.mechTypes
);
329 data_blob_free(&spnego
->negTokenInit
.mechToken
);
330 data_blob_free(&spnego
->negTokenInit
.mechListMIC
);
332 case SPNEGO_NEG_TOKEN_TARG
:
333 if (spnego
->negTokenTarg
.supportedMech
) {
334 free(spnego
->negTokenTarg
.supportedMech
);
336 data_blob_free(&spnego
->negTokenTarg
.responseToken
);
337 data_blob_free(&spnego
->negTokenTarg
.mechListMIC
);
343 ZERO_STRUCTP(spnego
);