File tree Expand file tree Collapse file tree 5 files changed +69
-19
lines changed Expand file tree Collapse file tree 5 files changed +69
-19
lines changed Original file line number Diff line number Diff line change 36
36
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
37
// --------------------------------------------------------------------------
38
38
39
- #include < getopt.h>
40
-
41
- #include < iostream>
42
- #include < string>
43
-
44
39
#include " sockpp/tcp_connector.h"
45
40
#include " sockpp/tls/connector.h"
46
41
#include " sockpp/tls/context.h"
47
42
#include " sockpp/tls/error.h"
48
43
#include " sockpp/version.h"
49
44
45
+ #include < getopt.h>
46
+
47
+ #include < iostream>
48
+ #include < fstream>
49
+ #include < string>
50
+
50
51
using namespace std ;
51
52
52
53
int main (int argc, char * argv[]) {
@@ -138,6 +139,17 @@ int main(int argc, char* argv[]) {
138
139
return 1 ;
139
140
}
140
141
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
+
141
153
if (auto res = conn.write (" HELO" ); !res) {
142
154
cerr << " Error sending request [0x" << hex << res.error ().value ()
143
155
<< " ]: " << res.error_message () << endl;
@@ -150,6 +162,5 @@ int main(int argc, char* argv[]) {
150
162
return 1 ;
151
163
}
152
164
153
- cout << " Successful connection to " << addr << endl;
154
165
return 0 ;
155
166
}
Original file line number Diff line number Diff line change @@ -179,9 +179,9 @@ class tls_context
179
179
);
180
180
/* *
181
181
* Sets the verify flag in the context to the specified mode.
182
- * This wraps <a
182
+ * This wraps <A
183
183
* href="https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_verify.html">
184
- * SSL_CTX_set_verify</a>
184
+ * SSL_CTX_set_verify
185
185
* @param mode The verification mode.
186
186
*/
187
187
void set_verify (verify_t mode) noexcept ;
Original file line number Diff line number Diff line change @@ -113,25 +113,27 @@ class tls_socket : public stream_socket
113
113
* this may be null if the socket failed on construction.
114
114
*/
115
115
SSL* ssl () { return ssl_; }
116
-
117
- uint32_t peer_certificate_status () { return 0 ; }
118
-
119
116
/* *
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.
122
120
*/
123
- string peer_certificate_status_message () { return string{}; }
121
+ tls_socket& operator =(tls_socket&& rhs);
124
122
125
123
/* *
126
124
* Returns the peer's X.509 certificate data, in binary DER format.
127
125
*/
128
- string peer_certificate () { return string{}; }
126
+ binary peer_certificate ();
127
+
129
128
/* *
130
- * Move assignment.
131
- * @param rhs The other socket to move into this one.
132
- * @return A reference to this object.
129
+ *
133
130
*/
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 ();
135
137
136
138
// I/O primitives
137
139
Original file line number Diff line number Diff line change 49
49
50
50
#include < chrono>
51
51
#include < string>
52
+ #include < cstdint>
52
53
53
54
namespace sockpp {
54
55
@@ -63,6 +64,9 @@ using std::string;
63
64
/* * A sockpp::duration is a std::chrono::duration */
64
65
using std::chrono::duration;
65
66
67
+ /* * A binary blob as a basic string/collection of uint8_t */
68
+ using binary = std::basic_string<uint8_t >;
69
+
66
70
// Time units are std::chrono time unite.
67
71
using std::chrono::microseconds;
68
72
using std::chrono::milliseconds;
Original file line number Diff line number Diff line change @@ -73,6 +73,39 @@ tls_socket& tls_socket::operator=(tls_socket&& rhs) {
73
73
return *this ;
74
74
}
75
75
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
+
76
109
result<size_t > tls_socket::read (void * buf, size_t n) {
77
110
size_t nx;
78
111
int ret = ::SSL_read_ex (ssl_, buf, n, &nx);
You can’t perform that action at this time.
0 commit comments