1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | #include <sys/socket.h> |
---|
6 | #include <sys/types.h> |
---|
7 | |
---|
8 | #include <netinet/in.h> |
---|
9 | #include <netdb.h> |
---|
10 | #include <unistd.h> |
---|
11 | #include <fcntl.h> |
---|
12 | |
---|
13 | #include <openssl/ssl.h> |
---|
14 | #include <openssl/err.h> |
---|
15 | #include <openssl/crypto.h> |
---|
16 | #include <openssl/x509.h> |
---|
17 | #include <openssl/pem.h> |
---|
18 | #include <openssl/bio.h> |
---|
19 | |
---|
20 | |
---|
21 | //#define HOST "im.pr.gov.br" |
---|
22 | //#define IP "200.189.113.148" |
---|
23 | #define HOST "localhost" |
---|
24 | #define IP "10.15.20.42" |
---|
25 | //#define HOST "im.celepar.parana" |
---|
26 | //#define IP "10.15.20.219" |
---|
27 | #define PORT "5222" |
---|
28 | |
---|
29 | int tls = 0; |
---|
30 | |
---|
31 | void Read(void * fd, void *buf, int count) |
---|
32 | { |
---|
33 | if ( tls == 0 ) |
---|
34 | read(*(int *) fd, buf, (ssize_t)count); |
---|
35 | else |
---|
36 | SSL_read((SSL *) fd, buf, count); |
---|
37 | } |
---|
38 | |
---|
39 | void Write(void * fd, void *buf, int count) |
---|
40 | { |
---|
41 | if ( tls == 0 ) |
---|
42 | write(*(int *) fd, buf, (ssize_t)count); |
---|
43 | else |
---|
44 | SSL_write((SSL *) fd, buf, count); |
---|
45 | } |
---|
46 | |
---|
47 | int main(void) |
---|
48 | { |
---|
49 | struct sockaddr_in socketaddr; |
---|
50 | int socketfd, |
---|
51 | nHostPort; |
---|
52 | int err, |
---|
53 | i, |
---|
54 | unit; |
---|
55 | extern int tls; |
---|
56 | char buff[4096]; |
---|
57 | char * _xml = (char)NULL, |
---|
58 | * xml_connect = {"<?xml version='1.0' encoding='UTF-8'?><stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>"}, |
---|
59 | * xml_starttls = {"<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"}; |
---|
60 | |
---|
61 | /*SSL PART*/ |
---|
62 | SSL_METHOD *meth; |
---|
63 | SSL_CTX *ctx; |
---|
64 | SSL *myssl; |
---|
65 | /*SSL PART*/ |
---|
66 | |
---|
67 | _xml = (char *) malloc(sizeof(xml_connect)); |
---|
68 | |
---|
69 | if ( (nHostPort = strtol(PORT, 0, 10)) == 0 ) |
---|
70 | { |
---|
71 | printf("\n<port>\n\n"); |
---|
72 | printf("\ncould not make a socket\n"); |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | socketfd = socket(AF_INET, SOCK_STREAM, 0); |
---|
77 | |
---|
78 | socketaddr.sin_family = AF_INET; |
---|
79 | //socketaddr.sin_addr.s_addr = INADDR_ANY;//inet_addr(IP); |
---|
80 | socketaddr.sin_addr.s_addr = inet_addr(IP); |
---|
81 | socketaddr.sin_port = htons(nHostPort); |
---|
82 | |
---|
83 | OpenSSL_add_all_algorithms(); |
---|
84 | SSL_library_init(); |
---|
85 | SSL_load_error_strings(); |
---|
86 | |
---|
87 | meth=TLSv1_client_method(); |
---|
88 | |
---|
89 | ctx=SSL_CTX_new(meth); |
---|
90 | if (!ctx) |
---|
91 | { |
---|
92 | printf("Error creating the context.\n"); |
---|
93 | exit(0); |
---|
94 | } |
---|
95 | |
---|
96 | err = connect(socketfd, (struct sockaddr*)&socketaddr, sizeof(socketaddr)); |
---|
97 | //fcntl(socketfd, F_SETFL, fcntl(socketfd, F_GETFL, 0) | O_NDELAY | O_NONBLOCK); |
---|
98 | |
---|
99 | if ( err < 0 ) |
---|
100 | { |
---|
101 | printf("Socket returned error #%d,program terminated\n", err); |
---|
102 | exit(0); |
---|
103 | } |
---|
104 | |
---|
105 | sprintf(_xml, xml_connect, HOST); |
---|
106 | Write(&socketfd, _xml, strlen(_xml)); |
---|
107 | //usleep(500); |
---|
108 | Read (&socketfd, buff, sizeof(buff)); |
---|
109 | printf("%s\n\n", buff); |
---|
110 | bzero(buff, 4096); |
---|
111 | |
---|
112 | puts("-----------\n"); |
---|
113 | |
---|
114 | /* |
---|
115 | read (socketfd, buff, sizeof(buff)); |
---|
116 | printf("%s\n\n", buff); |
---|
117 | bzero(buff, 4096); |
---|
118 | |
---|
119 | puts("-----------\n"); |
---|
120 | */ |
---|
121 | |
---|
122 | sprintf(_xml, xml_starttls); |
---|
123 | Write(&socketfd, _xml, strlen(_xml)); |
---|
124 | //usleep(500); |
---|
125 | Read (&socketfd, buff, sizeof(buff)); |
---|
126 | printf("%s\n\n", buff); |
---|
127 | bzero(buff, 4096); |
---|
128 | |
---|
129 | SSL_CTX_set_verify(ctx,SSL_VERIFY_NONE,NULL); |
---|
130 | /*Create new ssl object*/ |
---|
131 | myssl=SSL_new(ctx); |
---|
132 | |
---|
133 | if(!myssl){ |
---|
134 | printf("Error creating SSL structure.\n"); |
---|
135 | exit(0); |
---|
136 | } |
---|
137 | |
---|
138 | /*Bind the socket to the SSL structure*/ |
---|
139 | SSL_set_fd(myssl,socketfd); |
---|
140 | |
---|
141 | /*Connect to the server, SSL layer.*/ |
---|
142 | err=SSL_connect(myssl); |
---|
143 | printf("SSL error #%d in accept,program terminated\n",err); |
---|
144 | /*Check for error in connect.*/ |
---|
145 | if (err<1) { |
---|
146 | |
---|
147 | err=SSL_get_error(myssl,err); |
---|
148 | printf("SSL error #%d in accept,program terminated\n",err); |
---|
149 | printf("%d -- %d\n\n", SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE); |
---|
150 | |
---|
151 | if(err==5){printf("sockerrno is:\n");} |
---|
152 | |
---|
153 | close(socketfd); |
---|
154 | SSL_CTX_free(ctx); |
---|
155 | exit(0); |
---|
156 | } |
---|
157 | |
---|
158 | tls = 1; |
---|
159 | |
---|
160 | /*Print out connection details*/ |
---|
161 | printf("SSL connection on socket %x,Version: %s, Cipher: %s\n\n\n", |
---|
162 | socketfd, |
---|
163 | SSL_get_version(myssl), |
---|
164 | SSL_get_cipher(myssl)); |
---|
165 | |
---|
166 | sprintf(_xml, xml_connect, HOST); |
---|
167 | SSL_write(myssl, _xml, strlen(_xml)); |
---|
168 | usleep(10000); |
---|
169 | SSL_read(myssl, buff, sizeof(buff)); |
---|
170 | printf("%s\n\n", buff); |
---|
171 | bzero(buff, 4096); |
---|
172 | |
---|
173 | usleep(10000); |
---|
174 | SSL_read(myssl, buff, sizeof(buff)); |
---|
175 | printf("%s\n\n", buff); |
---|
176 | bzero(buff, 4096); |
---|
177 | |
---|
178 | SSL_clear(myssl); |
---|
179 | SSL_shutdown(myssl); |
---|
180 | SSL_free(myssl); |
---|
181 | SSL_CTX_free(ctx); |
---|
182 | fflush(stdin); |
---|
183 | fflush(stdout); |
---|
184 | exit(0); |
---|
185 | } |
---|