ksslsession.cc
00001 /* This file is part of the KDE project 00002 * 00003 * Copyright (C) 2003 George Staikos <staikos@kde.org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "ksslsession.h" 00022 00023 #ifdef HAVE_CONFIG_H 00024 #include <config.h> 00025 #endif 00026 00027 #include <kopenssl.h> 00028 #include <kmdcodec.h> 00029 00030 KSSLSession::KSSLSession() : _session(0L) { 00031 } 00032 00033 00034 KSSLSession::~KSSLSession() { 00035 #ifdef KSSL_HAVE_SSL 00036 if (_session) { 00037 KOpenSSLProxy::self()->SSL_SESSION_free(static_cast<SSL_SESSION*>(_session)); 00038 _session = 0L; 00039 } 00040 #endif 00041 } 00042 00043 00044 QString KSSLSession::toString() const { 00045 QString rc; 00046 #ifdef KSSL_HAVE_SSL 00047 QByteArray qba; 00048 SSL_SESSION *session = static_cast<SSL_SESSION*>(_session); 00049 unsigned int slen = KOpenSSLProxy::self()->i2d_SSL_SESSION(session, 0L); 00050 unsigned char *csess = new unsigned char[slen]; 00051 unsigned char *p = csess; 00052 00053 if (!KOpenSSLProxy::self()->i2d_SSL_SESSION(session, &p)) { 00054 delete[] csess; 00055 return QString::null; 00056 } 00057 00058 // encode it into a QString 00059 qba.duplicate((const char*)csess, slen); 00060 delete[] csess; 00061 rc = KCodecs::base64Encode(qba); 00062 #endif 00063 return rc; 00064 } 00065 00066 00067 KSSLSession *KSSLSession::fromString(const QString& s) { 00068 KSSLSession *session = 0L; 00069 #ifdef KSSL_HAVE_SSL 00070 QByteArray qba, qbb = s.local8Bit().copy(); 00071 KCodecs::base64Decode(qbb, qba); 00072 unsigned char *qbap = reinterpret_cast<unsigned char *>(qba.data()); 00073 SSL_SESSION *ss = KOSSL::self()->d2i_SSL_SESSION(0L, &qbap, qba.size()); 00074 if (ss) { 00075 session = new KSSLSession; 00076 session->_session = ss; 00077 } 00078 #endif 00079 return session; 00080 } 00081 00082