/src/usrsctp/usrsctplib/netinet/sctp_auth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*- |
2 | | * SPDX-License-Identifier: BSD-3-Clause |
3 | | * |
4 | | * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. |
5 | | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. |
6 | | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions are met: |
10 | | * |
11 | | * a) Redistributions of source code must retain the above copyright notice, |
12 | | * this list of conditions and the following disclaimer. |
13 | | * |
14 | | * b) Redistributions in binary form must reproduce the above copyright |
15 | | * notice, this list of conditions and the following disclaimer in |
16 | | * the documentation and/or other materials provided with the distribution. |
17 | | * |
18 | | * c) Neither the name of Cisco Systems, Inc. nor the names of its |
19 | | * contributors may be used to endorse or promote products derived |
20 | | * from this software without specific prior written permission. |
21 | | * |
22 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
23 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
24 | | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
26 | | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
27 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
28 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
29 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
30 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
31 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
32 | | * THE POSSIBILITY OF SUCH DAMAGE. |
33 | | */ |
34 | | |
35 | | #include <netinet/sctp_os.h> |
36 | | #include <netinet/sctp.h> |
37 | | #include <netinet/sctp_header.h> |
38 | | #include <netinet/sctp_pcb.h> |
39 | | #include <netinet/sctp_var.h> |
40 | | #include <netinet/sctp_sysctl.h> |
41 | | #include <netinet/sctputil.h> |
42 | | #include <netinet/sctp_indata.h> |
43 | | #include <netinet/sctp_output.h> |
44 | | #include <netinet/sctp_auth.h> |
45 | | |
46 | | #ifdef SCTP_DEBUG |
47 | | #define SCTP_AUTH_DEBUG (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1) |
48 | | #define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2) |
49 | | #endif /* SCTP_DEBUG */ |
50 | | |
51 | | void |
52 | | sctp_clear_chunklist(sctp_auth_chklist_t *chklist) |
53 | 31.2k | { |
54 | 31.2k | memset(chklist, 0, sizeof(*chklist)); |
55 | | /* chklist->num_chunks = 0; */ |
56 | 31.2k | } |
57 | | |
58 | | sctp_auth_chklist_t * |
59 | | sctp_alloc_chunklist(void) |
60 | 31.2k | { |
61 | 31.2k | sctp_auth_chklist_t *chklist; |
62 | | |
63 | 31.2k | SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist), |
64 | 31.2k | SCTP_M_AUTH_CL); |
65 | 31.2k | if (chklist == NULL) { |
66 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n"); |
67 | 31.2k | } else { |
68 | 31.2k | sctp_clear_chunklist(chklist); |
69 | 31.2k | } |
70 | 31.2k | return (chklist); |
71 | 31.2k | } |
72 | | |
73 | | void |
74 | | sctp_free_chunklist(sctp_auth_chklist_t *list) |
75 | 31.2k | { |
76 | 31.2k | if (list != NULL) |
77 | 31.2k | SCTP_FREE(list, SCTP_M_AUTH_CL); |
78 | 31.2k | } |
79 | | |
80 | | sctp_auth_chklist_t * |
81 | | sctp_copy_chunklist(sctp_auth_chklist_t *list) |
82 | 11.4k | { |
83 | 11.4k | sctp_auth_chklist_t *new_list; |
84 | | |
85 | 11.4k | if (list == NULL) |
86 | 0 | return (NULL); |
87 | | |
88 | | /* get a new list */ |
89 | 11.4k | new_list = sctp_alloc_chunklist(); |
90 | 11.4k | if (new_list == NULL) |
91 | 0 | return (NULL); |
92 | | /* copy it */ |
93 | 11.4k | memcpy(new_list, list, sizeof(*new_list)); |
94 | | |
95 | 11.4k | return (new_list); |
96 | 11.4k | } |
97 | | |
98 | | /* |
99 | | * add a chunk to the required chunks list |
100 | | */ |
101 | | int |
102 | | sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t *list) |
103 | 44.6k | { |
104 | 44.6k | if (list == NULL) |
105 | 0 | return (-1); |
106 | | |
107 | | /* is chunk restricted? */ |
108 | 44.6k | if ((chunk == SCTP_INITIATION) || |
109 | 44.6k | (chunk == SCTP_INITIATION_ACK) || |
110 | 44.6k | (chunk == SCTP_SHUTDOWN_COMPLETE) || |
111 | 44.6k | (chunk == SCTP_AUTHENTICATION)) { |
112 | 830 | return (-1); |
113 | 830 | } |
114 | 43.8k | if (list->chunks[chunk] == 0) { |
115 | 41.0k | list->chunks[chunk] = 1; |
116 | 41.0k | list->num_chunks++; |
117 | 41.0k | SCTPDBG(SCTP_DEBUG_AUTH1, |
118 | 41.0k | "SCTP: added chunk %u (0x%02x) to Auth list\n", |
119 | 41.0k | chunk, chunk); |
120 | 41.0k | } |
121 | 43.8k | return (0); |
122 | 44.6k | } |
123 | | |
124 | | /* |
125 | | * delete a chunk from the required chunks list |
126 | | */ |
127 | | int |
128 | | sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t *list) |
129 | 0 | { |
130 | 0 | if (list == NULL) |
131 | 0 | return (-1); |
132 | | |
133 | 0 | if (list->chunks[chunk] == 1) { |
134 | 0 | list->chunks[chunk] = 0; |
135 | 0 | list->num_chunks--; |
136 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, |
137 | 0 | "SCTP: deleted chunk %u (0x%02x) from Auth list\n", |
138 | 0 | chunk, chunk); |
139 | 0 | } |
140 | 0 | return (0); |
141 | 0 | } |
142 | | |
143 | | size_t |
144 | | sctp_auth_get_chklist_size(const sctp_auth_chklist_t *list) |
145 | 22.0k | { |
146 | 22.0k | if (list == NULL) |
147 | 0 | return (0); |
148 | 22.0k | else |
149 | 22.0k | return (list->num_chunks); |
150 | 22.0k | } |
151 | | |
152 | | /* |
153 | | * return the current number and list of required chunks caller must |
154 | | * guarantee ptr has space for up to 256 bytes |
155 | | */ |
156 | | int |
157 | | sctp_serialize_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr) |
158 | 22.7k | { |
159 | 22.7k | int i, count = 0; |
160 | | |
161 | 22.7k | if (list == NULL) |
162 | 0 | return (0); |
163 | | |
164 | 5.83M | for (i = 0; i < 256; i++) { |
165 | 5.81M | if (list->chunks[i] != 0) { |
166 | 45.4k | *ptr++ = i; |
167 | 45.4k | count++; |
168 | 45.4k | } |
169 | 5.81M | } |
170 | 22.7k | return (count); |
171 | 22.7k | } |
172 | | |
173 | | int |
174 | | sctp_pack_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr) |
175 | 0 | { |
176 | 0 | int i, size = 0; |
177 | |
|
178 | 0 | if (list == NULL) |
179 | 0 | return (0); |
180 | | |
181 | 0 | if (list->num_chunks <= 32) { |
182 | | /* just list them, one byte each */ |
183 | 0 | for (i = 0; i < 256; i++) { |
184 | 0 | if (list->chunks[i] != 0) { |
185 | 0 | *ptr++ = i; |
186 | 0 | size++; |
187 | 0 | } |
188 | 0 | } |
189 | 0 | } else { |
190 | 0 | int index, offset; |
191 | | |
192 | | /* pack into a 32 byte bitfield */ |
193 | 0 | for (i = 0; i < 256; i++) { |
194 | 0 | if (list->chunks[i] != 0) { |
195 | 0 | index = i / 8; |
196 | 0 | offset = i % 8; |
197 | 0 | ptr[index] |= (1 << offset); |
198 | 0 | } |
199 | 0 | } |
200 | 0 | size = 32; |
201 | 0 | } |
202 | 0 | return (size); |
203 | 0 | } |
204 | | |
205 | | int |
206 | | sctp_unpack_auth_chunks(const uint8_t *ptr, uint8_t num_chunks, |
207 | | sctp_auth_chklist_t *list) |
208 | 0 | { |
209 | 0 | int i; |
210 | 0 | int size; |
211 | |
|
212 | 0 | if (list == NULL) |
213 | 0 | return (0); |
214 | | |
215 | 0 | if (num_chunks <= 32) { |
216 | | /* just pull them, one byte each */ |
217 | 0 | for (i = 0; i < num_chunks; i++) { |
218 | 0 | (void)sctp_auth_add_chunk(*ptr++, list); |
219 | 0 | } |
220 | 0 | size = num_chunks; |
221 | 0 | } else { |
222 | 0 | int index, offset; |
223 | | |
224 | | /* unpack from a 32 byte bitfield */ |
225 | 0 | for (index = 0; index < 32; index++) { |
226 | 0 | for (offset = 0; offset < 8; offset++) { |
227 | 0 | if (ptr[index] & (1 << offset)) { |
228 | 0 | (void)sctp_auth_add_chunk((index * 8) + offset, list); |
229 | 0 | } |
230 | 0 | } |
231 | 0 | } |
232 | 0 | size = 32; |
233 | 0 | } |
234 | 0 | return (size); |
235 | 0 | } |
236 | | |
237 | | /* |
238 | | * allocate structure space for a key of length keylen |
239 | | */ |
240 | | sctp_key_t * |
241 | | sctp_alloc_key(uint32_t keylen) |
242 | 20.7k | { |
243 | 20.7k | sctp_key_t *new_key; |
244 | | |
245 | 20.7k | SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen, |
246 | 20.7k | SCTP_M_AUTH_KY); |
247 | 20.7k | if (new_key == NULL) { |
248 | | /* out of memory */ |
249 | 0 | return (NULL); |
250 | 0 | } |
251 | 20.7k | new_key->keylen = keylen; |
252 | 20.7k | return (new_key); |
253 | 20.7k | } |
254 | | |
255 | | void |
256 | | sctp_free_key(sctp_key_t *key) |
257 | 55.9k | { |
258 | 55.9k | if (key != NULL) |
259 | 55.9k | SCTP_FREE(key,SCTP_M_AUTH_KY); |
260 | 55.9k | } |
261 | | |
262 | | void |
263 | | sctp_print_key(sctp_key_t *key, const char *str) |
264 | 0 | { |
265 | 0 | uint32_t i; |
266 | |
|
267 | 0 | if (key == NULL) { |
268 | 0 | SCTP_PRINTF("%s: [Null key]\n", str); |
269 | 0 | return; |
270 | 0 | } |
271 | 0 | SCTP_PRINTF("%s: len %u, ", str, key->keylen); |
272 | 0 | if (key->keylen) { |
273 | 0 | for (i = 0; i < key->keylen; i++) |
274 | 0 | SCTP_PRINTF("%02x", key->key[i]); |
275 | 0 | SCTP_PRINTF("\n"); |
276 | 0 | } else { |
277 | 0 | SCTP_PRINTF("[Null key]\n"); |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | | void |
282 | | sctp_show_key(sctp_key_t *key, const char *str) |
283 | 0 | { |
284 | 0 | uint32_t i; |
285 | |
|
286 | 0 | if (key == NULL) { |
287 | 0 | SCTP_PRINTF("%s: [Null key]\n", str); |
288 | 0 | return; |
289 | 0 | } |
290 | 0 | SCTP_PRINTF("%s: len %u, ", str, key->keylen); |
291 | 0 | if (key->keylen) { |
292 | 0 | for (i = 0; i < key->keylen; i++) |
293 | 0 | SCTP_PRINTF("%02x", key->key[i]); |
294 | 0 | SCTP_PRINTF("\n"); |
295 | 0 | } else { |
296 | 0 | SCTP_PRINTF("[Null key]\n"); |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | static uint32_t |
301 | | sctp_get_keylen(sctp_key_t *key) |
302 | 4.83k | { |
303 | 4.83k | if (key != NULL) |
304 | 3.62k | return (key->keylen); |
305 | 1.20k | else |
306 | 1.20k | return (0); |
307 | 4.83k | } |
308 | | |
309 | | /* |
310 | | * generate a new random key of length 'keylen' |
311 | | */ |
312 | | sctp_key_t * |
313 | | sctp_generate_random_key(uint32_t keylen) |
314 | 0 | { |
315 | 0 | sctp_key_t *new_key; |
316 | |
|
317 | 0 | new_key = sctp_alloc_key(keylen); |
318 | 0 | if (new_key == NULL) { |
319 | | /* out of memory */ |
320 | 0 | return (NULL); |
321 | 0 | } |
322 | 0 | SCTP_READ_RANDOM(new_key->key, keylen); |
323 | 0 | new_key->keylen = keylen; |
324 | 0 | return (new_key); |
325 | 0 | } |
326 | | |
327 | | sctp_key_t * |
328 | | sctp_set_key(uint8_t *key, uint32_t keylen) |
329 | 0 | { |
330 | 0 | sctp_key_t *new_key; |
331 | |
|
332 | 0 | new_key = sctp_alloc_key(keylen); |
333 | 0 | if (new_key == NULL) { |
334 | | /* out of memory */ |
335 | 0 | return (NULL); |
336 | 0 | } |
337 | 0 | memcpy(new_key->key, key, keylen); |
338 | 0 | return (new_key); |
339 | 0 | } |
340 | | |
341 | | /*- |
342 | | * given two keys of variable size, compute which key is "larger/smaller" |
343 | | * returns: 1 if key1 > key2 |
344 | | * -1 if key1 < key2 |
345 | | * 0 if key1 = key2 |
346 | | */ |
347 | | static int |
348 | | sctp_compare_key(sctp_key_t *key1, sctp_key_t *key2) |
349 | 604 | { |
350 | 604 | uint32_t maxlen; |
351 | 604 | uint32_t i; |
352 | 604 | uint32_t key1len, key2len; |
353 | 604 | uint8_t *key_1, *key_2; |
354 | 604 | uint8_t val1, val2; |
355 | | |
356 | | /* sanity/length check */ |
357 | 604 | key1len = sctp_get_keylen(key1); |
358 | 604 | key2len = sctp_get_keylen(key2); |
359 | 604 | if ((key1len == 0) && (key2len == 0)) |
360 | 0 | return (0); |
361 | 604 | else if (key1len == 0) |
362 | 0 | return (-1); |
363 | 604 | else if (key2len == 0) |
364 | 0 | return (1); |
365 | | |
366 | 604 | if (key1len < key2len) { |
367 | 599 | maxlen = key2len; |
368 | 599 | } else { |
369 | 5 | maxlen = key1len; |
370 | 5 | } |
371 | 604 | key_1 = key1->key; |
372 | 604 | key_2 = key2->key; |
373 | | /* check for numeric equality */ |
374 | 668 | for (i = 0; i < maxlen; i++) { |
375 | | /* left-pad with zeros */ |
376 | 668 | val1 = (i < (maxlen - key1len)) ? 0 : *(key_1++); |
377 | 668 | val2 = (i < (maxlen - key2len)) ? 0 : *(key_2++); |
378 | 668 | if (val1 > val2) { |
379 | 3 | return (1); |
380 | 665 | } else if (val1 < val2) { |
381 | 601 | return (-1); |
382 | 601 | } |
383 | 668 | } |
384 | | /* keys are equal value, so check lengths */ |
385 | 0 | if (key1len == key2len) |
386 | 0 | return (0); |
387 | 0 | else if (key1len < key2len) |
388 | 0 | return (-1); |
389 | 0 | else |
390 | 0 | return (1); |
391 | 0 | } |
392 | | |
393 | | /* |
394 | | * generate the concatenated keying material based on the two keys and the |
395 | | * shared key (if available). draft-ietf-tsvwg-auth specifies the specific |
396 | | * order for concatenation |
397 | | */ |
398 | | sctp_key_t * |
399 | | sctp_compute_hashkey(sctp_key_t *key1, sctp_key_t *key2, sctp_key_t *shared) |
400 | 604 | { |
401 | 604 | uint32_t keylen; |
402 | 604 | sctp_key_t *new_key; |
403 | 604 | uint8_t *key_ptr; |
404 | | |
405 | 604 | keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) + |
406 | 604 | sctp_get_keylen(shared); |
407 | | |
408 | 604 | if (keylen > 0) { |
409 | | /* get space for the new key */ |
410 | 604 | new_key = sctp_alloc_key(keylen); |
411 | 604 | if (new_key == NULL) { |
412 | | /* out of memory */ |
413 | 0 | return (NULL); |
414 | 0 | } |
415 | 604 | new_key->keylen = keylen; |
416 | 604 | key_ptr = new_key->key; |
417 | 604 | } else { |
418 | | /* all keys empty/null?! */ |
419 | 0 | return (NULL); |
420 | 0 | } |
421 | | |
422 | | /* concatenate the keys */ |
423 | 604 | if (sctp_compare_key(key1, key2) <= 0) { |
424 | | /* key is shared + key1 + key2 */ |
425 | 601 | if (sctp_get_keylen(shared)) { |
426 | 0 | memcpy(key_ptr, shared->key, shared->keylen); |
427 | 0 | key_ptr += shared->keylen; |
428 | 0 | } |
429 | 601 | if (sctp_get_keylen(key1)) { |
430 | 601 | memcpy(key_ptr, key1->key, key1->keylen); |
431 | 601 | key_ptr += key1->keylen; |
432 | 601 | } |
433 | 601 | if (sctp_get_keylen(key2)) { |
434 | 601 | memcpy(key_ptr, key2->key, key2->keylen); |
435 | 601 | } |
436 | 601 | } else { |
437 | | /* key is shared + key2 + key1 */ |
438 | 3 | if (sctp_get_keylen(shared)) { |
439 | 0 | memcpy(key_ptr, shared->key, shared->keylen); |
440 | 0 | key_ptr += shared->keylen; |
441 | 0 | } |
442 | 3 | if (sctp_get_keylen(key2)) { |
443 | 3 | memcpy(key_ptr, key2->key, key2->keylen); |
444 | 3 | key_ptr += key2->keylen; |
445 | 3 | } |
446 | 3 | if (sctp_get_keylen(key1)) { |
447 | 3 | memcpy(key_ptr, key1->key, key1->keylen); |
448 | 3 | } |
449 | 3 | } |
450 | 604 | return (new_key); |
451 | 604 | } |
452 | | |
453 | | sctp_sharedkey_t * |
454 | | sctp_alloc_sharedkey(void) |
455 | 22.8k | { |
456 | 22.8k | sctp_sharedkey_t *new_key; |
457 | | |
458 | 22.8k | SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key), |
459 | 22.8k | SCTP_M_AUTH_KY); |
460 | 22.8k | if (new_key == NULL) { |
461 | | /* out of memory */ |
462 | 0 | return (NULL); |
463 | 0 | } |
464 | 22.8k | new_key->keyid = 0; |
465 | 22.8k | new_key->key = NULL; |
466 | 22.8k | new_key->refcount = 1; |
467 | 22.8k | new_key->deactivated = 0; |
468 | 22.8k | return (new_key); |
469 | 22.8k | } |
470 | | |
471 | | void |
472 | | sctp_free_sharedkey(sctp_sharedkey_t *skey) |
473 | 22.8k | { |
474 | 22.8k | if (skey == NULL) |
475 | 0 | return; |
476 | | |
477 | 22.8k | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) { |
478 | 22.8k | if (skey->key != NULL) |
479 | 0 | sctp_free_key(skey->key); |
480 | 22.8k | SCTP_FREE(skey, SCTP_M_AUTH_KY); |
481 | 22.8k | } |
482 | 22.8k | } |
483 | | |
484 | | sctp_sharedkey_t * |
485 | | sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id) |
486 | 604 | { |
487 | 604 | sctp_sharedkey_t *skey; |
488 | | |
489 | 604 | LIST_FOREACH(skey, shared_keys, next) { |
490 | 604 | if (skey->keyid == key_id) |
491 | 604 | return (skey); |
492 | 604 | } |
493 | 0 | return (NULL); |
494 | 604 | } |
495 | | |
496 | | int |
497 | | sctp_insert_sharedkey(struct sctp_keyhead *shared_keys, |
498 | | sctp_sharedkey_t *new_skey) |
499 | 22.8k | { |
500 | 22.8k | sctp_sharedkey_t *skey; |
501 | | |
502 | 22.8k | if ((shared_keys == NULL) || (new_skey == NULL)) |
503 | 0 | return (EINVAL); |
504 | | |
505 | | /* insert into an empty list? */ |
506 | 22.8k | if (LIST_EMPTY(shared_keys)) { |
507 | 22.8k | LIST_INSERT_HEAD(shared_keys, new_skey, next); |
508 | 22.8k | return (0); |
509 | 22.8k | } |
510 | | /* insert into the existing list, ordered by key id */ |
511 | 0 | LIST_FOREACH(skey, shared_keys, next) { |
512 | 0 | if (new_skey->keyid < skey->keyid) { |
513 | | /* insert it before here */ |
514 | 0 | LIST_INSERT_BEFORE(skey, new_skey, next); |
515 | 0 | return (0); |
516 | 0 | } else if (new_skey->keyid == skey->keyid) { |
517 | | /* replace the existing key */ |
518 | | /* verify this key *can* be replaced */ |
519 | 0 | if ((skey->deactivated) || (skey->refcount > 1)) { |
520 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, |
521 | 0 | "can't replace shared key id %u\n", |
522 | 0 | new_skey->keyid); |
523 | 0 | return (EBUSY); |
524 | 0 | } |
525 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, |
526 | 0 | "replacing shared key id %u\n", |
527 | 0 | new_skey->keyid); |
528 | 0 | LIST_INSERT_BEFORE(skey, new_skey, next); |
529 | 0 | LIST_REMOVE(skey, next); |
530 | 0 | sctp_free_sharedkey(skey); |
531 | 0 | return (0); |
532 | 0 | } |
533 | 0 | if (LIST_NEXT(skey, next) == NULL) { |
534 | | /* belongs at the end of the list */ |
535 | 0 | LIST_INSERT_AFTER(skey, new_skey, next); |
536 | 0 | return (0); |
537 | 0 | } |
538 | 0 | } |
539 | | /* shouldn't reach here */ |
540 | 0 | return (EINVAL); |
541 | 0 | } |
542 | | |
543 | | void |
544 | | sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id) |
545 | 0 | { |
546 | 0 | sctp_sharedkey_t *skey; |
547 | | |
548 | | /* find the shared key */ |
549 | 0 | skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id); |
550 | | |
551 | | /* bump the ref count */ |
552 | 0 | if (skey) { |
553 | 0 | atomic_add_int(&skey->refcount, 1); |
554 | 0 | SCTPDBG(SCTP_DEBUG_AUTH2, |
555 | 0 | "%s: stcb %p key %u refcount acquire to %d\n", |
556 | 0 | __func__, (void *)stcb, key_id, skey->refcount); |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | void |
561 | | sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked) |
562 | 0 | { |
563 | 0 | sctp_sharedkey_t *skey; |
564 | | |
565 | | /* find the shared key */ |
566 | 0 | skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id); |
567 | | |
568 | | /* decrement the ref count */ |
569 | 0 | if (skey) { |
570 | 0 | SCTPDBG(SCTP_DEBUG_AUTH2, |
571 | 0 | "%s: stcb %p key %u refcount release to %d\n", |
572 | 0 | __func__, (void *)stcb, key_id, skey->refcount); |
573 | | |
574 | | /* see if a notification should be generated */ |
575 | 0 | if ((skey->refcount <= 2) && (skey->deactivated)) { |
576 | | /* notify ULP that key is no longer used */ |
577 | 0 | sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, |
578 | 0 | 0, &key_id, so_locked); |
579 | 0 | SCTPDBG(SCTP_DEBUG_AUTH2, |
580 | 0 | "%s: stcb %p key %u no longer used, %d\n", |
581 | 0 | __func__, (void *)stcb, key_id, skey->refcount); |
582 | 0 | } |
583 | 0 | sctp_free_sharedkey(skey); |
584 | 0 | } |
585 | 0 | } |
586 | | |
587 | | static sctp_sharedkey_t * |
588 | | sctp_copy_sharedkey(const sctp_sharedkey_t *skey) |
589 | 11.4k | { |
590 | 11.4k | sctp_sharedkey_t *new_skey; |
591 | | |
592 | 11.4k | if (skey == NULL) |
593 | 0 | return (NULL); |
594 | 11.4k | new_skey = sctp_alloc_sharedkey(); |
595 | 11.4k | if (new_skey == NULL) |
596 | 0 | return (NULL); |
597 | 11.4k | if (skey->key != NULL) |
598 | 0 | new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen); |
599 | 11.4k | else |
600 | 11.4k | new_skey->key = NULL; |
601 | 11.4k | new_skey->keyid = skey->keyid; |
602 | 11.4k | return (new_skey); |
603 | 11.4k | } |
604 | | |
605 | | int |
606 | | sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest) |
607 | 11.4k | { |
608 | 11.4k | sctp_sharedkey_t *skey, *new_skey; |
609 | 11.4k | int count = 0; |
610 | | |
611 | 11.4k | if ((src == NULL) || (dest == NULL)) |
612 | 0 | return (0); |
613 | 11.4k | LIST_FOREACH(skey, src, next) { |
614 | 11.4k | new_skey = sctp_copy_sharedkey(skey); |
615 | 11.4k | if (new_skey != NULL) { |
616 | 11.4k | if (sctp_insert_sharedkey(dest, new_skey)) { |
617 | 0 | sctp_free_sharedkey(new_skey); |
618 | 11.4k | } else { |
619 | 11.4k | count++; |
620 | 11.4k | } |
621 | 11.4k | } |
622 | 11.4k | } |
623 | 11.4k | return (count); |
624 | 11.4k | } |
625 | | |
626 | | sctp_hmaclist_t * |
627 | | sctp_alloc_hmaclist(uint16_t num_hmacs) |
628 | 31.1k | { |
629 | 31.1k | sctp_hmaclist_t *new_list; |
630 | 31.1k | int alloc_size; |
631 | | |
632 | 31.1k | alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]); |
633 | 31.1k | SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size, |
634 | 31.1k | SCTP_M_AUTH_HL); |
635 | 31.1k | if (new_list == NULL) { |
636 | | /* out of memory */ |
637 | 0 | return (NULL); |
638 | 0 | } |
639 | 31.1k | new_list->max_algo = num_hmacs; |
640 | 31.1k | new_list->num_algo = 0; |
641 | 31.1k | return (new_list); |
642 | 31.1k | } |
643 | | |
644 | | void |
645 | | sctp_free_hmaclist(sctp_hmaclist_t *list) |
646 | 31.1k | { |
647 | 31.1k | if (list != NULL) { |
648 | 31.1k | SCTP_FREE(list,SCTP_M_AUTH_HL); |
649 | 31.1k | } |
650 | 31.1k | } |
651 | | |
652 | | int |
653 | | sctp_auth_add_hmacid(sctp_hmaclist_t *list, uint16_t hmac_id) |
654 | 29.8k | { |
655 | 29.8k | int i; |
656 | 29.8k | if (list == NULL) |
657 | 0 | return (-1); |
658 | 29.8k | if (list->num_algo == list->max_algo) { |
659 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, |
660 | 0 | "SCTP: HMAC id list full, ignoring add %u\n", hmac_id); |
661 | 0 | return (-1); |
662 | 0 | } |
663 | | #if defined(SCTP_SUPPORT_HMAC_SHA256) |
664 | | if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) && |
665 | | (hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) { |
666 | | #else |
667 | 29.8k | if (hmac_id != SCTP_AUTH_HMAC_ID_SHA1) { |
668 | 10.0k | #endif |
669 | 10.0k | return (-1); |
670 | 10.0k | } |
671 | | /* Now is it already in the list */ |
672 | 19.7k | for (i = 0; i < list->num_algo; i++) { |
673 | 69 | if (list->hmac[i] == hmac_id) { |
674 | | /* already in list */ |
675 | 69 | return (-1); |
676 | 69 | } |
677 | 69 | } |
678 | 19.7k | SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id); |
679 | 19.7k | list->hmac[list->num_algo++] = hmac_id; |
680 | 19.7k | return (0); |
681 | 19.7k | } |
682 | | |
683 | | sctp_hmaclist_t * |
684 | | sctp_copy_hmaclist(sctp_hmaclist_t *list) |
685 | 11.4k | { |
686 | 11.4k | sctp_hmaclist_t *new_list; |
687 | 11.4k | int i; |
688 | | |
689 | 11.4k | if (list == NULL) |
690 | 0 | return (NULL); |
691 | | /* get a new list */ |
692 | 11.4k | new_list = sctp_alloc_hmaclist(list->max_algo); |
693 | 11.4k | if (new_list == NULL) |
694 | 0 | return (NULL); |
695 | | /* copy it */ |
696 | 11.4k | new_list->max_algo = list->max_algo; |
697 | 11.4k | new_list->num_algo = list->num_algo; |
698 | 22.8k | for (i = 0; i < list->num_algo; i++) |
699 | 11.4k | new_list->hmac[i] = list->hmac[i]; |
700 | 11.4k | return (new_list); |
701 | 11.4k | } |
702 | | |
703 | | sctp_hmaclist_t * |
704 | | sctp_default_supported_hmaclist(void) |
705 | 11.4k | { |
706 | 11.4k | sctp_hmaclist_t *new_list; |
707 | | |
708 | | #if defined(SCTP_SUPPORT_HMAC_SHA256) |
709 | | new_list = sctp_alloc_hmaclist(2); |
710 | | #else |
711 | 11.4k | new_list = sctp_alloc_hmaclist(1); |
712 | 11.4k | #endif |
713 | 11.4k | if (new_list == NULL) |
714 | 0 | return (NULL); |
715 | | #if defined(SCTP_SUPPORT_HMAC_SHA256) |
716 | | /* We prefer SHA256, so list it first */ |
717 | | (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256); |
718 | | #endif |
719 | 11.4k | (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1); |
720 | 11.4k | return (new_list); |
721 | 11.4k | } |
722 | | |
723 | | /*- |
724 | | * HMAC algos are listed in priority/preference order |
725 | | * find the best HMAC id to use for the peer based on local support |
726 | | */ |
727 | | uint16_t |
728 | | sctp_negotiate_hmacid(sctp_hmaclist_t *peer, sctp_hmaclist_t *local) |
729 | 8.77k | { |
730 | 8.77k | int i, j; |
731 | | |
732 | 8.77k | if ((local == NULL) || (peer == NULL)) |
733 | 477 | return (SCTP_AUTH_HMAC_ID_RSVD); |
734 | | |
735 | 8.30k | for (i = 0; i < peer->num_algo; i++) { |
736 | 8.30k | for (j = 0; j < local->num_algo; j++) { |
737 | 8.30k | if (peer->hmac[i] == local->hmac[j]) { |
738 | | /* found the "best" one */ |
739 | 8.30k | SCTPDBG(SCTP_DEBUG_AUTH1, |
740 | 8.30k | "SCTP: negotiated peer HMAC id %u\n", |
741 | 8.30k | peer->hmac[i]); |
742 | 8.30k | return (peer->hmac[i]); |
743 | 8.30k | } |
744 | 8.30k | } |
745 | 8.30k | } |
746 | | /* didn't find one! */ |
747 | 0 | return (SCTP_AUTH_HMAC_ID_RSVD); |
748 | 8.30k | } |
749 | | |
750 | | /*- |
751 | | * serialize the HMAC algo list and return space used |
752 | | * caller must guarantee ptr has appropriate space |
753 | | */ |
754 | | int |
755 | | sctp_serialize_hmaclist(sctp_hmaclist_t *list, uint8_t *ptr) |
756 | 34.1k | { |
757 | 34.1k | int i; |
758 | 34.1k | uint16_t hmac_id; |
759 | | |
760 | 34.1k | if (list == NULL) |
761 | 0 | return (0); |
762 | | |
763 | 68.2k | for (i = 0; i < list->num_algo; i++) { |
764 | 34.1k | hmac_id = htons(list->hmac[i]); |
765 | 34.1k | memcpy(ptr, &hmac_id, sizeof(hmac_id)); |
766 | 34.1k | ptr += sizeof(hmac_id); |
767 | 34.1k | } |
768 | 34.1k | return (list->num_algo * sizeof(hmac_id)); |
769 | 34.1k | } |
770 | | |
771 | | int |
772 | | sctp_verify_hmac_param (struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs) |
773 | 9.10k | { |
774 | 9.10k | uint32_t i; |
775 | | |
776 | 21.1k | for (i = 0; i < num_hmacs; i++) { |
777 | 21.0k | if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) { |
778 | 9.05k | return (0); |
779 | 9.05k | } |
780 | 21.0k | } |
781 | 53 | return (-1); |
782 | 9.10k | } |
783 | | |
784 | | sctp_authinfo_t * |
785 | | sctp_alloc_authinfo(void) |
786 | 0 | { |
787 | 0 | sctp_authinfo_t *new_authinfo; |
788 | |
|
789 | 0 | SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo), |
790 | 0 | SCTP_M_AUTH_IF); |
791 | |
|
792 | 0 | if (new_authinfo == NULL) { |
793 | | /* out of memory */ |
794 | 0 | return (NULL); |
795 | 0 | } |
796 | 0 | memset(new_authinfo, 0, sizeof(*new_authinfo)); |
797 | 0 | return (new_authinfo); |
798 | 0 | } |
799 | | |
800 | | void |
801 | | sctp_free_authinfo(sctp_authinfo_t *authinfo) |
802 | 11.4k | { |
803 | 11.4k | if (authinfo == NULL) |
804 | 0 | return; |
805 | | |
806 | 11.4k | if (authinfo->random != NULL) |
807 | 11.4k | sctp_free_key(authinfo->random); |
808 | 11.4k | if (authinfo->peer_random != NULL) |
809 | 8.77k | sctp_free_key(authinfo->peer_random); |
810 | 11.4k | if (authinfo->assoc_key != NULL) |
811 | 604 | sctp_free_key(authinfo->assoc_key); |
812 | 11.4k | if (authinfo->recv_key != NULL) |
813 | 0 | sctp_free_key(authinfo->recv_key); |
814 | | |
815 | | /* We are NOT dynamically allocating authinfo's right now... */ |
816 | | /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */ |
817 | 11.4k | } |
818 | | |
819 | | uint32_t |
820 | | sctp_get_auth_chunk_len(uint16_t hmac_algo) |
821 | 717 | { |
822 | 717 | int size; |
823 | | |
824 | 717 | size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo); |
825 | 717 | return (SCTP_SIZE32(size)); |
826 | 717 | } |
827 | | |
828 | | uint32_t |
829 | | sctp_get_hmac_digest_len(uint16_t hmac_algo) |
830 | 16.3k | { |
831 | 16.3k | switch (hmac_algo) { |
832 | 16.3k | case SCTP_AUTH_HMAC_ID_SHA1: |
833 | 16.3k | return (SCTP_AUTH_DIGEST_LEN_SHA1); |
834 | | #if defined(SCTP_SUPPORT_HMAC_SHA256) |
835 | | case SCTP_AUTH_HMAC_ID_SHA256: |
836 | | return (SCTP_AUTH_DIGEST_LEN_SHA256); |
837 | | #endif |
838 | 65 | default: |
839 | | /* unknown HMAC algorithm: can't do anything */ |
840 | 65 | return (0); |
841 | 16.3k | } /* end switch */ |
842 | 16.3k | } |
843 | | |
844 | | static inline int |
845 | | sctp_get_hmac_block_len(uint16_t hmac_algo) |
846 | 14.4k | { |
847 | 14.4k | switch (hmac_algo) { |
848 | 14.4k | case SCTP_AUTH_HMAC_ID_SHA1: |
849 | 14.4k | return (64); |
850 | | #if defined(SCTP_SUPPORT_HMAC_SHA256) |
851 | | case SCTP_AUTH_HMAC_ID_SHA256: |
852 | | return (64); |
853 | | #endif |
854 | 0 | case SCTP_AUTH_HMAC_ID_RSVD: |
855 | 0 | default: |
856 | | /* unknown HMAC algorithm: can't do anything */ |
857 | 0 | return (0); |
858 | 14.4k | } /* end switch */ |
859 | 14.4k | } |
860 | | |
861 | | #if defined(__Userspace__) |
862 | | /* __Userspace__ SHA1_Init is defined in libcrypto.a (libssl-dev on Ubuntu) */ |
863 | | #endif |
864 | | static void |
865 | | sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t *ctx) |
866 | 28.2k | { |
867 | 28.2k | switch (hmac_algo) { |
868 | 28.2k | case SCTP_AUTH_HMAC_ID_SHA1: |
869 | 28.2k | SCTP_SHA1_INIT(&ctx->sha1); |
870 | 28.2k | break; |
871 | | #if defined(SCTP_SUPPORT_HMAC_SHA256) |
872 | | case SCTP_AUTH_HMAC_ID_SHA256: |
873 | | SCTP_SHA256_INIT(&ctx->sha256); |
874 | | break; |
875 | | #endif |
876 | 0 | case SCTP_AUTH_HMAC_ID_RSVD: |
877 | 0 | default: |
878 | | /* unknown HMAC algorithm: can't do anything */ |
879 | 0 | return; |
880 | 28.2k | } /* end switch */ |
881 | 28.2k | } |
882 | | |
883 | | static void |
884 | | sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t *ctx, |
885 | | uint8_t *text, uint32_t textlen) |
886 | 285k | { |
887 | 285k | switch (hmac_algo) { |
888 | 285k | case SCTP_AUTH_HMAC_ID_SHA1: |
889 | 285k | SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen); |
890 | 285k | break; |
891 | | #if defined(SCTP_SUPPORT_HMAC_SHA256) |
892 | | case SCTP_AUTH_HMAC_ID_SHA256: |
893 | | SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen); |
894 | | break; |
895 | | #endif |
896 | 0 | case SCTP_AUTH_HMAC_ID_RSVD: |
897 | 0 | default: |
898 | | /* unknown HMAC algorithm: can't do anything */ |
899 | 0 | return; |
900 | 285k | } /* end switch */ |
901 | 285k | } |
902 | | |
903 | | static void |
904 | | sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t *ctx, |
905 | | uint8_t *digest) |
906 | 28.2k | { |
907 | 28.2k | switch (hmac_algo) { |
908 | 28.2k | case SCTP_AUTH_HMAC_ID_SHA1: |
909 | 28.2k | SCTP_SHA1_FINAL(digest, &ctx->sha1); |
910 | 28.2k | break; |
911 | | #if defined(SCTP_SUPPORT_HMAC_SHA256) |
912 | | case SCTP_AUTH_HMAC_ID_SHA256: |
913 | | SCTP_SHA256_FINAL(digest, &ctx->sha256); |
914 | | break; |
915 | | #endif |
916 | 0 | case SCTP_AUTH_HMAC_ID_RSVD: |
917 | 0 | default: |
918 | | /* unknown HMAC algorithm: can't do anything */ |
919 | 0 | return; |
920 | 28.2k | } /* end switch */ |
921 | 28.2k | } |
922 | | |
923 | | /*- |
924 | | * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104) |
925 | | * |
926 | | * Compute the HMAC digest using the desired hash key, text, and HMAC |
927 | | * algorithm. Resulting digest is placed in 'digest' and digest length |
928 | | * is returned, if the HMAC was performed. |
929 | | * |
930 | | * WARNING: it is up to the caller to supply sufficient space to hold the |
931 | | * resultant digest. |
932 | | */ |
933 | | uint32_t |
934 | | sctp_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen, |
935 | | uint8_t *text, uint32_t textlen, uint8_t *digest) |
936 | 0 | { |
937 | 0 | uint32_t digestlen; |
938 | 0 | uint32_t blocklen; |
939 | 0 | sctp_hash_context_t ctx; |
940 | 0 | uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ |
941 | 0 | uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; |
942 | 0 | uint32_t i; |
943 | | |
944 | | /* sanity check the material and length */ |
945 | 0 | if ((key == NULL) || (keylen == 0) || (text == NULL) || |
946 | 0 | (textlen == 0) || (digest == NULL)) { |
947 | | /* can't do HMAC with empty key or text or digest store */ |
948 | 0 | return (0); |
949 | 0 | } |
950 | | /* validate the hmac algo and get the digest length */ |
951 | 0 | digestlen = sctp_get_hmac_digest_len(hmac_algo); |
952 | 0 | if (digestlen == 0) |
953 | 0 | return (0); |
954 | | |
955 | | /* hash the key if it is longer than the hash block size */ |
956 | 0 | blocklen = sctp_get_hmac_block_len(hmac_algo); |
957 | 0 | if (keylen > blocklen) { |
958 | 0 | sctp_hmac_init(hmac_algo, &ctx); |
959 | 0 | sctp_hmac_update(hmac_algo, &ctx, key, keylen); |
960 | 0 | sctp_hmac_final(hmac_algo, &ctx, temp); |
961 | | /* set the hashed key as the key */ |
962 | 0 | keylen = digestlen; |
963 | 0 | key = temp; |
964 | 0 | } |
965 | | /* initialize the inner/outer pads with the key and "append" zeroes */ |
966 | 0 | memset(ipad, 0, blocklen); |
967 | 0 | memset(opad, 0, blocklen); |
968 | 0 | memcpy(ipad, key, keylen); |
969 | 0 | memcpy(opad, key, keylen); |
970 | | |
971 | | /* XOR the key with ipad and opad values */ |
972 | 0 | for (i = 0; i < blocklen; i++) { |
973 | 0 | ipad[i] ^= 0x36; |
974 | 0 | opad[i] ^= 0x5c; |
975 | 0 | } |
976 | | |
977 | | /* perform inner hash */ |
978 | 0 | sctp_hmac_init(hmac_algo, &ctx); |
979 | 0 | sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); |
980 | 0 | sctp_hmac_update(hmac_algo, &ctx, text, textlen); |
981 | 0 | sctp_hmac_final(hmac_algo, &ctx, temp); |
982 | | |
983 | | /* perform outer hash */ |
984 | 0 | sctp_hmac_init(hmac_algo, &ctx); |
985 | 0 | sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); |
986 | 0 | sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); |
987 | 0 | sctp_hmac_final(hmac_algo, &ctx, digest); |
988 | |
|
989 | 0 | return (digestlen); |
990 | 0 | } |
991 | | |
992 | | /* mbuf version */ |
993 | | uint32_t |
994 | | sctp_hmac_m(uint16_t hmac_algo, uint8_t *key, uint32_t keylen, |
995 | | struct mbuf *m, uint32_t m_offset, uint8_t *digest, uint32_t trailer) |
996 | 13.8k | { |
997 | 13.8k | uint32_t digestlen; |
998 | 13.8k | uint32_t blocklen; |
999 | 13.8k | sctp_hash_context_t ctx; |
1000 | 13.8k | uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ |
1001 | 13.8k | uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; |
1002 | 13.8k | uint32_t i; |
1003 | 13.8k | struct mbuf *m_tmp; |
1004 | | |
1005 | | /* sanity check the material and length */ |
1006 | 13.8k | if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) { |
1007 | | /* can't do HMAC with empty key or text or digest store */ |
1008 | 0 | return (0); |
1009 | 0 | } |
1010 | | /* validate the hmac algo and get the digest length */ |
1011 | 13.8k | digestlen = sctp_get_hmac_digest_len(hmac_algo); |
1012 | 13.8k | if (digestlen == 0) |
1013 | 0 | return (0); |
1014 | | |
1015 | | /* hash the key if it is longer than the hash block size */ |
1016 | 13.8k | blocklen = sctp_get_hmac_block_len(hmac_algo); |
1017 | 13.8k | if (keylen > blocklen) { |
1018 | 0 | sctp_hmac_init(hmac_algo, &ctx); |
1019 | 0 | sctp_hmac_update(hmac_algo, &ctx, key, keylen); |
1020 | 0 | sctp_hmac_final(hmac_algo, &ctx, temp); |
1021 | | /* set the hashed key as the key */ |
1022 | 0 | keylen = digestlen; |
1023 | 0 | key = temp; |
1024 | 0 | } |
1025 | | /* initialize the inner/outer pads with the key and "append" zeroes */ |
1026 | 13.8k | memset(ipad, 0, blocklen); |
1027 | 13.8k | memset(opad, 0, blocklen); |
1028 | 13.8k | memcpy(ipad, key, keylen); |
1029 | 13.8k | memcpy(opad, key, keylen); |
1030 | | |
1031 | | /* XOR the key with ipad and opad values */ |
1032 | 897k | for (i = 0; i < blocklen; i++) { |
1033 | 883k | ipad[i] ^= 0x36; |
1034 | 883k | opad[i] ^= 0x5c; |
1035 | 883k | } |
1036 | | |
1037 | | /* perform inner hash */ |
1038 | 13.8k | sctp_hmac_init(hmac_algo, &ctx); |
1039 | 13.8k | sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); |
1040 | | /* find the correct starting mbuf and offset (get start of text) */ |
1041 | 13.8k | m_tmp = m; |
1042 | 14.3k | while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) { |
1043 | 553 | m_offset -= SCTP_BUF_LEN(m_tmp); |
1044 | 553 | m_tmp = SCTP_BUF_NEXT(m_tmp); |
1045 | 553 | } |
1046 | | /* now use the rest of the mbuf chain for the text */ |
1047 | 256k | while (m_tmp != NULL) { |
1048 | 243k | if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) { |
1049 | 696 | sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset, |
1050 | 696 | SCTP_BUF_LEN(m_tmp) - (trailer+m_offset)); |
1051 | 242k | } else { |
1052 | 242k | sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset, |
1053 | 242k | SCTP_BUF_LEN(m_tmp) - m_offset); |
1054 | 242k | } |
1055 | | |
1056 | | /* clear the offset since it's only for the first mbuf */ |
1057 | 243k | m_offset = 0; |
1058 | 243k | m_tmp = SCTP_BUF_NEXT(m_tmp); |
1059 | 243k | } |
1060 | 13.8k | sctp_hmac_final(hmac_algo, &ctx, temp); |
1061 | | |
1062 | | /* perform outer hash */ |
1063 | 13.8k | sctp_hmac_init(hmac_algo, &ctx); |
1064 | 13.8k | sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); |
1065 | 13.8k | sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); |
1066 | 13.8k | sctp_hmac_final(hmac_algo, &ctx, digest); |
1067 | | |
1068 | 13.8k | return (digestlen); |
1069 | 13.8k | } |
1070 | | |
1071 | | /* |
1072 | | * computes the requested HMAC using a key struct (which may be modified if |
1073 | | * the keylen exceeds the HMAC block len). |
1074 | | */ |
1075 | | uint32_t |
1076 | | sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key, uint8_t *text, |
1077 | | uint32_t textlen, uint8_t *digest) |
1078 | 0 | { |
1079 | 0 | uint32_t digestlen; |
1080 | 0 | uint32_t blocklen; |
1081 | 0 | sctp_hash_context_t ctx; |
1082 | 0 | uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; |
1083 | | |
1084 | | /* sanity check */ |
1085 | 0 | if ((key == NULL) || (text == NULL) || (textlen == 0) || |
1086 | 0 | (digest == NULL)) { |
1087 | | /* can't do HMAC with empty key or text or digest store */ |
1088 | 0 | return (0); |
1089 | 0 | } |
1090 | | /* validate the hmac algo and get the digest length */ |
1091 | 0 | digestlen = sctp_get_hmac_digest_len(hmac_algo); |
1092 | 0 | if (digestlen == 0) |
1093 | 0 | return (0); |
1094 | | |
1095 | | /* hash the key if it is longer than the hash block size */ |
1096 | 0 | blocklen = sctp_get_hmac_block_len(hmac_algo); |
1097 | 0 | if (key->keylen > blocklen) { |
1098 | 0 | sctp_hmac_init(hmac_algo, &ctx); |
1099 | 0 | sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); |
1100 | 0 | sctp_hmac_final(hmac_algo, &ctx, temp); |
1101 | | /* save the hashed key as the new key */ |
1102 | 0 | key->keylen = digestlen; |
1103 | 0 | memcpy(key->key, temp, key->keylen); |
1104 | 0 | } |
1105 | 0 | return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen, |
1106 | 0 | digest)); |
1107 | 0 | } |
1108 | | |
1109 | | /* mbuf version */ |
1110 | | uint32_t |
1111 | | sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key, struct mbuf *m, |
1112 | | uint32_t m_offset, uint8_t *digest) |
1113 | 614 | { |
1114 | 614 | uint32_t digestlen; |
1115 | 614 | uint32_t blocklen; |
1116 | 614 | sctp_hash_context_t ctx; |
1117 | 614 | uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; |
1118 | | |
1119 | | /* sanity check */ |
1120 | 614 | if ((key == NULL) || (m == NULL) || (digest == NULL)) { |
1121 | | /* can't do HMAC with empty key or text or digest store */ |
1122 | 0 | return (0); |
1123 | 0 | } |
1124 | | /* validate the hmac algo and get the digest length */ |
1125 | 614 | digestlen = sctp_get_hmac_digest_len(hmac_algo); |
1126 | 614 | if (digestlen == 0) |
1127 | 0 | return (0); |
1128 | | |
1129 | | /* hash the key if it is longer than the hash block size */ |
1130 | 614 | blocklen = sctp_get_hmac_block_len(hmac_algo); |
1131 | 614 | if (key->keylen > blocklen) { |
1132 | 604 | sctp_hmac_init(hmac_algo, &ctx); |
1133 | 604 | sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); |
1134 | 604 | sctp_hmac_final(hmac_algo, &ctx, temp); |
1135 | | /* save the hashed key as the new key */ |
1136 | 604 | key->keylen = digestlen; |
1137 | 604 | memcpy(key->key, temp, key->keylen); |
1138 | 604 | } |
1139 | 614 | return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0)); |
1140 | 614 | } |
1141 | | |
1142 | | int |
1143 | | sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id) |
1144 | 0 | { |
1145 | 0 | int i; |
1146 | |
|
1147 | 0 | if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD)) |
1148 | 0 | return (0); |
1149 | | |
1150 | 0 | for (i = 0; i < list->num_algo; i++) |
1151 | 0 | if (list->hmac[i] == id) |
1152 | 0 | return (1); |
1153 | | |
1154 | | /* not in the list */ |
1155 | 0 | return (0); |
1156 | 0 | } |
1157 | | |
1158 | | /*- |
1159 | | * clear any cached key(s) if they match the given key id on an association. |
1160 | | * the cached key(s) will be recomputed and re-cached at next use. |
1161 | | * ASSUMES TCB_LOCK is already held |
1162 | | */ |
1163 | | void |
1164 | | sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid) |
1165 | 17.5k | { |
1166 | 17.5k | if (stcb == NULL) |
1167 | 0 | return; |
1168 | | |
1169 | 17.5k | if (keyid == stcb->asoc.authinfo.assoc_keyid) { |
1170 | 17.5k | sctp_free_key(stcb->asoc.authinfo.assoc_key); |
1171 | 17.5k | stcb->asoc.authinfo.assoc_key = NULL; |
1172 | 17.5k | } |
1173 | 17.5k | if (keyid == stcb->asoc.authinfo.recv_keyid) { |
1174 | 17.5k | sctp_free_key(stcb->asoc.authinfo.recv_key); |
1175 | 17.5k | stcb->asoc.authinfo.recv_key = NULL; |
1176 | 17.5k | } |
1177 | 17.5k | } |
1178 | | |
1179 | | /*- |
1180 | | * clear any cached key(s) if they match the given key id for all assocs on |
1181 | | * an endpoint. |
1182 | | * ASSUMES INP_WLOCK is already held |
1183 | | */ |
1184 | | void |
1185 | | sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid) |
1186 | 0 | { |
1187 | 0 | struct sctp_tcb *stcb; |
1188 | |
|
1189 | 0 | if (inp == NULL) |
1190 | 0 | return; |
1191 | | |
1192 | | /* clear the cached keys on all assocs on this instance */ |
1193 | 0 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
1194 | 0 | SCTP_TCB_LOCK(stcb); |
1195 | 0 | sctp_clear_cachedkeys(stcb, keyid); |
1196 | 0 | SCTP_TCB_UNLOCK(stcb); |
1197 | 0 | } |
1198 | 0 | } |
1199 | | |
1200 | | /*- |
1201 | | * delete a shared key from an association |
1202 | | * ASSUMES TCB_LOCK is already held |
1203 | | */ |
1204 | | int |
1205 | | sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) |
1206 | 0 | { |
1207 | 0 | sctp_sharedkey_t *skey; |
1208 | |
|
1209 | 0 | if (stcb == NULL) |
1210 | 0 | return (-1); |
1211 | | |
1212 | | /* is the keyid the assoc active sending key */ |
1213 | 0 | if (keyid == stcb->asoc.authinfo.active_keyid) |
1214 | 0 | return (-1); |
1215 | | |
1216 | | /* does the key exist? */ |
1217 | 0 | skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); |
1218 | 0 | if (skey == NULL) |
1219 | 0 | return (-1); |
1220 | | |
1221 | | /* are there other refcount holders on the key? */ |
1222 | 0 | if (skey->refcount > 1) |
1223 | 0 | return (-1); |
1224 | | |
1225 | | /* remove it */ |
1226 | 0 | LIST_REMOVE(skey, next); |
1227 | 0 | sctp_free_sharedkey(skey); /* frees skey->key as well */ |
1228 | | |
1229 | | /* clear any cached keys */ |
1230 | 0 | sctp_clear_cachedkeys(stcb, keyid); |
1231 | 0 | return (0); |
1232 | 0 | } |
1233 | | |
1234 | | /*- |
1235 | | * deletes a shared key from the endpoint |
1236 | | * ASSUMES INP_WLOCK is already held |
1237 | | */ |
1238 | | int |
1239 | | sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) |
1240 | 0 | { |
1241 | 0 | sctp_sharedkey_t *skey; |
1242 | |
|
1243 | 0 | if (inp == NULL) |
1244 | 0 | return (-1); |
1245 | | |
1246 | | /* is the keyid the active sending key on the endpoint */ |
1247 | 0 | if (keyid == inp->sctp_ep.default_keyid) |
1248 | 0 | return (-1); |
1249 | | |
1250 | | /* does the key exist? */ |
1251 | 0 | skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); |
1252 | 0 | if (skey == NULL) |
1253 | 0 | return (-1); |
1254 | | |
1255 | | /* endpoint keys are not refcounted */ |
1256 | | |
1257 | | /* remove it */ |
1258 | 0 | LIST_REMOVE(skey, next); |
1259 | 0 | sctp_free_sharedkey(skey); /* frees skey->key as well */ |
1260 | | |
1261 | | /* clear any cached keys */ |
1262 | 0 | sctp_clear_cachedkeys_ep(inp, keyid); |
1263 | 0 | return (0); |
1264 | 0 | } |
1265 | | |
1266 | | /*- |
1267 | | * set the active key on an association |
1268 | | * ASSUMES TCB_LOCK is already held |
1269 | | */ |
1270 | | int |
1271 | | sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid) |
1272 | 0 | { |
1273 | 0 | sctp_sharedkey_t *skey = NULL; |
1274 | | |
1275 | | /* find the key on the assoc */ |
1276 | 0 | skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); |
1277 | 0 | if (skey == NULL) { |
1278 | | /* that key doesn't exist */ |
1279 | 0 | return (-1); |
1280 | 0 | } |
1281 | 0 | if ((skey->deactivated) && (skey->refcount > 1)) { |
1282 | | /* can't reactivate a deactivated key with other refcounts */ |
1283 | 0 | return (-1); |
1284 | 0 | } |
1285 | | |
1286 | | /* set the (new) active key */ |
1287 | 0 | stcb->asoc.authinfo.active_keyid = keyid; |
1288 | | /* reset the deactivated flag */ |
1289 | 0 | skey->deactivated = 0; |
1290 | |
|
1291 | 0 | return (0); |
1292 | 0 | } |
1293 | | |
1294 | | /*- |
1295 | | * set the active key on an endpoint |
1296 | | * ASSUMES INP_WLOCK is already held |
1297 | | */ |
1298 | | int |
1299 | | sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid) |
1300 | 0 | { |
1301 | 0 | sctp_sharedkey_t *skey; |
1302 | | |
1303 | | /* find the key */ |
1304 | 0 | skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); |
1305 | 0 | if (skey == NULL) { |
1306 | | /* that key doesn't exist */ |
1307 | 0 | return (-1); |
1308 | 0 | } |
1309 | 0 | inp->sctp_ep.default_keyid = keyid; |
1310 | 0 | return (0); |
1311 | 0 | } |
1312 | | |
1313 | | /*- |
1314 | | * deactivates a shared key from the association |
1315 | | * ASSUMES INP_WLOCK is already held |
1316 | | */ |
1317 | | int |
1318 | | sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) |
1319 | 0 | { |
1320 | 0 | sctp_sharedkey_t *skey; |
1321 | |
|
1322 | 0 | if (stcb == NULL) |
1323 | 0 | return (-1); |
1324 | | |
1325 | | /* is the keyid the assoc active sending key */ |
1326 | 0 | if (keyid == stcb->asoc.authinfo.active_keyid) |
1327 | 0 | return (-1); |
1328 | | |
1329 | | /* does the key exist? */ |
1330 | 0 | skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); |
1331 | 0 | if (skey == NULL) |
1332 | 0 | return (-1); |
1333 | | |
1334 | | /* are there other refcount holders on the key? */ |
1335 | 0 | if (skey->refcount == 1) { |
1336 | | /* no other users, send a notification for this key */ |
1337 | 0 | sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, 0, &keyid, |
1338 | 0 | SCTP_SO_LOCKED); |
1339 | 0 | } |
1340 | | |
1341 | | /* mark the key as deactivated */ |
1342 | 0 | skey->deactivated = 1; |
1343 | |
|
1344 | 0 | return (0); |
1345 | 0 | } |
1346 | | |
1347 | | /*- |
1348 | | * deactivates a shared key from the endpoint |
1349 | | * ASSUMES INP_WLOCK is already held |
1350 | | */ |
1351 | | int |
1352 | | sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) |
1353 | 0 | { |
1354 | 0 | sctp_sharedkey_t *skey; |
1355 | |
|
1356 | 0 | if (inp == NULL) |
1357 | 0 | return (-1); |
1358 | | |
1359 | | /* is the keyid the active sending key on the endpoint */ |
1360 | 0 | if (keyid == inp->sctp_ep.default_keyid) |
1361 | 0 | return (-1); |
1362 | | |
1363 | | /* does the key exist? */ |
1364 | 0 | skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); |
1365 | 0 | if (skey == NULL) |
1366 | 0 | return (-1); |
1367 | | |
1368 | | /* endpoint keys are not refcounted */ |
1369 | | |
1370 | | /* remove it */ |
1371 | 0 | LIST_REMOVE(skey, next); |
1372 | 0 | sctp_free_sharedkey(skey); /* frees skey->key as well */ |
1373 | |
|
1374 | 0 | return (0); |
1375 | 0 | } |
1376 | | |
1377 | | /* |
1378 | | * get local authentication parameters from cookie (from INIT-ACK) |
1379 | | */ |
1380 | | void |
1381 | | sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m, |
1382 | | uint32_t offset, uint32_t length) |
1383 | 0 | { |
1384 | 0 | struct sctp_paramhdr *phdr, tmp_param; |
1385 | 0 | uint16_t plen, ptype; |
1386 | 0 | uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; |
1387 | 0 | struct sctp_auth_random *p_random = NULL; |
1388 | 0 | uint16_t random_len = 0; |
1389 | 0 | uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; |
1390 | 0 | struct sctp_auth_hmac_algo *hmacs = NULL; |
1391 | 0 | uint16_t hmacs_len = 0; |
1392 | 0 | uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; |
1393 | 0 | struct sctp_auth_chunk_list *chunks = NULL; |
1394 | 0 | uint16_t num_chunks = 0; |
1395 | 0 | sctp_key_t *new_key; |
1396 | 0 | uint32_t keylen; |
1397 | | |
1398 | | /* convert to upper bound */ |
1399 | 0 | length += offset; |
1400 | |
|
1401 | 0 | phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, |
1402 | 0 | sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param); |
1403 | 0 | while (phdr != NULL) { |
1404 | 0 | ptype = ntohs(phdr->param_type); |
1405 | 0 | plen = ntohs(phdr->param_length); |
1406 | |
|
1407 | 0 | if ((plen < sizeof(struct sctp_paramhdr)) || |
1408 | 0 | (offset + plen > length)) |
1409 | 0 | break; |
1410 | | |
1411 | 0 | if (ptype == SCTP_RANDOM) { |
1412 | 0 | if (plen > sizeof(random_store)) |
1413 | 0 | break; |
1414 | 0 | phdr = sctp_get_next_param(m, offset, |
1415 | 0 | (struct sctp_paramhdr *)random_store, plen); |
1416 | 0 | if (phdr == NULL) |
1417 | 0 | return; |
1418 | | /* save the random and length for the key */ |
1419 | 0 | p_random = (struct sctp_auth_random *)phdr; |
1420 | 0 | random_len = plen - sizeof(*p_random); |
1421 | 0 | } else if (ptype == SCTP_HMAC_LIST) { |
1422 | 0 | uint16_t num_hmacs; |
1423 | 0 | uint16_t i; |
1424 | |
|
1425 | 0 | if (plen > sizeof(hmacs_store)) |
1426 | 0 | break; |
1427 | 0 | phdr = sctp_get_next_param(m, offset, |
1428 | 0 | (struct sctp_paramhdr *)hmacs_store, plen); |
1429 | 0 | if (phdr == NULL) |
1430 | 0 | return; |
1431 | | /* save the hmacs list and num for the key */ |
1432 | 0 | hmacs = (struct sctp_auth_hmac_algo *)phdr; |
1433 | 0 | hmacs_len = plen - sizeof(*hmacs); |
1434 | 0 | num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); |
1435 | 0 | if (stcb->asoc.local_hmacs != NULL) |
1436 | 0 | sctp_free_hmaclist(stcb->asoc.local_hmacs); |
1437 | 0 | stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs); |
1438 | 0 | if (stcb->asoc.local_hmacs != NULL) { |
1439 | 0 | for (i = 0; i < num_hmacs; i++) { |
1440 | 0 | (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs, |
1441 | 0 | ntohs(hmacs->hmac_ids[i])); |
1442 | 0 | } |
1443 | 0 | } |
1444 | 0 | } else if (ptype == SCTP_CHUNK_LIST) { |
1445 | 0 | int i; |
1446 | |
|
1447 | 0 | if (plen > sizeof(chunks_store)) |
1448 | 0 | break; |
1449 | 0 | phdr = sctp_get_next_param(m, offset, |
1450 | 0 | (struct sctp_paramhdr *)chunks_store, plen); |
1451 | 0 | if (phdr == NULL) |
1452 | 0 | return; |
1453 | 0 | chunks = (struct sctp_auth_chunk_list *)phdr; |
1454 | 0 | num_chunks = plen - sizeof(*chunks); |
1455 | | /* save chunks list and num for the key */ |
1456 | 0 | if (stcb->asoc.local_auth_chunks != NULL) |
1457 | 0 | sctp_clear_chunklist(stcb->asoc.local_auth_chunks); |
1458 | 0 | else |
1459 | 0 | stcb->asoc.local_auth_chunks = sctp_alloc_chunklist(); |
1460 | 0 | for (i = 0; i < num_chunks; i++) { |
1461 | 0 | (void)sctp_auth_add_chunk(chunks->chunk_types[i], |
1462 | 0 | stcb->asoc.local_auth_chunks); |
1463 | 0 | } |
1464 | 0 | } |
1465 | | /* get next parameter */ |
1466 | 0 | offset += SCTP_SIZE32(plen); |
1467 | 0 | if (offset + sizeof(struct sctp_paramhdr) > length) |
1468 | 0 | break; |
1469 | 0 | phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), |
1470 | 0 | (uint8_t *)&tmp_param); |
1471 | 0 | } |
1472 | | /* concatenate the full random key */ |
1473 | 0 | keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; |
1474 | 0 | if (chunks != NULL) { |
1475 | 0 | keylen += sizeof(*chunks) + num_chunks; |
1476 | 0 | } |
1477 | 0 | new_key = sctp_alloc_key(keylen); |
1478 | 0 | if (new_key != NULL) { |
1479 | | /* copy in the RANDOM */ |
1480 | 0 | if (p_random != NULL) { |
1481 | 0 | keylen = sizeof(*p_random) + random_len; |
1482 | 0 | memcpy(new_key->key, p_random, keylen); |
1483 | 0 | } else { |
1484 | 0 | keylen = 0; |
1485 | 0 | } |
1486 | | /* append in the AUTH chunks */ |
1487 | 0 | if (chunks != NULL) { |
1488 | 0 | memcpy(new_key->key + keylen, chunks, |
1489 | 0 | sizeof(*chunks) + num_chunks); |
1490 | 0 | keylen += sizeof(*chunks) + num_chunks; |
1491 | 0 | } |
1492 | | /* append in the HMACs */ |
1493 | 0 | if (hmacs != NULL) { |
1494 | 0 | memcpy(new_key->key + keylen, hmacs, |
1495 | 0 | sizeof(*hmacs) + hmacs_len); |
1496 | 0 | } |
1497 | 0 | } |
1498 | 0 | if (stcb->asoc.authinfo.random != NULL) |
1499 | 0 | sctp_free_key(stcb->asoc.authinfo.random); |
1500 | 0 | stcb->asoc.authinfo.random = new_key; |
1501 | 0 | stcb->asoc.authinfo.random_len = random_len; |
1502 | 0 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); |
1503 | 0 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); |
1504 | | |
1505 | | /* negotiate what HMAC to use for the peer */ |
1506 | 0 | stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, |
1507 | 0 | stcb->asoc.local_hmacs); |
1508 | | |
1509 | | /* copy defaults from the endpoint */ |
1510 | | /* FIX ME: put in cookie? */ |
1511 | 0 | stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid; |
1512 | | /* copy out the shared key list (by reference) from the endpoint */ |
1513 | 0 | (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys, |
1514 | 0 | &stcb->asoc.shared_keys); |
1515 | 0 | } |
1516 | | |
1517 | | /* |
1518 | | * compute and fill in the HMAC digest for a packet |
1519 | | */ |
1520 | | void |
1521 | | sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset, |
1522 | | struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid) |
1523 | 614 | { |
1524 | 614 | uint32_t digestlen; |
1525 | 614 | sctp_sharedkey_t *skey; |
1526 | 614 | sctp_key_t *key; |
1527 | | |
1528 | 614 | if ((stcb == NULL) || (auth == NULL)) |
1529 | 0 | return; |
1530 | | |
1531 | | /* zero the digest + chunk padding */ |
1532 | 614 | digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); |
1533 | 614 | memset(auth->hmac, 0, SCTP_SIZE32(digestlen)); |
1534 | | |
1535 | | /* is the desired key cached? */ |
1536 | 614 | if ((keyid != stcb->asoc.authinfo.assoc_keyid) || |
1537 | 614 | (stcb->asoc.authinfo.assoc_key == NULL)) { |
1538 | 604 | if (stcb->asoc.authinfo.assoc_key != NULL) { |
1539 | | /* free the old cached key */ |
1540 | 0 | sctp_free_key(stcb->asoc.authinfo.assoc_key); |
1541 | 0 | } |
1542 | 604 | skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); |
1543 | | /* the only way skey is NULL is if null key id 0 is used */ |
1544 | 604 | if (skey != NULL) |
1545 | 604 | key = skey->key; |
1546 | 0 | else |
1547 | 0 | key = NULL; |
1548 | | /* compute a new assoc key and cache it */ |
1549 | 604 | stcb->asoc.authinfo.assoc_key = |
1550 | 604 | sctp_compute_hashkey(stcb->asoc.authinfo.random, |
1551 | 604 | stcb->asoc.authinfo.peer_random, key); |
1552 | 604 | stcb->asoc.authinfo.assoc_keyid = keyid; |
1553 | 604 | SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n", |
1554 | 604 | stcb->asoc.authinfo.assoc_keyid); |
1555 | | #ifdef SCTP_DEBUG |
1556 | | if (SCTP_AUTH_DEBUG) |
1557 | | sctp_print_key(stcb->asoc.authinfo.assoc_key, |
1558 | | "Assoc Key"); |
1559 | | #endif |
1560 | 604 | } |
1561 | | |
1562 | | /* set in the active key id */ |
1563 | 614 | auth->shared_key_id = htons(keyid); |
1564 | | |
1565 | | /* compute and fill in the digest */ |
1566 | 614 | (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key, |
1567 | 614 | m, auth_offset, auth->hmac); |
1568 | 614 | } |
1569 | | |
1570 | | static void |
1571 | | sctp_zero_m(struct mbuf *m, uint32_t m_offset, uint32_t size) |
1572 | 0 | { |
1573 | 0 | struct mbuf *m_tmp; |
1574 | 0 | uint8_t *data; |
1575 | 0 |
|
1576 | 0 | /* sanity check */ |
1577 | 0 | if (m == NULL) |
1578 | 0 | return; |
1579 | 0 |
|
1580 | 0 | /* find the correct starting mbuf and offset (get start position) */ |
1581 | 0 | m_tmp = m; |
1582 | 0 | while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) { |
1583 | 0 | m_offset -= SCTP_BUF_LEN(m_tmp); |
1584 | 0 | m_tmp = SCTP_BUF_NEXT(m_tmp); |
1585 | 0 | } |
1586 | 0 | /* now use the rest of the mbuf chain */ |
1587 | 0 | while ((m_tmp != NULL) && (size > 0)) { |
1588 | 0 | data = mtod(m_tmp, uint8_t *) + m_offset; |
1589 | 0 | if (size > (uint32_t)(SCTP_BUF_LEN(m_tmp) - m_offset)) { |
1590 | 0 | memset(data, 0, SCTP_BUF_LEN(m_tmp) - m_offset); |
1591 | 0 | size -= SCTP_BUF_LEN(m_tmp) - m_offset; |
1592 | 0 | } else { |
1593 | 0 | memset(data, 0, size); |
1594 | 0 | size = 0; |
1595 | 0 | } |
1596 | 0 | /* clear the offset since it's only for the first mbuf */ |
1597 | 0 | m_offset = 0; |
1598 | 0 | m_tmp = SCTP_BUF_NEXT(m_tmp); |
1599 | 0 | } |
1600 | 0 | } |
1601 | | |
1602 | | /*- |
1603 | | * process the incoming Authentication chunk |
1604 | | * return codes: |
1605 | | * -1 on any authentication error |
1606 | | * 0 on authentication verification |
1607 | | */ |
1608 | | int |
1609 | | sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth, |
1610 | | struct mbuf *m, uint32_t offset) |
1611 | 2.10k | { |
1612 | 2.10k | uint16_t chunklen; |
1613 | 2.10k | uint16_t shared_key_id; |
1614 | 2.10k | uint16_t hmac_id; |
1615 | 2.10k | sctp_sharedkey_t *skey; |
1616 | 2.10k | uint32_t digestlen; |
1617 | 2.10k | uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX]; |
1618 | 2.10k | uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX]; |
1619 | | |
1620 | | /* auth is checked for NULL by caller */ |
1621 | 2.10k | chunklen = ntohs(auth->ch.chunk_length); |
1622 | 2.10k | if (chunklen < sizeof(*auth)) { |
1623 | 0 | SCTP_STAT_INCR(sctps_recvauthfailed); |
1624 | 0 | return (-1); |
1625 | 0 | } |
1626 | 2.10k | SCTP_STAT_INCR(sctps_recvauth); |
1627 | | |
1628 | | /* get the auth params */ |
1629 | 2.10k | shared_key_id = ntohs(auth->shared_key_id); |
1630 | 2.10k | hmac_id = ntohs(auth->hmac_id); |
1631 | 2.10k | SCTPDBG(SCTP_DEBUG_AUTH1, |
1632 | 2.10k | "SCTP AUTH Chunk: shared key %u, HMAC id %u\n", |
1633 | 2.10k | shared_key_id, hmac_id); |
1634 | | |
1635 | 2.10k | #if defined(__Userspace__) && defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) |
1636 | 2.10k | return (0); |
1637 | 0 | #endif |
1638 | | /* is the indicated HMAC supported? */ |
1639 | 0 | if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) { |
1640 | 0 | struct mbuf *op_err; |
1641 | 0 | struct sctp_error_auth_invalid_hmac *cause; |
1642 | |
|
1643 | 0 | SCTP_STAT_INCR(sctps_recvivalhmacid); |
1644 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, |
1645 | 0 | "SCTP Auth: unsupported HMAC id %u\n", |
1646 | 0 | hmac_id); |
1647 | | /* |
1648 | | * report this in an Error Chunk: Unsupported HMAC |
1649 | | * Identifier |
1650 | | */ |
1651 | 0 | op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_auth_invalid_hmac), |
1652 | 0 | 0, M_NOWAIT, 1, MT_HEADER); |
1653 | 0 | if (op_err != NULL) { |
1654 | | /* pre-reserve some space */ |
1655 | 0 | SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); |
1656 | | /* fill in the error */ |
1657 | 0 | cause = mtod(op_err, struct sctp_error_auth_invalid_hmac *); |
1658 | 0 | cause->cause.code = htons(SCTP_CAUSE_UNSUPPORTED_HMACID); |
1659 | 0 | cause->cause.length = htons(sizeof(struct sctp_error_auth_invalid_hmac)); |
1660 | 0 | cause->hmac_id = ntohs(hmac_id); |
1661 | 0 | SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_auth_invalid_hmac); |
1662 | | /* queue it */ |
1663 | 0 | sctp_queue_op_err(stcb, op_err); |
1664 | 0 | } |
1665 | 0 | return (-1); |
1666 | 0 | } |
1667 | | /* get the indicated shared key, if available */ |
1668 | 0 | if ((stcb->asoc.authinfo.recv_key == NULL) || |
1669 | 0 | (stcb->asoc.authinfo.recv_keyid != shared_key_id)) { |
1670 | | /* find the shared key on the assoc first */ |
1671 | 0 | skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, |
1672 | 0 | shared_key_id); |
1673 | | /* if the shared key isn't found, discard the chunk */ |
1674 | 0 | if (skey == NULL) { |
1675 | 0 | SCTP_STAT_INCR(sctps_recvivalkeyid); |
1676 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, |
1677 | 0 | "SCTP Auth: unknown key id %u\n", |
1678 | 0 | shared_key_id); |
1679 | 0 | return (-1); |
1680 | 0 | } |
1681 | | /* generate a notification if this is a new key id */ |
1682 | 0 | if (stcb->asoc.authinfo.recv_keyid != shared_key_id) { |
1683 | 0 | sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb, 0, |
1684 | 0 | &shared_key_id, SCTP_SO_NOT_LOCKED); |
1685 | 0 | } |
1686 | | /* compute a new recv assoc key and cache it */ |
1687 | 0 | if (stcb->asoc.authinfo.recv_key != NULL) |
1688 | 0 | sctp_free_key(stcb->asoc.authinfo.recv_key); |
1689 | 0 | stcb->asoc.authinfo.recv_key = |
1690 | 0 | sctp_compute_hashkey(stcb->asoc.authinfo.random, |
1691 | 0 | stcb->asoc.authinfo.peer_random, skey->key); |
1692 | 0 | stcb->asoc.authinfo.recv_keyid = shared_key_id; |
1693 | | #ifdef SCTP_DEBUG |
1694 | | if (SCTP_AUTH_DEBUG) |
1695 | | sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key"); |
1696 | | #endif |
1697 | 0 | } |
1698 | | /* validate the digest length */ |
1699 | 0 | digestlen = sctp_get_hmac_digest_len(hmac_id); |
1700 | 0 | if (chunklen < (sizeof(*auth) + digestlen)) { |
1701 | | /* invalid digest length */ |
1702 | 0 | SCTP_STAT_INCR(sctps_recvauthfailed); |
1703 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, |
1704 | 0 | "SCTP Auth: chunk too short for HMAC\n"); |
1705 | 0 | return (-1); |
1706 | 0 | } |
1707 | | /* save a copy of the digest, zero the pseudo header, and validate */ |
1708 | 0 | memcpy(digest, auth->hmac, digestlen); |
1709 | 0 | sctp_zero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen)); |
1710 | 0 | (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key, |
1711 | 0 | m, offset, computed_digest); |
1712 | | |
1713 | | /* compare the computed digest with the one in the AUTH chunk */ |
1714 | 0 | if (timingsafe_bcmp(digest, computed_digest, digestlen) != 0) { |
1715 | 0 | SCTP_STAT_INCR(sctps_recvauthfailed); |
1716 | 0 | SCTPDBG(SCTP_DEBUG_AUTH1, |
1717 | 0 | "SCTP Auth: HMAC digest check failed\n"); |
1718 | 0 | return (-1); |
1719 | 0 | } |
1720 | 0 | return (0); |
1721 | 0 | } |
1722 | | |
1723 | | /* |
1724 | | * Generate NOTIFICATION |
1725 | | */ |
1726 | | void |
1727 | | sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication, |
1728 | | uint16_t keyid, int so_locked) |
1729 | 0 | { |
1730 | 0 | struct mbuf *m_notify; |
1731 | 0 | struct sctp_authkey_event *auth; |
1732 | 0 | struct sctp_queued_to_read *control; |
1733 | |
|
1734 | 0 | KASSERT(stcb != NULL, ("stcb == NULL")); |
1735 | 0 | SCTP_TCB_LOCK_ASSERT(stcb); |
1736 | 0 | SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep); |
1737 | | |
1738 | 0 | if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT)) |
1739 | | /* event not enabled */ |
1740 | 0 | return; |
1741 | | |
1742 | 0 | m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event), |
1743 | 0 | 0, M_NOWAIT, 1, MT_HEADER); |
1744 | 0 | if (m_notify == NULL) |
1745 | | /* no space left */ |
1746 | 0 | return; |
1747 | | |
1748 | 0 | SCTP_BUF_LEN(m_notify) = 0; |
1749 | 0 | auth = mtod(m_notify, struct sctp_authkey_event *); |
1750 | 0 | memset(auth, 0, sizeof(struct sctp_authkey_event)); |
1751 | 0 | auth->auth_type = SCTP_AUTHENTICATION_EVENT; |
1752 | 0 | auth->auth_flags = 0; |
1753 | 0 | auth->auth_length = sizeof(*auth); |
1754 | 0 | auth->auth_keynumber = keyid; |
1755 | | /* XXXMT: The following is BSD specific. */ |
1756 | 0 | if (indication == SCTP_AUTH_NEW_KEY) { |
1757 | 0 | auth->auth_altkeynumber = stcb->asoc.authinfo.recv_keyid; |
1758 | 0 | } else { |
1759 | 0 | auth->auth_altkeynumber = 0; |
1760 | 0 | } |
1761 | 0 | auth->auth_indication = indication; |
1762 | 0 | auth->auth_assoc_id = sctp_get_associd(stcb); |
1763 | |
|
1764 | 0 | SCTP_BUF_LEN(m_notify) = sizeof(*auth); |
1765 | 0 | SCTP_BUF_NEXT(m_notify) = NULL; |
1766 | | |
1767 | | /* append to socket */ |
1768 | 0 | control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, |
1769 | 0 | 0, 0, stcb->asoc.context, 0, 0, 0, m_notify); |
1770 | 0 | if (control == NULL) { |
1771 | | /* no memory */ |
1772 | 0 | sctp_m_freem(m_notify); |
1773 | 0 | return; |
1774 | 0 | } |
1775 | 0 | control->length = SCTP_BUF_LEN(m_notify); |
1776 | 0 | control->spec_flags = M_NOTIFICATION; |
1777 | | /* not that we need this */ |
1778 | 0 | control->tail_mbuf = m_notify; |
1779 | 0 | sctp_add_to_readq(stcb->sctp_ep, stcb, control, |
1780 | 0 | &stcb->sctp_socket->so_rcv, 1, |
1781 | 0 | SCTP_READ_LOCK_HELD, so_locked); |
1782 | 0 | } |
1783 | | |
1784 | | /*- |
1785 | | * validates the AUTHentication related parameters in an INIT/INIT-ACK |
1786 | | * Note: currently only used for INIT as INIT-ACK is handled inline |
1787 | | * with sctp_load_addresses_from_init() |
1788 | | */ |
1789 | | int |
1790 | | sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit) |
1791 | 1.20k | { |
1792 | 1.20k | struct sctp_paramhdr *phdr, param_buf; |
1793 | 1.20k | uint16_t ptype, plen; |
1794 | 1.20k | int peer_supports_asconf = 0; |
1795 | 1.20k | int peer_supports_auth = 0; |
1796 | 1.20k | int got_random = 0, got_hmacs = 0, got_chklist = 0; |
1797 | 1.20k | uint8_t saw_asconf = 0; |
1798 | 1.20k | uint8_t saw_asconf_ack = 0; |
1799 | | |
1800 | | /* go through each of the params. */ |
1801 | 1.20k | phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); |
1802 | 156k | while (phdr) { |
1803 | 156k | ptype = ntohs(phdr->param_type); |
1804 | 156k | plen = ntohs(phdr->param_length); |
1805 | | |
1806 | 156k | if (offset + plen > limit) { |
1807 | 266 | break; |
1808 | 266 | } |
1809 | 156k | if (plen < sizeof(struct sctp_paramhdr)) { |
1810 | 43 | break; |
1811 | 43 | } |
1812 | 156k | if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { |
1813 | | /* A supported extension chunk */ |
1814 | 2.47k | struct sctp_supported_chunk_types_param *pr_supported; |
1815 | 2.47k | uint8_t local_store[SCTP_SMALL_CHUNK_STORE]; |
1816 | 2.47k | int num_ent, i; |
1817 | | |
1818 | 2.47k | if (plen > sizeof(local_store)) { |
1819 | 3 | break; |
1820 | 3 | } |
1821 | 2.47k | phdr = sctp_get_next_param(m, offset, |
1822 | 2.47k | (struct sctp_paramhdr *)&local_store, |
1823 | 2.47k | plen); |
1824 | 2.47k | if (phdr == NULL) { |
1825 | 0 | return (-1); |
1826 | 0 | } |
1827 | 2.47k | pr_supported = (struct sctp_supported_chunk_types_param *)phdr; |
1828 | 2.47k | num_ent = plen - sizeof(struct sctp_paramhdr); |
1829 | 28.1k | for (i = 0; i < num_ent; i++) { |
1830 | 25.7k | switch (pr_supported->chunk_types[i]) { |
1831 | 2.21k | case SCTP_ASCONF: |
1832 | 5.28k | case SCTP_ASCONF_ACK: |
1833 | 5.28k | peer_supports_asconf = 1; |
1834 | 5.28k | break; |
1835 | 20.4k | default: |
1836 | | /* one we don't care about */ |
1837 | 20.4k | break; |
1838 | 25.7k | } |
1839 | 25.7k | } |
1840 | 153k | } else if (ptype == SCTP_RANDOM) { |
1841 | | /* enforce the random length */ |
1842 | 765 | if (plen != (sizeof(struct sctp_auth_random) + |
1843 | 765 | SCTP_AUTH_RANDOM_SIZE_REQUIRED)) { |
1844 | 22 | SCTPDBG(SCTP_DEBUG_AUTH1, |
1845 | 22 | "SCTP: invalid RANDOM len\n"); |
1846 | 22 | return (-1); |
1847 | 22 | } |
1848 | 743 | got_random = 1; |
1849 | 152k | } else if (ptype == SCTP_HMAC_LIST) { |
1850 | 789 | struct sctp_auth_hmac_algo *hmacs; |
1851 | 789 | uint8_t store[SCTP_PARAM_BUFFER_SIZE]; |
1852 | 789 | int num_hmacs; |
1853 | | |
1854 | 789 | if (plen > sizeof(store)) { |
1855 | 21 | break; |
1856 | 21 | } |
1857 | 768 | phdr = sctp_get_next_param(m, offset, |
1858 | 768 | (struct sctp_paramhdr *)store, |
1859 | 768 | plen); |
1860 | 768 | if (phdr == NULL) { |
1861 | 0 | return (-1); |
1862 | 0 | } |
1863 | 768 | hmacs = (struct sctp_auth_hmac_algo *)phdr; |
1864 | 768 | num_hmacs = (plen - sizeof(*hmacs)) / sizeof(hmacs->hmac_ids[0]); |
1865 | | /* validate the hmac list */ |
1866 | 768 | if (sctp_verify_hmac_param(hmacs, num_hmacs)) { |
1867 | 33 | SCTPDBG(SCTP_DEBUG_AUTH1, |
1868 | 33 | "SCTP: invalid HMAC param\n"); |
1869 | 33 | return (-1); |
1870 | 33 | } |
1871 | 735 | got_hmacs = 1; |
1872 | 152k | } else if (ptype == SCTP_CHUNK_LIST) { |
1873 | 3.17k | struct sctp_auth_chunk_list *chunks; |
1874 | 3.17k | uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE]; |
1875 | 3.17k | int i, num_chunks; |
1876 | | |
1877 | 3.17k | if (plen > sizeof(chunks_store)) { |
1878 | 10 | break; |
1879 | 10 | } |
1880 | 3.16k | phdr = sctp_get_next_param(m, offset, |
1881 | 3.16k | (struct sctp_paramhdr *)chunks_store, |
1882 | 3.16k | plen); |
1883 | 3.16k | if (phdr == NULL) { |
1884 | 0 | return (-1); |
1885 | 0 | } |
1886 | | /*- |
1887 | | * Flip through the list and mark that the |
1888 | | * peer supports asconf/asconf_ack. |
1889 | | */ |
1890 | 3.16k | chunks = (struct sctp_auth_chunk_list *)phdr; |
1891 | 3.16k | num_chunks = plen - sizeof(*chunks); |
1892 | 21.5k | for (i = 0; i < num_chunks; i++) { |
1893 | | /* record asconf/asconf-ack if listed */ |
1894 | 18.3k | if (chunks->chunk_types[i] == SCTP_ASCONF) |
1895 | 1.91k | saw_asconf = 1; |
1896 | 18.3k | if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) |
1897 | 2.42k | saw_asconf_ack = 1; |
1898 | 18.3k | } |
1899 | 3.16k | if (num_chunks) |
1900 | 1.83k | got_chklist = 1; |
1901 | 3.16k | } |
1902 | | |
1903 | 156k | offset += SCTP_SIZE32(plen); |
1904 | 156k | if (offset >= limit) { |
1905 | 570 | break; |
1906 | 570 | } |
1907 | 155k | phdr = sctp_get_next_param(m, offset, ¶m_buf, |
1908 | 155k | sizeof(param_buf)); |
1909 | 155k | } |
1910 | | /* validate authentication required parameters */ |
1911 | 1.15k | if (got_random && got_hmacs) { |
1912 | 38 | peer_supports_auth = 1; |
1913 | 1.11k | } else { |
1914 | 1.11k | peer_supports_auth = 0; |
1915 | 1.11k | } |
1916 | 1.15k | if (!peer_supports_auth && got_chklist) { |
1917 | 132 | SCTPDBG(SCTP_DEBUG_AUTH1, |
1918 | 132 | "SCTP: peer sent chunk list w/o AUTH\n"); |
1919 | 132 | return (-1); |
1920 | 132 | } |
1921 | 1.01k | if (peer_supports_asconf && !peer_supports_auth) { |
1922 | 83 | SCTPDBG(SCTP_DEBUG_AUTH1, |
1923 | 83 | "SCTP: peer supports ASCONF but not AUTH\n"); |
1924 | 83 | return (-1); |
1925 | 936 | } else if ((peer_supports_asconf) && (peer_supports_auth) && |
1926 | 936 | ((saw_asconf == 0) || (saw_asconf_ack == 0))) { |
1927 | 2 | return (-2); |
1928 | 2 | } |
1929 | 934 | return (0); |
1930 | 1.01k | } |
1931 | | |
1932 | | void |
1933 | | sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb) |
1934 | 11.4k | { |
1935 | 11.4k | uint16_t chunks_len = 0; |
1936 | 11.4k | uint16_t hmacs_len = 0; |
1937 | 11.4k | uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT; |
1938 | 11.4k | sctp_key_t *new_key; |
1939 | 11.4k | uint16_t keylen; |
1940 | | |
1941 | | /* initialize hmac list from endpoint */ |
1942 | 11.4k | stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); |
1943 | 11.4k | if (stcb->asoc.local_hmacs != NULL) { |
1944 | 11.4k | hmacs_len = stcb->asoc.local_hmacs->num_algo * |
1945 | 11.4k | sizeof(stcb->asoc.local_hmacs->hmac[0]); |
1946 | 11.4k | } |
1947 | | /* initialize auth chunks list from endpoint */ |
1948 | 11.4k | stcb->asoc.local_auth_chunks = |
1949 | 11.4k | sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); |
1950 | 11.4k | if (stcb->asoc.local_auth_chunks != NULL) { |
1951 | 11.4k | int i; |
1952 | 2.93M | for (i = 0; i < 256; i++) { |
1953 | 2.91M | if (stcb->asoc.local_auth_chunks->chunks[i]) |
1954 | 22.8k | chunks_len++; |
1955 | 2.91M | } |
1956 | 11.4k | } |
1957 | | /* copy defaults from the endpoint */ |
1958 | 11.4k | stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid; |
1959 | | |
1960 | | /* copy out the shared key list (by reference) from the endpoint */ |
1961 | 11.4k | (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, |
1962 | 11.4k | &stcb->asoc.shared_keys); |
1963 | | |
1964 | | /* now set the concatenated key (random + chunks + hmacs) */ |
1965 | | /* key includes parameter headers */ |
1966 | 11.4k | keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len + |
1967 | 11.4k | hmacs_len; |
1968 | 11.4k | new_key = sctp_alloc_key(keylen); |
1969 | 11.4k | if (new_key != NULL) { |
1970 | 11.4k | struct sctp_paramhdr *ph; |
1971 | 11.4k | int plen; |
1972 | | /* generate and copy in the RANDOM */ |
1973 | 11.4k | ph = (struct sctp_paramhdr *)new_key->key; |
1974 | 11.4k | ph->param_type = htons(SCTP_RANDOM); |
1975 | 11.4k | plen = sizeof(*ph) + random_len; |
1976 | 11.4k | ph->param_length = htons(plen); |
1977 | 11.4k | SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len); |
1978 | 11.4k | keylen = plen; |
1979 | | |
1980 | | /* append in the AUTH chunks */ |
1981 | | /* NOTE: currently we always have chunks to list */ |
1982 | 11.4k | ph = (struct sctp_paramhdr *)(new_key->key + keylen); |
1983 | 11.4k | ph->param_type = htons(SCTP_CHUNK_LIST); |
1984 | 11.4k | plen = sizeof(*ph) + chunks_len; |
1985 | 11.4k | ph->param_length = htons(plen); |
1986 | 11.4k | keylen += sizeof(*ph); |
1987 | 11.4k | if (stcb->asoc.local_auth_chunks) { |
1988 | 11.4k | int i; |
1989 | 2.93M | for (i = 0; i < 256; i++) { |
1990 | 2.91M | if (stcb->asoc.local_auth_chunks->chunks[i]) |
1991 | 22.8k | new_key->key[keylen++] = i; |
1992 | 2.91M | } |
1993 | 11.4k | } |
1994 | | |
1995 | | /* append in the HMACs */ |
1996 | 11.4k | ph = (struct sctp_paramhdr *)(new_key->key + keylen); |
1997 | 11.4k | ph->param_type = htons(SCTP_HMAC_LIST); |
1998 | 11.4k | plen = sizeof(*ph) + hmacs_len; |
1999 | 11.4k | ph->param_length = htons(plen); |
2000 | 11.4k | keylen += sizeof(*ph); |
2001 | 11.4k | (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs, |
2002 | 11.4k | new_key->key + keylen); |
2003 | 11.4k | } |
2004 | 11.4k | if (stcb->asoc.authinfo.random != NULL) |
2005 | 0 | sctp_free_key(stcb->asoc.authinfo.random); |
2006 | 11.4k | stcb->asoc.authinfo.random = new_key; |
2007 | 11.4k | stcb->asoc.authinfo.random_len = random_len; |
2008 | 11.4k | } |
2009 | | |
2010 | | |
2011 | | #ifdef SCTP_HMAC_TEST |
2012 | | /* |
2013 | | * HMAC and key concatenation tests |
2014 | | */ |
2015 | | static void |
2016 | | sctp_print_digest(uint8_t *digest, uint32_t digestlen, const char *str) |
2017 | | { |
2018 | | uint32_t i; |
2019 | | |
2020 | | SCTP_PRINTF("\n%s: 0x", str); |
2021 | | if (digest == NULL) |
2022 | | return; |
2023 | | |
2024 | | for (i = 0; i < digestlen; i++) |
2025 | | SCTP_PRINTF("%02x", digest[i]); |
2026 | | } |
2027 | | |
2028 | | static int |
2029 | | sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t *key, |
2030 | | uint32_t keylen, uint8_t *text, uint32_t textlen, |
2031 | | uint8_t *digest, uint32_t digestlen) |
2032 | | { |
2033 | | uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX]; |
2034 | | |
2035 | | SCTP_PRINTF("\n%s:", str); |
2036 | | sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest); |
2037 | | sctp_print_digest(digest, digestlen, "Expected digest"); |
2038 | | sctp_print_digest(computed_digest, digestlen, "Computed digest"); |
2039 | | if (memcmp(digest, computed_digest, digestlen) != 0) { |
2040 | | SCTP_PRINTF("\nFAILED"); |
2041 | | return (-1); |
2042 | | } else { |
2043 | | SCTP_PRINTF("\nPASSED"); |
2044 | | return (0); |
2045 | | } |
2046 | | } |
2047 | | |
2048 | | |
2049 | | /* |
2050 | | * RFC 2202: HMAC-SHA1 test cases |
2051 | | */ |
2052 | | void |
2053 | | sctp_test_hmac_sha1(void) |
2054 | | { |
2055 | | uint8_t *digest; |
2056 | | uint8_t key[128]; |
2057 | | uint32_t keylen; |
2058 | | uint8_t text[128]; |
2059 | | uint32_t textlen; |
2060 | | uint32_t digestlen = 20; |
2061 | | int failed = 0; |
2062 | | |
2063 | | /*- |
2064 | | * test_case = 1 |
2065 | | * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b |
2066 | | * key_len = 20 |
2067 | | * data = "Hi There" |
2068 | | * data_len = 8 |
2069 | | * digest = 0xb617318655057264e28bc0b6fb378c8ef146be00 |
2070 | | */ |
2071 | | keylen = 20; |
2072 | | memset(key, 0x0b, keylen); |
2073 | | textlen = 8; |
2074 | | strcpy(text, "Hi There"); |
2075 | | digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"; |
2076 | | if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, |
2077 | | text, textlen, digest, digestlen) < 0) |
2078 | | failed++; |
2079 | | |
2080 | | /*- |
2081 | | * test_case = 2 |
2082 | | * key = "Jefe" |
2083 | | * key_len = 4 |
2084 | | * data = "what do ya want for nothing?" |
2085 | | * data_len = 28 |
2086 | | * digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79 |
2087 | | */ |
2088 | | keylen = 4; |
2089 | | strcpy(key, "Jefe"); |
2090 | | textlen = 28; |
2091 | | strcpy(text, "what do ya want for nothing?"); |
2092 | | digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"; |
2093 | | if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, |
2094 | | text, textlen, digest, digestlen) < 0) |
2095 | | failed++; |
2096 | | |
2097 | | /*- |
2098 | | * test_case = 3 |
2099 | | * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
2100 | | * key_len = 20 |
2101 | | * data = 0xdd repeated 50 times |
2102 | | * data_len = 50 |
2103 | | * digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3 |
2104 | | */ |
2105 | | keylen = 20; |
2106 | | memset(key, 0xaa, keylen); |
2107 | | textlen = 50; |
2108 | | memset(text, 0xdd, textlen); |
2109 | | digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"; |
2110 | | if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, |
2111 | | text, textlen, digest, digestlen) < 0) |
2112 | | failed++; |
2113 | | |
2114 | | /*- |
2115 | | * test_case = 4 |
2116 | | * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819 |
2117 | | * key_len = 25 |
2118 | | * data = 0xcd repeated 50 times |
2119 | | * data_len = 50 |
2120 | | * digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da |
2121 | | */ |
2122 | | keylen = 25; |
2123 | | memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen); |
2124 | | textlen = 50; |
2125 | | memset(text, 0xcd, textlen); |
2126 | | digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"; |
2127 | | if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, |
2128 | | text, textlen, digest, digestlen) < 0) |
2129 | | failed++; |
2130 | | |
2131 | | /*- |
2132 | | * test_case = 5 |
2133 | | * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c |
2134 | | * key_len = 20 |
2135 | | * data = "Test With Truncation" |
2136 | | * data_len = 20 |
2137 | | * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 |
2138 | | * digest-96 = 0x4c1a03424b55e07fe7f27be1 |
2139 | | */ |
2140 | | keylen = 20; |
2141 | | memset(key, 0x0c, keylen); |
2142 | | textlen = 20; |
2143 | | strcpy(text, "Test With Truncation"); |
2144 | | digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"; |
2145 | | if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, |
2146 | | text, textlen, digest, digestlen) < 0) |
2147 | | failed++; |
2148 | | |
2149 | | /*- |
2150 | | * test_case = 6 |
2151 | | * key = 0xaa repeated 80 times |
2152 | | * key_len = 80 |
2153 | | * data = "Test Using Larger Than Block-Size Key - Hash Key First" |
2154 | | * data_len = 54 |
2155 | | * digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112 |
2156 | | */ |
2157 | | keylen = 80; |
2158 | | memset(key, 0xaa, keylen); |
2159 | | textlen = 54; |
2160 | | strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First"); |
2161 | | digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"; |
2162 | | if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, |
2163 | | text, textlen, digest, digestlen) < 0) |
2164 | | failed++; |
2165 | | |
2166 | | /*- |
2167 | | * test_case = 7 |
2168 | | * key = 0xaa repeated 80 times |
2169 | | * key_len = 80 |
2170 | | * data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" |
2171 | | * data_len = 73 |
2172 | | * digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91 |
2173 | | */ |
2174 | | keylen = 80; |
2175 | | memset(key, 0xaa, keylen); |
2176 | | textlen = 73; |
2177 | | strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"); |
2178 | | digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"; |
2179 | | if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, |
2180 | | text, textlen, digest, digestlen) < 0) |
2181 | | failed++; |
2182 | | |
2183 | | /* done with all tests */ |
2184 | | if (failed) |
2185 | | SCTP_PRINTF("\nSHA1 test results: %d cases failed", failed); |
2186 | | else |
2187 | | SCTP_PRINTF("\nSHA1 test results: all test cases passed"); |
2188 | | } |
2189 | | |
2190 | | /* |
2191 | | * test assoc key concatenation |
2192 | | */ |
2193 | | static int |
2194 | | sctp_test_key_concatenation(sctp_key_t *key1, sctp_key_t *key2, |
2195 | | sctp_key_t *expected_key) |
2196 | | { |
2197 | | sctp_key_t *key; |
2198 | | int ret_val; |
2199 | | |
2200 | | sctp_show_key(key1, "\nkey1"); |
2201 | | sctp_show_key(key2, "\nkey2"); |
2202 | | key = sctp_compute_hashkey(key1, key2, NULL); |
2203 | | sctp_show_key(expected_key, "\nExpected"); |
2204 | | sctp_show_key(key, "\nComputed"); |
2205 | | if (memcmp(key, expected_key, expected_key->keylen) != 0) { |
2206 | | SCTP_PRINTF("\nFAILED"); |
2207 | | ret_val = -1; |
2208 | | } else { |
2209 | | SCTP_PRINTF("\nPASSED"); |
2210 | | ret_val = 0; |
2211 | | } |
2212 | | sctp_free_key(key1); |
2213 | | sctp_free_key(key2); |
2214 | | sctp_free_key(expected_key); |
2215 | | sctp_free_key(key); |
2216 | | return (ret_val); |
2217 | | } |
2218 | | |
2219 | | |
2220 | | void |
2221 | | sctp_test_authkey(void) |
2222 | | { |
2223 | | sctp_key_t *key1, *key2, *expected_key; |
2224 | | int failed = 0; |
2225 | | |
2226 | | /* test case 1 */ |
2227 | | key1 = sctp_set_key("\x01\x01\x01\x01", 4); |
2228 | | key2 = sctp_set_key("\x01\x02\x03\x04", 4); |
2229 | | expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8); |
2230 | | if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) |
2231 | | failed++; |
2232 | | |
2233 | | /* test case 2 */ |
2234 | | key1 = sctp_set_key("\x00\x00\x00\x01", 4); |
2235 | | key2 = sctp_set_key("\x02", 1); |
2236 | | expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5); |
2237 | | if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) |
2238 | | failed++; |
2239 | | |
2240 | | /* test case 3 */ |
2241 | | key1 = sctp_set_key("\x01", 1); |
2242 | | key2 = sctp_set_key("\x00\x00\x00\x02", 4); |
2243 | | expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5); |
2244 | | if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) |
2245 | | failed++; |
2246 | | |
2247 | | /* test case 4 */ |
2248 | | key1 = sctp_set_key("\x00\x00\x00\x01", 4); |
2249 | | key2 = sctp_set_key("\x01", 1); |
2250 | | expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5); |
2251 | | if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) |
2252 | | failed++; |
2253 | | |
2254 | | /* test case 5 */ |
2255 | | key1 = sctp_set_key("\x01", 1); |
2256 | | key2 = sctp_set_key("\x00\x00\x00\x01", 4); |
2257 | | expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5); |
2258 | | if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) |
2259 | | failed++; |
2260 | | |
2261 | | /* test case 6 */ |
2262 | | key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11); |
2263 | | key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11); |
2264 | | expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22); |
2265 | | if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) |
2266 | | failed++; |
2267 | | |
2268 | | /* test case 7 */ |
2269 | | key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11); |
2270 | | key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11); |
2271 | | expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22); |
2272 | | if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) |
2273 | | failed++; |
2274 | | |
2275 | | /* done with all tests */ |
2276 | | if (failed) |
2277 | | SCTP_PRINTF("\nKey concatenation test results: %d cases failed", failed); |
2278 | | else |
2279 | | SCTP_PRINTF("\nKey concatenation test results: all test cases passed"); |
2280 | | } |
2281 | | |
2282 | | |
2283 | | #if defined(STANDALONE_HMAC_TEST) |
2284 | | int |
2285 | | main(void) |
2286 | | { |
2287 | | sctp_test_hmac_sha1(); |
2288 | | sctp_test_authkey(); |
2289 | | } |
2290 | | |
2291 | | #endif /* STANDALONE_HMAC_TEST */ |
2292 | | |
2293 | | #endif /* SCTP_HMAC_TEST */ |