1 // SPDX-License-Identifier: GPL-2.0-or-later
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
6 * Copyright (c) 2000 RP Internet (www.rpi.net.au).
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/oid_registry.h>
19 #include "connection.h"
21 #include "ksmbd_spnego_negtokeninit.asn1.h"
22 #include "ksmbd_spnego_negtokentarg.asn1.h"
24 #define NTLMSSP_OID_LEN 10
26 static char NTLMSSP_OID_STR[NTLMSSP_OID_LEN] = { 0x2b, 0x06, 0x01, 0x04, 0x01,
27 0x82, 0x37, 0x02, 0x02, 0x0a };
30 ksmbd_decode_negTokenInit(unsigned char *security_blob, int length,
31 struct ksmbd_conn *conn)
33 return asn1_ber_decoder(&ksmbd_spnego_negtokeninit_decoder, conn,
34 security_blob, length);
38 ksmbd_decode_negTokenTarg(unsigned char *security_blob, int length,
39 struct ksmbd_conn *conn)
41 return asn1_ber_decoder(&ksmbd_spnego_negtokentarg_decoder, conn,
42 security_blob, length);
45 static int compute_asn_hdr_len_bytes(int len)
49 else if (len > 0xFFFF)
59 static void encode_asn_tag(char *buf, unsigned int *ofs, char tag, char seq,
64 char hdr_len = compute_asn_hdr_len_bytes(length);
65 int len = length + 2 + hdr_len;
73 buf[index++] = 0x80 | hdr_len;
74 for (i = hdr_len - 1; i >= 0; i--)
75 buf[index++] = (len >> (i * 8)) & 0xFF;
79 len = len - (index - *ofs);
85 buf[index++] = 0x80 | hdr_len;
86 for (i = hdr_len - 1; i >= 0; i--)
87 buf[index++] = (len >> (i * 8)) & 0xFF;
90 *ofs += (index - *ofs);
93 int build_spnego_ntlmssp_neg_blob(unsigned char **pbuffer, u16 *buflen,
94 char *ntlm_blob, int ntlm_blob_len)
98 int neg_result_len = 4 + compute_asn_hdr_len_bytes(1) * 2 + 1;
99 int oid_len = 4 + compute_asn_hdr_len_bytes(NTLMSSP_OID_LEN) * 2 +
101 int ntlmssp_len = 4 + compute_asn_hdr_len_bytes(ntlm_blob_len) * 2 +
103 int total_len = 4 + compute_asn_hdr_len_bytes(neg_result_len +
104 oid_len + ntlmssp_len) * 2 +
105 neg_result_len + oid_len + ntlmssp_len;
107 buf = kmalloc(total_len, GFP_KERNEL);
111 /* insert main gss header */
112 encode_asn_tag(buf, &ofs, 0xa1, 0x30, neg_result_len + oid_len +
115 /* insert neg result */
116 encode_asn_tag(buf, &ofs, 0xa0, 0x0a, 1);
120 encode_asn_tag(buf, &ofs, 0xa1, 0x06, NTLMSSP_OID_LEN);
121 memcpy(buf + ofs, NTLMSSP_OID_STR, NTLMSSP_OID_LEN);
122 ofs += NTLMSSP_OID_LEN;
124 /* insert response token - ntlmssp blob */
125 encode_asn_tag(buf, &ofs, 0xa2, 0x04, ntlm_blob_len);
126 memcpy(buf + ofs, ntlm_blob, ntlm_blob_len);
127 ofs += ntlm_blob_len;
134 int build_spnego_ntlmssp_auth_blob(unsigned char **pbuffer, u16 *buflen,
138 unsigned int ofs = 0;
139 int neg_result_len = 4 + compute_asn_hdr_len_bytes(1) * 2 + 1;
140 int total_len = 4 + compute_asn_hdr_len_bytes(neg_result_len) * 2 +
143 buf = kmalloc(total_len, GFP_KERNEL);
147 /* insert main gss header */
148 encode_asn_tag(buf, &ofs, 0xa1, 0x30, neg_result_len);
150 /* insert neg result */
151 encode_asn_tag(buf, &ofs, 0xa0, 0x0a, 1);
162 int ksmbd_gssapi_this_mech(void *context, size_t hdrlen, unsigned char tag,
163 const void *value, size_t vlen)
167 oid = look_up_OID(value, vlen);
168 if (oid != OID_spnego) {
171 sprint_oid(value, vlen, buf, sizeof(buf));
172 ksmbd_debug(AUTH, "Unexpected OID: %s\n", buf);
179 int ksmbd_neg_token_init_mech_type(void *context, size_t hdrlen,
180 unsigned char tag, const void *value,
183 struct ksmbd_conn *conn = context;
187 oid = look_up_OID(value, vlen);
188 if (oid == OID_ntlmssp) {
189 mech_type = KSMBD_AUTH_NTLMSSP;
190 } else if (oid == OID_mskrb5) {
191 mech_type = KSMBD_AUTH_MSKRB5;
192 } else if (oid == OID_krb5) {
193 mech_type = KSMBD_AUTH_KRB5;
194 } else if (oid == OID_krb5u2u) {
195 mech_type = KSMBD_AUTH_KRB5U2U;
199 sprint_oid(value, vlen, buf, sizeof(buf));
200 ksmbd_debug(AUTH, "Unexpected OID: %s\n", buf);
204 conn->auth_mechs |= mech_type;
205 if (conn->preferred_auth_mech == 0)
206 conn->preferred_auth_mech = mech_type;
211 static int ksmbd_neg_token_alloc(void *context, size_t hdrlen,
212 unsigned char tag, const void *value,
215 struct ksmbd_conn *conn = context;
220 conn->mechToken = kmemdup_nul(value, vlen, GFP_KERNEL);
221 if (!conn->mechToken)
224 conn->mechTokenLen = (unsigned int)vlen;
229 int ksmbd_neg_token_init_mech_token(void *context, size_t hdrlen,
230 unsigned char tag, const void *value,
233 return ksmbd_neg_token_alloc(context, hdrlen, tag, value, vlen);
236 int ksmbd_neg_token_targ_resp_token(void *context, size_t hdrlen,
237 unsigned char tag, const void *value,
240 return ksmbd_neg_token_alloc(context, hdrlen, tag, value, vlen);