【问题标题】:OpenSSL communication between client and server客户端和服务器之间的 OpenSSL 通信
【发布时间】:2015-10-21 16:53:12
【问题描述】:

我得到了使用 openSSL 在客户端和服务器之间建立通信的任务: [1] 客户端 --> 服务器:提示要求用户输入数字 X [2] 服务器 --> 客户端:X + 1。 [3] 客户端 --> 检查服务器的回答是否正确,并输出结果。客户端 重复第 1 步。

我已经完成了以下步骤,但是我遇到了分段错误,你们能告诉我哪里出错了

sserver.cc

#include <openssl/ssl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>


int main(int argc, char* argv[])
{
  if (argc != 4) {
    printf("./exec CertFile KeyFile port");
    return -1;
  }

  char* cert_file = argv[1];
  char* key_file = argv[2];
  int port = atoi(argv[3]);

  // init the ssl lib
  SSL_library_init();

  //SSL_METHOD* method;
  //SSL_CTX *ctx;
  OpenSSL_add_all_algorithms();
  SSL_load_error_strings();

  const SSL_METHOD* method = SSLv3_server_method();
  SSL_CTX *ctx = SSL_CTX_new(method);

  // load the server's certificate
  SSL_CTX_use_certificate_file(ctx, cert_file, SSL_FILETYPE_PEM);
  // load the server's private key
  SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM);
  // check the private against the known certificate
  if (!SSL_CTX_check_private_key(ctx)) {
    printf("Private key does not match\n");
    abort();
  }

  // standard tcp server setup and connection
  int sd, client;
  struct sockaddr_in addr;
  sd = socket(PF_INET, SOCK_STREAM, 0);
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = INADDR_ANY;
  bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  listen(sd, 10);
  client = accept(sd, 0, 0);

  SSL* ssl = SSL_new(ctx);
  SSL_set_fd(ssl, client);
  SSL_accept(ssl);

  char buf[1024];
  int buf_size = 1024;
    int ClientResponse,StoredValue;
    int TempValue;

  // real work here
  while(1) {

    // read message from client, plus one, then send back to client
    //ClientResponse=SSL_read(ssl,(void*)StoredValue,3);
    ClientResponse=SSL_read(ssl,(void*)buf,buf_size);
    ClientResponse=ClientResponse+1;
    SSL_write(ssl,(const void*)ClientResponse,buf_size);
  }

  client = SSL_get_fd(ssl);
  SSL_free(ssl);
  close(sd);
}

sclient.cc

#include <openssl/ssl.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
  if (argc != 3) {
    printf("./exec hostname port");
    return -1;
  }

  char* hostname = argv[1];
  int port = atoi(argv[2]);

  // init the ssl lib
  SSL_library_init();
  printf("client...1\n");

  //SSL_METHOD* method;
  SSL_CTX *ctx;
  OpenSSL_add_all_algorithms();
  SSL_load_error_strings();

  const SSL_METHOD* method = SSLv3_client_method();
  ctx = SSL_CTX_new(method);

  // create a standard tcp client
  int server;
  struct hostent* host;
  struct sockaddr_in addr;
  printf("client...2\n");

  host = gethostbyname(hostname);
  server = socket(PF_INET, SOCK_STREAM, 0);
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = *(long*)(host->h_addr);
  printf("client...3\n");

  connect(server, (struct sockaddr*)&addr, sizeof(addr));
  printf("client...4\n");

  SSL* ssl;
  ssl = SSL_new(ctx);
  SSL_set_fd(ssl, server);

  printf("client...5\n");
  int sv = SSL_connect(ssl);
  printf("client...6\n");
  printf("sv = %d\n", sv);
  if (sv != 1) {
    printf("Can't establish ssl connection with server...\n");
    // send a string to
    SSL_free(ssl);
    return -1;
  }
    int UserInput,ServerOutput;
    int StoredValue;
    int TempValue;

  // real work here
  while(1) {

    // 1. ask the user to input a random number, and send to server using SSL library
    printf("Enter a number user:\n");
    scanf("%d",&UserInput);
    TempValue=UserInput;
    SSL_write(ssl,(const void*)UserInput,TempValue);

    // 2. wait for the response from the server

    // 3. Check if the response is correct or not
    ServerOutput=SSL_read(ssl,(void*)StoredValue,TempValue);
    if(ServerOutput==UserInput)
        printf("\nCorrect %d",UserInput);
    else
        printf("\nFalse");
  }

  SSL_free(ssl);
}

【问题讨论】:

    标签: c ssl openssl ssl-certificate


    【解决方案1】:

    你错误地使用了指针。

    在服务器部分代码应该是这样的:

    SSL_read(ssl, &TempValue, sizeof(TempValue));
    TempValue++;
    SSL_write(ssl, &TempValue, sizeof(TempValue));
    

    客户端部分:

    SSL_write(ssl, &UserInput, sizeof(UserInput));
    SSL_read(ssl, &ServerOutput, sizeof(ServerOutput));
    printf("UserInput=%d, ServerOutput=%d\n", UserInput, ServerOutput);
    

    还请阅读文档中应该返回的内容 SSL_readSSL_write

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多