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 | |
---|
12 | #include <openssl/ssl.h> |
---|
13 | #include <openssl/err.h> |
---|
14 | #include <openssl/crypto.h> |
---|
15 | #include <openssl/x509.h> |
---|
16 | #include <openssl/pem.h> |
---|
17 | #include <openssl/ssl.h> |
---|
18 | #include <openssl/err.h> |
---|
19 | */ |
---|
20 | /* define HOME to be dir for key and certificate files... */ |
---|
21 | //#define HOME "/usr/local/ssl/" |
---|
22 | /* Make these what you want for certificate & key files */ |
---|
23 | //#define CERT_FILE "mycert.pem" |
---|
24 | //#define KEY_FILE "mycert.pem" |
---|
25 | |
---|
26 | /*Cipher list to be used*/ |
---|
27 | //#define CIPHER_LIST "RC4-MD5" |
---|
28 | |
---|
29 | /*Trusted CAs location*/ |
---|
30 | //#define CA_FILE CERT_FILE |
---|
31 | //#define CA_DIR NULL |
---|
32 | |
---|
33 | /*Password for the key file*/ |
---|
34 | //#define KEY_PASSWD "" |
---|
35 | |
---|
36 | //#define IP "im.celepar.parana" |
---|
37 | |
---|
38 | //#define PORT "5222" |
---|
39 | |
---|
40 | #include "server.h" |
---|
41 | |
---|
42 | |
---|
43 | int main(void) |
---|
44 | { |
---|
45 | int jabber_fd = (int)NULL, |
---|
46 | jabber_port = (int)NULL, |
---|
47 | jabber_error = (int)NULL; |
---|
48 | |
---|
49 | struct sockaddr_in jabber_addr; |
---|
50 | |
---|
51 | jabber_port = __DEFAULT_JABBER_PORT__; |
---|
52 | jabber_fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
53 | |
---|
54 | fcntl(jabber_fd, F_SETFL, fcntl(jabber_fd, F_GETFL, 0) | O_NDELAY); |
---|
55 | |
---|
56 | jabber_addr.sin_family = AF_INET; |
---|
57 | inet_aton(__DEFAULT_JABBER_IP__, &jabber_addr.sin_addr); |
---|
58 | jabber_addr.sin_port = htons(jabber_port); |
---|
59 | |
---|
60 | jabber_error = connect(jabber_fd, (struct sockaddr *) &(jabber_addr), sizeof(jabber_addr)); |
---|
61 | |
---|
62 | printf("Socket returned error #%d,program terminated\n", jabber_error); |
---|
63 | |
---|
64 | exit(0); |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | int socketfd, nHostPort; |
---|
79 | int err; |
---|
80 | char buff[32]; |
---|
81 | struct sockaddr_in socketaddr; |
---|
82 | |
---|
83 | /*SSL PART*/ |
---|
84 | SSL_METHOD *meth; |
---|
85 | SSL_CTX *ctx; |
---|
86 | SSL *myssl; |
---|
87 | /*SSL PART*/ |
---|
88 | |
---|
89 | if ( (nHostPort = strtol(PORT, 0, 10)) == 0 ) |
---|
90 | { |
---|
91 | printf("\n<port>\n\n"); |
---|
92 | printf("\ncould not make a socket\n"); |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | socketfd=socket(AF_INET,SOCK_STREAM,0); |
---|
97 | |
---|
98 | socketaddr.sin_family=AF_INET; |
---|
99 | inet_aton("im.celepar.parana", &socketaddr.sin_addr); |
---|
100 | socketaddr.sin_port=htons(5222); |
---|
101 | |
---|
102 | /* Connect to the server, TCP/IP layer,*/ |
---|
103 | err=connect(socketfd,(struct sockaddr*)&socketaddr,sizeof(socketaddr)); |
---|
104 | if(err<0){ |
---|
105 | printf("Socket returned error #%d,program terminated\n", err); |
---|
106 | exit(0); |
---|
107 | } |
---|
108 | exit(0); |
---|
109 | |
---|
110 | /* SSL Part*/ |
---|
111 | SSL_library_init(); |
---|
112 | SSL_load_error_strings(); |
---|
113 | |
---|
114 | meth=TLSv1_client_method(); |
---|
115 | |
---|
116 | ctx=SSL_CTX_new(meth); |
---|
117 | if (!ctx) { |
---|
118 | printf("Error creating the context.\n"); |
---|
119 | exit(0); |
---|
120 | } |
---|
121 | |
---|
122 | /* |
---|
123 | if (SSL_CTX_set_cipher_list(ctx,CIPHER_LIST) <= 0) { |
---|
124 | printf("Error setting the cipher list.\n"); |
---|
125 | exit(0); |
---|
126 | } |
---|
127 | */ |
---|
128 | /*Indicate the certificate file to be used*/ |
---|
129 | /* if (SSL_CTX_use_certificate_file(ctx,CERT_FILE, SSL_FILETYPE_PEM) <= 0) { |
---|
130 | printf("Error setting the certificate file.\n"); |
---|
131 | exit(0); |
---|
132 | }*/ |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | /*Load the password for the Private Key*/ |
---|
137 | //SSL_CTX_set_default_passwd_cb_userdata(ctx,KEY_PASSWD); |
---|
138 | |
---|
139 | |
---|
140 | /*Indicate the key file to be used*/ |
---|
141 | /*if (SSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM) <= 0) { |
---|
142 | printf("Error setting the key file.\n"); |
---|
143 | exit(0); |
---|
144 | }*/ |
---|
145 | |
---|
146 | /*Make sure the key and certificate file match*/ |
---|
147 | /*if (!SSL_CTX_check_private_key(ctx)) { |
---|
148 | printf("Private key does not match the certificate public key\n"); |
---|
149 | exit(0); |
---|
150 | }*/ |
---|
151 | |
---|
152 | /* Set the list of trusted CAs based on the file and/or directory provided*/ |
---|
153 | /* if(SSL_CTX_load_verify_locations(ctx,CA_FILE,CA_DIR)<1){ |
---|
154 | printf("Error setting verify location\n"); |
---|
155 | exit(0); |
---|
156 | }*/ |
---|
157 | |
---|
158 | /* Set for server verification*/ |
---|
159 | // SSL_CTX_set_verify(ctx,SSL_VERIFY_PEER,NULL); |
---|
160 | |
---|
161 | /*Create new ssl object*/ |
---|
162 | myssl=SSL_new(ctx); |
---|
163 | |
---|
164 | if(!myssl){ |
---|
165 | printf("Error creating SSL structure.\n"); |
---|
166 | exit(0); |
---|
167 | } |
---|
168 | |
---|
169 | /*Bind the socket to the SSL structure*/ |
---|
170 | SSL_set_fd(myssl,socketfd); |
---|
171 | |
---|
172 | /*Connect to the server, SSL layer.*/ |
---|
173 | err=SSL_connect(myssl); |
---|
174 | /*Check for error in connect.*/ |
---|
175 | if (err<1) { |
---|
176 | |
---|
177 | err=SSL_get_error(myssl,err); |
---|
178 | printf("SSL error #%d in accept,program terminated\n",err); |
---|
179 | |
---|
180 | if(err==5){printf("sockerrno is:\n");} |
---|
181 | |
---|
182 | close(socketfd); |
---|
183 | SSL_CTX_free(ctx); |
---|
184 | exit(0); |
---|
185 | } |
---|
186 | |
---|
187 | /*Print out connection details*/ |
---|
188 | printf("SSL connection on socket %x,Version: %s, Cipher: %s\n", |
---|
189 | socketfd, |
---|
190 | SSL_get_version(myssl), |
---|
191 | SSL_get_cipher(myssl)); |
---|
192 | |
---|
193 | puts("_________OK_______________"); |
---|
194 | exit(0); |
---|
195 | /*Send message to the server.*/ |
---|
196 | err=SSL_write(myssl,"Hello there!!!!",sizeof("Hello there!!!!")+1); |
---|
197 | /*Check for error in write.*/ |
---|
198 | if(err<1){ |
---|
199 | err=SSL_get_error(myssl,err); |
---|
200 | printf("Error #%d in write,program terminated\n",err); |
---|
201 | /********************************/ |
---|
202 | /* If err=6 it means the client */ |
---|
203 | /* issued an SSL_shutdown. You */ |
---|
204 | /* must respond with a shutdown */ |
---|
205 | /* to complete a graceful */ |
---|
206 | /* shutdown */ |
---|
207 | /********************************/ |
---|
208 | if(err==6){ |
---|
209 | SSL_shutdown(myssl); |
---|
210 | } |
---|
211 | SSL_free(myssl); |
---|
212 | close(socketfd); |
---|
213 | SSL_CTX_free(ctx); |
---|
214 | exit(0); |
---|
215 | } |
---|
216 | |
---|
217 | /*Read servers response.*/ |
---|
218 | err = SSL_read (myssl, buff, sizeof(buff)); |
---|
219 | /*Check for error in read.*/ |
---|
220 | if(err<1){ |
---|
221 | err=SSL_get_error(myssl,err); |
---|
222 | printf("Error #%d in read,program terminated\n",err); |
---|
223 | /********************************/ |
---|
224 | /* If err=6 it means the client */ |
---|
225 | /* issued an SSL_shutdown. You */ |
---|
226 | /* must respond with a shutdown */ |
---|
227 | /* to complete a graceful */ |
---|
228 | /* shutdown */ |
---|
229 | /********************************/ |
---|
230 | if(err==6){ |
---|
231 | SSL_shutdown(myssl); |
---|
232 | } |
---|
233 | SSL_free(myssl); |
---|
234 | close(socketfd); |
---|
235 | SSL_CTX_free(ctx); |
---|
236 | exit(0); |
---|
237 | } |
---|
238 | printf("Server said: %s\n",buff); |
---|
239 | err=SSL_shutdown(myssl); |
---|
240 | buff[0] = '\0'; |
---|
241 | SSL_read (myssl, buff, sizeof(buff)); |
---|
242 | /*********************************/ |
---|
243 | /* If err is equal to 0 you are */ |
---|
244 | /* issuing the shutdown first. */ |
---|
245 | /* If its 1 then you are */ |
---|
246 | /* responding to a previously */ |
---|
247 | /* issued shutdown message */ |
---|
248 | /*********************************/ |
---|
249 | if(err==0) |
---|
250 | { |
---|
251 | err=SSL_shutdown(myssl); |
---|
252 | buff[0] = '\0'; |
---|
253 | SSL_read (myssl, buff, sizeof(buff)); |
---|
254 | if(err<1){ |
---|
255 | printf("#1 Error in shutdown\n"); |
---|
256 | }else if(err==1){ |
---|
257 | printf("Client exited gracefully\n"); |
---|
258 | } |
---|
259 | }else if(err<0){ |
---|
260 | printf("Error in shutdown\n"); |
---|
261 | |
---|
262 | }else if(err==1){ |
---|
263 | printf("Client exited gracefully\n"); |
---|
264 | } |
---|
265 | close(socketfd); |
---|
266 | SSL_free(myssl); |
---|
267 | SSL_CTX_free(ctx); |
---|
268 | exit(0); |
---|
269 | |
---|
270 | } |
---|