SessionCache.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #ifndef __SESSION_CACHE_H__
  20. #define __SESSION_CACHE_H__
  21. #include <openssl/pem.h>
  22. #include <openssl/conf.h>
  23. #include <openssl/x509v3.h>
  24. #include <openssl/ssl.h>
  25. #include <openssl/err.h>
  26. #include <boost/thread.hpp>
  27. #include <boost/bind.hpp>
  28. #define CACHE_SIZE 20
  29. #define MAX_ENCODING_SIZE 4096
  30. #define MAX_ID_SIZE 512
  31. typedef struct {
  32. int idLength;
  33. unsigned char id[MAX_ID_SIZE];
  34. int encodingLength;
  35. unsigned char encoding[MAX_ENCODING_SIZE];
  36. } EncodedSession;
  37. typedef struct {
  38. int current;
  39. EncodedSession sessions[CACHE_SIZE];
  40. } SessionCacheBlock;
  41. class SessionCache {
  42. public:
  43. static SessionCache* getInstance();
  44. static SSL_SESSION * getSessionIdTramp(SSL *s, unsigned char *id, int idLength, int *ref);
  45. static int setNewSessionIdTramp(SSL *s, SSL_SESSION *session);
  46. int setNewSessionId(SSL *s, SSL_SESSION *session);
  47. int setNewSessionId(SSL *s, SSL_SESSION *session, unsigned char *id, int idLength);
  48. SSL_SESSION * getSessionId(SSL *s, unsigned char *id, int idLength, int *ref);
  49. private:
  50. static SessionCache *sessionCache;
  51. static boost::mutex singletonLock;
  52. SessionCacheBlock cache;
  53. boost::mutex cacheLock;
  54. SessionCache();
  55. void removeSessionId(unsigned char* id, int idLength);
  56. };
  57. #endif