You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.8 KiB

  1. /* Copyright (C) 2000 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. #include <my_global.h>
  13. #ifdef HAVE_OPENSSL
  14. #include <my_sys.h>
  15. #include <m_string.h>
  16. #include <m_ctype.h>
  17. #include "mysql.h"
  18. #include "errmsg.h"
  19. #include <my_dir.h>
  20. #include <my_getopt.h>
  21. #include <signal.h>
  22. #include <violite.h>
  23. const char *VER="0.2";
  24. #ifndef DBUG_OFF
  25. const char *default_dbug_option="d:t:O,/tmp/viotest-ssl.trace";
  26. #endif
  27. void
  28. fatal_error(const char *r)
  29. {
  30. perror(r);
  31. exit(0);
  32. }
  33. void
  34. print_usage()
  35. {
  36. printf("viossl-test: testing SSL virtual IO. Usage:\n");
  37. printf("viossl-test server-key server-cert client-key client-cert [CAfile] [CApath]\n");
  38. }
  39. int main(int argc, char **argv)
  40. {
  41. char* server_key = 0;
  42. char* server_cert = 0;
  43. char* client_key = 0;
  44. char* client_cert = 0;
  45. char* ca_file = 0;
  46. char* ca_path = 0;
  47. int child_pid,sv[2];
  48. struct st_VioSSLAcceptorFd* ssl_acceptor=0;
  49. struct st_VioSSLConnectorFd* ssl_connector=0;
  50. Vio* client_vio=0;
  51. Vio* server_vio=0;
  52. MY_INIT(argv[0]);
  53. DBUG_PROCESS(argv[0]);
  54. DBUG_PUSH(default_dbug_option);
  55. if (argc<5)
  56. {
  57. print_usage();
  58. return 1;
  59. }
  60. server_key = argv[1];
  61. server_cert = argv[2];
  62. client_key = argv[3];
  63. client_cert = argv[4];
  64. if (argc>5)
  65. ca_file = argv[5];
  66. if (argc>6)
  67. ca_path = argv[6];
  68. printf("Server key/cert : %s/%s\n", server_key, server_cert);
  69. printf("Client key/cert : %s/%s\n", client_key, client_cert);
  70. if (ca_file!=0)
  71. printf("CAfile : %s\n", ca_file);
  72. if (ca_path!=0)
  73. printf("CApath : %s\n", ca_path);
  74. if (socketpair(PF_UNIX, SOCK_STREAM, IPPROTO_IP, sv)==-1)
  75. fatal_error("socketpair");
  76. ssl_acceptor = new_VioSSLAcceptorFd(server_key, server_cert, ca_file,
  77. ca_path);
  78. ssl_connector = new_VioSSLConnectorFd(client_key, client_cert, ca_file,
  79. ca_path);
  80. client_vio = (Vio*)my_malloc(sizeof(struct st_vio),MYF(0));
  81. client_vio->sd = sv[0];
  82. sslconnect(ssl_connector,client_vio);
  83. server_vio = (Vio*)my_malloc(sizeof(struct st_vio),MYF(0));
  84. server_vio->sd = sv[1];
  85. sslaccept(ssl_acceptor,server_vio);
  86. printf("Socketpair: %d , %d\n", client_vio->sd, server_vio->sd);
  87. child_pid = fork();
  88. if (child_pid==-1)
  89. {
  90. my_free((gptr)ssl_acceptor,MYF(0));
  91. my_free((gptr)ssl_connector,MYF(0));
  92. fatal_error("fork");
  93. }
  94. if (child_pid==0)
  95. {
  96. /* child, therefore, client */
  97. char xbuf[100];
  98. int r = vio_ssl_read(client_vio,xbuf, sizeof(xbuf));
  99. if (r<=0) {
  100. my_free((gptr)ssl_acceptor,MYF(0));
  101. my_free((gptr)ssl_connector,MYF(0));
  102. fatal_error("client:SSL_read");
  103. }
  104. xbuf[r] = 0;
  105. printf("client:got %s\n", xbuf);
  106. my_free((gptr)client_vio,MYF(0));
  107. my_free((gptr)ssl_acceptor,MYF(0));
  108. my_free((gptr)ssl_connector,MYF(0));
  109. sleep(1);
  110. }
  111. else
  112. {
  113. const char* s = "Huhuhuh";
  114. int r = vio_ssl_write(server_vio,(gptr)s, strlen(s));
  115. if (r<=0) {
  116. my_free((gptr)ssl_acceptor,MYF(0));
  117. my_free((gptr)ssl_connector,MYF(0));
  118. fatal_error("server:SSL_write");
  119. }
  120. my_free((gptr)server_vio,MYF(0));
  121. my_free((gptr)ssl_acceptor,MYF(0));
  122. my_free((gptr)ssl_connector,MYF(0));
  123. sleep(1);
  124. }
  125. return 0;
  126. }
  127. #else /* HAVE_OPENSSL */
  128. int main() {
  129. return 0;
  130. }
  131. #endif /* HAVE_OPENSSL */