This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection_info_openssl.cpp
More file actions
97 lines (72 loc) · 1.75 KB
/
Copy pathconnection_info_openssl.cpp
File metadata and controls
97 lines (72 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Copyright (C) 2020 Tristan. All Rights Reserved.
* This file is licensed under the BSD 2-Clause license.
* See the COPYING file for licensing information.
*/
#include "connection_info.hpp"
#include <chrono>
#include <iostream>
#include <sstream>
#include <vector>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include "logger.hpp"
namespace Net {
void
ConnectionInfo::TLSDestroy() {
Logger::Debug(static_cast<const char *>(__PRETTY_FUNCTION__), "Called");
SSL_free(static_cast<SSL *>(ssl));
SSL_CTX_free(static_cast<SSL_CTX *>(sslContext));
ssl = nullptr;
sslContext = nullptr;
}
bool
ConnectionInfo::TLSRead(char *buf, std::size_t len) {
do {
auto ret = SSL_read(static_cast<SSL *>(ssl), buf, len);
if (ret <= 0)
return false;
buf += ret;
len -= ret;
} while (len > 0);
return true;
}
std::optional<char>
ConnectionInfo::TLSReadChar() {
char character;
if (SSL_read(static_cast<SSL *>(ssl), &character, 1) == -1)
return std::optional<char>();
return std::optional<char>(character);
}
bool
ConnectionInfo::TLSSetup() {
// OpenSSL_add_all_algorithms();
// SSL_load_error_strings();
SSL_CTX *ctx = SSL_CTX_new(SSLv23_method());
if (ctx == nullptr) {
ERR_print_errors_fp(stderr);
return false;
}
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, socket);
if (SSL_connect(ssl) == -1) {
ERR_print_errors_fp(stderr);
return false;
}
this->sslContext = ctx;
this->ssl = ssl;
isAuthenticated = true;
return true;
}
bool
ConnectionInfo::TLSWrite(const char *buf, std::size_t len) {
do {
auto ret = SSL_write(static_cast<SSL *>(ssl), buf, len);
if (ret <= 0)
return false;
buf += ret;
len -= ret;
} while (len > 0);
return true;
}
} // namespace Net