27void show_hmac_debug(
unsigned char* hex,
unsigned int length,
const std::string& context)
30 int off = context.length();
31 buf =
new char[length * 3 + off + 2];
32 strcpy(buf, context.c_str());
33 for (
unsigned int i = 0; i < length; i++) {
34 sprintf(&(buf[off + i * 3]),
"%X ", hex[i]);
44 authentication_enabled(false)
47 static bool auth_warning_shown =
false;
48 if (auth_warning_shown) {
59 fname = rf.
findFile(
"auth.conf", opt);
64 yCDebug(AUTHHMAC,
"Cannot find auth.conf file. Authentication disabled.\n");
65 auth_warning_shown =
true;
74 yCWarning(AUTHHMAC,
"No \"AUTH\" group found in auth.conf file. Authentication disabled.\n");
75 auth_warning_shown =
true;
80 if (!(key.length() > 0)) {
81 yCWarning(AUTHHMAC,
"No \"key\" found in \"AUTH\" group in auth.conf file. Authentication disabled.\n");
82 auth_warning_shown =
true;
86 size_t key_len = key.length();
87 auto* tmp =
new unsigned char[key_len];
88 strcpy(
reinterpret_cast<char*
>(tmp), key.c_str());
89 HMAC_INIT(&context, tmp,
static_cast<unsigned int>(key_len));
91 srand(
static_cast<unsigned>(time(
nullptr)));
93 if (!authentication_enabled) {
94 yCInfo(AUTHHMAC,
"Authentication enabled.\n");
95 authentication_enabled =
true;
103 if (!authentication_enabled) {
127 if (!send_hmac(streamOut, nonce1, mac)) {
134 if (!receive_hmac(streamIn, nonce2, mac)) {
142 if (!check_hmac(mac, mac_check)) {
157 return send_hmac(streamOut, nonce3, mac);
162 if (!authentication_enabled) {
182 if (!receive_hmac(streamIn, nonce1, mac)) {
188 if (!check_hmac(mac, mac_check)) {
200 if (!send_hmac(streamOut, nonce2, mac)) {
208 if (!receive_hmac(streamIn, nonce3, mac)) {
216 if (!check_hmac(mac, mac_check)) {
225bool AuthHMAC::send_hmac(
OutputStream* stream,
unsigned char* nonce,
unsigned char* mac)
229 stream->
write(nonce_bytes);
230 stream->
write(mac_bytes);
235 return stream->
isOk();
238bool AuthHMAC::receive_hmac(
InputStream* stream,
unsigned char* nonce,
unsigned char* mac)
242 stream->
read(nonce_bytes);
243 stream->
read(mac_bytes);
248 return stream->
isOk();
251bool AuthHMAC::check_hmac(
unsigned char* mac,
unsigned char* mac_check)
255 std::string check =
"digest check ";
257 check +=
"successful";
267void AuthHMAC::fill_nonce(
unsigned char* nonce)
269 std::random_device rd;
270 std::mt19937 mt(rd());
271 std::uniform_int_distribution<int> dist(0, 255);
272 for (
unsigned int i = 0; i <
NONCE_LEN; i++) {
273 nonce[i] =
static_cast<unsigned char>(dist(mt));
void show_hmac_debug(unsigned char *hex, unsigned int length, const std::string &context)
A simple collection of objects that can be described and transmitted in a portable way.
bool isNull() const override
Checks if the object is invalid.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
A simple abstraction for a block of bytes.
static void unlock()
Call post() on a global mutual-exclusion semaphore allocated by YARP.
static void lock()
Call wait() on a global mutual-exclusion semaphore allocated by YARP.
Simple specification of the minimum functions needed from output streams.
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
virtual void write(char ch)
Write a single byte to the stream.
A class for storing options and configuration information.
bool fromConfigFile(const std::string &fname, bool wipe=true)
Interprets a file as a list of properties.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
These options are loosely based on http://wiki.icub.org/wiki/YARP_ResourceFinder.
MessageFilter messageFilter
Helper class for finding config files and other external resources.
static ResourceFinder & getResourceFinderSingleton()
Access a ResourceFinder singleton whose lifetime will match that of the YARP library.
std::string findFile(const std::string &name)
Find the full path to a file.
virtual std::string asString() const
Get string value.
bool authDest(yarp::os::InputStream *streamIn, yarp::os::OutputStream *streamOut)
bool authSource(yarp::os::InputStream *streamIn, yarp::os::OutputStream *streamOut)
#define yCInfo(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.