-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtesttls.c
More file actions
102 lines (89 loc) · 2.07 KB
/
testtls.c
File metadata and controls
102 lines (89 loc) · 2.07 KB
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
#include <stdio.h>
#include <dstring.h>
#include <dnet.h>
void
getSmtpCommands(dsocket *sd)
{
int err;
size_t wlen;
char wbuf[4096]={0}, rbuf[4096]={0};
while (1) {
printf("smtp> ");
fgets(wbuf, sizeof(wbuf), stdin);
if (strncasecmp(wbuf, "quit", 4) == 0) {
break;
}
wlen = strlen(wbuf);
wbuf[wlen-1] = '\0';
strncat(wbuf, "\r\n", sizeof(wbuf));
wlen += 1;
if (dnetWrite(sd, wbuf, wlen) == -1) {
printf("SSL Write error.\n\t");
break;
}
err = dnetRead(sd, rbuf, sizeof(rbuf)-1);
if (err == -1) {
printf("dnetRecv Error.\n");
break;
}
rbuf[err] = '\0';
printf("<-- %s\n", rbuf);
memset(rbuf, '\0', sizeof(rbuf));
memset(wbuf, '\0', sizeof(wbuf));
}
}
int main(int argc, char **argv)
{
dsocket *sd=NULL;
char *put=NULL;
char *server=NULL;
uint port;
char buf[4096] = {0};
if (argc != 3) {
printf("%s server port\n", argv[0]);
exit(1);
} else {
server = argv[1];
port = atoi(argv[2]);
}
// The next function doesn't have to be called unless we want
// to specify our own get password function. Otherwise OpenSSL
// will prompt the user on the cmd line.
// SSL_CTX_set_default_passwd_cb(ctx, getPassword);
/* END Init SSL/TLS */
sd = dnetConnect(server, port);
if (!sd) {
perror("Coulnd't connect to server.");
return 1;
}
dnetRead(sd, buf, sizeof(buf)-1);
printf("<-- %s", buf);
put = "EHLO snaghosting.com\r\n";
dnetWrite(sd, put, strlen(put));
printf("--> %s", put);
memset(buf, '\0', sizeof(buf));
dnetRead(sd, buf, sizeof(buf)-1);
printf("<-- %s\n", buf);
put = "STARTTLS\r\n";
dnetWrite(sd, put, strlen(put));
printf("--> %s", put);
memset(buf, '\0', sizeof(buf));
dnetRead(sd, buf, sizeof(buf)-1);
printf("--> %s", buf);
memset(buf, '\0', sizeof(buf));
if (dnetUseTls(sd) == ERROR) {
printf("TLS Connection failed miserably.\n");
dnetClose(sd);
return 1;
}
if (dnetVerifyCert(sd) == ERROR) {
printf("Couldn't verify peer certificate.\n");
dnetClose(sd);
return 1;
}
/* Do cert verification stuff here. */
getSmtpCommands(sd);
dnetWrite(sd, "QUIT\r\n", 6);
dnetClose(sd);
return 0;
}