Use conditional compilation for cryptographic functions.
[tinc] / src / sptps.c
1 /*
2     sptps.c -- Simple Peer-to-Peer Security
3     Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>,
4                   2010      Brandon L. Black <blblack@gmail.com>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "cipher.h"
24 #include "crypto.h"
25 #include "digest.h"
26 #include "ecdh.h"
27 #include "ecdsa.h"
28 #include "logger.h"
29 #include "prf.h"
30 #include "sptps.h"
31
32 unsigned int sptps_replaywin = 16;
33
34 /*
35    Nonce MUST be exchanged first (done)
36    Signatures MUST be done over both nonces, to guarantee the signature is fresh
37    Otherwise: if ECDHE key of one side is compromised, it can be reused!
38
39    Add explicit tag to beginning of structure to distinguish the client and server when signing. (done)
40
41    Sign all handshake messages up to ECDHE kex with long-term public keys. (done)
42
43    HMACed KEX finished message to prevent downgrade attacks and prove you have the right key material (done by virtue of ECDSA over the whole ECDHE exchange?)
44
45    Explicit close message needs to be added.
46
47    Maybe do add some alert messages to give helpful error messages? Not more than TLS sends.
48
49    Use counter mode instead of OFB. (done)
50
51    Make sure ECC operations are fixed time (aka prevent side-channel attacks).
52 */
53
54 void sptps_log_quiet(sptps_t *s, int s_errno, const char *format, va_list ap) {
55 }
56
57 void sptps_log_stderr(sptps_t *s, int s_errno, const char *format, va_list ap) {
58         vfprintf(stderr, format, ap);
59         fputc('\n', stderr);
60 }
61
62 void (*sptps_log)(sptps_t *s, int s_errno, const char *format, va_list ap) = sptps_log_stderr;
63
64 // Log an error message.
65 static bool error(sptps_t *s, int s_errno, const char *format, ...) {
66         if(format) {
67                 va_list ap;
68                 va_start(ap, format);
69                 sptps_log(s, s_errno, format, ap);
70                 va_end(ap);
71         }
72
73         errno = s_errno;
74         return false;
75 }
76
77 static void warning(sptps_t *s, const char *format, ...) {
78         va_list ap;
79         va_start(ap, format);
80         sptps_log(s, 0, format, ap);
81         va_end(ap);
82 }
83
84 // Send a record (datagram version, accepts all record types, handles encryption and authentication).
85 static bool send_record_priv_datagram(sptps_t *s, uint8_t type, const char *data, uint16_t len) {
86         char buffer[len + 23UL];
87
88         // Create header with sequence number, length and record type
89         uint32_t seqno = htonl(s->outseqno++);
90         uint16_t netlen = htons(len);
91
92         memcpy(buffer, &netlen, 2);
93         memcpy(buffer + 2, &seqno, 4);
94         buffer[6] = type;
95
96         // Add plaintext (TODO: avoid unnecessary copy)
97         memcpy(buffer + 7, data, len);
98
99         if(s->outstate) {
100                 // If first handshake has finished, encrypt and HMAC
101                 cipher_set_counter(s->outcipher, &seqno, sizeof seqno);
102                 if(!cipher_counter_xor(s->outcipher, buffer + 6, len + 1UL, buffer + 6))
103                         return false;
104
105                 if(!digest_create(s->outdigest, buffer, len + 7UL, buffer + 7UL + len))
106                         return false;
107
108                 return s->send_data(s->handle, type, buffer + 2, len + 21UL);
109         } else {
110                 // Otherwise send as plaintext
111                 return s->send_data(s->handle, type, buffer + 2, len + 5UL);
112         }
113 }
114 // Send a record (private version, accepts all record types, handles encryption and authentication).
115 static bool send_record_priv(sptps_t *s, uint8_t type, const char *data, uint16_t len) {
116         if(s->datagram)
117                 return send_record_priv_datagram(s, type, data, len);
118
119         char buffer[len + 23UL];
120
121         // Create header with sequence number, length and record type
122         uint32_t seqno = htonl(s->outseqno++);
123         uint16_t netlen = htons(len);
124
125         memcpy(buffer, &seqno, 4);
126         memcpy(buffer + 4, &netlen, 2);
127         buffer[6] = type;
128
129         // Add plaintext (TODO: avoid unnecessary copy)
130         memcpy(buffer + 7, data, len);
131
132         if(s->outstate) {
133                 // If first handshake has finished, encrypt and HMAC
134                 if(!cipher_counter_xor(s->outcipher, buffer + 4, len + 3UL, buffer + 4))
135                         return false;
136
137                 if(!digest_create(s->outdigest, buffer, len + 7UL, buffer + 7UL + len))
138                         return false;
139
140                 return s->send_data(s->handle, type, buffer + 4, len + 19UL);
141         } else {
142                 // Otherwise send as plaintext
143                 return s->send_data(s->handle, type, buffer + 4, len + 3UL);
144         }
145 }
146
147 // Send an application record.
148 bool sptps_send_record(sptps_t *s, uint8_t type, const char *data, uint16_t len) {
149         // Sanity checks: application cannot send data before handshake is finished,
150         // and only record types 0..127 are allowed.
151         if(!s->outstate)
152                 return error(s, EINVAL, "Handshake phase not finished yet");
153
154         if(type >= SPTPS_HANDSHAKE)
155                 return error(s, EINVAL, "Invalid application record type");
156
157         return send_record_priv(s, type, data, len);
158 }
159
160 // Send a Key EXchange record, containing a random nonce and an ECDHE public key.
161 static bool send_kex(sptps_t *s) {
162         size_t keylen = ECDH_SIZE;
163
164         // Make room for our KEX message, which we will keep around since send_sig() needs it.
165         if(s->mykex)
166                 abort();
167         s->mykex = realloc(s->mykex, 1 + 32 + keylen);
168         if(!s->mykex)
169                 return error(s, errno, strerror(errno));
170
171         // Set version byte to zero.
172         s->mykex[0] = SPTPS_VERSION;
173
174         // Create a random nonce.
175         randomize(s->mykex + 1, 32);
176
177         // Create a new ECDH public key.
178         if(!(s->ecdh = ecdh_generate_public(s->mykex + 1 + 32)))
179                 return false;
180
181         return send_record_priv(s, SPTPS_HANDSHAKE, s->mykex, 1 + 32 + keylen);
182 }
183
184 // Send a SIGnature record, containing an ECDSA signature over both KEX records.
185 static bool send_sig(sptps_t *s) {
186         size_t keylen = ECDH_SIZE;
187         size_t siglen = ecdsa_size(s->mykey);
188
189         // Concatenate both KEX messages, plus tag indicating if it is from the connection originator, plus label
190         char msg[(1 + 32 + keylen) * 2 + 1 + s->labellen];
191         char sig[siglen];
192
193         msg[0] = s->initiator;
194         memcpy(msg + 1, s->mykex, 1 + 32 + keylen);
195         memcpy(msg + 1 + 33 + keylen, s->hiskex, 1 + 32 + keylen);
196         memcpy(msg + 1 + 2 * (33 + keylen), s->label, s->labellen);
197
198         // Sign the result.
199         if(!ecdsa_sign(s->mykey, msg, sizeof msg, sig))
200                 return false;
201
202         // Send the SIG exchange record.
203         return send_record_priv(s, SPTPS_HANDSHAKE, sig, sizeof sig);
204 }
205
206 // Generate key material from the shared secret created from the ECDHE key exchange.
207 static bool generate_key_material(sptps_t *s, const char *shared, size_t len) {
208         // Initialise cipher and digest structures if necessary
209         if(!s->outstate) {
210                 s->incipher = cipher_open_by_name("aes-256-ecb");
211                 s->outcipher = cipher_open_by_name("aes-256-ecb");
212                 s->indigest = digest_open_by_name("sha256", 16);
213                 s->outdigest = digest_open_by_name("sha256", 16);
214                 if(!s->incipher || !s->outcipher || !s->indigest || !s->outdigest)
215                         return false;
216         }
217
218         // Allocate memory for key material
219         size_t keylen = digest_keylength(s->indigest) + digest_keylength(s->outdigest) + cipher_keylength(s->incipher) + cipher_keylength(s->outcipher);
220
221         s->key = realloc(s->key, keylen);
222         if(!s->key)
223                 return error(s, errno, strerror(errno));
224
225         // Create the HMAC seed, which is "key expansion" + session label + server nonce + client nonce
226         char seed[s->labellen + 64 + 13];
227         strcpy(seed, "key expansion");
228         if(s->initiator) {
229                 memcpy(seed + 13, s->mykex + 1, 32);
230                 memcpy(seed + 45, s->hiskex + 1, 32);
231         } else {
232                 memcpy(seed + 13, s->hiskex + 1, 32);
233                 memcpy(seed + 45, s->mykex + 1, 32);
234         }
235         memcpy(seed + 77, s->label, s->labellen);
236
237         // Use PRF to generate the key material
238         if(!prf(shared, len, seed, s->labellen + 64 + 13, s->key, keylen))
239                 return false;
240
241         return true;
242 }
243
244 // Send an ACKnowledgement record.
245 static bool send_ack(sptps_t *s) {
246         return send_record_priv(s, SPTPS_HANDSHAKE, "", 0);
247 }
248
249 // Receive an ACKnowledgement record.
250 static bool receive_ack(sptps_t *s, const char *data, uint16_t len) {
251         if(len)
252                 return error(s, EIO, "Invalid ACK record length");
253
254         if(s->initiator) {
255                 bool result
256                         = cipher_set_counter_key(s->incipher, s->key)
257                         && digest_set_key(s->indigest, s->key + cipher_keylength(s->incipher), digest_keylength(s->indigest));
258                 if(!result)
259                         return false;
260         } else {
261                 bool result
262                         = cipher_set_counter_key(s->incipher, s->key + cipher_keylength(s->outcipher) + digest_keylength(s->outdigest))
263                         && digest_set_key(s->indigest, s->key + cipher_keylength(s->outcipher) + digest_keylength(s->outdigest) + cipher_keylength(s->incipher), digest_keylength(s->indigest));
264                 if(!result)
265                         return false;
266         }
267
268         free(s->key);
269         s->key = NULL;
270         s->instate = true;
271
272         return true;
273 }
274
275 // Receive a Key EXchange record, respond by sending a SIG record.
276 static bool receive_kex(sptps_t *s, const char *data, uint16_t len) {
277         // Verify length of the HELLO record
278         if(len != 1 + 32 + ECDH_SIZE)
279                 return error(s, EIO, "Invalid KEX record length");
280
281         // Ignore version number for now.
282
283         // Make a copy of the KEX message, send_sig() and receive_sig() need it
284         if(s->hiskex)
285                 abort();
286         s->hiskex = realloc(s->hiskex, len);
287         if(!s->hiskex)
288                 return error(s, errno, strerror(errno));
289
290         memcpy(s->hiskex, data, len);
291
292         return send_sig(s);
293 }
294
295 // Receive a SIGnature record, verify it, if it passed, compute the shared secret and calculate the session keys.
296 static bool receive_sig(sptps_t *s, const char *data, uint16_t len) {
297         size_t keylen = ECDH_SIZE;
298         size_t siglen = ecdsa_size(s->hiskey);
299
300         // Verify length of KEX record.
301         if(len != siglen)
302                 return error(s, EIO, "Invalid KEX record length");
303
304         // Concatenate both KEX messages, plus tag indicating if it is from the connection originator
305         char msg[(1 + 32 + keylen) * 2 + 1 + s->labellen];
306
307         msg[0] = !s->initiator;
308         memcpy(msg + 1, s->hiskex, 1 + 32 + keylen);
309         memcpy(msg + 1 + 33 + keylen, s->mykex, 1 + 32 + keylen);
310         memcpy(msg + 1 + 2 * (33 + keylen), s->label, s->labellen);
311
312         // Verify signature.
313         if(!ecdsa_verify(s->hiskey, msg, sizeof msg, data))
314                 return false;
315
316         // Compute shared secret.
317         char shared[ECDH_SHARED_SIZE];
318         if(!ecdh_compute_shared(s->ecdh, s->hiskex + 1 + 32, shared))
319                 return false;
320
321         // Generate key material from shared secret.
322         if(!generate_key_material(s, shared, sizeof shared))
323                 return false;
324
325         free(s->mykex);
326         free(s->hiskex);
327
328         s->mykex = NULL;
329         s->hiskex = NULL;
330
331         // Send cipher change record
332         if(s->outstate && !send_ack(s))
333                 return false;
334
335         // TODO: only set new keys after ACK has been set/received
336         if(s->initiator) {
337                 bool result
338                         = cipher_set_counter_key(s->outcipher, s->key + cipher_keylength(s->incipher) + digest_keylength(s->indigest))
339                         && digest_set_key(s->outdigest, s->key + cipher_keylength(s->incipher) + digest_keylength(s->indigest) + cipher_keylength(s->outcipher), digest_keylength(s->outdigest));
340                 if(!result)
341                         return false;
342         } else {
343                 bool result
344                         =  cipher_set_counter_key(s->outcipher, s->key)
345                         && digest_set_key(s->outdigest, s->key + cipher_keylength(s->outcipher), digest_keylength(s->outdigest));
346                 if(!result)
347                         return false;
348         }
349
350         return true;
351 }
352
353 // Force another Key EXchange (for testing purposes).
354 bool sptps_force_kex(sptps_t *s) {
355         if(!s->outstate || s->state != SPTPS_SECONDARY_KEX)
356                 return error(s, EINVAL, "Cannot force KEX in current state");
357
358         s->state = SPTPS_KEX;
359         return send_kex(s);
360 }
361
362 // Receive a handshake record.
363 static bool receive_handshake(sptps_t *s, const char *data, uint16_t len) {
364         // Only a few states to deal with handshaking.
365         switch(s->state) {
366                 case SPTPS_SECONDARY_KEX:
367                         // We receive a secondary KEX request, first respond by sending our own.
368                         if(!send_kex(s))
369                                 return false;
370                 case SPTPS_KEX:
371                         // We have sent our KEX request, we expect our peer to sent one as well.
372                         if(!receive_kex(s, data, len))
373                                 return false;
374                         s->state = SPTPS_SIG;
375                         return true;
376                 case SPTPS_SIG:
377                         // If we already sent our secondary public ECDH key, we expect the peer to send his.
378                         if(!receive_sig(s, data, len))
379                                 return false;
380                         if(s->outstate)
381                                 s->state = SPTPS_ACK;
382                         else {
383                                 s->outstate = true;
384                                 if(!receive_ack(s, NULL, 0))
385                                         return false;
386                                 s->receive_record(s->handle, SPTPS_HANDSHAKE, NULL, 0);
387                                 s->state = SPTPS_SECONDARY_KEX;
388                         }
389
390                         return true;
391                 case SPTPS_ACK:
392                         // We expect a handshake message to indicate transition to the new keys.
393                         if(!receive_ack(s, data, len))
394                                 return false;
395                         s->receive_record(s->handle, SPTPS_HANDSHAKE, NULL, 0);
396                         s->state = SPTPS_SECONDARY_KEX;
397                         return true;
398                 // TODO: split ACK into a VERify and ACK?
399                 default:
400                         return error(s, EIO, "Invalid session state");
401         }
402 }
403
404 // Check datagram for valid HMAC
405 bool sptps_verify_datagram(sptps_t *s, const char *data, size_t len) {
406         if(!s->instate || len < 21)
407                 return false;
408
409         char buffer[len + 23];
410         uint16_t netlen = htons(len - 21);
411
412         memcpy(buffer, &netlen, 2);
413         memcpy(buffer + 2, data, len);
414
415         return digest_verify(s->indigest, buffer, len - 14, buffer + len - 14);
416 }
417
418 // Receive incoming data, datagram version.
419 static bool sptps_receive_data_datagram(sptps_t *s, const char *data, size_t len) {
420         if(len < (s->instate ? 21 : 5))
421                 return error(s, EIO, "Received short packet");
422
423         uint32_t seqno;
424         memcpy(&seqno, data, 4);
425         seqno = ntohl(seqno);
426
427         if(!s->instate) {
428                 if(seqno != s->inseqno)
429                         return error(s, EIO, "Invalid packet seqno: %d != %d", seqno, s->inseqno);
430
431                 s->inseqno = seqno + 1;
432
433                 uint8_t type = data[4];
434
435                 if(type != SPTPS_HANDSHAKE)
436                         return error(s, EIO, "Application record received before handshake finished");
437
438                 return receive_handshake(s, data + 5, len - 5);
439         }
440
441         // Check HMAC.
442         uint16_t netlen = htons(len - 21);
443
444         char buffer[len + 23];
445
446         memcpy(buffer, &netlen, 2);
447         memcpy(buffer + 2, data, len);
448
449         if(!digest_verify(s->indigest, buffer, len - 14, buffer + len - 14))
450                 return error(s, EIO, "Invalid HMAC");
451
452         // Replay protection using a sliding window of configurable size.
453         // s->inseqno is expected sequence number
454         // seqno is received sequence number
455         // s->late[] is a circular buffer, a 1 bit means a packet has not been received yet
456         // The circular buffer contains bits for sequence numbers from s->inseqno - s->replaywin * 8 to (but excluding) s->inseqno.
457         if(s->replaywin) {
458                 if(seqno != s->inseqno) {
459                         if(seqno >= s->inseqno + s->replaywin * 8) {
460                                 // Prevent packets that jump far ahead of the queue from causing many others to be dropped.
461                                 if(s->farfuture++ < s->replaywin >> 2)
462                                         return error(s, EIO, "Packet is %d seqs in the future, dropped (%u)\n", seqno - s->inseqno, s->farfuture);
463
464                                 // Unless we have seen lots of them, in which case we consider the others lost.
465                                 warning(s, "Lost %d packets\n", seqno - s->inseqno);
466                                 memset(s->late, 0, s->replaywin);
467                         } else if (seqno < s->inseqno) {
468                                 // If the sequence number is farther in the past than the bitmap goes, or if the packet was already received, drop it.
469                                 if((s->inseqno >= s->replaywin * 8 && seqno < s->inseqno - s->replaywin * 8) || !(s->late[(seqno / 8) % s->replaywin] & (1 << seqno % 8)))
470                                         return error(s, EIO, "Received late or replayed packet, seqno %d, last received %d\n", seqno, s->inseqno);
471                         } else {
472                                 // We missed some packets. Mark them in the bitmap as being late.
473                                 for(int i = s->inseqno; i < seqno; i++)
474                                         s->late[(i / 8) % s->replaywin] |= 1 << i % 8;
475                         }
476                 }
477
478                 // Mark the current packet as not being late.
479                 s->late[(seqno / 8) % s->replaywin] &= ~(1 << seqno % 8);
480                 s->farfuture = 0;
481         }
482
483         if(seqno > s->inseqno)
484                 s->inseqno = seqno + 1;
485
486         if(!s->inseqno)
487                 s->received = 0;
488         else
489                 s->received++;
490
491         // Decrypt.
492         memcpy(&seqno, buffer + 2, 4);
493         cipher_set_counter(s->incipher, &seqno, sizeof seqno);
494         if(!cipher_counter_xor(s->incipher, buffer + 6, len - 4, buffer + 6))
495                 return false;
496
497         // Append a NULL byte for safety.
498         buffer[len - 14] = 0;
499
500         uint8_t type = buffer[6];
501
502         if(type < SPTPS_HANDSHAKE) {
503                 if(!s->instate)
504                         return error(s, EIO, "Application record received before handshake finished");
505                 if(!s->receive_record(s->handle, type, buffer + 7, len - 21))
506                         return false;
507         } else if(type == SPTPS_HANDSHAKE) {
508                 if(!receive_handshake(s, buffer + 7, len - 21))
509                         return false;
510         } else {
511                 return error(s, EIO, "Invalid record type");
512         }
513
514         return true;
515 }
516
517 // Receive incoming data. Check if it contains a complete record, if so, handle it.
518 bool sptps_receive_data(sptps_t *s, const char *data, size_t len) {
519         if(s->datagram)
520                 return sptps_receive_data_datagram(s, data, len);
521
522         while(len) {
523                 // First read the 2 length bytes.
524                 if(s->buflen < 6) {
525                         size_t toread = 6 - s->buflen;
526                         if(toread > len)
527                                 toread = len;
528
529                         memcpy(s->inbuf + s->buflen, data, toread);
530
531                         s->buflen += toread;
532                         len -= toread;
533                         data += toread;
534
535                         // Exit early if we don't have the full length.
536                         if(s->buflen < 6)
537                                 return true;
538
539                         // Decrypt the length bytes
540
541                         if(s->instate) {
542                                 if(!cipher_counter_xor(s->incipher, s->inbuf + 4, 2, &s->reclen))
543                                         return false;
544                         } else {
545                                 memcpy(&s->reclen, s->inbuf + 4, 2);
546                         }
547
548                         s->reclen = ntohs(s->reclen);
549
550                         // If we have the length bytes, ensure our buffer can hold the whole request.
551                         s->inbuf = realloc(s->inbuf, s->reclen + 23UL);
552                         if(!s->inbuf)
553                                 return error(s, errno, strerror(errno));
554
555                         // Add sequence number.
556                         uint32_t seqno = htonl(s->inseqno++);
557                         memcpy(s->inbuf, &seqno, 4);
558
559                         // Exit early if we have no more data to process.
560                         if(!len)
561                                 return true;
562                 }
563
564                 // Read up to the end of the record.
565                 size_t toread = s->reclen + (s->instate ? 23UL : 7UL) - s->buflen;
566                 if(toread > len)
567                         toread = len;
568
569                 memcpy(s->inbuf + s->buflen, data, toread);
570                 s->buflen += toread;
571                 len -= toread;
572                 data += toread;
573
574                 // If we don't have a whole record, exit.
575                 if(s->buflen < s->reclen + (s->instate ? 23UL : 7UL))
576                         return true;
577
578                 // Check HMAC and decrypt.
579                 if(s->instate) {
580                         if(!digest_verify(s->indigest, s->inbuf, s->reclen + 7UL, s->inbuf + s->reclen + 7UL))
581                                 return error(s, EIO, "Invalid HMAC");
582
583                         if(!cipher_counter_xor(s->incipher, s->inbuf + 6UL, s->reclen + 1UL, s->inbuf + 6UL))
584                                 return false;
585                 }
586
587                 // Append a NULL byte for safety.
588                 s->inbuf[s->reclen + 7UL] = 0;
589
590                 uint8_t type = s->inbuf[6];
591
592                 if(type < SPTPS_HANDSHAKE) {
593                         if(!s->instate)
594                                 return error(s, EIO, "Application record received before handshake finished");
595                         if(!s->receive_record(s->handle, type, s->inbuf + 7, s->reclen))
596                                 return false;
597                 } else if(type == SPTPS_HANDSHAKE) {
598                         if(!receive_handshake(s, s->inbuf + 7, s->reclen))
599                                 return false;
600                 } else {
601                         return error(s, EIO, "Invalid record type");
602                 }
603
604                 s->buflen = 4;
605         }
606
607         return true;
608 }
609
610 // Start a SPTPS session.
611 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) {
612         // Initialise struct sptps
613         memset(s, 0, sizeof *s);
614
615         s->handle = handle;
616         s->initiator = initiator;
617         s->datagram = datagram;
618         s->mykey = mykey;
619         s->hiskey = hiskey;
620         s->replaywin = sptps_replaywin;
621         if(s->replaywin) {
622                 s->late = malloc(s->replaywin);
623                 if(!s->late)
624                         return error(s, errno, strerror(errno));
625         }
626
627         s->label = malloc(labellen);
628         if(!s->label)
629                 return error(s, errno, strerror(errno));
630
631         if(!datagram) {
632                 s->inbuf = malloc(7);
633                 if(!s->inbuf)
634                         return error(s, errno, strerror(errno));
635                 s->buflen = 4;
636                 memset(s->inbuf, 0, 4);
637         }
638
639         memcpy(s->label, label, labellen);
640         s->labellen = labellen;
641
642         s->send_data = send_data;
643         s->receive_record = receive_record;
644
645         // Do first KEX immediately
646         s->state = SPTPS_KEX;
647         return send_kex(s);
648 }
649
650 // Stop a SPTPS session.
651 bool sptps_stop(sptps_t *s) {
652         // Clean up any resources.
653         cipher_close(s->incipher);
654         cipher_close(s->outcipher);
655         digest_close(s->indigest);
656         digest_close(s->outdigest);
657         ecdh_free(s->ecdh);
658         free(s->inbuf);
659         free(s->mykex);
660         free(s->hiskex);
661         free(s->key);
662         free(s->label);
663         free(s->late);
664         memset(s, 0, sizeof *s);
665         return true;
666 }