aboutsummaryrefslogtreecommitdiff
path: root/vmime-master/src/vmime/net/tls/openssl/TLSSocket_OpenSSL.cpp
blob: 978f0ca6c432369d4f810d808d72f5b3a5b36027 (plain)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002 Vincent Richard <vincent@vmime.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 3 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "vmime/config.hpp"


#if VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_TLS_SUPPORT && VMIME_TLS_SUPPORT_LIB_IS_OPENSSL


#include <openssl/ssl.h>
#include <openssl/err.h>

#include "vmime/net/tls/openssl/TLSSocket_OpenSSL.hpp"
#include "vmime/net/tls/openssl/TLSSession_OpenSSL.hpp"
#include "vmime/net/tls/openssl/OpenSSLInitializer.hpp"

#include "vmime/platform.hpp"

#include "vmime/security/cert/openssl/X509Certificate_OpenSSL.hpp"

#include "vmime/utility/stringUtils.hpp"

#include <vector>
#include <cstring>


namespace vmime {
namespace net {
namespace tls {


static OpenSSLInitializer::autoInitializer openSSLInitializer;


#if OPENSSL_VERSION_NUMBER < 0x10100000L

// static
BIO_METHOD TLSSocket_OpenSSL::sm_customBIOMethod = {
	100 | BIO_TYPE_SOURCE_SINK,
	"vmime::socket glue",
	TLSSocket_OpenSSL::bio_write,
	TLSSocket_OpenSSL::bio_read,
	TLSSocket_OpenSSL::bio_puts,
	NULL,  // gets
	TLSSocket_OpenSSL::bio_ctrl,
	TLSSocket_OpenSSL::bio_create,
	TLSSocket_OpenSSL::bio_destroy,
	0
};

#define BIO_set_init(b, val) b->init = val
#define BIO_set_data(b, val) b->ptr = val
#define BIO_set_num(b, val) b->num = val
#define BIO_set_flags(b, val) b->flags = val
#define BIO_set_shutdown(b, val) b->shutdown = val
#define BIO_get_init(b) b->init
#define BIO_get_data(b) b->ptr
#define BIO_get_shutdown(b) b->shutdown

#else

#define BIO_set_num(b, val)

#endif



// static
shared_ptr <TLSSocket> TLSSocket::wrap(
	const shared_ptr <TLSSession>& session,
	const shared_ptr <socket>& sok
) {

	return make_shared <TLSSocket_OpenSSL>(dynamicCast <TLSSession_OpenSSL>(session), sok);
}


TLSSocket_OpenSSL::TLSSocket_OpenSSL(
	const shared_ptr <TLSSession_OpenSSL>& session,
	const shared_ptr <socket>& sok
)
	: m_session(session),
	  m_wrapped(sok),
	  m_connected(false),
	  m_ssl(0),
	  m_status(0),
	  m_ex() {

}


TLSSocket_OpenSSL::~TLSSocket_OpenSSL() {

	try {
		disconnect();
	} catch (...) {
		// Don't throw in destructor
	}
}


void TLSSocket_OpenSSL::createSSLHandle() {

	if (m_wrapped->isConnected()) {
		string peerName = getPeerName();
		
		if (peerName.empty()) {
			throw exceptions::tls_exception("Unknown host name, will not be able to set SNI");
		}

#if OPENSSL_VERSION_NUMBER < 0x10100000L

		BIO* sockBio = BIO_new(&sm_customBIOMethod);
		sockBio->ptr = this;
		sockBio->init = 1;

#else

		BIO_METHOD* bioMeth = BIO_meth_new(BIO_TYPE_SOURCE_SINK | BIO_get_new_index(), "vmime::socket glue");

		if (!bioMeth) {
			BIO_meth_free(bioMeth);
			throw exceptions::tls_exception("BIO_meth_new() failed");
		}

		BIO_meth_set_write(bioMeth, TLSSocket_OpenSSL::bio_write);
		BIO_meth_set_read(bioMeth, TLSSocket_OpenSSL::bio_read);
		BIO_meth_set_puts(bioMeth, TLSSocket_OpenSSL::bio_puts);
		BIO_meth_set_ctrl(bioMeth, TLSSocket_OpenSSL::bio_ctrl);
		BIO_meth_set_create(bioMeth, TLSSocket_OpenSSL::bio_create);
		BIO_meth_set_destroy(bioMeth, TLSSocket_OpenSSL::bio_destroy);

		BIO* sockBio = BIO_new(bioMeth);
		BIO_set_data(sockBio, this);
		BIO_set_init(sockBio, 1);

#endif

		if (!sockBio) {
			throw exceptions::tls_exception("BIO_new() failed");
		}

		m_ssl = SSL_new(m_session->getContext());

		if (!m_ssl) {
			BIO_free(sockBio);
			throw exceptions::tls_exception("Cannot create SSL object");
		}

		SSL_set_bio(m_ssl, sockBio, sockBio);
		SSL_set_tlsext_host_name(m_ssl, peerName.c_str());
		SSL_set_connect_state(m_ssl);
		SSL_set_mode(m_ssl, SSL_MODE_AUTO_RETRY | SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);

	} else {

		throw exceptions::tls_exception("Unconnected socket error");
	}
}


void TLSSocket_OpenSSL::connect(const string& address, const port_t port) {

	try {

		m_wrapped->connect(address, port);
		
		createSSLHandle();

		handshake();

	} catch (...) {

		disconnect();
		throw;
	}
}


void TLSSocket_OpenSSL::disconnect() {

	if (m_ssl) {

		// Don't shut down the socket more than once.
		int shutdownState = SSL_get_shutdown(m_ssl);
		bool shutdownSent = (shutdownState & SSL_SENT_SHUTDOWN) == SSL_SENT_SHUTDOWN;

		if (!shutdownSent) {
			SSL_shutdown(m_ssl);
		}

		SSL_free(m_ssl);
		m_ssl = 0;
	}

	if (m_connected) {
		m_connected = false;
		m_wrapped->disconnect();
	}
}


bool TLSSocket_OpenSSL::isConnected() const {

	return m_wrapped->isConnected() && m_connected;
}


size_t TLSSocket_OpenSSL::getBlockSize() const {

	return 16384;  // 16 KB
}


const string TLSSocket_OpenSSL::getPeerName() const {

	return m_wrapped->getPeerName();
}


const string TLSSocket_OpenSSL::getPeerAddress() const {

	return m_wrapped->getPeerAddress();
}


shared_ptr <timeoutHandler> TLSSocket_OpenSSL::getTimeoutHandler() {

	return m_wrapped->getTimeoutHandler();
}


void TLSSocket_OpenSSL::setTracer(const shared_ptr <net::tracer>& tracer) {

	m_wrapped->setTracer(tracer);
}


shared_ptr <net::tracer> TLSSocket_OpenSSL::getTracer() {

	return m_wrapped->getTracer();
}


bool TLSSocket_OpenSSL::waitForRead(const int msecs) {

	return m_wrapped->waitForRead(msecs);
}


bool TLSSocket_OpenSSL::waitForWrite(const int msecs) {

	return m_wrapped->waitForWrite(msecs);
}


void TLSSocket_OpenSSL::receive(string& buffer) {

	const size_t size = receiveRaw(m_buffer, sizeof(m_buffer));

	if (size != 0) {
		buffer = utility::stringUtils::makeStringFromBytes(m_buffer, size);
	} else {
		buffer.clear();
	}
}


void TLSSocket_OpenSSL::send(const string& buffer) {

	sendRaw(reinterpret_cast <const byte_t*>(buffer.data()), buffer.length());
}


void TLSSocket_OpenSSL::send(const char* str) {

	sendRaw(reinterpret_cast <const byte_t*>(str), ::strlen(str));
}


size_t TLSSocket_OpenSSL::receiveRaw(byte_t* buffer, const size_t count) {

	if (!m_ssl) {
		throw exceptions::socket_not_connected_exception();
	}

	m_status &= ~(STATUS_WANT_WRITE | STATUS_WANT_READ);

	ERR_clear_error();
	int rc = SSL_read(m_ssl, buffer, static_cast <int>(count));

	if (m_ex.get()) {
		internalThrow();
	}

	if (rc <= 0) {

		int error = SSL_get_error(m_ssl, rc);

		if (error == SSL_ERROR_WANT_WRITE) {
			m_status |= STATUS_WANT_WRITE;
			return 0;
		} else if (error == SSL_ERROR_WANT_READ) {
			m_status |= STATUS_WANT_READ;
			return 0;
		}

		handleError(rc);
	}

	return rc;
}


void TLSSocket_OpenSSL::sendRaw(const byte_t* buffer, const size_t count) {

	if (!m_ssl) {
		throw exceptions::socket_not_connected_exception();
	}

	m_status &= ~(STATUS_WANT_WRITE | STATUS_WANT_READ);

	for (size_t size = count ; size > 0 ; ) {

		ERR_clear_error();
		int rc = SSL_write(m_ssl, buffer, static_cast <int>(size));

		if (rc <= 0) {

			int error = SSL_get_error(m_ssl, rc);

			if (error == SSL_ERROR_WANT_READ) {
				m_wrapped->waitForRead();
				continue;
			} else if (error == SSL_ERROR_WANT_WRITE) {
				m_wrapped->waitForWrite();
				continue;
			}

			handleError(rc);

		} else {

			buffer += rc;
			size -= rc;
		}
	}
}


size_t TLSSocket_OpenSSL::sendRawNonBlocking(const byte_t* buffer, const size_t count) {

	if (!m_ssl) {
		throw exceptions::socket_not_connected_exception();
	}

	m_status &= ~(STATUS_WANT_WRITE | STATUS_WANT_READ);

	ERR_clear_error();
	int rc = SSL_write(m_ssl, buffer, static_cast <int>(count));

	if (m_ex.get()) {
		internalThrow();
	}

	if (rc <= 0) {

		int error = SSL_get_error(m_ssl, rc);

		if (error == SSL_ERROR_WANT_WRITE) {
			m_status |= STATUS_WANT_WRITE;
			return 0;
		} else if (error == SSL_ERROR_WANT_READ) {
			m_status |= STATUS_WANT_READ;
			return 0;
		}

		handleError(rc);
	}

	return rc;
}


void TLSSocket_OpenSSL::handshake() {

	shared_ptr <timeoutHandler> toHandler = m_wrapped->getTimeoutHandler();

	if (toHandler) {
		toHandler->resetTimeOut();
	}

	if (getTracer()) {
		getTracer()->traceSend("Beginning SSL/TLS handshake");
	}

	// Start handshaking process
	if (!m_ssl) {
		createSSLHandle();
	}

	try {

		int rc;

		ERR_clear_error();

		while ((rc = SSL_do_handshake(m_ssl)) <= 0) {

			const int err = SSL_get_error(m_ssl, rc);

			if (err == SSL_ERROR_WANT_READ) {
				m_wrapped->waitForRead();
			} else if (err == SSL_ERROR_WANT_WRITE) {
				m_wrapped->waitForWrite();
			} else {
				handleError(rc);
			}

			// Check whether the time-out delay is elapsed
			if (toHandler && toHandler->isTimeOut()) {

				if (!toHandler->handleTimeOut()) {
					throw exceptions::operation_timed_out();
				}

				toHandler->resetTimeOut();
			}

			ERR_clear_error();
		}

	} catch (...) {

		throw;
	}

	// Verify server's certificate(s)
	shared_ptr <security::cert::certificateChain> certs = getPeerCertificates();

	if (!certs) {
		throw exceptions::tls_exception("No peer certificate.");
	}

	m_session->getCertificateVerifier()->verify(certs, getPeerName());

	m_connected = true;
}


shared_ptr <security::cert::certificateChain> TLSSocket_OpenSSL::getPeerCertificates() {

	if (getTracer()) {
		getTracer()->traceSend("Getting peer certificates");
	}

	STACK_OF(X509)* chain = SSL_get_peer_cert_chain(m_ssl);

	if (chain == NULL) {
		return null;
	}

	int certCount = sk_X509_num(chain);

	if (certCount == 0) {
		return null;
	}

	bool error = false;
	std::vector <shared_ptr <security::cert::certificate> > certs;

	for (int i = 0; i < certCount && !error; i++) {

		shared_ptr <vmime::security::cert::X509Certificate> cert =
			vmime::security::cert::X509Certificate_OpenSSL::importInternal(sk_X509_value(chain, i));

		if (cert) {
			certs.push_back(cert);
		} else {
			error = true;
		}
	}

	if (error) {
		return null;
	}

	return make_shared <security::cert::certificateChain>(certs);
}


void TLSSocket_OpenSSL::internalThrow() {

	if (m_ex.get()) {
		throw *m_ex;
	}
}


void TLSSocket_OpenSSL::handleError(int rc) {

	if (rc > 0) {
		return;
	}

	internalThrow();

	int  sslError   = SSL_get_error(m_ssl, rc);
	long lastError  = ERR_get_error();

	switch (sslError) {

		case SSL_ERROR_ZERO_RETURN:

			disconnect();
			return;

		case SSL_ERROR_SYSCALL: {

			if (lastError == 0) {

				if (rc == 0) {

					throw exceptions::tls_exception("SSL connection unexpectedly closed");

				} else {

					std::ostringstream oss;
					oss << "The BIO reported an error: " << rc;
					oss.flush();
					throw exceptions::tls_exception(oss.str());
				}
			}

			break;
		}

		case SSL_ERROR_WANT_READ:

			BIO_set_retry_read(SSL_get_rbio(m_ssl));
			break;

		case SSL_ERROR_WANT_WRITE:

			BIO_set_retry_write(SSL_get_wbio(m_ssl));
			break;

		// This happens only for BIOs of type BIO_s_connect() or BIO_s_accept()
		case SSL_ERROR_WANT_CONNECT:
		case SSL_ERROR_WANT_ACCEPT:
		// SSL_CTX_set_client_cert_cb related, not used
		case SSL_ERROR_WANT_X509_LOOKUP:
		case SSL_ERROR_SSL:
		default:

			if (lastError == 0) {

				throw exceptions::tls_exception("Unexpected SSL IO error");

			} else {

				char buffer[256];
				ERR_error_string_n(lastError, buffer, sizeof(buffer));
				vmime::string msg(buffer);
				throw exceptions::tls_exception(msg);
			}

			break;
	}
}


unsigned int TLSSocket_OpenSSL::getStatus() const {

	return m_status;
}


// Implementation of custom BIO methods


// static
int TLSSocket_OpenSSL::bio_write(BIO* bio, const char* buf, int len) {

	BIO_clear_retry_flags(bio);

	if (buf == NULL || len <= 0) {
		return -1;
	}

	TLSSocket_OpenSSL *sok = reinterpret_cast <TLSSocket_OpenSSL*>(BIO_get_data(bio));

	if (!BIO_get_init(bio) || !sok) {
		return -1;
	}

	try {

		const size_t n = sok->m_wrapped->sendRawNonBlocking(
			reinterpret_cast <const byte_t*>(buf), len
		);

		if (n == 0 && sok->m_wrapped->getStatus() & socket::STATUS_WOULDBLOCK) {
			BIO_set_retry_write(bio);
			return -1;
		}

		return static_cast <int>(n);

	} catch (exception& e) {

		// Workaround for passing C++ exceptions from C BIO functions
		sok->m_ex.reset(e.clone());
		return -1;
	}
}


// static
int TLSSocket_OpenSSL::bio_read(BIO* bio, char* buf, int len) {

	BIO_clear_retry_flags(bio);

	if (buf == NULL || len <= 0) {
		return -1;
	}

	TLSSocket_OpenSSL *sok = reinterpret_cast <TLSSocket_OpenSSL*>(BIO_get_data(bio));

	if (!BIO_get_init(bio) || !sok) {
		return -1;
	}

	try {

		const size_t n = sok->m_wrapped->receiveRaw(
			reinterpret_cast <byte_t*>(buf), len
		);

		if (n == 0 || sok->m_wrapped->getStatus() & socket::STATUS_WOULDBLOCK) {
			BIO_set_retry_read(bio);
			return -1;
		}

		return static_cast <int>(n);

	} catch (exception& e) {

		// Workaround for passing C++ exceptions from C BIO functions
		sok->m_ex.reset(e.clone());
		return -1;
	}
}


// static
int TLSSocket_OpenSSL::bio_puts(BIO* bio, const char* str) {

	return bio_write(bio, str, static_cast <int>(strlen(str)));
}


// static
long TLSSocket_OpenSSL::bio_ctrl(BIO* bio, int cmd, long num, void* /* ptr */) {

	long ret = 1;

	switch (cmd) {

		case BIO_CTRL_INFO:

			ret = 0;
			break;

		case BIO_CTRL_GET_CLOSE:

			ret = BIO_get_shutdown(bio);
			break;

		case BIO_CTRL_SET_CLOSE:

			BIO_set_shutdown(bio, static_cast <int>(num));
			break;

		case BIO_CTRL_PENDING:
		case BIO_CTRL_WPENDING:

			ret = 0;
			break;

		case BIO_CTRL_DUP:
		case BIO_CTRL_FLUSH:

			ret = 1;
			break;

		default:

			ret = 0;
			break;
	}

	return ret;
}


// static
int TLSSocket_OpenSSL::bio_create(BIO* bio) {

	BIO_set_init(bio, 0);
	BIO_set_num(bio, 0);
	BIO_set_data(bio, NULL);
	BIO_set_flags(bio, 0);

	return 1;
}


// static
int TLSSocket_OpenSSL::bio_destroy(BIO* bio) {

	if (!bio) {
		return 0;
	}

	if (BIO_get_shutdown(bio)) {
		BIO_set_data(bio, NULL);
		BIO_set_init(bio, 0);
		BIO_set_flags(bio, 0);
	}

	return 1;
}


} // tls
} // net
} // vmime


#endif // VMIME_HAVE_MESSAGING_FEATURES && VMIME_HAVE_TLS_SUPPORT && VMIME_TLS_SUPPORT_LIB_IS_OPENSSL