29
29
#include <arpa/inet.h>
30
30
#include <netinet/in.h>
31
31
#include <unistd.h>
32
+ #include <netdb.h>
32
33
33
34
/* wolfSSL */
34
35
#include <wolfssl/options.h>
47
48
static void print_SSL_error (const char * msg , SSL * ssl )
48
49
{
49
50
int err ;
50
-
51
+
51
52
if (ssl != NULL ) {
52
53
err = wolfSSL_get_error (ssl , 0 );
53
54
fprintf (stderr , "ERROR: %s (err %d, %s)\n" , msg , err ,
@@ -67,28 +68,28 @@ static int read_SESS(const char* file, SSL* ssl)
67
68
size_t sz ;
68
69
WOLFSSL_SESSION * sess = NULL ;
69
70
int ret = WOLFSSL_FAILURE ;
70
-
71
+
71
72
if (((fp = fopen (file , "rb" )) == NULL ) ||
72
73
(fseek (fp , 0 , SEEK_END ) != 0 ) ||
73
74
((sz = ftell (fp )) == -1 )) {
74
75
fprintf (stderr , "ERROR : failed file %s operation \n" , file );
75
76
goto cleanup ;
76
77
}
77
-
78
+
78
79
rewind (fp );
79
80
if ((buff = (unsigned char * )malloc (sz )) == NULL ||
80
81
(fread (buff , 1 , sz , fp ) != sz )) {
81
82
fprintf (stderr , "ERROR : failed reading file\n" );
82
83
goto cleanup ;
83
84
}
84
-
85
+
85
86
printf ("%s size = %ld\n" , SAVED_SESS , sz );
86
-
87
+
87
88
p = buff ;
88
89
if ((sess = wolfSSL_d2i_SSL_SESSION (NULL , (const unsigned char * * )& p , sz )) == NULL ) {
89
90
print_SSL_error ("wolfSSL_d2i_SSL_SESSION" , NULL );
90
91
}
91
-
92
+
92
93
if (sess != NULL && (ret = wolfSSL_set_session (ssl , sess ) != WOLFSSL_SUCCESS )) {
93
94
print_SSL_error ("failed SSL session" , ssl );
94
95
} else {
@@ -118,7 +119,7 @@ int main(int argc, char **argv)
118
119
119
120
char msg [MSG_SIZE ];
120
121
int ret = WOLFSSL_FAILURE ;
121
-
122
+
122
123
(void )ipadd ;
123
124
124
125
/* SSL objects */
@@ -128,15 +129,15 @@ int main(int argc, char **argv)
128
129
memset (& servAddr , 0 , sizeof (servAddr ));
129
130
130
131
/* Check for proper calling convention */
131
- if (argc == 1 )
132
+ if (argc == 1 )
132
133
fprintf (stderr , "Send to localhost(%s)\n" , LOCALHOST );
133
134
if (argc >=2 ) {
134
135
host = gethostbyname (argv [1 ]);
135
136
memcpy (& servAddr .sin_addr , host -> h_addr_list [0 ], host -> h_length );
136
137
}
137
- if (argc >= 3 )
138
+ if (argc >= 3 )
138
139
ca_cert = argv [2 ];
139
- if (argc == 4 )
140
+ if (argc == 4 )
140
141
port = atoi (argv [3 ]);
141
142
if (argc >= 5 ) {
142
143
fprintf (stderr , "ERROR: Too many arguments.\n" );
@@ -148,23 +149,23 @@ int main(int argc, char **argv)
148
149
fprintf (stderr , "ERROR: failed to initialize the library\n" );
149
150
goto cleanup ;
150
151
}
151
-
152
+
152
153
/* Create and initialize an SSL context object*/
153
154
if ((ctx = wolfSSL_CTX_new (SSLv23_client_method ())) == NULL ) {
154
155
fprintf (stderr , "ERROR: failed to create an SSL context object\n" );
155
156
goto cleanup ;
156
157
}
157
158
158
159
/* Load client certificate into WOLFwolfSSL_CTX */
159
- if ((ret = wolfSSL_CTX_use_certificate_file (ctx , CERT_FILE ,
160
+ if ((ret = wolfSSL_CTX_use_certificate_file (ctx , CERT_FILE ,
160
161
WOLFSSL_FILETYPE_PEM )) != WOLFSSL_SUCCESS ) {
161
162
fprintf (stderr , "ERROR: failed to load %s, please check the file.\n" ,
162
163
CERT_FILE );
163
164
goto cleanup ;
164
165
}
165
166
166
167
/* Load client key into WOLFwolfSSL_CTX */
167
- if ((ret = wolfSSL_CTX_use_PrivateKey_file (ctx , KEY_FILE ,
168
+ if ((ret = wolfSSL_CTX_use_PrivateKey_file (ctx , KEY_FILE ,
168
169
WOLFSSL_FILETYPE_PEM )) != WOLFSSL_SUCCESS ) {
169
170
fprintf (stderr , "ERROR: failed to load %s, please check the file.\n" ,
170
171
KEY_FILE );
@@ -178,17 +179,17 @@ int main(int argc, char **argv)
178
179
goto cleanup ;
179
180
}
180
181
181
- /*
182
- * Set up a TCP Socket and connect to the server
182
+ /*
183
+ * Set up a TCP Socket and connect to the server
183
184
*/
184
185
if ((sockfd = socket (AF_INET , SOCK_STREAM , 0 )) == -1 ) {
185
186
fprintf (stderr , "ERROR: failed to create a socket. errno %d\n" , errno );
186
187
goto cleanup ;
187
188
}
188
-
189
+
189
190
servAddr .sin_family = AF_INET ; /* using IPv4 */
190
191
servAddr .sin_port = htons (port ); /* on DEFAULT_PORT */
191
-
192
+
192
193
if ((ret = connect (sockfd , (struct sockaddr * )& servAddr , sizeof (servAddr )))
193
194
== -1 ) {
194
195
fprintf (stderr , "ERROR: failed to connect. errno %d\n" , errno );
@@ -206,7 +207,7 @@ int main(int argc, char **argv)
206
207
fprintf (stderr , "ERROR: failed to read session information\n" );
207
208
goto cleanup ;
208
209
}
209
-
210
+
210
211
/* Attach the socket to the SSL */
211
212
if ((ret = wolfSSL_set_fd (ssl , sockfd )) != WOLFSSL_SUCCESS ) {
212
213
fprintf (stderr , "ERROR: Failed to set the file descriptor\n" );
@@ -226,7 +227,7 @@ int main(int argc, char **argv)
226
227
printf ("Session is not reused. New session was negotiated.\n" );
227
228
}
228
229
229
- /*
230
+ /*
230
231
* Application messaging
231
232
*/
232
233
while (1 ) {
@@ -235,18 +236,18 @@ int main(int argc, char **argv)
235
236
break ;
236
237
if (strcmp (msg , "\n" ) == 0 ){ /* if empty send HTTP request */
237
238
strncpy (msg , kHttpGetMsg , sizeof (msg ));
238
- } else
239
+ } else
239
240
msg [strnlen (msg , sizeof (msg ))- 1 ] = '\0' ;
240
241
/* send a message to the server */
241
242
if ((ret = wolfSSL_write (ssl , msg , strnlen (msg , sizeof (msg )))) < 0 ) {
242
243
print_SSL_error ("failed SSL write" , ssl );
243
244
break ;
244
245
}
245
246
246
- /*
247
+ /*
247
248
* closing the session, and write session information into a file
248
249
* before writing session information, the file is removed if exists
249
- */
250
+ */
250
251
if (strcmp (msg , "break" ) == 0 ) {
251
252
printf ("Sending break command\n" );
252
253
ret = WOLFSSL_SUCCESS ;
0 commit comments