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