Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secure tunnel v3 #84

Merged
merged 51 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
86b7771
add connection_id and related API
sbSteveK Feb 27, 2023
8677550
connection ids wip
sbSteveK Mar 8, 2023
720d261
removed duplicate assignment
sbSteveK Mar 8, 2023
b7200c1
Connection id related to existing API
sbSteveK Mar 15, 2023
fdfce92
change function name to better reflect its action
sbSteveK Mar 15, 2023
0db4a36
function name change
sbSteveK Mar 15, 2023
8a8ba69
names again
sbSteveK Mar 15, 2023
1cdf0e4
remove unecessary logging
sbSteveK Mar 15, 2023
e5e49cf
removed unused define
sbSteveK Mar 15, 2023
b18723b
back compat and related tests (#86)
sbSteveK Mar 27, 2023
21f1864
test edit
sbSteveK Mar 27, 2023
fe95b41
fix merge conflict
sbSteveK Mar 27, 2023
eefbc94
test cmake
sbSteveK Mar 27, 2023
430cf40
fixed connection start test
sbSteveK Mar 27, 2023
94c651e
checking on reconnect tests improved
sbSteveK Mar 27, 2023
e6d6dbe
fix windows race condition
sbSteveK Mar 27, 2023
a07692c
move condition variable notify to within lock
sbSteveK Mar 27, 2023
4022217
fix logic path on a protocol version missmatch
sbSteveK Mar 27, 2023
7565fad
missed a spot
sbSteveK Mar 27, 2023
c74fa07
more race conditions
sbSteveK Mar 27, 2023
a643ca7
clean up tests for clarity
sbSteveK Mar 28, 2023
6445d31
thread sanitizer fix
sbSteveK Mar 28, 2023
5967445
handle websocket pings in tests
sbSteveK Mar 28, 2023
bfbc50a
added appropriate check in between moving on in test
sbSteveK Mar 28, 2023
67c2477
ignore a second service ids message
sbSteveK Mar 28, 2023
a842d18
handling of connection id separated from message view
sbSteveK Mar 28, 2023
c0eaf23
simplified a test
sbSteveK Mar 28, 2023
a091101
forgot a wait for check in a test
sbSteveK Mar 28, 2023
aaf3aaf
added wrong kind of wait
sbSteveK Mar 28, 2023
12257d5
added additional stream closed checks
sbSteveK Mar 28, 2023
a8aedef
prevent double read of received byte buffer
sbSteveK Mar 29, 2023
36d0713
added pause for sends
sbSteveK Mar 29, 2023
dbce742
add mutex lock for incoming data buf
sbSteveK Mar 29, 2023
25cb7a8
code review changes
sbSteveK Mar 31, 2023
f4e3489
remove defaulting source mode to V3
sbSteveK Apr 3, 2023
6817cd3
remove comment
sbSteveK Apr 3, 2023
9197bcd
modified the active stream check to account for V1 streams
sbSteveK Apr 3, 2023
e621622
sending of V1 messages from Destination
sbSteveK Apr 3, 2023
a46b3c6
sending of V2 messages from destination
sbSteveK Apr 3, 2023
76805f3
Description of connection id manipulation reasoning
sbSteveK Apr 3, 2023
eacf708
source device connection id compatibility
sbSteveK Apr 3, 2023
79ad39e
move aws_secure_tunnel_message_type_to_c_string to secure_tunnel.h
sbSteveK Apr 5, 2023
5a886e4
remove mutex lock on received data buffer
sbSteveK Apr 6, 2023
42a49f0
moved connection_reset api to private header. Might move to public if…
sbSteveK Apr 6, 2023
3a6ddce
add delay to mock websocket sends
sbSteveK Apr 6, 2023
9b2ce72
moved connection related members out of options storage into its own …
sbSteveK Apr 6, 2023
b7487ec
init tls_ctx_opt before goto error calls
sbSteveK Apr 11, 2023
cabba56
removed header logging. added jitter (#87)
sbSteveK Apr 18, 2023
ab76927
Secure tunnel v3 tls (#88)
sbSteveK Apr 18, 2023
1ec416e
Secure tunnel v3 tls (#88)
sbSteveK Apr 18, 2023
21428bc
github ci.yml updates
sbSteveK Apr 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'main'

env:
BUILDER_VERSION: v0.9.26
BUILDER_VERSION: v0.9.40
BUILDER_SOURCE: releases
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
PACKAGE_NAME: aws-c-iot
Expand Down Expand Up @@ -105,12 +105,12 @@ jobs:
runs-on: windows-2019 # windows-2019 is last env with Visual Studio 2015 (v14.0) toolset
strategy:
matrix:
arch: [Win32, x64]
arch: [x86, x64]
steps:
- name: Build ${{ env.PACKAGE_NAME }} + consumers
run: |
python -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder.pyz')"
python builder.pyz build -p ${{ env.PACKAGE_NAME }} --cmake-extra=-Tv140 --cmake-extra=-A${{ matrix.arch }}
python builder.pyz build -p ${{ env.PACKAGE_NAME }} --target windows-${{ matrix.arch }} --compiler msvc-14

windows-shared-libs:
runs-on: windows-2022 # latest
Expand All @@ -128,8 +128,8 @@ jobs:
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
chmod a+x builder
./builder build -p ${{ env.PACKAGE_NAME }}


# Test downstream repos.
# This should not be required because we can run into a chicken and egg problem if there is a change that needs some fix in a downstream repo.
downstream:
Expand Down
5 changes: 4 additions & 1 deletion include/aws/iotdevice/iotdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ enum aws_iotdevice_error {
AWS_ERROR_IOTDEVICE_DEFENDER_PUBLISH_FAILURE,
AWS_ERROR_IOTDEVICE_DEFENDER_UNKNOWN_TASK_STATUS,

AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_STREAM_ID,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_CONNECTION_ID,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INVALID_SERVICE_ID,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_INCORRECT_MODE,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_BAD_SERVICE_ID,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DATA_OPTIONS_VALIDATION,
Expand All @@ -34,6 +36,7 @@ enum aws_iotdevice_error {
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_OPERATION_FAILED_DUE_TO_OFFLINE_QUEUE_POLICY,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_UNEXPECTED_HANGUP,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_USER_REQUESTED_STOP,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_PROTOCOL_VERSION_MISSMATCH,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_TERMINATED,
AWS_ERROR_IOTDEVICE_SECURE_TUNNELING_DECODE_FAILURE,

Expand Down
53 changes: 47 additions & 6 deletions include/aws/iotdevice/private/secure_tunneling_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <aws/common/task_scheduler.h>
#include <aws/http/proxy.h>
#include <aws/http/websocket.h>
#include <aws/io/host_resolver.h>
#include <aws/io/socket.h>
#include <aws/io/tls_channel_handler.h>

Expand Down Expand Up @@ -99,10 +100,21 @@ struct data_tunnel_pair {
struct aws_allocator *allocator;
struct aws_byte_buf buf;
struct aws_byte_cursor cur;
enum aws_secure_tunnel_message_type type;
const struct aws_secure_tunnel *secure_tunnel;
bool length_prefix_written;
};

struct aws_secure_tunnel_message_storage {
struct aws_allocator *allocator;
struct aws_secure_tunnel_message_view storage_view;

struct aws_byte_cursor service_id;
struct aws_byte_cursor payload;

struct aws_byte_buf storage;
};

/*
* Secure tunnel configuration
*/
Expand All @@ -120,28 +132,42 @@ struct aws_secure_tunnel_options_storage {

struct aws_string *endpoint_host;

/* Stream related info */
int32_t stream_id;

struct aws_hash_table service_ids;

/* Callbacks */
aws_secure_tunnel_message_received_fn *on_message_received;
aws_secure_tunneling_on_connection_complete_fn *on_connection_complete;
aws_secure_tunneling_on_connection_shutdown_fn *on_connection_shutdown;
aws_secure_tunneling_on_stream_start_fn *on_stream_start;
aws_secure_tunneling_on_stream_reset_fn *on_stream_reset;
aws_secure_tunneling_on_connection_start_fn *on_connection_start;
aws_secure_tunneling_on_connection_reset_fn *on_connection_reset;
aws_secure_tunneling_on_session_reset_fn *on_session_reset;
aws_secure_tunneling_on_stopped_fn *on_stopped;
aws_secure_tunneling_on_send_message_complete_fn *on_send_message_complete;

aws_secure_tunneling_on_send_data_complete_fn *on_send_data_complete;
aws_secure_tunneling_on_termination_complete_fn *on_termination_complete;
void *secure_tunnel_on_termination_user_data;

void *user_data;
enum aws_secure_tunneling_local_proxy_mode local_proxy_mode;
};

struct aws_secure_tunnel_connections {
struct aws_allocator *allocator;

uint8_t protocol_version;

/* Used for streams not using multiplexing (service ids) */
int32_t stream_id;
struct aws_hash_table connection_ids;

/* Table containing streams using multiplexing (service ids) */
struct aws_hash_table service_ids;

/* Message used for initializing a stream upon a reconnect due to a protocol version missmatch */
struct aws_secure_tunnel_message_storage *restore_stream_message_view;
struct aws_secure_tunnel_message_storage restore_stream_message;
};

struct aws_secure_tunnel_vtable {
/* aws_high_res_clock_get_ticks */
uint64_t (*get_current_time_fn)(void);
Expand Down Expand Up @@ -175,9 +201,16 @@ struct aws_secure_tunnel {
*/
struct aws_secure_tunnel_options_storage *config;

/*
* Stores connection related information
*/
struct aws_secure_tunnel_connections *connections;

struct aws_tls_ctx *tls_ctx;
struct aws_tls_connection_options tls_con_opt;

struct aws_host_resolution_config host_resolution_config;

/*
* The recurrent task that runs all secure tunnel logic outside of external event callbacks. Bound to the secure
* tunnel's event loop.
Expand Down Expand Up @@ -267,6 +300,14 @@ AWS_IOTDEVICE_API void aws_secure_tunnel_set_vtable(
*/
AWS_IOTDEVICE_API const struct aws_secure_tunnel_vtable *aws_secure_tunnel_get_default_vtable(void);

/*
* For testing purposes. This message type should only be sent due to internal logic.
*/
AWS_IOTDEVICE_API
int aws_secure_tunnel_connection_reset(
struct aws_secure_tunnel *secure_tunnel,
const struct aws_secure_tunnel_message_view *message_options);

AWS_EXTERN_C_END

#endif /* AWS_IOTDEVICE_SECURE_TUNNELING_IMPL_H */
37 changes: 26 additions & 11 deletions include/aws/iotdevice/private/secure_tunneling_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,22 @@ enum aws_secure_tunnel_operation_type {
AWS_STOT_PING,
AWS_STOT_MESSAGE,
AWS_STOT_STREAM_RESET,
AWS_STOT_STREAM_START
AWS_STOT_STREAM_START,
AWS_STOT_CONNECTION_START,
AWS_STOT_CONNECTION_RESET,
};

struct aws_service_id_element {
struct aws_allocator *allocator;
struct aws_byte_cursor service_id_cur;
struct aws_string *service_id_string;
int32_t stream_id;
struct aws_hash_table connection_ids;
};

struct aws_secure_tunnel_message_storage {
struct aws_connection_id_element {
struct aws_allocator *allocator;
struct aws_secure_tunnel_message_view storage_view;

bool ignorable;
int32_t stream_id;
struct aws_byte_cursor service_id;
struct aws_byte_cursor payload;

struct aws_byte_buf storage;
uint32_t connection_id;
};

/* Basic vtable for all secure tunnel operations. Implementations are per-message type */
Expand All @@ -57,10 +53,15 @@ struct aws_secure_tunnel_operation_vtable {
struct aws_secure_tunnel_operation *operation,
struct aws_secure_tunnel *secure_tunnel);

/* Set the stream id of outgoing st_msg to +1 of the currently set stream id */
/* Set the stream id of outgoing STREAM START message to +1 of the currently set stream id */
int (*aws_secure_tunnel_operation_set_next_stream_id_fn)(
struct aws_secure_tunnel_operation *operation,
struct aws_secure_tunnel *secure_tunnel);

/* Set the connection id of outbound CONNECTION START as active for the Source device */
int (*aws_secure_tunnel_operation_set_connection_start_id)(
struct aws_secure_tunnel_operation *operation,
struct aws_secure_tunnel *secure_tunnel);
};

/**
Expand Down Expand Up @@ -172,6 +173,12 @@ struct aws_secure_tunnel_options_storage *aws_secure_tunnel_options_storage_new(
struct aws_allocator *allocator,
const struct aws_secure_tunnel_options *options);

AWS_IOTDEVICE_API
void aws_secure_tunnel_connections_destroy(struct aws_secure_tunnel_connections *storage);

AWS_IOTDEVICE_API
struct aws_secure_tunnel_connections *aws_secure_tunnel_connections_new(struct aws_allocator *allocator);

AWS_IOTDEVICE_API
void aws_secure_tunnel_options_storage_log(
const struct aws_secure_tunnel_options_storage *options_storage,
Expand All @@ -197,6 +204,14 @@ struct aws_service_id_element *aws_service_id_element_new(
const struct aws_byte_cursor *service_id,
int32_t stream_id);

AWS_IOTDEVICE_API
void aws_connection_id_destroy(void *data);

AWS_IOTDEVICE_API
struct aws_connection_id_element *aws_connection_id_element_new(
struct aws_allocator *allocator,
uint32_t connection_id);

AWS_EXTERN_C_END

#endif /* AWS_IOTDEVICE_SECURE_TUNNELING_OPERATION_H */
5 changes: 1 addition & 4 deletions include/aws/iotdevice/private/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define AWS_IOT_ST_MAXIMUM_1_BYTE_VARINT_VALUE 128
#define AWS_IOT_ST_MAXIMUM_2_BYTE_VARINT_VALUE 16384
#define AWS_IOT_ST_MAXIMUM_3_BYTE_VARINT_VALUE 2097152
#define AWS_IOT_ST_MAX_MESSAGE_SIZE (64 * 1024)
#define AWS_IOT_ST_MAX_PAYLOAD_SIZE (63 * 1024)
sbSteveK marked this conversation as resolved.
Show resolved Hide resolved

enum aws_secure_tunnel_field_number {
AWS_SECURE_TUNNEL_FN_TYPE = 1,
Expand Down Expand Up @@ -56,9 +56,6 @@ int aws_secure_tunnel_deserialize_message_from_cursor(
struct aws_byte_cursor *cursor,
aws_secure_tunnel_on_message_received_fn *on_message_received);

AWS_IOTDEVICE_API
const char *aws_secure_tunnel_message_type_to_c_string(enum aws_secure_tunnel_message_type message_type);

AWS_EXTERN_C_END

#endif
Loading