This source file includes following definitions.
- der_length_size
- der_write_length
- der_read_length
- g_token_size
- g_make_token_header
- g_verify_token_header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 #include <linux/types.h>
35 #include <linux/module.h>
36 #include <linux/string.h>
37 #include <linux/sunrpc/sched.h>
38 #include <linux/sunrpc/gss_asn1.h>
39
40
41 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
42 # define RPCDBG_FACILITY RPCDBG_AUTH
43 #endif
44
45
46
47 #define TWRITE_STR(ptr, str, len) \
48 memcpy((ptr), (char *) (str), (len)); \
49 (ptr) += (len);
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 static int
76 der_length_size( int length)
77 {
78 if (length < (1<<7))
79 return 1;
80 else if (length < (1<<8))
81 return 2;
82 #if (SIZEOF_INT == 2)
83 else
84 return 3;
85 #else
86 else if (length < (1<<16))
87 return 3;
88 else if (length < (1<<24))
89 return 4;
90 else
91 return 5;
92 #endif
93 }
94
95 static void
96 der_write_length(unsigned char **buf, int length)
97 {
98 if (length < (1<<7)) {
99 *(*buf)++ = (unsigned char) length;
100 } else {
101 *(*buf)++ = (unsigned char) (der_length_size(length)+127);
102 #if (SIZEOF_INT > 2)
103 if (length >= (1<<24))
104 *(*buf)++ = (unsigned char) (length>>24);
105 if (length >= (1<<16))
106 *(*buf)++ = (unsigned char) ((length>>16)&0xff);
107 #endif
108 if (length >= (1<<8))
109 *(*buf)++ = (unsigned char) ((length>>8)&0xff);
110 *(*buf)++ = (unsigned char) (length&0xff);
111 }
112 }
113
114
115
116
117 static int
118 der_read_length(unsigned char **buf, int *bufsize)
119 {
120 unsigned char sf;
121 int ret;
122
123 if (*bufsize < 1)
124 return -1;
125 sf = *(*buf)++;
126 (*bufsize)--;
127 if (sf & 0x80) {
128 if ((sf &= 0x7f) > ((*bufsize)-1))
129 return -1;
130 if (sf > SIZEOF_INT)
131 return -1;
132 ret = 0;
133 for (; sf; sf--) {
134 ret = (ret<<8) + (*(*buf)++);
135 (*bufsize)--;
136 }
137 } else {
138 ret = sf;
139 }
140
141 return ret;
142 }
143
144
145
146 int
147 g_token_size(struct xdr_netobj *mech, unsigned int body_size)
148 {
149
150 body_size += 2 + (int) mech->len;
151 return 1 + der_length_size(body_size) + body_size;
152 }
153
154 EXPORT_SYMBOL_GPL(g_token_size);
155
156
157
158
159 void
160 g_make_token_header(struct xdr_netobj *mech, int body_size, unsigned char **buf)
161 {
162 *(*buf)++ = 0x60;
163 der_write_length(buf, 2 + mech->len + body_size);
164 *(*buf)++ = 0x06;
165 *(*buf)++ = (unsigned char) mech->len;
166 TWRITE_STR(*buf, mech->data, ((int) mech->len));
167 }
168
169 EXPORT_SYMBOL_GPL(g_make_token_header);
170
171
172
173
174
175
176
177
178
179 u32
180 g_verify_token_header(struct xdr_netobj *mech, int *body_size,
181 unsigned char **buf_in, int toksize)
182 {
183 unsigned char *buf = *buf_in;
184 int seqsize;
185 struct xdr_netobj toid;
186 int ret = 0;
187
188 if ((toksize-=1) < 0)
189 return G_BAD_TOK_HEADER;
190 if (*buf++ != 0x60)
191 return G_BAD_TOK_HEADER;
192
193 if ((seqsize = der_read_length(&buf, &toksize)) < 0)
194 return G_BAD_TOK_HEADER;
195
196 if (seqsize != toksize)
197 return G_BAD_TOK_HEADER;
198
199 if ((toksize-=1) < 0)
200 return G_BAD_TOK_HEADER;
201 if (*buf++ != 0x06)
202 return G_BAD_TOK_HEADER;
203
204 if ((toksize-=1) < 0)
205 return G_BAD_TOK_HEADER;
206 toid.len = *buf++;
207
208 if ((toksize-=toid.len) < 0)
209 return G_BAD_TOK_HEADER;
210 toid.data = buf;
211 buf+=toid.len;
212
213 if (! g_OID_equal(&toid, mech))
214 ret = G_WRONG_MECH;
215
216
217
218
219 if ((toksize-=2) < 0)
220 return G_BAD_TOK_HEADER;
221
222 if (ret)
223 return ret;
224
225 if (!ret) {
226 *buf_in = buf;
227 *body_size = toksize;
228 }
229
230 return ret;
231 }
232
233 EXPORT_SYMBOL_GPL(g_verify_token_header);