forked from shuque/danetls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
starttls.c
170 lines (160 loc) · 4.27 KB
/
starttls.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
/*
* starttls.c
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ldns/ldns.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/x509v3.h>
#include "starttls.h"
extern int debug;
enum APP_STARTTLS starttls = STARTTLS_NONE;
/*
* do_starttls() -
* Perform application specific STARTTLS conversation. This routine
* speaks just enough of the protocol to determine whether it can
* proceed with TLS session establishment.
*/
#define MYBUFSIZE 2048
int do_starttls(enum APP_STARTTLS starttls, BIO *sbio,
char *service, const char *hostname)
{
int rc = 0;
char buffer[MYBUFSIZE], myhostname[MYBUFSIZE], param[MYBUFSIZE], *cp;
int read_len;
switch (starttls) {
case STARTTLS_SMTP: {
int seen_starttls = 0, reply_code = -1;
BIO *fbio = BIO_new(BIO_f_buffer());
BIO_push(fbio, sbio);
/* consume greeting (possibly multiline) & inspect reply code */
while (1) {
read_len = BIO_gets(fbio, buffer, MYBUFSIZE);
(void) sscanf(buffer, "%3d", &reply_code);
if (debug) {
cp = strstr(buffer, "\r\n");
*cp = '\0';
fprintf(stdout, "recv: %s\n", buffer);
}
if (read_len <= 3 || buffer[3] != '-')
break;
}
if (reply_code != 220) {
fprintf(stdout, "Invalid ESMTP greeting: %s\n", buffer);
BIO_pop(fbio);
BIO_free(fbio);
return rc;
}
/* Send EHLO, read response, and look for STARTTLS parameter */
(void) gethostname(myhostname, MYBUFSIZE);
if (debug) {
fprintf(stdout, "send: EHLO %s\n", myhostname);
}
BIO_printf(fbio, "EHLO %s\r\n", myhostname);
(void)BIO_flush(fbio);
while (1) {
read_len = BIO_gets(fbio, buffer, MYBUFSIZE);
(void) sscanf(buffer, "%3d", &reply_code);
(void) sscanf(buffer+4, "%255s", param);
if (strcmp(param, "STARTTLS") == 0)
seen_starttls = 1;
if (debug) {
cp = strstr(buffer, "\r\n");
*cp = '\0';
fprintf(stdout, "recv: %s\n", buffer);
}
if (read_len <= 3 || buffer[3] != '-')
break;
}
BIO_pop(fbio);
BIO_free(fbio);
if (reply_code == 250 && seen_starttls) {
/* send STARTTLS command and inspect reply code */
if (debug) {
fprintf(stdout, "send: STARTTLS\n");
}
BIO_printf(sbio, "STARTTLS\r\n");
BIO_read(sbio, buffer, MYBUFSIZE);
if (debug) {
cp = strstr(buffer, "\r\n");
*cp = '\0';
fprintf(stdout, "recv: %s\n", buffer);
}
(void) sscanf(buffer, "%3d", &reply_code);
if (reply_code == 220)
rc = 1;
else
fprintf(stderr, "Invalid response to STARTTLS: %s\n", buffer);
} else if (reply_code != 250) {
fprintf(stderr, "Invalid reply code to SMTP EHLO: %d\n", reply_code);
} else {
fprintf(stderr, "Unable to find STARTTLS in SMTP EHLO response.\n");
}
break;
}
case STARTTLS_XMPP_CLIENT:
case STARTTLS_XMPP_SERVER: {
int readn, seen_starttls = 0;
snprintf(buffer, sizeof(buffer),
"<?xml version='1.0'?>"
"<stream:stream "
"to='%s' "
"version='1.0' xml:lang='en' "
"xmlns='jabber:%s' "
"xmlns:stream='http://etherx.jabber.org/streams'>",
service ? service : hostname,
starttls == STARTTLS_XMPP_CLIENT ? "client" : "server");
if (debug) {
fprintf(stdout, "send: %s\n", buffer);
}
BIO_printf(sbio, buffer);
while (1) {
readn = BIO_read(sbio, buffer, MYBUFSIZE);
if (readn == 0) break;
buffer[readn] = '\0';
if (debug) {
fprintf(stdout, "recv: %s\n", buffer);
}
if (strstr(buffer, "<starttls xmlns") &&
strstr(buffer, "urn:ietf:params:xml:ns:xmpp-tls")) {
seen_starttls = 1;
break;
}
}
if (!seen_starttls)
fprintf(stderr, "Unable to find STARTTLS in XMPP response.\n");
else {
snprintf(buffer, sizeof(buffer),
"<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
if (debug) {
fprintf(stdout, "send: %s\n", buffer);
}
BIO_printf(sbio, buffer);
readn = BIO_read(sbio, buffer, MYBUFSIZE);
buffer[readn] = '\0';
if (debug) {
fprintf(stdout, "recv: %s\n", buffer);
}
if (strstr(buffer, "<proceed"))
rc = 1;
}
break;
}
default:
fprintf(stderr, "STARTTLS application not implemented.\n");
break;
}
return rc;
}