| 1042 | | #ifdef WI_SSL |
|---|
| 1043 | | if(context && context->ssl_ctx) { |
|---|
| 1044 | | socket->ssl = SSL_new(context->ssl_ctx); |
|---|
| 1045 | | |
|---|
| 1046 | | if(!socket->ssl) { |
|---|
| 1047 | | wi_error_set_openssl_error(); |
|---|
| 1048 | | |
|---|
| 1049 | | return false; |
|---|
| 1050 | | } |
|---|
| 1051 | | |
|---|
| 1052 | | if(SSL_set_fd(socket->ssl, socket->sd) != 1) { |
|---|
| 1053 | | wi_error_set_openssl_error(); |
|---|
| 1054 | | |
|---|
| 1055 | | return false; |
|---|
| 1056 | | } |
|---|
| 1057 | | |
|---|
| 1058 | | ret = SSL_connect(socket->ssl); |
|---|
| 1059 | | |
|---|
| 1060 | | if(ret != 1) { |
|---|
| 1061 | | do { |
|---|
| 1062 | | err = SSL_get_error(socket->ssl, ret); |
|---|
| 1063 | | |
|---|
| 1064 | | if(err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) { |
|---|
| 1065 | | wi_error_set_openssl_error(); |
|---|
| 1066 | | |
|---|
| 1067 | | return false; |
|---|
| 1068 | | } |
|---|
| 1069 | | |
|---|
| 1070 | | state = wi_socket_wait_descriptor(socket->sd, 1.0, (err == SSL_ERROR_WANT_READ), (err == SSL_ERROR_WANT_WRITE)); |
|---|
| 1071 | | |
|---|
| 1072 | | if(state == WI_SOCKET_ERROR) |
|---|
| 1073 | | break; |
|---|
| 1074 | | else if(state == WI_SOCKET_READY) { |
|---|
| 1075 | | ret = SSL_connect(socket->ssl); |
|---|
| 1076 | | |
|---|
| 1077 | | if(ret == 1) |
|---|
| 1078 | | break; |
|---|
| 1079 | | } |
|---|
| 1080 | | |
|---|
| 1081 | | timeout -= 1.0; |
|---|
| 1082 | | } while(timeout >= 0.0); |
|---|
| 1083 | | |
|---|
| 1084 | | if(state == WI_SOCKET_ERROR) |
|---|
| 1085 | | return false; |
|---|
| 1086 | | |
|---|
| 1087 | | if(timeout <= 0.0) { |
|---|
| 1088 | | wi_error_set_errno(ETIMEDOUT); |
|---|
| 1089 | | |
|---|
| 1090 | | return false; |
|---|
| 1091 | | } |
|---|
| 1092 | | } |
|---|
| 1093 | | } |
|---|
| 1094 | | #endif |
|---|
| 1095 | | |
|---|
| 1104 | | |
|---|
| 1105 | | #ifdef WI_SSL |
|---|
| 1106 | | if(context && context->ssl_ctx) { |
|---|
| 1107 | | socket->ssl = SSL_new(context->ssl_ctx); |
|---|
| | 1063 | } |
|---|
| | 1064 | |
|---|
| | 1065 | socket->direction = WI_SOCKET_READ; |
|---|
| | 1066 | |
|---|
| | 1067 | return true; |
|---|
| | 1068 | } |
|---|
| | 1069 | |
|---|
| | 1070 | |
|---|
| | 1071 | |
|---|
| | 1072 | wi_boolean_t wi_socket_tls_connect(wi_socket_t *socket, wi_socket_context_t *context, wi_time_interval_t timeout) { |
|---|
| | 1073 | #ifdef WI_SSL |
|---|
| | 1074 | int ret; |
|---|
| | 1075 | wi_boolean_t blocking; |
|---|
| | 1076 | |
|---|
| | 1077 | if(timeout > 0.0) { |
|---|
| | 1078 | blocking = wi_socket_blocking(socket); |
|---|
| | 1079 | |
|---|
| | 1080 | if(blocking) |
|---|
| | 1081 | wi_socket_set_blocking(socket, false); |
|---|
| | 1082 | |
|---|
| | 1083 | socket->ssl = SSL_new(context->ssl_ctx); |
|---|
| | 1084 | |
|---|
| | 1085 | if(!socket->ssl) { |
|---|
| | 1086 | wi_error_set_openssl_error(); |
|---|
| 1109 | | if(!socket->ssl) { |
|---|
| 1110 | | wi_error_set_openssl_error(); |
|---|
| | 1088 | return false; |
|---|
| | 1089 | } |
|---|
| | 1090 | |
|---|
| | 1091 | if(SSL_set_fd(socket->ssl, socket->sd) != 1) { |
|---|
| | 1092 | wi_error_set_openssl_error(); |
|---|
| | 1093 | |
|---|
| | 1094 | return false; |
|---|
| | 1095 | } |
|---|
| | 1096 | |
|---|
| | 1097 | ret = SSL_connect(socket->ssl); |
|---|
| | 1098 | |
|---|
| | 1099 | if(ret != 1) { |
|---|
| | 1100 | do { |
|---|
| | 1101 | err = SSL_get_error(socket->ssl, ret); |
|---|
| | 1102 | |
|---|
| | 1103 | if(err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) { |
|---|
| | 1104 | wi_error_set_openssl_error(); |
|---|
| | 1105 | |
|---|
| | 1106 | return false; |
|---|
| | 1107 | } |
|---|
| | 1108 | |
|---|
| | 1109 | state = wi_socket_wait_descriptor(socket->sd, 1.0, (err == SSL_ERROR_WANT_READ), (err == SSL_ERROR_WANT_WRITE)); |
|---|
| | 1110 | |
|---|
| | 1111 | if(state == WI_SOCKET_ERROR) |
|---|
| | 1112 | break; |
|---|
| | 1113 | else if(state == WI_SOCKET_READY) { |
|---|
| | 1114 | ret = SSL_connect(socket->ssl); |
|---|
| | 1115 | |
|---|
| | 1116 | if(ret == 1) |
|---|
| | 1117 | break; |
|---|
| | 1118 | } |
|---|
| | 1119 | |
|---|
| | 1120 | timeout -= 1.0; |
|---|
| | 1121 | } while(timeout >= 0.0); |
|---|
| | 1122 | |
|---|
| | 1123 | if(state == WI_SOCKET_ERROR) |
|---|
| | 1124 | return false; |
|---|
| | 1125 | |
|---|
| | 1126 | if(timeout <= 0.0) { |
|---|
| | 1127 | wi_error_set_errno(ETIMEDOUT); |
|---|
| 1115 | | if(SSL_set_fd(socket->ssl, socket->sd) != 1) { |
|---|
| 1116 | | wi_error_set_openssl_error(); |
|---|
| 1117 | | |
|---|
| 1118 | | return false; |
|---|
| 1119 | | } |
|---|
| 1120 | | |
|---|
| 1121 | | if(SSL_connect(socket->ssl) != 1) { |
|---|
| 1122 | | wi_error_set_openssl_error(); |
|---|
| 1123 | | |
|---|
| 1124 | | return false; |
|---|
| 1125 | | } |
|---|
| 1126 | | } |
|---|
| 1127 | | #endif |
|---|
| 1128 | | } |
|---|
| 1129 | | |
|---|
| 1130 | | socket->direction = WI_SOCKET_READ; |
|---|
| | 1141 | return false; |
|---|
| | 1142 | } |
|---|
| | 1143 | |
|---|
| | 1144 | if(SSL_set_fd(socket->ssl, socket->sd) != 1) { |
|---|
| | 1145 | wi_error_set_openssl_error(); |
|---|
| | 1146 | |
|---|
| | 1147 | return false; |
|---|
| | 1148 | } |
|---|
| | 1149 | |
|---|
| | 1150 | if(SSL_connect(socket->ssl) != 1) { |
|---|
| | 1151 | wi_error_set_openssl_error(); |
|---|
| | 1152 | |
|---|
| | 1153 | return false; |
|---|
| | 1154 | } |
|---|
| | 1155 | } |
|---|
| 1171 | | #ifdef WI_SSL |
|---|
| 1172 | | if(context && context->ssl_ctx) { |
|---|
| 1173 | | ssl = SSL_new(context->ssl_ctx); |
|---|
| 1174 | | |
|---|
| 1175 | | if(!ssl) { |
|---|
| 1176 | | wi_error_set_openssl_error(); |
|---|
| 1177 | | |
|---|
| 1178 | | goto err; |
|---|
| 1179 | | } |
|---|
| 1180 | | |
|---|
| 1181 | | if(SSL_set_fd(ssl, sd) != 1) { |
|---|
| 1182 | | wi_error_set_openssl_error(); |
|---|
| 1183 | | |
|---|
| 1184 | | goto err; |
|---|
| 1185 | | } |
|---|
| 1186 | | |
|---|
| 1187 | | if(!context->certificate && context->dh) { |
|---|
| 1188 | | if(SSL_set_tmp_dh(ssl, context->dh) != 1) { |
|---|
| 1189 | | wi_error_set_openssl_error(); |
|---|
| 1190 | | |
|---|
| 1191 | | goto err; |
|---|
| 1192 | | } |
|---|
| 1193 | | } |
|---|
| 1194 | | |
|---|
| 1195 | | if(timeout > 0.0) { |
|---|
| 1196 | | if(wi_socket_wait_descriptor(sd, timeout, true, false) != WI_SOCKET_READY) |
|---|
| 1197 | | goto err; |
|---|
| 1198 | | } |
|---|
| 1199 | | |
|---|
| 1200 | | if(SSL_accept(ssl) != 1) { |
|---|
| 1201 | | wi_error_set_openssl_error(); |
|---|
| 1202 | | |
|---|
| 1203 | | goto err; |
|---|
| 1204 | | } |
|---|
| 1205 | | } |
|---|
| 1206 | | #endif |
|---|
| 1223 | | #ifdef WI_SSL |
|---|
| | 1230 | if(sd >= 0) |
|---|
| | 1231 | close(sd); |
|---|
| | 1232 | |
|---|
| | 1233 | return NULL; |
|---|
| | 1234 | } |
|---|
| | 1235 | |
|---|
| | 1236 | |
|---|
| | 1237 | |
|---|
| | 1238 | wi_boolean_t wi_socket_accept_tls(wi_socket_t *socket, wi_socket_context_t *context, wi_time_interval_t timeout) { |
|---|
| | 1239 | #ifdef WI_SSL |
|---|
| | 1240 | SSL *ssl = NULL; |
|---|
| | 1241 | struct sockaddr_storage ss; |
|---|
| | 1242 | socklen_t length; |
|---|
| | 1243 | int sd; |
|---|
| | 1244 | |
|---|
| | 1245 | ssl = SSL_new(context->ssl_ctx); |
|---|
| | 1246 | |
|---|
| | 1247 | if(!ssl) { |
|---|
| | 1248 | wi_error_set_openssl_error(); |
|---|
| | 1249 | |
|---|
| | 1250 | goto err; |
|---|
| | 1251 | } |
|---|
| | 1252 | |
|---|
| | 1253 | if(SSL_set_fd(ssl, sd) != 1) { |
|---|
| | 1254 | wi_error_set_openssl_error(); |
|---|
| | 1255 | |
|---|
| | 1256 | goto err; |
|---|
| | 1257 | } |
|---|
| | 1258 | |
|---|
| | 1259 | if(!context->certificate && context->dh) { |
|---|
| | 1260 | if(SSL_set_tmp_dh(ssl, context->dh) != 1) { |
|---|
| | 1261 | wi_error_set_openssl_error(); |
|---|
| | 1262 | |
|---|
| | 1263 | goto err; |
|---|
| | 1264 | } |
|---|
| | 1265 | } |
|---|
| | 1266 | |
|---|
| | 1267 | if(timeout > 0.0) { |
|---|
| | 1268 | if(wi_socket_wait_descriptor(sd, timeout, true, false) != WI_SOCKET_READY) |
|---|
| | 1269 | goto err; |
|---|
| | 1270 | } |
|---|
| | 1271 | |
|---|
| | 1272 | if(SSL_accept(ssl) != 1) { |
|---|
| | 1273 | wi_error_set_openssl_error(); |
|---|
| | 1274 | |
|---|
| | 1275 | goto err; |
|---|
| | 1276 | } |
|---|
| | 1277 | |
|---|
| | 1278 | socket->ssl = ssl; |
|---|
| | 1279 | |
|---|
| | 1280 | return true; |
|---|
| | 1281 | |
|---|
| | 1282 | err: |
|---|