Commit a8187e9ad76cf2c52f91b0b483dae0545dd3e7c4

Authored by perry.werneck@gmail.com
1 parent 8a48b002

Melhorando tratamento do indicador de SSL

Showing 4 changed files with 85 additions and 32 deletions   Show diff stats
@@ -157,7 +157,7 @@ @@ -157,7 +157,7 @@
157 157
158 #define OIA_FLAG_BOXSOLID LIB3270_FLAG_BOXSOLID 158 #define OIA_FLAG_BOXSOLID LIB3270_FLAG_BOXSOLID
159 #define OIA_FLAG_UNDERA LIB3270_FLAG_UNDERA 159 #define OIA_FLAG_UNDERA LIB3270_FLAG_UNDERA
160 - #define OIA_FLAG_SECURE LIB3270_FLAG_SECURE 160 +// #define OIA_FLAG_SECURE LIB3270_FLAG_SECURE
161 #define OIA_FLAG_TYPEAHEAD LIB3270_FLAG_TYPEAHEAD 161 #define OIA_FLAG_TYPEAHEAD LIB3270_FLAG_TYPEAHEAD
162 #define OIA_FLAG_PRINTER LIB3270_FLAG_PRINTER 162 #define OIA_FLAG_PRINTER LIB3270_FLAG_PRINTER
163 #define OIA_FLAG_REVERSE LIB3270_FLAG_REVERSE 163 #define OIA_FLAG_REVERSE LIB3270_FLAG_REVERSE
@@ -356,7 +356,7 @@ @@ -356,7 +356,7 @@
356 LOCAL_EXTERN void Input_String(const unsigned char *str); 356 LOCAL_EXTERN void Input_String(const unsigned char *str);
357 LOCAL_EXTERN void screen_size(int *rows, int *cols); 357 LOCAL_EXTERN void screen_size(int *rows, int *cols);
358 358
359 - #define query_secure_connection(h) lib3270_get_ssl_state(h) 359 +// #define query_secure_connection(h) lib3270_get_ssl_state(h)
360 #define lib3270_paste_string(str) lib3270_set_string(NULL,str) 360 #define lib3270_paste_string(str) lib3270_set_string(NULL,str)
361 #define get_3270_terminal_size(h,r,c) lib3270_get_screen_size(h,r,c) 361 #define get_3270_terminal_size(h,r,c) lib3270_get_screen_size(h,r,c)
362 362
@@ -118,6 +118,10 @@ static void message(H3270 *session, LIB3270_NOTIFY id , const char *title, const @@ -118,6 +118,10 @@ static void message(H3270 *session, LIB3270_NOTIFY id , const char *title, const
118 lib3270_write_log(session,"%s",text); 118 lib3270_write_log(session,"%s",text);
119 } 119 }
120 120
  121 +static void update_ssl(H3270 *session, LIB3270_SSL_STATE state)
  122 +{
  123 +}
  124 +
121 static void lib3270_session_init(H3270 *hSession, const char *model) 125 static void lib3270_session_init(H3270 *hSession, const char *model)
122 { 126 {
123 int ovc, ovr; 127 int ovc, ovr;
@@ -144,6 +148,7 @@ static void lib3270_session_init(H3270 *hSession, const char *model) @@ -144,6 +148,7 @@ static void lib3270_session_init(H3270 *hSession, const char *model)
144 hSession->update_selection = update_selection; 148 hSession->update_selection = update_selection;
145 hSession->cursor = set_cursor; 149 hSession->cursor = set_cursor;
146 hSession->message = message; 150 hSession->message = message;
  151 + hSession->update_ssl = update_ssl;
147 152
148 hSession->sock = -1; 153 hSession->sock = -1;
149 hSession->model_num = -1; 154 hSession->model_num = -1;
@@ -546,15 +546,19 @@ static void status_connect(H3270 *session, int connected, void *dunno) @@ -546,15 +546,19 @@ static void status_connect(H3270 *session, int connected, void *dunno)
546 else 546 else
547 id = LIB3270_STATUS_CONNECTED; 547 id = LIB3270_STATUS_CONNECTED;
548 548
549 -#if defined(HAVE_LIBSSL) /*[*/ 549 +/*
  550 +#if defined(HAVE_LIBSSL)
550 set_status(session,OIA_FLAG_SECURE,session->secure_connection); 551 set_status(session,OIA_FLAG_SECURE,session->secure_connection);
551 -#endif /*]*/ 552 +#endif
  553 +*/
552 554
553 } 555 }
554 else 556 else
555 { 557 {
556 set_status(session,OIA_FLAG_BOXSOLID,False); 558 set_status(session,OIA_FLAG_BOXSOLID,False);
  559 +/*
557 set_status(session,OIA_FLAG_SECURE,False); 560 set_status(session,OIA_FLAG_SECURE,False);
  561 +*/
558 562
559 id = LIB3270_STATUS_DISCONNECTED; 563 id = LIB3270_STATUS_DISCONNECTED;
560 } 564 }
@@ -351,8 +351,19 @@ static void output_possible(H3270 *session); @@ -351,8 +351,19 @@ static void output_possible(H3270 *session);
351 #endif /*]*/ 351 #endif /*]*/
352 352
353 353
  354 +/*--[ Implement ]------------------------------------------------------------------------------------*/
  355 +
  356 +void set_ssl_state(H3270 *session, LIB3270_SSL_STATE state)
  357 +{
  358 + if(state == session->secure)
  359 + return;
  360 +
  361 + trace_dsn("SSL state changes to %d",(int) state);
  362 + trace("SSL state changes to %d",(int) state);
  363 +
  364 + session->update_ssl(session,session->secure = state);
  365 +}
354 366
355 -  
356 #if defined(_WIN32) /*[*/ 367 #if defined(_WIN32) /*[*/
357 void sockstart(H3270 *session) 368 void sockstart(H3270 *session)
358 { 369 {
@@ -477,6 +488,8 @@ int net_connect(H3270 *session, const char *host, char *portname, Boolean ls, Bo @@ -477,6 +488,8 @@ int net_connect(H3270 *session, const char *host, char *portname, Boolean ls, Bo
477 488
478 #define close_fail { (void) SOCK_CLOSE(session->sock); session->sock = -1; return -1; } 489 #define close_fail { (void) SOCK_CLOSE(session->sock); session->sock = -1; return -1; }
479 490
  491 + set_ssl_state(session,LIB3270_SSL_UNSECURE);
  492 +
480 #if defined(_WIN32) 493 #if defined(_WIN32)
481 sockstart(session); 494 sockstart(session);
482 #endif 495 #endif
@@ -763,44 +776,49 @@ static void net_connected(H3270 *session) @@ -763,44 +776,49 @@ static void net_connected(H3270 *session)
763 776
764 #if defined(HAVE_LIBSSL) /*[*/ 777 #if defined(HAVE_LIBSSL) /*[*/
765 /* Set up SSL. */ 778 /* Set up SSL. */
766 - if(session->ssl_host && !session->secure_connection) 779 + if(session->ssl_con && session->secure == LIB3270_SSL_UNDEFINED)
767 { 780 {
768 int rc; 781 int rc;
769 782
  783 + set_ssl_state(session,LIB3270_SSL_NEGOTIATING);
  784 +
770 if (SSL_set_fd(session->ssl_con, session->sock) != 1) 785 if (SSL_set_fd(session->ssl_con, session->sock) != 1)
771 { 786 {
772 trace_dsn("Can't set fd!\n"); 787 trace_dsn("Can't set fd!\n");
773 popup_system_error(&h3270,_( "Connection failed" ), _( "Can't set SSL socket file descriptor" ), "%s", SSL_state_string_long(session->ssl_con)); 788 popup_system_error(&h3270,_( "Connection failed" ), _( "Can't set SSL socket file descriptor" ), "%s", SSL_state_string_long(session->ssl_con));
  789 + set_ssl_state(session,LIB3270_SSL_UNSECURE);
774 } 790 }
  791 + else
  792 + {
  793 + non_blocking(False);
  794 + rc = SSL_connect(session->ssl_con);
775 795
776 - non_blocking(False);  
777 - rc = SSL_connect(session->ssl_con); 796 + if(rc != 1)
  797 + {
  798 + unsigned long e = ERR_get_error();
  799 + const char * state = SSL_state_string_long(session->ssl_con);
778 800
779 - if(rc != 1)  
780 - {  
781 - unsigned long e = ERR_get_error();  
782 - const char * state = SSL_state_string_long(session->ssl_con); 801 + trace_dsn("TLS/SSL tunneled connection failed with error %ld, rc=%d and state=%s",e,rc,state);
783 802
784 - trace_dsn("TLS/SSL tunneled connection failed with error %ld, rc=%d and state=%s",e,rc,state); 803 + host_disconnect(session,True);
785 804
786 - host_disconnect(session,True); 805 + if(e != session->last_ssl_error)
  806 + {
  807 + session->message( &h3270,
  808 + LIB3270_NOTIFY_ERROR,
  809 + _( "Connection failed" ),
  810 + _( "SSL negotiation failed" ),
  811 + state);
  812 + session->last_ssl_error = e;
  813 + }
  814 + return;
787 815
788 - if(e != session->last_ssl_error)  
789 - {  
790 - session->message( &h3270,  
791 - LIB3270_NOTIFY_ERROR,  
792 - _( "Connection failed" ),  
793 - _( "SSL negotiation failed" ),  
794 - state);  
795 - session->last_ssl_error = e;  
796 } 816 }
797 - return;  
798 - 817 + non_blocking(True);
799 } 818 }
800 - non_blocking(True);  
801 819
802 - session->secure_connection = True;  
803 - trace_dsn("TLS/SSL tunneled connection complete. Connection is now secure.\n"); 820 +// session->secure_connection = True;
  821 +// trace_dsn("TLS/SSL tunneled connection complete. Connection is now secure.\n");
804 822
805 /* Tell everyone else again. */ 823 /* Tell everyone else again. */
806 host_connected(session); 824 host_connected(session);
@@ -897,18 +915,22 @@ static void output_possible(H3270 *session) @@ -897,18 +915,22 @@ static void output_possible(H3270 *session)
897 */ 915 */
898 void net_disconnect(void) 916 void net_disconnect(void)
899 { 917 {
900 -#if defined(HAVE_LIBSSL) /*[*/ 918 +#if defined(HAVE_LIBSSL)
901 if (h3270.ssl_con != NULL) 919 if (h3270.ssl_con != NULL)
902 { 920 {
903 SSL_shutdown(h3270.ssl_con); 921 SSL_shutdown(h3270.ssl_con);
904 SSL_free(h3270.ssl_con); 922 SSL_free(h3270.ssl_con);
905 h3270.ssl_con = NULL; 923 h3270.ssl_con = NULL;
906 } 924 }
907 - h3270.secure_connection = False;  
908 -#endif /*]*/ 925 +#endif
  926 +
  927 + set_ssl_state(&h3270,LIB3270_SSL_UNSECURE);
  928 +
909 if (CONNECTED) 929 if (CONNECTED)
910 (void) shutdown(h3270.sock, 2); 930 (void) shutdown(h3270.sock, 2);
  931 +
911 (void) SOCK_CLOSE(h3270.sock); 932 (void) SOCK_CLOSE(h3270.sock);
  933 +
912 h3270.sock = -1; 934 h3270.sock = -1;
913 trace_dsn("SENT disconnect\n"); 935 trace_dsn("SENT disconnect\n");
914 936
@@ -3233,6 +3255,8 @@ static void ssl_init(H3270 *session) @@ -3233,6 +3255,8 @@ static void ssl_init(H3270 *session)
3233 { 3255 {
3234 static SSL_CTX *ssl_ctx = NULL; 3256 static SSL_CTX *ssl_ctx = NULL;
3235 3257
  3258 + set_ssl_state(session,LIB3270_SSL_UNDEFINED);
  3259 +
3236 if(ssl_ctx == NULL) 3260 if(ssl_ctx == NULL)
3237 { 3261 {
3238 lib3270_write_log(session,"%s","Initializing SSL context"); 3262 lib3270_write_log(session,"%s","Initializing SSL context");
@@ -3293,6 +3317,9 @@ static void client_info_callback(INFO_CONST SSL *s, int where, int ret) @@ -3293,6 +3317,9 @@ static void client_info_callback(INFO_CONST SSL *s, int where, int ret)
3293 break; 3317 break;
3294 3318
3295 case SSL_CB_CONNECT_EXIT: 3319 case SSL_CB_CONNECT_EXIT:
  3320 +
  3321 + trace("%s: SSL_CB_CONNECT_EXIT",__FUNCTION__);
  3322 +
3296 if (ret == 0) 3323 if (ret == 0)
3297 { 3324 {
3298 trace_dsn("SSL_connect: failed in %s\n",SSL_state_string_long(s)); 3325 trace_dsn("SSL_connect: failed in %s\n",SSL_state_string_long(s));
@@ -3349,6 +3376,15 @@ static void client_info_callback(INFO_CONST SSL *s, int where, int ret) @@ -3349,6 +3376,15 @@ static void client_info_callback(INFO_CONST SSL *s, int where, int ret)
3349 3376
3350 if(where & SSL_CB_ALERT) 3377 if(where & SSL_CB_ALERT)
3351 lib3270_write_log(NULL,"SSL","ALERT: %s",SSL_alert_type_string_long(ret)); 3378 lib3270_write_log(NULL,"SSL","ALERT: %s",SSL_alert_type_string_long(ret));
  3379 +
  3380 + if(where & SSL_CB_HANDSHAKE_DONE)
  3381 + {
  3382 + trace("%s: SSL_CB_HANDSHAKE_DONE state=%04x",__FUNCTION__,SSL_state(s));
  3383 + if(SSL_state(s) == 0x03)
  3384 + set_ssl_state(&h3270,LIB3270_SSL_SECURE);
  3385 + else
  3386 + set_ssl_state(&h3270,LIB3270_SSL_UNSECURE);
  3387 + }
3352 } 3388 }
3353 3389
3354 /* Process a STARTTLS subnegotiation. */ 3390 /* Process a STARTTLS subnegotiation. */
@@ -3406,10 +3442,10 @@ static void continue_tls(unsigned char *sbbuf, int len) @@ -3406,10 +3442,10 @@ static void continue_tls(unsigned char *sbbuf, int len)
3406 return; 3442 return;
3407 } 3443 }
3408 3444
3409 - h3270.secure_connection = True; 3445 +// h3270.secure_connection = True;
3410 3446
3411 /* Success. */ 3447 /* Success. */
3412 - trace_dsn("TLS/SSL negotiated connection complete. Connection is now secure.\n"); 3448 +// trace_dsn("TLS/SSL negotiated connection complete. Connection is now secure.\n");
3413 3449
3414 /* Tell the world that we are (still) connected, now in secure mode. */ 3450 /* Tell the world that we are (still) connected, now in secure mode. */
3415 host_connected(&h3270); 3451 host_connected(&h3270);
@@ -3455,6 +3491,13 @@ net_proxy_port(void) @@ -3455,6 +3491,13 @@ net_proxy_port(void)
3455 return NULL; 3491 return NULL;
3456 } 3492 }
3457 3493
  3494 +LIB3270_EXPORT LIB3270_SSL_STATE lib3270_get_secure(H3270 *session)
  3495 +{
  3496 + CHECK_SESSION_HANDLE(session);
  3497 + return session->secure;
  3498 +}
  3499 +
  3500 +/*
3458 LIB3270_EXPORT int lib3270_get_ssl_state(H3270 *h) 3501 LIB3270_EXPORT int lib3270_get_ssl_state(H3270 *h)
3459 { 3502 {
3460 CHECK_SESSION_HANDLE(h); 3503 CHECK_SESSION_HANDLE(h);
@@ -3465,6 +3508,7 @@ LIB3270_EXPORT int lib3270_get_ssl_state(H3270 *h) @@ -3465,6 +3508,7 @@ LIB3270_EXPORT int lib3270_get_ssl_state(H3270 *h)
3465 return 0; 3508 return 0;
3466 #endif 3509 #endif
3467 } 3510 }
  3511 +*/
3468 3512
3469 /* 3513 /*
3470 int Get3270Socket(void) 3514 int Get3270Socket(void)