138c8a9a5SSteve French // SPDX-License-Identifier: GPL-2.0-or-later
238c8a9a5SSteve French /*
338c8a9a5SSteve French * The ASB.1/BER parsing code is derived from ip_nat_snmp_basic.c which was in
438c8a9a5SSteve French * turn derived from the gxsnmp package by Gregory McLean & Jochen Friedrich
538c8a9a5SSteve French *
638c8a9a5SSteve French * Copyright (c) 2000 RP Internet (www.rpi.net.au).
738c8a9a5SSteve French */
838c8a9a5SSteve French
938c8a9a5SSteve French #include <linux/module.h>
1038c8a9a5SSteve French #include <linux/types.h>
1138c8a9a5SSteve French #include <linux/kernel.h>
1238c8a9a5SSteve French #include <linux/mm.h>
1338c8a9a5SSteve French #include <linux/slab.h>
1438c8a9a5SSteve French #include <linux/oid_registry.h>
1538c8a9a5SSteve French
1638c8a9a5SSteve French #include "glob.h"
1738c8a9a5SSteve French
1838c8a9a5SSteve French #include "asn1.h"
1938c8a9a5SSteve French #include "connection.h"
2038c8a9a5SSteve French #include "auth.h"
2138c8a9a5SSteve French #include "ksmbd_spnego_negtokeninit.asn1.h"
2238c8a9a5SSteve French #include "ksmbd_spnego_negtokentarg.asn1.h"
2338c8a9a5SSteve French
2438c8a9a5SSteve French #define NTLMSSP_OID_LEN 10
2538c8a9a5SSteve French
2638c8a9a5SSteve French static char NTLMSSP_OID_STR[NTLMSSP_OID_LEN] = { 0x2b, 0x06, 0x01, 0x04, 0x01,
2738c8a9a5SSteve French 0x82, 0x37, 0x02, 0x02, 0x0a };
2838c8a9a5SSteve French
2938c8a9a5SSteve French int
ksmbd_decode_negTokenInit(unsigned char * security_blob,int length,struct ksmbd_conn * conn)3038c8a9a5SSteve French ksmbd_decode_negTokenInit(unsigned char *security_blob, int length,
3138c8a9a5SSteve French struct ksmbd_conn *conn)
3238c8a9a5SSteve French {
3338c8a9a5SSteve French return asn1_ber_decoder(&ksmbd_spnego_negtokeninit_decoder, conn,
3438c8a9a5SSteve French security_blob, length);
3538c8a9a5SSteve French }
3638c8a9a5SSteve French
3738c8a9a5SSteve French int
ksmbd_decode_negTokenTarg(unsigned char * security_blob,int length,struct ksmbd_conn * conn)3838c8a9a5SSteve French ksmbd_decode_negTokenTarg(unsigned char *security_blob, int length,
3938c8a9a5SSteve French struct ksmbd_conn *conn)
4038c8a9a5SSteve French {
4138c8a9a5SSteve French return asn1_ber_decoder(&ksmbd_spnego_negtokentarg_decoder, conn,
4238c8a9a5SSteve French security_blob, length);
4338c8a9a5SSteve French }
4438c8a9a5SSteve French
compute_asn_hdr_len_bytes(int len)4538c8a9a5SSteve French static int compute_asn_hdr_len_bytes(int len)
4638c8a9a5SSteve French {
4738c8a9a5SSteve French if (len > 0xFFFFFF)
4838c8a9a5SSteve French return 4;
4938c8a9a5SSteve French else if (len > 0xFFFF)
5038c8a9a5SSteve French return 3;
5138c8a9a5SSteve French else if (len > 0xFF)
5238c8a9a5SSteve French return 2;
5338c8a9a5SSteve French else if (len > 0x7F)
5438c8a9a5SSteve French return 1;
5538c8a9a5SSteve French else
5638c8a9a5SSteve French return 0;
5738c8a9a5SSteve French }
5838c8a9a5SSteve French
encode_asn_tag(char * buf,unsigned int * ofs,char tag,char seq,int length)5938c8a9a5SSteve French static void encode_asn_tag(char *buf, unsigned int *ofs, char tag, char seq,
6038c8a9a5SSteve French int length)
6138c8a9a5SSteve French {
6238c8a9a5SSteve French int i;
6338c8a9a5SSteve French int index = *ofs;
6438c8a9a5SSteve French char hdr_len = compute_asn_hdr_len_bytes(length);
6538c8a9a5SSteve French int len = length + 2 + hdr_len;
6638c8a9a5SSteve French
6738c8a9a5SSteve French /* insert tag */
6838c8a9a5SSteve French buf[index++] = tag;
6938c8a9a5SSteve French
7038c8a9a5SSteve French if (!hdr_len) {
7138c8a9a5SSteve French buf[index++] = len;
7238c8a9a5SSteve French } else {
7338c8a9a5SSteve French buf[index++] = 0x80 | hdr_len;
7438c8a9a5SSteve French for (i = hdr_len - 1; i >= 0; i--)
7538c8a9a5SSteve French buf[index++] = (len >> (i * 8)) & 0xFF;
7638c8a9a5SSteve French }
7738c8a9a5SSteve French
7838c8a9a5SSteve French /* insert seq */
7938c8a9a5SSteve French len = len - (index - *ofs);
8038c8a9a5SSteve French buf[index++] = seq;
8138c8a9a5SSteve French
8238c8a9a5SSteve French if (!hdr_len) {
8338c8a9a5SSteve French buf[index++] = len;
8438c8a9a5SSteve French } else {
8538c8a9a5SSteve French buf[index++] = 0x80 | hdr_len;
8638c8a9a5SSteve French for (i = hdr_len - 1; i >= 0; i--)
8738c8a9a5SSteve French buf[index++] = (len >> (i * 8)) & 0xFF;
8838c8a9a5SSteve French }
8938c8a9a5SSteve French
9038c8a9a5SSteve French *ofs += (index - *ofs);
9138c8a9a5SSteve French }
9238c8a9a5SSteve French
build_spnego_ntlmssp_neg_blob(unsigned char ** pbuffer,u16 * buflen,char * ntlm_blob,int ntlm_blob_len)9338c8a9a5SSteve French int build_spnego_ntlmssp_neg_blob(unsigned char **pbuffer, u16 *buflen,
9438c8a9a5SSteve French char *ntlm_blob, int ntlm_blob_len)
9538c8a9a5SSteve French {
9638c8a9a5SSteve French char *buf;
9738c8a9a5SSteve French unsigned int ofs = 0;
9838c8a9a5SSteve French int neg_result_len = 4 + compute_asn_hdr_len_bytes(1) * 2 + 1;
9938c8a9a5SSteve French int oid_len = 4 + compute_asn_hdr_len_bytes(NTLMSSP_OID_LEN) * 2 +
10038c8a9a5SSteve French NTLMSSP_OID_LEN;
10138c8a9a5SSteve French int ntlmssp_len = 4 + compute_asn_hdr_len_bytes(ntlm_blob_len) * 2 +
10238c8a9a5SSteve French ntlm_blob_len;
10338c8a9a5SSteve French int total_len = 4 + compute_asn_hdr_len_bytes(neg_result_len +
10438c8a9a5SSteve French oid_len + ntlmssp_len) * 2 +
10538c8a9a5SSteve French neg_result_len + oid_len + ntlmssp_len;
10638c8a9a5SSteve French
10738c8a9a5SSteve French buf = kmalloc(total_len, GFP_KERNEL);
10838c8a9a5SSteve French if (!buf)
10938c8a9a5SSteve French return -ENOMEM;
11038c8a9a5SSteve French
11138c8a9a5SSteve French /* insert main gss header */
11238c8a9a5SSteve French encode_asn_tag(buf, &ofs, 0xa1, 0x30, neg_result_len + oid_len +
11338c8a9a5SSteve French ntlmssp_len);
11438c8a9a5SSteve French
11538c8a9a5SSteve French /* insert neg result */
11638c8a9a5SSteve French encode_asn_tag(buf, &ofs, 0xa0, 0x0a, 1);
11738c8a9a5SSteve French buf[ofs++] = 1;
11838c8a9a5SSteve French
11938c8a9a5SSteve French /* insert oid */
12038c8a9a5SSteve French encode_asn_tag(buf, &ofs, 0xa1, 0x06, NTLMSSP_OID_LEN);
12138c8a9a5SSteve French memcpy(buf + ofs, NTLMSSP_OID_STR, NTLMSSP_OID_LEN);
12238c8a9a5SSteve French ofs += NTLMSSP_OID_LEN;
12338c8a9a5SSteve French
12438c8a9a5SSteve French /* insert response token - ntlmssp blob */
12538c8a9a5SSteve French encode_asn_tag(buf, &ofs, 0xa2, 0x04, ntlm_blob_len);
12638c8a9a5SSteve French memcpy(buf + ofs, ntlm_blob, ntlm_blob_len);
12738c8a9a5SSteve French ofs += ntlm_blob_len;
12838c8a9a5SSteve French
12938c8a9a5SSteve French *pbuffer = buf;
13038c8a9a5SSteve French *buflen = total_len;
13138c8a9a5SSteve French return 0;
13238c8a9a5SSteve French }
13338c8a9a5SSteve French
build_spnego_ntlmssp_auth_blob(unsigned char ** pbuffer,u16 * buflen,int neg_result)13438c8a9a5SSteve French int build_spnego_ntlmssp_auth_blob(unsigned char **pbuffer, u16 *buflen,
13538c8a9a5SSteve French int neg_result)
13638c8a9a5SSteve French {
13738c8a9a5SSteve French char *buf;
13838c8a9a5SSteve French unsigned int ofs = 0;
13938c8a9a5SSteve French int neg_result_len = 4 + compute_asn_hdr_len_bytes(1) * 2 + 1;
14038c8a9a5SSteve French int total_len = 4 + compute_asn_hdr_len_bytes(neg_result_len) * 2 +
14138c8a9a5SSteve French neg_result_len;
14238c8a9a5SSteve French
14338c8a9a5SSteve French buf = kmalloc(total_len, GFP_KERNEL);
14438c8a9a5SSteve French if (!buf)
14538c8a9a5SSteve French return -ENOMEM;
14638c8a9a5SSteve French
14738c8a9a5SSteve French /* insert main gss header */
14838c8a9a5SSteve French encode_asn_tag(buf, &ofs, 0xa1, 0x30, neg_result_len);
14938c8a9a5SSteve French
15038c8a9a5SSteve French /* insert neg result */
15138c8a9a5SSteve French encode_asn_tag(buf, &ofs, 0xa0, 0x0a, 1);
15238c8a9a5SSteve French if (neg_result)
15338c8a9a5SSteve French buf[ofs++] = 2;
15438c8a9a5SSteve French else
15538c8a9a5SSteve French buf[ofs++] = 0;
15638c8a9a5SSteve French
15738c8a9a5SSteve French *pbuffer = buf;
15838c8a9a5SSteve French *buflen = total_len;
15938c8a9a5SSteve French return 0;
16038c8a9a5SSteve French }
16138c8a9a5SSteve French
ksmbd_gssapi_this_mech(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)16238c8a9a5SSteve French int ksmbd_gssapi_this_mech(void *context, size_t hdrlen, unsigned char tag,
16338c8a9a5SSteve French const void *value, size_t vlen)
16438c8a9a5SSteve French {
16538c8a9a5SSteve French enum OID oid;
16638c8a9a5SSteve French
16738c8a9a5SSteve French oid = look_up_OID(value, vlen);
16838c8a9a5SSteve French if (oid != OID_spnego) {
16938c8a9a5SSteve French char buf[50];
17038c8a9a5SSteve French
17138c8a9a5SSteve French sprint_oid(value, vlen, buf, sizeof(buf));
17238c8a9a5SSteve French ksmbd_debug(AUTH, "Unexpected OID: %s\n", buf);
17338c8a9a5SSteve French return -EBADMSG;
17438c8a9a5SSteve French }
17538c8a9a5SSteve French
17638c8a9a5SSteve French return 0;
17738c8a9a5SSteve French }
17838c8a9a5SSteve French
ksmbd_neg_token_init_mech_type(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)17938c8a9a5SSteve French int ksmbd_neg_token_init_mech_type(void *context, size_t hdrlen,
18038c8a9a5SSteve French unsigned char tag, const void *value,
18138c8a9a5SSteve French size_t vlen)
18238c8a9a5SSteve French {
18338c8a9a5SSteve French struct ksmbd_conn *conn = context;
18438c8a9a5SSteve French enum OID oid;
18538c8a9a5SSteve French int mech_type;
18638c8a9a5SSteve French
18738c8a9a5SSteve French oid = look_up_OID(value, vlen);
18838c8a9a5SSteve French if (oid == OID_ntlmssp) {
18938c8a9a5SSteve French mech_type = KSMBD_AUTH_NTLMSSP;
19038c8a9a5SSteve French } else if (oid == OID_mskrb5) {
19138c8a9a5SSteve French mech_type = KSMBD_AUTH_MSKRB5;
19238c8a9a5SSteve French } else if (oid == OID_krb5) {
19338c8a9a5SSteve French mech_type = KSMBD_AUTH_KRB5;
19438c8a9a5SSteve French } else if (oid == OID_krb5u2u) {
19538c8a9a5SSteve French mech_type = KSMBD_AUTH_KRB5U2U;
19638c8a9a5SSteve French } else {
19738c8a9a5SSteve French char buf[50];
19838c8a9a5SSteve French
19938c8a9a5SSteve French sprint_oid(value, vlen, buf, sizeof(buf));
20038c8a9a5SSteve French ksmbd_debug(AUTH, "Unexpected OID: %s\n", buf);
20138c8a9a5SSteve French return -EBADMSG;
20238c8a9a5SSteve French }
20338c8a9a5SSteve French
20438c8a9a5SSteve French conn->auth_mechs |= mech_type;
20538c8a9a5SSteve French if (conn->preferred_auth_mech == 0)
20638c8a9a5SSteve French conn->preferred_auth_mech = mech_type;
20738c8a9a5SSteve French
20838c8a9a5SSteve French return 0;
20938c8a9a5SSteve French }
21038c8a9a5SSteve French
ksmbd_neg_token_alloc(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)21138c8a9a5SSteve French static int ksmbd_neg_token_alloc(void *context, size_t hdrlen,
21238c8a9a5SSteve French unsigned char tag, const void *value,
21338c8a9a5SSteve French size_t vlen)
21438c8a9a5SSteve French {
21538c8a9a5SSteve French struct ksmbd_conn *conn = context;
21638c8a9a5SSteve French
217*92e47016SNamjae Jeon if (!vlen)
218*92e47016SNamjae Jeon return -EINVAL;
219*92e47016SNamjae Jeon
220084ba46fSYang Yingliang conn->mechToken = kmemdup_nul(value, vlen, GFP_KERNEL);
22138c8a9a5SSteve French if (!conn->mechToken)
22238c8a9a5SSteve French return -ENOMEM;
22338c8a9a5SSteve French
224*92e47016SNamjae Jeon conn->mechTokenLen = (unsigned int)vlen;
225*92e47016SNamjae Jeon
22638c8a9a5SSteve French return 0;
22738c8a9a5SSteve French }
22838c8a9a5SSteve French
ksmbd_neg_token_init_mech_token(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)22938c8a9a5SSteve French int ksmbd_neg_token_init_mech_token(void *context, size_t hdrlen,
23038c8a9a5SSteve French unsigned char tag, const void *value,
23138c8a9a5SSteve French size_t vlen)
23238c8a9a5SSteve French {
23338c8a9a5SSteve French return ksmbd_neg_token_alloc(context, hdrlen, tag, value, vlen);
23438c8a9a5SSteve French }
23538c8a9a5SSteve French
ksmbd_neg_token_targ_resp_token(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)23638c8a9a5SSteve French int ksmbd_neg_token_targ_resp_token(void *context, size_t hdrlen,
23738c8a9a5SSteve French unsigned char tag, const void *value,
23838c8a9a5SSteve French size_t vlen)
23938c8a9a5SSteve French {
24038c8a9a5SSteve French return ksmbd_neg_token_alloc(context, hdrlen, tag, value, vlen);
24138c8a9a5SSteve French }
242