summaryrefslogtreecommitdiffstats
path: root/tdeio/kssl/kssl.cc
blob: 0f34a55daf09da217fd98602120ffcd4863d1e5c (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
/* This file is part of the KDE project
 *
 * Copyright (C) 2000-2003 George Staikos <staikos@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// this hack provided by Malte Starostik to avoid glibc/openssl bug
// on some systems
#ifdef KSSL_HAVE_SSL
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define crypt _openssl_crypt
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#undef crypt
#endif

#include "kssl.h"

#include <kdebug.h>
#include <kstandarddirs.h>
#include <ksock.h>
#include <ksockaddr.h>

#include <kopenssl.h>
#include <ksslx509v3.h>
#include <ksslpkcs12.h>
#include <ksslsession.h>
#include <tdelocale.h>
#include <ksocks.h>


class KSSLPrivate {
public:
	KSSLPrivate() {
		lastInitTLS = false;
		kossl = KOpenSSLProxy::self();
		session = 0L;
	}

	~KSSLPrivate() {
		delete session;
		session = 0L;
	}

	bool lastInitTLS;
	KSSLCertificate::KSSLValidation m_cert_vfy_res;
	TQString proxyPeer;

#ifdef KSSL_HAVE_SSL
	SSL *m_ssl;
	SSL_CTX *m_ctx;
	SSL_METHOD *m_meth;
#endif
	KSSLSession *session;
	KOSSL *kossl;
};


KSSL::KSSL(bool init) {
	d = new KSSLPrivate;
	m_bInit = false;
	m_bAutoReconfig = true;
	m_cfg = new KSSLSettings();
#ifdef KSSL_HAVE_SSL  
	d->m_ssl = 0L;
#endif  

	if (init)
		initialize();
}


KSSL::~KSSL() {
	close();
	delete m_cfg;
	delete d;
}


int KSSL::seedWithEGD() {
int rc = 0;
#ifdef KSSL_HAVE_SSL
	if (m_cfg->useEGD() && !m_cfg->getEGDPath().isEmpty()) {
		rc = d->kossl->RAND_egd(m_cfg->getEGDPath().latin1());
		if (rc < 0) 
			kdDebug(7029) << "KSSL: Error seeding PRNG with the EGD." << endl;
		else kdDebug(7029) << "KSSL: PRNG was seeded with " << rc 
				   << " bytes from the EGD." << endl;
	} else if (m_cfg->useEFile() && !m_cfg->getEGDPath().isEmpty()) {
		rc = d->kossl->RAND_load_file(m_cfg->getEGDPath().latin1(), -1);
		if (rc < 0) 
			kdDebug(7029) << "KSSL: Error seeding PRNG with the entropy file." << endl;
		else kdDebug(7029) << "KSSL: PRNG was seeded with " << rc 
				   << " bytes from the entropy file." << endl;
	}
#endif
return rc;
}


bool KSSL::TLSInit() {
#ifdef KSSL_HAVE_SSL
// kdDebug(7029) << "KSSL TLS initialize" << endl;
	if (m_bInit)
		return false;

	if (m_bAutoReconfig)
		m_cfg->load();

	if (!m_cfg->tlsv1())
		return false;

	seedWithEGD();
	d->m_meth = d->kossl->TLS_client_method();
	d->lastInitTLS = true;

	m_pi.reset();

	d->m_ctx = d->kossl->SSL_CTX_new(d->m_meth);
	if (d->m_ctx == 0L) {
		return false;
	}

	// set cipher list
	TQString clist = m_cfg->getCipherList();
	//kdDebug(7029) << "Cipher list: " << clist << endl;
	if (!clist.isEmpty())
		d->kossl->SSL_CTX_set_cipher_list(d->m_ctx, const_cast<char *>(clist.ascii()));

	m_bInit = true;
return true;
#else
return false;
#endif
}


bool KSSL::initialize() {
#ifdef KSSL_HAVE_SSL
	kdDebug(7029) << "KSSL initialize" << endl;
	if (m_bInit)
		return false;

	if (m_bAutoReconfig)
		m_cfg->load();

	seedWithEGD();
	// FIXME: we should be able to force SSL off entirely.
	d->lastInitTLS = false;

	m_pi.reset();

	if (m_cfg->tlsv1() || (m_cfg->sslv3() && m_cfg->sslv2())) {
		d->m_meth = d->kossl->TLS_client_method();
	}
	else if (m_cfg->sslv3()) {
		d->m_meth = d->kossl->SSLv3_client_method();
	}
	else if (m_cfg->sslv2()) {
		d->m_meth = d->kossl->SSLv2_client_method();
	}

/*
if (m_cfg->sslv2() && m_cfg->sslv3()) kdDebug(7029) << "Double method" << endl;
else if (m_cfg->sslv2()) kdDebug(7029) << "SSL2 method" << endl;
else if (m_cfg->sslv3()) kdDebug(7029) << "SSL3 method" << endl;
*/

	d->m_ctx = d->kossl->SSL_CTX_new(d->m_meth);
	if (d->m_ctx == 0L) {
		return false;
	}

	// set cipher list
	TQString clist = m_cfg->getCipherList();
	kdDebug(7029) << "Cipher list: " << clist << endl;
	if (!clist.isEmpty())
		d->kossl->SSL_CTX_set_cipher_list(d->m_ctx, const_cast<char *>(clist.ascii()));

	m_bInit = true;
return true;
#else
return false;
#endif
}


bool KSSL::takeSession(KSSLSession *session) {
#ifdef KSSL_HAVE_SSL
	if (!session) {
		delete d->session;
		d->session = 0L;
		return true;
	}

	// Take session reference
	d->session = new KSSLSession;
	d->session->_session = session->_session;
	session->_session = 0L;

	return true;
#else
	return false;
#endif
}


void KSSL::close() {
#ifdef KSSL_HAVE_SSL
//kdDebug(7029) << "KSSL close" << endl;
	if (!m_bInit)
		return;

	delete d->session;
	d->session = 0L;

	if (d->m_ssl) {
		d->kossl->SSL_shutdown(d->m_ssl);
		d->kossl->SSL_free(d->m_ssl);
		d->m_ssl = 0L;
	}

	d->kossl->SSL_CTX_free(d->m_ctx);
	if (m_cfg->useEFile() && !m_cfg->getEGDPath().isEmpty()) {
		d->kossl->RAND_write_file(m_cfg->getEGDPath().latin1());
	}

	m_bInit = false;
#endif
}


bool KSSL::reInitialize() {
	close();
return initialize();
}

// get the callback file - it's hidden away in here
//#include "ksslcallback.c"


bool KSSL::setVerificationLogic() {
#if 0
#ifdef KSSL_HAVE_SSL
  //  SSL_set_verify_result(d->m_ssl, X509_V_OK);
  //  SSL_CTX_set_verify(d->m_ctx, SSL_VERIFY_PEER, X509Callback);
#endif
#endif
return true;
}


int KSSL::accept(int sock) {
#ifdef KSSL_HAVE_SSL
// kdDebug(7029) << "KSSL accept" << endl;
int rc;
	if (!m_bInit)
		return -1;
	d->m_ssl = d->kossl->SSL_new(d->m_ctx);
	if (!d->m_ssl)
		return -1;

	if (d->session) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L
		if (static_cast<SSL_SESSION*>(d->session->_session)->sess_cert == 0)
		{
			kdDebug(7029) << "Can't reuse session, no certificate." << endl;
			delete d->session;
			d->session = 0;
		}
		else
#endif
		if (1 == d->kossl->SSL_set_session(d->m_ssl,
			static_cast<SSL_SESSION*>(d->session->_session))) {
			kdDebug(7029) << "Session ID is being reused." << endl;
		} else {
			kdDebug(7029) << "Error attempting to reuse session." << endl;
			delete d->session;
			d->session = 0;
		}
	}

/*
	if (!setVerificationLogic()) {
		d->kossl->SSL_shutdown(d->m_ssl);
		d->kossl->SSL_free(d->m_ssl);
		d->m_ssl = 0;
		return -1;
	}
*/

	int off = SSL_OP_ALL;
	if (!d->lastInitTLS && !m_cfg->tlsv1())
		off |= SSL_OP_NO_TLSv1;
	if (!m_cfg->sslv3())
		off |= SSL_OP_NO_SSLv3;
	if (!m_cfg->sslv2())
		off |= SSL_OP_NO_SSLv2;

	d->kossl->_SSL_set_options(d->m_ssl, off);

	rc = d->kossl->SSL_set_fd(d->m_ssl, sock);
	if (rc == 0) {
		d->kossl->SSL_shutdown(d->m_ssl);
		d->kossl->SSL_free(d->m_ssl);
		d->m_ssl = 0;
		return rc;
	}
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
	d->kossl->SSL_set_tlsext_host_name(d->m_ssl, d->proxyPeer.ascii());
#endif

	rc = d->kossl->SSL_accept(d->m_ssl);
	if (rc == 1) {
		setConnectionInfo();
		setPeerInfo();
		kdDebug(7029) << "KSSL connected OK" << endl;
	} else {
		kdDebug(7029) << "KSSL accept failed - rc = " << rc << endl;
		kdDebug(7029) << "                      ERROR = "
			      << d->kossl->SSL_get_error(d->m_ssl, rc) << endl;
		d->kossl->SSL_shutdown(d->m_ssl);
		d->kossl->SSL_free(d->m_ssl);
		d->m_ssl = 0;
		return -1;
	}

	if (!d->kossl->_SSL_session_reused(d->m_ssl)) {
		if (d->session) {
			kdDebug(7029) << "Session reuse failed.  New session used instead." << endl;
			delete d->session;
			d->session = 0L;
		}
	}

	if (!d->session) {
		SSL_SESSION *sess = d->kossl->SSL_get1_session(d->m_ssl);
		if (sess) {
			d->session = new KSSLSession;
			d->session->_session = sess;
		}
	}

return rc;
#else
return -1;
#endif
}


int KSSL::connect(int sock) {
#ifdef KSSL_HAVE_SSL
// kdDebug(7029) << "KSSL connect" << endl;
int rc;
	if (!m_bInit)
		return -1;
	d->m_ssl = d->kossl->SSL_new(d->m_ctx);
	if (!d->m_ssl)
		return -1;

	if (d->session) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L
		if (static_cast<SSL_SESSION*>(d->session->_session)->sess_cert == 0)
		{
			kdDebug(7029) << "Can't reuse session, no certificate." << endl;
			delete d->session;
			d->session = 0;
		}
		else
#endif
		if (1 == d->kossl->SSL_set_session(d->m_ssl,
			static_cast<SSL_SESSION*>(d->session->_session))) {
			kdDebug(7029) << "Session ID is being reused." << endl;
		} else {
			kdDebug(7029) << "Error attempting to reuse session." << endl;
			delete d->session;
			d->session = 0;
		}
	}

/*
	if (!setVerificationLogic()) {
		d->kossl->SSL_shutdown(d->m_ssl);
		d->kossl->SSL_free(d->m_ssl);
		d->m_ssl = 0;
		return -1;
	}
*/

	int off = SSL_OP_ALL;
	if (!d->lastInitTLS && !m_cfg->tlsv1())
		off |= SSL_OP_NO_TLSv1;
	if (!m_cfg->sslv3())
		off |= SSL_OP_NO_SSLv3;
	if (!m_cfg->sslv2())
		off |= SSL_OP_NO_SSLv2;

	d->kossl->_SSL_set_options(d->m_ssl, off);

	rc = d->kossl->SSL_set_fd(d->m_ssl, sock);
	if (rc == 0) {
		d->kossl->SSL_shutdown(d->m_ssl);
		d->kossl->SSL_free(d->m_ssl);
		d->m_ssl = 0;
		return rc;
	}
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
	d->kossl->SSL_set_tlsext_host_name(d->m_ssl, d->proxyPeer.ascii());
#endif

connect_again:
	rc = d->kossl->SSL_connect(d->m_ssl);
	if (rc == 1) {
		setConnectionInfo();
		setPeerInfo();
		kdDebug(7029) << "KSSL connected OK" << endl;
	} else {
		int err = d->kossl->SSL_get_error(d->m_ssl, rc);
		if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
			// nonblocking - but we block anyways in connect() :)
			goto connect_again;
		} else {
			kdDebug(7029) << "KSSL connect failed - rc = "
				<< rc << endl;
			kdDebug(7029) << "                   ERROR = "
				<< err << endl;
			d->kossl->ERR_print_errors_fp(stderr);
			d->kossl->SSL_shutdown(d->m_ssl);
			d->kossl->SSL_free(d->m_ssl);
			d->m_ssl = 0;
			return -1;
		}
	}

	if (!d->kossl->_SSL_session_reused(d->m_ssl)) {
		if (d->session) {
			kdDebug(7029) << "Session reuse failed.  New session used instead." << endl;
			delete d->session;
			d->session = 0L;
		}
	}

	if (!d->session) {
		SSL_SESSION *sess = d->kossl->SSL_get1_session(d->m_ssl);
		if (sess) {
			d->session = new KSSLSession;
			d->session->_session = sess;
		}
	}

return rc;
#else
return -1;
#endif
}


int KSSL::pending() {
#ifdef KSSL_HAVE_SSL
	if (!m_bInit)
		return -1;
return d->kossl->SSL_pending(d->m_ssl);
#else
return -1;
#endif
}


int KSSL::peek(void *buf, int len) {
#ifdef KSSL_HAVE_SSL
	if (!m_bInit)
		return -1;
	// FIXME: enhance to work the way read() does below, handling errors
return d->kossl->SSL_peek(d->m_ssl, buf, len);
#else
return -1;
#endif
}


int KSSL::read(void *buf, int len) {
#ifdef KSSL_HAVE_SSL
	int rc = 0;
	int maxIters = 10;

	if (!m_bInit)
		return -1;

read_again:
	rc = d->kossl->SSL_read(d->m_ssl, (char *)buf, len);
	if (rc <= 0) {
		int err = d->kossl->SSL_get_error(d->m_ssl, rc);

		if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
			kdDebug(7029) << "SSL read() returning 0: " << err << endl;
			if (maxIters-- > 0) {
				::usleep(20000); // 20ms sleep
				goto read_again;
			}
			return 0;
		}

		kdDebug(7029) << "SSL READ ERROR: " << err << endl;
		if (err != SSL_ERROR_NONE &&
		    err != SSL_ERROR_ZERO_RETURN && err != SSL_ERROR_SYSCALL) {
			rc = -1;      // OpenSSL returns 0 on error too
			d->kossl->ERR_print_errors_fp(stderr);
		}

//		else if (err == SSL_ERROR_ZERO_RETURN)
//			rc = 0;
	}
return rc;
#else
return -1;
#endif
}


int KSSL::write(const void *buf, int len) {
#ifdef KSSL_HAVE_SSL
	if (!m_bInit)
		return -1;

write_again:
	int rc = d->kossl->SSL_write(d->m_ssl, (const char *)buf, len);
	if (rc <= 0) {      // OpenSSL returns 0 on error too
		int err = d->kossl->SSL_get_error(d->m_ssl, rc);

		if (err == SSL_ERROR_WANT_WRITE) {
			::usleep(20000); // 20ms sleep
			goto write_again;
		}

		kdDebug(7029) << "SSL WRITE ERROR: " << err << endl;
		if (err != SSL_ERROR_NONE &&
		    err != SSL_ERROR_ZERO_RETURN && err != SSL_ERROR_SYSCALL)
			rc = -1;
	}

return rc;
#else
return -1;
#endif
}


bool KSSL::reconfig() {
	return reInitialize();
}


void KSSL::setAutoReconfig(bool ar) {
	m_bAutoReconfig = ar;
}


bool KSSL::setSettings(KSSLSettings *settings) {
	delete m_cfg;
	m_cfg = settings;
	return reconfig();
}


#ifdef KSSL_HAVE_SSL
bool KSSL::m_bSSLWorks = true;
#else
bool KSSL::m_bSSLWorks = false;
#endif

bool KSSL::doesSSLWork() {
	return m_bSSLWorks;
}


void KSSL::setConnectionInfo() {
#ifdef KSSL_HAVE_SSL
SSL_CIPHER *sc;
char buf[1024];

	buf[0] = 0;  // for safety.
	sc = d->kossl->SSL_get_current_cipher(d->m_ssl);
	if (!sc) {
		kdDebug(7029) << "KSSL get current cipher failed - we're probably gonna crash!" << endl;
		return;
	}

	// set the number of bits, bits used
	m_ci.m_iCipherUsedBits = d->kossl->SSL_CIPHER_get_bits(sc, &(m_ci.m_iCipherBits));
	// set the cipher version
	m_ci.m_cipherVersion = d->kossl->SSL_CIPHER_get_version(sc);
	// set the cipher name
	m_ci.m_cipherName = d->kossl->SSL_CIPHER_get_name(sc);
	// set the cipher description
	m_ci.m_cipherDescription = d->kossl->SSL_CIPHER_description(sc, buf, 1023);

#endif
}


void KSSL::setPeerInfo() {
#ifdef KSSL_HAVE_SSL
	m_pi.setPeerHost(d->proxyPeer);
	m_pi.m_cert.setCert(d->kossl->SSL_get_peer_certificate(d->m_ssl));
	STACK_OF(X509) *xs = d->kossl->SSL_get_peer_cert_chain(d->m_ssl);
	if (xs)
		xs = reinterpret_cast<STACK_OF(X509)*>(d->kossl->OPENSSL_sk_dup(xs));   // Leak?
	m_pi.m_cert.setChain((void *)xs);
#endif
}


KSSLConnectionInfo& KSSL::connectionInfo() {
	return m_ci;
}


// KDE 4: Make it const TQString &
void KSSL::setPeerHost(TQString realHost) {
	d->proxyPeer = realHost;
}

// deprecated
void KSSL::setProxyUse(bool, TQString, int, TQString) {
}


KSSLPeerInfo& KSSL::peerInfo() {
	return m_pi;
}


bool KSSL::setClientCertificate(KSSLPKCS12 *pkcs) {
#ifdef KSSL_HAVE_SSL
	if (!pkcs || !pkcs->getCertificate())
		return false;

int rc;
X509 *x = pkcs->getCertificate()->getCert();
EVP_PKEY *k = pkcs->getPrivateKey();

	if (!x || !k) return false;

	if (!pkcs->getCertificate()->x509V3Extensions().certTypeSSLClient())
		return false;

	rc = d->kossl->SSL_CTX_use_certificate(d->m_ctx, x);
	if (rc <= 0) {
		kdDebug(7029) << "KSSL - SSL_CTX_use_certificate failed.  rc = " << rc << endl;
		return false;
	}

	rc = d->kossl->SSL_CTX_use_PrivateKey(d->m_ctx, k);
	if (rc <= 0) {
		kdDebug(7029) << "KSSL - SSL_CTX_use_PrivateKey failed.  rc = " << rc << endl;
		return false;
	}

	return true;
#else
	return false;
#endif
}

const KSSLSession* KSSL::session() const {
	return d->session;
}

bool KSSL::reusingSession() const {
#ifdef KSSL_HAVE_SSL
	return (d->m_ssl && d->kossl->_SSL_session_reused(d->m_ssl));
#else
	return false;
#endif
}