C:从 OpenSSL 套接字读取

标签 c linux ssl networking openssl

我正在使用 OpenSSL 库来保护网络消息,但出于某种原因,它似乎并不总是有效。其实大部分时间是行不通的。当我运行编译后的代码并连接到套接字时,大多数时候它也只是在子进程中运行主进程的代码,但有时它会运行子指令。很明显,这不是它应该的工作方式, child 应该在它一直处理客户端 (handle_client(newfd)) 之后退出。一个有趣的部分是,如果我从子指令中删除 handle_client(newfd) 行并在其中放一些小东西,比如 printf("test"),那么子指令就会工作每次它应该打印测试并在那之后立即退出。这是 fork() 中的某种限制,还是我不应该在 child 身上运行这么多代码?或者是其他东西?非常感谢任何帮助!

主.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/wait.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include "json.h"
#include "create_socket.h"
#include "get_addr.h"
#include "handle_income.h"
#include "handle_client.h"
int main(void) {
  int newfd;
  struct sockaddr_storage their_addr;
  char s[INET6_ADDRSTRLEN];
  pid_t pid;
  unsigned int cpc = 0;
  int listenfd = create_socket("8069");
  if (listenfd < 0)
    exit(1);
  while(1) {
    socklen_t sin_size = sizeof their_addr;
    if ((newfd = accept(listenfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
      continue;
    inet_ntop(their_addr.ss_family,
      get_addr((struct sockaddr *)&their_addr), s, sizeof s);
    printf("conn %s siz %d\n", s, (int) sin_size); //REMOVE
    if ((pid = fork()) < 0) {
      exit(1);
    } else if (pid == 0) {
      close(listenfd);
      handle_client(newfd);
      exit(0);
    }
    printf("child %d\n", (int) pid); //REMOVE
    cpc++;
    while(cpc) {
      pid = waitpid((pid_t) -1, NULL, WNOHANG);
      if (pid < 0)
        exit(1);
      else if (pid == 0)
        break;
      else
        cpc--;
    }
  }
  EVP_cleanup();
  exit(0);
}

handle_client.h:

#define READ_SIZE 32
void handle_client(int newfd) {
  char *buffer = NULL;
  char *tmp_buffer = malloc(READ_SIZE);
  unsigned long buffer_size = 0;
  unsigned long received = 0;
  int status = 0;
  SSL_load_error_strings();
  OpenSSL_add_all_algorithms();
  SSL_CTX *sslctx = SSL_CTX_new(SSLv23_server_method());
  if (sslctx) {
    SSL_CTX_set_ecdh_auto(sslctx, 1);
    if ((SSL_CTX_use_certificate_file(sslctx, "/ssl-cert.pem", SSL_FILETYPE_PEM)) > 0) {
      if ((SSL_CTX_use_PrivateKey_file(sslctx, "/ssl-key.pem", SSL_FILETYPE_PEM)) > 0) {
        SSL *ssl = SSL_new(sslctx);
        SSL_set_fd(ssl, newfd);
        if (SSL_accept(ssl) > 0) {
          fcntl(newfd, F_SETFL, fcntl(newfd, F_GETFL, 0) | O_NONBLOCK);
          do {
            if (received >= buffer_size) {
              char *tmp;
              buffer_size += READ_SIZE;
              if ((tmp = realloc(buffer, buffer_size)) == NULL) {
                break;
              } else {
                buffer = tmp;
              }
            }
            status = SSL_read(ssl, tmp_buffer, READ_SIZE);
            if (status > 0) {
              received += status;
              strncat(buffer, tmp_buffer, status);
            } else {
              ERR_print_errors_fp(stderr);
            }
          } while (status > 0);
          free(tmp_buffer);
          buffer[received] = 0;
          if (received < buffer_size) {
            buffer = realloc(buffer, received);
          }
          printf("%s\n", buffer); //REMOVE
          char *response = handle_income(buffer);
          SSL_write(ssl, response, strlen(response));
          printf("%s\n", response); //REMOVE
        }
        SSL_free(ssl);
      }
    }
  }
  SSL_CTX_free(sslctx);
  close(newfd);
}

最佳答案

可能还有其他问题,但你应该修复以下缓冲区溢出错误,并检查它是否也修复了可见问题:

SSL_read 可能返回小于 READ_SIZE。所以下一段代码就坏了。 例如,当第一个 SSL_read() 返回 16,而下一个调用返回 32 (=READ_SIZE) 时,就会出现问题。在第二次 strncat 调用期间分配的 buffer 大小为 32,因此在 strncat() 期间可能会发生缓冲区溢出。

        if (received >= buffer_size) {
          char *tmp;
          buffer_size += READ_SIZE;
          if ((tmp = realloc(buffer, buffer_size)) == NULL) {
            break;
          } else {
            buffer = tmp;
          }
        }
        status = SSL_read(ssl, tmp_buffer, READ_SIZE);
        if (status > 0) {
          received += status;
          strncat(buffer, tmp_buffer, status);
        }
        ...

strncat 调用可能存在其他问题。它需要一个额外的字节作为字符串的空终止符。来自手册页:

the size of dest must be at least strlen(dest)+n+1

关于C:从 OpenSSL 套接字读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52598841/

相关文章:

c - 如何在 C 中正确使用 %s?

linux - 如何在unix系统中连接两个文件的列

c# - 如何在 Asp.Net Web API 上实现 Https/SSL 连接?

java - 客户端和服务器的 SSL 握手失败

ssl - 为 SSL 配置 Mamp Pro(非自签名)

c - st_mtimensec 在 struct stat 中不可用

c - 在 C 中从位域形成并集是否可能且正确?

c++ - 轴的欧拉序列是否干扰四元数结果

linux - shell 引用行为

linux - 在 linux 日志文件中特定事件后获取行