]> git.meshlink.io Git - meshlink/blob - src/sptps.h
Never automatically try to bind to ports >= 32768.
[meshlink] / src / sptps.h
1 #ifndef MESHLINK_SPTPS_H
2 #define MESHLINK_SPTPS_H
3
4 /*
5     sptps.h -- Simple Peer-to-Peer Security
6     Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
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 // Packet header sizes
43 #define SPTPS_OVERHEAD 19
44 #define SPTPS_DATAGRAM_OVERHEAD 21
45
46 typedef bool (*send_data_t)(void *handle, uint8_t type, const void *data, size_t len);
47 typedef bool (*receive_record_t)(void *handle, uint8_t type, const void *data, uint16_t len);
48
49 typedef struct sptps {
50         // State
51         bool initiator;
52         bool datagram;
53         bool instate;
54         bool outstate;
55
56         int state;
57
58         // Main member variables
59         char *inbuf;
60         size_t buflen;
61
62         chacha_poly1305_ctx_t *incipher;
63         uint32_t replaywin;
64         uint32_t inseqno;
65         uint32_t received;
66         uint16_t reclen;
67
68         chacha_poly1305_ctx_t *outcipher;
69         uint32_t outseqno;
70
71         char *late;
72
73         char *decrypted_buffer;
74         size_t decrypted_buffer_len;
75
76         // Callbacks
77         void *handle;
78         send_data_t send_data;
79         receive_record_t receive_record;
80
81         // Variables used for the authentication phase
82         ecdsa_t *mykey;
83         ecdsa_t *hiskey;
84         ecdh_t *ecdh;
85
86         char *mykex;
87         char *hiskex;
88         char *key;
89         char *label;
90         size_t labellen;
91
92 } sptps_t;
93
94 void sptps_log_quiet(sptps_t *s, int s_errno, const char *format, va_list ap);
95 void sptps_log_stderr(sptps_t *s, int s_errno, const char *format, va_list ap);
96 extern void (*sptps_log)(sptps_t *s, int s_errno, const char *format, va_list ap);
97 bool sptps_start(sptps_t *s, void *handle, bool initiator, bool datagram, ecdsa_t *mykey, ecdsa_t *hiskey, const char *label, size_t labellen, send_data_t send_data, receive_record_t receive_record) __attribute__((__warn_unused_result__));
98 bool sptps_stop(sptps_t *s);
99 bool sptps_send_record(sptps_t *s, uint8_t type, const void *data, uint16_t len);
100 bool sptps_receive_data(sptps_t *s, const void *data, size_t len) __attribute__((__warn_unused_result__));
101 bool sptps_force_kex(sptps_t *s) __attribute__((__warn_unused_result__));
102 bool sptps_verify_datagram(sptps_t *s, const void *data, size_t len) __attribute__((__warn_unused_result__));
103
104 #endif