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