1*2f1aa25bSmpi /* $OpenBSD: exchange.h,v 1.37 2018/01/15 09:54:48 mpi Exp $ */ 2fc150becSniklas /* $EOM: exchange.h,v 1.28 2000/09/28 12:54:28 niklas Exp $ */ 32040585eSniklas 42040585eSniklas /* 542af7185Sniklas * Copyright (c) 1998, 1999, 2001 Niklas Hallqvist. All rights reserved. 62040585eSniklas * 72040585eSniklas * Redistribution and use in source and binary forms, with or without 82040585eSniklas * modification, are permitted provided that the following conditions 92040585eSniklas * are met: 102040585eSniklas * 1. Redistributions of source code must retain the above copyright 112040585eSniklas * notice, this list of conditions and the following disclaimer. 122040585eSniklas * 2. Redistributions in binary form must reproduce the above copyright 132040585eSniklas * notice, this list of conditions and the following disclaimer in the 142040585eSniklas * documentation and/or other materials provided with the distribution. 152040585eSniklas * 162040585eSniklas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 172040585eSniklas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 182040585eSniklas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 192040585eSniklas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 202040585eSniklas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 212040585eSniklas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 222040585eSniklas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 232040585eSniklas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 242040585eSniklas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 252040585eSniklas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 262040585eSniklas */ 272040585eSniklas 282040585eSniklas /* 292040585eSniklas * This code was written under funding by Ericsson Radio Systems. 302040585eSniklas */ 312040585eSniklas 322040585eSniklas #ifndef _EXCHANGE_H_ 332040585eSniklas #define _EXCHANGE_H_ 342040585eSniklas 352040585eSniklas #include <sys/types.h> 362040585eSniklas #include <sys/queue.h> 372040585eSniklas 382040585eSniklas #include "exchange_num.h" 392040585eSniklas #include "isakmp.h" 402040585eSniklas 412040585eSniklas /* Remove an exchange if it has not been fully negotiated in this time. */ 422040585eSniklas #define EXCHANGE_MAX_TIME 120 432040585eSniklas 442040585eSniklas struct crypto_xf; 452040585eSniklas struct certreq_aca; 462040585eSniklas struct doi; 472040585eSniklas struct event; 482040585eSniklas struct keystate; 492040585eSniklas struct message; 502040585eSniklas struct payload; 512040585eSniklas struct transport; 522040585eSniklas struct sa; 532040585eSniklas 542040585eSniklas struct exchange { 552040585eSniklas /* Link to exchanges with the same hash value. */ 562040585eSniklas LIST_ENTRY(exchange) link; 572040585eSniklas 5810dd6cf1Smpi /* This exchange is linked to the global exchange list. */ 5910dd6cf1Smpi int linked; 6010dd6cf1Smpi 612b81057dSniklas /* A name of the SAs this exchange will result in. XXX non unique? */ 622b81057dSniklas char *name; 632b81057dSniklas 64fb9475d6Sderaadt /* 65fb9475d6Sderaadt * A name of the major policy deciding offers and acceptable 66fb9475d6Sderaadt * proposals. 67fb9475d6Sderaadt */ 682b81057dSniklas char *policy; 692b81057dSniklas 702b81057dSniklas /* 712b81057dSniklas * A function with a polymorphic argument called after the exchange 7241dd258eSniklas * has been run to its end, successfully. The 2nd argument is true 7341dd258eSniklas * if the finalization hook is called due to the exchange not running 7441dd258eSniklas * to its end normally. 752b81057dSniklas */ 761328c4e6Sniklas void (*finalize)(struct exchange *, void *, int); 772b81057dSniklas void *finalize_arg; 782b81057dSniklas 792040585eSniklas /* When several SA's are being negotiated we keep them here. */ 802040585eSniklas TAILQ_HEAD(sa_head, sa) sa_list; 812040585eSniklas 822040585eSniklas /* 832040585eSniklas * The event that will occur when it has taken too long time to try to 842040585eSniklas * run the exchange and which will trigger auto-destruction. 852040585eSniklas */ 862040585eSniklas struct event *death; 872040585eSniklas 882040585eSniklas /* 892040585eSniklas * Both initiator and responder cookies. 902040585eSniklas * XXX For code clarity we might split this into two fields. 912040585eSniklas */ 922040585eSniklas u_int8_t cookies[ISAKMP_HDR_COOKIES_LEN]; 932040585eSniklas 942040585eSniklas /* The message ID signifying phase 2 exchanges. */ 952040585eSniklas u_int8_t message_id[ISAKMP_HDR_MESSAGE_ID_LEN]; 962040585eSniklas 972040585eSniklas /* The exchange type we are using. */ 982040585eSniklas u_int8_t type; 992040585eSniklas 1002040585eSniklas /* Phase is 1 for ISAKMP SA exchanges, and 2 for application ones. */ 1012040585eSniklas u_int8_t phase; 1022040585eSniklas 1032040585eSniklas /* The "step counter" of the exchange, starting from zero. */ 1042040585eSniklas u_int8_t step; 1052040585eSniklas 1062040585eSniklas /* 1 if we are the initiator, 0 if we are the responder. */ 1072040585eSniklas u_int8_t initiator; 1082040585eSniklas 1092040585eSniklas /* Various flags, look below for descriptions. */ 1102040585eSniklas u_int32_t flags; 1112040585eSniklas 1122040585eSniklas /* The DOI that is to handle DOI-specific issues for this exchange. */ 1132040585eSniklas struct doi *doi; 1142040585eSniklas 1152040585eSniklas /* 116baf9095eShshoexer * A "program counter" into the script that validate message contents 117baf9095eShshoexer * for this exchange. 1182040585eSniklas */ 1192040585eSniklas int16_t *exch_pc; 1202040585eSniklas 1212040585eSniklas /* The last message received, used for checking for duplicates. */ 1222040585eSniklas struct message *last_received; 1232040585eSniklas 1242040585eSniklas /* The last message sent, to be acked when something new is received. */ 1252040585eSniklas struct message *last_sent; 1262040585eSniklas 1272040585eSniklas /* 128baf9095eShshoexer * If some message is queued up for sending, we want to be able to 129baf9095eShshoexer * remove it from the queue, when the exchange is deleted. 13039eca902Sniklas */ 13139eca902Sniklas struct message *in_transit; 13239eca902Sniklas 13339eca902Sniklas /* 1342040585eSniklas * Initiator's & responder's nonces respectively, with lengths. 1352040585eSniklas * XXX Should this be in the DOI-specific parts instead? 1362040585eSniklas */ 1372040585eSniklas u_int8_t *nonce_i; 1382040585eSniklas size_t nonce_i_len; 1392040585eSniklas u_int8_t *nonce_r; 1402040585eSniklas size_t nonce_r_len; 1412040585eSniklas 142fb9475d6Sderaadt /* 143fb9475d6Sderaadt * The ID payload contents for the initiator & responder, 144fb9475d6Sderaadt * respectively. 145fb9475d6Sderaadt */ 1462040585eSniklas u_int8_t *id_i; 1472040585eSniklas size_t id_i_len; 1482040585eSniklas u_int8_t *id_r; 1492040585eSniklas size_t id_r_len; 1502040585eSniklas 151a7b8c2e4Sniklas /* Policy session identifier, where applicable. */ 152ccea4478Sniklas int policy_id; 153ccea4478Sniklas 1542040585eSniklas /* Crypto info needed to encrypt/decrypt packets in this exchange. */ 1552040585eSniklas struct crypto_xf *crypto; 1569183be26Sho size_t key_length; 1572040585eSniklas struct keystate *keystate; 1582040585eSniklas 159fb9475d6Sderaadt /* 160fb9475d6Sderaadt * Used only by KeyNote, to cache the key used to authenticate Phase 161fb9475d6Sderaadt * 1 162fb9475d6Sderaadt */ 1638bc59326Sangelos char *keynote_key; /* printable format */ 1648bc59326Sangelos 165fb1921ccSniklas /* 166fb1921ccSniklas * Received certificate - used to verify signatures on packet, 167fb1921ccSniklas * stored here for later policy processing. 1688bc59326Sangelos * 1698bc59326Sangelos * The rules for the recv_* and sent_* fields are: 1708bc59326Sangelos * - recv_cert stores the credential (if any) received from the peer; 1718bc59326Sangelos * the kernel may pass us one, but we ignore it. We pass it to the 1728bc59326Sangelos * kernel so processes can peek at it. When doing passphrase 1738bc59326Sangelos * authentication in Phase 1, this is empty. 1748bc59326Sangelos * - recv_key stores the key (public or private) used by the peer 1758bc59326Sangelos * to authenticate. Otherwise, same properties as recv_cert except 1768bc59326Sangelos * that we don't tell the kernel about passphrases (so we don't 1778bc59326Sangelos * reveal system-wide passphrases). Processes that used passphrase 1788bc59326Sangelos * authentication already know the passphrase! We ignore it if/when 1798bc59326Sangelos * received from the kernel (meaningless). 1808bc59326Sangelos * - sent_cert stores the credential, if any, we used to authenticate 1818bc59326Sangelos * with the peer. It may be passed to us by the kernel, or we may 1828bc59326Sangelos * have found it in our certificate storage. In either case, there's 1838bc59326Sangelos * no point passing it to the kernel, so we don't. 1848bc59326Sangelos * - sent key stores the private key we used for authentication with 1858bc59326Sangelos * the peer (private key or passphrase). This may have been received 1868bc59326Sangelos * from the kernel, or may be a system-wide setting. In either case, 1878bc59326Sangelos * we don't pass it to the kernel, to avoid revealing such information 1888bc59326Sangelos * to processes (processes either already know it, or have no business 1898bc59326Sangelos * knowing it). 190fb1921ccSniklas */ 1918bc59326Sangelos int recv_certtype, recv_keytype; 192fb9475d6Sderaadt void *recv_cert; /* Certificate received from peer, 193fb9475d6Sderaadt * native format */ 194fb9475d6Sderaadt void *recv_key; /* Key peer used to authenticate, 195fb9475d6Sderaadt * native format */ 1968bc59326Sangelos 197c95fe336Shshoexer /* Likewise, for certificates we use. */ 1988bc59326Sangelos int sent_certtype, sent_keytype; 199fb9475d6Sderaadt void *sent_cert; /* Certificate (to be) sent to peer, 200fb9475d6Sderaadt * native format */ 201fb1921ccSniklas 202a7b8c2e4Sniklas /* ACQUIRE sequence number. */ 2032bf8caf4Sangelos u_int32_t seq; 2042bf8caf4Sangelos 205ee0e5087Sniklas /* XXX This is no longer necessary, it is covered by policy. */ 206ee0e5087Sniklas 207a7b8c2e4Sniklas /* Acceptable authorities for cert requests. */ 2082040585eSniklas TAILQ_HEAD(aca_head, certreq_aca) aca_list; 2092040585eSniklas 2102040585eSniklas /* DOI-specific opaque data. */ 2112040585eSniklas void *data; 2122040585eSniklas }; 2132040585eSniklas 2142040585eSniklas /* The flag bits. */ 215875f57d0Shshoexer #define EXCHANGE_FLAG_I_COMMITTED 0x0001 216875f57d0Shshoexer #define EXCHANGE_FLAG_HE_COMMITTED 0x0002 2172040585eSniklas #define EXCHANGE_FLAG_COMMITTED (EXCHANGE_FLAG_I_COMMITTED \ 2182040585eSniklas | EXCHANGE_FLAG_HE_COMMITTED) 219875f57d0Shshoexer #define EXCHANGE_FLAG_ENCRYPT 0x0004 220875f57d0Shshoexer #define EXCHANGE_FLAG_NAT_T_CAP_PEER 0x0008 /* Peer is NAT capable. */ 221875f57d0Shshoexer #define EXCHANGE_FLAG_NAT_T_ENABLE 0x0010 /* We are doing NAT-T. */ 222875f57d0Shshoexer #define EXCHANGE_FLAG_NAT_T_KEEPALIVE 0x0020 /* We are the NAT:ed peer. */ 223875f57d0Shshoexer #define EXCHANGE_FLAG_DPD_CAP_PEER 0x0040 /* Peer is DPD capable. */ 224875f57d0Shshoexer #define EXCHANGE_FLAG_NAT_T_RFC 0x0080 /* Peer does RFC NAT-T. */ 225875f57d0Shshoexer #define EXCHANGE_FLAG_NAT_T_DRAFT 0x0100 /* Peer does draft NAT-T.*/ 22617f91f1cShshoexer #define EXCHANGE_FLAG_OPENBSD 0x0200 /* Peer is OpenBSD */ 2272040585eSniklas 228c5d35a5bSniklas extern int exchange_add_certs(struct message *); 22905442ddfStom extern int exchange_add_certreqs(struct message *); 2302040585eSniklas extern void exchange_finalize(struct message *); 2312040585eSniklas extern void exchange_free(struct exchange *); 232c5d35a5bSniklas extern void exchange_free_aca_list(struct exchange *); 233baf9095eShshoexer extern void exchange_establish(char *name, void (*)(struct exchange *, 234a28d886cShshoexer void *, int), void *, int); 23510dd6cf1Smpi extern int exchange_establish_p1(struct transport *, u_int8_t, u_int32_t, 236baf9095eShshoexer char *, void *, void (*)(struct exchange *, void *, int), 237a28d886cShshoexer void *, int); 23810dd6cf1Smpi extern int exchange_establish_p2(struct sa *, u_int8_t, char *, void *, 239baf9095eShshoexer void (*)(struct exchange *, void *, int), void *); 2402040585eSniklas extern int exchange_gen_nonce(struct message *, size_t); 2412040585eSniklas extern void exchange_init(void); 2422040585eSniklas extern struct exchange *exchange_lookup(u_int8_t *, int); 243e79f6b54Sniklas extern struct exchange *exchange_lookup_by_name(char *, int); 2442040585eSniklas extern struct exchange *exchange_lookup_from_icookie(u_int8_t *); 2452040585eSniklas extern void exchange_report(void); 2462040585eSniklas extern void exchange_run(struct message *); 2472040585eSniklas extern int exchange_save_nonce(struct message *); 2482040585eSniklas extern int exchange_save_certreq(struct message *); 249b26670e8Sho extern int16_t *exchange_script(struct exchange *); 2502040585eSniklas extern struct exchange *exchange_setup_p1(struct message *, u_int32_t); 2512040585eSniklas extern struct exchange *exchange_setup_p2(struct message *, u_int8_t); 2522040585eSniklas extern void exchange_upgrade_p1(struct message *); 2532040585eSniklas 2542040585eSniklas #endif /* _EXCHANGE_H_ */ 255