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