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