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