1 /*- 2 * Copyright (c) 2001-2006, Cisco Systems, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * a) Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * 10 * b) Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the distribution. 13 * 14 * c) Neither the name of Cisco Systems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 * THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 #define HAVE_SHA2 34 35 #ifndef __SCTP_AUTH_H__ 36 #define __SCTP_AUTH_H__ 37 38 #include <sys/queue.h> 39 #include <sys/mbuf.h> 40 41 #ifdef USE_SCTP_SHA1 42 #include <netinet/sctp_sha1.h> 43 #else 44 #include <crypto/sha1.h> 45 /* map standard crypto API names */ 46 #define SHA1_Init SHA1Init 47 #define SHA1_Update SHA1Update 48 #define SHA1_Final(x,y) SHA1Final((caddr_t)x, y) 49 #endif 50 51 #if defined(HAVE_SHA2) 52 #include <crypto/sha2/sha2.h> 53 #endif 54 55 #include <sys/md5.h> 56 /* map standard crypto API names */ 57 #define MD5_Init MD5Init 58 #define MD5_Update MD5Update 59 #define MD5_Final MD5Final 60 61 /* digest lengths */ 62 #define SCTP_AUTH_DIGEST_LEN_SHA1 20 63 #define SCTP_AUTH_DIGEST_LEN_MD5 16 64 #define SCTP_AUTH_DIGEST_LEN_SHA224 28 65 #define SCTP_AUTH_DIGEST_LEN_SHA256 32 66 #define SCTP_AUTH_DIGEST_LEN_SHA384 48 67 #define SCTP_AUTH_DIGEST_LEN_SHA512 64 68 #define SCTP_AUTH_DIGEST_LEN_MAX 64 69 70 /* random sizes */ 71 #define SCTP_AUTH_RANDOM_SIZE_DEFAULT 32 72 #define SCTP_AUTH_RANDOM_SIZE_REQUIRED 32 73 #define SCTP_AUTH_RANDOM_SIZE_MAX 256 74 75 /* union of all supported HMAC algorithm contexts */ 76 typedef union sctp_hash_context { 77 SHA1_CTX sha1; 78 MD5_CTX md5; 79 #ifdef HAVE_SHA2 80 SHA256_CTX sha256; 81 SHA384_CTX sha384; 82 SHA512_CTX sha512; 83 #endif 84 } sctp_hash_context_t; 85 86 typedef struct sctp_key { 87 uint32_t keylen; 88 uint8_t key[0]; 89 } sctp_key_t; 90 91 typedef struct sctp_shared_key { 92 LIST_ENTRY(sctp_shared_key) next; 93 sctp_key_t *key; /* key text */ 94 uint16_t keyid; /* shared key ID */ 95 } sctp_sharedkey_t; 96 97 LIST_HEAD(sctp_keyhead, sctp_shared_key); 98 99 /* authentication chunks list */ 100 typedef struct sctp_auth_chklist { 101 uint8_t chunks[256]; 102 uint8_t num_chunks; 103 } sctp_auth_chklist_t; 104 105 /* hmac algos supported list */ 106 typedef struct sctp_hmaclist { 107 uint16_t max_algo; /* max algorithms allocated */ 108 uint16_t num_algo; /* num algorithms used */ 109 uint16_t hmac[0]; 110 } sctp_hmaclist_t; 111 112 /* authentication info */ 113 typedef struct sctp_authinfo { 114 sctp_key_t *random; /* local random key (concatenated) */ 115 uint32_t random_len; /* local random number length for param */ 116 sctp_key_t *peer_random;/* peer's random key (concatenated) */ 117 uint16_t assoc_keyid; /* current send keyid (cached) */ 118 uint16_t recv_keyid; /* last recv keyid (cached) */ 119 sctp_key_t *assoc_key; /* cached send key */ 120 sctp_key_t *recv_key; /* cached recv key */ 121 } sctp_authinfo_t; 122 123 124 /* 125 * global variables 126 */ 127 extern uint32_t sctp_asconf_auth_nochk; /* sysctl to disable ASCONF auth chk */ 128 extern uint32_t sctp_auth_disable; /* sysctl for temp feature interop */ 129 extern uint32_t sctp_auth_random_len; /* sysctl */ 130 131 /* 132 * Macros 133 */ 134 135 #define sctp_auth_is_required_chunk(chunk, list) ((list == NULL) ? (0) : (list->chunks[chunk] != 0)) 136 137 /* 138 * function prototypes 139 */ 140 141 /* socket option api functions */ 142 extern sctp_auth_chklist_t *sctp_alloc_chunklist(void); 143 extern void sctp_free_chunklist(sctp_auth_chklist_t * chklist); 144 extern void sctp_clear_chunklist(sctp_auth_chklist_t * chklist); 145 extern sctp_auth_chklist_t *sctp_copy_chunklist(sctp_auth_chklist_t * chklist); 146 extern int sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list); 147 extern int sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list); 148 extern int sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list); 149 extern void sctp_auth_set_default_chunks(sctp_auth_chklist_t * list); 150 extern int 151 sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, 152 uint8_t * ptr); 153 extern int sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr); 154 extern int 155 sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks, 156 sctp_auth_chklist_t * list); 157 158 /* key handling */ 159 extern sctp_key_t *sctp_alloc_key(uint32_t keylen); 160 extern void sctp_free_key(sctp_key_t * key); 161 extern void sctp_print_key(sctp_key_t * key, const char *str); 162 extern void sctp_show_key(sctp_key_t * key, const char *str); 163 extern sctp_key_t *sctp_generate_random_key(uint32_t keylen); 164 extern sctp_key_t *sctp_set_key(uint8_t * key, uint32_t keylen); 165 extern sctp_key_t * 166 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, 167 sctp_key_t * shared); 168 169 /* shared key handling */ 170 extern sctp_sharedkey_t *sctp_alloc_sharedkey(void); 171 extern void sctp_free_sharedkey(sctp_sharedkey_t * skey); 172 extern sctp_sharedkey_t * 173 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, 174 uint16_t key_id); 175 extern void 176 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys, 177 sctp_sharedkey_t * new_skey); 178 extern int 179 sctp_copy_skeylist(const struct sctp_keyhead *src, 180 struct sctp_keyhead *dest); 181 182 /* hmac list handling */ 183 extern sctp_hmaclist_t *sctp_alloc_hmaclist(uint8_t num_hmacs); 184 extern void sctp_free_hmaclist(sctp_hmaclist_t * list); 185 extern int sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id); 186 extern sctp_hmaclist_t *sctp_copy_hmaclist(sctp_hmaclist_t * list); 187 extern sctp_hmaclist_t *sctp_default_supported_hmaclist(void); 188 extern uint16_t 189 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, 190 sctp_hmaclist_t * local); 191 extern int sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr); 192 extern int 193 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, 194 uint32_t num_hmacs); 195 196 extern sctp_authinfo_t *sctp_alloc_authinfo(void); 197 extern void sctp_free_authinfo(sctp_authinfo_t * authinfo); 198 199 /* keyed-HMAC functions */ 200 extern uint32_t sctp_get_auth_chunk_len(uint16_t hmac_algo); 201 extern uint32_t sctp_get_hmac_digest_len(uint16_t hmac_algo); 202 extern uint32_t 203 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 204 const uint8_t * text, uint32_t textlen, uint8_t * digest); 205 extern int 206 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 207 const uint8_t * text, uint32_t textlen, uint8_t * digest, 208 uint32_t digestlen); 209 extern uint32_t 210 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, 211 const uint8_t * text, uint32_t textlen, uint8_t * digest); 212 extern int sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id); 213 214 /* mbuf versions */ 215 extern uint32_t 216 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 217 struct mbuf *m, uint32_t m_offset, uint8_t * digest); 218 extern uint32_t 219 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m, 220 uint32_t m_offset, uint8_t * digest); 221 222 /* 223 * authentication routines 224 */ 225 extern void sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid); 226 extern void sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid); 227 extern int sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid); 228 extern int sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid); 229 extern int sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid); 230 extern int sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid); 231 232 extern void 233 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m, 234 uint32_t offset, uint32_t length); 235 extern void 236 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset, 237 struct sctp_auth_chunk *auth, 238 struct sctp_tcb *stcb); 239 extern struct mbuf * 240 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end, 241 struct sctp_auth_chunk **auth_ret, 242 uint32_t * offset, struct sctp_tcb *stcb, 243 uint8_t chunk); 244 extern int 245 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *ch, 246 struct mbuf *m, uint32_t offset); 247 extern void 248 sctp_notify_authentication(struct sctp_tcb *stcb, 249 uint32_t indication, uint16_t keyid, 250 uint16_t alt_keyid); 251 extern int 252 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit); 253 extern void 254 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb); 255 256 257 /* test functions */ 258 extern void sctp_test_hmac_sha1(void); 259 extern void sctp_test_hmac_md5(void); 260 extern void sctp_test_authkey(void); 261 262 #endif /* __SCTP_AUTH_H__ */ 263