Skip to content

Commit 7cced74

Browse files
committed
lib/net: Use FingerprintData to represent fingerprints
1 parent 50534ec commit 7cced74

8 files changed

Lines changed: 70 additions & 42 deletions

File tree

src/gui/src/SslCertificate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void SslCertificate::generateFingerprint(const std::string& cert_path)
7676

7777
auto local_path = DataDirectories::local_ssl_fingerprints_path();
7878
barrier::FingerprintDatabase db;
79-
db.add_trusted(barrier::FingerprintData{"sha1", fingerprint});
79+
db.add_trusted(fingerprint);
8080
db.write(local_path);
8181

8282
emit info(tr("SSL fingerprint generated."));
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
#ifndef BARRIER_LIB_NET_FINGERPRINT_TYPE_H
19-
#define BARRIER_LIB_NET_FINGERPRINT_TYPE_H
20-
21-
#include <string>
18+
#include "base/String.h"
19+
#include "FingerprintDatabase.h"
20+
#include "io/fstream.h"
21+
#include <algorithm>
22+
#include <fstream>
2223

2324
namespace barrier {
2425

25-
enum FingerprintType {
26-
INVALID,
27-
SHA1, // deprecated
28-
SHA256,
29-
};
26+
bool FingerprintData::operator==(const FingerprintData& other) const
27+
{
28+
return algorithm == other.algorithm && data == other.data;
29+
}
3030

31-
inline const char* fingerprint_type_to_string(FingerprintType type)
31+
const char* fingerprint_type_to_string(FingerprintType type)
3232
{
3333
switch (type) {
3434
case FingerprintType::INVALID: return "invalid";
@@ -38,7 +38,7 @@ inline const char* fingerprint_type_to_string(FingerprintType type)
3838
return "invalid";
3939
}
4040

41-
inline FingerprintType fingerprint_type_from_string(const std::string& type)
41+
FingerprintType fingerprint_type_from_string(const std::string& type)
4242
{
4343
if (type == "sha1") {
4444
return FingerprintType::SHA1;
@@ -50,5 +50,3 @@ inline FingerprintType fingerprint_type_from_string(const std::string& type)
5050
}
5151

5252
} // namespace barrier
53-
54-
#endif // BARRIER_LIB_NET_FINGERPRINT_TYPE_H

src/lib/net/FingerprintData.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
barrier -- mouse and keyboard sharing utility
3+
Copyright (C) Barrier contributors
4+
5+
This package is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License
7+
found in the file LICENSE that should have accompanied this file.
8+
9+
This package is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef BARRIER_LIB_NET_FINGERPRINT_DATA_H
19+
#define BARRIER_LIB_NET_FINGERPRINT_DATA_H
20+
21+
#include <string>
22+
#include <vector>
23+
24+
namespace barrier {
25+
26+
enum FingerprintType {
27+
INVALID,
28+
SHA1, // deprecated
29+
SHA256,
30+
};
31+
32+
struct FingerprintData {
33+
std::string algorithm;
34+
std::vector<std::uint8_t> data;
35+
36+
bool valid() const { return !algorithm.empty(); }
37+
38+
bool operator==(const FingerprintData& other) const;
39+
};
40+
41+
const char* fingerprint_type_to_string(FingerprintType type);
42+
FingerprintType fingerprint_type_from_string(const std::string& type);
43+
44+
} // namespace barrier
45+
46+
#endif // BARRIER_LIB_NET_FINGERPRINT_TYPE_H

src/lib/net/FingerprintDatabase.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323

2424
namespace barrier {
2525

26-
bool FingerprintData::operator==(const FingerprintData& other) const
27-
{
28-
return algorithm == other.algorithm && data == other.data;
29-
}
30-
3126
void FingerprintDatabase::read(const std::string& path)
3227
{
3328
std::ifstream file;

src/lib/net/FingerprintDatabase.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,13 @@
1818
#ifndef BARRIER_LIB_NET_FINGERPRINT_DATABASE_H
1919
#define BARRIER_LIB_NET_FINGERPRINT_DATABASE_H
2020

21-
#include "FingerprintType.h"
21+
#include "FingerprintData.h"
2222
#include <iosfwd>
2323
#include <string>
2424
#include <vector>
2525

2626
namespace barrier {
2727

28-
struct FingerprintData {
29-
std::string algorithm;
30-
std::vector<std::uint8_t> data;
31-
32-
bool valid() const { return !algorithm.empty(); }
33-
34-
bool operator==(const FingerprintData& other) const;
35-
};
36-
3728
class FingerprintDatabase {
3829
public:
3930
void read(const std::string& path);

src/lib/net/SecureSocket.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,17 +657,17 @@ bool
657657
SecureSocket::verifyCertFingerprint()
658658
{
659659
// calculate received certificate fingerprint
660-
std::vector<std::uint8_t> fingerprint_raw;
660+
barrier::FingerprintData fingerprint;
661661
try {
662-
fingerprint_raw = barrier::get_ssl_cert_fingerprint(SSL_get_peer_certificate(m_ssl->m_ssl),
663-
barrier::FingerprintType::SHA1);
662+
fingerprint = barrier::get_ssl_cert_fingerprint(SSL_get_peer_certificate(m_ssl->m_ssl),
663+
barrier::FingerprintType::SHA1);
664664
} catch (const std::exception& e) {
665665
LOG((CLOG_ERR "%s", e.what()));
666666
return false;
667667
}
668668

669669
LOG((CLOG_NOTE "server fingerprint: %s",
670-
barrier::format_ssl_fingerprint(fingerprint_raw).c_str()));
670+
barrier::format_ssl_fingerprint(fingerprint.data).c_str()));
671671

672672
auto fingerprint_db_path = DataDirectories::trusted_servers_ssl_fingerprints_path();
673673

@@ -685,7 +685,6 @@ SecureSocket::verifyCertFingerprint()
685685
fingerprint_db_path.c_str()));
686686
}
687687

688-
barrier::FingerprintData fingerprint{"sha1", fingerprint_raw};
689688
if (db.is_trusted(fingerprint)) {
690689
LOG((CLOG_NOTE "Fingerprint matches trusted fingerprint"));
691690
return true;

src/lib/net/SecureUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18+
#include "FingerprintDatabase.h"
1819
#include "SecureUtils.h"
1920
#include "base/String.h"
2021
#include "base/finally.h"
@@ -59,7 +60,7 @@ std::string format_ssl_fingerprint(const std::vector<uint8_t>& fingerprint, bool
5960
return result;
6061
}
6162

62-
std::vector<std::uint8_t> get_ssl_cert_fingerprint(X509* cert, FingerprintType type)
63+
FingerprintData get_ssl_cert_fingerprint(X509* cert, FingerprintType type)
6364
{
6465
if (!cert) {
6566
throw std::runtime_error("certificate is null");
@@ -77,11 +78,10 @@ std::vector<std::uint8_t> get_ssl_cert_fingerprint(X509* cert, FingerprintType t
7778
std::vector<std::uint8_t> digest_vec;
7879
digest_vec.assign(reinterpret_cast<std::uint8_t*>(digest),
7980
reinterpret_cast<std::uint8_t*>(digest) + digest_length);
80-
return digest_vec;
81+
return {fingerprint_type_to_string(type), digest_vec};
8182
}
8283

83-
std::vector<std::uint8_t> get_pem_file_cert_fingerprint(const std::string& path,
84-
FingerprintType type)
84+
FingerprintData get_pem_file_cert_fingerprint(const std::string& path, FingerprintType type)
8585
{
8686
auto fp = fopen_utf8_path(path, "r");
8787
if (!fp) {

src/lib/net/SecureUtils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#ifndef BARRIER_LIB_NET_SECUREUTILS_H
1919
#define BARRIER_LIB_NET_SECUREUTILS_H
2020

21-
#include "FingerprintType.h"
21+
#include "FingerprintData.h"
2222
#include <openssl/ossl_typ.h>
2323
#include <cstdint>
2424
#include <string>
@@ -29,10 +29,9 @@ namespace barrier {
2929
std::string format_ssl_fingerprint(const std::vector<std::uint8_t>& fingerprint,
3030
bool separator = true);
3131

32-
std::vector<std::uint8_t> get_ssl_cert_fingerprint(X509* cert, FingerprintType type);
32+
FingerprintData get_ssl_cert_fingerprint(X509* cert, FingerprintType type);
3333

34-
std::vector<std::uint8_t> get_pem_file_cert_fingerprint(const std::string& path,
35-
FingerprintType type);
34+
FingerprintData get_pem_file_cert_fingerprint(const std::string& path, FingerprintType type);
3635

3736
void generate_pem_self_signed_cert(const std::string& path);
3837

0 commit comments

Comments
 (0)