d6f6ca3c97e49a5dbf8a02d14b71211c50ecabc8
[gssweb.git] / json_gssapi / src / cache / GSSContextCache.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  *
4  * For license details, see the LICENSE file in the root of this project.
5  *
6  */
7
8 // #include <glib.h>
9 #include <stdexcept>
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
12
13 #include "utils/base64.h"
14 #include "GSSContextCache.h"
15
16 #define KEYLEN 128
17
18 GSSContextCache* GSSContextCache::_instance = 0;
19
20 GSSContextCache::GSSContextCache()
21 {
22
23 }
24
25 GSSContextCache::~GSSContextCache()
26 {
27
28 }
29
30 GSSContextCache* GSSContextCache::instance()
31 {
32     if (_instance == 0)
33       _instance = new GSSContextCache;
34     
35     return _instance;
36 }
37
38
39 std::string GSSContextCache::store ( GSSContext& data, std::string inKey )
40 {
41   /* Variables      */
42   std::string key;
43   
44   /* Error checking */
45   /* Setup          */
46   /* Main           */
47   // Generate a key
48   if ( inKey.length() > 0 )
49   {
50     key = inKey;
51   }
52   else if ( !generateKey(key) )
53   {
54     // Key generation failed.  Eeek!
55     throw std::runtime_error("Could not generate random data for an ID");
56   }
57   
58   // Store the key/value pair in the map
59   // Store the key in the context for convenience
60   contexts[key] = data;
61   
62   /* Cleanup        */
63   /* Return         */
64   // Return the key for future reference
65   return(key);
66 }
67
68 /*************************************
69  * Generate random bytes, and base64 *
70  * encode them to be JSONable keys   *
71  *************************************/
72 bool GSSContextCache::generateKey(std::string &key)
73 {
74   /* Variables      */
75   int  osslReturn = 0;
76   unsigned char theKey[KEYLEN];
77   bool existingErrors = false;
78   
79   /* Error checking */
80   // See if there are any queued OpenSSL errors already.
81   existingErrors = ( 0 == ERR_peek_error() ); 
82   
83   /* Setup          */
84   /* Main           */
85   // Generate random byte string
86   osslReturn = RAND_pseudo_bytes(theKey, KEYLEN);
87   
88   // Discard the error message if there weren't any OpenSSL errors to begin with.
89   if (osslReturn == 1 && !existingErrors)
90   {
91     while (0 != ERR_get_error() );
92     return(false);
93   }
94
95   // Encode the binary string
96   key = (char *)theKey;
97   key = base64_encode(key);
98   
99   /* Cleanup        */
100   /* Return         */
101   return(true);
102 }
103
104 GSSContext& GSSContextCache::retrieve ( std::string key )
105 {
106   /* Variables      */
107   //GSSContext data;
108   
109   /* Error checking */
110   /* Setup          */
111   /* Main           */
112   // Maybe do something about data entries that are expired?
113   
114   /* Cleanup        */
115   /* Return         */
116   return contexts[key];
117 }
118
119   /* Variables      */
120   /* Error checking */
121   /* Setup          */
122   /* Main           */
123   /* Cleanup        */
124   /* Return         */