From 0e406d491823bfc9dfed0fcc7934cdece8db7dd0 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Mon, 1 Nov 2021 02:52:29 +0200 Subject: [PATCH] lib/net: Extract fingerprint formatting out of SecureSocket --- src/lib/net/SecureSocket.cpp | 22 ++------------------- src/lib/net/SecureSocket.h | 1 - src/lib/net/SecureUtils.cpp | 38 ++++++++++++++++++++++++++++++++++++ src/lib/net/SecureUtils.h | 25 ++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 src/lib/net/SecureUtils.cpp create mode 100644 src/lib/net/SecureUtils.h diff --git a/src/lib/net/SecureSocket.cpp b/src/lib/net/SecureSocket.cpp index c3c1a064..f6ed0194 100644 --- a/src/lib/net/SecureSocket.cpp +++ b/src/lib/net/SecureSocket.cpp @@ -16,6 +16,7 @@ */ #include "SecureSocket.h" +#include "SecureUtils.h" #include "net/TSocketMultiplexerMethodJob.h" #include "base/TMethodEventJob.h" @@ -656,25 +657,6 @@ SecureSocket::disconnect() sendEvent(getEvents()->forIStream().inputShutdown()); } -void SecureSocket::formatFingerprint(std::string& fingerprint, bool hex, bool separator) -{ - if (hex) { - // to hexadecimal - barrier::string::toHex(fingerprint, 2); - } - - // all uppercase - barrier::string::uppercase(fingerprint); - - if (separator) { - // add colon to separate each 2 characters - size_t separators = fingerprint.size() / 2; - for (size_t i = 1; i < separators; i++) { - fingerprint.insert(i * 3 - 1, ":"); - } - } -} - bool SecureSocket::verifyCertFingerprint() { @@ -693,7 +675,7 @@ SecureSocket::verifyCertFingerprint() // format fingerprint into hexdecimal format with colon separator std::string fingerprint(reinterpret_cast(tempFingerprint), tempFingerprintLen); - formatFingerprint(fingerprint); + format_ssl_fingerprint(fingerprint); LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str())); std::string trustedServersFilename; diff --git a/src/lib/net/SecureSocket.h b/src/lib/net/SecureSocket.h index f861d662..24653b6f 100644 --- a/src/lib/net/SecureSocket.h +++ b/src/lib/net/SecureSocket.h @@ -68,7 +68,6 @@ private: void showError(const std::string& reason); std::string getError(); void disconnect(); - void formatFingerprint(std::string& fingerprint, bool hex = true, bool separator = true); bool verifyCertFingerprint(); MultiplexerJobStatus serviceConnect(ISocketMultiplexerJob*, bool, bool, bool); diff --git a/src/lib/net/SecureUtils.cpp b/src/lib/net/SecureUtils.cpp new file mode 100644 index 00000000..c796e9c7 --- /dev/null +++ b/src/lib/net/SecureUtils.cpp @@ -0,0 +1,38 @@ +/* + barrier -- mouse and keyboard sharing utility + Copyright (C) Barrier contributors + + This package is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + found in the file LICENSE that should have accompanied this file. + + This package is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "SecureUtils.h" +#include "base/String.h" + +void format_ssl_fingerprint(std::string& fingerprint, bool hex, bool separator) +{ + if (hex) { + // to hexadecimal + barrier::string::toHex(fingerprint, 2); + } + + // all uppercase + barrier::string::uppercase(fingerprint); + + if (separator) { + // add colon to separate each 2 characters + size_t separators = fingerprint.size() / 2; + for (size_t i = 1; i < separators; i++) { + fingerprint.insert(i * 3 - 1, ":"); + } + } +} diff --git a/src/lib/net/SecureUtils.h b/src/lib/net/SecureUtils.h new file mode 100644 index 00000000..7b6d09bc --- /dev/null +++ b/src/lib/net/SecureUtils.h @@ -0,0 +1,25 @@ +/* + barrier -- mouse and keyboard sharing utility + Copyright (C) Barrier contributors + + This package is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + found in the file LICENSE that should have accompanied this file. + + This package is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef BARRIER_LIB_NET_SECUREUTILS_H +#define BARRIER_LIB_NET_SECUREUTILS_H + +#include + +void format_ssl_fingerprint(std::string& fingerprint, bool hex = true, bool separator = true); + +#endif // BARRIER_LIB_NET_SECUREUTILS_H