-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathwclient.c
136 lines (115 loc) · 2.86 KB
/
wclient.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
/* A simple HTTPS client
It connects to the server, makes an HTTP
request and waits for the response
*/
#include "common.h"
#include "client.h"
static char *REQUEST_TEMPLATE=
"GET / HTTP/1.0\r\nUser-Agent:"
"EKRClient\r\nHost: %s:%d\r\n\r\n";
static char *host=HOST;
static int port=PORT;
static int require_server_auth=1;
static int http_request(ssl)
SSL *ssl;
{
char *request=0;
char buf[BUFSIZZ];
int r;
int len, request_len;
/* Now construct our HTTP request */
request_len=strlen(REQUEST_TEMPLATE)+
strlen(host)+6;
if(!(request=(char *)malloc(request_len)))
err_exit("Couldn't allocate request");
snprintf(request,request_len,REQUEST_TEMPLATE,
host,port);
/* Find the exact request_len */
request_len=strlen(request);
r=SSL_write(ssl,request,request_len);
switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
if(request_len!=r)
err_exit("Incomplete write!");
break;
default:
berr_exit("SSL write problem");
}
/* Now read the server's response, assuming
that it's terminated by a close */
while(1){
r=SSL_read(ssl,buf,BUFSIZZ);
switch(SSL_get_error(ssl,r)){
case SSL_ERROR_NONE:
len=r;
break;
case SSL_ERROR_ZERO_RETURN:
goto shutdown;
case SSL_ERROR_SYSCALL:
fprintf(stderr,
"SSL Error: Premature close\n");
goto done;
default:
berr_exit("SSL read problem");
}
fwrite(buf,1,len,stdout);
}
shutdown:
r=SSL_shutdown(ssl);
switch(r){
case 1:
break; /* Success */
case 0:
case -1:
default:
berr_exit("Shutdown failed");
}
done:
SSL_free(ssl);
free(request);
return(0);
}
int main(argc,argv)
int argc;
char **argv;
{
SSL_CTX *ctx;
SSL *ssl;
BIO *sbio;
int sock;
extern char *optarg;
int c;
while((c=getopt(argc,argv,"h:p:i"))!=-1){
switch(c){
case 'h':
if(!(host=strdup(optarg)))
err_exit("Out of memory");
break;
case 'p':
if(!(port=atoi(optarg)))
err_exit("Bogus port specified");
break;
case 'i':
require_server_auth=0;
break;
}
}
/* Build our SSL context*/
ctx=initialize_ctx(KEYFILE,PASSWORD);
/* Connect the TCP socket*/
sock=tcp_connect(host,port);
/* Connect the SSL socket */
ssl=SSL_new(ctx);
sbio=BIO_new_socket(sock,BIO_NOCLOSE);
SSL_set_bio(ssl,sbio,sbio);
if(SSL_connect(ssl)<=0)
berr_exit("SSL connect error");
if(require_server_auth)
check_cert(ssl,host);
/* Now make our HTTP request */
http_request(ssl);
/* Shutdown the socket */
destroy_ctx(ctx);
close(sock);
exit(0);
}