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

Basic authentication using HMAC for federates joining a federation #105

Merged
merged 46 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
544adc7
Add OpenSSL dependency in CMake.
hokeun Aug 2, 2022
fca0264
Add new message types for HMAC-authenticated in joining federation.
hokeun Aug 2, 2022
73f6b6a
Add code for sending RTI hello.
hokeun Aug 2, 2022
811b457
Add code for performing HMAC authentication on federate's side.
hokeun Aug 2, 2022
f83f58a
Merge branch 'main' of https://github.com/lf-lang/reactor-c into secu…
hokeun Aug 26, 2022
af769f3
Fixed typos
Sep 26, 2022
a17c5d3
Added HMAC check protocol demo
Jakio815 Sep 26, 2022
5d88b51
Fixed some typos
Jakio815 Sep 27, 2022
54c9c25
Changed protocol to include msg type & federate ID when creating HMAC…
Jakio815 Sep 27, 2022
ae31ef0
HMAC authentication finished. For build, CCmakeGenerator.java must be…
Jakio815 Sep 27, 2022
b424053
Add auth options in RTI_instance as bool type is_auth.
Jakio815 Oct 5, 2022
9cdc725
Fixed bug on RTI -a option
Jakio815 Oct 5, 2022
33bb32c
HMAC authorization only happens when FEDERATED_AUTH state
Jakio815 Oct 6, 2022
4da4fd8
RTI now optionally includes openssl with cmake option -DAUTH=ON
Jakio815 Oct 6, 2022
8f9792a
Merge branch 'main' into security
Jakio815 Oct 6, 2022
f866768
Update comments and make definition for federate authentication more …
hokeun Oct 7, 2022
ce27076
Update complier definition and comments.
hokeun Oct 7, 2022
f74bb5e
Make variable and parameter names more descriptive, separate #include…
hokeun Oct 7, 2022
6a23b50
Make RTI more robust so that it does not die when authentication fails.
hokeun Oct 8, 2022
968caca
Add comments for authentication messages.
hokeun Oct 8, 2022
ac904a7
Use more standard way to get required buffer length and encoding fed …
hokeun Oct 8, 2022
e4853d7
Add error handling for HMAC function.
hokeun Oct 8, 2022
2a3e39f
Remove unused message type.
hokeun Oct 8, 2022
c68b2fd
Move ifdef dependencies to top & Add comments.
Jakio815 Oct 30, 2022
d168550
Minory fixes, not finished
Jakio815 Oct 30, 2022
129e866
Merge branch 'main' of github.com:lf-lang/reactor-c into security
Jakio815 Oct 30, 2022
9354daa
Added comments
Jakio815 Oct 31, 2022
4f17f20
Added SHA256_HMAC_LENGTH & only print error not exit when wrong messa…
Jakio815 Oct 31, 2022
ea507c2
Merge branch 'main' of github.com:lf-lang/reactor-c into security
Jakio815 Nov 10, 2022
d6fc5ba
Updated lf-lang/lingua-franca version
Jakio815 Nov 14, 2022
4cea135
Update lingua-franca-ref.txt
Nov 16, 2022
8ed5649
Update lingua-franca-ref.txt
Nov 16, 2022
83491bf
Update linguq-franca-ref.txt
Nov 16, 2022
4ece789
Temporarily change ci test
Nov 16, 2022
e3352f0
Temporarily change ci test branch to auth
Nov 16, 2022
75d0f13
Update lingua-frnaca-ref.txt version
Nov 17, 2022
5dfbb52
Update lingua-franca-ref.txt
Nov 18, 2022
58a0c4c
Update lingua-franca.txt
Nov 18, 2022
5b80211
Update lingua-franca-ref.txt
Nov 19, 2022
268260f
Update lingua-franca-ref.txt
Nov 19, 2022
491cccc
Update lingua-franca-ref.txt
Nov 19, 2022
c10629b
More specific data types for Cpp compilers
Nov 21, 2022
78911bc
Restore ci test
Nov 21, 2022
0cfbcd7
Restore ci test
Nov 21, 2022
3254e8c
Updated lingua-franca.txt
Nov 21, 2022
d06c6ff
Fixed from MSG_TYPE_RTI_HELLO to MSG_TYPE_RTI_NONCE
Dec 21, 2022
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
5 changes: 5 additions & 0 deletions core/federated/RTI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ target_compile_definitions(RTI PUBLIC NUMBER_OF_WORKERS)
find_package(Threads REQUIRED)
target_link_libraries(RTI Threads::Threads)

# Find OpenSSL and link to it
find_package(OpenSSL REQUIRED)
target_link_libraries(RTI OpenSSL::SSL)


install(
TARGETS RTI
DESTINATION bin
Expand Down
17 changes: 17 additions & 0 deletions core/federated/RTI/rti.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <strings.h> // Defines bzero().
#include <assert.h>
#include <sys/wait.h> // Defines wait() for process to change state.
#include <openssl/rand.h> // For secure random number generation.
#include "platform.h" // Platform-specific types and functions
#include "utils/util.c" // Defines print functions (e.g., lf_print).
#include "net_util.c" // Defines network functions.
Expand Down Expand Up @@ -1790,6 +1791,18 @@ int receive_udp_message_and_set_up_clock_sync(int socket_id, uint16_t fed_id) {
return 1;
}

/**
* Send RTI hello message for authenticated joining of federation.
*
* @param socket Socket for the incoming federate tryting to authenticate.
*/
void send_rti_hello(int socket) {
lhstrh marked this conversation as resolved.
Show resolved Hide resolved
size_t message_length = 1 + RTI_HELLO_LENGTH;
unsigned char buffer[message_length];
buffer[0] = MSG_TYPE_RTI_HELLO;
RAND_bytes(&(buffer[1]), RTI_HELLO_LENGTH);
ssize_t bytes_written = write_to_socket(socket, message_length, buffer);
}

/**
* Wait for one incoming connection request from each federate,
Expand Down Expand Up @@ -1818,6 +1831,10 @@ void connect_to_federates(int socket_descriptor) {
}
}

// Send RTI hello including a nounce.
// If authenticated hello is required.
send_rti_hello(socket_id);

// The first message from the federate should contain its ID and the federation ID.
int32_t fed_id = receive_and_check_fed_id_message(socket_id, (struct sockaddr_in*)&client_fd);
if (fed_id >= 0
Expand Down
25 changes: 25 additions & 0 deletions core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,24 @@ void connect_to_federate(uint16_t remote_federate_id) {
}
}

void perform_hmac_authentication(int rti_socket) {
lhstrh marked this conversation as resolved.
Show resolved Hide resolved
LF_PRINT_LOG("Connected to an RTI. Performing HAMC-based authentication.");
unsigned char buffer[1 + RTI_HELLO_LENGTH];
read_from_socket_errexit(rti_socket, 1 + RTI_HELLO_LENGTH, buffer, "Failed to read RTI hello.");

if (buffer[0] != MSG_TYPE_RTI_HELLO) {
lf_print_error_and_exit("Received unexpected response %u from the RTI (see net_common.h).",
buffer[0]);
}

char hex_buffer[RTI_HELLO_LENGTH * 2 + 1];
for (int i = 0; i < RTI_HELLO_LENGTH; i++) {
sprintf(&hex_buffer[i * 2], "%x", buffer[1 + i]);
}
hex_buffer[RTI_HELLO_LENGTH * 2] = '\0';
LF_PRINT_LOG("Nonce received from RTI : %s", hex_buffer);
}

/**
* Connect to the RTI at the specified host and port and return
* the socket descriptor for the connection. If this fails, the
Expand Down Expand Up @@ -896,6 +914,9 @@ void connect_to_rti(const char* hostname, int port) {
int result = -1;
int count_retries = 0;

// See https://github.com/lf-lang/lingua-franca/issues/1146 for more information.
bool use_hmac_authentication = true;

while (result < 0) {
// Create an IPv4 socket for TCP (not UDP) communication over IP (0).
_fed.socket_TCP_RTI = socket(AF_INET, SOCK_STREAM, 0);
Expand Down Expand Up @@ -964,6 +985,10 @@ void connect_to_rti(const char* hostname, int port) {
// Notify the RTI of the ID of this federate and its federation.
unsigned char buffer[4];

if (use_hmac_authentication) {
perform_hmac_authentication(_fed.socket_TCP_RTI);
}

LF_PRINT_LOG("Connected to an RTI. Sending federation ID for authentication.");

// Send the message type first.
Expand Down
11 changes: 11 additions & 0 deletions core/federated/net_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,17 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define MSG_TYPE_FED_IDS 1

/**
* Byte identifying a message from an RTI to a federate containing
* a 8-byte nonce.
* The next eight bytes are a 8-byte random nonce.
*/
#define MSG_TYPE_RTI_HELLO 100
#define RTI_HELLO_LENGTH 8

#define MSG_TYPE_AUTHENTICATED_FED_IDS 101


/**
* Byte identifying a timestamp message, which is 64 bits long.
* Each federate sends its starting physical time as a message of this
Expand Down