sctp_auth.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #ifndef _NETINET_SCTP_AUTH_H_
  39. #define _NETINET_SCTP_AUTH_H_
  40. #include <netinet/sctp_os.h>
  41. /* digest lengths */
  42. #define SCTP_AUTH_DIGEST_LEN_SHA1 20
  43. #define SCTP_AUTH_DIGEST_LEN_SHA256 32
  44. #define SCTP_AUTH_DIGEST_LEN_MAX SCTP_AUTH_DIGEST_LEN_SHA256
  45. /* random sizes */
  46. #define SCTP_AUTH_RANDOM_SIZE_DEFAULT 32
  47. #define SCTP_AUTH_RANDOM_SIZE_REQUIRED 32
  48. /* union of all supported HMAC algorithm contexts */
  49. typedef union sctp_hash_context {
  50. SCTP_SHA1_CTX sha1;
  51. #if defined(SCTP_SUPPORT_HMAC_SHA256)
  52. SCTP_SHA256_CTX sha256;
  53. #endif
  54. } sctp_hash_context_t;
  55. typedef struct sctp_key {
  56. uint32_t keylen;
  57. uint8_t key[];
  58. } sctp_key_t;
  59. typedef struct sctp_shared_key {
  60. LIST_ENTRY(sctp_shared_key) next;
  61. sctp_key_t *key; /* key text */
  62. uint32_t refcount; /* reference count */
  63. uint16_t keyid; /* shared key ID */
  64. uint8_t deactivated; /* key is deactivated */
  65. } sctp_sharedkey_t;
  66. LIST_HEAD(sctp_keyhead, sctp_shared_key);
  67. /* authentication chunks list */
  68. typedef struct sctp_auth_chklist {
  69. uint8_t chunks[256];
  70. uint8_t num_chunks;
  71. } sctp_auth_chklist_t;
  72. /* hmac algos supported list */
  73. typedef struct sctp_hmaclist {
  74. uint16_t max_algo; /* max algorithms allocated */
  75. uint16_t num_algo; /* num algorithms used */
  76. uint16_t hmac[];
  77. } sctp_hmaclist_t;
  78. /* authentication info */
  79. typedef struct sctp_authinformation {
  80. sctp_key_t *random; /* local random key (concatenated) */
  81. uint32_t random_len; /* local random number length for param */
  82. sctp_key_t *peer_random;/* peer's random key (concatenated) */
  83. sctp_key_t *assoc_key; /* cached concatenated send key */
  84. sctp_key_t *recv_key; /* cached concatenated recv key */
  85. uint16_t active_keyid; /* active send keyid */
  86. uint16_t assoc_keyid; /* current send keyid (cached) */
  87. uint16_t recv_keyid; /* last recv keyid (cached) */
  88. } sctp_authinfo_t;
  89. /*
  90. * Macros
  91. */
  92. #define sctp_auth_is_required_chunk(chunk, list) ((list == NULL) ? (0) : (list->chunks[chunk] != 0))
  93. /*
  94. * function prototypes
  95. */
  96. /* socket option api functions */
  97. extern sctp_auth_chklist_t *sctp_alloc_chunklist(void);
  98. extern void sctp_free_chunklist(sctp_auth_chklist_t *chklist);
  99. extern void sctp_clear_chunklist(sctp_auth_chklist_t *chklist);
  100. extern sctp_auth_chklist_t *sctp_copy_chunklist(sctp_auth_chklist_t *chklist);
  101. extern int sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t *list);
  102. extern int sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t *list);
  103. extern size_t sctp_auth_get_chklist_size(const sctp_auth_chklist_t *list);
  104. extern int sctp_serialize_auth_chunks(const sctp_auth_chklist_t *list,
  105. uint8_t *ptr);
  106. extern int sctp_pack_auth_chunks(const sctp_auth_chklist_t *list,
  107. uint8_t *ptr);
  108. extern int sctp_unpack_auth_chunks(const uint8_t *ptr, uint8_t num_chunks,
  109. sctp_auth_chklist_t *list);
  110. /* key handling */
  111. extern sctp_key_t *sctp_alloc_key(uint32_t keylen);
  112. extern void sctp_free_key(sctp_key_t *key);
  113. extern void sctp_print_key(sctp_key_t *key, const char *str);
  114. extern void sctp_show_key(sctp_key_t *key, const char *str);
  115. extern sctp_key_t *sctp_generate_random_key(uint32_t keylen);
  116. extern sctp_key_t *sctp_set_key(uint8_t *key, uint32_t keylen);
  117. extern sctp_key_t *sctp_compute_hashkey(sctp_key_t *key1, sctp_key_t *key2,
  118. sctp_key_t *shared);
  119. /* shared key handling */
  120. extern sctp_sharedkey_t *sctp_alloc_sharedkey(void);
  121. extern void sctp_free_sharedkey(sctp_sharedkey_t *skey);
  122. extern sctp_sharedkey_t *sctp_find_sharedkey(struct sctp_keyhead *shared_keys,
  123. uint16_t key_id);
  124. extern int sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
  125. sctp_sharedkey_t *new_skey);
  126. extern int sctp_copy_skeylist(const struct sctp_keyhead *src,
  127. struct sctp_keyhead *dest);
  128. /* ref counts on shared keys, by key id */
  129. extern void sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t keyid);
  130. extern void sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t keyid,
  131. int so_locked);
  132. /* hmac list handling */
  133. extern sctp_hmaclist_t *sctp_alloc_hmaclist(uint16_t num_hmacs);
  134. extern void sctp_free_hmaclist(sctp_hmaclist_t *list);
  135. extern int sctp_auth_add_hmacid(sctp_hmaclist_t *list, uint16_t hmac_id);
  136. extern sctp_hmaclist_t *sctp_copy_hmaclist(sctp_hmaclist_t *list);
  137. extern sctp_hmaclist_t *sctp_default_supported_hmaclist(void);
  138. extern uint16_t sctp_negotiate_hmacid(sctp_hmaclist_t *peer,
  139. sctp_hmaclist_t *local);
  140. extern int sctp_serialize_hmaclist(sctp_hmaclist_t *list, uint8_t *ptr);
  141. extern int sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs,
  142. uint32_t num_hmacs);
  143. extern sctp_authinfo_t *sctp_alloc_authinfo(void);
  144. extern void sctp_free_authinfo(sctp_authinfo_t *authinfo);
  145. /* keyed-HMAC functions */
  146. extern uint32_t sctp_get_auth_chunk_len(uint16_t hmac_algo);
  147. extern uint32_t sctp_get_hmac_digest_len(uint16_t hmac_algo);
  148. extern uint32_t sctp_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
  149. uint8_t *text, uint32_t textlen, uint8_t *digest);
  150. extern uint32_t sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key,
  151. uint8_t *text, uint32_t textlen, uint8_t *digest);
  152. extern int sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id);
  153. /* mbuf versions */
  154. extern uint32_t sctp_hmac_m(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
  155. struct mbuf *m, uint32_t m_offset, uint8_t *digest, uint32_t trailer);
  156. extern uint32_t sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key,
  157. struct mbuf *m, uint32_t m_offset, uint8_t *digest);
  158. /*
  159. * authentication routines
  160. */
  161. extern void sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid);
  162. extern void sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid);
  163. extern int sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid);
  164. extern int sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid);
  165. extern int sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid);
  166. extern int sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid);
  167. extern int sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid);
  168. extern int sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid);
  169. extern void sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
  170. uint32_t offset, uint32_t length);
  171. extern void sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
  172. struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t key_id);
  173. extern struct mbuf *sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
  174. struct sctp_auth_chunk **auth_ret, uint32_t *offset,
  175. struct sctp_tcb *stcb, uint8_t chunk);
  176. extern int sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *ch,
  177. struct mbuf *m, uint32_t offset);
  178. extern void sctp_notify_authentication(struct sctp_tcb *stcb,
  179. uint32_t indication, uint16_t keyid, uint16_t alt_keyid, int so_locked);
  180. extern int sctp_validate_init_auth_params(struct mbuf *m, int offset,
  181. int limit);
  182. extern void sctp_initialize_auth_params(struct sctp_inpcb *inp,
  183. struct sctp_tcb *stcb);
  184. /* test functions */
  185. #ifdef SCTP_HMAC_TEST
  186. extern void sctp_test_hmac_sha1(void);
  187. extern void sctp_test_authkey(void);
  188. #endif
  189. #endif /* __SCTP_AUTH_H__ */