2 * Copyright (c) 2014 <copyright holder> <email>
4 * For license details, see the LICENSE file in the root of this project.
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
13 #include "utils/base64.h"
14 #include "GSSContextCache.h"
18 GSSContextCache* GSSContextCache::_instance = 0;
20 GSSContextCache::GSSContextCache()
25 GSSContextCache::~GSSContextCache()
30 GSSContextCache* GSSContextCache::instance()
33 _instance = new GSSContextCache;
39 std::string GSSContextCache::store ( GSSContext& data, std::string inKey )
48 if ( inKey.length() > 0 )
52 else if ( !generateKey(key) )
54 // Key generation failed. Eeek!
55 throw std::runtime_error("Could not generate random data for an ID");
58 // Store the key/value pair in the map
59 // Store the key in the context for convenience
64 // Return the key for future reference
68 /*************************************
69 * Generate random bytes, and base64 *
70 * encode them to be JSONable keys *
71 *************************************/
72 bool GSSContextCache::generateKey(std::string &key)
76 unsigned char theKey[KEYLEN];
77 bool existingErrors = false;
80 // See if there are any queued OpenSSL errors already.
81 existingErrors = ( 0 == ERR_peek_error() );
85 // Generate random byte string
86 osslReturn = RAND_pseudo_bytes(theKey, KEYLEN);
88 // Discard the error message if there weren't any OpenSSL errors to begin with.
89 if (osslReturn == 1 && !existingErrors)
91 while (0 != ERR_get_error() );
95 // Encode the binary string
97 key = base64_encode(key);
104 GSSContext& GSSContextCache::retrieve ( std::string key )
112 // Maybe do something about data entries that are expired?
116 return contexts[key];