new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_auth / ssl-test.c
1 /* Copyright (C) 2011 Trend Micro Inc. All rights reserved.
2  *
3  * OSSEC HIDS is a free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License (version 2) as
5  * published by the FSF - Free Software Foundation.
6  *
7  * Note that this license applies to the source code, as well as
8  * decoders, rules and any other data file included with OSSEC (unless
9  * otherwise specified).
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
13  * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
14  * NON-INFRINGEMENT.  See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * In addition, as a special exception, the copyright holders give
20  * permission to link the code of portions of this program with the
21  * OpenSSL library under certain conditions as described in each
22  * individual source file, and distribute linked combinations
23  * including the two.
24  *
25  * You must obey the GNU General Public License in all respects
26  * for all of the code used other than OpenSSL.  If you modify
27  * file(s) with this exception, you may extend this exception to your
28  * version of the file(s), but you are not obligated to do so.  If you
29  * do not wish to do so, delete this exception statement from your
30  * version.  If you delete this exception statement from all source
31  * files in the program, then also delete it here.
32  *
33  */
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <sys/param.h>
39 #include <sys/wait.h>
40 #include <sys/select.h>
41 #include <sys/utsname.h>
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <stdarg.h>
47 #include <fcntl.h>
48 #include <dirent.h>
49 #include <ctype.h>
50 #include <signal.h>
51
52 #include <netdb.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <sys/socket.h>
56 #include <sys/un.h>
57
58 #include <openssl/ssl.h>
59 #include <openssl/err.h>
60 #include <openssl/bio.h>
61
62 #define TEST "GET / HTTP/1.0\r\n\r\n\r\n"
63
64
65 int main(int argc, char **argv)
66 {
67     int c;
68     int sock = 0, portnum, ret = 0;
69     char *host = NULL, *port = "443";
70     SSL_CTX *ctx;
71     SSL *ssl;
72     SSL_METHOD *sslmeth;
73     BIO *sbio;
74     BIO *bio_err = 0;
75
76     while ((c = getopt(argc, argv, "h:p:")) != -1) {
77         switch (c) {
78             case 'h':
79                 host = optarg;
80                 break;
81             case 'p':
82                 portnum = atoi(optarg);
83                 if (portnum <= 0 || portnum >= 65536) {
84                     exit(1);
85                 }
86                 port = optarg;
87                 break;
88             default:
89                 exit(1);
90                 break;
91         }
92     }
93
94     if (!bio_err) {
95         SSL_library_init();
96         SSL_load_error_strings();
97         OpenSSL_add_all_algorithms();
98         bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
99     }
100
101     sslmeth = SSLv23_method();
102     ctx = SSL_CTX_new(sslmeth);
103     if (!ctx) {
104         printf("CTX ERROR\n");
105         exit(1);
106     }
107
108     if (!host) {
109         printf("ERROR - host not set.\n");
110         exit(1);
111     }
112
113     /* Connect via TCP */
114     sock = OS_ConnectTCP(port, host);
115     if (sock <= 0) {
116         printf("connect error\n");
117         exit(1);
118     }
119
120     /* Connect the SSL socket */
121     ssl = SSL_new(ctx);
122     sbio = BIO_new_socket(sock, BIO_NOCLOSE);
123     SSL_set_bio(ssl, sbio, sbio);
124     ret = SSL_connect(ssl);
125     if (ret <= 0) {
126         printf("SSL connect error\n");
127         ERR_print_errors_fp(stderr);
128         exit(1);
129     }
130
131     printf("Connected!\n");
132
133     ret = SSL_write(ssl, TEST, sizeof(TEST));
134     if (ret < 0) {
135         printf("SSL write error\n");
136         ERR_print_errors_fp(stderr);
137         exit(1);
138     }
139
140     while (1) {
141         char buf[2048];
142         ret = SSL_read(ssl, buf, sizeof(buf) - 1);
143         printf("ret: %d\n", ret);
144         switch (SSL_get_error(ssl, ret)) {
145             case SSL_ERROR_NONE:
146                 buf[ret] = '\0';
147                 printf("no error: %s\n", buf);
148                 break;
149             case SSL_ERROR_ZERO_RETURN:
150                 printf("no return\n");
151                 exit(1);
152                 break;
153             case SSL_ERROR_SYSCALL:
154                 fprintf(stderr,
155                         "SSL Error: Premature close\n");
156                 exit(1);
157                 break;
158             default:
159                 printf("default error\n");
160                 exit(1);
161                 break;
162         }
163
164     }
165
166     exit(0);
167 }
168