/src/net-snmp/snmplib/transports/snmpTLSTCPDomain.c
Line | Count | Source |
1 | | /* Portions of this file are subject to the following copyright(s). See |
2 | | * the Net-SNMP's COPYING file for more details and other copyrights |
3 | | * that may apply: |
4 | | */ |
5 | | /* |
6 | | * See the following web pages for useful documentation on this transport: |
7 | | * http://www.net-snmp.org/wiki/index.php/TUT:Using_TLS |
8 | | * http://www.net-snmp.org/wiki/index.php/Using_DTLS |
9 | | */ |
10 | | #include <net-snmp/net-snmp-config.h> |
11 | | |
12 | | #include <net-snmp/net-snmp-features.h> |
13 | | |
14 | | netsnmp_feature_require(cert_util); |
15 | | |
16 | | #include <stdio.h> |
17 | | #include <sys/types.h> |
18 | | #include <ctype.h> |
19 | | #include <errno.h> |
20 | | |
21 | | #ifdef HAVE_STRING_H |
22 | | #include <string.h> |
23 | | #else |
24 | | #include <strings.h> |
25 | | #endif |
26 | | #ifdef HAVE_STDLIB_H |
27 | | #include <stdlib.h> |
28 | | #endif |
29 | | #ifdef HAVE_UNISTD_H |
30 | | #include <unistd.h> |
31 | | #endif |
32 | | #ifdef HAVE_SYS_SOCKET_H |
33 | | #include <sys/socket.h> |
34 | | #endif |
35 | | #ifdef HAVE_NETINET_IN_H |
36 | | #include <netinet/in.h> |
37 | | #endif |
38 | | #ifdef HAVE_ARPA_INET_H |
39 | | #include <arpa/inet.h> |
40 | | #endif |
41 | | #ifdef HAVE_NETDB_H |
42 | | #include <netdb.h> |
43 | | #endif |
44 | | #ifdef HAVE_SYS_UIO_H |
45 | | #include <sys/uio.h> |
46 | | #endif |
47 | | |
48 | | #ifdef HAVE_ARPA_INET_H |
49 | | #include <arpa/inet.h> |
50 | | #endif |
51 | | |
52 | | #include "../memcheck.h" |
53 | | |
54 | | #include <net-snmp/types.h> |
55 | | #include <net-snmp/output_api.h> |
56 | | #include <net-snmp/config_api.h> |
57 | | #include <net-snmp/library/snmp_assert.h> |
58 | | #include <net-snmp/library/snmp_impl.h> |
59 | | #include <net-snmp/library/snmpIPv4BaseDomain.h> |
60 | | #include <net-snmp/library/snmpSocketBaseDomain.h> |
61 | | #include <net-snmp/library/snmpTLSBaseDomain.h> |
62 | | #include <net-snmp/library/snmpTLSTCPDomain.h> |
63 | | #include <net-snmp/library/snmpTCPDomain.h> |
64 | | #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN |
65 | | #include <net-snmp/library/snmpTCPIPv6Domain.h> |
66 | | #include <net-snmp/library/snmpIPv6BaseDomain.h> |
67 | | #endif |
68 | | #include <net-snmp/library/system.h> |
69 | | #include <net-snmp/library/tools.h> |
70 | | #include <net-snmp/library/cert_util.h> |
71 | | #include <net-snmp/library/snmp_openssl.h> |
72 | | #include <net-snmp/library/callback.h> |
73 | | #include "snmpIPBaseDomain.h" |
74 | | #include "snmpTLSBaseDomain.h" |
75 | | |
76 | | static void |
77 | | _tlstcp_format_addr_string(_netsnmpTLSBaseData *tlsdata); |
78 | | |
79 | | #include "openssl/bio.h" |
80 | | #include "openssl/ssl.h" |
81 | | #include "openssl/err.h" |
82 | | |
83 | | #ifndef INADDR_NONE |
84 | | #define INADDR_NONE -1 |
85 | | #endif |
86 | | |
87 | | #define WE_ARE_SERVER 0 |
88 | | #define WE_ARE_CLIENT 1 |
89 | | |
90 | | const oid netsnmpTLSTCPDomain[] = { TRANSPORT_DOMAIN_TLS_TCP_IP }; |
91 | | size_t netsnmpTLSTCPDomain_len = OID_LENGTH(netsnmpTLSTCPDomain); |
92 | | |
93 | | static netsnmp_tdomain tlstcpDomain; |
94 | | |
95 | | /* |
96 | | * Return a string representing the address in data, or else the "far end" |
97 | | * address if data is NULL. |
98 | | */ |
99 | | |
100 | | static char * |
101 | | netsnmp_tlstcp_fmtaddr(netsnmp_transport *t, const void *data, int len) |
102 | 0 | { |
103 | 0 | if (t && !data) { |
104 | 0 | data = t->data; |
105 | 0 | len = t->data_length; |
106 | 0 | } |
107 | |
|
108 | 0 | switch (data ? len : 0) { |
109 | 0 | case sizeof(netsnmp_indexed_addr_pair): |
110 | 0 | #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN |
111 | 0 | if (t && t->base_transport && |
112 | 0 | t->base_transport->domain == netsnmp_TCPIPv6Domain) |
113 | 0 | return netsnmp_ipv6_fmtaddr("TLSTCP", t, data, len); |
114 | 0 | #endif |
115 | 0 | return netsnmp_ipv4_fmtaddr("TLSTCP", t, data, len); |
116 | 0 | case sizeof(netsnmp_tmStateReference): { |
117 | 0 | const netsnmp_tmStateReference *r = data; |
118 | 0 | const netsnmp_indexed_addr_pair *p = &r->addresses; |
119 | |
|
120 | 0 | #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN |
121 | 0 | if (t && t->base_transport && |
122 | 0 | t->base_transport->domain == netsnmp_TCPIPv6Domain) |
123 | 0 | return netsnmp_ipv6_fmtaddr("TLSTCP", t, p, sizeof(*p)); |
124 | 0 | #endif |
125 | 0 | return netsnmp_ipv4_fmtaddr("TLSTCP", t, p, sizeof(*p)); |
126 | 0 | } |
127 | 0 | case sizeof(_netsnmpTLSBaseData): { |
128 | 0 | const _netsnmpTLSBaseData *b = data; |
129 | 0 | char *buf; |
130 | |
|
131 | 0 | if (asprintf(&buf, "TLSTCP: %s", b->addr_string) < 0) |
132 | 0 | buf = NULL; |
133 | 0 | return buf; |
134 | 0 | } |
135 | 0 | case 0: |
136 | 0 | return strdup("TLSTCP: unknown"); |
137 | 0 | default: { |
138 | 0 | char *buf; |
139 | |
|
140 | 0 | if (asprintf(&buf, "TLSTCP: len %d", len) < 0) |
141 | 0 | buf = NULL; |
142 | 0 | return buf; |
143 | 0 | } |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | | static void |
148 | | _tlstcp_sync_socks(netsnmp_transport *t) |
149 | 0 | { |
150 | 0 | if (t && t->base_transport && t->base_transport->sock != t->sock) { |
151 | 0 | DEBUGMSGTL(("tlstcp", "Syncing sockets: base %" NETSNMP_FMT_SKT " -> main %" NETSNMP_FMT_SKT "\n", |
152 | 0 | t->base_transport->sock, t->sock)); |
153 | 0 | t->base_transport->sock = t->sock; |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | static void netsnmp_tlstcp_get_taddr(struct netsnmp_transport_s *t, |
158 | | void **addr, size_t *addr_len) |
159 | 0 | { |
160 | 0 | *addr_len = t->remote_length; |
161 | 0 | *addr = netsnmp_memdup(t->remote, *addr_len); |
162 | 0 | } |
163 | | |
164 | | /* |
165 | | * You can write something into opaque that will subsequently get passed back |
166 | | * to your send function if you like. For instance, you might want to |
167 | | * remember where a PDU came from, so that you can send a reply there... |
168 | | */ |
169 | | |
170 | | static int |
171 | | netsnmp_tlstcp_copy(const netsnmp_transport *oldt, netsnmp_transport *newt) |
172 | 0 | { |
173 | 0 | _netsnmpTLSBaseData *oldtlsdata = (_netsnmpTLSBaseData *) oldt->data; |
174 | 0 | _netsnmpTLSBaseData *newtlsdata = (_netsnmpTLSBaseData *) newt->data; |
175 | |
|
176 | 0 | newtlsdata->ssl = oldtlsdata->ssl; |
177 | 0 | oldtlsdata->ssl = NULL; |
178 | 0 | newtlsdata->ssl_context = NULL; |
179 | 0 | newtlsdata->accept_bio = NULL; |
180 | 0 | newtlsdata->handshake_state = oldtlsdata->handshake_state; |
181 | 0 | oldtlsdata->handshake_state = NETSNMP_TLS_HANDSHAKE_IDLE; |
182 | 0 | newtlsdata->flags = oldtlsdata->flags; |
183 | |
|
184 | 0 | if (oldtlsdata->addr_string) |
185 | 0 | newtlsdata->addr_string = strdup(oldtlsdata->addr_string); |
186 | 0 | if (oldtlsdata->securityName) |
187 | 0 | newtlsdata->securityName = strdup(oldtlsdata->securityName); |
188 | 0 | if (oldtlsdata->our_identity) |
189 | 0 | newtlsdata->our_identity = strdup(oldtlsdata->our_identity); |
190 | 0 | if (oldtlsdata->their_identity) |
191 | 0 | newtlsdata->their_identity = strdup(oldtlsdata->their_identity); |
192 | 0 | if (oldtlsdata->their_fingerprint) |
193 | 0 | newtlsdata->their_fingerprint = strdup(oldtlsdata->their_fingerprint); |
194 | 0 | if (oldtlsdata->their_hostname) |
195 | 0 | newtlsdata->their_hostname = strdup(oldtlsdata->their_hostname); |
196 | 0 | if (oldtlsdata->trust_cert) |
197 | 0 | newtlsdata->trust_cert = strdup(oldtlsdata->trust_cert); |
198 | 0 | if (oldtlsdata->addr) { |
199 | 0 | newtlsdata->addr = netsnmp_memdup(oldtlsdata->addr, |
200 | 0 | sizeof(*oldtlsdata->addr)); |
201 | 0 | if (newtlsdata->addr) |
202 | 0 | newtlsdata->addr_len = sizeof(*newtlsdata->addr); |
203 | 0 | } |
204 | |
|
205 | 0 | return 0; |
206 | 0 | } |
207 | | |
208 | | static int |
209 | | _tlstcp_send_all(netsnmp_transport *base_transport, const void *buf, int len) |
210 | 0 | { |
211 | 0 | const char *p = (const char *)buf; |
212 | 0 | int remaining = len; |
213 | |
|
214 | 0 | if (!base_transport || !base_transport->f_send) |
215 | 0 | return -1; |
216 | | |
217 | 0 | while (remaining > 0) { |
218 | 0 | int sent = |
219 | 0 | base_transport->f_send(base_transport, p, remaining, NULL, NULL); |
220 | 0 | if (sent < 0) { |
221 | 0 | if (errno == EINTR) |
222 | 0 | continue; |
223 | 0 | return -1; |
224 | 0 | } |
225 | 0 | if (sent == 0) |
226 | 0 | return -1; |
227 | 0 | p += sent; |
228 | 0 | remaining -= sent; |
229 | 0 | } |
230 | 0 | return len; |
231 | 0 | } |
232 | | |
233 | | static int |
234 | | _tlstcp_socket_send_all(NETSNMP_SOCKET sock, const void *buf, int len) |
235 | 0 | { |
236 | 0 | const char *p = (const char *)buf; |
237 | 0 | int remaining = len; |
238 | |
|
239 | 0 | while (remaining > 0) { |
240 | 0 | int sent = send(sock, p, remaining, 0); |
241 | 0 | if (sent < 0) { |
242 | 0 | if (errno == EINTR) |
243 | 0 | continue; |
244 | 0 | return -1; |
245 | 0 | } |
246 | 0 | if (sent == 0) |
247 | 0 | return -1; |
248 | 0 | p += sent; |
249 | 0 | remaining -= sent; |
250 | 0 | } |
251 | 0 | return len; |
252 | 0 | } |
253 | | |
254 | | static int |
255 | | _tlstcp_step_handshake(netsnmp_transport *t) |
256 | 0 | { |
257 | 0 | _netsnmpTLSBaseData *tlsdata = (_netsnmpTLSBaseData *) t->data; |
258 | 0 | SSL *ssl = tlsdata->ssl; |
259 | 0 | BIO *write_bio; |
260 | 0 | char out_buf[4096]; |
261 | 0 | int out_bytes; |
262 | 0 | int rc; |
263 | |
|
264 | 0 | if (!ssl) |
265 | 0 | return -1; |
266 | | |
267 | 0 | write_bio = SSL_get_wbio(ssl); |
268 | |
|
269 | 0 | rc = SSL_do_handshake(ssl); |
270 | | |
271 | | /* Flush any handshake messages produced */ |
272 | 0 | if (write_bio) { |
273 | 0 | while ((out_bytes = BIO_read(write_bio, out_buf, sizeof(out_buf))) > 0) { |
274 | 0 | if (_tlstcp_send_all(t->base_transport, out_buf, out_bytes) < 0) { |
275 | 0 | tlsdata->handshake_state = NETSNMP_TLS_HANDSHAKE_FAILED; |
276 | 0 | return -1; |
277 | 0 | } |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | 0 | if (rc == 1) { |
282 | 0 | int vrfy; |
283 | 0 | if (tlsdata->flags & NETSNMP_TLSBASE_IS_CLIENT) { |
284 | 0 | vrfy = netsnmp_tlsbase_verify_server_cert(ssl, tlsdata); |
285 | 0 | if (vrfy != SNMPERR_SUCCESS) { |
286 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONUNKNOWNSERVERCERTIFICATE); |
287 | 0 | snmp_log(LOG_ERR, "tlstcp: failed to verify ssl certificate\n"); |
288 | 0 | tlsdata->handshake_state = NETSNMP_TLS_HANDSHAKE_FAILED; |
289 | 0 | return -1; |
290 | 0 | } |
291 | 0 | } else { |
292 | 0 | vrfy = netsnmp_tlsbase_verify_client_cert(ssl, tlsdata); |
293 | 0 | if (vrfy != SNMPERR_SUCCESS) { |
294 | 0 | snmp_log(LOG_ERR, "TLSTCP: Failed checking client certificate\n"); |
295 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONINVALIDCLIENTCERTIFICATES); |
296 | 0 | snmp_log(LOG_ERR, "TLSTCP: Failed SSL_accept\n"); |
297 | 0 | tlsdata->handshake_state = NETSNMP_TLS_HANDSHAKE_FAILED; |
298 | 0 | return -1; |
299 | 0 | } |
300 | 0 | } |
301 | 0 | tlsdata->handshake_state = NETSNMP_TLS_HANDSHAKE_COMPLETE; |
302 | 0 | return 1; |
303 | 0 | } |
304 | | |
305 | 0 | { |
306 | 0 | int err = SSL_get_error(ssl, rc); |
307 | 0 | if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { |
308 | 0 | return 0; |
309 | 0 | } |
310 | 0 | } |
311 | | |
312 | 0 | tlsdata->handshake_state = NETSNMP_TLS_HANDSHAKE_FAILED; |
313 | 0 | if (!(tlsdata->flags & NETSNMP_TLSBASE_IS_CLIENT)) |
314 | 0 | snmp_log(LOG_ERR, "TLSTCP: Failed SSL_accept\n"); |
315 | 0 | else |
316 | 0 | snmp_log(LOG_ERR, "tlstcp: failed to ssl_connect\n"); |
317 | 0 | _openssl_log_error(rc, ssl, "SSL_do_handshake"); |
318 | 0 | return -1; |
319 | 0 | } |
320 | | |
321 | | static int |
322 | | _tlstcp_add_buffered_data(_netsnmpTLSBaseData *tlsdata, const void *buf, size_t size) |
323 | 0 | { |
324 | 0 | if (tlsdata->write_cache && tlsdata->write_cache_len > 0) { |
325 | 0 | size_t newsize = tlsdata->write_cache_len + size; |
326 | 0 | char *newbuf = realloc(tlsdata->write_cache, newsize); |
327 | 0 | if (!newbuf) |
328 | 0 | return -1; |
329 | 0 | tlsdata->write_cache = newbuf; |
330 | 0 | memcpy(tlsdata->write_cache + tlsdata->write_cache_len, buf, size); |
331 | 0 | tlsdata->write_cache_len = newsize; |
332 | 0 | } else { |
333 | 0 | SNMP_FREE(tlsdata->write_cache); |
334 | 0 | tlsdata->write_cache = netsnmp_memdup(buf, size); |
335 | 0 | if (!tlsdata->write_cache) |
336 | 0 | return -1; |
337 | 0 | tlsdata->write_cache_len = size; |
338 | 0 | } |
339 | 0 | return 0; |
340 | 0 | } |
341 | | |
342 | | static int |
343 | | _tlstcp_flush_buffered_data(netsnmp_transport *t) |
344 | 0 | { |
345 | 0 | _netsnmpTLSBaseData *tlsdata = (_netsnmpTLSBaseData *) t->data; |
346 | 0 | SSL *ssl = tlsdata->ssl; |
347 | 0 | BIO *write_bio; |
348 | 0 | char out_buf[4096]; |
349 | 0 | int out_bytes; |
350 | 0 | int rc; |
351 | |
|
352 | 0 | if (!tlsdata->write_cache || tlsdata->write_cache_len == 0) |
353 | 0 | return 0; |
354 | | |
355 | 0 | if (tlsdata->handshake_state != NETSNMP_TLS_HANDSHAKE_COMPLETE) |
356 | 0 | return 0; |
357 | | |
358 | 0 | write_bio = SSL_get_wbio(ssl); |
359 | |
|
360 | 0 | rc = SSL_write(ssl, tlsdata->write_cache, tlsdata->write_cache_len); |
361 | 0 | if (rc > 0) { |
362 | 0 | if ((size_t)rc >= tlsdata->write_cache_len) { |
363 | 0 | SNMP_FREE(tlsdata->write_cache); |
364 | 0 | tlsdata->write_cache_len = 0; |
365 | 0 | } else { |
366 | 0 | memmove(tlsdata->write_cache, tlsdata->write_cache + rc, |
367 | 0 | tlsdata->write_cache_len - rc); |
368 | 0 | tlsdata->write_cache_len -= rc; |
369 | 0 | } |
370 | 0 | } |
371 | |
|
372 | 0 | if (write_bio) { |
373 | 0 | while ((out_bytes = BIO_read(write_bio, out_buf, sizeof(out_buf))) > 0) { |
374 | 0 | if (_tlstcp_send_all(t->base_transport, out_buf, out_bytes) < 0) |
375 | 0 | return -1; |
376 | 0 | } |
377 | 0 | } |
378 | | |
379 | 0 | return 0; |
380 | 0 | } |
381 | | |
382 | | static int |
383 | | netsnmp_tlstcp_recv(netsnmp_transport *t, void *buf, int size, |
384 | | void **opaque, int *olength) |
385 | 0 | { |
386 | 0 | int rc = -1; |
387 | 0 | netsnmp_tmStateReference *tmStateRef = NULL; |
388 | 0 | _netsnmpTLSBaseData *tlsdata; |
389 | 0 | int bytes_read_total = 0; |
390 | 0 | char raw_buf[4096]; |
391 | 0 | char out_buf[4096]; |
392 | 0 | int bytes_read; |
393 | 0 | BIO *write_bio; |
394 | 0 | BIO *read_bio; |
395 | 0 | int out_bytes; |
396 | 0 | int got_eof = 0; |
397 | 0 | SSL *ssl; |
398 | 0 | int err; |
399 | 0 | int pending_before; |
400 | |
|
401 | 0 | if (!t || !NETSNMP_IS_VALID_SOCKET(t->sock) || !t->data) { |
402 | 0 | snmp_log(LOG_ERR, |
403 | 0 | "tlstcp received an invalid invocation with missing data\n"); |
404 | 0 | DEBUGMSGTL(("tlstcp", "recvfrom fd %" NETSNMP_FMT_SKT " err %d (\"%s\")\n", |
405 | 0 | (t ? t->sock : NETSNMP_INVALID_SOCKET), errno, |
406 | 0 | strerror(errno))); |
407 | 0 | if (t) |
408 | 0 | DEBUGMSGTL(("tlstcp", " tdata = %p", t->data)); |
409 | 0 | DEBUGMSGTL(("tlstcp", "\n")); |
410 | 0 | return -1; |
411 | 0 | } |
412 | | |
413 | 0 | _tlstcp_sync_socks(t); |
414 | |
|
415 | 0 | tlsdata = t->data; |
416 | 0 | ssl = tlsdata->ssl; |
417 | 0 | if (!ssl) { |
418 | 0 | snmp_log(LOG_ERR, |
419 | 0 | "tlstcp received an invalid invocation without ssl data\n"); |
420 | 0 | return -1; |
421 | 0 | } |
422 | | |
423 | 0 | read_bio = SSL_get_rbio(ssl); |
424 | 0 | write_bio = SSL_get_wbio(ssl); |
425 | | |
426 | | /* 1. Read raw ciphertext from base transport */ |
427 | 0 | do { |
428 | 0 | void *recv_opaque = NULL; |
429 | 0 | int recv_olen = 0; |
430 | |
|
431 | 0 | bytes_read = t->base_transport->f_recv(t->base_transport, raw_buf, |
432 | 0 | sizeof(raw_buf), &recv_opaque, |
433 | 0 | &recv_olen); |
434 | 0 | free(recv_opaque); |
435 | |
|
436 | 0 | DEBUGMSGTL(("tlstcp", "base recv returned %d\n", bytes_read)); |
437 | 0 | if (bytes_read > 0) { |
438 | 0 | bytes_read_total += bytes_read; |
439 | 0 | BIO_write(read_bio, raw_buf, bytes_read); |
440 | 0 | DEBUGMSGTL(("tlstcp", "wrote %d bytes of ciphertext to read_bio\n", |
441 | 0 | bytes_read)); |
442 | 0 | } else if (bytes_read == 0) { |
443 | 0 | DEBUGMSGTL(("tlstcp", "remote side closed connection\n")); |
444 | 0 | got_eof = 1; |
445 | 0 | break; |
446 | 0 | } else { |
447 | 0 | if (errno != EAGAIN && |
448 | 0 | (EAGAIN == EWOULDBLOCK || errno != EWOULDBLOCK) && |
449 | 0 | errno != EINTR) { |
450 | 0 | DEBUGMSGTL(("tlstcp", "base recv error: %d\n", errno)); |
451 | 0 | if (bytes_read_total > 0) { |
452 | 0 | got_eof = 1; |
453 | 0 | break; |
454 | 0 | } |
455 | 0 | return -1; |
456 | 0 | } |
457 | 0 | } |
458 | 0 | } while (bytes_read > 0); |
459 | | |
460 | 0 | if (bytes_read_total > 0) |
461 | 0 | tlsdata->want_read = 0; |
462 | | |
463 | | /* 2. Advance handshake if in progress */ |
464 | 0 | if (tlsdata->handshake_state == NETSNMP_TLS_HANDSHAKE_IN_PROGRESS) { |
465 | 0 | int h_rc = _tlstcp_step_handshake(t); |
466 | 0 | if (h_rc < 0) |
467 | 0 | return -1; |
468 | 0 | if (tlsdata->handshake_state != NETSNMP_TLS_HANDSHAKE_COMPLETE) { |
469 | 0 | t->flags |= NETSNMP_TRANSPORT_FLAG_EMPTY_PKT; |
470 | 0 | *opaque = NULL; |
471 | 0 | *olength = 0; |
472 | 0 | return 0; |
473 | 0 | } |
474 | 0 | } |
475 | | |
476 | 0 | if (tlsdata->handshake_state != NETSNMP_TLS_HANDSHAKE_COMPLETE) { |
477 | 0 | if (got_eof) |
478 | 0 | return -1; |
479 | 0 | t->flags |= NETSNMP_TRANSPORT_FLAG_EMPTY_PKT; |
480 | 0 | *opaque = NULL; |
481 | 0 | *olength = 0; |
482 | 0 | return 0; |
483 | 0 | } |
484 | | |
485 | | /* 3. Flush any buffered outgoing writes */ |
486 | 0 | if (tlsdata->write_cache_len > 0) |
487 | 0 | _tlstcp_flush_buffered_data(t); |
488 | | |
489 | | /* 4. Read decrypted plaintext from SSL */ |
490 | 0 | tmStateRef = SNMP_MALLOC_TYPEDEF(netsnmp_tmStateReference); |
491 | |
|
492 | 0 | if (tmStateRef == NULL) { |
493 | 0 | *opaque = NULL; |
494 | 0 | *olength = 0; |
495 | 0 | return -1; |
496 | 0 | } |
497 | | |
498 | | /* Set the transportDomain */ |
499 | 0 | memcpy(tmStateRef->transportDomain, |
500 | 0 | netsnmpTLSTCPDomain, sizeof(netsnmpTLSTCPDomain[0]) * |
501 | 0 | netsnmpTLSTCPDomain_len); |
502 | 0 | tmStateRef->transportDomainLen = netsnmpTLSTCPDomain_len; |
503 | 0 | if (tlsdata->addr) { |
504 | 0 | memcpy(&tmStateRef->addresses, tlsdata->addr, |
505 | 0 | sizeof(netsnmp_indexed_addr_pair)); |
506 | 0 | tmStateRef->have_addresses = 1; |
507 | 0 | } else { |
508 | 0 | tmStateRef->have_addresses = 0; |
509 | 0 | } |
510 | |
|
511 | 0 | do { |
512 | 0 | pending_before = read_bio ? BIO_pending(read_bio) : 0; |
513 | |
|
514 | 0 | rc = SSL_read(ssl, buf, size); |
515 | 0 | MAKE_MEM_DEFINED(&rc, sizeof(rc)); |
516 | 0 | if (rc > 0) { |
517 | 0 | MAKE_MEM_DEFINED(buf, rc); |
518 | 0 | tlsdata->want_read = 0; |
519 | 0 | break; |
520 | 0 | } |
521 | | |
522 | | /* Check if SSL wants to write something */ |
523 | 0 | while ((out_bytes = BIO_read(write_bio, out_buf, sizeof(out_buf))) > 0) { |
524 | 0 | DEBUGMSGTL(("tlstcp", |
525 | 0 | "recv: writing %d bytes of ciphertext to base transport\n", |
526 | 0 | out_bytes)); |
527 | 0 | if (_tlstcp_send_all(t->base_transport, out_buf, out_bytes) < 0) { |
528 | 0 | DEBUGMSGTL( |
529 | 0 | ("tlstcp", "recv: failed to send ciphertext reply\n")); |
530 | 0 | } |
531 | 0 | } |
532 | |
|
533 | 0 | err = SSL_get_error(ssl, rc); |
534 | 0 | if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) |
535 | 0 | break; |
536 | 0 | if (!read_bio || BIO_pending(read_bio) >= pending_before) |
537 | 0 | break; |
538 | 0 | } while (read_bio && BIO_pending(read_bio) > 0); |
539 | |
|
540 | 0 | err = SSL_get_error(ssl, rc); |
541 | | |
542 | | /* Check if SSL wants to write something */ |
543 | 0 | while ((out_bytes = BIO_read(write_bio, out_buf, sizeof(out_buf))) > 0) { |
544 | 0 | DEBUGMSGTL(("tlstcp", |
545 | 0 | "recv: writing %d bytes of ciphertext to base transport\n", |
546 | 0 | out_bytes)); |
547 | 0 | if (_tlstcp_send_all(t->base_transport, out_buf, out_bytes) < 0) { |
548 | 0 | DEBUGMSGTL(("tlstcp", "recv: failed to send ciphertext reply\n")); |
549 | 0 | } |
550 | 0 | } |
551 | |
|
552 | 0 | if (rc <= 0) { |
553 | 0 | if ((err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) && |
554 | 0 | !got_eof) { |
555 | 0 | tlsdata->want_read = 1; |
556 | 0 | DEBUGMSGTL(("tlstcp", "SSL_read wanted read/write (want_read=1)\n")); |
557 | 0 | t->flags |= NETSNMP_TRANSPORT_FLAG_EMPTY_PKT; |
558 | 0 | SNMP_FREE(tmStateRef); |
559 | 0 | return 0; |
560 | 0 | } |
561 | 0 | if (rc == 0 || err == SSL_ERROR_ZERO_RETURN || got_eof) { |
562 | 0 | DEBUGMSGTL(("tlstcp", |
563 | 0 | "remote side closed connection (SSL or socket)\n")); |
564 | 0 | SNMP_FREE(tmStateRef); |
565 | 0 | return -1; |
566 | 0 | } |
567 | 0 | _openssl_log_error(rc, ssl, "SSL_read"); |
568 | 0 | SNMP_FREE(tmStateRef); |
569 | 0 | return rc; |
570 | 0 | } |
571 | | |
572 | 0 | DEBUGMSGTL(("tlstcp", "received %d decoded bytes from tls\n", rc)); |
573 | | |
574 | | /* log the packet */ |
575 | 0 | DEBUGIF("tlstcp") { |
576 | 0 | char *str = netsnmp_tlstcp_fmtaddr(t, NULL, 0); |
577 | 0 | DEBUGMSGTL(("tlstcp", |
578 | 0 | "recvfrom fd %" NETSNMP_FMT_SKT " got %d bytes (from %s)\n", |
579 | 0 | t->sock, rc, str)); |
580 | 0 | free(str); |
581 | 0 | } |
582 | | |
583 | | /* Other wrap-up things common to TLS and DTLS */ |
584 | 0 | if (netsnmp_tlsbase_wrapup_recv(tmStateRef, tlsdata, opaque, olength) != |
585 | 0 | SNMPERR_SUCCESS) |
586 | 0 | return SNMPERR_GENERR; |
587 | | |
588 | 0 | return rc; |
589 | 0 | } |
590 | | |
591 | | static int |
592 | | netsnmp_tlstcp_pending(netsnmp_transport *t) |
593 | 0 | { |
594 | 0 | _netsnmpTLSBaseData *tlsdata = t->data; |
595 | |
|
596 | 0 | if (tlsdata && tlsdata->ssl) { |
597 | 0 | if (tlsdata->handshake_state == NETSNMP_TLS_HANDSHAKE_COMPLETE) { |
598 | 0 | BIO *read_bio = SSL_get_rbio(tlsdata->ssl); |
599 | 0 | int ssl_p = SSL_pending(tlsdata->ssl); |
600 | 0 | int bio_p = read_bio ? BIO_pending(read_bio) : 0; |
601 | |
|
602 | 0 | if (ssl_p > 0 || (bio_p > 0 && !tlsdata->want_read)) { |
603 | 0 | DEBUGMSGTL(("tlstcp", "pending: SSL %d, BIO %d (want_read %d)\n", |
604 | 0 | ssl_p, bio_p, tlsdata->want_read)); |
605 | 0 | return 1; |
606 | 0 | } |
607 | 0 | } |
608 | 0 | } |
609 | 0 | return 0; |
610 | 0 | } |
611 | | |
612 | | static int |
613 | | netsnmp_tlstcp_send(netsnmp_transport *t, const void *buf, int size, |
614 | | void **opaque, int *olength) |
615 | 0 | { |
616 | 0 | int rc = -1; |
617 | 0 | const netsnmp_tmStateReference *tmStateRef = NULL; |
618 | 0 | _netsnmpTLSBaseData *tlsdata; |
619 | 0 | char out_buf[4096]; |
620 | 0 | BIO *write_bio; |
621 | 0 | int out_bytes; |
622 | 0 | SSL *ssl; |
623 | |
|
624 | 0 | DEBUGTRACETOK("tlstcp"); |
625 | |
|
626 | 0 | if (opaque != NULL && *opaque != NULL && |
627 | 0 | *olength == sizeof(netsnmp_tmStateReference)) { |
628 | 0 | tmStateRef = (const netsnmp_tmStateReference *) *opaque; |
629 | 0 | } else { |
630 | 0 | snmp_log(LOG_ERR, "TLSTCP was called with an invalid state; possibly the wrong security model is in use. It should be 'tsm'.\n"); |
631 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONINVALIDCACHES); |
632 | 0 | return SNMPERR_GENERR; |
633 | 0 | } |
634 | | |
635 | 0 | if (NULL == t->data) { |
636 | 0 | snmp_log(LOG_ERR, "netsnmp_tlstcp_send received no incoming data\n"); |
637 | 0 | return -1; |
638 | 0 | } |
639 | | |
640 | 0 | _tlstcp_sync_socks(t); |
641 | |
|
642 | 0 | tlsdata = t->data; |
643 | 0 | ssl = tlsdata->ssl; |
644 | |
|
645 | 0 | if (!ssl) { |
646 | 0 | snmp_log(LOG_ERR, "tlstcp_send was called without a SSL connection.\n"); |
647 | 0 | return SNMPERR_GENERR; |
648 | 0 | } |
649 | | |
650 | 0 | if ((tlsdata->flags & NETSNMP_TLSBASE_IS_CLIENT) && |
651 | 0 | !tlsdata->securityName && tmStateRef && tmStateRef->securityNameLen > 0) |
652 | 0 | tlsdata->securityName = strdup(tmStateRef->securityName); |
653 | | |
654 | | /* If handshake is still in progress or we have buffered data, queue this packet */ |
655 | 0 | if (tlsdata->handshake_state != NETSNMP_TLS_HANDSHAKE_COMPLETE || |
656 | 0 | tlsdata->write_cache_len > 0) { |
657 | 0 | if (_tlstcp_add_buffered_data(tlsdata, buf, size) < 0) { |
658 | 0 | snmp_log(LOG_ERR, "tlstcp_send: failed to buffer data\n"); |
659 | 0 | return -1; |
660 | 0 | } |
661 | 0 | if (tlsdata->handshake_state == NETSNMP_TLS_HANDSHAKE_IN_PROGRESS) |
662 | 0 | _tlstcp_step_handshake(t); |
663 | 0 | if (tlsdata->handshake_state == NETSNMP_TLS_HANDSHAKE_COMPLETE) |
664 | 0 | _tlstcp_flush_buffered_data(t); |
665 | 0 | return size; |
666 | 0 | } |
667 | | |
668 | 0 | write_bio = SSL_get_wbio(ssl); |
669 | |
|
670 | 0 | rc = SSL_write(ssl, buf, size); |
671 | 0 | DEBUGMSGTL(("tlstcp", "wrote %d bytes to ssl\n", size)); |
672 | 0 | if (rc <= 0) { |
673 | 0 | int err = SSL_get_error(ssl, rc); |
674 | 0 | if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { |
675 | | /* Buffer data to be retried when socket is ready */ |
676 | 0 | if (_tlstcp_add_buffered_data(tlsdata, buf, size) < 0) { |
677 | 0 | snmp_log(LOG_ERR, "tlstcp_send: failed to buffer data\n"); |
678 | 0 | return -1; |
679 | 0 | } |
680 | 0 | rc = size; |
681 | 0 | } else { |
682 | 0 | _openssl_log_error(rc, ssl, "SSL_write"); |
683 | 0 | return rc; |
684 | 0 | } |
685 | 0 | } |
686 | | |
687 | 0 | if (write_bio) { |
688 | 0 | while ((out_bytes = BIO_read(write_bio, out_buf, sizeof(out_buf))) > 0) { |
689 | 0 | int sent = _tlstcp_send_all(t->base_transport, out_buf, out_bytes); |
690 | 0 | if (sent < 0) { |
691 | 0 | snmp_log(LOG_ERR, "TLSTCP: send failed\n"); |
692 | 0 | return -1; |
693 | 0 | } |
694 | 0 | } |
695 | 0 | } |
696 | | |
697 | 0 | return rc; |
698 | 0 | } |
699 | | |
700 | | |
701 | | |
702 | | static void |
703 | | _tlstcp_drain(netsnmp_transport *t) |
704 | 0 | { |
705 | 0 | _netsnmpTLSBaseData *tlsdata = t->data; |
706 | 0 | int i; |
707 | 0 | char drain_buf[4096]; |
708 | 0 | void *opaque = NULL; |
709 | 0 | int olen = 0; |
710 | 0 | fd_set readfs; |
711 | 0 | struct timeval tv; |
712 | |
|
713 | 0 | if (!tlsdata || !tlsdata->ssl || |
714 | 0 | (tlsdata->write_cache_len == 0 && |
715 | 0 | tlsdata->handshake_state != NETSNMP_TLS_HANDSHAKE_IN_PROGRESS)) |
716 | 0 | return; |
717 | | |
718 | 0 | for (i = 0; i < 10 && (tlsdata->write_cache_len > 0 || |
719 | 0 | tlsdata->handshake_state == NETSNMP_TLS_HANDSHAKE_IN_PROGRESS); ++i) { |
720 | 0 | if (tlsdata->handshake_state == NETSNMP_TLS_HANDSHAKE_IN_PROGRESS) |
721 | 0 | _tlstcp_step_handshake(t); |
722 | 0 | if (tlsdata->handshake_state == NETSNMP_TLS_HANDSHAKE_COMPLETE && |
723 | 0 | tlsdata->write_cache_len > 0) |
724 | 0 | _tlstcp_flush_buffered_data(t); |
725 | 0 | if (tlsdata->write_cache_len == 0 && |
726 | 0 | tlsdata->handshake_state == NETSNMP_TLS_HANDSHAKE_COMPLETE) |
727 | 0 | break; |
728 | | |
729 | 0 | if (t->sock < 0 || t->sock >= FD_SETSIZE) |
730 | 0 | break; |
731 | 0 | FD_ZERO(&readfs); |
732 | 0 | FD_SET(t->sock, &readfs); |
733 | 0 | tv.tv_sec = 0; |
734 | 0 | tv.tv_usec = 50000; |
735 | 0 | if (select(t->sock + 1, &readfs, NULL, NULL, &tv) > 0) { |
736 | 0 | netsnmp_tlstcp_recv(t, drain_buf, sizeof(drain_buf), &opaque, &olen); |
737 | 0 | SNMP_FREE(opaque); |
738 | 0 | } |
739 | 0 | } |
740 | 0 | } |
741 | | |
742 | | static int |
743 | | netsnmp_tlstcp_close(netsnmp_transport *t) |
744 | 0 | { |
745 | 0 | _netsnmpTLSBaseData *tlsdata; |
746 | 0 | char out_buf[4096]; |
747 | 0 | BIO *write_bio; |
748 | 0 | int out_bytes; |
749 | 0 | int rc = 0; |
750 | |
|
751 | 0 | if (NULL == t || NULL == t->data) |
752 | 0 | return -1; |
753 | | |
754 | 0 | tlsdata = t->data; |
755 | |
|
756 | 0 | if (tlsdata->flags & NETSNMP_TLSBASE_IS_CLIENT) |
757 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONCLIENTCLOSES); |
758 | 0 | else |
759 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONSERVERCLOSES); |
760 | |
|
761 | 0 | _tlstcp_sync_socks(t); |
762 | |
|
763 | 0 | _tlstcp_drain(t); |
764 | |
|
765 | 0 | DEBUGMSGTL(("tlstcp", "Shutting down SSL connection\n")); |
766 | 0 | if (tlsdata->ssl) { |
767 | 0 | SSL_shutdown(tlsdata->ssl); |
768 | | /* Flush write_bio */ |
769 | 0 | write_bio = SSL_get_wbio(tlsdata->ssl); |
770 | 0 | if (write_bio) { |
771 | 0 | while ((out_bytes = BIO_read(write_bio, out_buf, sizeof(out_buf))) > |
772 | 0 | 0) { |
773 | 0 | if (t->base_transport) { |
774 | 0 | if (_tlstcp_send_all(t->base_transport, out_buf, |
775 | 0 | out_bytes) < 0) |
776 | 0 | break; |
777 | 0 | } else if (NETSNMP_IS_VALID_SOCKET(t->sock)) { |
778 | 0 | if (_tlstcp_socket_send_all(t->sock, out_buf, out_bytes) < |
779 | 0 | 0) |
780 | 0 | break; |
781 | 0 | } |
782 | 0 | } |
783 | 0 | } |
784 | 0 | } |
785 | |
|
786 | 0 | netsnmp_tlsbase_free_tlsdata(tlsdata); |
787 | 0 | t->data = NULL; |
788 | |
|
789 | 0 | if (t->base_transport) { |
790 | 0 | rc = t->base_transport->f_close(t->base_transport); |
791 | 0 | netsnmp_transport_free(t->base_transport); |
792 | 0 | t->base_transport = NULL; |
793 | 0 | t->sock = NETSNMP_INVALID_SOCKET; |
794 | 0 | } else if (NETSNMP_IS_VALID_SOCKET(t->sock)) { |
795 | 0 | rc = netsnmp_socketbase_close(t); |
796 | 0 | } |
797 | |
|
798 | 0 | return rc; |
799 | 0 | } |
800 | | |
801 | | static NETSNMP_SOCKET |
802 | | netsnmp_tlstcp_accept(netsnmp_transport *t) |
803 | 0 | { |
804 | 0 | _netsnmpTLSBaseData *tlsdata = t->data; |
805 | 0 | _netsnmp_verify_info *verify_info; |
806 | 0 | BIO *read_bio, *write_bio; |
807 | 0 | NETSNMP_SOCKET newsock; |
808 | 0 | SSL *ssl; |
809 | |
|
810 | 0 | DEBUGMSGTL(("tlstcp", "netsnmp_tlstcp_accept called\n")); |
811 | |
|
812 | 0 | if (!t->base_transport) { |
813 | 0 | snmp_log(LOG_ERR, "TLSTCP: missing base transport in accept\n"); |
814 | 0 | return NETSNMP_INVALID_SOCKET; |
815 | 0 | } |
816 | | |
817 | 0 | newsock = t->base_transport->f_accept(t->base_transport); |
818 | 0 | if (!NETSNMP_IS_VALID_SOCKET(newsock)) |
819 | 0 | return NETSNMP_INVALID_SOCKET; |
820 | | |
821 | 0 | if (t->base_transport->data) { |
822 | 0 | if (!tlsdata->addr) { |
823 | 0 | tlsdata->addr = malloc(sizeof(netsnmp_indexed_addr_pair)); |
824 | 0 | } |
825 | 0 | if (tlsdata->addr) { |
826 | 0 | memcpy(tlsdata->addr, t->base_transport->data, |
827 | 0 | sizeof(netsnmp_indexed_addr_pair)); |
828 | 0 | tlsdata->addr_len = sizeof(netsnmp_indexed_addr_pair); |
829 | 0 | _tlstcp_format_addr_string(tlsdata); |
830 | 0 | } |
831 | 0 | } |
832 | |
|
833 | 0 | if (netsnmp_set_non_blocking_mode(newsock, TRUE) < 0) |
834 | 0 | DEBUGMSGTL(("tlstcp", |
835 | 0 | "couldn't set non-blocking mode on fd %" NETSNMP_FMT_SKT "\n", |
836 | 0 | newsock)); |
837 | |
|
838 | 0 | ssl = SSL_new(tlsdata->ssl_context); |
839 | 0 | if (!ssl) { |
840 | 0 | snmp_log(LOG_ERR, |
841 | 0 | "TLSTCP: Failed to create SSL object for accepted connection\n"); |
842 | 0 | goto close_newsock; |
843 | 0 | } |
844 | | |
845 | 0 | read_bio = BIO_new(BIO_s_mem()); |
846 | 0 | write_bio = BIO_new(BIO_s_mem()); |
847 | 0 | if (!read_bio || !write_bio) { |
848 | 0 | snmp_log(LOG_ERR, "TLSTCP: Failed to create memory BIOs\n"); |
849 | 0 | BIO_free(read_bio); |
850 | 0 | BIO_free(write_bio); |
851 | 0 | goto free_ssl; |
852 | 0 | } |
853 | 0 | SSL_set_bio(ssl, read_bio, write_bio); |
854 | 0 | SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); |
855 | |
|
856 | 0 | verify_info = SNMP_MALLOC_TYPEDEF(_netsnmp_verify_info); |
857 | 0 | if (NULL == verify_info) { |
858 | 0 | snmp_log(LOG_ERR, "TLSTCP: failed to allocate verify_info\n"); |
859 | 0 | goto free_ssl; |
860 | 0 | } |
861 | 0 | SSL_set_ex_data(ssl, tls_get_verify_info_index(), verify_info); |
862 | |
|
863 | 0 | SSL_set_accept_state(ssl); |
864 | 0 | tlsdata->handshake_state = NETSNMP_TLS_HANDSHAKE_IN_PROGRESS; |
865 | |
|
866 | 0 | DEBUGMSGTL(("tlstcp", "accept succeeded on sock %" NETSNMP_FMT_SKT "\n", |
867 | 0 | newsock)); |
868 | |
|
869 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONACCEPTS); |
870 | |
|
871 | 0 | if (tlsdata->ssl) |
872 | 0 | SSL_free(tlsdata->ssl); |
873 | 0 | tlsdata->ssl = ssl; |
874 | |
|
875 | 0 | return newsock; |
876 | | |
877 | 0 | free_ssl: |
878 | 0 | SSL_free(ssl); |
879 | |
|
880 | 0 | close_newsock: |
881 | | #ifdef HAVE_CLOSESOCKET |
882 | | closesocket(newsock); |
883 | | #else |
884 | 0 | close(newsock); |
885 | 0 | #endif |
886 | 0 | return NETSNMP_INVALID_SOCKET; |
887 | 0 | } |
888 | | |
889 | | static netsnmp_transport * |
890 | | netsnmp_tlstcp_open_client(netsnmp_transport *t) |
891 | 0 | { |
892 | 0 | _netsnmpTLSBaseData *tlsdata = t->data; |
893 | 0 | _netsnmp_verify_info *verify_info; |
894 | 0 | BIO *read_bio, *write_bio; |
895 | 0 | SSL_CTX *ctx; |
896 | 0 | SSL *ssl; |
897 | |
|
898 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENS); |
899 | |
|
900 | 0 | if (!t->base_transport) { |
901 | 0 | snmp_log(LOG_ERR, "TLSTCP: missing base transport\n"); |
902 | 0 | return NULL; |
903 | 0 | } |
904 | | |
905 | | /* set up the needed SSL context */ |
906 | 0 | tlsdata->ssl_context = ctx = sslctx_client_setup(TLS_method(), tlsdata); |
907 | 0 | if (!ctx) { |
908 | 0 | snmp_log(LOG_ERR, "failed to create TLS context\n"); |
909 | 0 | goto err_free_tlsdata; |
910 | 0 | } |
911 | | |
912 | 0 | DEBUGMSGTL(("tlstcp", "connecting to tlstcp %s\n", |
913 | 0 | tlsdata->addr_string)); |
914 | 0 | SNMP_FREE(t->remote); |
915 | 0 | t->remote = strdup(tlsdata->addr_string); |
916 | 0 | t->remote_length = strlen(tlsdata->addr_string) + 1; |
917 | | |
918 | | /* Create the SSL layer */ |
919 | 0 | ssl = tlsdata->ssl = SSL_new(ctx); |
920 | 0 | if (NULL == ssl) { |
921 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENERRORS); |
922 | 0 | snmp_log(LOG_ERR, "tlstcp: failed to create a SSL connection\n"); |
923 | 0 | goto err_free_tlsdata; |
924 | 0 | } |
925 | | |
926 | 0 | read_bio = BIO_new(BIO_s_mem()); |
927 | 0 | write_bio = BIO_new(BIO_s_mem()); |
928 | 0 | if (!read_bio || !write_bio) { |
929 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENERRORS); |
930 | 0 | snmp_log(LOG_ERR, "tlstcp: failed to create memory BIOs\n"); |
931 | 0 | BIO_free(read_bio); |
932 | 0 | BIO_free(write_bio); |
933 | 0 | goto err_free_ssl; |
934 | 0 | } |
935 | | |
936 | | /* Bind the SSL layer to the BIOs */ |
937 | 0 | SSL_set_bio(ssl, read_bio, write_bio); |
938 | 0 | SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); |
939 | |
|
940 | 0 | verify_info = SNMP_MALLOC_TYPEDEF(_netsnmp_verify_info); |
941 | 0 | if (NULL == verify_info) { |
942 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENERRORS); |
943 | 0 | snmp_log(LOG_ERR, "tlstcp: failed to allocate verify_info\n"); |
944 | 0 | goto err_free_ssl; |
945 | 0 | } |
946 | | |
947 | 0 | SSL_set_ex_data(ssl, tls_get_verify_info_index(), verify_info); |
948 | |
|
949 | 0 | t->sock = t->base_transport->sock; |
950 | 0 | if (NETSNMP_IS_VALID_SOCKET(t->sock)) { |
951 | 0 | if (netsnmp_set_non_blocking_mode(t->sock, TRUE) < 0) { |
952 | 0 | DEBUGMSGTL(("tlstcp", |
953 | 0 | "couldn't set non-blocking mode on client fd %" |
954 | 0 | NETSNMP_FMT_SKT "\n", t->sock)); |
955 | 0 | } |
956 | 0 | } |
957 | |
|
958 | 0 | SSL_set_connect_state(ssl); |
959 | 0 | tlsdata->handshake_state = NETSNMP_TLS_HANDSHAKE_IN_PROGRESS; |
960 | | |
961 | | /* Perform initial non-blocking handshake step (sends ClientHello) */ |
962 | 0 | if (_tlstcp_step_handshake(t) < 0) { |
963 | 0 | snmp_increment_statistic(STAT_TLSTM_SNMPTLSTMSESSIONOPENERRORS); |
964 | 0 | snmp_log(LOG_ERR, "tlstcp: failed initial handshake step\n"); |
965 | 0 | goto err_free_ssl; |
966 | 0 | } |
967 | | |
968 | 0 | return t; |
969 | | |
970 | 0 | err_free_ssl: |
971 | 0 | err_free_tlsdata: |
972 | 0 | netsnmp_tlstcp_close(t); |
973 | 0 | return NULL; |
974 | 0 | } |
975 | | |
976 | | static netsnmp_transport * |
977 | | netsnmp_tlstcp_open_server(netsnmp_transport *t) |
978 | 0 | { |
979 | 0 | _netsnmpTLSBaseData *tlsdata = t->data; |
980 | |
|
981 | 0 | if (!t->base_transport) { |
982 | 0 | snmp_log(LOG_ERR, "TLSTCP: missing base transport\n"); |
983 | 0 | return NULL; |
984 | 0 | } |
985 | | |
986 | 0 | #ifndef NETSNMP_NO_LISTEN_SUPPORT |
987 | 0 | DEBUGMSGTL(("tlstcp", "listening on tlstcp port %s\n", |
988 | 0 | tlsdata->addr_string)); |
989 | 0 | SNMP_FREE(t->local); |
990 | 0 | t->local = strdup(tlsdata->addr_string); |
991 | 0 | t->local_length = strlen(tlsdata->addr_string) + 1; |
992 | | |
993 | | /* create the OpenSSL TLS context */ |
994 | 0 | tlsdata->ssl_context = sslctx_server_setup(TLS_method()); |
995 | 0 | if (NULL == tlsdata->ssl_context) { |
996 | 0 | netsnmp_tlstcp_close(t); |
997 | 0 | return NULL; |
998 | 0 | } |
999 | | |
1000 | 0 | t->sock = t->base_transport->sock; |
1001 | 0 | if (NETSNMP_IS_VALID_SOCKET(t->sock)) { |
1002 | 0 | if (netsnmp_set_non_blocking_mode(t->sock, TRUE) < 0) { |
1003 | 0 | DEBUGMSGTL(("tlstcp", |
1004 | 0 | "couldn't set non-blocking mode on server fd %" NETSNMP_FMT_SKT "\n", |
1005 | 0 | t->sock)); |
1006 | 0 | } |
1007 | 0 | } |
1008 | 0 | t->flags |= NETSNMP_TRANSPORT_FLAG_LISTEN; |
1009 | | #else /* NETSNMP_NO_LISTEN_SUPPORT */ |
1010 | | netsnmp_tlstcp_close(t); |
1011 | | return NULL; |
1012 | | #endif /* NETSNMP_NO_LISTEN_SUPPORT */ |
1013 | |
|
1014 | 0 | return t; |
1015 | 0 | } |
1016 | | |
1017 | | netsnmp_transport * |
1018 | | netsnmp_tlstcp_open(netsnmp_transport *t) |
1019 | 0 | { |
1020 | 0 | _netsnmpTLSBaseData *tlsdata; |
1021 | |
|
1022 | 0 | netsnmp_assert_or_return(t != NULL, NULL); |
1023 | 0 | netsnmp_assert_or_return(t->data != NULL, NULL); |
1024 | 0 | netsnmp_assert_or_return(sizeof(_netsnmpTLSBaseData) == t->data_length, |
1025 | 0 | NULL); |
1026 | | |
1027 | 0 | tlsdata = t->data; |
1028 | |
|
1029 | 0 | if (tlsdata->flags & NETSNMP_TLSBASE_IS_CLIENT) |
1030 | 0 | return netsnmp_tlstcp_open_client(t); |
1031 | 0 | else |
1032 | 0 | return netsnmp_tlstcp_open_server(t); |
1033 | 0 | } |
1034 | | |
1035 | | static void |
1036 | | _tlstcp_format_addr_string(_netsnmpTLSBaseData *tlsdata) |
1037 | 0 | { |
1038 | 0 | char buf[128]; |
1039 | 0 | if (!tlsdata || !tlsdata->addr) |
1040 | 0 | return; |
1041 | | |
1042 | 0 | if (tlsdata->addr_string) { |
1043 | 0 | free(tlsdata->addr_string); |
1044 | 0 | tlsdata->addr_string = NULL; |
1045 | 0 | } |
1046 | |
|
1047 | 0 | if (tlsdata->addr->remote_addr.sa.sa_family == AF_INET) { |
1048 | 0 | char a[16]; |
1049 | 0 | snprintf(buf, sizeof(buf), "%s:%hu", |
1050 | 0 | inet_ntop(AF_INET, &tlsdata->addr->remote_addr.sin.sin_addr, |
1051 | 0 | a, sizeof(a)), |
1052 | 0 | ntohs(tlsdata->addr->remote_addr.sin.sin_port)); |
1053 | 0 | tlsdata->addr_string = strdup(buf); |
1054 | 0 | } |
1055 | 0 | #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN |
1056 | 0 | else if (tlsdata->addr->remote_addr.sa.sa_family == AF_INET6) { |
1057 | 0 | char a[64]; |
1058 | 0 | snprintf(buf, sizeof(buf), "[%s]:%hu", |
1059 | 0 | inet_ntop(AF_INET6, &tlsdata->addr->remote_addr.sin6.sin6_addr, |
1060 | 0 | a, sizeof(a)), |
1061 | 0 | ntohs(tlsdata->addr->remote_addr.sin6.sin6_port)); |
1062 | 0 | tlsdata->addr_string = strdup(buf); |
1063 | 0 | } |
1064 | 0 | #endif |
1065 | 0 | } |
1066 | | |
1067 | | /* |
1068 | | * Create a TLS-based transport for SNMP. Local is TRUE if addr is the local |
1069 | | * address to bind to (i.e. this is a server-type session); otherwise addr is |
1070 | | * the remote address to send things to. |
1071 | | */ |
1072 | | |
1073 | | static netsnmp_transport * |
1074 | | _tlstcp_transport_common(netsnmp_transport *t, int local) |
1075 | 0 | { |
1076 | 0 | char *tmp = NULL; |
1077 | 0 | int tmp_len = 0; |
1078 | |
|
1079 | 0 | if (NULL == t) |
1080 | 0 | return NULL; |
1081 | | |
1082 | | /* Save base transport for clients; need in send/recv functions later */ |
1083 | 0 | if (t->data) { |
1084 | 0 | tmp = t->data; |
1085 | 0 | tmp_len = t->data_length; |
1086 | 0 | t->data = NULL; |
1087 | 0 | } |
1088 | 0 | t->base_transport = netsnmp_transport_copy(t); |
1089 | |
|
1090 | 0 | if (tmp) { |
1091 | 0 | t->data = tmp; |
1092 | 0 | t->data_length = tmp_len; |
1093 | 0 | } |
1094 | |
|
1095 | 0 | if (NULL == t->base_transport) { |
1096 | 0 | netsnmp_socketbase_close(t); |
1097 | 0 | netsnmp_transport_free(t); |
1098 | 0 | return NULL; |
1099 | 0 | } |
1100 | | |
1101 | 0 | if (NULL != t->data && |
1102 | 0 | t->data_length == sizeof(netsnmp_indexed_addr_pair)) { |
1103 | 0 | _netsnmpTLSBaseData *tlsdata = |
1104 | 0 | netsnmp_tlsbase_allocate_tlsdata(t, local); |
1105 | 0 | if (NULL == tlsdata) { |
1106 | 0 | netsnmp_socketbase_close(t); |
1107 | 0 | netsnmp_transport_free(t); |
1108 | 0 | return NULL; |
1109 | 0 | } |
1110 | 0 | tlsdata->addr = (netsnmp_indexed_addr_pair *) t->data; |
1111 | 0 | tlsdata->addr_len = t->data_length; |
1112 | 0 | t->data = tlsdata; |
1113 | 0 | t->data_length = sizeof(_netsnmpTLSBaseData); |
1114 | | |
1115 | | /* Format addr_string from tlsdata->addr */ |
1116 | 0 | _tlstcp_format_addr_string(tlsdata); |
1117 | 0 | } |
1118 | | |
1119 | 0 | t->domain = netsnmpTLSTCPDomain; |
1120 | 0 | t->domain_length = netsnmpTLSTCPDomain_len; |
1121 | |
|
1122 | 0 | t->f_recv = netsnmp_tlstcp_recv; |
1123 | 0 | t->f_send = netsnmp_tlstcp_send; |
1124 | 0 | t->f_open = netsnmp_tlstcp_open; |
1125 | 0 | t->f_close = netsnmp_tlstcp_close; |
1126 | 0 | t->f_accept = netsnmp_tlstcp_accept; |
1127 | 0 | t->f_copy = netsnmp_tlstcp_copy; |
1128 | 0 | t->f_pending = netsnmp_tlstcp_pending; |
1129 | 0 | t->f_config = netsnmp_tlsbase_config; |
1130 | 0 | t->f_setup_session = netsnmp_tlsbase_session_init; |
1131 | 0 | t->f_fmtaddr = netsnmp_tlstcp_fmtaddr; |
1132 | 0 | t->f_get_taddr = netsnmp_tlstcp_get_taddr; |
1133 | |
|
1134 | 0 | t->flags |= NETSNMP_TRANSPORT_FLAG_TUNNELED | NETSNMP_TRANSPORT_FLAG_STREAM; |
1135 | |
|
1136 | 0 | return t; |
1137 | 0 | } |
1138 | | |
1139 | | static netsnmp_transport * |
1140 | | netsnmp_tlstcp_transport(const struct netsnmp_ep *ep, int local) |
1141 | 0 | { |
1142 | 0 | netsnmp_transport *t = NULL; |
1143 | |
|
1144 | 0 | #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN |
1145 | 0 | if (ep->a.sin.sin_family == AF_INET6) { |
1146 | 0 | t = netsnmp_tcp6_transport(ep, local); |
1147 | 0 | } else |
1148 | 0 | #endif |
1149 | 0 | { |
1150 | 0 | t = netsnmp_tcp_transport(ep, local); |
1151 | 0 | } |
1152 | |
|
1153 | 0 | if (!t) |
1154 | 0 | return NULL; |
1155 | | |
1156 | 0 | t = _tlstcp_transport_common(t, local); |
1157 | 0 | if (!t) |
1158 | 0 | return NULL; |
1159 | | |
1160 | 0 | return t; |
1161 | 0 | } |
1162 | | |
1163 | | netsnmp_transport * |
1164 | | netsnmp_tlstcp_create_tstring(const char *str, int local, |
1165 | | const char *default_target) |
1166 | 0 | { |
1167 | 0 | _netsnmpTLSBaseData *tlsdata; |
1168 | 0 | netsnmp_transport *t = NULL; |
1169 | 0 | struct netsnmp_ep ep; |
1170 | 0 | const char *cp; |
1171 | 0 | char buf[SPRINT_MAX_LEN]; |
1172 | |
|
1173 | 0 | if (netsnmp_sockaddr_in3(&ep, str, default_target)) { |
1174 | 0 | t = netsnmp_tlstcp_transport(&ep, local); |
1175 | 0 | } |
1176 | 0 | #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN |
1177 | 0 | else if (netsnmp_sockaddr_in6_3(&ep, str, default_target)) { |
1178 | 0 | t = netsnmp_tlstcp_transport(&ep, local); |
1179 | 0 | } |
1180 | 0 | #endif |
1181 | |
|
1182 | 0 | if (!t) |
1183 | 0 | return NULL; |
1184 | | |
1185 | | /* see if we can extract the remote hostname */ |
1186 | 0 | if (!local && t->data && str) { |
1187 | 0 | tlsdata = (_netsnmpTLSBaseData *)t->data; |
1188 | | /* search for a : */ |
1189 | 0 | if (NULL != (cp = strrchr(str, ':'))) { |
1190 | 0 | snprintf(buf, sizeof(buf), "%.*s", (int) SNMP_MIN(cp - str, sizeof(buf) - 1), |
1191 | 0 | str); |
1192 | 0 | } else { |
1193 | | /* else the entire spec is a host name only */ |
1194 | 0 | strlcpy(buf, str, sizeof(buf)); |
1195 | 0 | } |
1196 | 0 | tlsdata->their_hostname = strdup(buf); |
1197 | 0 | } |
1198 | |
|
1199 | 0 | return t; |
1200 | 0 | } |
1201 | | |
1202 | | |
1203 | | netsnmp_transport * |
1204 | | netsnmp_tlstcp_create_ostring(const void *o, size_t o_len, int local) |
1205 | 0 | { |
1206 | 0 | struct netsnmp_ep ep; |
1207 | |
|
1208 | 0 | memset(&ep, 0, sizeof(ep)); |
1209 | 0 | if (netsnmp_ipv4_ostring_to_sockaddr(&ep.a.sin, o, o_len)) |
1210 | 0 | return netsnmp_tlstcp_transport(&ep, local); |
1211 | 0 | #ifdef NETSNMP_TRANSPORT_TCPIPV6_DOMAIN |
1212 | 0 | else if (netsnmp_ipv6_ostring_to_sockaddr(&ep.a.sin6, o, o_len)) |
1213 | 0 | return netsnmp_tlstcp_transport(&ep, local); |
1214 | 0 | #endif |
1215 | 0 | else |
1216 | 0 | return NULL; |
1217 | 0 | } |
1218 | | |
1219 | | void |
1220 | | netsnmp_tlstcp_ctor(void) |
1221 | 4.64k | { |
1222 | 4.64k | DEBUGMSGTL(("tlstcp", "registering TLS constructor\n")); |
1223 | | |
1224 | | /* config settings */ |
1225 | | |
1226 | 4.64k | tlstcpDomain.name = netsnmpTLSTCPDomain; |
1227 | 4.64k | tlstcpDomain.name_length = netsnmpTLSTCPDomain_len; |
1228 | 4.64k | tlstcpDomain.prefix = calloc(3, sizeof(char *)); |
1229 | 4.64k | if (!tlstcpDomain.prefix) { |
1230 | 0 | snmp_log(LOG_ERR, "calloc() failed - out of memory\n"); |
1231 | 0 | return; |
1232 | 0 | } |
1233 | 4.64k | tlstcpDomain.prefix[0] = "tlstcp"; |
1234 | 4.64k | tlstcpDomain.prefix[1] = "tls"; |
1235 | | |
1236 | 4.64k | tlstcpDomain.f_create_from_tstring_new = netsnmp_tlstcp_create_tstring; |
1237 | 4.64k | tlstcpDomain.f_create_from_ostring = netsnmp_tlstcp_create_ostring; |
1238 | | |
1239 | 4.64k | netsnmp_tdomain_register(&tlstcpDomain); |
1240 | 4.64k | } |