Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / lib / tls / mbedtls / wrapper / library / ssl_lib.c
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "ssl_lib.h"
16 #include "ssl_pkey.h"
17 #include "ssl_x509.h"
18 #include "ssl_cert.h"
19 #include "ssl_dbg.h"
20 #include "ssl_port.h"
21
22 #include "core/private.h"
23
24 char *
25 lws_strncpy(char *dest, const char *src, size_t size);
26
27 #define SSL_SEND_DATA_MAX_LENGTH 1460
28
29 /**
30  * @brief create a new SSL session object
31  */
32 static SSL_SESSION* SSL_SESSION_new(void)
33 {
34     SSL_SESSION *session;
35
36     session = ssl_mem_zalloc(sizeof(SSL_SESSION));
37     if (!session) {
38         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no enough memory > (session)");
39         goto failed1;
40     }
41
42     session->peer = X509_new();
43     if (!session->peer) {
44        SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "X509_new() return NULL");
45        goto failed2;
46     }
47
48     return session;
49
50 failed2:
51     ssl_mem_free(session);
52 failed1:
53     return NULL;
54 }
55
56 /**
57  * @brief free a new SSL session object
58  */
59 static void SSL_SESSION_free(SSL_SESSION *session)
60 {
61     X509_free(session->peer);
62     ssl_mem_free(session);
63 }
64
65 /**
66  * @brief Discover whether the current connection is in the error state
67  */
68 int ossl_statem_in_error(const SSL *ssl)
69 {
70     SSL_ASSERT1(ssl);
71
72     if (ssl->statem.state == MSG_FLOW_ERROR)
73         return 1;
74
75     return 0;
76 }
77
78 /**
79  * @brief get the SSL specifical statement
80  */
81 int SSL_want(const SSL *ssl)
82 {
83     SSL_ASSERT1(ssl);
84
85     return ssl->rwstate;
86 }
87
88 /**
89  * @brief check if SSL want nothing
90  */
91 int SSL_want_nothing(const SSL *ssl)
92 {
93     SSL_ASSERT1(ssl);
94
95     if (ssl->err)
96             return 1;
97
98     return (SSL_want(ssl) == SSL_NOTHING);
99 }
100
101 /**
102  * @brief check if SSL want to read
103  */
104 int SSL_want_read(const SSL *ssl)
105 {
106     SSL_ASSERT1(ssl);
107
108     if (ssl->err)
109             return 0;
110
111     return (SSL_want(ssl) == SSL_READING);
112 }
113
114 /**
115  * @brief check if SSL want to write
116  */
117 int SSL_want_write(const SSL *ssl)
118 {
119     SSL_ASSERT1(ssl);
120
121     if (ssl->err)
122             return 0;
123
124     return (SSL_want(ssl) == SSL_WRITING);
125 }
126
127 /**
128  * @brief check if SSL want to lookup X509 certification
129  */
130 int SSL_want_x509_lookup(const SSL *ssl)
131 {
132     SSL_ASSERT1(ssl);
133
134     return (SSL_want(ssl) == SSL_WRITING);
135 }
136
137 /**
138  * @brief get SSL error code
139  */
140 int SSL_get_error(const SSL *ssl, int ret_code)
141 {
142     int ret = SSL_ERROR_SYSCALL;
143
144     SSL_ASSERT1(ssl);
145
146     if (ret_code > 0)
147         ret = SSL_ERROR_NONE;
148     else if (ret_code < 0)
149     {
150         if (ssl->err == SSL_ERROR_WANT_READ || SSL_want_read(ssl))
151             ret = SSL_ERROR_WANT_READ;
152         else if (ssl->err == SSL_ERROR_WANT_WRITE || SSL_want_write(ssl))
153             ret = SSL_ERROR_WANT_WRITE;
154         else
155             ret = SSL_ERROR_SYSCALL; //unknown
156     }
157     else // ret_code == 0
158     {
159         if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN)
160             ret = SSL_ERROR_ZERO_RETURN;
161         else
162             ret = SSL_ERROR_SYSCALL;
163     }
164
165     return ret;
166 }
167
168 /**
169  * @brief get the SSL state
170  */
171 OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
172 {
173     OSSL_HANDSHAKE_STATE state;
174
175     SSL_ASSERT1(ssl);
176
177     state = SSL_METHOD_CALL(get_state, ssl);
178
179     return state;
180 }
181
182 /**
183  * @brief create a SSL context
184  */
185 SSL_CTX* SSL_CTX_new(const SSL_METHOD *method)
186 {
187     SSL_CTX *ctx;
188     CERT *cert;
189     X509 *client_ca;
190
191     if (!method) {
192         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no no_method");
193         return NULL;
194     }
195
196     client_ca = X509_new();
197     if (!client_ca) {
198         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "X509_new() return NULL");
199         goto failed1;
200     }
201
202     cert = ssl_cert_new();
203     if (!cert) {
204         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "ssl_cert_new() return NULL");
205         goto failed2;
206     }
207
208     ctx = (SSL_CTX *)ssl_mem_zalloc(sizeof(SSL_CTX));
209     if (!ctx) {
210         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no enough memory > (ctx)");
211         goto failed3;
212     }
213
214     ctx->method = method;
215     ctx->client_CA = client_ca;
216     ctx->cert = cert;
217
218     ctx->version = method->version;
219
220     return ctx;
221
222 failed3:
223     ssl_cert_free(cert);
224 failed2:
225     X509_free(client_ca);
226 failed1:
227     return NULL;
228 }
229
230 /**
231  * @brief free a SSL context
232  */
233 void SSL_CTX_free(SSL_CTX* ctx)
234 {
235     SSL_ASSERT3(ctx);
236
237     ssl_cert_free(ctx->cert);
238
239     X509_free(ctx->client_CA);
240
241     if (ctx->alpn_protos)
242             ssl_mem_free(ctx->alpn_protos);
243
244     ssl_mem_free(ctx);
245 }
246
247 /**
248  * @brief set  the SSL context version
249  */
250 int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
251 {
252     SSL_ASSERT1(ctx);
253     SSL_ASSERT1(meth);
254
255     ctx->method = meth;
256
257     ctx->version = meth->version;
258
259     return 1;
260 }
261
262 /**
263  * @brief get the SSL context current method
264  */
265 const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
266 {
267     SSL_ASSERT2(ctx);
268
269     return ctx->method;
270 }
271
272 /**
273  * @brief create a SSL
274  */
275 SSL *SSL_new(SSL_CTX *ctx)
276 {
277     int ret = 0;
278     SSL *ssl;
279
280     if (!ctx) {
281         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no ctx");
282         return NULL;
283     }
284
285     ssl = (SSL *)ssl_mem_zalloc(sizeof(SSL));
286     if (!ssl) {
287         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "no enough memory > (ssl)");
288         goto failed1;
289     }
290
291     ssl->session = SSL_SESSION_new();
292     if (!ssl->session) {
293         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_SESSION_new() return NULL");
294         goto failed2;
295     }
296
297     ssl->cert = __ssl_cert_new(ctx->cert);
298     if (!ssl->cert) {
299         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "__ssl_cert_new() return NULL");
300         goto failed3;
301     }
302
303     ssl->client_CA = __X509_new(ctx->client_CA);
304     if (!ssl->client_CA) {
305         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "__X509_new() return NULL");
306         goto failed4;
307     }
308
309     ssl->ctx = ctx;
310     ssl->method = ctx->method;
311
312     ssl->version = ctx->version;
313     ssl->options = ctx->options;
314
315     ssl->verify_mode = ctx->verify_mode;
316
317     ret = SSL_METHOD_CALL(new, ssl);
318     if (ret) {
319         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_METHOD_CALL(new) return %d", ret);
320         goto failed5;
321     }
322
323    _ssl_set_alpn_list(ssl);
324
325     ssl->rwstate = SSL_NOTHING;
326
327     return ssl;
328
329 failed5:
330     X509_free(ssl->client_CA);
331 failed4:
332     ssl_cert_free(ssl->cert);
333 failed3:
334     SSL_SESSION_free(ssl->session);
335 failed2:
336     ssl_mem_free(ssl);
337 failed1:
338     return NULL;
339 }
340
341 /**
342  * @brief free the SSL
343  */
344 void SSL_free(SSL *ssl)
345 {
346     SSL_ASSERT3(ssl);
347
348     SSL_METHOD_CALL(free, ssl);
349
350     X509_free(ssl->client_CA);
351
352     ssl_cert_free(ssl->cert);
353
354     SSL_SESSION_free(ssl->session);
355
356     if (ssl->alpn_protos)
357             ssl_mem_free(ssl->alpn_protos);
358
359     ssl_mem_free(ssl);
360 }
361
362 /**
363  * @brief perform the SSL handshake
364  */
365 int SSL_do_handshake(SSL *ssl)
366 {
367     int ret;
368
369     SSL_ASSERT1(ssl);
370
371     ret = SSL_METHOD_CALL(handshake, ssl);
372
373     return ret;
374 }
375
376 /**
377  * @brief connect to the remote SSL server
378  */
379 int SSL_connect(SSL *ssl)
380 {
381     SSL_ASSERT1(ssl);
382
383     return SSL_do_handshake(ssl);
384 }
385
386 /**
387  * @brief accept the remote connection
388  */
389 int SSL_accept(SSL *ssl)
390 {
391     SSL_ASSERT1(ssl);
392
393     return SSL_do_handshake(ssl);
394 }
395
396 /**
397  * @brief shutdown the connection
398  */
399 int SSL_shutdown(SSL *ssl)
400 {
401     int ret;
402
403     SSL_ASSERT1(ssl);
404
405     if (SSL_get_state(ssl) != TLS_ST_OK) return 1;
406
407     ret = SSL_METHOD_CALL(shutdown, ssl);
408
409     return ret;
410 }
411
412 /**
413  * @brief reset the SSL
414  */
415 int SSL_clear(SSL *ssl)
416 {
417     int ret;
418
419     SSL_ASSERT1(ssl);
420
421     ret = SSL_shutdown(ssl);
422     if (1 != ret) {
423         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_shutdown return %d", ret);
424         goto failed1;
425     }
426
427     SSL_METHOD_CALL(free, ssl);
428
429     ret = SSL_METHOD_CALL(new, ssl);
430     if (!ret) {
431         SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_METHOD_CALL(new) return %d", ret);
432         goto failed1;
433     }
434
435     return 1;
436
437 failed1:
438     return ret;
439 }
440
441 /**
442  * @brief read data from to remote
443  */
444 int SSL_read(SSL *ssl, void *buffer, int len)
445 {
446     int ret;
447
448     SSL_ASSERT1(ssl);
449     SSL_ASSERT1(buffer);
450     SSL_ASSERT1(len);
451
452     ssl->rwstate = SSL_READING;
453
454     ret = SSL_METHOD_CALL(read, ssl, buffer, len);
455
456     if (ret == len)
457         ssl->rwstate = SSL_NOTHING;
458
459     return ret;
460 }
461
462 /**
463  * @brief send the data to remote
464  */
465 int SSL_write(SSL *ssl, const void *buffer, int len)
466 {
467     int ret;
468     int send_bytes, bytes;
469     const unsigned char *pbuf;
470
471     SSL_ASSERT1(ssl);
472     SSL_ASSERT1(buffer);
473     SSL_ASSERT1(len);
474
475     ssl->rwstate = SSL_WRITING;
476
477     send_bytes = len;
478     pbuf = (const unsigned char *)buffer;
479
480     do {
481         if (send_bytes > SSL_SEND_DATA_MAX_LENGTH)
482             bytes = SSL_SEND_DATA_MAX_LENGTH;
483         else
484             bytes = send_bytes;
485
486         if (ssl->interrupted_remaining_write) {
487                 bytes = ssl->interrupted_remaining_write;
488                 ssl->interrupted_remaining_write = 0;
489         }
490
491         ret = SSL_METHOD_CALL(send, ssl, pbuf, bytes);
492         //printf("%s: ssl_pm said %d for %d requested (cum %d)\n", __func__, ret, bytes, len -send_bytes);
493         /* the return is a NEGATIVE OpenSSL error code, or the length sent */
494         if (ret > 0) {
495             pbuf += ret;
496             send_bytes -= ret;
497         } else
498                 ssl->interrupted_remaining_write = bytes;
499     } while (ret > 0 && send_bytes && ret == bytes);
500
501     if (ret >= 0) {
502         ret = len - send_bytes;
503         if (!ret)
504                 ssl->rwstate = SSL_NOTHING;
505     } else {
506             if (send_bytes == len)
507                 ret = -1;
508             else
509                     ret = len - send_bytes;
510     }
511
512     return ret;
513 }
514
515 /**
516  * @brief get SSL context of the SSL
517  */
518 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
519 {
520     SSL_ASSERT2(ssl);
521
522     return ssl->ctx;
523 }
524
525 /**
526  * @brief get the SSL current method
527  */
528 const SSL_METHOD *SSL_get_ssl_method(SSL *ssl)
529 {
530     SSL_ASSERT2(ssl);
531
532     return ssl->method;
533 }
534
535 /**
536  * @brief set the SSL method
537  */
538 int SSL_set_ssl_method(SSL *ssl, const SSL_METHOD *method)
539 {
540     int ret;
541
542     SSL_ASSERT1(ssl);
543     SSL_ASSERT1(method);
544
545     if (ssl->version != method->version) {
546
547         ret = SSL_shutdown(ssl);
548         if (1 != ret) {
549             SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_shutdown return %d", ret);
550             goto failed1;
551         }
552
553         SSL_METHOD_CALL(free, ssl);
554
555         ssl->method = method;
556
557         ret = SSL_METHOD_CALL(new, ssl);
558         if (!ret) {
559             SSL_DEBUG(SSL_LIB_ERROR_LEVEL, "SSL_METHOD_CALL(new) return %d", ret);
560             goto failed1;
561         }
562     } else {
563         ssl->method = method;
564     }
565
566
567     return 1;
568
569 failed1:
570     return ret;
571 }
572
573 /**
574  * @brief get SSL shutdown mode
575  */
576 int SSL_get_shutdown(const SSL *ssl)
577 {
578     SSL_ASSERT1(ssl);
579
580     return ssl->shutdown;
581 }
582
583 /**
584  * @brief set SSL shutdown mode
585  */
586 void SSL_set_shutdown(SSL *ssl, int mode)
587 {
588     SSL_ASSERT3(ssl);
589
590     ssl->shutdown = mode;
591 }
592
593
594 /**
595  * @brief get the number of the bytes to be read
596  */
597 int SSL_pending(const SSL *ssl)
598 {
599     int ret;
600
601     SSL_ASSERT1(ssl);
602
603     ret = SSL_METHOD_CALL(pending, ssl);
604
605     return ret;
606 }
607
608 /**
609  * @brief check if some data can be read
610  */
611 int SSL_has_pending(const SSL *ssl)
612 {
613     int ret;
614
615     SSL_ASSERT1(ssl);
616
617     if (SSL_pending(ssl))
618         ret = 1;
619     else
620         ret = 0;
621
622     return ret;
623 }
624
625 /**
626  * @brief clear the SSL context option bit of "op"
627  */
628 unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op)
629 {
630     SSL_ASSERT1(ctx);
631
632     return ctx->options &= ~op;
633 }
634
635 /**
636  * @brief get the SSL context option
637  */
638 unsigned long SSL_CTX_get_options(SSL_CTX *ctx)
639 {
640     SSL_ASSERT1(ctx);
641
642     return ctx->options;
643 }
644
645 /**
646  * @brief set the option of the SSL context
647  */
648 unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long opt)
649 {
650     SSL_ASSERT1(ctx);
651
652     return ctx->options |= opt;
653 }
654
655 /**
656  * @brief clear SSL option
657  */
658 unsigned long SSL_clear_options(SSL *ssl, unsigned long op)
659 {
660     SSL_ASSERT1(ssl);
661
662     return ssl->options & ~op;
663 }
664
665 /**
666  * @brief get SSL option
667  */
668 unsigned long SSL_get_options(SSL *ssl)
669 {
670     SSL_ASSERT1(ssl);
671
672     return ssl->options;
673 }
674
675 /**
676  * @brief clear SSL option
677  */
678 unsigned long SSL_set_options(SSL *ssl, unsigned long op)
679 {
680     SSL_ASSERT1(ssl);
681
682     return ssl->options |= op;
683 }
684
685 /**
686  * @brief get the socket handle of the SSL
687  */
688 int SSL_get_fd(const SSL *ssl)
689 {
690     int ret;
691
692     SSL_ASSERT1(ssl);
693
694     ret = SSL_METHOD_CALL(get_fd, ssl, 0);
695
696     return ret;
697 }
698
699 /**
700  * @brief get the read only socket handle of the SSL
701  */
702 int SSL_get_rfd(const SSL *ssl)
703 {
704     int ret;
705
706     SSL_ASSERT1(ssl);
707
708     ret = SSL_METHOD_CALL(get_fd, ssl, 0);
709
710     return ret;
711 }
712
713 /**
714  * @brief get the write only socket handle of the SSL
715  */
716 int SSL_get_wfd(const SSL *ssl)
717 {
718     int ret;
719
720     SSL_ASSERT1(ssl);
721
722     ret = SSL_METHOD_CALL(get_fd, ssl, 0);
723
724     return ret;
725 }
726
727 /**
728  * @brief bind the socket file description into the SSL
729  */
730 int SSL_set_fd(SSL *ssl, int fd)
731 {
732     SSL_ASSERT1(ssl);
733     SSL_ASSERT1(fd >= 0);
734
735     SSL_METHOD_CALL(set_fd, ssl, fd, 0);
736
737     return 1;
738 }
739
740 /**
741  * @brief bind the read only socket file description into the SSL
742  */
743 int SSL_set_rfd(SSL *ssl, int fd)
744 {
745     SSL_ASSERT1(ssl);
746     SSL_ASSERT1(fd >= 0);
747
748     SSL_METHOD_CALL(set_fd, ssl, fd, 0);
749
750     return 1;
751 }
752
753 /**
754  * @brief bind the write only socket file description into the SSL
755  */
756 int SSL_set_wfd(SSL *ssl, int fd)
757 {
758     SSL_ASSERT1(ssl);
759     SSL_ASSERT1(fd >= 0);
760
761     SSL_METHOD_CALL(set_fd, ssl, fd, 0);
762
763     return 1;
764 }
765
766 /**
767  * @brief get SSL version
768  */
769 int SSL_version(const SSL *ssl)
770 {
771     SSL_ASSERT1(ssl);
772
773     return ssl->version;
774 }
775
776 /**
777  * @brief get the SSL version string
778  */
779 static const char* ssl_protocol_to_string(int version)
780 {
781     const char *str;
782
783     if (version == TLS1_2_VERSION)
784         str = "TLSv1.2";
785     else if (version == TLS1_1_VERSION)
786         str = "TLSv1.1";
787     else if (version == TLS1_VERSION)
788         str = "TLSv1";
789     else if (version == SSL3_VERSION)
790         str = "SSLv3";
791     else
792         str = "unknown";
793
794     return str;
795 }
796
797 /**
798  * @brief get the SSL current version
799  */
800 const char *SSL_get_version(const SSL *ssl)
801 {
802     SSL_ASSERT2(ssl);
803
804     return ssl_protocol_to_string(SSL_version(ssl));
805 }
806
807 /**
808  * @brief get alert description string
809  */
810 const char* SSL_alert_desc_string(int value)
811 {
812     const char *str;
813
814     switch (value & 0xff)
815     {
816         case SSL3_AD_CLOSE_NOTIFY:
817             str = "CN";
818             break;
819         case SSL3_AD_UNEXPECTED_MESSAGE:
820             str = "UM";
821             break;
822         case SSL3_AD_BAD_RECORD_MAC:
823             str = "BM";
824             break;
825         case SSL3_AD_DECOMPRESSION_FAILURE:
826             str = "DF";
827             break;
828         case SSL3_AD_HANDSHAKE_FAILURE:
829             str = "HF";
830             break;
831         case SSL3_AD_NO_CERTIFICATE:
832             str = "NC";
833             break;
834         case SSL3_AD_BAD_CERTIFICATE:
835             str = "BC";
836             break;
837         case SSL3_AD_UNSUPPORTED_CERTIFICATE:
838             str = "UC";
839             break;
840         case SSL3_AD_CERTIFICATE_REVOKED:
841             str = "CR";
842             break;
843         case SSL3_AD_CERTIFICATE_EXPIRED:
844             str = "CE";
845             break;
846         case SSL3_AD_CERTIFICATE_UNKNOWN:
847             str = "CU";
848             break;
849         case SSL3_AD_ILLEGAL_PARAMETER:
850             str = "IP";
851             break;
852         case TLS1_AD_DECRYPTION_FAILED:
853             str = "DC";
854             break;
855         case TLS1_AD_RECORD_OVERFLOW:
856             str = "RO";
857             break;
858         case TLS1_AD_UNKNOWN_CA:
859             str = "CA";
860             break;
861         case TLS1_AD_ACCESS_DENIED:
862             str = "AD";
863             break;
864         case TLS1_AD_DECODE_ERROR:
865             str = "DE";
866             break;
867         case TLS1_AD_DECRYPT_ERROR:
868             str = "CY";
869             break;
870         case TLS1_AD_EXPORT_RESTRICTION:
871             str = "ER";
872             break;
873         case TLS1_AD_PROTOCOL_VERSION:
874             str = "PV";
875             break;
876         case TLS1_AD_INSUFFICIENT_SECURITY:
877             str = "IS";
878             break;
879         case TLS1_AD_INTERNAL_ERROR:
880             str = "IE";
881             break;
882         case TLS1_AD_USER_CANCELLED:
883             str = "US";
884             break;
885         case TLS1_AD_NO_RENEGOTIATION:
886             str = "NR";
887             break;
888         case TLS1_AD_UNSUPPORTED_EXTENSION:
889             str = "UE";
890             break;
891         case TLS1_AD_CERTIFICATE_UNOBTAINABLE:
892             str = "CO";
893             break;
894         case TLS1_AD_UNRECOGNIZED_NAME:
895             str = "UN";
896             break;
897         case TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
898             str = "BR";
899             break;
900         case TLS1_AD_BAD_CERTIFICATE_HASH_VALUE:
901             str = "BH";
902             break;
903         case TLS1_AD_UNKNOWN_PSK_IDENTITY:
904             str = "UP";
905             break;
906         default:
907             str = "UK";
908             break;
909     }
910
911     return str;
912 }
913
914 /**
915  * @brief get alert description long string
916  */
917 const char* SSL_alert_desc_string_long(int value)
918 {
919     const char *str;
920
921     switch (value & 0xff)
922     {
923         case SSL3_AD_CLOSE_NOTIFY:
924             str = "close notify";
925             break;
926         case SSL3_AD_UNEXPECTED_MESSAGE:
927             str = "unexpected_message";
928             break;
929         case SSL3_AD_BAD_RECORD_MAC:
930             str = "bad record mac";
931             break;
932         case SSL3_AD_DECOMPRESSION_FAILURE:
933             str = "decompression failure";
934             break;
935         case SSL3_AD_HANDSHAKE_FAILURE:
936             str = "handshake failure";
937             break;
938         case SSL3_AD_NO_CERTIFICATE:
939             str = "no certificate";
940             break;
941         case SSL3_AD_BAD_CERTIFICATE:
942             str = "bad certificate";
943             break;
944         case SSL3_AD_UNSUPPORTED_CERTIFICATE:
945             str = "unsupported certificate";
946             break;
947         case SSL3_AD_CERTIFICATE_REVOKED:
948             str = "certificate revoked";
949             break;
950         case SSL3_AD_CERTIFICATE_EXPIRED:
951             str = "certificate expired";
952             break;
953         case SSL3_AD_CERTIFICATE_UNKNOWN:
954             str = "certificate unknown";
955             break;
956         case SSL3_AD_ILLEGAL_PARAMETER:
957             str = "illegal parameter";
958             break;
959         case TLS1_AD_DECRYPTION_FAILED:
960             str = "decryption failed";
961             break;
962         case TLS1_AD_RECORD_OVERFLOW:
963             str = "record overflow";
964             break;
965         case TLS1_AD_UNKNOWN_CA:
966             str = "unknown CA";
967             break;
968         case TLS1_AD_ACCESS_DENIED:
969             str = "access denied";
970             break;
971         case TLS1_AD_DECODE_ERROR:
972             str = "decode error";
973             break;
974         case TLS1_AD_DECRYPT_ERROR:
975             str = "decrypt error";
976             break;
977         case TLS1_AD_EXPORT_RESTRICTION:
978             str = "export restriction";
979             break;
980         case TLS1_AD_PROTOCOL_VERSION:
981             str = "protocol version";
982             break;
983         case TLS1_AD_INSUFFICIENT_SECURITY:
984             str = "insufficient security";
985             break;
986         case TLS1_AD_INTERNAL_ERROR:
987             str = "internal error";
988             break;
989         case TLS1_AD_USER_CANCELLED:
990             str = "user canceled";
991             break;
992         case TLS1_AD_NO_RENEGOTIATION:
993             str = "no renegotiation";
994             break;
995         case TLS1_AD_UNSUPPORTED_EXTENSION:
996             str = "unsupported extension";
997             break;
998         case TLS1_AD_CERTIFICATE_UNOBTAINABLE:
999             str = "certificate unobtainable";
1000             break;
1001         case TLS1_AD_UNRECOGNIZED_NAME:
1002             str = "unrecognized name";
1003             break;
1004         case TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
1005             str = "bad certificate status response";
1006             break;
1007         case TLS1_AD_BAD_CERTIFICATE_HASH_VALUE:
1008             str = "bad certificate hash value";
1009             break;
1010         case TLS1_AD_UNKNOWN_PSK_IDENTITY:
1011             str = "unknown PSK identity";
1012             break;
1013         default:
1014             str = "unknown";
1015             break;
1016     }
1017
1018     return str;
1019 }
1020
1021 /**
1022  * @brief get alert type string
1023  */
1024 const char *SSL_alert_type_string(int value)
1025 {
1026     const char *str;
1027
1028     switch (value >> 8)
1029     {
1030     case SSL3_AL_WARNING:
1031         str = "W";
1032         break;
1033     case SSL3_AL_FATAL:
1034         str = "F";
1035         break;
1036     default:
1037         str = "U";
1038         break;
1039     }
1040
1041     return str;
1042 }
1043
1044 /**
1045  * @brief get alert type long string
1046  */
1047 const char *SSL_alert_type_string_long(int value)
1048 {
1049     const char *str;
1050
1051     switch (value >> 8)
1052     {
1053         case SSL3_AL_WARNING:
1054             str = "warning";
1055             break;
1056         case SSL3_AL_FATAL:
1057             str = "fatal";
1058             break;
1059         default:
1060             str = "unknown";
1061             break;
1062     }
1063
1064     return str;
1065 }
1066
1067 /**
1068  * @brief get the state string where SSL is reading
1069  */
1070 const char *SSL_rstate_string(SSL *ssl)
1071 {
1072     const char *str;
1073
1074     SSL_ASSERT2(ssl);
1075
1076     switch (ssl->rlayer.rstate)
1077     {
1078         case SSL_ST_READ_HEADER:
1079             str = "RH";
1080             break;
1081         case SSL_ST_READ_BODY:
1082             str = "RB";
1083             break;
1084         case SSL_ST_READ_DONE:
1085             str = "RD";
1086             break;
1087         default:
1088             str = "unknown";
1089             break;
1090     }
1091
1092     return str;
1093 }
1094
1095 /**
1096  * @brief get the statement long string where SSL is reading
1097  */
1098 const char *SSL_rstate_string_long(SSL *ssl)
1099 {
1100     const char *str = "unknown";
1101
1102     SSL_ASSERT2(ssl);
1103
1104     switch (ssl->rlayer.rstate)
1105     {
1106         case SSL_ST_READ_HEADER:
1107             str = "read header";
1108             break;
1109         case SSL_ST_READ_BODY:
1110             str = "read body";
1111             break;
1112         case SSL_ST_READ_DONE:
1113             str = "read done";
1114             break;
1115         default:
1116             break;
1117     }
1118
1119     return str;
1120 }
1121
1122 /**
1123  * @brief get SSL statement string
1124  */
1125 char *SSL_state_string(const SSL *ssl)
1126 {
1127     char *str = "UNKWN ";
1128
1129     SSL_ASSERT2(ssl);
1130
1131     if (ossl_statem_in_error(ssl))
1132         str = "SSLERR";
1133     else
1134     {
1135         switch (SSL_get_state(ssl))
1136         {
1137             case TLS_ST_BEFORE:
1138                 str = "PINIT ";
1139                 break;
1140             case TLS_ST_OK:
1141                 str =  "SSLOK ";
1142                 break;
1143             case TLS_ST_CW_CLNT_HELLO:
1144                 str = "TWCH";
1145                 break;
1146             case TLS_ST_CR_SRVR_HELLO:
1147                 str = "TRSH";
1148                 break;
1149             case TLS_ST_CR_CERT:
1150                 str = "TRSC";
1151                 break;
1152             case TLS_ST_CR_KEY_EXCH:
1153                 str = "TRSKE";
1154                 break;
1155             case TLS_ST_CR_CERT_REQ:
1156                 str = "TRCR";
1157                 break;
1158             case TLS_ST_CR_SRVR_DONE:
1159                 str = "TRSD";
1160                 break;
1161             case TLS_ST_CW_CERT:
1162                 str = "TWCC";
1163                 break;
1164             case TLS_ST_CW_KEY_EXCH:
1165                 str = "TWCKE";
1166                 break;
1167             case TLS_ST_CW_CERT_VRFY:
1168                 str = "TWCV";
1169                 break;
1170             case TLS_ST_SW_CHANGE:
1171             case TLS_ST_CW_CHANGE:
1172                 str = "TWCCS";
1173                 break;
1174             case TLS_ST_SW_FINISHED:
1175             case TLS_ST_CW_FINISHED:
1176                 str = "TWFIN";
1177                 break;
1178             case TLS_ST_SR_CHANGE:
1179             case TLS_ST_CR_CHANGE:
1180                 str = "TRCCS";
1181                 break;
1182             case TLS_ST_SR_FINISHED:
1183             case TLS_ST_CR_FINISHED:
1184                 str = "TRFIN";
1185                 break;
1186             case TLS_ST_SW_HELLO_REQ:
1187                 str = "TWHR";
1188                 break;
1189             case TLS_ST_SR_CLNT_HELLO:
1190                 str = "TRCH";
1191                 break;
1192             case TLS_ST_SW_SRVR_HELLO:
1193                 str = "TWSH";
1194                 break;
1195             case TLS_ST_SW_CERT:
1196                 str = "TWSC";
1197                 break;
1198             case TLS_ST_SW_KEY_EXCH:
1199                 str = "TWSKE";
1200                 break;
1201             case TLS_ST_SW_CERT_REQ:
1202                 str = "TWCR";
1203                 break;
1204             case TLS_ST_SW_SRVR_DONE:
1205                 str = "TWSD";
1206                 break;
1207             case TLS_ST_SR_CERT:
1208                 str = "TRCC";
1209                 break;
1210             case TLS_ST_SR_KEY_EXCH:
1211                 str = "TRCKE";
1212                 break;
1213             case TLS_ST_SR_CERT_VRFY:
1214                 str = "TRCV";
1215                 break;
1216             case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1217                 str = "DRCHV";
1218                 break;
1219             case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1220                 str = "DWCHV";
1221                 break;
1222             default:
1223                 break;
1224         }
1225     }
1226
1227     return str;
1228 }
1229
1230 /**
1231  * @brief get SSL statement long string
1232  */
1233 char *SSL_state_string_long(const SSL *ssl)
1234 {
1235     char *str = "UNKWN ";
1236
1237     SSL_ASSERT2(ssl);
1238
1239     if (ossl_statem_in_error(ssl))
1240         str = "SSLERR";
1241     else
1242     {
1243         switch (SSL_get_state(ssl))
1244         {
1245             case TLS_ST_BEFORE:
1246                 str = "before SSL initialization";
1247                 break;
1248             case TLS_ST_OK:
1249                 str = "SSL negotiation finished successfully";
1250                 break;
1251             case TLS_ST_CW_CLNT_HELLO:
1252                 str = "SSLv3/TLS write client hello";
1253                 break;
1254             case TLS_ST_CR_SRVR_HELLO:
1255                 str = "SSLv3/TLS read server hello";
1256                 break;
1257             case TLS_ST_CR_CERT:
1258                 str = "SSLv3/TLS read server certificate";
1259                 break;
1260             case TLS_ST_CR_KEY_EXCH:
1261                 str = "SSLv3/TLS read server key exchange";
1262                 break;
1263             case TLS_ST_CR_CERT_REQ:
1264                 str = "SSLv3/TLS read server certificate request";
1265                 break;
1266             case TLS_ST_CR_SESSION_TICKET:
1267                 str = "SSLv3/TLS read server session ticket";
1268                 break;
1269             case TLS_ST_CR_SRVR_DONE:
1270                 str = "SSLv3/TLS read server done";
1271                 break;
1272             case TLS_ST_CW_CERT:
1273                 str = "SSLv3/TLS write client certificate";
1274                 break;
1275             case TLS_ST_CW_KEY_EXCH:
1276                 str = "SSLv3/TLS write client key exchange";
1277                 break;
1278             case TLS_ST_CW_CERT_VRFY:
1279                 str = "SSLv3/TLS write certificate verify";
1280                 break;
1281             case TLS_ST_CW_CHANGE:
1282             case TLS_ST_SW_CHANGE:
1283                 str = "SSLv3/TLS write change cipher spec";
1284                 break;
1285             case TLS_ST_CW_FINISHED:
1286             case TLS_ST_SW_FINISHED:
1287                 str = "SSLv3/TLS write finished";
1288                 break;
1289             case TLS_ST_CR_CHANGE:
1290             case TLS_ST_SR_CHANGE:
1291                 str = "SSLv3/TLS read change cipher spec";
1292                 break;
1293             case TLS_ST_CR_FINISHED:
1294             case TLS_ST_SR_FINISHED:
1295                 str = "SSLv3/TLS read finished";
1296                 break;
1297             case TLS_ST_SR_CLNT_HELLO:
1298                 str = "SSLv3/TLS read client hello";
1299                 break;
1300             case TLS_ST_SW_HELLO_REQ:
1301                 str = "SSLv3/TLS write hello request";
1302                 break;
1303             case TLS_ST_SW_SRVR_HELLO:
1304                 str = "SSLv3/TLS write server hello";
1305                 break;
1306             case TLS_ST_SW_CERT:
1307                 str = "SSLv3/TLS write certificate";
1308                 break;
1309             case TLS_ST_SW_KEY_EXCH:
1310                 str = "SSLv3/TLS write key exchange";
1311                 break;
1312             case TLS_ST_SW_CERT_REQ:
1313                 str = "SSLv3/TLS write certificate request";
1314                 break;
1315             case TLS_ST_SW_SESSION_TICKET:
1316                 str = "SSLv3/TLS write session ticket";
1317                 break;
1318             case TLS_ST_SW_SRVR_DONE:
1319                 str = "SSLv3/TLS write server done";
1320                 break;
1321             case TLS_ST_SR_CERT:
1322                 str = "SSLv3/TLS read client certificate";
1323                 break;
1324             case TLS_ST_SR_KEY_EXCH:
1325                 str = "SSLv3/TLS read client key exchange";
1326                 break;
1327             case TLS_ST_SR_CERT_VRFY:
1328                 str = "SSLv3/TLS read certificate verify";
1329                 break;
1330             case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1331                 str = "DTLS1 read hello verify request";
1332                 break;
1333             case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1334                 str = "DTLS1 write hello verify request";
1335                 break;
1336             default:
1337                 break;
1338         }
1339     }
1340
1341     return str;
1342 }
1343
1344 /**
1345  * @brief set the SSL context read buffer length
1346  */
1347 void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len)
1348 {
1349     SSL_ASSERT3(ctx);
1350
1351     ctx->read_buffer_len = len;
1352 }
1353
1354 /**
1355  * @brief set the SSL read buffer length
1356  */
1357 void SSL_set_default_read_buffer_len(SSL *ssl, size_t len)
1358 {
1359     SSL_ASSERT3(ssl);
1360     SSL_ASSERT3(len);
1361
1362     SSL_METHOD_CALL(set_bufflen, ssl, len);
1363 }
1364
1365 /**
1366  * @brief set the SSL information callback function
1367  */
1368 void SSL_set_info_callback(SSL *ssl, void (*cb) (const SSL *ssl, int type, int val))
1369 {
1370     SSL_ASSERT3(ssl);
1371
1372     ssl->info_callback = cb;
1373 }
1374
1375 /**
1376  * @brief add SSL context reference count by '1'
1377  */
1378 int SSL_CTX_up_ref(SSL_CTX *ctx)
1379 {
1380     SSL_ASSERT1(ctx);
1381
1382     /**
1383      * no support multi-thread SSL here
1384      */
1385     ctx->references++;
1386
1387     return 1;
1388 }
1389
1390 /**
1391  * @brief set the SSL security level
1392  */
1393 void SSL_set_security_level(SSL *ssl, int level)
1394 {
1395     SSL_ASSERT3(ssl);
1396
1397     ssl->cert->sec_level = level;
1398 }
1399
1400 /**
1401  * @brief get the SSL security level
1402  */
1403 int SSL_get_security_level(const SSL *ssl)
1404 {
1405     SSL_ASSERT1(ssl);
1406
1407     return ssl->cert->sec_level;
1408 }
1409
1410 /**
1411  * @brief get the SSL verifying mode of the SSL context
1412  */
1413 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
1414 {
1415     SSL_ASSERT1(ctx);
1416
1417     return ctx->verify_mode;
1418 }
1419
1420 /**
1421  * @brief set the session timeout time
1422  */
1423 long SSL_CTX_set_timeout(SSL_CTX *ctx, long t)
1424 {
1425     long l;
1426
1427     SSL_ASSERT1(ctx);
1428
1429     l = ctx->session_timeout;
1430     ctx->session_timeout = t;
1431
1432     return l;
1433 }
1434
1435 /**
1436  * @brief get the session timeout time
1437  */
1438 long SSL_CTX_get_timeout(const SSL_CTX *ctx)
1439 {
1440     SSL_ASSERT1(ctx);
1441
1442     return ctx->session_timeout;
1443 }
1444
1445 /**
1446  * @brief set the SSL if we can read as many as data
1447  */
1448 void SSL_set_read_ahead(SSL *ssl, int yes)
1449 {
1450     SSL_ASSERT3(ssl);
1451
1452     ssl->rlayer.read_ahead = yes;
1453 }
1454
1455 /**
1456  * @brief set the SSL context if we can read as many as data
1457  */
1458 void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes)
1459 {
1460     SSL_ASSERT3(ctx);
1461
1462     ctx->read_ahead = yes;
1463 }
1464
1465 /**
1466  * @brief get the SSL ahead signal if we can read as many as data
1467  */
1468 int SSL_get_read_ahead(const SSL *ssl)
1469 {
1470     SSL_ASSERT1(ssl);
1471
1472     return ssl->rlayer.read_ahead;
1473 }
1474
1475 /**
1476  * @brief get the SSL context ahead signal if we can read as many as data
1477  */
1478 long SSL_CTX_get_read_ahead(SSL_CTX *ctx)
1479 {
1480     SSL_ASSERT1(ctx);
1481
1482     return ctx->read_ahead;
1483 }
1484
1485 /**
1486  * @brief check if the SSL context can read as many as data
1487  */
1488 long SSL_CTX_get_default_read_ahead(SSL_CTX *ctx)
1489 {
1490     SSL_ASSERT1(ctx);
1491
1492     return ctx->read_ahead;
1493 }
1494
1495 /**
1496  * @brief set SSL session time
1497  */
1498 long SSL_set_time(SSL *ssl, long t)
1499 {
1500     SSL_ASSERT1(ssl);
1501
1502     ssl->session->time = t;
1503
1504     return t;
1505 }
1506
1507 /**
1508  * @brief set SSL session timeout time
1509  */
1510 long SSL_set_timeout(SSL *ssl, long t)
1511 {
1512     SSL_ASSERT1(ssl);
1513
1514     ssl->session->timeout = t;
1515
1516     return t;
1517 }
1518
1519 /**
1520  * @brief get the verifying result of the SSL certification
1521  */
1522 long SSL_get_verify_result(const SSL *ssl)
1523 {
1524     SSL_ASSERT1(ssl);
1525
1526     return SSL_METHOD_CALL(get_verify_result, ssl);
1527 }
1528
1529 /**
1530  * @brief get the SSL verifying depth of the SSL context
1531  */
1532 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
1533 {
1534     SSL_ASSERT1(ctx);
1535
1536     return ctx->param.depth;
1537 }
1538
1539 /**
1540  * @brief set the SSL verify depth of the SSL context
1541  */
1542 void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
1543 {
1544     SSL_ASSERT3(ctx);
1545
1546     ctx->param.depth = depth;
1547 }
1548
1549 /**
1550  * @brief get the SSL verifying depth of the SSL
1551  */
1552 int SSL_get_verify_depth(const SSL *ssl)
1553 {
1554     SSL_ASSERT1(ssl);
1555
1556     return ssl->param.depth;
1557 }
1558
1559 /**
1560  * @brief set the SSL verify depth of the SSL
1561  */
1562 void SSL_set_verify_depth(SSL *ssl, int depth)
1563 {
1564     SSL_ASSERT3(ssl);
1565
1566     ssl->param.depth = depth;
1567 }
1568
1569 /**
1570  * @brief set the SSL context verifying of the SSL context
1571  */
1572 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*verify_callback)(int, X509_STORE_CTX *))
1573 {
1574     SSL_ASSERT3(ctx);
1575
1576     ctx->verify_mode = mode;
1577     ctx->default_verify_callback = verify_callback;
1578 }
1579
1580 /**
1581  * @brief set the SSL verifying of the SSL context
1582  */
1583 void SSL_set_verify(SSL *ssl, int mode, int (*verify_callback)(int, X509_STORE_CTX *))
1584 {
1585     SSL_ASSERT3(ssl);
1586
1587     ssl->verify_mode = mode;
1588     ssl->verify_callback = verify_callback;
1589 }
1590
1591 void ERR_error_string_n(unsigned long e, char *buf, size_t len)
1592 {
1593         lws_strncpy(buf, "unknown", len);
1594 }
1595
1596 void ERR_free_strings(void)
1597 {
1598 }
1599
1600 char *ERR_error_string(unsigned long e, char *buf)
1601 {
1602         if (!buf)
1603                 return "unknown";
1604
1605         switch(e) {
1606                 case X509_V_ERR_INVALID_CA:
1607                         strcpy(buf, "CA is not trusted");
1608                         break;
1609                 case X509_V_ERR_HOSTNAME_MISMATCH:
1610                         strcpy(buf, "Hostname mismatch");
1611                         break;
1612                 case X509_V_ERR_CA_KEY_TOO_SMALL:
1613                         strcpy(buf, "CA key too small");
1614                         break;
1615                 case X509_V_ERR_CA_MD_TOO_WEAK:
1616                         strcpy(buf, "MD key too weak");
1617                         break;
1618                 case X509_V_ERR_CERT_NOT_YET_VALID:
1619                         strcpy(buf, "Cert from the future");
1620                         break;
1621                 case X509_V_ERR_CERT_HAS_EXPIRED:
1622                         strcpy(buf, "Cert expired");
1623                         break;
1624                 default:
1625                         strcpy(buf, "unknown");
1626                         break;
1627         }
1628
1629         return buf;
1630 }
1631
1632 void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx)
1633 {
1634         return NULL;
1635 }
1636
1637 /*
1638  * Openssl wants the valid protocol names supplied like this:
1639  *
1640  * (unsigned char *)"\x02h2\x08http/1.1", 6 + 9
1641  *
1642  * Mbedtls wants this:
1643  *
1644  * Pointer to a NULL-terminated list of supported protocols, in decreasing
1645  * preference order. The pointer to the list is recorded by the library for
1646  * later reference as required, so the lifetime of the table must be at least
1647  * as long as the lifetime of the SSL configuration structure.
1648  *
1649  * So accept the OpenSSL style and convert to mbedtls style
1650  */
1651
1652
1653 static void
1654 _openssl_alpn_to_mbedtls(struct alpn_ctx *ac, char ***palpn_protos)
1655 {
1656         unsigned char *p = ac->data, *q;
1657         unsigned char len;
1658         char **alpn_protos;
1659         int count = 0;
1660
1661         /* find out how many entries he gave us */
1662
1663         len = *p++;
1664         while (p - ac->data < ac->len) {
1665                 if (len--) {
1666                         p++;
1667                         continue;
1668                 }
1669                 count++;
1670                 len = *p++;
1671                 if (!len)
1672                         break;
1673         }
1674
1675         if (!len)
1676                 count++;
1677
1678         if (!count)
1679                 return;
1680
1681         /* allocate space for count + 1 pointers and the data afterwards */
1682
1683         alpn_protos = ssl_mem_zalloc((count + 1) * sizeof(char *) + ac->len + 1);
1684         if (!alpn_protos)
1685                 return;
1686
1687         *palpn_protos = alpn_protos;
1688
1689         /* convert to mbedtls format */
1690
1691         q = (unsigned char *)alpn_protos + (count + 1) * sizeof(char *);
1692         p = ac->data;
1693         count = 0;
1694
1695         len = *p++;
1696         alpn_protos[count] = (char *)q;
1697         while (p - ac->data < ac->len) {
1698                 if (len--) {
1699                         *q++ = *p++;
1700                         continue;
1701                 }
1702                 *q++ = '\0';
1703                 count++;
1704                 len = *p++;
1705                 alpn_protos[count] = (char *)q;
1706                 if (!len)
1707                         break;
1708         }
1709         if (!len) {
1710                 *q++ = '\0';
1711                 count++;
1712                 /* len = *p++; */
1713                 alpn_protos[count] = (char *)q;
1714         }
1715         alpn_protos[count] = NULL; /* last pointer ends list with NULL */
1716 }
1717
1718 void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, next_proto_cb cb, void *arg)
1719 {
1720         struct alpn_ctx *ac = arg;
1721
1722         ctx->alpn_cb = cb;
1723
1724         _openssl_alpn_to_mbedtls(ac, (char ***)&ctx->alpn_protos);
1725 }
1726
1727 void SSL_set_alpn_select_cb(SSL *ssl, void *arg)
1728 {
1729         struct alpn_ctx *ac = arg;
1730
1731         _openssl_alpn_to_mbedtls(ac, (char ***)&ssl->alpn_protos);
1732
1733         _ssl_set_alpn_list(ssl);
1734 }