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