Fix datagram SPTPS.
[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(!ecdh_generate_public(&s->ecdh, 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                 bool result
211                         =  cipher_open_by_name(&s->incipher, "aes-256-ecb")
212                         && cipher_open_by_name(&s->outcipher, "aes-256-ecb")
213                         && digest_open_by_name(&s->indigest, "sha256", 16)
214                         && digest_open_by_name(&s->outdigest, "sha256", 16);
215                 if(!result)
216                         return false;
217         }
218
219         // Allocate memory for key material
220         size_t keylen = digest_keylength(&s->indigest) + digest_keylength(&s->outdigest) + cipher_keylength(&s->incipher) + cipher_keylength(&s->outcipher);
221
222         s->key = realloc(s->key, keylen);
223         if(!s->key)
224                 return error(s, errno, strerror(errno));
225
226         // Create the HMAC seed, which is "key expansion" + session label + server nonce + client nonce
227         char seed[s->labellen + 64 + 13];
228         strcpy(seed, "key expansion");
229         if(s->initiator) {
230                 memcpy(seed + 13, s->mykex + 1, 32);
231                 memcpy(seed + 45, s->hiskex + 1, 32);
232         } else {
233                 memcpy(seed + 13, s->hiskex + 1, 32);
234                 memcpy(seed + 45, s->mykex + 1, 32);
235         }
236         memcpy(seed + 77, s->label, s->labellen);
237
238         // Use PRF to generate the key material
239         if(!prf(shared, len, seed, s->labellen + 64 + 13, s->key, keylen))
240                 return false;
241
242         return true;
243 }
244
245 // Send an ACKnowledgement record.
246 static bool send_ack(sptps_t *s) {
247         return send_record_priv(s, SPTPS_HANDSHAKE, "", 0);
248 }
249
250 // Receive an ACKnowledgement record.
251 static bool receive_ack(sptps_t *s, const char *data, uint16_t len) {
252         if(len)
253                 return error(s, EIO, "Invalid ACK record length");
254
255         if(s->initiator) {
256                 bool result
257                         = cipher_set_counter_key(&s->incipher, s->key)
258                         && digest_set_key(&s->indigest, s->key + cipher_keylength(&s->incipher), digest_keylength(&s->indigest));
259                 if(!result)
260                         return false;
261         } else {
262                 bool result
263                         = cipher_set_counter_key(&s->incipher, s->key + cipher_keylength(&s->outcipher) + digest_keylength(&s->outdigest))
264                         && digest_set_key(&s->indigest, s->key + cipher_keylength(&s->outcipher) + digest_keylength(&s->outdigest) + cipher_keylength(&s->incipher), digest_keylength(&s->indigest));
265                 if(!result)
266                         return false;
267         }
268
269         free(s->key);
270         s->key = NULL;
271         s->instate = true;
272
273         return true;
274 }
275
276 // Receive a Key EXchange record, respond by sending a SIG record.
277 static bool receive_kex(sptps_t *s, const char *data, uint16_t len) {
278         // Verify length of the HELLO record
279         if(len != 1 + 32 + ECDH_SIZE)
280                 return error(s, EIO, "Invalid KEX record length");
281
282         // Ignore version number for now.
283
284         // Make a copy of the KEX message, send_sig() and receive_sig() need it
285         if(s->hiskex)
286                 abort();
287         s->hiskex = realloc(s->hiskex, len);
288         if(!s->hiskex)
289                 return error(s, errno, strerror(errno));
290
291         memcpy(s->hiskex, data, len);
292
293         return send_sig(s);
294 }
295
296 // Receive a SIGnature record, verify it, if it passed, compute the shared secret and calculate the session keys.
297 static bool receive_sig(sptps_t *s, const char *data, uint16_t len) {
298         size_t keylen = ECDH_SIZE;
299         size_t siglen = ecdsa_size(&s->hiskey);
300
301         // Verify length of KEX record.
302         if(len != siglen)
303                 return error(s, EIO, "Invalid KEX record length");
304
305         // Concatenate both KEX messages, plus tag indicating if it is from the connection originator
306         char msg[(1 + 32 + keylen) * 2 + 1 + s->labellen];
307
308         msg[0] = !s->initiator;
309         memcpy(msg + 1, s->hiskex, 1 + 32 + keylen);
310         memcpy(msg + 1 + 33 + keylen, s->mykex, 1 + 32 + keylen);
311         memcpy(msg + 1 + 2 * (33 + keylen), s->label, s->labellen);
312
313         // Verify signature.
314         if(!ecdsa_verify(&s->hiskey, msg, sizeof msg, data))
315                 return false;
316
317         // Compute shared secret.
318         char shared[ECDH_SHARED_SIZE];
319         if(!ecdh_compute_shared(&s->ecdh, s->hiskex + 1 + 32, shared))
320                 return false;
321
322         // Generate key material from shared secret.
323         if(!generate_key_material(s, shared, sizeof shared))
324                 return false;
325
326         free(s->mykex);
327         free(s->hiskex);
328
329         s->mykex = NULL;
330         s->hiskex = NULL;
331
332         // Send cipher change record
333         if(s->outstate && !send_ack(s))
334                 return false;
335
336         // TODO: only set new keys after ACK has been set/received
337         if(s->initiator) {
338                 bool result
339                         = cipher_set_counter_key(&s->outcipher, s->key + cipher_keylength(&s->incipher) + digest_keylength(&s->indigest))
340                         && digest_set_key(&s->outdigest, s->key + cipher_keylength(&s->incipher) + digest_keylength(&s->indigest) + cipher_keylength(&s->outcipher), digest_keylength(&s->outdigest));
341                 if(!result)
342                         return false;
343         } else {
344                 bool result
345                         =  cipher_set_counter_key(&s->outcipher, s->key)
346                         && digest_set_key(&s->outdigest, s->key + cipher_keylength(&s->outcipher), digest_keylength(&s->outdigest));
347                 if(!result)
348                         return false;
349         }
350
351         return true;
352 }
353
354 // Force another Key EXchange (for testing purposes).
355 bool sptps_force_kex(sptps_t *s) {
356         if(!s->outstate || s->state != SPTPS_SECONDARY_KEX)
357                 return error(s, EINVAL, "Cannot force KEX in current state");
358
359         s->state = SPTPS_KEX;
360         return send_kex(s);
361 }
362
363 // Receive a handshake record.
364 static bool receive_handshake(sptps_t *s, const char *data, uint16_t len) {
365         // Only a few states to deal with handshaking.
366         switch(s->state) {
367                 case SPTPS_SECONDARY_KEX:
368                         // We receive a secondary KEX request, first respond by sending our own.
369                         if(!send_kex(s))
370                                 return false;
371                 case SPTPS_KEX:
372                         // We have sent our KEX request, we expect our peer to sent one as well.
373                         if(!receive_kex(s, data, len))
374                                 return false;
375                         s->state = SPTPS_SIG;
376                         return true;
377                 case SPTPS_SIG:
378                         // If we already sent our secondary public ECDH key, we expect the peer to send his.
379                         if(!receive_sig(s, data, len))
380                                 return false;
381                         if(s->outstate)
382                                 s->state = SPTPS_ACK;
383                         else {
384                                 s->outstate = true;
385                                 if(!receive_ack(s, NULL, 0))
386                                         return false;
387                                 s->receive_record(s->handle, SPTPS_HANDSHAKE, NULL, 0);
388                                 s->state = SPTPS_SECONDARY_KEX;
389                         }
390
391                         return true;
392                 case SPTPS_ACK:
393                         // We expect a handshake message to indicate transition to the new keys.
394                         if(!receive_ack(s, data, len))
395                                 return false;
396                         s->receive_record(s->handle, SPTPS_HANDSHAKE, NULL, 0);
397                         s->state = SPTPS_SECONDARY_KEX;
398                         return true;
399                 // TODO: split ACK into a VERify and ACK?
400                 default:
401                         return error(s, EIO, "Invalid session state");
402         }
403 }
404
405 // Check datagram for valid HMAC
406 bool sptps_verify_datagram(sptps_t *s, const char *data, size_t len) {
407         if(!s->instate || len < 21)
408                 return false;
409
410         char buffer[len + 23];
411         uint16_t netlen = htons(len - 21);
412
413         memcpy(buffer, &netlen, 2);
414         memcpy(buffer + 2, data, len);
415
416         return digest_verify(&s->indigest, buffer, len - 14, buffer + len - 14);
417 }
418
419 // Receive incoming data, datagram version.
420 static bool sptps_receive_data_datagram(sptps_t *s, const char *data, size_t len) {
421         if(len < (s->instate ? 21 : 5))
422                 return error(s, EIO, "Received short packet");
423
424         uint32_t seqno;
425         memcpy(&seqno, data, 4);
426         seqno = ntohl(seqno);
427
428         if(!s->instate) {
429                 if(seqno != s->inseqno)
430                         return error(s, EIO, "Invalid packet seqno: %d != %d", seqno, s->inseqno);
431
432                 s->inseqno = seqno + 1;
433
434                 uint8_t type = data[4];
435
436                 if(type != SPTPS_HANDSHAKE)
437                         return error(s, EIO, "Application record received before handshake finished");
438
439                 return receive_handshake(s, data + 5, len - 5);
440         }
441
442         // Check HMAC.
443         uint16_t netlen = htons(len - 21);
444
445         char buffer[len + 23];
446
447         memcpy(buffer, &netlen, 2);
448         memcpy(buffer + 2, data, len);
449
450         if(!digest_verify(&s->indigest, buffer, len - 14, buffer + len - 14))
451                 return error(s, EIO, "Invalid HMAC");
452
453         // Replay protection using a sliding window of configurable size.
454         // s->inseqno is expected sequence number
455         // seqno is received sequence number
456         // s->late[] is a circular buffer, a 1 bit means a packet has not been received yet
457         // The circular buffer contains bits for sequence numbers from s->inseqno - s->replaywin * 8 to (but excluding) s->inseqno.
458         if(s->replaywin) {
459                 if(seqno != s->inseqno) {
460                         if(seqno >= s->inseqno + s->replaywin * 8) {
461                                 // Prevent packets that jump far ahead of the queue from causing many others to be dropped.
462                                 if(s->farfuture++ < s->replaywin >> 2)
463                                         return error(s, EIO, "Packet is %d seqs in the future, dropped (%u)\n", seqno - s->inseqno, s->farfuture);
464
465                                 // Unless we have seen lots of them, in which case we consider the others lost.
466                                 warning(s, "Lost %d packets\n", seqno - s->inseqno);
467                                 memset(s->late, 0, s->replaywin);
468                         } else if (seqno < s->inseqno) {
469                                 // If the sequence number is farther in the past than the bitmap goes, or if the packet was already received, drop it.
470                                 if((s->inseqno >= s->replaywin * 8 && seqno < s->inseqno - s->replaywin * 8) || !(s->late[(seqno / 8) % s->replaywin] & (1 << seqno % 8)))
471                                         return error(s, EIO, "Received late or replayed packet, seqno %d, last received %d\n", seqno, s->inseqno);
472                         } else {
473                                 // We missed some packets. Mark them in the bitmap as being late.
474                                 for(int i = s->inseqno; i < seqno; i++)
475                                         s->late[(i / 8) % s->replaywin] |= 1 << i % 8;
476                         }
477                 }
478
479                 // Mark the current packet as not being late.
480                 s->late[(seqno / 8) % s->replaywin] &= ~(1 << seqno % 8);
481                 s->farfuture = 0;
482         }
483
484         if(seqno > s->inseqno)
485                 s->inseqno = seqno + 1;
486
487         if(!s->inseqno)
488                 s->received = 0;
489         else
490                 s->received++;
491
492         // Decrypt.
493         memcpy(&seqno, buffer + 2, 4);
494         cipher_set_counter(&s->incipher, &seqno, sizeof seqno);
495         if(!cipher_counter_xor(&s->incipher, buffer + 6, len - 4, buffer + 6))
496                 return false;
497
498         // Append a NULL byte for safety.
499         buffer[len - 14] = 0;
500
501         uint8_t type = buffer[6];
502
503         if(type < SPTPS_HANDSHAKE) {
504                 if(!s->instate)
505                         return error(s, EIO, "Application record received before handshake finished");
506                 if(!s->receive_record(s->handle, type, buffer + 7, len - 21))
507                         return false;
508         } else if(type == SPTPS_HANDSHAKE) {
509                 if(!receive_handshake(s, buffer + 7, len - 21))
510                         return false;
511         } else {
512                 return error(s, EIO, "Invalid record type");
513         }
514
515         return true;
516 }
517
518 // Receive incoming data. Check if it contains a complete record, if so, handle it.
519 bool sptps_receive_data(sptps_t *s, const char *data, size_t len) {
520         if(s->datagram)
521                 return sptps_receive_data_datagram(s, data, len);
522
523         while(len) {
524                 // First read the 2 length bytes.
525                 if(s->buflen < 6) {
526                         size_t toread = 6 - s->buflen;
527                         if(toread > len)
528                                 toread = len;
529
530                         memcpy(s->inbuf + s->buflen, data, toread);
531
532                         s->buflen += toread;
533                         len -= toread;
534                         data += toread;
535
536                         // Exit early if we don't have the full length.
537                         if(s->buflen < 6)
538                                 return true;
539
540                         // Decrypt the length bytes
541
542                         if(s->instate) {
543                                 if(!cipher_counter_xor(&s->incipher, s->inbuf + 4, 2, &s->reclen))
544                                         return false;
545                         } else {
546                                 memcpy(&s->reclen, s->inbuf + 4, 2);
547                         }
548
549                         s->reclen = ntohs(s->reclen);
550
551                         // If we have the length bytes, ensure our buffer can hold the whole request.
552                         s->inbuf = realloc(s->inbuf, s->reclen + 23UL);
553                         if(!s->inbuf)
554                                 return error(s, errno, strerror(errno));
555
556                         // Add sequence number.
557                         uint32_t seqno = htonl(s->inseqno++);
558                         memcpy(s->inbuf, &seqno, 4);
559
560                         // Exit early if we have no more data to process.
561                         if(!len)
562                                 return true;
563                 }
564
565                 // Read up to the end of the record.
566                 size_t toread = s->reclen + (s->instate ? 23UL : 7UL) - s->buflen;
567                 if(toread > len)
568                         toread = len;
569
570                 memcpy(s->inbuf + s->buflen, data, toread);
571                 s->buflen += toread;
572                 len -= toread;
573                 data += toread;
574
575                 // If we don't have a whole record, exit.
576                 if(s->buflen < s->reclen + (s->instate ? 23UL : 7UL))
577                         return true;
578
579                 // Check HMAC and decrypt.
580                 if(s->instate) {
581                         if(!digest_verify(&s->indigest, s->inbuf, s->reclen + 7UL, s->inbuf + s->reclen + 7UL))
582                                 return error(s, EIO, "Invalid HMAC");
583
584                         if(!cipher_counter_xor(&s->incipher, s->inbuf + 6UL, s->reclen + 1UL, s->inbuf + 6UL))
585                                 return false;
586                 }
587
588                 // Append a NULL byte for safety.
589                 s->inbuf[s->reclen + 7UL] = 0;
590
591                 uint8_t type = s->inbuf[6];
592
593                 if(type < SPTPS_HANDSHAKE) {
594                         if(!s->instate)
595                                 return error(s, EIO, "Application record received before handshake finished");
596                         if(!s->receive_record(s->handle, type, s->inbuf + 7, s->reclen))
597                                 return false;
598                 } else if(type == SPTPS_HANDSHAKE) {
599                         if(!receive_handshake(s, s->inbuf + 7, s->reclen))
600                                 return false;
601                 } else {
602                         return error(s, EIO, "Invalid record type");
603                 }
604
605                 s->buflen = 4;
606         }
607
608         return true;
609 }
610
611 // Start a SPTPS session.
612 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) {
613         // Initialise struct sptps
614         memset(s, 0, sizeof *s);
615
616         s->handle = handle;
617         s->initiator = initiator;
618         s->datagram = datagram;
619         s->mykey = mykey;
620         s->hiskey = hiskey;
621         s->replaywin = sptps_replaywin;
622         if(s->replaywin) {
623                 s->late = malloc(s->replaywin);
624                 if(!s->late)
625                         return error(s, errno, strerror(errno));
626         }
627
628         s->label = malloc(labellen);
629         if(!s->label)
630                 return error(s, errno, strerror(errno));
631
632         if(!datagram) {
633                 s->inbuf = malloc(7);
634                 if(!s->inbuf)
635                         return error(s, errno, strerror(errno));
636                 s->buflen = 4;
637                 memset(s->inbuf, 0, 4);
638         }
639
640         memcpy(s->label, label, labellen);
641         s->labellen = labellen;
642
643         s->send_data = send_data;
644         s->receive_record = receive_record;
645
646         // Do first KEX immediately
647         s->state = SPTPS_KEX;
648         return send_kex(s);
649 }
650
651 // Stop a SPTPS session.
652 bool sptps_stop(sptps_t *s) {
653         // Clean up any resources.
654         cipher_close(&s->incipher);
655         cipher_close(&s->outcipher);
656         digest_close(&s->indigest);
657         digest_close(&s->outdigest);
658         ecdh_free(&s->ecdh);
659         free(s->inbuf);
660         free(s->mykex);
661         free(s->hiskex);
662         free(s->key);
663         free(s->label);
664         free(s->late);
665         memset(s, 0, sizeof *s);
666         return true;
667 }