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,-";
33 #endif
34 
35 void
fatal_error(const char * r)36 fatal_error(	const char*	r)
37 {
38 	perror(r);
39 	exit(0);
40 }
41 
42 int
main(int argc,char ** argv)43 main(	int	argc __attribute__((unused)),
44 	char**	argv)
45 {
46 	char	client_key[] = "../SSL/client-key.pem",	client_cert[] = "../SSL/client-cert.pem";
47 	char	ca_file[] = "../SSL/cacert.pem",	*ca_path = 0, *cipher=0;
48 	struct st_VioSSLFd* ssl_connector= 0;
49 	struct sockaddr_in sa;
50 	Vio* client_vio=0;
51 	int err;
52 	char	xbuf[100]="Ohohhhhoh1234";
53         enum enum_ssl_init_error ssl_init_error;
54         unsigned long ssl_error;
55 
56 	MY_INIT(argv[0]);
57         DBUG_PROCESS(argv[0]);
58         DBUG_PUSH(default_dbug_option);
59 
60 	printf("Client key/cert : %s/%s\n", client_key, client_cert);
61 	if (ca_file!=0)
62 		printf("CAfile          : %s\n", ca_file);
63 	if (ca_path!=0)
64 		printf("CApath          : %s\n", ca_path);
65 
66 	ssl_connector = new_VioSSLConnectorFd(client_key, client_cert, ca_file, ca_path, cipher,
67                                               &ssl_init_error);
68 	if(!ssl_connector) {
69                  fatal_error("client:new_VioSSLConnectorFd failed");
70 	}
71 
72 	/* ----------------------------------------------- */
73 	/* Create a socket and connect to server using normal socket calls. */
74 
75 	client_vio = vio_new(socket (AF_INET, SOCK_STREAM, 0), VIO_TYPE_TCPIP, TRUE);
76 
77 	memset (&sa, '\0', sizeof(sa));
78 	sa.sin_family      = AF_INET;
79 	sa.sin_addr.s_addr = inet_addr ("127.0.0.1");   /* Server IP */
80 	sa.sin_port        = htons     (1111);          /* Server Port number */
81 
82 	err = connect(client_vio->sd, (struct sockaddr*) &sa,
83 		sizeof(sa));
84 
85 	/* ----------------------------------------------- */
86 	/* Now we have TCP conncetion. Start SSL negotiation. */
87 	read(client_vio->sd,xbuf, sizeof(xbuf));
88         sslconnect(ssl_connector,client_vio,60L,&ssl_error);
89 	err = vio_read(client_vio,xbuf, sizeof(xbuf));
90 	if (err<=0) {
91 		my_free(ssl_connector);
92 		fatal_error("client:SSL_read");
93 	}
94 	xbuf[err] = 0;
95 	printf("client:got %s\n", xbuf);
96 	my_free(client_vio);
97 	my_free(ssl_connector);
98 	return 0;
99 }
100 #else /* HAVE_OPENSSL */
101 
main()102 int main() {
103 return 0;
104 }
105 #endif /* HAVE_OPENSSL */
106