Skip to content

Commit 87cfdd8

Browse files
committed
Added tls_socket::peer_certificate()
1 parent e227f81 commit 87cfdd8

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed

examples/tls/tlsconn.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@
3636
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3737
// --------------------------------------------------------------------------
3838

39-
#include <getopt.h>
40-
41-
#include <iostream>
42-
#include <string>
43-
4439
#include "sockpp/tcp_connector.h"
4540
#include "sockpp/tls/connector.h"
4641
#include "sockpp/tls/context.h"
4742
#include "sockpp/tls/error.h"
4843
#include "sockpp/version.h"
4944

45+
#include <getopt.h>
46+
47+
#include <iostream>
48+
#include <fstream>
49+
#include <string>
50+
5051
using namespace std;
5152

5253
int main(int argc, char* argv[]) {
@@ -138,6 +139,17 @@ int main(int argc, char* argv[]) {
138139
return 1;
139140
}
140141

142+
cout << "Successful connection to " << addr << endl;
143+
144+
if (auto cert = conn.peer_certificate(); cert.empty()) {
145+
cout << "No peer certificate" << endl;
146+
}
147+
else {
148+
ofstream fil("peer.cer", ios::binary);
149+
fil.write(reinterpret_cast<const char*>(cert.data()), cert.size());
150+
cout << "Wrote peer certificate to peer.cer" << endl;
151+
}
152+
141153
if (auto res = conn.write("HELO"); !res) {
142154
cerr << "Error sending request [0x" << hex << res.error().value()
143155
<< "]: " << res.error_message() << endl;
@@ -150,6 +162,5 @@ int main(int argc, char* argv[]) {
150162
return 1;
151163
}
152164

153-
cout << "Successful connection to " << addr << endl;
154165
return 0;
155166
}

include/sockpp/tls/openssl_context.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ class tls_context
179179
);
180180
/**
181181
* Sets the verify flag in the context to the specified mode.
182-
* This wraps <a
182+
* This wraps <A
183183
* href="https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_verify.html">
184-
* SSL_CTX_set_verify</a>
184+
* SSL_CTX_set_verify
185185
* @param mode The verification mode.
186186
*/
187187
void set_verify(verify_t mode) noexcept;

include/sockpp/tls/openssl_socket.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,27 @@ class tls_socket : public stream_socket
113113
* this may be null if the socket failed on construction.
114114
*/
115115
SSL* ssl() { return ssl_; }
116-
117-
uint32_t peer_certificate_status() { return 0; }
118-
119116
/**
120-
* Returns an error message describing any problem with the peer's
121-
* certificate.
117+
* Move assignment.
118+
* @param rhs The other socket to move into this one.
119+
* @return A reference to this object.
122120
*/
123-
string peer_certificate_status_message() { return string{}; }
121+
tls_socket& operator=(tls_socket&& rhs);
124122

125123
/**
126124
* Returns the peer's X.509 certificate data, in binary DER format.
127125
*/
128-
string peer_certificate() { return string{}; }
126+
binary peer_certificate();
127+
129128
/**
130-
* Move assignment.
131-
* @param rhs The other socket to move into this one.
132-
* @return A reference to this object.
129+
*
133130
*/
134-
tls_socket& operator=(tls_socket&& rhs);
131+
uint32_t peer_certificate_status();
132+
/**
133+
* Returns an error message describing any problem with the peer's
134+
* certificate.
135+
*/
136+
string peer_certificate_status_message();
135137

136138
// I/O primitives
137139

include/sockpp/types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
#include <chrono>
5151
#include <string>
52+
#include <cstdint>
5253

5354
namespace sockpp {
5455

@@ -63,6 +64,9 @@ using std::string;
6364
/** A sockpp::duration is a std::chrono::duration */
6465
using std::chrono::duration;
6566

67+
/** A binary blob as a basic string/collection of uint8_t */
68+
using binary = std::basic_string<uint8_t>;
69+
6670
// Time units are std::chrono time unite.
6771
using std::chrono::microseconds;
6872
using std::chrono::milliseconds;

src/tls/openssl_socket.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,39 @@ tls_socket& tls_socket::operator=(tls_socket&& rhs) {
7373
return *this;
7474
}
7575

76+
binary tls_socket::peer_certificate() {
77+
// TODO: Implement this
78+
X509* cert = SSL_get0_peer_certificate(ssl_);
79+
80+
if (!cert)
81+
return binary{};
82+
83+
uint8_t* buf = nullptr;
84+
int len = i2d_X509(cert, &buf);
85+
86+
// TODO: Return an error result on <0?
87+
if (len <= 0)
88+
return binary{};
89+
90+
binary certBin{buf, size_t(len)};
91+
OPENSSL_free(buf);
92+
93+
return certBin;
94+
}
95+
96+
uint32_t tls_socket::peer_certificate_status() {
97+
// TODO: Implement this?
98+
return 0;
99+
}
100+
101+
// Returns an error message describing any problem with
102+
// the peer's certificate.
103+
string tls_socket::peer_certificate_status_message() {
104+
// TODO: Implement this?
105+
return string{};
106+
}
107+
108+
76109
result<size_t> tls_socket::read(void* buf, size_t n) {
77110
size_t nx;
78111
int ret = ::SSL_read_ex(ssl_, buf, n, &nx);

0 commit comments

Comments
 (0)