1 /*- 2 * Copyright (c) 2001-2007, by 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 34 #ifndef __SCTP_AUTH_H__ 35 #define __SCTP_AUTH_H__ 36 37 38 /* digest lengths */ 39 #define SCTP_AUTH_DIGEST_LEN_SHA1 20 40 #define SCTP_AUTH_DIGEST_LEN_MD5 16 41 #define SCTP_AUTH_DIGEST_LEN_SHA224 28 42 #define SCTP_AUTH_DIGEST_LEN_SHA256 32 43 #define SCTP_AUTH_DIGEST_LEN_SHA384 48 44 #define SCTP_AUTH_DIGEST_LEN_SHA512 64 45 #define SCTP_AUTH_DIGEST_LEN_MAX 64 46 47 /* random sizes */ 48 #define SCTP_AUTH_RANDOM_SIZE_DEFAULT 32 49 #define SCTP_AUTH_RANDOM_SIZE_REQUIRED 32 50 #define SCTP_AUTH_RANDOM_SIZE_MAX 256 51 52 /* union of all supported HMAC algorithm contexts */ 53 typedef union sctp_hash_context { 54 SHA1_CTX sha1; 55 MD5_CTX md5; 56 #ifdef HAVE_SHA2 57 SHA256_CTX sha256; 58 SHA384_CTX sha384; 59 SHA512_CTX sha512; 60 #endif 61 } sctp_hash_context_t; 62 63 typedef struct sctp_key { 64 uint32_t keylen; 65 uint8_t key[0]; 66 } sctp_key_t; 67 68 typedef struct sctp_shared_key { 69 LIST_ENTRY(sctp_shared_key) next; 70 sctp_key_t *key; /* key text */ 71 uint16_t keyid; /* shared key ID */ 72 } sctp_sharedkey_t; 73 74 LIST_HEAD(sctp_keyhead, sctp_shared_key); 75 76 /* authentication chunks list */ 77 typedef struct sctp_auth_chklist { 78 uint8_t chunks[256]; 79 uint8_t num_chunks; 80 } sctp_auth_chklist_t; 81 82 /* hmac algos supported list */ 83 typedef struct sctp_hmaclist { 84 uint16_t max_algo; /* max algorithms allocated */ 85 uint16_t num_algo; /* num algorithms used */ 86 uint16_t hmac[0]; 87 } sctp_hmaclist_t; 88 89 /* authentication info */ 90 typedef struct sctp_authinfo { 91 sctp_key_t *random; /* local random key (concatenated) */ 92 uint32_t random_len; /* local random number length for param */ 93 sctp_key_t *peer_random;/* peer's random key (concatenated) */ 94 uint16_t assoc_keyid; /* current send keyid (cached) */ 95 uint16_t recv_keyid; /* last recv keyid (cached) */ 96 sctp_key_t *assoc_key; /* cached send key */ 97 sctp_key_t *recv_key; /* cached recv key */ 98 } sctp_authinfo_t; 99 100 101 102 /* 103 * Macros 104 */ 105 #define sctp_auth_is_required_chunk(chunk, list) ((list == NULL) ? (0) : (list->chunks[chunk] != 0)) 106 107 /* 108 * function prototypes 109 */ 110 111 /* socket option api functions */ 112 extern sctp_auth_chklist_t *sctp_alloc_chunklist(void); 113 extern void sctp_free_chunklist(sctp_auth_chklist_t * chklist); 114 extern void sctp_clear_chunklist(sctp_auth_chklist_t * chklist); 115 extern sctp_auth_chklist_t *sctp_copy_chunklist(sctp_auth_chklist_t * chklist); 116 extern int sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list); 117 extern int sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list); 118 extern size_t sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list); 119 extern void sctp_auth_set_default_chunks(sctp_auth_chklist_t * list); 120 extern int 121 sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr); 122 extern int sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr); 123 extern int 124 sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks, 125 sctp_auth_chklist_t * list); 126 127 /* key handling */ 128 extern sctp_key_t *sctp_alloc_key(uint32_t keylen); 129 extern void sctp_free_key(sctp_key_t * key); 130 extern void sctp_print_key(sctp_key_t * key, const char *str); 131 extern void sctp_show_key(sctp_key_t * key, const char *str); 132 extern sctp_key_t *sctp_generate_random_key(uint32_t keylen); 133 extern sctp_key_t *sctp_set_key(uint8_t * key, uint32_t keylen); 134 extern sctp_key_t * 135 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, 136 sctp_key_t * shared); 137 138 /* shared key handling */ 139 extern sctp_sharedkey_t *sctp_alloc_sharedkey(void); 140 extern void sctp_free_sharedkey(sctp_sharedkey_t * skey); 141 extern sctp_sharedkey_t * 142 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id); 143 extern void 144 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys, 145 sctp_sharedkey_t * new_skey); 146 extern int 147 sctp_copy_skeylist(const struct sctp_keyhead *src, 148 struct sctp_keyhead *dest); 149 150 /* hmac list handling */ 151 extern sctp_hmaclist_t *sctp_alloc_hmaclist(uint8_t num_hmacs); 152 extern void sctp_free_hmaclist(sctp_hmaclist_t * list); 153 extern int sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id); 154 extern sctp_hmaclist_t *sctp_copy_hmaclist(sctp_hmaclist_t * list); 155 extern sctp_hmaclist_t *sctp_default_supported_hmaclist(void); 156 extern uint16_t 157 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, 158 sctp_hmaclist_t * local); 159 extern int sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr); 160 extern int 161 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, 162 uint32_t num_hmacs); 163 164 extern sctp_authinfo_t *sctp_alloc_authinfo(void); 165 extern void sctp_free_authinfo(sctp_authinfo_t * authinfo); 166 167 /* keyed-HMAC functions */ 168 extern uint32_t sctp_get_auth_chunk_len(uint16_t hmac_algo); 169 extern uint32_t sctp_get_hmac_digest_len(uint16_t hmac_algo); 170 extern uint32_t 171 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 172 uint8_t * text, uint32_t textlen, uint8_t * digest); 173 extern int 174 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 175 uint8_t * text, uint32_t textlen, uint8_t * digest, 176 uint32_t digestlen); 177 extern uint32_t 178 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, 179 uint8_t * text, uint32_t textlen, uint8_t * digest); 180 extern int sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id); 181 182 /* mbuf versions */ 183 extern uint32_t 184 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 185 struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer); 186 extern uint32_t 187 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m, 188 uint32_t m_offset, uint8_t * digest); 189 190 /* 191 * authentication routines 192 */ 193 extern void sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid); 194 extern void sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid); 195 extern int sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid); 196 extern int sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid); 197 extern int sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid); 198 extern int sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid); 199 200 extern void 201 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m, 202 uint32_t offset, uint32_t length); 203 extern void 204 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset, 205 struct sctp_auth_chunk *auth, 206 struct sctp_tcb *stcb); 207 extern struct mbuf * 208 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end, 209 struct sctp_auth_chunk **auth_ret, 210 uint32_t * offset, struct sctp_tcb *stcb, 211 uint8_t chunk); 212 extern int 213 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *ch, 214 struct mbuf *m, uint32_t offset); 215 extern void 216 sctp_notify_authentication(struct sctp_tcb *stcb, 217 uint32_t indication, uint16_t keyid, 218 uint16_t alt_keyid); 219 extern int 220 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit); 221 extern void 222 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb); 223 224 225 /* test functions */ 226 extern void sctp_test_hmac_sha1(void); 227 extern void sctp_test_hmac_md5(void); 228 extern void sctp_test_authkey(void); 229 230 #endif /* __SCTP_AUTH_H__ */ 231