1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/*
* Copyright (C) 2000,2001 Nikos Mavroyanopoulos
*
* This file is part of GNUTLS.
*
* GNUTLS is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNUTLS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include "../lib/gnutls.h"
#include <sys/time.h>
#define SA struct sockaddr
#define ERR(err,s) if (err==-1) {perror(s);return(1);}
#define MAX_BUF 4096
#define RESUME
#define MAX(X,Y) (X >= Y ? X : Y);
#define CAFILE "ca.pem"
#define CRLFILE NULL
static int print_info( GNUTLS_STATE state) {
char *tmp;
const ANON_CLIENT_AUTH_INFO *dh_info;
const X509PKI_CLIENT_AUTH_INFO *x509_info;
tmp = gnutls_kx_get_name(gnutls_get_current_kx( state));
printf("- Key Exchange: %s\n", tmp); free(tmp);
if (gnutls_get_current_kx(state) == GNUTLS_KX_DH_ANON) {
dh_info = gnutls_get_auth_info(state);
if (dh_info != NULL)
printf("- Anonymous DH using prime of %d bits\n",
dh_info->dh_bits);
}
if (gnutls_get_current_kx(state) == GNUTLS_KX_RSA) {
x509_info = gnutls_get_auth_info(state);
if (x509_info != NULL) {
switch( x509_info->peer_certificate_status) {
case GNUTLS_NOT_VERIFIED:
printf("- Peer's X509 Certificate was NOT verified\n");
break;
case GNUTLS_EXPIRED:
printf("- Peer's X509 Certificate was verified but is expired\n");
break;
case GNUTLS_VERIFIED:
printf("- Peer's X509 Certificate was verified\n");
break;
case GNUTLS_INVALID:
default:
printf("- Peer's X509 Certificate was invalid\n");
break;
}
}
}
tmp = gnutls_version_get_name(gnutls_get_current_version(state));
printf("- Version: %s\n", tmp);
free(tmp);
tmp = gnutls_compression_get_name(gnutls_get_current_compression_method( state));
printf("- Compression: %s\n", tmp); free(tmp);
tmp = gnutls_cipher_get_name(gnutls_get_current_cipher( state));
printf("- Cipher: %s\n", tmp); free(tmp);
tmp = gnutls_mac_get_name(gnutls_get_current_mac_algorithm( state));
printf("- MAC: %s\n", tmp); free(tmp);
return 0;
}
int main(int argc, char** argv)
{
int err, ret;
int sd, ii;
struct sockaddr_in sa;
GNUTLS_STATE state;
char buffer[MAX_BUF+1];
char *session;
char* session_id;
int session_size;
int session_id_size;
char* tmp_session_id;
int tmp_session_id_size;
fd_set rset;
int maxfd;
struct timeval tv;
int user_term = 0;
SRP_CLIENT_CREDENTIALS cred;
X509PKI_CLIENT_CREDENTIALS xcred;
if (argc!=3) {
fprintf(stderr, "Usage: cli [IP] [PORT]\n");
exit(1);
}
if (gnutls_global_init("pkix.asn", "pkcs1.asn") < 0) {
fprintf(stderr, "global state initialization error\n");
exit(1);
}
/* X509 stuff */
if (gnutls_allocate_x509_client_sc( &xcred, 0) < 0) { /* no priv key */
fprintf(stderr, "memory error\n");
exit(1);
}
gnutls_set_x509_client_trust( xcred, CAFILE, CRLFILE);
/* SRP stuff */
if (gnutls_allocate_srp_client_sc( &cred)<0) {
fprintf(stderr, "memory error\n");
exit(1);
}
gnutls_set_srp_client_cred( cred, "test", "test");
sd = socket(AF_INET, SOCK_STREAM, 0);
ERR(sd, "socket");
memset(&sa, '\0', sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(atoi(argv[2]));
inet_pton( AF_INET, argv[1], &sa.sin_addr);
err = connect(sd, (SA *) & sa, sizeof(sa));
ERR(err, "connect");
#ifdef RESUME
gnutls_init(&state, GNUTLS_CLIENT);
gnutls_set_current_version( state, GNUTLS_TLS1);
gnutls_set_cipher_priority( state, GNUTLS_3DES_CBC, GNUTLS_ARCFOUR, GNUTLS_RIJNDAEL_CBC, 0);
gnutls_set_compression_priority( state, GNUTLS_ZLIB, GNUTLS_NULL_COMPRESSION, 0);
gnutls_set_kx_priority( state, GNUTLS_KX_RSA, GNUTLS_KX_SRP, GNUTLS_KX_DH_ANON, 0);
gnutls_set_cred( state, GNUTLS_ANON, NULL);
gnutls_set_cred( state, GNUTLS_SRP, &cred);
gnutls_set_cred( state, GNUTLS_X509PKI, &xcred);
gnutls_ext_set_dnsname( state, "hello.server.org");
gnutls_set_mac_priority( state, GNUTLS_MAC_SHA, GNUTLS_MAC_MD5, 0);
ret = gnutls_handshake(sd, state);
if (ret < 0) {
fprintf(stderr, "*** Handshake has failed\n");
gnutls_perror(ret);
gnutls_deinit(state);
return 1;
} else {
printf("- Handshake was completed\n");
}
gnutls_get_current_session( state, NULL, &session_size);
session = malloc(session_size);
gnutls_get_current_session( state, session, &session_size);
gnutls_get_current_session_id( state, NULL, &session_id_size);
session_id = malloc(session_id_size);
gnutls_get_current_session_id( state, session_id, &session_id_size);
/* print some information */
print_info( state);
printf("- Disconnecting\n");
gnutls_bye(sd, state);
shutdown( sd, SHUT_WR);
close(sd);
gnutls_deinit( state);
printf("\n\n- Connecting again- trying to resume previous session\n");
sd = socket(AF_INET, SOCK_STREAM, 0);
ERR(sd, "socket");
err = connect(sd, (SA *) & sa, sizeof(sa));
ERR(err, "connect");
#endif
/* Begin handshake again */
gnutls_init(&state, GNUTLS_CLIENT);
gnutls_set_current_version( state, GNUTLS_TLS1);
gnutls_set_cipher_priority( state, GNUTLS_3DES_CBC, GNUTLS_TWOFISH_CBC, GNUTLS_RIJNDAEL_CBC, GNUTLS_ARCFOUR, 0);
gnutls_set_compression_priority( state, GNUTLS_NULL_COMPRESSION, 0);
gnutls_set_kx_priority( state, GNUTLS_KX_RSA, GNUTLS_KX_SRP, GNUTLS_KX_DH_ANON, 0);
gnutls_set_cred( state, GNUTLS_ANON, NULL);
gnutls_set_cred( state, GNUTLS_SRP, &cred);
gnutls_set_cred( state, GNUTLS_X509PKI, &xcred);
gnutls_ext_set_dnsname( state, "hello.server.org");
gnutls_set_mac_priority( state, GNUTLS_MAC_SHA, GNUTLS_MAC_MD5, 0);
#ifdef RESUME
gnutls_set_current_session( state, session, session_size);
free(session);
#endif
ret = gnutls_handshake(sd, state);
if (ret < 0) {
fprintf(stderr, "*** Handshake failed\n");
gnutls_perror(ret);
gnutls_deinit(state);
return 1;
} else {
printf("- Handshake was completed\n");
}
/* check if we actually resumed the previous session */
gnutls_get_current_session_id( state, NULL, &tmp_session_id_size);
tmp_session_id = malloc(tmp_session_id_size);
gnutls_get_current_session_id( state, tmp_session_id, &tmp_session_id_size);
if (memcmp( tmp_session_id, session_id, session_id_size)==0) {
printf("- Previous session was resumed\n");
} else {
fprintf(stderr, "*** Previous session was NOT resumed\n");
}
free(tmp_session_id);
free(session_id);
/* print some information */
print_info( state);
printf("\n- Simple Client Mode:\n\n");
FD_ZERO(&rset);
for(;;) {
FD_SET(fileno(stdin), &rset);
FD_SET(sd, &rset);
maxfd = MAX(fileno(stdin), sd);
tv.tv_sec = 3;
tv.tv_usec = 0;
select(maxfd+1, &rset, NULL, NULL, &tv);
if (FD_ISSET(sd, &rset)) {
bzero(buffer, MAX_BUF+1);
ret = gnutls_read(sd, state, buffer, MAX_BUF);
/* remove new line */
if (gnutls_is_fatal_error(ret) == 1) {
if (ret == GNUTLS_E_CLOSURE_ALERT_RECEIVED || ret == GNUTLS_E_INVALID_SESSION) {
printf("- Peer has closed the GNUTLS connection\n");
break;
} else {
fprintf(stderr, "*** Received corrupted data(%d) - server has terminated the connection abnormally\n",
ret);
break;
}
} else {
if (ret==GNUTLS_E_WARNING_ALERT_RECEIVED || ret==GNUTLS_E_FATAL_ALERT_RECEIVED)
printf("* Received alert [%d]\n", gnutls_get_last_alert(state));
if (ret==GNUTLS_E_GOT_HELLO_REQUEST)
printf("* Received HelloRequest message (server asked to rehandshake)\n");
if (ret > 0) {
printf("- Received[%d]: ", ret);
for (ii=0;ii<ret;ii++) {
fputc(buffer[ii], stdout);
}
fputs("\n", stdout);
}
}
if (user_term!=0) break;
}
if (FD_ISSET(fileno(stdin), &rset)) {
if( fgets(buffer, MAX_BUF, stdin) == NULL) {
gnutls_bye(sd, state);
user_term = 1;
continue;
}
gnutls_write( sd, state, buffer, strlen(buffer));
printf("- Sent: %d bytes\n", strlen(buffer));
}
}
if (user_term!=0) gnutls_bye(sd, state);
shutdown( sd, SHUT_RDWR); /* no more receptions */
close(sd);
gnutls_free_srp_client_sc( cred);
gnutls_free_x509_client_sc( xcred);
gnutls_deinit( state);
gnutls_global_deinit();
return 0;
}
|