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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DBGC_CLASS DBGC_AUTH
29 static BOOL
read_negTokenInit(ASN1_DATA
*asn1
, negTokenInit_t
*token
)
33 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
34 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
36 while (!asn1
->has_error
&& 0 < asn1_tag_remaining(asn1
)) {
39 switch (asn1
->data
[asn1
->ofs
]) {
42 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
43 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
45 token
->mechTypes
= SMB_MALLOC_P(char *);
46 for (i
= 0; !asn1
->has_error
&&
47 0 < asn1_tag_remaining(asn1
); i
++) {
49 SMB_REALLOC_ARRAY(token
->mechTypes
, char *, i
+ 2);
50 asn1_read_OID(asn1
, &token
->mechTypes
[i
]);
52 token
->mechTypes
[i
] = NULL
;
59 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
60 asn1_read_Integer(asn1
, &token
->reqFlags
);
61 token
->reqFlags
|= SPNEGO_REQ_FLAG
;
66 asn1_start_tag(asn1
, ASN1_CONTEXT(2));
67 asn1_read_OctetString(asn1
, &token
->mechToken
);
72 asn1_start_tag(asn1
, ASN1_CONTEXT(3));
73 if (asn1
->data
[asn1
->ofs
] == ASN1_OCTET_STRING
) {
74 asn1_read_OctetString(asn1
,
77 /* RFC 2478 says we have an Octet String here,
78 but W2k sends something different... */
80 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
81 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
82 asn1_read_GeneralString(asn1
, &mechListMIC
);
87 data_blob(mechListMIC
, strlen(mechListMIC
));
88 SAFE_FREE(mechListMIC
);
93 asn1
->has_error
= True
;
101 return !asn1
->has_error
;
104 static BOOL
write_negTokenInit(ASN1_DATA
*asn1
, negTokenInit_t
*token
)
106 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
107 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
109 /* Write mechTypes */
110 if (token
->mechTypes
&& *token
->mechTypes
) {
113 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
114 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
115 for (i
= 0; token
->mechTypes
[i
]; i
++) {
116 asn1_write_OID(asn1
, token
->mechTypes
[i
]);
123 if (token
->reqFlags
& SPNEGO_REQ_FLAG
) {
124 int flags
= token
->reqFlags
& ~SPNEGO_REQ_FLAG
;
126 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
127 asn1_write_Integer(asn1
, flags
);
131 /* write mechToken */
132 if (token
->mechToken
.data
) {
133 asn1_push_tag(asn1
, ASN1_CONTEXT(2));
134 asn1_write_OctetString(asn1
, token
->mechToken
.data
,
135 token
->mechToken
.length
);
139 /* write mechListMIC */
140 if (token
->mechListMIC
.data
) {
141 asn1_push_tag(asn1
, ASN1_CONTEXT(3));
143 /* This is what RFC 2478 says ... */
144 asn1_write_OctetString(asn1
, token
->mechListMIC
.data
,
145 token
->mechListMIC
.length
);
147 /* ... but unfortunately this is what Windows
149 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
150 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
151 asn1_push_tag(asn1
, ASN1_GENERAL_STRING
);
152 asn1_write(asn1
, token
->mechListMIC
.data
,
153 token
->mechListMIC
.length
);
164 return !asn1
->has_error
;
167 static BOOL
read_negTokenTarg(ASN1_DATA
*asn1
, negTokenTarg_t
*token
)
171 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
172 asn1_start_tag(asn1
, ASN1_SEQUENCE(0));
174 while (!asn1
->has_error
&& 0 < asn1_tag_remaining(asn1
)) {
175 switch (asn1
->data
[asn1
->ofs
]) {
176 case ASN1_CONTEXT(0):
177 asn1_start_tag(asn1
, ASN1_CONTEXT(0));
178 asn1_start_tag(asn1
, ASN1_ENUMERATED
);
179 asn1_read_uint8(asn1
, &token
->negResult
);
183 case ASN1_CONTEXT(1):
184 asn1_start_tag(asn1
, ASN1_CONTEXT(1));
185 asn1_read_OID(asn1
, &token
->supportedMech
);
188 case ASN1_CONTEXT(2):
189 asn1_start_tag(asn1
, ASN1_CONTEXT(2));
190 asn1_read_OctetString(asn1
, &token
->responseToken
);
193 case ASN1_CONTEXT(3):
194 asn1_start_tag(asn1
, ASN1_CONTEXT(3));
195 asn1_read_OctetString(asn1
, &token
->mechListMIC
);
199 asn1
->has_error
= True
;
207 return !asn1
->has_error
;
210 static BOOL
write_negTokenTarg(ASN1_DATA
*asn1
, negTokenTarg_t
*token
)
212 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
213 asn1_push_tag(asn1
, ASN1_SEQUENCE(0));
215 asn1_push_tag(asn1
, ASN1_CONTEXT(0));
216 asn1_write_enumerated(asn1
, token
->negResult
);
219 if (token
->supportedMech
) {
220 asn1_push_tag(asn1
, ASN1_CONTEXT(1));
221 asn1_write_OID(asn1
, token
->supportedMech
);
225 if (token
->responseToken
.data
) {
226 asn1_push_tag(asn1
, ASN1_CONTEXT(2));
227 asn1_write_OctetString(asn1
, token
->responseToken
.data
,
228 token
->responseToken
.length
);
232 if (token
->mechListMIC
.data
) {
233 asn1_push_tag(asn1
, ASN1_CONTEXT(3));
234 asn1_write_OctetString(asn1
, token
->mechListMIC
.data
,
235 token
->mechListMIC
.length
);
242 return !asn1
->has_error
;
245 ssize_t
read_spnego_data(DATA_BLOB data
, SPNEGO_DATA
*token
)
252 asn1_load(&asn1
, data
);
254 switch (asn1
.data
[asn1
.ofs
]) {
255 case ASN1_APPLICATION(0):
256 asn1_start_tag(&asn1
, ASN1_APPLICATION(0));
257 asn1_check_OID(&asn1
, OID_SPNEGO
);
258 if (read_negTokenInit(&asn1
, &token
->negTokenInit
)) {
259 token
->type
= SPNEGO_NEG_TOKEN_INIT
;
263 case ASN1_CONTEXT(1):
264 if (read_negTokenTarg(&asn1
, &token
->negTokenTarg
)) {
265 token
->type
= SPNEGO_NEG_TOKEN_TARG
;
272 if (!asn1
.has_error
) ret
= asn1
.ofs
;
278 ssize_t
write_spnego_data(DATA_BLOB
*blob
, SPNEGO_DATA
*spnego
)
285 switch (spnego
->type
) {
286 case SPNEGO_NEG_TOKEN_INIT
:
287 asn1_push_tag(&asn1
, ASN1_APPLICATION(0));
288 asn1_write_OID(&asn1
, OID_SPNEGO
);
289 write_negTokenInit(&asn1
, &spnego
->negTokenInit
);
292 case SPNEGO_NEG_TOKEN_TARG
:
293 write_negTokenTarg(&asn1
, &spnego
->negTokenTarg
);
296 asn1
.has_error
= True
;
300 if (!asn1
.has_error
) {
301 *blob
= data_blob(asn1
.data
, asn1
.length
);
309 BOOL
free_spnego_data(SPNEGO_DATA
*spnego
)
313 if (!spnego
) goto out
;
315 switch(spnego
->type
) {
316 case SPNEGO_NEG_TOKEN_INIT
:
317 if (spnego
->negTokenInit
.mechTypes
) {
319 for (i
= 0; spnego
->negTokenInit
.mechTypes
[i
]; i
++) {
320 free(spnego
->negTokenInit
.mechTypes
[i
]);
322 free(spnego
->negTokenInit
.mechTypes
);
324 data_blob_free(&spnego
->negTokenInit
.mechToken
);
325 data_blob_free(&spnego
->negTokenInit
.mechListMIC
);
327 case SPNEGO_NEG_TOKEN_TARG
:
328 if (spnego
->negTokenTarg
.supportedMech
) {
329 free(spnego
->negTokenTarg
.supportedMech
);
331 data_blob_free(&spnego
->negTokenTarg
.responseToken
);
332 data_blob_free(&spnego
->negTokenTarg
.mechListMIC
);
338 ZERO_STRUCTP(spnego
);