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-2021, Tomasz Sowa
* Copyright (c) 2014-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -40,6 +40,7 @@
#include "utf8/utf8.h"
#include "date/date.h"
#include "misc.h"
#include "convert/text.h"
namespace Winix
@@ -48,8 +49,8 @@ namespace Winix
SessionIdManager::SessionIdManager()
{
algorithm_type = 'a';
key_tab_size = 256;
algorithm_type = ALGORITHM_MULTIPLE_KEYS;
key_tab_size = 0;
key_index = 0;
last_key_generated = 0;
key_renew_time = 60;
@@ -57,9 +58,12 @@ SessionIdManager::SessionIdManager()
}
void SessionIdManager::Init(const std::wstring & keys_file)
void SessionIdManager::InitMultipleKeys(const std::wstring & keys_file)
{
algorithm_type = ALGORITHM_MULTIPLE_KEYS;
was_inited = true;
key_tab_size = 256;
key_tab1.resize(key_tab_size);
key_tab2.resize(key_tab_size);
@@ -72,6 +76,57 @@ void SessionIdManager::Init(const std::wstring & keys_file)
}
bool SessionIdManager::InitSingleKeys(const std::wstring & key1, const std::wstring & key2)
{
algorithm_type = ALGORITHM_SINGLE_KEYS;
key_tab_size = 1;
key_tab1.resize(key_tab_size);
key_tab2.resize(key_tab_size);
aes1.resize(key_tab_size);
aes2.resize(key_tab_size);
was_inited = true;
was_inited = was_inited && InitializeKey(key1, key_tab1);
was_inited = was_inited && InitializeKey(key2, key_tab2);
if( was_inited )
{
InitializeAesKeys();
}
return was_inited;
}
bool SessionIdManager::IsInitialized()
{
return was_inited;
}
bool SessionIdManager::InitializeKey(const std::wstring & key, std::vector<std::string> & key_tab)
{
if( key.size() == 16 * 2 || key.size() == 24 * 2 || key.size() == 32 * 2 )
{
if( !pt::hex_string_to_bytes(key, key_tab[0]) )
{
log << log1 << "SIM: an incorrect character in a key, expected a hex digits in the range 0-9 or a-f" << logend;
}
}
else
{
log << log1 << "SIM: provided key has size: " << key.size()
<< ", expected size is 16, 24 or 32 bytes (written as two hex digits each)" << logend;
return false;
}
return true;
}
void SessionIdManager::SetKeyRenewTime(time_t renew_time)
{
key_renew_time = renew_time;
@@ -400,6 +455,7 @@ return true;
}
bool SessionIdManager::EncodeToken(size_t id, unsigned int index, time_t cur_utc_time, std::wstring & token)
{
size_t pad_top_size;
@@ -413,10 +469,14 @@ char pad_bottom_value;
if( !was_inited )
return false;
CheckKeys(cur_utc_time);
if( algorithm_type == ALGORITHM_MULTIPLE_KEYS )
{
CheckKeys(cur_utc_time);
}
RandPadding(pad_top_size, pad_top_value, pad_bottom_size, pad_bottom_value);
string_token += algorithm_type;
string_token += GetAlgoritmTypeAsString();
string_token += (unsigned char)key_index;
string_token += pad_top_value;
string_token += pad_bottom_value;
@@ -553,6 +613,17 @@ return false;
}
char SessionIdManager::GetAlgoritmTypeAsString()
{
if( algorithm_type == ALGORITHM_MULTIPLE_KEYS )
return 'a';
if( algorithm_type == ALGORITHM_SINGLE_KEYS )
return 'b';
return 'a';
}
} // namespace Winix