ksslcsessioncache.cc

00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2003 Stefan Rompf <sux@loplof.de>
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 <qpair.h>
00022 #include <qstring.h>
00023 #include <qptrlist.h>
00024 
00025 #include <kdebug.h>
00026 #include <kstaticdeleter.h>
00027 #include <kurl.h>
00028 
00029 #ifdef Q_WS_WIN
00030 #include "ksslconfig_win.h"
00031 #else
00032 #include "ksslconfig.h"
00033 #endif
00034 
00035 #include "ksslcsessioncache.h"
00036 
00037 /* 
00038  * Operation:
00039  *
00040  * Sessions will be stored per running application, not KDE
00041  * wide, to avoid security problems with hostile programs
00042  * that negotiate sessions with weak cryptographic keys and store
00043  * them for everybody to use - I really don't want that.
00044  *
00045  * Retrieval is organised similiar to George's thoughts in the KSSLD
00046  * certificate cache: The cache is organised as a list, with the
00047  * recently fetched (or stored) session first.
00048  *
00049  * The cache has an artificial limit of 32 sessions (should really
00050  * be enough), and relies on the peer server for timeouts
00051  *
00052  */
00053 #define MAX_ENTRIES 32
00054 
00055 #ifdef KSSL_HAVE_SSL
00056 
00057 typedef QPair<QString,QString> KSSLCSession;
00058 typedef QPtrList<KSSLCSession> KSSLCSessions;
00059 
00060 static KSSLCSessions *sessions = 0L;
00061 static KStaticDeleter<KSSLCSessions> med;
00062 
00063 
00064 static QString URLtoKey(const KURL &kurl) {
00065     return kurl.host() + ":" + kurl.protocol() + ":" + QString::number(kurl.port());
00066 }
00067 
00068 
00069 static void setup() {
00070     KSSLCSessions *ses = new KSSLCSessions;
00071     ses->setAutoDelete(true);
00072     med.setObject(sessions, ses);
00073 }
00074 
00075 #endif
00076 
00077 QString KSSLCSessionCache::getSessionForURL(const KURL &kurl) {
00078 #ifdef KSSL_HAVE_SSL
00079     if (!sessions) return QString::null;
00080     QString key = URLtoKey(kurl);
00081 
00082     for(KSSLCSession *it = sessions->first(); it; it=sessions->next()) {
00083     if (it->first == key) {
00084         sessions->take();
00085         sessions->prepend(it);
00086         return it->second;
00087     }
00088     }
00089 
00090     // Negative caching disabled: cache pollution
00091 #if 0 
00092     kdDebug(7029) <<"Negative caching " <<key <<endl;
00093     if (sessions->count() >= MAX_ENTRIES) sessions->removeLast();
00094     sessions->prepend(new KSSLCSession(key, QString::null));
00095 #endif
00096 
00097 #endif
00098     return QString::null;
00099 }
00100 
00101 
00102 void KSSLCSessionCache::putSessionForURL(const KURL &kurl, const QString &session) {
00103 #ifdef KSSL_HAVE_SSL
00104     if (!sessions) setup();
00105     QString key = URLtoKey(kurl);
00106     KSSLCSession *it;
00107 
00108     for(it = sessions->first(); it && it->first != key; it=sessions->next());
00109 
00110     if (it) {
00111     sessions->take();
00112     it->second = session;
00113     } else {
00114     it = new KSSLCSession(key, session);
00115     if (sessions->count() >= MAX_ENTRIES) sessions->removeLast();
00116     }
00117 
00118     sessions->prepend(it);
00119 #endif
00120 }
KDE Home | KDE Accessibility Home | Description of Access Keys