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 "GSSContextCache.h"
17 GSSContextCache* GSSContextCache::_instance = 0;
19 GSSContextCache::GSSContextCache()
24 GSSContextCache::~GSSContextCache()
29 GSSContextCache* GSSContextCache::instance()
32 _instance = new GSSContextCache;
38 std::string GSSContextCache::store ( GSSContext& data, std::string inKey )
47 if ( inKey.length() > 0 )
51 else if ( !generateKey(key) )
53 // Key generation failed. Eeek!
54 throw std::runtime_error("Could not generate random data for an ID");
57 // Store the key/value pair in the map
58 // Store the key in the context for convenience
63 // Return the key for future reference
67 /*************************************
68 * Generate random bytes, and base64 *
69 * encode them to be JSONable keys *
70 *************************************/
71 bool GSSContextCache::generateKey(std::string &key)
75 unsigned char theKey[KEYLEN];
76 bool existingErrors = false;
79 // See if there are any queued OpenSSL errors already.
80 existingErrors = ( 0 == ERR_peek_error() );
84 // Generate random byte string
85 osslReturn = RAND_pseudo_bytes(theKey, KEYLEN);
87 // Discard the error message if there weren't any OpenSSL errors to begin with.
88 if (osslReturn == 1 && !existingErrors)
90 while (0 != ERR_get_error() );
94 // Encode the binary string
95 key = g_base64_encode(theKey, KEYLEN);
102 GSSContext& GSSContextCache::retrieve ( std::string key )
110 // Maybe do something about data entries that are expired?
114 return contexts[key];