/* * This file is part of the sslsosk project. * Copyright 2023 Edward V. Emelianov . * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include // pthread_kill #include #include #include #include #include "cmdlnopts.h" #include "sslsock.h" #ifdef SERVER #include "io.h" #include "motors.h" #include "server.h" #else #include "client.h" #endif #ifdef SERVER static int OpenConn(int port){ int sd = socket(PF_INET, SOCK_STREAM, 0); if(sd < 0){ LOGERR("Can't open socket"); ERRX("socket()"); } int enable = 1; // allow reuse of descriptor if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (void *)&enable, sizeof(int)) < 0){ LOGERR("Can't apply SO_REUSEADDR to socket"); ERR("setsockopt()"); } struct sockaddr_in addr = {0}; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = INADDR_ANY; if(bind(sd, (struct sockaddr*)&addr, sizeof(addr))){ LOGWARN("Can't bind port %d", port); ERR("bind()"); } if(listen(sd, BACKLOG)){ LOGWARN("Can't listen()"); ERR("listen()"); } return sd; } #else static int OpenConn(int port){ FNAME(); int sd; struct hostent *host; struct sockaddr_in addr; if((host = gethostbyname(G.serverhost)) == NULL ){ LOGWARN("gethostbyname(%s) error", G.serverhost); ERR("gethostbyname()"); } sd = socket(PF_INET, SOCK_STREAM, 0); DBG("sd=%d", sd); bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); //addr.sin_addr.s_addr = *(long*)(host->h_addr); memcpy(&addr.sin_addr, host->h_addr, host->h_length); if(connect(sd, (struct sockaddr*)&addr, sizeof(addr))){ close(sd); LOGWARN("Can't connect to %s", G.serverhost); ERR("connect()"); } return sd; } #endif static SSL_CTX* InitCTX(void){ const SSL_METHOD *method; SSL_CTX *ctx; //OpenSSL_add_all_algorithms(); //SSL_load_error_strings(); method = #ifdef CLIENT TLS_client_method(); #else TLS_server_method(); #endif ctx = SSL_CTX_new(method); if(!ctx){ LOGWARN("Can't create SSL context"); ERRX("SSL_CTX_new()"); } if(!SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION)){ LOGWARN("Can't set minimum TLS protocol version"); ERRX("SSL_CTX_set_min_proto_version()"); } if(SSL_CTX_load_verify_locations(ctx, G.ca, NULL) != 1){ LOGWARN("Could not set the CA file location\n"); ERRX("Could not set the CA file location\n"); } #ifdef SERVER STACK_OF(X509_NAME) *cert_names = SSL_load_client_CA_file(G.ca); if(!cert_names){ LOGWARN("Could not load client CA file %s", G.ca); ERRX("SSL_load_client_CA_file()"); } SSL_CTX_set_client_CA_list(ctx, cert_names); #endif if(SSL_CTX_use_certificate_file(ctx, G.cert, SSL_FILETYPE_PEM) <= 0){ ERR_print_errors_fp(stderr); LOGWARN("Can't use SSL certificate %s", G.cert); ERRX("Can't use SSL certificate %s", G.cert); } if(SSL_CTX_use_PrivateKey_file(ctx, G.key, SSL_FILETYPE_PEM) <= 0){ ERR_print_errors_fp(stderr); LOGWARN("Can't use SSL key %s", G.key); ERRX("Can't use SSL key %s", G.key); } if(!SSL_CTX_check_private_key(ctx)){ ERR_print_errors_fp(stderr); LOGWARN("Private key does not match the public certificate\n"); ERRX("Private key does not match the public certificate\n"); } // don't need as socket would be non-blocking // SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY); #ifdef SERVER SSL_CTX_set_verify(ctx, // Specify that we need to verify the client as well SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL); #else SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); #endif SSL_CTX_set_verify_depth(ctx, 1); // We accept only certificates signed by the CA himself return ctx; } int open_socket(){ FNAME(); SSL_library_init(); SSL_CTX *ctx = InitCTX(); int fd = OpenConn(atoi(G.port)); #ifdef SERVER if(G.emulmode) set_emulation_mode(); if(!motors_open(G.serialpath, G.serialspeed)){ LOGERR("Can't open %s @%d", G.serialpath, G.serialspeed); WARNX("Can't open %s @%d", G.serialpath, G.serialspeed); close(fd); SSL_CTX_free(ctx); return 1; } DBG("Motors opened: Stop them"); motors_stop(); DBG("Inhibit"); io_set_relays(0); // turn off all relays io_relay_on(RELAY_BLOCK_MOTORS); // turn on locking relay serverproc(ctx, fd); #else if(G.terminal) terminal(ctx, fd); else clientproc(ctx, fd); #endif close(fd); SSL_CTX_free(ctx); return 0; } static int geterrcode(SSL *ssl, int errcode){ int sslerr = SSL_get_error(ssl, errcode); DBG("ERR: %d", sslerr); if(SSL_ERROR_WANT_READ == sslerr || SSL_ERROR_WANT_WRITE == sslerr) return 0; // empty call // don't log "zero return" when client disconnected if(sslerr == SSL_ERROR_ZERO_RETURN) return -1; int sd = SSL_get_fd(ssl); LOGERR("SSL error %d @client %d", sslerr, sd); WARNX("SSL error %d @client %d", sslerr, sd); return -1; } /** * @brief read_string - read '\n'-terminated string from SSL * @param ssl - SSL * @param buf - buffer for text * @param l - max buf length (including zero) * @return amount of bytes read or -1 if client disconnected */ int read_string(SSL *ssl, char *buf, int l){ if(!ssl || l < 1) return 0; int bytes = SSL_peek(ssl, buf, l); //DBG("Peek: %d", bytes); if(bytes < 1){ // nothing to read or error return geterrcode(ssl, bytes); } char *eol = memchr(buf, '\n', bytes); if(!eol){ // not found: overflow or no enough symbols if(bytes == l){ // oops: overflow, thow it out SSL_read(ssl, buf, l); } return 0; } l = eol - buf + 1; char *ptr = buf; int total = 0; while(l){ bytes = SSL_read(ssl, ptr, l); //DBG("Read: %d", bytes); if(bytes <= 0){ // error return geterrcode(ssl, bytes); } l -= bytes; ptr += bytes; total += bytes; } ptr[-1] = 0; // replace '\n' with 0 return total; } /** * @brief write_string - write '\n'-terminated string * @param ssl - SSL * @param buf - zero-terminated buffer with text (last symbol should be '\n') * @return FALSE in case of fail or timeout (1s) */ int write_string(SSL *ssl, char *buf){ double t0 = sl_dtime(); int l = strlen(buf); while(sl_dtime() - t0 < G.acc_timeout && l > 0){ int written = SSL_write(ssl, buf, l); if(written <= 0){ int err = SSL_get_error(ssl, written); if(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE){ // try next time usleep(10000); continue; } return FALSE; // error } buf += written; l -= written; if(l) usleep(10000); } if(l > 0) return FALSE; return TRUE; }