KIO
ksslcsessioncache.cpp
Go to the documentation of this file.
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 "ksslcsessioncache.h" 00022 00023 #include <QtCore/QCoreApplication> 00024 #include <QtCore/QPair> 00025 #include <QtCore/QString> 00026 00027 #include <kdebug.h> 00028 #include <kurl.h> 00029 00030 #include <ksslconfig.h> 00031 00032 /* 00033 * Operation: 00034 * 00035 * Sessions will be stored per running application, not KDE 00036 * wide, to avoid security problems with hostile programs 00037 * that negotiate sessions with weak cryptographic keys and store 00038 * them for everybody to use - I really don't want that. 00039 * 00040 * Retrieval is organized similar to George's thoughts in the KSSLD 00041 * certificate cache: The cache is organised as a list, with the 00042 * recently fetched (or stored) session first. 00043 * 00044 * The cache has an artificial limit of 32 sessions (should really 00045 * be enough), and relies on the peer server for timeouts 00046 * 00047 */ 00048 #define MAX_ENTRIES 32 00049 00050 #ifdef KSSL_HAVE_SSL 00051 00052 typedef QPair<QString,QString> KSSLCSession; 00053 typedef QList<KSSLCSession> KSSLCSessions; 00054 00055 static KSSLCSessions *sessions = 0L; 00056 00057 static QString URLtoKey(const KUrl &kurl) { 00058 return kurl.host() + ':' + kurl.protocol() + ':' + QString::number(kurl.port()); 00059 } 00060 00061 00062 static void cleanupKSSLCSessions() { 00063 delete sessions; 00064 sessions = 0; 00065 } 00066 00067 static void setup() { 00068 sessions = new KSSLCSessions; 00069 qAddPostRoutine(cleanupKSSLCSessions); 00070 } 00071 00072 #endif 00073 00074 QString KSSLCSessionCache::getSessionForUrl(const KUrl &kurl) { 00075 #ifdef KSSL_HAVE_SSL 00076 if (!sessions) return QString(); 00077 QString key = URLtoKey(kurl); 00078 00079 for (int i = 0; i < sessions->size(); ++i) { 00080 if (sessions->at(i).first == key) { 00081 QString snd = sessions->at(i).second; 00082 sessions->prepend(sessions->takeAt(i)); 00083 return snd; 00084 } 00085 } 00086 00087 // Negative caching disabled: cache pollution 00088 #if 0 00089 kDebug(7029) <<"Negative caching " <<key; 00090 if (sessions->count() >= MAX_ENTRIES) sessions->removeLast(); 00091 sessions->prepend(new KSSLCSession(key, QString())); 00092 #endif 00093 00094 #endif 00095 return QString(); 00096 } 00097 00098 00099 void KSSLCSessionCache::putSessionForUrl(const KUrl &kurl, const QString &session) { 00100 #ifdef KSSL_HAVE_SSL 00101 if (!sessions) setup(); 00102 QString key = URLtoKey(kurl); 00103 KSSLCSessions::iterator it = sessions->begin(); 00104 00105 while ( it != sessions->end() ) { 00106 if ( it->first == key ) 00107 break; 00108 ++it; 00109 } 00110 00111 if (it != sessions->end()) { 00112 it->second = session; 00113 } else { 00114 if (sessions->size() >= MAX_ENTRIES) 00115 sessions->removeLast(); 00116 sessions->prepend(KSSLCSession(key, session)); 00117 } 00118 00119 #endif 00120 }
KDE 4.6 API Reference