source: trunk/server/source3/libsmb/clispnego.c@ 751

Last change on this file since 751 was 751, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.9

File size: 15.1 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 simple kerberos5/SPNEGO routines
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Jim McDonough <[email protected]> 2002
6 Copyright (C) Luke Howard 2003
7 Copyright (C) Jeremy Allison 2010
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
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/>.
21*/
22
23#include "includes.h"
24#include "../libcli/auth/spnego.h"
25#include "smb_krb5.h"
26#include "../lib/util/asn1.h"
27
28/*
29 generate a negTokenInit packet given a list of supported
30 OIDs (the mechanisms) a blob, and a principal name string
31*/
32
33DATA_BLOB spnego_gen_negTokenInit(TALLOC_CTX *ctx,
34 const char *OIDs[],
35 DATA_BLOB *psecblob,
36 const char *principal)
37{
38 int i;
39 ASN1_DATA *data;
40 DATA_BLOB ret;
41
42 data = asn1_init(talloc_tos());
43 if (data == NULL) {
44 return data_blob_null;
45 }
46
47 asn1_push_tag(data,ASN1_APPLICATION(0));
48 asn1_write_OID(data,OID_SPNEGO);
49 asn1_push_tag(data,ASN1_CONTEXT(0));
50 asn1_push_tag(data,ASN1_SEQUENCE(0));
51
52 asn1_push_tag(data,ASN1_CONTEXT(0));
53 asn1_push_tag(data,ASN1_SEQUENCE(0));
54 for (i=0; OIDs[i]; i++) {
55 asn1_write_OID(data,OIDs[i]);
56 }
57 asn1_pop_tag(data);
58 asn1_pop_tag(data);
59
60 if (psecblob && psecblob->length && psecblob->data) {
61 asn1_push_tag(data, ASN1_CONTEXT(2));
62 asn1_write_OctetString(data,psecblob->data,
63 psecblob->length);
64 asn1_pop_tag(data);
65 }
66
67 if (principal) {
68 asn1_push_tag(data, ASN1_CONTEXT(3));
69 asn1_push_tag(data, ASN1_SEQUENCE(0));
70 asn1_push_tag(data, ASN1_CONTEXT(0));
71 asn1_write_GeneralString(data,principal);
72 asn1_pop_tag(data);
73 asn1_pop_tag(data);
74 asn1_pop_tag(data);
75 }
76
77 asn1_pop_tag(data);
78 asn1_pop_tag(data);
79
80 asn1_pop_tag(data);
81
82 if (data->has_error) {
83 DEBUG(1,("Failed to build negTokenInit at offset %d\n", (int)data->ofs));
84 }
85
86 ret = data_blob_talloc(ctx, data->data, data->length);
87 asn1_free(data);
88
89 return ret;
90}
91
92/*
93 parse a negTokenInit packet giving a GUID, a list of supported
94 OIDs (the mechanisms) and a principal name string
95*/
96bool spnego_parse_negTokenInit(TALLOC_CTX *ctx,
97 DATA_BLOB blob,
98 char *OIDs[ASN1_MAX_OIDS],
99 char **principal,
100 DATA_BLOB *secblob)
101{
102 int i;
103 bool ret;
104 ASN1_DATA *data;
105
106 for (i = 0; i < ASN1_MAX_OIDS; i++) {
107 OIDs[i] = NULL;
108 }
109
110 data = asn1_init(talloc_tos());
111 if (data == NULL) {
112 return false;
113 }
114
115 asn1_load(data, blob);
116
117 asn1_start_tag(data,ASN1_APPLICATION(0));
118
119 asn1_check_OID(data,OID_SPNEGO);
120
121 /* negTokenInit [0] NegTokenInit */
122 asn1_start_tag(data,ASN1_CONTEXT(0));
123 asn1_start_tag(data,ASN1_SEQUENCE(0));
124
125 /* mechTypes [0] MechTypeList OPTIONAL */
126
127 /* Not really optional, we depend on this to decide
128 * what mechanisms we have to work with. */
129
130 asn1_start_tag(data,ASN1_CONTEXT(0));
131 asn1_start_tag(data,ASN1_SEQUENCE(0));
132 for (i=0; asn1_tag_remaining(data) > 0 && i < ASN1_MAX_OIDS-1; i++) {
133 asn1_read_OID(data,ctx, &OIDs[i]);
134 if (data->has_error) {
135 break;
136 }
137 }
138 OIDs[i] = NULL;
139 asn1_end_tag(data);
140 asn1_end_tag(data);
141
142 if (principal) {
143 *principal = NULL;
144 }
145 if (secblob) {
146 *secblob = data_blob_null;
147 }
148
149 /*
150 Win7 + Live Sign-in Assistant attaches a mechToken
151 ASN1_CONTEXT(2) to the negTokenInit packet
152 which breaks our negotiation if we just assume
153 the next tag is ASN1_CONTEXT(3).
154 */
155
156 if (asn1_peek_tag(data, ASN1_CONTEXT(1))) {
157 uint8 flags;
158
159 /* reqFlags [1] ContextFlags OPTIONAL */
160 asn1_start_tag(data, ASN1_CONTEXT(1));
161 asn1_start_tag(data, ASN1_BIT_STRING);
162 while (asn1_tag_remaining(data) > 0) {
163 asn1_read_uint8(data, &flags);
164 }
165 asn1_end_tag(data);
166 asn1_end_tag(data);
167 }
168
169 if (asn1_peek_tag(data, ASN1_CONTEXT(2))) {
170 DATA_BLOB sblob = data_blob_null;
171 /* mechToken [2] OCTET STRING OPTIONAL */
172 asn1_start_tag(data, ASN1_CONTEXT(2));
173 asn1_read_OctetString(data, ctx, &sblob);
174 asn1_end_tag(data);
175 if (secblob) {
176 *secblob = sblob;
177 } else {
178 data_blob_free(&sblob);
179 }
180 }
181
182 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
183 char *princ = NULL;
184 /* mechListMIC [3] OCTET STRING OPTIONAL */
185 asn1_start_tag(data, ASN1_CONTEXT(3));
186 asn1_start_tag(data, ASN1_SEQUENCE(0));
187 asn1_start_tag(data, ASN1_CONTEXT(0));
188 asn1_read_GeneralString(data, ctx, &princ);
189 asn1_end_tag(data);
190 asn1_end_tag(data);
191 asn1_end_tag(data);
192 if (principal) {
193 *principal = princ;
194 } else {
195 TALLOC_FREE(princ);
196 }
197 }
198
199 asn1_end_tag(data);
200 asn1_end_tag(data);
201
202 asn1_end_tag(data);
203
204 ret = !data->has_error;
205 if (data->has_error) {
206 int j;
207 if (principal) {
208 TALLOC_FREE(*principal);
209 }
210 if (secblob) {
211 data_blob_free(secblob);
212 }
213 for(j = 0; j < i && j < ASN1_MAX_OIDS-1; j++) {
214 TALLOC_FREE(OIDs[j]);
215 }
216 }
217
218 asn1_free(data);
219 return ret;
220}
221
222/*
223 generate a krb5 GSS-API wrapper packet given a ticket
224*/
225DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const uint8 tok_id[2])
226{
227 ASN1_DATA *data;
228 DATA_BLOB ret;
229
230 data = asn1_init(talloc_tos());
231 if (data == NULL) {
232 return data_blob_null;
233 }
234
235 asn1_push_tag(data, ASN1_APPLICATION(0));