Guest User

server_program_with_ssl

a guest
Mar 12th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.79 KB | None | 0 0
  1.  
  2. #include "header.h"
  3. //#include "osal.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <netinet/in.h>
  8. #include <sys/socket.h>
  9. #include <unistd.h>
  10. #include <arpa/inet.h>
  11. #include <openssl/ssl.h>
  12. #include <openssl/err.h>
  13. #include <openssl/x509.h>
  14.  
  15. /*
  16. #define MAXBUF 1024
  17. #define SERVER_IP  "172.16.20.250"
  18. #define MASK "255.255.255.0"
  19. #define GATWAY  "172.16.20.1"
  20. #define DNS_SERVER "192.168.0.111"
  21. #define SERVER_PORT 7788
  22. */
  23.  
  24. #define MAXBUF 1024
  25. #define SERVER_IP  "127.0.0.1"
  26. #define SERVER_PORT 7788
  27. //#define CAFILE    "./ca.pem"
  28. #define SERVCERT    "./server.pem"
  29. #define SERVERKEY   "./server.key"
  30.  
  31.  
  32.  
  33. int SSL_Read_Write(SSL_CTX *ctx, int fd, int client_sockfd)
  34. {
  35.    
  36.     SSL *ssl;
  37.     char ssl_buf[MAXBUF];
  38.     socklen_t len;
  39.     ssl = SSL_new(ctx);
  40.     //SSL_set_fd(ssl, new_fd);
  41.     SSL_set_fd(ssl, fd);
  42.  
  43.     if (SSL_accept(ssl) == -1)
  44.     {
  45.         perror("SSL_accept");
  46.         //close(new_fd);
  47.         close(fd);
  48.         printf("Shehzad\n");
  49.         goto error;
  50.     }
  51.     printf("SSL_accept success.\n");
  52.     printf("Recv\n");
  53.     bzero(ssl_buf, MAXBUF);
  54.  
  55.     while(1)
  56.     {
  57.         len = SSL_read(ssl, ssl_buf, MAXBUF);
  58.         if(len < 0)
  59.         {
  60.             printf("SSL_read error, errno=%d(%s)\n", errno, strerror(errno));
  61.             ERR_print_errors_fp(stdout);
  62.             exit(-1);
  63.         }
  64.  
  65.         if(0 == len)
  66.         {
  67.             continue;
  68.         }
  69.  
  70.     break;
  71.     }
  72.     printf("SSL_read %d bytes: %s\n", len, ssl_buf);
  73.    
  74.     printf("serving client on fd %d\n", fd);
  75.  
  76.     bzero(ssl_buf, MAXBUF);
  77.     strcpy(ssl_buf, "Hello Client: This is ssl server ,i have received you msg!\n");
  78.     printf("send\n");
  79.     len = SSL_write(ssl, ssl_buf, strlen(ssl_buf));
  80.     if (len <= 0)
  81.     {
  82.         printf ("SSL_write error, errno=%d(%s)\n",
  83.               errno, strerror(errno));
  84.         goto error;
  85.     }
  86.  
  87.     printf("SSL_write %d bytes: %s\n", len, ssl_buf);
  88.  
  89.  
  90.     error:
  91.     SSL_shutdown(ssl);
  92.     SSL_free(ssl);
  93.     //  close(new_fd);
  94. //  close(client_sockfd);
  95.     printf("Inside Got\n");
  96.  
  97.     return 0;
  98.  
  99. }
  100.  
  101.  
  102. int ssl_listen(SSL_CTX *ctx, int sockfd)
  103. {
  104.     struct sockaddr_in client;
  105.     char ssl_buf[MAXBUF];
  106.     SSL *ssl;
  107.     int new_fd;
  108.     int server_sockfd, client_sockfd;
  109.     struct sockaddr_in client_address;
  110.     int result;
  111.     int server_len, client_len;
  112.     fd_set readfds, testfds; //Added by Srini
  113.     char recv[] ="Recv";
  114.     char send[] ="Send";
  115.     char accep[] ="Accept";
  116.     socklen_t len;
  117.    
  118.     /*
  119.     printf("Accept\n");
  120.     len = sizeof(struct sockaddr);
  121.     if ((new_fd = accept(sockfd, (struct sockaddr *)&client,&len)) == -1)
  122.     {
  123.        perror("accept");
  124.        exit(1);
  125.     }
  126.  
  127.     printf("accept from %s, port %d, socket %d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port), new_fd);
  128.     */
  129.     printf("sockfd = %d\n", sockfd);
  130.     server_sockfd = sockfd;
  131.  
  132.     FD_ZERO(&readfds);
  133.     FD_SET(server_sockfd, &readfds);
  134.  
  135.     printf("server_sockfd = %d\n", server_sockfd);
  136.     printf("After set\n");
  137.     /*  Now wait for clients and requests.
  138.     Since we have passed a null pointer as the timeout parameter, no timeout will occur.
  139.     The program will exit and report an error if select returns a value of less than 1.  */
  140.  
  141.     int counter;
  142.     counter = 0;
  143.     while(1)
  144.     {
  145.         char ch;
  146.         int fd;
  147.         int nread;
  148.  
  149.         counter++;
  150.         printf("counter = %d\n", counter);
  151.  
  152.         testfds = (fd_set)readfds;
  153.  
  154.         printf("server waiting\n");
  155.  
  156.     //  printf("server_sockfd_while = %d\n", server_sockfd);
  157.  
  158.           printf("readfds = %d\n",readfds);
  159.           printf("testfds = %d\n", testfds);
  160.           printf("server_sockfd=%d\n", server_sockfd);
  161.  
  162.         printf("server_sockfd_after = %d\n", server_sockfd);
  163.         result = select(FD_SETSIZE, &testfds, (fd_set *)0,
  164.             (fd_set *)0, (struct timeval *) 0);
  165.         printf("result = %d\n", result);
  166.  
  167.         if(result < 1) {
  168.             perror("server5");
  169.             exit(1);
  170.         }
  171.  
  172.         /*  Once we know we've got activity,
  173.         we find which descriptor it's on by checking each in turn using FD_ISSET.  */
  174.  
  175.         for(fd = 0; fd < FD_SETSIZE; fd++)
  176.         {
  177.             if(FD_ISSET(fd,&testfds))
  178.             {
  179.  
  180.                 //printf("readfds = %d\tserver_sockfd=%d\ttestfds = %d\tfd = %d\n",readfds, server_sockfd, testfds, fd);
  181.  
  182.         /*  If the activity is on server_sockfd, it must be a request for a new connection
  183.         and we add the associated client_sockfd to the descriptor set.  */
  184.  
  185.                 if(fd == server_sockfd)
  186.                 {
  187.                     client_len = sizeof(client_address);
  188.                     client_sockfd = accept(server_sockfd,
  189.                     (struct sockaddr *)&client_address, &client_len);
  190.                     FD_SET(client_sockfd, &readfds);
  191.                     printf("adding client on fd %d\n", client_sockfd);
  192.                 }
  193.  
  194.                 /*  If it isn't the server, it must be client activity.
  195.                 If close is received, the client has gone away and we remove it from the descriptor set.
  196.                 Otherwise, we 'serve' the client as in the previous examples.  */
  197.  
  198.                 else
  199.                 {
  200.                         ioctl(fd, FIONREAD, &nread);
  201.                     printf("nread = %d\n", nread);
  202.  
  203.                         if(nread == 0)
  204.                     {
  205.                         close(fd);
  206.                         FD_CLR(fd, &readfds);
  207.                         printf("removing client on fd %d\n", fd);
  208.                         }      
  209.  
  210.                         else
  211.                     {
  212.  
  213.                         /*             
  214.                         read(fd, &ch, 1);
  215.                         sleep(5);
  216.                         printf("serving client on fd %d\n", fd);
  217.                         ch++;
  218.                         write(fd, &ch, 1);
  219.                         */
  220.  
  221.                         SSL_Read_Write(ctx, fd, client_sockfd);
  222.                         /*
  223.                         ssl = SSL_new(ctx);
  224.                         //SSL_set_fd(ssl, new_fd);
  225.                         SSL_set_fd(ssl, fd);
  226.  
  227.                         if (SSL_accept(ssl) == -1)
  228.                         {
  229.                             perror("SSL_accept");
  230.                             //close(new_fd);
  231.                             close(fd);
  232.                             printf("Shehzad\n");
  233.                             goto error;
  234.                         }
  235.                         printf("SSL_accept success.\n");
  236.                         printf("Recv\n");
  237.                         bzero(ssl_buf, MAXBUF);
  238.  
  239.                         while(1)
  240.                         {
  241.                             len = SSL_read(ssl, ssl_buf, MAXBUF);
  242.                             if(len < 0)
  243.                             {
  244.                                 printf("SSL_read error, errno=%d(%s)\n", errno, strerror(errno));
  245.                                 ERR_print_errors_fp(stdout);
  246.                                 exit(-1);
  247.                             }
  248.  
  249.                             if(0 == len)
  250.                             {
  251.                                 continue;
  252.                             }
  253.  
  254.                         break;
  255.                         }
  256.                         printf("SSL_read %d bytes: %s\n", len, ssl_buf);
  257.                        
  258.                         printf("serving client on fd %d\n", fd);
  259.  
  260.                         bzero(ssl_buf, MAXBUF);
  261.                         strcpy(ssl_buf, "Hello Client: This is ssl server ,i have received you msg!\n");
  262.                         printf("send\n");
  263.                         len = SSL_write(ssl, ssl_buf, strlen(ssl_buf));
  264.                         if (len <= 0)
  265.                         {
  266.                             printf ("SSL_write error, errno=%d(%s)\n",
  267.                                   errno, strerror(errno));
  268.                             goto error;
  269.                         }
  270.  
  271.                         printf("SSL_write %d bytes: %s\n", len, ssl_buf);
  272.                         */
  273.                         }
  274.                 }
  275.             }
  276.         }
  277.     }
  278.  
  279. /*
  280.     error:
  281.     SSL_shutdown(ssl);
  282.     SSL_free(ssl);
  283. //  close(new_fd);
  284.     close(client_sockfd);
  285.     printf("Inside Got\n");
  286. */
  287.     return 0;
  288. }
  289.  
  290.  
  291. //static int SSL_CTX_use_PrivateKey_file_pass(SSL_CTX *ctx,char *filename,char *pass)
  292. //{
  293. //  EVP_PKEY *pkey=NULL;
  294. //  BIO   *key=NULL;
  295. //
  296. //  key=BIO_new(BIO_s_file());
  297. //  BIO_read_filename(key,filename);
  298. //  pkey=PEM_read_bio_PrivateKey(key,NULL,NULL,pass);
  299. //  if(pkey==NULL)
  300. //  {
  301. //      printf("PEM_read_bio_PrivateKey err");
  302. //      return -1;
  303. //  }
  304. //  if (SSL_CTX_use_PrivateKey(ctx,pkey) <= 0)
  305. //  {
  306. //      printf("SSL_CTX_use_PrivateKey err\n");
  307. //      return -1;
  308. //  }
  309. //  BIO_free(key);
  310. //  return 1;
  311. //}
  312.  
  313. int server_listen()
  314. {
  315.    int sockfd;
  316.    struct sockaddr_in server;
  317.    SSL_CTX *ctx;
  318.    SSL_library_init();
  319.    OpenSSL_add_all_algorithms();
  320.    SSL_load_error_strings();
  321.  
  322.    ctx = SSL_CTX_new(SSLv23_server_method());
  323.    if (ctx == NULL)
  324.    {
  325.         ERR_print_errors_fp(stdout);
  326.         printf("SSL_CTX_new err\n");
  327.         exit(-1);
  328.    }
  329.  
  330. //   SSL_CTX_set_quiet_shutdown(ctx,1);//�رպ�֪ͨ�Է�
  331.  
  332.  
  333.    //  SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,NULL);//�Ƿ���Ҫ��֤�Է���ȱʡSSL_VERIFY_NONE����Ҫ��֤��SSL_VERIFY_PEER����Ҫ��֤�Է���wgx mask
  334.    //   if ((iret = SSL_CTX_load_verify_locations(ctx,CAFILE,NULL)) == 0)//��Ҫ��֤�Է��ͷ���CA֤��,����SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,NULL)���͸ú����ɶԳ���,wgx mask
  335.    //   {
  336.    //     printf("SSL_CTX_load_verify_locations err: %d\n", iret);
  337.    //     exit(1);
  338.    //   }
  339.  
  340.    if(SSL_CTX_use_certificate_file(ctx, SERVCERT, SSL_FILETYPE_PEM) <= 0)//�����Լ���֤���ļ�,���������й�Կ
  341.    {
  342.         ERR_print_errors_fp(stdout);
  343.         printf("SSL_CTX_use_certificate_file err\n");
  344.         exit(-1);
  345.    }
  346.  
  347.  
  348.    SSL_CTX_set_default_passwd_cb_userdata(ctx,"123456");
  349.    SSL_CTX_use_PrivateKey_file(ctx,SERVCERT,SSL_FILETYPE_PEM);//ֱ�Ӵ�pem�ļ��ж�ȡ˽Կ,������ֱ�Ӵ�key�ļ��ж�ȡ,����2������,�������汻���ε����ݼ���
  350.  
  351. //   if(SSL_CTX_use_PrivateKey_file_pass(ctx, SERVERKEY, "123456") < 0) //����˽Կ,��key�ļ��ж�ȡ˽Կ,��Ϊ������֤��ʱ�����õ���Կ��123456,���Դ˴�����Կ��123456,��ͬ��֤�����ܴ˴���Կ�᲻һ��
  352. //   {
  353. //      ERR_print_errors_fp(stdout);
  354. //      printf("SSL_CTX_use_PrivateKey_file_pass err\n");
  355. //      exit(-1);
  356. //   }
  357.  
  358.     if (!SSL_CTX_check_private_key(ctx))//����֤����˽Կ�Ƿ�ƥ��
  359.     {
  360.         printf("SSL_CTX_check_private_key err\n");
  361.         ERR_print_errors_fp(stdout);
  362.         exit(-1);
  363.     }
  364.  
  365. //   SSL_CTX_set_cipher_list(ctx,"RC4-MD5");//ѡ����ȫͨ��Э�� ��wgx mask
  366.  
  367.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  368.     {
  369.         perror("socket");
  370.         exit(-1);
  371.     }
  372.  
  373.     bzero(&server, sizeof(server));
  374.     server.sin_family = PF_INET;
  375.     server.sin_port = htons(SERVER_PORT);
  376.     server.sin_addr.s_addr = INADDR_ANY;
  377.  
  378.     if (bind(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1)
  379.     {
  380.         perror("bind");
  381.         exit(-1);
  382.     }
  383.  
  384.     if (listen(sockfd, 2) == -1) {
  385.         perror("listen");
  386.         exit(-1);
  387.     }
  388.  
  389.     ssl_listen(ctx,sockfd);
  390.     close(sockfd);
  391.     SSL_CTX_free(ctx);
  392.  
  393.     return 0;
  394. }
  395.  
  396. int SslServer()
  397. {
  398.  
  399.     printf("SSL Server\n");
  400.  
  401.     printf("SSL Listen\n");
  402.  
  403.     server_listen();
  404.  
  405.     return 0;
  406. }
  407.  
  408. int main()
  409. {
  410.     int ret;
  411.     SslServer();
  412.     usleep(100*2000);
  413.     return 0;
  414. }
Add Comment
Please, Sign In to add comment