sctp_auth.c 59 KB

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