码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-08 09:21:01      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

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. }

转自:http://hoytluo.blog.51cto.com/1154435/564822/

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

标签:

原文地址:http://www.cnblogs.com/duxp/p/4486698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!