-
Notifications
You must be signed in to change notification settings - Fork 0
/
testclient.c
386 lines (319 loc) · 11 KB
/
testclient.c
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <openssl/rsa.h>
#include <sys/socket.h>
#include <strings.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include "hash.h"
#include "aes.h"
#include "shared.h"
const char *PREFERRED_CIPHERS = "HIGH:!aNULL:!kRSA:!SRP:!PSK:!CAMELLIA:!RC4:!MD5:!DSS";
int verify_callback(int preverify, X509_STORE_CTX* x509_ctx);
void print_san_name(const char* label, X509* const cert);
void print_cn_name(const char* label, X509_NAME* const name);
#define HOSTNAME "www.random.org"
#define HOST_RESOURCE "/cgi-bin/randbyte?nbytes=32&format=h"
#define PRIVATE_KEY "sprivate.pem"
#define CERTIFICATE "scert.pem"
void die( char *msg )
{
printf("%s\n", msg);
exit(1);
}
int connect_to_server(char *address, char *port)
{
int sockfd, n, portno;
struct addrinfo hints, *servinfo, *p;
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if((rv = getaddrinfo(address, port, &hints, &servinfo)) != 0)
die("Addr info error");
sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
if(sockfd < 0)
die("socket() error");
if(connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen) < 0)
die("connect() error");
printf("connected to serer\n");
return sockfd;
}
void parseRequest(char *request, SSL *ssl)
{
char *token_seperators = "\t \r\n";
char *method = "";
char *requestFile = "";
char *mode = "";
char *pswd = "";
int putRequest, encrypted;
char reqPack[200];
strcpy(reqPack, request);
//use tokenizer to parse request line
method = strtok(request, token_seperators);
requestFile = strtok(NULL, token_seperators);
mode = strtok(NULL, token_seperators);
pswd = strtok(NULL, token_seperators);
// make sure minumum fields are specified
if(method == NULL || requestFile == NULL || mode == NULL) {
printError("Missing parameters, a minimum of a filename and \"N\" or \"E\" is required");
return;
}
//get request type
if(strcmp(method, "get") == 0) {
printf("get request\n");
putRequest = 0;
}
else if(strcmp(method, "put") == 0) {
printf("put request\n");
putRequest = 1;
}
else {
printError("Invalid commands, options are \"get\" \"put\" \"stop\"");
return;
}
//get mode type
if(strcmp(mode, "E") == 0) {
printf("encrypt mode\n");
if(pswd == NULL) {
//make sure password is specified when in E mode
printError("Missing parameters, \"E\" requires password\n");
return;
}
pswd[strlen(pswd)] = '\0';
encrypted = 1;
}
else if (strcmp(mode, "N") == 0) {
printf("no encryption\n");
encrypted = 0;
}
else {
printError("Valid modes are N and E");
return;
}
printf("trying to print request\n");
printf("%s\n", reqPack);
strcat(reqPack, "\n");
Send(ssl, (void *)reqPack, strlen(reqPack));
FILE *outfp;
FILE *infp;
if(!isPut && isEnc) {
FILE *outfp = fopen(requestFile, "rb");
FILE *infp = fdopen(SSL_get_fd(ssl), "wb");
aesdec(infp, outfp, pswd);
char hashBuff[2048/8], recvHash[2048/8];
create_hash(hashBuf, outfp);
Recv(ssl, recvHash, strlen(recvHash));
//compare recvHash and hashBuf
}
if(isPut && isEnc) {
FILE *infp = fopen(requestFile, "rb");
char hashBuff[2048/8];
create_hash(hashBuff, infp);
FILE *outfp = fdopen(SSL_get_fd(ssl), "wb");
aesenc(infp, outfp, pswd);
Send(ssl, hashBuff, stlen(hashBuff));
}
printf("hi\n");
//now or recieve hash of data
}
int compare_hash(char *h1, char *h2) {
}
void create_hash(char *hashBuff, FILE *fp) {
char *ftext;
fseek(fp, 0, SEEK_END);
int fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
printf("filesize: %d\n", fsize);
//Send(sock, &sendSize, sizeof(unsigned int));
printf("b");
ftext = malloc(fsize + 1);
printf("c");
fread(ftext, fsize, 1, f);
fseek(fp, 0, SEEK_SET);
hash(ftext, hashBuff);
free(ftext);
}
int main(int argc,char **argv)
{
SSL_CTX *ctx;
SSL *ssl;
BIO *sbio, *bbio, *acpt, *out;
int sock, res;
unsigned long sslerr = 0;
if(argc < 3) {
printf("port\n");
exit(1);
}
char *host = argv[1];
char *port = argv[2];
init_libs();
printf("1\n");
/* Build our SSL context*/
ctx = SSL_CTX_new(SSLv23_client_method());
//need own certificate, CA certificate optional
//SSL_CTX_use_certificate_chain_file(ctx, "server.pem");
if(!SSL_CTX_use_PrivateKey_file(ctx, PRIVATE_KEY, SSL_FILETYPE_PEM) ||
!SSL_CTX_use_certificate_file(ctx, CERTIFICATE, SSL_FILETYPE_PEM) ||
!SSL_CTX_check_private_key(ctx)) {
printf("cert errorors");
exit(1);
}
// not really sure what this does
// SSL_CTX_set_session_id_context(ctx, sid, 4);
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_callback);
ssl = SSL_new(ctx);
sock = connect_to_server(host, port);
//this loads the certificates
SSL_CTX_load_verify_locations(ctx, "rootCA.pem", NULL);
//SSL_set_session_id_context(ssl, sid, 4);
//sbio = BIO_new_ssl(ctx, 0);
printf("connected\n");
sbio = BIO_new(BIO_s_socket());
BIO_set_fd(sbio, sock, BIO_NOCLOSE);
SSL_set_bio(ssl, sbio, sbio);
//handshake on server
SSL_connect(ssl);
if(SSL_get_verify_result(ssl) != X509_V_OK)
printf("certificate not verified");
/*
X509 *peer = SSL_get_peer_certificate(ssl);
char peer_CN[256];
X509_NAME_get_text_by_NID(X509_get_subject_name(peer),
NID_commonName, peer_CN, 256);
if(strcasecmp(peer_CN, host))
printf("comm name does not match host name");
*/
char requestLine[1000];
printf(">>");
while(1) {
if(fgets(requestLine, sizeof(requestLine), stdin) == NULL)
printError("something wrong with getting stdin");
requestLine[strlen(requestLine)] = '\0';
char *stop;
stop = "stop";
if(strcmp(requestLine, stop) == 0)
break;
printf("parsing request: %s\n", requestLine);
parseRequest(requestLine, ssl);
printf(">>");
}
}
int verify_callback(int preverify, X509_STORE_CTX* x509_ctx)
{
/* For error codes, see http://www.openssl.org/docs/apps/verify.html */
int depth = X509_STORE_CTX_get_error_depth(x509_ctx);
int err = X509_STORE_CTX_get_error(x509_ctx);
X509* cert = X509_STORE_CTX_get_current_cert(x509_ctx);
X509_NAME* iname = cert ? X509_get_issuer_name(cert) : NULL;
X509_NAME* sname = cert ? X509_get_subject_name(cert) : NULL;
fprintf(stdout, "verify_callback (depth=%d)(preverify=%d)\n", depth, preverify);
/* Issuer is the authority we trust that warrants nothing useful */
print_cn_name("Issuer (cn)", iname);
/* Subject is who the certificate is issued to by the authority */
print_cn_name("Subject (cn)", sname);
if(depth == 0) {
/* If depth is 0, its the server's certificate. Print the SANs */
print_san_name("Subject (san)", cert);
}
if(preverify == 0)
{
if(err == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
fprintf(stdout, " Error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY\n");
else if(err == X509_V_ERR_CERT_UNTRUSTED)
fprintf(stdout, " Error = X509_V_ERR_CERT_UNTRUSTED\n");
else if(err == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN)
fprintf(stdout, " Error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN\n");
else if(err == X509_V_ERR_CERT_NOT_YET_VALID)
fprintf(stdout, " Error = X509_V_ERR_CERT_NOT_YET_VALID\n");
else if(err == X509_V_ERR_CERT_HAS_EXPIRED)
fprintf(stdout, " Error = X509_V_ERR_CERT_HAS_EXPIRED\n");
else if(err == X509_V_OK)
fprintf(stdout, " Error = X509_V_OK\n");
else
fprintf(stdout, " Error = %d\n", err);
}
#if !defined(NDEBUG)
return 1;
#else
return preverify;
#endif
}
void print_cn_name(const char* label, X509_NAME* const name)
{
int idx = -1, success = 0;
unsigned char *utf8 = NULL;
do
{
if(!name) break; /* failed */
idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
if(!(idx > -1)) break; /* failed */
X509_NAME_ENTRY* entry = X509_NAME_get_entry(name, idx);
if(!entry) break; /* failed */
ASN1_STRING* data = X509_NAME_ENTRY_get_data(entry);
if(!data) break; /* failed */
int length = ASN1_STRING_to_UTF8(&utf8, data);
if(!utf8 || !(length > 0)) break; /* failed */
fprintf(stdout, " %s: %s\n", label, utf8);
success = 1;
} while (0);
if(utf8)
OPENSSL_free(utf8);
if(!success)
fprintf(stdout, " %s: <not available>\n", label);
}
void print_san_name(const char* label, X509* const cert)
{
int success = 0;
GENERAL_NAMES* names = NULL;
unsigned char* utf8 = NULL;
do
{
if(!cert) break; /* failed */
names = X509_get_ext_d2i(cert, NID_subject_alt_name, 0, 0 );
if(!names) break;
int i = 0, count = sk_GENERAL_NAME_num(names);
if(!count) break; /* failed */
for( i = 0; i < count; ++i )
{
GENERAL_NAME* entry = sk_GENERAL_NAME_value(names, i);
if(!entry) continue;
if(GEN_DNS == entry->type)
{
int len1 = 0, len2 = -1;
len1 = ASN1_STRING_to_UTF8(&utf8, entry->d.dNSName);
if(utf8) {
len2 = (int)strlen((const char*)utf8);
}
if(len1 != len2) {
fprintf(stderr, " Strlen and ASN1_STRING size do not match (embedded null?): %d vs %d\n", len2, len1);
}
/* If there's a problem with string lengths, then */
/* we skip the candidate and move on to the next. */
/* Another policy would be to fails since it probably */
/* indicates the client is under attack. */
if(utf8 && len1 && len2 && (len1 == len2)) {
fprintf(stdout, " %s: %s\n", label, utf8);
success = 1;
}
if(utf8) {
OPENSSL_free(utf8), utf8 = NULL;
}
}
else
{
fprintf(stderr, " Unknown GENERAL_NAME type: %d\n", entry->type);
}
}
} while (0);
if(names)
GENERAL_NAMES_free(names);
if(utf8)
OPENSSL_free(utf8);
if(!success)
fprintf(stdout, " %s: <not available>\n", label);
}