aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-06-20 23:52:56 -0400
committerDeterminant <[email protected]>2019-06-20 23:52:56 -0400
commit823585c4db2ef6752d12f489c83edab577b86099 (patch)
tree9ee3d121e74cc88cb8496d87c19a49a0e2681b93
parent65f7c078663da387fd2cee8ab401c7ef66081138 (diff)
finish test_msgnet_tls exampletls
-rw-r--r--include/salticidae/conn.h20
-rw-r--r--include/salticidae/crypto.h21
-rw-r--r--test/.gitignore2
-rw-r--r--test/CMakeLists.txt3
-rw-r--r--test/alice.pem83
-rw-r--r--test/bench_network_tls.cpp6
-rw-r--r--test/bob.pem83
-rw-r--r--test/test_msgnet_tls.cpp182
8 files changed, 387 insertions, 13 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h
index 59d93fc..a3da96c 100644
--- a/include/salticidae/conn.h
+++ b/include/salticidae/conn.h
@@ -112,7 +112,9 @@ class ConnPool {
void disp_terminate();
public:
- Conn(): ready_send(false), send_data_func(nullptr), recv_data_func(nullptr) {}
+ Conn(): ready_send(false),
+ send_data_func(nullptr), recv_data_func(nullptr),
+ tls(nullptr), peer_cert(nullptr) {}
Conn(const Conn &) = delete;
Conn(Conn &&other) = delete;
@@ -133,7 +135,7 @@ class ConnPool {
operator std::string() const;
const NetAddr &get_addr() const { return addr; }
- const X509 &get_peer_cert() const { return *peer_cert; }
+ const X509 *get_peer_cert() const { return peer_cert.get(); }
ConnMode get_mode() const { return mode; }
ConnPool *get_pool() const { return cpool; }
MPSCWriteBuffer &get_send_buffer() { return send_buffer; }
@@ -190,12 +192,16 @@ class ConnPool {
void update_conn(const conn_t &conn, bool connected) {
user_tcall->async_call([this, conn, connected](ThreadCall::Handle &) {
- if ((!conn_cb ||
- conn_cb(conn, connected)) &&
- enable_tls && connected)
- conn->worker->get_tcall()->async_call([conn](ThreadCall::Handle &) {
- conn->recv_data_func = Conn::_recv_data_tls;
+ bool ret = !conn_cb || conn_cb(conn, connected);
+ if (enable_tls && connected)
+ {
+ conn->worker->get_tcall()->async_call([conn, ret](ThreadCall::Handle &) {
+ if (ret)
+ conn->recv_data_func = Conn::_recv_data_tls;
+ else
+ conn->worker_terminate();
});
+ }
});
}
diff --git a/include/salticidae/crypto.h b/include/salticidae/crypto.h
index bcfd9dc..7eec030 100644
--- a/include/salticidae/crypto.h
+++ b/include/salticidae/crypto.h
@@ -168,8 +168,8 @@ class PKey {
return PKey(key);
}
- bytearray_t get_pubkey_der() {
- uint8_t *der;
+ bytearray_t get_pubkey_der() const {
+ uint8_t *der = nullptr;
auto ret = i2d_PublicKey(key, &der);
if (ret <= 0)
throw SalticidaeError(SALTI_ERROR_TLS_KEY);
@@ -179,8 +179,8 @@ class PKey {
return std::move(res);
}
- bytearray_t get_privkey_der() {
- uint8_t *der;
+ bytearray_t get_privkey_der() const {
+ uint8_t *der = nullptr;
auto ret = i2d_PrivateKey(key, &der);
if (ret <= 0)
throw SalticidaeError(SALTI_ERROR_TLS_KEY);
@@ -229,13 +229,24 @@ class X509 {
return X509(x509);
}
- PKey get_pubkey() {
+ PKey get_pubkey() const {
auto key = X509_get_pubkey(x509);
if (key == nullptr)
throw SalticidaeError(SALTI_ERROR_TLS_X509);
return PKey(key);
}
+ bytearray_t get_der() const {
+ uint8_t *der = nullptr;
+ auto ret = i2d_X509(x509, &der);
+ if (ret <= 0)
+ throw SalticidaeError(SALTI_ERROR_TLS_X509);
+ bytearray_t res(der, der + ret);
+ OPENSSL_cleanse(der, ret);
+ OPENSSL_free(der);
+ return std::move(res);
+ }
+
~X509() { if (x509) X509_free(x509); }
};
diff --git a/test/.gitignore b/test/.gitignore
index bbd91a2..7cebf5d 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -7,3 +7,5 @@ test_queue
bench_network
Makefile
test_msgnet_c
+test_msgnet_tls
+bench_network_tls
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index e7c5813..d112b7a 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -29,6 +29,9 @@ target_link_libraries(test_bits salticidae_static)
add_executable(test_msgnet test_msgnet.cpp)
target_link_libraries(test_msgnet salticidae_static)
+add_executable(test_msgnet_tls test_msgnet_tls.cpp)
+target_link_libraries(test_msgnet_tls salticidae_static)
+
add_executable(test_p2p test_p2p.cpp)
target_link_libraries(test_p2p salticidae_static)
diff --git a/test/alice.pem b/test/alice.pem
new file mode 100644
index 0000000..6efe00f
--- /dev/null
+++ b/test/alice.pem
@@ -0,0 +1,83 @@
+-----BEGIN PRIVATE KEY-----
+MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDZFKh7iaGPvfvh
+6qYrQN0Twq9nnpukjHYG+Zjuq/6T/JgMVezRXDA6e6nPH1va6tRzi+MsJ4Ues7al
+m6ddnxNgUOI2ROl9G6Gmx68Y75BGR1vKMUw7ujz8flaXYFO5mASSbE87pI5F/Hm/
+7k0Ke47rEE6Ke1iARcv0pW9loFT8y00NBXg/IQSUSb1riDXOHVK3fY8xHN5w5/pX
+waLNpqTByKOPcBCQhJBWh83n09vYysDEFzJC3QUvNWNCUttbgexqY8hJO4jP5SD+
+DmSiPw+6gNiyJdPdvmY+VgDXtd1d1ApihkiLlSa3EhQAMbS6ajM3NuwS5ikpnZ5c
+NDvGuBS4XQx8R+X93Q1xyeclNVkd2bFj0rdjfIxyurl8Nziq2/NM/AVCtBXCdGDO
+8aFnivPlA/FtfKbfuhhNVWts0ODLHR8dd5AZpGjtgZBJiXW97XQOeG0hDp8WwBNn
+s0UFEewkMOMX1ByA7ZWeOSiJHe+lcmVEs7RaUcEhs5OK/sgAo3Ig+4mO0z6VH8gp
+/WxP8ce+5xA6cTao2wJn3N1ux4Oq4fqZXR3rPh7IcEkHCyBKL1jxiUmPlIXvmED2
+PzyWSxyoDTm9w1TBLamD0Usdd1P99Rclgri3erhr6kT4YnWV0WMqrsP3ZserGTkm
+3Gsi6s6X9sTdu3Yi/P4N2nvRPZOB0wIDAQABAoICAD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d6qBH3eG2paTc+aVRIaJhhwR/ommpILiU8X/kXq6StLGB1YD5Jm0KvREyME/o0U7
+sT/91lpmMtRtwqaIOTZbUn1vEmS3aHZKS5q+Vs/eYZm3exF/L+Bzy40vn9wxU0I=
+-----END CERTIFICATE-----
diff --git a/test/bench_network_tls.cpp b/test/bench_network_tls.cpp
index bb5d0c1..1143877 100644
--- a/test/bench_network_tls.cpp
+++ b/test/bench_network_tls.cpp
@@ -82,7 +82,11 @@ struct MyNet: public MsgNetworkByteOp {
const NetAddr &peer,
double stat_timeout = -1):
MsgNetworkByteOp(ec, MsgNetworkByteOp::Config(
- ConnPool::Config().queue_capacity(65536).enable_tls(true).tls_cert_file("all.pem").tls_key_file("all.pem")).burst_size(1000)),
+ ConnPool::Config()
+ .queue_capacity(65536)
+ .enable_tls(true)
+ .tls_cert_file("alice.pem")
+ .tls_key_file("alice.pem")).burst_size(1000)),
name(name),
peer(peer),
ev_period_stat(ec, [this, stat_timeout](TimerEvent &) {
diff --git a/test/bob.pem b/test/bob.pem
new file mode 100644
index 0000000..8f8afcc
--- /dev/null
+++ b/test/bob.pem
@@ -0,0 +1,83 @@
+-----BEGIN PRIVATE KEY-----
+MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDfnQZzGUUL/pQN
+4g3U7fI6DHsvy6AztawB89m5nbXOFA9YGiFpzTGByZ6xAJRRnoz5HtbwjbkAYiUc
+G+sbGF7RSDAchAOC0EYKK8YQd/kGYzD/mIFzyzRSk1Kpc/SSIDKmuO9iFWfNSOqI
+zfkE8M0hV2vwbZ/tZfi/ToP8nH7Zllpa6E2SQhQRDsojMPSyMkP2uYvBL9CjRbZz
+gsVPaHItfQkP5c3+oNTgTNbv6HwI98h2xXYIAL8MKg7B/xj8BXczf9KGhWNCcRn/
+ci+wME08e9+FZUH1JYIQgRAu1Q7eGp5JhGl4Tls9/YTH7FFVTbpZZaoRj95mMKVT
+zrXOlSdiQcAuAnfKvSm733tK9VL/JFFW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Mq/w8aJ+kUk8IyGToxtBVvmjbINRK0bgANHJOifzp5ASYqvEZAcggot1n
+pvVToqGDxtuuVd4qX8/DXPCVooOJ0RAcGZ44+hXfN1T5icPmnqMLuoPx9qiUZO+T
+PIHg2DknmZSEunyy4LO5SiEsQVBzL3v8voYIqoobBuB5A2wyG9BXT5c3OM2rxDkX
+UfXjxMdiGu3+iOaGE0KePXVY6oy3vgbmaowh5n8ZN6sN8aJPty3ZdpYpRPXKJsft
+DtTH5sITHiisU2+rV2kJMKyASrxPWJAxkelahbUacOzid83TG+uqBCUJY0YPLRsF
+TCvZ/jqJ8PNXFiZWnfyVkH3qFNjzQ595xMLe5fE5p/pCEV7aqHI0Ds9VYy82IJAT
+070s/gtoLj5DussTetb9rh7Nt9ndJ5T7vvL9aeSmulRs2Z1j6u7rTvo4TjAbLbxx
+bCR8r/T+5G0F9qLC6o4C2kFfZSiogxtM8n6tCJzzpWnpMwVad0a+GuH4tdNFsQA=
+-----END CERTIFICATE-----
diff --git a/test/test_msgnet_tls.cpp b/test/test_msgnet_tls.cpp
new file mode 100644
index 0000000..9c82234
--- /dev/null
+++ b/test/test_msgnet_tls.cpp
@@ -0,0 +1,182 @@
+/**
+ * Copyright (c) 2018 Cornell University.
+ *
+ * Author: Ted Yin <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is furnished to do
+ * so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <cstdio>
+#include <string>
+#include <functional>
+
+#include "salticidae/msg.h"
+#include "salticidae/event.h"
+#include "salticidae/network.h"
+#include "salticidae/stream.h"
+
+using salticidae::NetAddr;
+using salticidae::DataStream;
+using salticidae::MsgNetwork;
+using salticidae::htole;
+using salticidae::letoh;
+using std::placeholders::_1;
+using std::placeholders::_2;
+
+/** Hello Message. */
+struct MsgHello {
+ static const uint8_t opcode = 0x0;
+ DataStream serialized;
+ std::string name;
+ std::string text;
+ /** Defines how to serialize the msg. */
+ MsgHello(const std::string &name,
+ const std::string &text) {
+ serialized << htole((uint32_t)name.length());
+ serialized << name << text;
+ }
+ /** Defines how to parse the msg. */
+ MsgHello(DataStream &&s) {
+ uint32_t len;
+ s >> len;
+ len = letoh(len);
+ name = std::string((const char *)s.get_data_inplace(len), len);
+ len = s.size();
+ text = std::string((const char *)s.get_data_inplace(len), len);
+ }
+};
+
+/** Acknowledgement Message. */
+struct MsgAck {
+ static const uint8_t opcode = 0x1;
+ DataStream serialized;
+ MsgAck() {}
+ MsgAck(DataStream &&s) {}
+};
+
+const uint8_t MsgHello::opcode;
+const uint8_t MsgAck::opcode;
+
+using MsgNetworkByteOp = MsgNetwork<uint8_t>;
+
+struct MyNet: public MsgNetworkByteOp {
+ const std::string name;
+ const salticidae::uint256_t peer_footprint;
+ const NetAddr peer;
+
+ MyNet(const salticidae::EventContext &ec,
+ const std::string &name,
+ const std::string &peer_footprint_hex,
+ const NetAddr &peer):
+ MsgNetwork<uint8_t>(ec, MsgNetwork::Config(
+ ConnPool::Config()
+ .enable_tls(true)
+ .tls_cert_file(name + ".pem")
+ .tls_key_file(name + ".pem")
+ )),
+ name(name),
+ peer_footprint(salticidae::from_hex(peer_footprint_hex)),
+ peer(peer) {
+ /* message handler could be a bound method */
+ reg_handler(
+ salticidae::generic_bind(&MyNet::on_receive_hello, this, _1, _2));
+
+ reg_conn_handler([this](const ConnPool::conn_t &conn, bool connected) {
+ bool res = true;
+ if (connected)
+ {
+ auto cert_der = salticidae::get_hash(conn->get_peer_cert()->get_der());
+ res = peer_footprint == cert_der;
+ if (conn->get_mode() == ConnPool::Conn::ACTIVE)
+ {
+ printf("[%s] Connected, sending hello.\n",
+ this->name.c_str());
+ /* send the first message through this connection */
+ send_msg(MsgHello(this->name, "Hello there!"),
+ salticidae::static_pointer_cast<Conn>(conn));
+ }
+ else
+ {
+ printf("[%s] Accepted, waiting for greetings.\n"
+ "The peer certificate footprint is %s (%s).\n",
+ this->name.c_str(), salticidae::get_hex(cert_der).c_str(),
+ res ? "ok" : "fail");
+ }
+ }
+ else
+ {
+ printf("[%s] Disconnected, retrying.\n", this->name.c_str());
+ /* try to reconnect to the same address */
+ connect(conn->get_addr(), false);
+ }
+ return res;
+ });
+ }
+
+ void on_receive_hello(MsgHello &&msg, const MyNet::conn_t &conn) {
+ printf("[%s] %s says %s\n",
+ name.c_str(),
+ msg.name.c_str(), msg.text.c_str());
+ /* send acknowledgement */
+ send_msg(MsgAck(), conn);
+ }
+};
+
+
+void on_receive_ack(MsgAck &&msg, const MyNet::conn_t &conn) {
+ auto net = static_cast<MyNet *>(conn->get_net());
+ printf("[%s] the peer knows\n", net->name.c_str());
+}
+
+int main() {
+ salticidae::EventContext ec;
+ NetAddr alice_addr("127.0.0.1:12345");
+ NetAddr bob_addr("127.0.0.1:12346");
+
+ /* test two nodes in the same main loop */
+ MyNet alice(ec, "alice", "ed5a9a8c7429dcb235a88244bc69d43d16b35008ce49736b27aaa3042a674043", bob_addr);
+ MyNet bob(ec, "bob", "ef3bea4e72f4d0e85da7643545312e2ff6dded5e176560bdffb1e53b1cef4896", alice_addr);
+
+ /* message handler could be a normal function */
+ alice.reg_handler(on_receive_ack);
+ bob.reg_handler(on_receive_ack);
+
+ /* start all threads */
+ alice.start();
+ bob.start();
+
+ /* accept incoming connections */
+ alice.listen(alice_addr);
+ bob.listen(bob_addr);
+
+ /* try to connect once */
+ alice.connect(bob_addr);
+ bob.connect(alice_addr);
+
+ /* the main loop can be shutdown by ctrl-c or kill */
+ auto shutdown = [&](int) {ec.stop();};
+ salticidae::SigEvent ev_sigint(ec, shutdown);
+ salticidae::SigEvent ev_sigterm(ec, shutdown);
+ ev_sigint.add(SIGINT);
+ ev_sigterm.add(SIGTERM);
+
+ /* enter the main loop */
+ ec.dispatch();
+ return 0;
+}