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