Skip to content

Commit 2f9ce27

Browse files
authored
Merge pull request #1234 from fwsGonzo/dev
Add Botan, TLS stream and Secure HTTP server
2 parents 04f8b2f + 68efdf6 commit 2f9ce27

15 files changed

Lines changed: 785 additions & 73 deletions

File tree

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ set(CMAKE_C_FLAGS "-target i686 -MMD ${CAPABS} ${WARNS} -nostdlib -nostdlibinc -
8989
option(from_bundle "Download and use pre-compiled libraries for cross-comilation" ON)
9090
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/cross_compiled_libraries.txt)
9191

92+
# Botan Crypto & TLS
93+
# Note: Include order matters!
94+
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/botan.cmake)
95+
9296
#
9397
# Subprojects
9498
#

api/https

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// -*- C++ -*-
2+
// This file is a part of the IncludeOS unikernel - www.includeos.org
3+
//
4+
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
5+
// and Alfred Bratterud
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
19+
#pragma once
20+
#ifndef API_HTTPS_HEADER
21+
#define API_HTTPS_HEADER
22+
23+
#include "net/http/response.hpp"
24+
#include "net/http/request.hpp"
25+
#include "net/http/secure_server.hpp"
26+
27+
#endif

api/memdisk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@
2525

2626
namespace fs
2727
{
28+
// new singleton interface for the memdisk
29+
inline Disk& memdisk()
30+
{
31+
static MemDisk device;
32+
static Disk disk {device};
33+
return disk;
34+
}
2835
// new_shared_memdisk() very likely contains FAT
36+
// Note: deprecated!
2937
inline Disk_ptr new_shared_memdisk()
3038
{
3139
static MemDisk device;

api/net/http/secure_server.hpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2016-2017 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#pragma once
19+
#ifndef NET_HTTP_SECURE_SERVER_HPP
20+
#define NET_HTTP_SECURE_SERVER_HPP
21+
22+
#include <net/http/server.hpp>
23+
#include <fs/dirent.hpp>
24+
#include <net/tls/server.hpp>
25+
26+
namespace http {
27+
28+
class Secure_server : public http::Server
29+
{
30+
public:
31+
Secure_server(
32+
fs::Dirent& ca_key,
33+
fs::Dirent& ca_cert,
34+
fs::Dirent& server_key,
35+
TCP& tcp,
36+
Request_handler cb
37+
);
38+
39+
Secure_server(
40+
Botan::Credentials_Manager* in_credman,
41+
Botan::RandomNumberGenerator& in_rng,
42+
TCP& tcp,
43+
Request_handler cb)
44+
: http::Server(tcp, cb), rng(in_rng), credman(in_credman)
45+
{
46+
on_connect = {this, &Secure_server::secure_connect};
47+
}
48+
49+
void secure_connect(TCP_conn conn)
50+
{
51+
auto* ptr = new net::tls::Server(conn, rng, *credman);
52+
53+
ptr->on_connect(
54+
[this, ptr] (net::Stream&)
55+
{
56+
// create and pass TLS socket
57+
Server::connect(std::unique_ptr<net::tls::Server>(ptr));
58+
});
59+
ptr->on_close([ptr] {
60+
printf("Secure_HTTP::on_close on %s\n", ptr->to_string().c_str());
61+
delete ptr;
62+
});
63+
}
64+
65+
private:
66+
Botan::RandomNumberGenerator& rng;
67+
std::unique_ptr<Botan::Credentials_Manager> credman;
68+
};
69+
70+
} // http
71+
72+
#endif

api/net/http/server.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ namespace http {
9090
*/
9191
Response_ptr create_response(status_t code = http::OK) const;
9292

93-
~Server();
93+
virtual ~Server();
94+
95+
protected:
96+
delegate<void(TCP_conn)> on_connect;
97+
void connect(Connection::Stream_ptr stream);
9498

9599
private:
96100
friend class Server_connection;
@@ -109,10 +113,9 @@ namespace http {
109113
Stat& stat_req_bad_;
110114
Stat& stat_timeouts_;
111115

112-
void connect(TCP_conn conn)
113-
{ connect(std::make_unique<Connection::Stream>(conn)); }
114-
115-
void connect(Connection::Stream_ptr stream);
116+
void connected(TCP_conn conn) {
117+
connect(std::make_unique<Connection::Stream>(conn));
118+
}
116119

117120
void close(Server_connection&);
118121

api/net/stream.hpp

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#pragma once
19+
#ifndef NET_STREAM_HPP
20+
#define NET_STREAM_HPP
21+
22+
#include <cstdint>
23+
#include <cstddef>
24+
#include <delegate>
25+
#include <util/chunk.hpp>
26+
#include "tcp/socket.hpp"
27+
28+
namespace net {
29+
class Stream;
30+
using Stream_ptr = std::unique_ptr<Stream>;
31+
/**
32+
* @brief An abstract network Stream interface based on TCP.
33+
*/
34+
class Stream {
35+
public:
36+
using buffer_t = std::shared_ptr<uint8_t>;
37+
using ptr = Stream_ptr;
38+
39+
/** Called when the stream is ready to be used. */
40+
using ConnectCallback = delegate<void(Stream& self)>;
41+
/**
42+
* @brief Event when the stream is connected/established/ready to be used.
43+
*
44+
* @param[in] cb The connect callback
45+
*/
46+
virtual void on_connect(ConnectCallback cb) = 0;
47+
48+
/** Called with a shared buffer and the length of the data when received. */
49+
using ReadCallback = delegate<void(buffer_t, size_t)>;
50+
/**
51+
* @brief Event when data is received.
52+
*
53+
* @param[in] n The size of the receive buffer
54+
* @param[in] cb The read callback
55+
*/
56+
virtual void on_read(size_t n, ReadCallback cb) = 0;
57+
58+
/** Called with nothing ¯\_(ツ)_/¯ */
59+
using CloseCallback = delegate<void()>;
60+
/**
61+
* @brief Event for when the Stream is being closed.
62+
*
63+
* @param[in] cb The close callback
64+
*/
65+
virtual void on_close(CloseCallback cb) = 0;
66+
67+
/** Called with the number of bytes written. */
68+
using WriteCallback = delegate<void(size_t)>;
69+
/**
70+
* @brief Event for when data has been written.
71+
*
72+
* @param[in] cb The write callback
73+
*/
74+
virtual void on_write(WriteCallback cb) = 0;
75+
76+
/**
77+
* @brief Async write of a data with a length.
78+
*
79+
* @param[in] buf data
80+
* @param[in] n length
81+
*/
82+
virtual void write(const void* buf, size_t n) = 0;
83+
84+
/**
85+
* @brief Async write of a chunk.
86+
*
87+
* @param[in] c A chunk
88+
*/
89+
virtual void write(Chunk c) = 0;
90+
91+
/**
92+
* @brief Async write of a shared buffer with a length.
93+
*
94+
* @param[in] buffer shared buffer
95+
* @param[in] n length
96+
*/
97+
virtual void write(buffer_t buf, size_t n) = 0;
98+
99+
/**
100+
* @brief Async write of a string.
101+
*
102+
* @param[in] str The string
103+
*/
104+
virtual void write(const std::string& str) = 0;
105+
106+
/**
107+
* @brief Closes the stream.
108+
*/
109+
virtual void close() = 0;
110+
111+
/**
112+
* @brief Aborts (terminates) the stream.
113+
*/
114+
virtual void abort() = 0;
115+
116+
/**
117+
* @brief Resets all callbacks.
118+
*/
119+
virtual void reset_callbacks() = 0;
120+
121+
/**
122+
* @brief Returns the streams local socket.
123+
*
124+
* @return A TCP Socket
125+
*/
126+
virtual tcp::Socket local() const = 0;
127+
128+
/**
129+
* @brief Returns the streams remote socket.
130+
*
131+
* @return A TCP Socket
132+
*/
133+
virtual tcp::Socket remote() const = 0;
134+
135+
/**
136+
* @brief Returns the local port.
137+
*
138+
* @return A TCP port
139+
*/
140+
virtual uint16_t local_port() const = 0;
141+
142+
/**
143+
* @brief Returns a string representation of the stream.
144+
*
145+
* @return String representation of the stream.
146+
*/
147+
virtual std::string to_string() const = 0;
148+
149+
/**
150+
* @brief Determines if connected (established).
151+
*
152+
* @return True if connected, False otherwise.
153+
*/
154+
virtual bool is_connected() const noexcept = 0;
155+
156+
/**
157+
* @brief Determines if writable. (write is allowed)
158+
*
159+
* @return True if writable, False otherwise.
160+
*/
161+
virtual bool is_writable() const noexcept = 0;
162+
163+
/**
164+
* @brief Determines if readable. (data can be received)
165+
*
166+
* @return True if readable, False otherwise.
167+
*/
168+
virtual bool is_readable() const noexcept = 0;
169+
170+
/**
171+
* @brief Determines if closing.
172+
*
173+
* @return True if closing, False otherwise.
174+
*/
175+
virtual bool is_closing() const noexcept = 0;
176+
177+
/**
178+
* @brief Determines if closed.
179+
*
180+
* @return True if closed, False otherwise.
181+
*/
182+
virtual bool is_closed() const noexcept = 0;
183+
184+
Stream() = default;
185+
virtual ~Stream() {}
186+
187+
}; // < class Stream
188+
189+
} // < namespace net
190+
191+
#endif // < NET_STREAM_HPP

0 commit comments

Comments
 (0)