使用openssl编写服务端和客户端程序

1.使用相同的ca生成两个证书,一个是server.cer,一个是client.cer,注意生成server.cer的时候必须指明证书可以用于服务端的。

服务器代码:

 
 
  1. #include "openssl/bio.h"
  2. #include "openssl/ssl.h"
  3. #include "openssl/err.h"
  4. #include <cutil.h>
  5. #define EXIT_IF_TRUE(x) if (x) \
  6. do { \
  7. fprintf(stderr, "Check '%s' is true\n", #x); \
  8. ERR_print_errors_fp(stderr); \
  9. exit(2); \
  10. }while(0)
  11. int main(int argc, char **argv)
  12. {
  13. SSL_CTX *ctx;
  14. SSL *ssl;
  15. X509 *client_cert;
  16. char szBuffer[1024];
  17. int nLen;
  18. struct sockaddr_in addr;
  19. int len;
  20. int nListenFd, nAcceptFd;
  21. // 初始化
  22. cutil_init();
  23. cutil_log_set_level(LOG_ALL);
  24. cutil_log_set_stderr(1);
  25. SSLeay_add_ssl_algorithms();
  26. OpenSSL_add_all_algorithms();
  27. SSL_load_error_strings();
  28. ERR_load_BIO_strings();
  29. // 我们使用SSL V3,V2
  30. EXIT_IF_TRUE((ctx = SSL_CTX_new (SSLv23_method())) == NULL);
  31. // 要求校验对方证书
  32. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
  33. // 加载CA的证书
  34. EXIT_IF_TRUE (!SSL_CTX_load_verify_locations(ctx, "cacert.cer", NULL));
  35. // 加载自己的证书
  36. EXIT_IF_TRUE (SSL_CTX_use_certificate_file(ctx, "server.cer", SSL_FILETYPE_PEM) <= 0) ;
  37. // 加载自己的私钥
  38. EXIT_IF_TRUE (SSL_CTX_use_PrivateKey_file(ctx, "server.key", SSL_FILETYPE_PEM) <= 0) ;
  39. // 判定私钥是否正确
  40. EXIT_IF_TRUE (!SSL_CTX_check_private_key(ctx));
  41. // 创建并等待连接
  42. nListenFd = cutil_socket_new(SOCK_STREAM);
  43. cutil_socket_bind(nListenFd, NULL, 8812, 1);
  44. memset(&addr, 0, sizeof(addr));
  45. len = sizeof(addr);
  46. nAcceptFd = accept(nListenFd, (struct sockaddr *)&addr, (size_t *)&len);
  47. cutil_log_debug("Accept a connect from [%s:%d]\n",
  48. inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
  49. // 将连接付给SSL
  50. EXIT_IF_TRUE( (ssl = SSL_new (ctx)) == NULL);
  51. SSL_set_fd (ssl, nAcceptFd);
  52. EXIT_IF_TRUE( SSL_accept (ssl) != 1);
  53. // 进行操作
  54. memset(szBuffer, 0, sizeof(szBuffer));
  55. nLen = SSL_read(ssl,szBuffer, sizeof(szBuffer));
  56. fprintf(stderr, "Get Len %d %s ok\n", nLen, szBuffer);
  57. strcat(szBuffer, " this is from server");
  58. SSL_write(ssl, szBuffer, strlen(szBuffer));
  59. // 释放资源
  60. SSL_free (ssl);
  61. SSL_CTX_free (ctx);
  62. close(nAcceptFd);
  63. }
客户端代码
 
 
  1. #include "openssl/bio.h"
  2. #include "openssl/ssl.h"
  3. #include "openssl/err.h"
  4. #include <cutil.h>
  5. #define EXIT_IF_TRUE(x) if (x) \
  6. do { \
  7. fprintf(stderr, "Check '%s' is true\n", #x); \
  8. ERR_print_errors_fp(stderr); \
  9. exit(2); \
  10. }while(0)
  11. int main(int argc, char **argv)
  12. {
  13. SSL_METHOD *meth;
  14. SSL_CTX *ctx;
  15. SSL *ssl;
  16. int nFd;
  17. int nLen;
  18. char szBuffer[1024];
  19. // 初始化
  20. cutil_init();
  21. cutil_log_set_level(LOG_ALL);
  22. cutil_log_set_stderr(1);
  23. SSLeay_add_ssl_algorithms();
  24. OpenSSL_add_all_algorithms();
  25. SSL_load_error_strings();
  26. ERR_load_BIO_strings();
  27. // 我们使用SSL V3,V2
  28. EXIT_IF_TRUE((ctx = SSL_CTX_new (SSLv23_method())) == NULL);
  29. // 要求校验对方证书
  30. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
  31. // 加载CA的证书
  32. EXIT_IF_TRUE (!SSL_CTX_load_verify_locations(ctx, "cacert.cer", NULL));
  33. // 加载自己的证书
  34. EXIT_IF_TRUE (SSL_CTX_use_certificate_file(ctx, "client.cer", SSL_FILETYPE_PEM) <= 0) ;
  35. // 加载自己的私钥
  36. EXIT_IF_TRUE (SSL_CTX_use_PrivateKey_file(ctx, "client.key", SSL_FILETYPE_PEM) <= 0) ;
  37. // 判定私钥是否正确
  38. EXIT_IF_TRUE (!SSL_CTX_check_private_key(ctx));
  39. // 创建连接
  40. nFd = cutil_socket_new(SOCK_STREAM);
  41. if(cutil_socket_connect(nFd, "127.0.0.1", 8812, 30) < 0)
  42. {
  43. cutil_log_error("连接服务器失败\n");
  44. return -1;
  45. }
  46. // 将连接付给SSL
  47. EXIT_IF_TRUE( (ssl = SSL_new (ctx)) == NULL);
  48. SSL_set_fd (ssl, nFd);
  49. EXIT_IF_TRUE( SSL_connect (ssl) != 1);
  50. // 进行操作
  51. sprintf(szBuffer, "this is from client %d", getpid());
  52. SSL_write(ssl, szBuffer, strlen(szBuffer));
  53. // 释放资源
  54. memset(szBuffer, 0, sizeof(szBuffer));
  55. nLen = SSL_read(ssl,szBuffer, sizeof(szBuffer));
  56. fprintf(stderr, "Get Len %d %s ok\n", nLen, szBuffer);
  57. SSL_free (ssl);
  58. SSL_CTX_free (ctx);
  59. close(nFd);
  60. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值