allow the SessionIdManager to use only two keys

This commit is contained in:
2022-07-26 05:18:42 +02:00
parent 522b57ade4
commit 2e8f4d1a26
3 changed files with 118 additions and 12 deletions

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2014-2018, Tomasz Sowa
* Copyright (c) 2014-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -83,21 +83,54 @@ public:
SessionIdManager();
/*
* initialization
* we are using keys stored in a file
* those keys are automatically generated
* this is the default algorithm
*/
static const int ALGORITHM_MULTIPLE_KEYS = 1;
/*
* we use only two keys
* those keys you have to provide
*/
static const int ALGORITHM_SINGLE_KEYS = 2;
/*
* initialization for ALGORITHM_MULTIPLE_KEYS algorithm
* this method takes about 1MB memory more (for AES key expansions)
* if you do not need the session cookie to be enrypted then don't call this method
*
*/
void Init(const std::wstring & keys_file);
void InitMultipleKeys(const std::wstring & keys_file);
/*
* initialization for ALGORITHM_SINGLE_KEYS algorithm
* keys should be provided as 16*2 or 24*2 or 32*2 hexadecimal characters
*
*/
bool InitSingleKeys(const std::wstring & key1, const std::wstring & key2);
/*
* true if the object is correctly initialized
*
*/
bool IsInitialized();
/*
* how often a new AES key pairs should be generated
* used with ALGORITHM_MULTIPLE_KEYS algorithm
*/
void SetKeyRenewTime(time_t renew_time);
/*
* encode/decode the session cookie
* make sure the Init() method is called first
* make sure InitMultipleKeys() or InitSingleKeys() method is called first
*
* for ALGORITHM_SINGLE_KEYS algorithm the cur_utc_time parameter is ignored
*
*/
bool EncodeToken(size_t id, unsigned int index, time_t cur_utc_time, std::wstring & token);
bool DecodeToken(const std::wstring & token, size_t & id, unsigned int & index);
@@ -106,7 +139,7 @@ public:
private:
bool was_inited;
char algorithm_type;
int algorithm_type;
std::string string_token, string_token_base64;
std::vector<std::string> key_tab1, key_tab2;
size_t key_index;
@@ -120,6 +153,7 @@ private:
std::string file_name_ascii;
std::vector<Tito::AES> aes1, aes2;
bool InitializeKey(const std::wstring & key, std::vector<std::string> & key_tab);
bool ReadKeysFromFile(const wchar_t * file);
bool ReadKeysFromFile(const std::wstring & file);
bool SaveKeysToFile(const wchar_t * file);
@@ -142,6 +176,7 @@ private:
bool CheckControlSums(const char * str);
void InitializeAesKeys(size_t index);
bool DecodeAES(const char * str, size_t key);
char GetAlgoritmTypeAsString();
template<typename Value>
void Append(std::string & str, Value val);