Don't assume sa.sa_family is a short int.
[tinc] / src / sptps.h
1 /*
2     sptps.h -- Simple Peer-to-Peer Security
3     Copyright (C) 2011-2014 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef __SPTPS_H__
21 #define __SPTPS_H__
22
23 #include "system.h"
24
25 #include "chacha-poly1305/chacha-poly1305.h"
26 #include "ecdh.h"
27 #include "ecdsa.h"
28
29 #define SPTPS_VERSION 0
30
31 // Record types
32 #define SPTPS_HANDSHAKE 128   // Key exchange and authentication
33 #define SPTPS_ALERT 129       // Warning or error messages
34 #define SPTPS_CLOSE 130       // Application closed the connection
35
36 // Key exchange states
37 #define SPTPS_KEX 1           // Waiting for the first Key EXchange record
38 #define SPTPS_SECONDARY_KEX 2 // Ready to receive a secondary Key EXchange record
39 #define SPTPS_SIG 3           // Waiting for a SIGnature record
40 #define SPTPS_ACK 4           // Waiting for an ACKnowledgement record
41
42 // Overhead for datagrams
43 #define SPTPS_DATAGRAM_OVERHEAD 21
44
45 typedef bool (*send_data_t)(void *handle, uint8_t type, const void *data, size_t len);
46 typedef bool (*receive_record_t)(void *handle, uint8_t type, const void *data, uint16_t len);
47
48 typedef struct sptps {
49         bool initiator;
50         bool datagram;
51         int state;
52
53         char *inbuf;
54         size_t buflen;
55         uint16_t reclen;
56
57         bool instate;
58         chacha_poly1305_ctx_t *incipher;
59         uint32_t inseqno;
60         uint32_t received;
61         unsigned int replaywin;
62         unsigned int farfuture;
63         char *late;
64
65         bool outstate;
66         chacha_poly1305_ctx_t *outcipher;
67         uint32_t outseqno;
68
69         ecdsa_t *mykey;
70         ecdsa_t *hiskey;
71         ecdh_t *ecdh;
72
73         char *mykex;
74         char *hiskex;
75         char *key;
76         char *label;
77         size_t labellen;
78
79         void *handle;
80         send_data_t send_data;
81         receive_record_t receive_record;
82 } sptps_t;
83
84 extern unsigned int sptps_replaywin;
85 extern void sptps_log_quiet(sptps_t *s, int s_errno, const char *format, va_list ap);
86 extern void sptps_log_stderr(sptps_t *s, int s_errno, const char *format, va_list ap);
87 extern void (*sptps_log)(sptps_t *s, int s_errno, const char *format, va_list ap);
88 extern bool sptps_start(sptps_t *s, void *handle, bool initiator, bool datagram, ecdsa_t *mykey, ecdsa_t *hiskey, const void *label, size_t labellen, send_data_t send_data, receive_record_t receive_record);
89 extern bool sptps_stop(sptps_t *s);
90 extern bool sptps_send_record(sptps_t *s, uint8_t type, const void *data, uint16_t len);
91 extern size_t sptps_receive_data(sptps_t *s, const void *data, size_t len);
92 extern bool sptps_force_kex(sptps_t *s);
93 extern bool sptps_verify_datagram(sptps_t *s, const void *data, size_t len);
94
95 #endif