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