xref: /openbsd/sbin/isakmpd/sa.h (revision f3571e78)
1 /*	$OpenBSD: sa.h,v 1.32 2003/06/04 07:31:17 ho Exp $	*/
2 /*	$EOM: sa.h,v 1.58 2000/10/10 12:39:01 provos Exp $	*/
3 
4 /*
5  * Copyright (c) 1998, 1999, 2001 Niklas Hallqvist.  All rights reserved.
6  * Copyright (c) 1999, 2001 Angelos D. Keromytis.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * This code was written under funding by Ericsson Radio Systems.
31  */
32 
33 #ifndef _SA_H_
34 #define _SA_H_
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/queue.h>
39 #include <sys/socket.h>
40 
41 #include "isakmp.h"
42 
43 /* Remove a SA if it has not been fully negotiated in this time.  */
44 #define SA_NEGOTIATION_MAX_TIME 120
45 
46 struct crypto_xf;
47 struct doi;
48 struct event;
49 struct exchange;
50 struct keystate;
51 struct message;
52 struct payload;
53 struct sa;
54 struct transport;
55 
56 /* A protection suite consists of a set of protocol descriptions like this.  */
57 struct proto {
58   /* Link to the next protocol in the suite.  */
59   TAILQ_ENTRY (proto) link;
60 
61   /* The SA we belong to.  */
62   struct sa *sa;
63 
64   /* The protocol number as found in the proposal payload.  */
65   u_int8_t no;
66 
67   /* The protocol this SA is for.  */
68   u_int8_t proto;
69 
70   /* Security parameter index info.  Element 0 - outgoing, 1 - incoming.  */
71   u_int8_t spi_sz[2];
72   u_int8_t *spi[2];
73 
74   /*
75    * The chosen transform, only valid while the incoming SA payload that held
76    * it is available for duplicate testing.
77    */
78   struct payload *chosen;
79 
80   /* The chosen transform's ID.  */
81   u_int8_t id;
82 
83   /* DOI-specific data.  */
84   void *data;
85 };
86 
87 struct sa {
88   /* Link to SAs with the same hash value.  */
89   LIST_ENTRY (sa) link;
90 
91   /*
92    * When several SA's are being negotiated in one message we connect them
93    * through this link.
94    */
95   TAILQ_ENTRY (sa) next;
96 
97   /* A name of the major policy deciding offers and acceptable proposals.  */
98   char *name;
99 
100   /* The transport this SA got negotiated over.  */
101   struct transport *transport;
102 
103   /* Both initiator and responder cookies.  */
104   u_int8_t cookies[ISAKMP_HDR_COOKIES_LEN];
105 
106   /* The message ID signifying non-ISAKMP SAs.  */
107   u_int8_t message_id[ISAKMP_HDR_MESSAGE_ID_LEN];
108 
109   /* The protection suite chosen.  */
110   TAILQ_HEAD (proto_head, proto) protos;
111 
112   /* The exchange type we should use when rekeying.  */
113   u_int8_t exch_type;
114 
115   /* Phase is 1 for ISAKMP SAs, and 2 for application ones.  */
116   u_int8_t phase;
117 
118   /* A reference counter for this structure.  */
119   u_int16_t refcnt;
120 
121   /* Various flags, look below for descriptions.  */
122   u_int32_t flags;
123 
124   /* The DOI that is to handle DOI-specific issues for this SA.  */
125   struct doi *doi;
126 
127   /* Crypto info needed to encrypt/decrypt packets protected by this SA.  */
128   struct crypto_xf *crypto;
129   int key_length;
130   struct keystate *keystate;
131 
132   /* IDs from Phase 1 */
133   u_int8_t *id_i;
134   size_t id_i_len;
135   u_int8_t *id_r;
136   size_t id_r_len;
137 
138   /* Set if we were the initiator of the SA/exchange in Phase 1 */
139   int initiator;
140 
141   /* Policy session ID, where applicable, copied over from the exchange */
142   int policy_id;
143 
144   /*
145    * The key used to authenticate phase 1, in printable format, used only by
146    * KeyNote.
147    */
148   char *keynote_key;
149 
150   /*
151    * Certificates or other information from Phase 1; these are copied from the
152    * exchange, so look at exchange.h for an explanation of their use.
153    */
154   int recv_certtype, recv_keytype;
155   /* Certificate received from peer, native format.  */
156   void *recv_cert;
157   /* Key peer used to authenticate, native format.  */
158   void *recv_key;
159 
160   /*
161    * Certificates or other information we used to authenticate to the peer,
162    * Phase 1.
163    */
164   int sent_certtype;
165   /* Certificate (to be) sent to peer, native format.  */
166   void *sent_cert;
167 
168   /* DOI-specific opaque data.  */
169   void *data;
170 
171   /* Lifetime data.  */
172   u_int64_t seconds;
173   u_int64_t kilobytes;
174 
175   /* ACQUIRE sequence number */
176   u_int32_t seq;
177 
178   /* The events that will occur when an SA has timed out.  */
179   struct event *soft_death;
180   struct event *death;
181 };
182 
183 /* This SA is alive.  */
184 #define SA_FLAG_READY		0x01
185 
186 /* Renegotiate the SA at each expiry.  */
187 #define SA_FLAG_STAYALIVE	0x02
188 
189 /* Establish the SA when it is needed.  */
190 #define SA_FLAG_ONDEMAND	0x04
191 
192 /* This SA has been replaced by another newer one.  */
193 #define SA_FLAG_REPLACED	0x08
194 
195 /* This SA has seen a soft timeout and wants to be renegotiated on use.  */
196 #define SA_FLAG_FADING		0x10
197 
198 /* This SA should always be actively renegotiated (with us as initiator).  */
199 #define SA_FLAG_ACTIVE_ONLY	0x20
200 
201 /* This SA flag is a placeholder for a TRANSACTION exchange "SA flag".  */
202 #define SA_FLAG_IKECFG		0x40
203 
204 /* Outfile for detailed SA information. */
205 #define SA_FILE "/var/run/isakmpd_sa"
206 
207 extern void proto_free (struct proto *proto);
208 extern int sa_add_transform (struct sa *, struct payload *, int,
209 			     struct proto **);
210 extern int sa_create (struct exchange *, struct transport *);
211 extern int sa_enter (struct sa *);
212 extern void sa_delete (struct sa *, int);
213 extern void sa_teardown_all (void);
214 extern struct sa *sa_find (int (*) (struct sa *, void *), void *);
215 extern int sa_flag (char *);
216 extern void sa_free (struct sa *);
217 extern void sa_init (void);
218 extern void sa_reinit (void);
219 extern struct sa *sa_isakmp_lookup_by_peer (struct sockaddr *, socklen_t);
220 extern void sa_isakmp_upgrade (struct message *);
221 extern struct sa *sa_lookup (u_int8_t *, u_int8_t *);
222 extern struct sa *sa_lookup_by_peer (struct sockaddr *, socklen_t);
223 extern struct sa *sa_lookup_by_header (u_int8_t *, int);
224 extern struct sa *sa_lookup_by_name (char *, int);
225 extern struct sa *sa_lookup_from_icookie (u_int8_t *);
226 extern struct sa *sa_lookup_isakmp_sa (struct sockaddr *, u_int8_t *);
227 extern void sa_mark_replaced (struct sa *);
228 extern void sa_reference (struct sa *);
229 extern void sa_release (struct sa *);
230 extern void sa_remove (struct sa *);
231 extern void sa_report (void);
232 extern void sa_dump (int, int, char *, struct sa *);
233 extern void sa_report_all (void);
234 extern int sa_setup_expirations (struct sa *);
235 #endif /* _SA_H_ */
236