Tweak the check that a ciphersuite has not changed since the HRR
[openssl.git] / ssl / tls13_enc.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdlib.h>
11 #include "ssl_locl.h"
12 #include <openssl/evp.h>
13 #include <openssl/kdf.h>
14
15 #define TLS13_MAX_LABEL_LEN     246
16
17 /* Always filled with zeros */
18 static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
19
20 /*
21  * Given a |secret|; a |label| of length |labellen|; and a |hash| of the
22  * handshake messages, derive a new secret |outlen| bytes long and store it in
23  * the location pointed to be |out|. The |hash| value may be NULL. Returns 1 on
24  * success  0 on failure.
25  */
26 int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
27                              const unsigned char *label, size_t labellen,
28                              const unsigned char *hash,
29                              unsigned char *out, size_t outlen)
30 {
31     const unsigned char label_prefix[] = "tls13 ";
32     EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
33     int ret;
34     size_t hkdflabellen;
35     size_t hashlen;
36     /*
37      * 2 bytes for length of whole HkdfLabel + 1 byte for length of combined
38      * prefix and label + bytes for the label itself + bytes for the hash
39      */
40     unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
41                             + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
42                             + EVP_MAX_MD_SIZE];
43     WPACKET pkt;
44
45     if (pctx == NULL)
46         return 0;
47
48     hashlen = EVP_MD_size(md);
49
50     if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
51             || !WPACKET_put_bytes_u16(&pkt, outlen)
52             || !WPACKET_start_sub_packet_u8(&pkt)
53             || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
54             || !WPACKET_memcpy(&pkt, label, labellen)
55             || !WPACKET_close(&pkt)
56             || !WPACKET_sub_memcpy_u8(&pkt, hash, (hash == NULL) ? 0 : hashlen)
57             || !WPACKET_get_total_written(&pkt, &hkdflabellen)
58             || !WPACKET_finish(&pkt)) {
59         EVP_PKEY_CTX_free(pctx);
60         WPACKET_cleanup(&pkt);
61         return 0;
62     }
63
64     ret = EVP_PKEY_derive_init(pctx) <= 0
65             || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
66                <= 0
67             || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
68             || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
69             || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
70             || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
71
72     EVP_PKEY_CTX_free(pctx);
73
74     return ret == 0;
75 }
76
77 /*
78  * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
79  * success  0 on failure.
80  */
81 int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
82                      unsigned char *key, size_t keylen)
83 {
84     static const unsigned char keylabel[] = "key";
85
86     return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
87                              NULL, key, keylen);
88 }
89
90 /*
91  * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
92  * success  0 on failure.
93  */
94 int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
95                     unsigned char *iv, size_t ivlen)
96 {
97     static const unsigned char ivlabel[] = "iv";
98
99     return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
100                              NULL, iv, ivlen);
101 }
102
103 int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
104                              const unsigned char *secret,
105                              unsigned char *fin, size_t finlen)
106 {
107     static const unsigned char finishedlabel[] = "finished";
108
109     return tls13_hkdf_expand(s, md, secret, finishedlabel,
110                              sizeof(finishedlabel) - 1, NULL, fin, finlen);
111 }
112
113 /*
114  * Given the previous secret |prevsecret| and a new input secret |insecret| of
115  * length |insecretlen|, generate a new secret and store it in the location
116  * pointed to by |outsecret|. Returns 1 on success  0 on failure.
117  */
118 int tls13_generate_secret(SSL *s, const EVP_MD *md,
119                           const unsigned char *prevsecret,
120                           const unsigned char *insecret,
121                           size_t insecretlen,
122                           unsigned char *outsecret)
123 {
124     size_t mdlen, prevsecretlen;
125     int ret;
126     EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
127     static const char derived_secret_label[] = "derived";
128     unsigned char preextractsec[EVP_MAX_MD_SIZE];
129
130     if (pctx == NULL)
131         return 0;
132
133     mdlen = EVP_MD_size(md);
134
135     if (insecret == NULL) {
136         insecret = default_zeros;
137         insecretlen = mdlen;
138     }
139     if (prevsecret == NULL) {
140         prevsecret = default_zeros;
141         prevsecretlen = 0;
142     } else {
143         EVP_MD_CTX *mctx = EVP_MD_CTX_new();
144         unsigned char hash[EVP_MAX_MD_SIZE];
145
146         /* The pre-extract derive step uses a hash of no messages */
147         if (mctx == NULL
148                 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
149                 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
150             EVP_MD_CTX_free(mctx);
151             EVP_PKEY_CTX_free(pctx);
152             return 0;
153         }
154         EVP_MD_CTX_free(mctx);
155
156         /* Generate the pre-extract secret */
157         if (!tls13_hkdf_expand(s, md, prevsecret,
158                                (unsigned char *)derived_secret_label,
159                                sizeof(derived_secret_label) - 1, hash,
160                                preextractsec, mdlen)) {
161             EVP_PKEY_CTX_free(pctx);
162             return 0;
163         }
164
165         prevsecret = preextractsec;
166         prevsecretlen = mdlen;
167     }
168
169     ret = EVP_PKEY_derive_init(pctx) <= 0
170             || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
171                <= 0
172             || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
173             || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
174             || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
175                <= 0
176             || EVP_PKEY_derive(pctx, outsecret, &mdlen)
177                <= 0;
178
179     EVP_PKEY_CTX_free(pctx);
180     if (prevsecret == preextractsec)
181         OPENSSL_cleanse(preextractsec, mdlen);
182     return ret == 0;
183 }
184
185 /*
186  * Given an input secret |insecret| of length |insecretlen| generate the
187  * handshake secret. This requires the early secret to already have been
188  * generated. Returns 1 on success  0 on failure.
189  */
190 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
191                                 size_t insecretlen)
192 {
193     return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
194                                  insecret, insecretlen,
195                                  (unsigned char *)&s->handshake_secret);
196 }
197
198 /*
199  * Given the handshake secret |prev| of length |prevlen| generate the master
200  * secret and store its length in |*secret_size|. Returns 1 on success  0 on
201  * failure.
202  */
203 int tls13_generate_master_secret(SSL *s, unsigned char *out,
204                                  unsigned char *prev, size_t prevlen,
205                                  size_t *secret_size)
206 {
207     const EVP_MD *md = ssl_handshake_md(s);
208
209     *secret_size = EVP_MD_size(md);
210     return tls13_generate_secret(s, md, prev, NULL, 0, out);
211 }
212
213 /*
214  * Generates the mac for the Finished message. Returns the length of the MAC or
215  * 0 on error.
216  */
217 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
218                              unsigned char *out)
219 {
220     const EVP_MD *md = ssl_handshake_md(s);
221     unsigned char hash[EVP_MAX_MD_SIZE];
222     size_t hashlen, ret = 0;
223     EVP_PKEY *key = NULL;
224     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
225
226     if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
227         goto err;
228
229     if (str == s->method->ssl3_enc->server_finished_label)
230         key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
231                                    s->server_finished_secret, hashlen);
232     else
233         key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
234                                    s->client_finished_secret, hashlen);
235
236     if (key == NULL
237             || ctx == NULL
238             || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
239             || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
240             || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0)
241         goto err;
242
243     ret = hashlen;
244  err:
245     EVP_PKEY_free(key);
246     EVP_MD_CTX_free(ctx);
247     return ret;
248 }
249
250 /*
251  * There isn't really a key block in TLSv1.3, but we still need this function
252  * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
253  */
254 int tls13_setup_key_block(SSL *s)
255 {
256     const EVP_CIPHER *c;
257     const EVP_MD *hash;
258     int mac_type = NID_undef;
259
260     s->session->cipher = s->s3->tmp.new_cipher;
261     if (!ssl_cipher_get_evp
262         (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
263         SSLerr(SSL_F_TLS13_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
264         return 0;
265     }
266
267     s->s3->tmp.new_sym_enc = c;
268     s->s3->tmp.new_hash = hash;
269
270     return 1;
271 }
272
273 static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
274                                     const EVP_CIPHER *ciph,
275                                     const unsigned char *insecret,
276                                     const unsigned char *hash,
277                                     const unsigned char *label,
278                                     size_t labellen, unsigned char *secret,
279                                     unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
280 {
281     unsigned char key[EVP_MAX_KEY_LENGTH];
282     size_t ivlen, keylen, taglen;
283     size_t hashlen = EVP_MD_size(md);
284
285     if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, secret,
286                            hashlen)) {
287         SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR);
288         goto err;
289     }
290
291     /* TODO(size_t): convert me */
292     keylen = EVP_CIPHER_key_length(ciph);
293     if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
294         uint32_t algenc;
295
296         ivlen = EVP_CCM_TLS_IV_LEN;
297         if (s->s3->tmp.new_cipher == NULL) {
298             /* We've not selected a cipher yet - we must be doing early data */
299             algenc = s->session->cipher->algorithm_enc;
300         } else {
301             algenc = s->s3->tmp.new_cipher->algorithm_enc;
302         }
303         if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
304             taglen = EVP_CCM8_TLS_TAG_LEN;
305          else
306             taglen = EVP_CCM_TLS_TAG_LEN;
307     } else {
308         ivlen = EVP_CIPHER_iv_length(ciph);
309         taglen = 0;
310     }
311
312     if (!tls13_derive_key(s, md, secret, key, keylen)
313             || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
314         SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR);
315         goto err;
316     }
317
318     if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
319         || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
320         || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
321                                                 taglen, NULL))
322         || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
323         SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_EVP_LIB);
324         goto err;
325     }
326
327     return 1;
328  err:
329     OPENSSL_cleanse(key, sizeof(key));
330     return 0;
331 }
332
333 int tls13_change_cipher_state(SSL *s, int which)
334 {
335     static const unsigned char client_early_traffic[] = "c e traffic";
336     static const unsigned char client_handshake_traffic[] = "c hs traffic";
337     static const unsigned char client_application_traffic[] = "c ap traffic";
338     static const unsigned char server_handshake_traffic[] = "s hs traffic";
339     static const unsigned char server_application_traffic[] = "s ap traffic";
340     static const unsigned char resumption_master_secret[] = "res master";
341     unsigned char *iv;
342     unsigned char secret[EVP_MAX_MD_SIZE];
343     unsigned char hashval[EVP_MAX_MD_SIZE];
344     unsigned char *hash = hashval;
345     unsigned char *insecret;
346     unsigned char *finsecret = NULL;
347     const char *log_label = NULL;
348     EVP_CIPHER_CTX *ciph_ctx;
349     size_t finsecretlen = 0;
350     const unsigned char *label;
351     size_t labellen, hashlen = 0;
352     int ret = 0;
353     const EVP_MD *md = NULL;
354     const EVP_CIPHER *cipher = NULL;
355
356     if (which & SSL3_CC_READ) {
357         if (s->enc_read_ctx != NULL) {
358             EVP_CIPHER_CTX_reset(s->enc_read_ctx);
359         } else {
360             s->enc_read_ctx = EVP_CIPHER_CTX_new();
361             if (s->enc_read_ctx == NULL) {
362                 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
363                 goto err;
364             }
365         }
366         ciph_ctx = s->enc_read_ctx;
367         iv = s->read_iv;
368
369         RECORD_LAYER_reset_read_sequence(&s->rlayer);
370     } else {
371         if (s->enc_write_ctx != NULL) {
372             EVP_CIPHER_CTX_reset(s->enc_write_ctx);
373         } else {
374             s->enc_write_ctx = EVP_CIPHER_CTX_new();
375             if (s->enc_write_ctx == NULL) {
376                 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
377                 goto err;
378             }
379         }
380         ciph_ctx = s->enc_write_ctx;
381         iv = s->write_iv;
382
383         RECORD_LAYER_reset_write_sequence(&s->rlayer);
384     }
385
386     if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
387             || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
388         if (which & SSL3_CC_EARLY) {
389             EVP_MD_CTX *mdctx = NULL;
390             long handlen;
391             void *hdata;
392             unsigned int hashlenui;
393             const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
394
395             insecret = s->early_secret;
396             label = client_early_traffic;
397             labellen = sizeof(client_early_traffic) - 1;
398             log_label = CLIENT_EARLY_LABEL;
399
400             handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
401             if (handlen <= 0) {
402                 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE,
403                        SSL_R_BAD_HANDSHAKE_LENGTH);
404                 goto err;
405             }
406             if (sslcipher == NULL) {
407                 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
408                 goto err;
409             }
410
411             /*
412              * We need to calculate the handshake digest using the digest from
413              * the session. We haven't yet selected our ciphersuite so we can't
414              * use ssl_handshake_md().
415              */
416             mdctx = EVP_MD_CTX_new();
417             if (mdctx == NULL) {
418                 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
419                 goto err;
420             }
421             cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
422             md = ssl_md(sslcipher->algorithm2);
423             if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
424                     || !EVP_DigestUpdate(mdctx, hdata, handlen)
425                     || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
426                 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
427                 EVP_MD_CTX_free(mdctx);
428                 goto err;
429             }
430             hashlen = hashlenui;
431             EVP_MD_CTX_free(mdctx);
432         } else if (which & SSL3_CC_HANDSHAKE) {
433             insecret = s->handshake_secret;
434             finsecret = s->client_finished_secret;
435             finsecretlen = EVP_MD_size(ssl_handshake_md(s));
436             label = client_handshake_traffic;
437             labellen = sizeof(client_handshake_traffic) - 1;
438             log_label = CLIENT_HANDSHAKE_LABEL;
439             /*
440              * The handshake hash used for the server read/client write handshake
441              * traffic secret is the same as the hash for the server
442              * write/client read handshake traffic secret. However, if we
443              * processed early data then we delay changing the server
444              * read/client write cipher state until later, and the handshake
445              * hashes have moved on. Therefore we use the value saved earlier
446              * when we did the server write/client read change cipher state.
447              */
448             hash = s->handshake_traffic_hash;
449         } else {
450             insecret = s->master_secret;
451             label = client_application_traffic;
452             labellen = sizeof(client_application_traffic) - 1;
453             log_label = CLIENT_APPLICATION_LABEL;
454             /*
455              * For this we only use the handshake hashes up until the server
456              * Finished hash. We do not include the client's Finished, which is
457              * what ssl_handshake_hash() would give us. Instead we use the
458              * previously saved value.
459              */
460             hash = s->server_finished_hash;
461         }
462     } else {
463         /* Early data never applies to client-read/server-write */
464         if (which & SSL3_CC_HANDSHAKE) {
465             insecret = s->handshake_secret;
466             finsecret = s->server_finished_secret;
467             finsecretlen = EVP_MD_size(ssl_handshake_md(s));
468             label = server_handshake_traffic;
469             labellen = sizeof(server_handshake_traffic) - 1;
470             log_label = SERVER_HANDSHAKE_LABEL;
471         } else {
472             insecret = s->master_secret;
473             label = server_application_traffic;
474             labellen = sizeof(server_application_traffic) - 1;
475             log_label = SERVER_APPLICATION_LABEL;
476         }
477     }
478
479     if (!(which & SSL3_CC_EARLY)) {
480         md = ssl_handshake_md(s);
481         cipher = s->s3->tmp.new_sym_enc;
482         if (!ssl3_digest_cached_records(s, 1)
483                 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
484             SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
485             goto err;
486         }
487     }
488
489     /*
490      * Save the hash of handshakes up to now for use when we calculate the
491      * client application traffic secret
492      */
493     if (label == server_application_traffic)
494         memcpy(s->server_finished_hash, hashval, hashlen);
495
496     if (label == server_handshake_traffic)
497         memcpy(s->handshake_traffic_hash, hashval, hashlen);
498
499     if (label == client_application_traffic) {
500         /*
501          * We also create the resumption master secret, but this time use the
502          * hash for the whole handshake including the Client Finished
503          */
504         if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
505                                resumption_master_secret,
506                                sizeof(resumption_master_secret) - 1,
507                                hashval, s->session->master_key, hashlen)) {
508             SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
509             goto err;
510         }
511         s->session->master_key_length = hashlen;
512     }
513
514     if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
515                                   insecret, hash, label, labellen, secret, iv,
516                                   ciph_ctx)) {
517         goto err;
518     }
519
520     if (label == server_application_traffic)
521         memcpy(s->server_app_traffic_secret, secret, hashlen);
522     else if (label == client_application_traffic)
523         memcpy(s->client_app_traffic_secret, secret, hashlen);
524
525     if (!ssl_log_secret(s, log_label, secret, hashlen)) {
526         SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
527         goto err;
528     }
529
530     if (finsecret != NULL
531             && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
532                                          finsecret, finsecretlen)) {
533         SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
534         goto err;
535     }
536
537     ret = 1;
538  err:
539     OPENSSL_cleanse(secret, sizeof(secret));
540     return ret;
541 }
542
543 int tls13_update_key(SSL *s, int sending)
544 {
545     static const unsigned char application_traffic[] = "traffic upd";
546     const EVP_MD *md = ssl_handshake_md(s);
547     size_t hashlen = EVP_MD_size(md);
548     unsigned char *insecret, *iv;
549     unsigned char secret[EVP_MAX_MD_SIZE];
550     EVP_CIPHER_CTX *ciph_ctx;
551     int ret = 0;
552
553     if (s->server == sending)
554         insecret = s->server_app_traffic_secret;
555     else
556         insecret = s->client_app_traffic_secret;
557
558     if (sending) {
559         iv = s->write_iv;
560         ciph_ctx = s->enc_write_ctx;
561         RECORD_LAYER_reset_write_sequence(&s->rlayer);
562     } else {
563         iv = s->read_iv;
564         ciph_ctx = s->enc_read_ctx;
565         RECORD_LAYER_reset_read_sequence(&s->rlayer);
566     }
567
568     if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
569                                   s->s3->tmp.new_sym_enc, insecret, NULL,
570                                   application_traffic,
571                                   sizeof(application_traffic) - 1, secret, iv,
572                                   ciph_ctx))
573         goto err;
574
575     memcpy(insecret, secret, hashlen);
576
577     ret = 1;
578  err:
579     OPENSSL_cleanse(secret, sizeof(secret));
580     return ret;
581 }
582
583 int tls13_alert_code(int code)
584 {
585     if (code == SSL_AD_MISSING_EXTENSION)
586         return code;
587
588     return tls1_alert_code(code);
589 }