Merge branch 'master' into yamux

# Conflicts:
#	test/test_secio.h
This commit is contained in:
John Jones 2017-11-29 06:18:45 -05:00
commit 59f3a0664a
2 changed files with 75 additions and 55 deletions

View file

@ -1,6 +1,7 @@
MIT License MIT License
Copyright (c) 2017 BitShares Munich IVS Copyright (c) 2017 Agorise, IBC.
Contains works from BitShares Munich IVS
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View file

@ -33,7 +33,6 @@ void print_stretched_key(struct StretchedKey* key) {
int test_secio_handshake() { int test_secio_handshake() {
/*
libp2p_logger_add_class("secio"); libp2p_logger_add_class("secio");
int retVal = 0; int retVal = 0;
@ -50,7 +49,7 @@ int test_secio_handshake() {
unsigned char final_id[final_id_size]; unsigned char final_id[final_id_size];
struct PrivateKey* private_key = NULL; struct PrivateKey* private_key = NULL;
struct SessionContext* secure_session = NULL; struct SessionContext secure_session = {0};
struct Peerstore *peerstore = NULL; struct Peerstore *peerstore = NULL;
struct Libp2pPeer* local_peer = NULL; struct Libp2pPeer* local_peer = NULL;
@ -77,19 +76,14 @@ int test_secio_handshake() {
local_peer = libp2p_peer_new(); local_peer = libp2p_peer_new();
peerstore = libp2p_peerstore_new(local_peer); peerstore = libp2p_peerstore_new(local_peer);
secure_session = libp2p_session_context_new();
//secure_session.host = "www.jmjatlanta.com"; //secure_session.host = "www.jmjatlanta.com";
secure_session->host = "107.170.104.234"; secure_session.host = "10.211.55.2";
secure_session->port = 4001; secure_session.port = 4001;
secure_session->traffic_type = TCP; secure_session.traffic_type = TCP;
// connect to host // connect to host
secure_session->insecure_stream = libp2p_net_multistream_connect(secure_session->host, secure_session->port); secure_session.insecure_stream = libp2p_net_multistream_connect(secure_session.host, secure_session.port);
if (secure_session->insecure_stream == NULL) { secure_session.default_stream = secure_session.insecure_stream;
libp2p_logger_error("test_secio", "Unable to open multistream to server %s port %d.\n", secure_session->host, secure_session->port); if (*((int*)secure_session.insecure_stream->socket_descriptor) == -1) {
goto exit;
}
secure_session->default_stream = secure_session->insecure_stream;
if (*((int*)secure_session->insecure_stream->socket_descriptor) == -1) {
fprintf(stderr, "test_secio_handshake: Unable to get socket descriptor\n"); fprintf(stderr, "test_secio_handshake: Unable to get socket descriptor\n");
goto exit; goto exit;
} }
@ -99,19 +93,30 @@ int test_secio_handshake() {
libp2p_logger_error("test_secio", "Unable to do handshake\n"); libp2p_logger_error("test_secio", "Unable to do handshake\n");
if (secure_session->shared_key != NULL) { if (secure_session->shared_key != NULL) {
fprintf(stdout, "Shared key: "); fprintf(stdout, "Shared key: ");
for(int i = 0; i < secure_session->shared_key_size; i++) for(int i = 0; i < secure_session.shared_key_size; i++)
fprintf(stdout, "%d ", secure_session->shared_key[i]); fprintf(stdout, "%d ", secure_session.shared_key[i]);
fprintf(stdout, "\nLocal stretched key: "); fprintf(stdout, "\nLocal stretched key: ");
print_stretched_key(secure_session->local_stretched_key); print_stretched_key(secure_session.local_stretched_key);
fprintf(stdout, "\nRemote stretched key: "); fprintf(stdout, "\nRemote stretched key: ");
print_stretched_key(secure_session->remote_stretched_key); print_stretched_key(secure_session.remote_stretched_key);
fprintf(stdout, "\n"); fprintf(stdout, "\n");
} }
goto exit; goto exit;
} }
/*
fprintf(stdout, "Shared key: ");
for(int i = 0; i < secure_session.shared_key_size; i++)
fprintf(stdout, "%d ", secure_session.shared_key[i]);
fprintf(stdout, "\nLocal stretched key: ");
print_stretched_key(secure_session.local_stretched_key);
fprintf(stdout, "\nRemote stretched key: ");
print_stretched_key(secure_session.remote_stretched_key);
fprintf(stdout, "\n");
*/
// now attempt to do something with it... try to negotiate a multistream // now attempt to do something with it... try to negotiate a multistream
if (libp2p_net_multistream_negotiate(secure_session) == 0) { if (libp2p_net_multistream_negotiate(&secure_session) == 0) {
fprintf(stdout, "Unable to negotiate multistream\n"); fprintf(stdout, "Unable to negotiate multistream\n");
goto exit; goto exit;
} }
@ -133,41 +138,40 @@ int test_secio_handshake() {
goto exit; goto exit;
} }
fprintf(stdout, "Results of ls (%d bytes long):\n%s\n", (int)results->data_size, results->data); fprintf(stdout, "Results of ls: %.*s", (int)results_size, results);
libp2p_stream_message_free(results); free(results);
results = NULL;
// try to yamux
char* yamux_string = "/yamux/1.0.0\n";
outgoing.data = (uint8_t*)yamux_string;
outgoing.data_size = strlen(yamux_string);
if (!libp2p_net_multistream_write(secure_session, &outgoing)) {
libp2p_logger_error("test_secio", "Unable to send yamux protocol request\n");
goto exit;
}
if (!libp2p_net_multistream_read(secure_session, &results, 30)) {
libp2p_logger_error("test_secio", "Unable to read reply to yamux request.\n");
goto exit;
}
fprintf(stdout, "Results of yamux request: %s\n", results->data);
libp2p_stream_message_free(results);
results = NULL; results = NULL;
retVal = 1; retVal = 1;
exit: exit:
if (peerstore != NULL) if (secure_session.insecure_stream != NULL)
libp2p_peerstore_free(peerstore); libp2p_net_multistream_stream_free(secure_session.insecure_stream);
if (secure_session.local_stretched_key != NULL)
libp2p_crypto_ephemeral_stretched_key_free(secure_session.local_stretched_key);
if (secure_session.remote_stretched_key != NULL)
libp2p_crypto_ephemeral_stretched_key_free(secure_session.remote_stretched_key);
if (secure_session.ephemeral_private_key != NULL)
libp2p_crypto_ephemeral_key_free(secure_session.ephemeral_private_key);
if (secure_session.remote_ephemeral_public_key != NULL)
free(secure_session.remote_ephemeral_public_key);
if (secure_session.chosen_cipher != NULL)
free(secure_session.chosen_cipher);
if (secure_session.chosen_curve != NULL)
free(secure_session.chosen_curve);
if (secure_session.chosen_hash != NULL)
free(secure_session.chosen_hash);
if (secure_session.shared_key != NULL)
free(secure_session.shared_key);
if (private_key != NULL) if (private_key != NULL)
libp2p_crypto_private_key_free(private_key); libp2p_crypto_private_key_free(private_key);
if (decode_base64 != NULL) if (decode_base64 != NULL)
free(decode_base64); free(decode_base64);
if (rsa_private_key != NULL) if (rsa_private_key != NULL)
libp2p_crypto_rsa_rsa_private_key_free(rsa_private_key); libp2p_crypto_rsa_rsa_private_key_free(rsa_private_key);
if (peerstore != NULL)
libp2p_peerstore_free(peerstore);
return retVal; return retVal;
*/
return 0;
} }
int libp2p_secio_encrypt(const struct SessionContext* session, const unsigned char* incoming, size_t incoming_size, unsigned char** outgoing, size_t* outgoing_size); int libp2p_secio_encrypt(const struct SessionContext* session, const unsigned char* incoming, size_t incoming_size, unsigned char** outgoing, size_t* outgoing_size);
@ -326,7 +330,6 @@ int test_secio_encrypt_like_go() {
*/ */
int test_secio_handshake_go() { int test_secio_handshake_go() {
/*
libp2p_logger_add_class("secio"); libp2p_logger_add_class("secio");
int retVal = 0; int retVal = 0;
@ -383,8 +386,17 @@ int test_secio_handshake_go() {
} }
// attempt to write the protocol, and see what comes back // attempt to write the protocol, and see what comes back
if (!libp2p_secio_initiate_handshake(secure_session, rsa_private_key, peerstore)) { char* protocol = "/secio/1.0.0\n";
libp2p_logger_error("test_secio", "Unable to do handshake.\n"); int protocol_size = strlen(protocol);
secure_session->insecure_stream->write(secure_session, (unsigned char*)protocol, protocol_size);
unsigned char* buffer = NULL;
size_t bytes_read = 0;
int timeout = 30;
secure_session->insecure_stream->read(secure_session, &buffer, &bytes_read, timeout);
if (!libp2p_secio_handshake(secure_session, rsa_private_key, peerstore)) {
fprintf(stderr, "test_secio_handshake: Unable to do handshake\n");
if (secure_session->shared_key != NULL) { if (secure_session->shared_key != NULL) {
fprintf(stdout, "Shared key: "); fprintf(stdout, "Shared key: ");
for(int i = 0; i < secure_session->shared_key_size; i++) for(int i = 0; i < secure_session->shared_key_size; i++)
@ -398,6 +410,17 @@ int test_secio_handshake_go() {
goto exit; goto exit;
} }
/*
fprintf(stdout, "Shared key: ");
for(int i = 0; i < secure_session.shared_key_size; i++)
fprintf(stdout, "%d ", secure_session.shared_key[i]);
fprintf(stdout, "\nLocal stretched key: ");
print_stretched_key(secure_session.local_stretched_key);
fprintf(stdout, "\nRemote stretched key: ");
print_stretched_key(secure_session.remote_stretched_key);
fprintf(stdout, "\n");
*/
// now attempt to do something with it... try to negotiate a multistream // now attempt to do something with it... try to negotiate a multistream
if (libp2p_net_multistream_negotiate(secure_session) == 0) { if (libp2p_net_multistream_negotiate(secure_session) == 0) {
fprintf(stdout, "Unable to negotiate multistream\n"); fprintf(stdout, "Unable to negotiate multistream\n");
@ -405,25 +428,23 @@ int test_secio_handshake_go() {
} }
// now attempt an "ls" // now attempt an "ls"
struct StreamMessage outgoing; if (libp2p_net_multistream_write(secure_session, (unsigned char*)"ls\n", 3) == 0) {
outgoing.data = (uint8_t*)"ls\n";
outgoing.data_size = 3;
if (libp2p_net_multistream_write(secure_session, &outgoing) == 0) {
fprintf(stdout, "Unable to send ls to multistream\n"); fprintf(stdout, "Unable to send ls to multistream\n");
goto exit; goto exit;
} }
// retrieve the response // retrieve the response
struct StreamMessage* results; unsigned char* results;
if (libp2p_net_multistream_read(secure_session, &results, 30) == 0) { size_t results_size;
if (libp2p_net_multistream_read(secure_session, &results, &results_size, 30) == 0) {
fprintf(stdout, "Unable to read ls results from multistream\n"); fprintf(stdout, "Unable to read ls results from multistream\n");
free(results); free(results);
goto exit; goto exit;
} }
fprintf(stdout, "Results of ls: %.*s", (int)results->data_size, results->data); fprintf(stdout, "Results of ls: %.*s", (int)results_size, results);
libp2p_stream_message_free(results); free(results);
results = NULL; results = NULL;
retVal = 1; retVal = 1;
@ -437,6 +458,4 @@ int test_secio_handshake_go() {
if (peerstore != NULL) if (peerstore != NULL)
libp2p_peerstore_free(peerstore); libp2p_peerstore_free(peerstore);
return retVal; return retVal;
*/
return 0;
} }