TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.2KB

  1. /* This file is part of the KDE project
  2. *
  3. * Copyright (C) 2003 Stefan Rompf <sux@loplof.de>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include <tqpair.h>
  21. #include <tqstring.h>
  22. #include <tqptrlist.h>
  23. #include <kdebug.h>
  24. #include <kstaticdeleter.h>
  25. #include <kurl.h>
  26. #ifdef Q_WS_WIN
  27. #include "ksslconfig_win.h"
  28. #else
  29. #include "ksslconfig.h"
  30. #endif
  31. #include "ksslcsessioncache.h"
  32. /*
  33. * Operation:
  34. *
  35. * Sessions will be stored per running application, not KDE
  36. * wide, to avoid security problems with hostile programs
  37. * that negotiate sessions with weak cryptographic keys and store
  38. * them for everybody to use - I really don't want that.
  39. *
  40. * Retrieval is organised similiar to George's thoughts in the KSSLD
  41. * certificate cache: The cache is organised as a list, with the
  42. * recently fetched (or stored) session first.
  43. *
  44. * The cache has an artificial limit of 32 sessions (should really
  45. * be enough), and relies on the peer server for timeouts
  46. *
  47. */
  48. #define MAX_ENTRIES 32
  49. #ifdef KSSL_HAVE_SSL
  50. typedef QPair<TQString,TQString> KSSLCSession;
  51. typedef TQPtrList<KSSLCSession> KSSLCSessions;
  52. static KSSLCSessions *sessions = 0L;
  53. static KStaticDeleter<KSSLCSessions> med;
  54. static TQString URLtoKey(const KURL &kurl) {
  55. return kurl.host() + ":" + kurl.protocol() + ":" + TQString::number(kurl.port());
  56. }
  57. static void setup() {
  58. KSSLCSessions *ses = new KSSLCSessions;
  59. ses->setAutoDelete(true);
  60. med.setObject(sessions, ses);
  61. }
  62. #endif
  63. TQString KSSLCSessionCache::getSessionForURL(const KURL &kurl) {
  64. #ifdef KSSL_HAVE_SSL
  65. if (!sessions) return TQString::null;
  66. TQString key = URLtoKey(kurl);
  67. for(KSSLCSession *it = sessions->first(); it; it=sessions->next()) {
  68. if (it->first == key) {
  69. sessions->take();
  70. sessions->prepend(it);
  71. return it->second;
  72. }
  73. }
  74. // Negative caching disabled: cache pollution
  75. #if 0
  76. kdDebug(7029) <<"Negative caching " <<key <<endl;
  77. if (sessions->count() >= MAX_ENTRIES) sessions->removeLast();
  78. sessions->prepend(new KSSLCSession(key, TQString::null));
  79. #endif
  80. #endif
  81. return TQString::null;
  82. }
  83. void KSSLCSessionCache::putSessionForURL(const KURL &kurl, const TQString &session) {
  84. #ifdef KSSL_HAVE_SSL
  85. if (!sessions) setup();
  86. TQString key = URLtoKey(kurl);
  87. KSSLCSession *it;
  88. for(it = sessions->first(); it && it->first != key; it=sessions->next());
  89. if (it) {
  90. sessions->take();
  91. it->second = session;
  92. } else {
  93. it = new KSSLCSession(key, session);
  94. if (sessions->count() >= MAX_ENTRIES) sessions->removeLast();
  95. }
  96. sessions->prepend(it);
  97. #endif
  98. }