1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * The ASB.1/BER parsing code is derived from ip_nat_snmp_basic.c which was in
4  * turn derived from the gxsnmp package by Gregory McLean & Jochen Friedrich
5  *
6  * Copyright (c) 2000 RP Internet (www.rpi.net.au).
7  */
8 
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/mm.h>
13 #include <linux/slab.h>
14 #include <linux/oid_registry.h>
15 
16 #include "glob.h"
17 
18 #include "asn1.h"
19 #include "connection.h"
20 #include "auth.h"
21 #include "ksmbd_spnego_negtokeninit.asn1.h"
22 #include "ksmbd_spnego_negtokentarg.asn1.h"
23 
24 #define SPNEGO_OID_LEN 7
25 #define NTLMSSP_OID_LEN  10
26 #define KRB5_OID_LEN  7
27 #define KRB5U2U_OID_LEN  8
28 #define MSKRB5_OID_LEN  7
29 static unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
30 static unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
31 static unsigned long KRB5_OID[7] = { 1, 2, 840, 113554, 1, 2, 2 };
32 static unsigned long KRB5U2U_OID[8] = { 1, 2, 840, 113554, 1, 2, 2, 3 };
33 static unsigned long MSKRB5_OID[7] = { 1, 2, 840, 48018, 1, 2, 2 };
34 
35 static char NTLMSSP_OID_STR[NTLMSSP_OID_LEN] = { 0x2b, 0x06, 0x01, 0x04, 0x01,
36 	0x82, 0x37, 0x02, 0x02, 0x0a };
37 
38 static bool
asn1_subid_decode(const unsigned char ** begin,const unsigned char * end,unsigned long * subid)39 asn1_subid_decode(const unsigned char **begin, const unsigned char *end,
40 		  unsigned long *subid)
41 {
42 	const unsigned char *ptr = *begin;
43 	unsigned char ch;
44 
45 	*subid = 0;
46 
47 	do {
48 		if (ptr >= end)
49 			return false;
50 
51 		ch = *ptr++;
52 		*subid <<= 7;
53 		*subid |= ch & 0x7F;
54 	} while ((ch & 0x80) == 0x80);
55 
56 	*begin = ptr;
57 	return true;
58 }
59 
asn1_oid_decode(const unsigned char * value,size_t vlen,unsigned long ** oid,size_t * oidlen)60 static bool asn1_oid_decode(const unsigned char *value, size_t vlen,
61 			    unsigned long **oid, size_t *oidlen)
62 {
63 	const unsigned char *iptr = value, *end = value + vlen;
64 	unsigned long *optr;
65 	unsigned long subid;
66 
67 	vlen += 1;
68 	if (vlen < 2 || vlen > UINT_MAX / sizeof(unsigned long))
69 		goto fail_nullify;
70 
71 	*oid = kmalloc(vlen * sizeof(unsigned long), GFP_KERNEL);
72 	if (!*oid)
73 		return false;
74 
75 	optr = *oid;
76 
77 	if (!asn1_subid_decode(&iptr, end, &subid))
78 		goto fail;
79 
80 	if (subid < 40) {
81 		optr[0] = 0;
82 		optr[1] = subid;
83 	} else if (subid < 80) {
84 		optr[0] = 1;
85 		optr[1] = subid - 40;
86 	} else {
87 		optr[0] = 2;
88 		optr[1] = subid - 80;
89 	}
90 
91 	*oidlen = 2;
92 	optr += 2;
93 
94 	while (iptr < end) {
95 		if (++(*oidlen) > vlen)
96 			goto fail;
97 
98 		if (!asn1_subid_decode(&iptr, end, optr++))
99 			goto fail;
100 	}
101 	return true;
102 
103 fail:
104 	kfree(*oid);
105 fail_nullify:
106 	*oid = NULL;
107 	return false;
108 }
109 
oid_eq(unsigned long * oid1,unsigned int oid1len,unsigned long * oid2,unsigned int oid2len)110 static bool oid_eq(unsigned long *oid1, unsigned int oid1len,
111 		   unsigned long *oid2, unsigned int oid2len)
112 {
113 	if (oid1len != oid2len)
114 		return false;
115 
116 	return memcmp(oid1, oid2, oid1len) == 0;
117 }
118 
119 int
ksmbd_decode_negTokenInit(unsigned char * security_blob,int length,struct ksmbd_conn * conn)120 ksmbd_decode_negTokenInit(unsigned char *security_blob, int length,
121 			  struct ksmbd_conn *conn)
122 {
123 	return asn1_ber_decoder(&ksmbd_spnego_negtokeninit_decoder, conn,
124 				security_blob, length);
125 }
126 
127 int
ksmbd_decode_negTokenTarg(unsigned char * security_blob,int length,struct ksmbd_conn * conn)128 ksmbd_decode_negTokenTarg(unsigned char *security_blob, int length,
129 			  struct ksmbd_conn *conn)
130 {
131 	return asn1_ber_decoder(&ksmbd_spnego_negtokentarg_decoder, conn,
132 				security_blob, length);
133 }
134 
compute_asn_hdr_len_bytes(int len)135 static int compute_asn_hdr_len_bytes(int len)
136 {
137 	if (len > 0xFFFFFF)
138 		return 4;
139 	else if (len > 0xFFFF)
140 		return 3;
141 	else if (len > 0xFF)
142 		return 2;
143 	else if (len > 0x7F)
144 		return 1;
145 	else
146 		return 0;
147 }
148 
encode_asn_tag(char * buf,unsigned int * ofs,char tag,char seq,int length)149 static void encode_asn_tag(char *buf, unsigned int *ofs, char tag, char seq,
150 			   int length)
151 {
152 	int i;
153 	int index = *ofs;
154 	char hdr_len = compute_asn_hdr_len_bytes(length);
155 	int len = length + 2 + hdr_len;
156 
157 	/* insert tag */
158 	buf[index++] = tag;
159 
160 	if (!hdr_len) {
161 		buf[index++] = len;
162 	} else {
163 		buf[index++] = 0x80 | hdr_len;
164 		for (i = hdr_len - 1; i >= 0; i--)
165 			buf[index++] = (len >> (i * 8)) & 0xFF;
166 	}
167 
168 	/* insert seq */
169 	len = len - (index - *ofs);
170 	buf[index++] = seq;
171 
172 	if (!hdr_len) {
173 		buf[index++] = len;
174 	} else {
175 		buf[index++] = 0x80 | hdr_len;
176 		for (i = hdr_len - 1; i >= 0; i--)
177 			buf[index++] = (len >> (i * 8)) & 0xFF;
178 	}
179 
180 	*ofs += (index - *ofs);
181 }
182 
build_spnego_ntlmssp_neg_blob(unsigned char ** pbuffer,u16 * buflen,char * ntlm_blob,int ntlm_blob_len)183 int build_spnego_ntlmssp_neg_blob(unsigned char **pbuffer, u16 *buflen,
184 				  char *ntlm_blob, int ntlm_blob_len)
185 {
186 	char *buf;
187 	unsigned int ofs = 0;
188 	int neg_result_len = 4 + compute_asn_hdr_len_bytes(1) * 2 + 1;
189 	int oid_len = 4 + compute_asn_hdr_len_bytes(NTLMSSP_OID_LEN) * 2 +
190 		NTLMSSP_OID_LEN;
191 	int ntlmssp_len = 4 + compute_asn_hdr_len_bytes(ntlm_blob_len) * 2 +
192 		ntlm_blob_len;
193 	int total_len = 4 + compute_asn_hdr_len_bytes(neg_result_len +
194 			oid_len + ntlmssp_len) * 2 +
195 			neg_result_len + oid_len + ntlmssp_len;
196 
197 	buf = kmalloc(total_len, GFP_KERNEL);
198 	if (!buf)
199 		return -ENOMEM;
200 
201 	/* insert main gss header */
202 	encode_asn_tag(buf, &ofs, 0xa1, 0x30, neg_result_len + oid_len +
203 			ntlmssp_len);
204 
205 	/* insert neg result */
206 	encode_asn_tag(buf, &ofs, 0xa0, 0x0a, 1);
207 	buf[ofs++] = 1;
208 
209 	/* insert oid */
210 	encode_asn_tag(buf, &ofs, 0xa1, 0x06, NTLMSSP_OID_LEN);
211 	memcpy(buf + ofs, NTLMSSP_OID_STR, NTLMSSP_OID_LEN);
212 	ofs += NTLMSSP_OID_LEN;
213 
214 	/* insert response token - ntlmssp blob */
215 	encode_asn_tag(buf, &ofs, 0xa2, 0x04, ntlm_blob_len);
216 	memcpy(buf + ofs, ntlm_blob, ntlm_blob_len);
217 	ofs += ntlm_blob_len;
218 
219 	*pbuffer = buf;
220 	*buflen = total_len;
221 	return 0;
222 }
223 
build_spnego_ntlmssp_auth_blob(unsigned char ** pbuffer,u16 * buflen,int neg_result)224 int build_spnego_ntlmssp_auth_blob(unsigned char **pbuffer, u16 *buflen,
225 				   int neg_result)
226 {
227 	char *buf;
228 	unsigned int ofs = 0;
229 	int neg_result_len = 4 + compute_asn_hdr_len_bytes(1) * 2 + 1;
230 	int total_len = 4 + compute_asn_hdr_len_bytes(neg_result_len) * 2 +
231 		neg_result_len;
232 
233 	buf = kmalloc(total_len, GFP_KERNEL);
234 	if (!buf)
235 		return -ENOMEM;
236 
237 	/* insert main gss header */
238 	encode_asn_tag(buf, &ofs, 0xa1, 0x30, neg_result_len);
239 
240 	/* insert neg result */
241 	encode_asn_tag(buf, &ofs, 0xa0, 0x0a, 1);
242 	if (neg_result)
243 		buf[ofs++] = 2;
244 	else
245 		buf[ofs++] = 0;
246 
247 	*pbuffer = buf;
248 	*buflen = total_len;
249 	return 0;
250 }
251 
ksmbd_gssapi_this_mech(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)252 int ksmbd_gssapi_this_mech(void *context, size_t hdrlen, unsigned char tag,
253 			   const void *value, size_t vlen)
254 {
255 	unsigned long *oid;
256 	size_t oidlen;
257 	int err = 0;
258 
259 	if (!asn1_oid_decode(value, vlen, &oid, &oidlen)) {
260 		err = -EBADMSG;
261 		goto out;
262 	}
263 
264 	if (!oid_eq(oid, oidlen, SPNEGO_OID, SPNEGO_OID_LEN))
265 		err = -EBADMSG;
266 	kfree(oid);
267 out:
268 	if (err) {
269 		char buf[50];
270 
271 		sprint_oid(value, vlen, buf, sizeof(buf));
272 		ksmbd_debug(AUTH, "Unexpected OID: %s\n", buf);
273 	}
274 	return err;
275 }
276 
ksmbd_neg_token_init_mech_type(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)277 int ksmbd_neg_token_init_mech_type(void *context, size_t hdrlen,
278 				   unsigned char tag, const void *value,
279 				   size_t vlen)
280 {
281 	struct ksmbd_conn *conn = context;
282 	unsigned long *oid;
283 	size_t oidlen;
284 	int mech_type;
285 	char buf[50];
286 
287 	if (!asn1_oid_decode(value, vlen, &oid, &oidlen))
288 		goto fail;
289 
290 	if (oid_eq(oid, oidlen, NTLMSSP_OID, NTLMSSP_OID_LEN))
291 		mech_type = KSMBD_AUTH_NTLMSSP;
292 	else if (oid_eq(oid, oidlen, MSKRB5_OID, MSKRB5_OID_LEN))
293 		mech_type = KSMBD_AUTH_MSKRB5;
294 	else if (oid_eq(oid, oidlen, KRB5_OID, KRB5_OID_LEN))
295 		mech_type = KSMBD_AUTH_KRB5;
296 	else if (oid_eq(oid, oidlen, KRB5U2U_OID, KRB5U2U_OID_LEN))
297 		mech_type = KSMBD_AUTH_KRB5U2U;
298 	else
299 		goto fail;
300 
301 	conn->auth_mechs |= mech_type;
302 	if (conn->preferred_auth_mech == 0)
303 		conn->preferred_auth_mech = mech_type;
304 
305 	kfree(oid);
306 	return 0;
307 
308 fail:
309 	kfree(oid);
310 	sprint_oid(value, vlen, buf, sizeof(buf));
311 	ksmbd_debug(AUTH, "Unexpected OID: %s\n", buf);
312 	return -EBADMSG;
313 }
314 
ksmbd_neg_token_init_mech_token(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)315 int ksmbd_neg_token_init_mech_token(void *context, size_t hdrlen,
316 				    unsigned char tag, const void *value,
317 				    size_t vlen)
318 {
319 	struct ksmbd_conn *conn = context;
320 
321 	conn->mechToken = kmalloc(vlen + 1, GFP_KERNEL);
322 	if (!conn->mechToken)
323 		return -ENOMEM;
324 
325 	memcpy(conn->mechToken, value, vlen);
326 	conn->mechToken[vlen] = '\0';
327 	return 0;
328 }
329 
ksmbd_neg_token_targ_resp_token(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)330 int ksmbd_neg_token_targ_resp_token(void *context, size_t hdrlen,
331 				    unsigned char tag, const void *value,
332 				    size_t vlen)
333 {
334 	struct ksmbd_conn *conn = context;
335 
336 	conn->mechToken = kmalloc(vlen + 1, GFP_KERNEL);
337 	if (!conn->mechToken)
338 		return -ENOMEM;
339 
340 	memcpy(conn->mechToken, value, vlen);
341 	conn->mechToken[vlen] = '\0';
342 	return 0;
343 }
344