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 "GSSNameCache.h"
14 #include "utils/base64.h"
18 GSSNameCache* GSSNameCache::_instance = 0;
20 GSSNameCache::GSSNameCache()
25 GSSNameCache::GSSNameCache ( const GSSNameCache& other )
30 GSSNameCache::~GSSNameCache()
35 GSSNameCache& GSSNameCache::operator= ( const GSSNameCache& other )
43 std::string GSSNameCache::store ( GSSName& data, std::string inKey )
52 if ( inKey.length() > 0 )
56 else if ( !generateKey(key) )
58 // Key generation failed. Eeek!
59 throw std::runtime_error("Could not generate random data for an ID");
62 // Store the key/value pair in the map
63 // Store the key in the context for convenience
64 // std::cout << "\n==> In GSSNameCache::store, about to store data in the names hash.\n";
69 // Return the key for future reference
73 /*************************************
74 * Generate random bytes, and base64 *
75 * encode them to be JSONable keys *
76 *************************************/
77 bool GSSNameCache::generateKey(std::string &key)
81 unsigned char theKey[KEYLEN];
82 bool existingErrors = false;
85 // See if there are any queued OpenSSL errors already.
86 existingErrors = ( 0 == ERR_peek_error() );
90 // Generate random byte string
91 osslReturn = RAND_pseudo_bytes(theKey, KEYLEN);
93 // Discard the error message if there weren't any OpenSSL errors to begin with.
94 if (osslReturn == 1 && !existingErrors)
96 while (0 != ERR_get_error() );
100 // Encode the binary string
101 key = (char *)theKey;
102 key = base64_encode(key);
109 GSSName& GSSNameCache::retrieve ( std::string key )
117 // Maybe do something about data entries that are expired?
121 return *(names[key]);
124 GSSNameCache* GSSNameCache::instance()
127 _instance = new GSSNameCache;