blob: b014f4638610c661f58bf8d479175e8721fb22b0 [file] [log] [blame]
Namjae Jeone2f34482021-03-16 10:49:09 +09001// 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>
Hyunchul Leefad41612021-04-19 17:26:15 +090014#include <linux/oid_registry.h>
Namjae Jeone2f34482021-03-16 10:49:09 +090015
16#include "glob.h"
17
18#include "asn1.h"
19#include "connection.h"
20#include "auth.h"
Hyunchul Lee99f45252021-06-09 10:06:57 +090021#include "ksmbd_spnego_negtokeninit.asn1.h"
22#include "ksmbd_spnego_negtokentarg.asn1.h"
Namjae Jeone2f34482021-03-16 10:49:09 +090023
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
29static unsigned long SPNEGO_OID[7] = { 1, 3, 6, 1, 5, 5, 2 };
30static unsigned long NTLMSSP_OID[10] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10 };
31static unsigned long KRB5_OID[7] = { 1, 2, 840, 113554, 1, 2, 2 };
32static unsigned long KRB5U2U_OID[8] = { 1, 2, 840, 113554, 1, 2, 2, 3 };
33static unsigned long MSKRB5_OID[7] = { 1, 2, 840, 48018, 1, 2, 2 };
34
35static char NTLMSSP_OID_STR[NTLMSSP_OID_LEN] = { 0x2b, 0x06, 0x01, 0x04, 0x01,
36 0x82, 0x37, 0x02, 0x02, 0x0a };
37
Hyunchul Leefad41612021-04-19 17:26:15 +090038static bool
39asn1_subid_decode(const unsigned char **begin, const unsigned char *end,
Namjae Jeon070fb212021-05-26 17:57:12 +090040 unsigned long *subid)
Namjae Jeone2f34482021-03-16 10:49:09 +090041{
Hyunchul Leefad41612021-04-19 17:26:15 +090042 const unsigned char *ptr = *begin;
Namjae Jeone2f34482021-03-16 10:49:09 +090043 unsigned char ch;
44
45 *subid = 0;
46
47 do {
Hyunchul Leefad41612021-04-19 17:26:15 +090048 if (ptr >= end)
49 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +090050
Hyunchul Leefad41612021-04-19 17:26:15 +090051 ch = *ptr++;
Namjae Jeone2f34482021-03-16 10:49:09 +090052 *subid <<= 7;
53 *subid |= ch & 0x7F;
54 } while ((ch & 0x80) == 0x80);
Hyunchul Leefad41612021-04-19 17:26:15 +090055
56 *begin = ptr;
57 return true;
Namjae Jeone2f34482021-03-16 10:49:09 +090058}
59
Hyunchul Leefad41612021-04-19 17:26:15 +090060static bool asn1_oid_decode(const unsigned char *value, size_t vlen,
Namjae Jeon070fb212021-05-26 17:57:12 +090061 unsigned long **oid, size_t *oidlen)
Namjae Jeone2f34482021-03-16 10:49:09 +090062{
Hyunchul Leefad41612021-04-19 17:26:15 +090063 const unsigned char *iptr = value, *end = value + vlen;
Namjae Jeone2f34482021-03-16 10:49:09 +090064 unsigned long *optr;
Hyunchul Leefad41612021-04-19 17:26:15 +090065 unsigned long subid;
Namjae Jeone2f34482021-03-16 10:49:09 +090066
Hyunchul Leefad41612021-04-19 17:26:15 +090067 vlen += 1;
Namjae Jeonc8ed1152021-05-26 16:42:12 +090068 if (vlen < 2 || vlen > UINT_MAX / sizeof(unsigned long))
Colin Ian King5fb68862021-06-18 09:54:53 +090069 goto fail_nullify;
Namjae Jeone2f34482021-03-16 10:49:09 +090070
Hyunchul Leefad41612021-04-19 17:26:15 +090071 *oid = kmalloc(vlen * sizeof(unsigned long), GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +090072 if (!*oid)
Hyunchul Leefad41612021-04-19 17:26:15 +090073 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +090074
75 optr = *oid;
76
Namjae Jeoncdd10392021-05-26 15:22:37 +090077 if (!asn1_subid_decode(&iptr, end, &subid))
78 goto fail;
Namjae Jeone2f34482021-03-16 10:49:09 +090079
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
Hyunchul Leefad41612021-04-19 17:26:15 +090091 *oidlen = 2;
Namjae Jeone2f34482021-03-16 10:49:09 +090092 optr += 2;
93
Hyunchul Leefad41612021-04-19 17:26:15 +090094 while (iptr < end) {
Namjae Jeoncdd10392021-05-26 15:22:37 +090095 if (++(*oidlen) > vlen)
96 goto fail;
Namjae Jeone2f34482021-03-16 10:49:09 +090097
Namjae Jeoncdd10392021-05-26 15:22:37 +090098 if (!asn1_subid_decode(&iptr, end, optr++))
99 goto fail;
Namjae Jeone2f34482021-03-16 10:49:09 +0900100 }
Hyunchul Leefad41612021-04-19 17:26:15 +0900101 return true;
Namjae Jeoncdd10392021-05-26 15:22:37 +0900102
103fail:
104 kfree(*oid);
Colin Ian King5fb68862021-06-18 09:54:53 +0900105fail_nullify:
Namjae Jeoncdd10392021-05-26 15:22:37 +0900106 *oid = NULL;
107 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900108}
109
Namjae Jeon070fb212021-05-26 17:57:12 +0900110static bool oid_eq(unsigned long *oid1, unsigned int oid1len,
111 unsigned long *oid2, unsigned int oid2len)
Namjae Jeone2f34482021-03-16 10:49:09 +0900112{
Namjae Jeone2f34482021-03-16 10:49:09 +0900113 if (oid1len != oid2len)
Hyunchul Leefad41612021-04-19 17:26:15 +0900114 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900115
Namjae Jeon3566a2b2021-05-26 15:23:55 +0900116 return memcmp(oid1, oid2, oid1len) == 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900117}
118
Namjae Jeone2f34482021-03-16 10:49:09 +0900119int
120ksmbd_decode_negTokenInit(unsigned char *security_blob, int length,
Namjae Jeon070fb212021-05-26 17:57:12 +0900121 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +0900122{
Hyunchul Lee99f45252021-06-09 10:06:57 +0900123 return asn1_ber_decoder(&ksmbd_spnego_negtokeninit_decoder, conn,
Hyunchul Leefad41612021-04-19 17:26:15 +0900124 security_blob, length);
Namjae Jeone2f34482021-03-16 10:49:09 +0900125}
126
127int
128ksmbd_decode_negTokenTarg(unsigned char *security_blob, int length,
Namjae Jeon070fb212021-05-26 17:57:12 +0900129 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +0900130{
Hyunchul Lee99f45252021-06-09 10:06:57 +0900131 return asn1_ber_decoder(&ksmbd_spnego_negtokentarg_decoder, conn,
Hyunchul Leefad41612021-04-19 17:26:15 +0900132 security_blob, length);
Namjae Jeone2f34482021-03-16 10:49:09 +0900133}
134
135static 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
Namjae Jeon070fb212021-05-26 17:57:12 +0900149static void encode_asn_tag(char *buf, unsigned int *ofs, char tag, char seq,
Namjae Jeone2f34482021-03-16 10:49:09 +0900150 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
Namjae Jeona2d63212021-05-26 16:40:39 +0900160 if (!hdr_len) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900161 buf[index++] = len;
Namjae Jeona2d63212021-05-26 16:40:39 +0900162 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +0900163 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
Namjae Jeona2d63212021-05-26 16:40:39 +0900172 if (!hdr_len) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900173 buf[index++] = len;
Namjae Jeona2d63212021-05-26 16:40:39 +0900174 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +0900175 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
183int build_spnego_ntlmssp_neg_blob(unsigned char **pbuffer, u16 *buflen,
Namjae Jeon070fb212021-05-26 17:57:12 +0900184 char *ntlm_blob, int ntlm_blob_len)
Namjae Jeone2f34482021-03-16 10:49:09 +0900185{
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
224int build_spnego_ntlmssp_auth_blob(unsigned char **pbuffer, u16 *buflen,
Namjae Jeon070fb212021-05-26 17:57:12 +0900225 int neg_result)
Namjae Jeone2f34482021-03-16 10:49:09 +0900226{
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}
Hyunchul Leefad41612021-04-19 17:26:15 +0900251
Hyunchul Lee99f45252021-06-09 10:06:57 +0900252int ksmbd_gssapi_this_mech(void *context, size_t hdrlen, unsigned char tag,
253 const void *value, size_t vlen)
Hyunchul Leefad41612021-04-19 17:26:15 +0900254{
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);
267out:
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
Hyunchul Lee99f45252021-06-09 10:06:57 +0900277int ksmbd_neg_token_init_mech_type(void *context, size_t hdrlen,
278 unsigned char tag, const void *value,
279 size_t vlen)
Hyunchul Leefad41612021-04-19 17:26:15 +0900280{
281 struct ksmbd_conn *conn = context;
282 unsigned long *oid;
283 size_t oidlen;
284 int mech_type;
Namjae Jeon8bae4412021-05-26 15:24:39 +0900285 char buf[50];
Hyunchul Leefad41612021-04-19 17:26:15 +0900286
Namjae Jeon8bae4412021-05-26 15:24:39 +0900287 if (!asn1_oid_decode(value, vlen, &oid, &oidlen))
288 goto fail;
Hyunchul Leefad41612021-04-19 17:26:15 +0900289
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
Namjae Jeon8bae4412021-05-26 15:24:39 +0900299 goto fail;
Hyunchul Leefad41612021-04-19 17:26:15 +0900300
301 conn->auth_mechs |= mech_type;
302 if (conn->preferred_auth_mech == 0)
303 conn->preferred_auth_mech = mech_type;
304
Hyunchul Leefad41612021-04-19 17:26:15 +0900305 kfree(oid);
306 return 0;
Namjae Jeon8bae4412021-05-26 15:24:39 +0900307
308fail:
309 kfree(oid);
310 sprint_oid(value, vlen, buf, sizeof(buf));
311 ksmbd_debug(AUTH, "Unexpected OID: %s\n", buf);
312 return -EBADMSG;
Hyunchul Leefad41612021-04-19 17:26:15 +0900313}
314
Hyunchul Lee99f45252021-06-09 10:06:57 +0900315int ksmbd_neg_token_init_mech_token(void *context, size_t hdrlen,
316 unsigned char tag, const void *value,
317 size_t vlen)
Hyunchul Leefad41612021-04-19 17:26:15 +0900318{
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
Hyunchul Lee99f45252021-06-09 10:06:57 +0900330int ksmbd_neg_token_targ_resp_token(void *context, size_t hdrlen,
331 unsigned char tag, const void *value,
332 size_t vlen)
Hyunchul Leefad41612021-04-19 17:26:15 +0900333{
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}