Skip to content
Snippets Groups Projects
Commit 2b3c02d6 authored by Deucе's avatar Deucе :ok_hand_tone4: Committed by Rob Swindell
Browse files

Update cryptlib to 3.4.7

parent b33a40e7
No related branches found
No related tags found
1 merge request!388Update cryptlib to 3.4.7
...@@ -27,9 +27,11 @@ CRYPT_SESSION ssh_session; ...@@ -27,9 +27,11 @@ CRYPT_SESSION ssh_session;
int ssh_channel; int ssh_channel;
int ssh_active = true; int ssh_active = true;
pthread_mutex_t ssh_mutex; pthread_mutex_t ssh_mutex;
pthread_mutex_t ssh_tx_mutex;
int sftp_channel = -1; int sftp_channel = -1;
bool sftp_active; bool sftp_active;
sftpc_state_t sftp_state; sftpc_state_t sftp_state;
bool pubkey_thread_running;
static void static void
FlushData(CRYPT_SESSION sess) FlushData(CRYPT_SESSION sess)
...@@ -60,17 +62,17 @@ cryptlib_error_message(int status, const char *msg) ...@@ -60,17 +62,17 @@ cryptlib_error_message(int status, const char *msg)
} }
static void static void
close_sftp_channel(void) close_sftp_channel(int chan)
{ {
sftpc_state_t oldstate; sftpc_state_t oldstate;
pthread_mutex_lock(&ssh_mutex); pthread_mutex_lock(&ssh_mutex);
if (sftp_channel != -1) { if (chan != -1) {
FlushData(ssh_session); FlushData(ssh_session);
if (cryptStatusOK(cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL, sftp_channel))) { if (cryptStatusOK(cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL, chan))) {
cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, 0); cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, 0);
} }
sftp_channel = -1;
} }
sftp_channel = -1;
oldstate = sftp_state; oldstate = sftp_state;
sftp_state = NULL; sftp_state = NULL;
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
...@@ -106,10 +108,10 @@ check_channel_open(int *chan) ...@@ -106,10 +108,10 @@ check_channel_open(int *chan)
if (cryptStatusError(status)) { if (cryptStatusError(status)) {
open = 0; open = 0;
} }
}
if (!open) { if (!open) {
cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, 0); cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, 0);
} }
}
return open; return open;
} }
...@@ -246,7 +248,7 @@ ssh_input_thread(void *args) ...@@ -246,7 +248,7 @@ ssh_input_thread(void *args)
*/ */
if (rd > 0 && !sftpc_recv(sftp_state, conn_api.rd_buf, rd)) { if (rd > 0 && !sftpc_recv(sftp_state, conn_api.rd_buf, rd)) {
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
close_sftp_channel(); close_sftp_channel(sftp_channel);
pthread_mutex_lock(&ssh_mutex); pthread_mutex_lock(&ssh_mutex);
FlushData(ssh_session); FlushData(ssh_session);
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
...@@ -295,6 +297,7 @@ ssh_output_thread(void *args) ...@@ -295,6 +297,7 @@ ssh_output_thread(void *args)
wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size); wr = conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
pthread_mutex_unlock(&(conn_outbuf.mutex)); pthread_mutex_unlock(&(conn_outbuf.mutex));
sent = 0; sent = 0;
pthread_mutex_lock(&ssh_tx_mutex);
while (ssh_active && sent < wr) { while (ssh_active && sent < wr) {
ret = 0; ret = 0;
pthread_mutex_lock(&ssh_mutex); pthread_mutex_lock(&ssh_mutex);
...@@ -320,6 +323,7 @@ ssh_output_thread(void *args) ...@@ -320,6 +323,7 @@ ssh_output_thread(void *args)
} }
sent += ret; sent += ret;
} }
pthread_mutex_unlock(&ssh_tx_mutex);
} }
else { else {
pthread_mutex_unlock(&(conn_outbuf.mutex)); pthread_mutex_unlock(&(conn_outbuf.mutex));
...@@ -328,7 +332,6 @@ ssh_output_thread(void *args) ...@@ -328,7 +332,6 @@ ssh_output_thread(void *args)
conn_api.output_thread_running = 2; conn_api.output_thread_running = 2;
} }
#if NOTYET
static bool static bool
sftp_send(uint8_t *buf, size_t sz, void *cb_data) sftp_send(uint8_t *buf, size_t sz, void *cb_data)
{ {
...@@ -337,6 +340,7 @@ sftp_send(uint8_t *buf, size_t sz, void *cb_data) ...@@ -337,6 +340,7 @@ sftp_send(uint8_t *buf, size_t sz, void *cb_data)
if (sz == 0) if (sz == 0)
return true; return true;
pthread_mutex_lock(&ssh_tx_mutex);
while (ssh_active && sent < sz) { while (ssh_active && sent < sz) {
int status; int status;
int ret = 0; int ret = 0;
...@@ -359,6 +363,7 @@ sftp_send(uint8_t *buf, size_t sz, void *cb_data) ...@@ -359,6 +363,7 @@ sftp_send(uint8_t *buf, size_t sz, void *cb_data)
} }
sent += ret; sent += ret;
} }
pthread_mutex_unlock(&ssh_tx_mutex);
return sent == sz; return sent == sz;
} }
...@@ -465,14 +470,28 @@ add_public_key(void *vpriv) ...@@ -465,14 +470,28 @@ add_public_key(void *vpriv)
{ {
int status; int status;
int active; int active;
int new_sftp_channel = -1;
char *priv = vpriv; char *priv = vpriv;
/* // Wait for at most five seconds for channel to be fully active
* TODO: We need to wait until the session is established. active = 0;
* Best way to do this is a channel property that indicates for (unsigned sleep_count = 0; sleep_count < 500 && conn_api.terminate == 0; sleep_count++) {
* what type of channel it is. pthread_mutex_lock(&ssh_mutex);
*/ if (ssh_channel != -1) {
SLEEP(1000); status = cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL, ssh_channel);
if (cryptStatusOK(status))
status = cl.GetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, &active);
}
pthread_mutex_unlock(&ssh_mutex);
if (cryptStatusOK(status) && active)
break;
SLEEP(10);
};
if (!active) {
pubkey_thread_running = false;
return;
}
pthread_mutex_lock(&ssh_tx_mutex);
pthread_mutex_lock(&ssh_mutex); pthread_mutex_lock(&ssh_mutex);
FlushData(ssh_session); FlushData(ssh_session);
status = cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL, CRYPT_UNUSED); status = cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL, CRYPT_UNUSED);
...@@ -490,7 +509,7 @@ add_public_key(void *vpriv) ...@@ -490,7 +509,7 @@ add_public_key(void *vpriv)
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
cryptlib_error_message(status, "setting subsystem"); cryptlib_error_message(status, "setting subsystem");
} else { } else {
status = cl.GetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL, &sftp_channel); status = cl.GetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL, &new_sftp_channel);
if (cryptStatusError(status)) { if (cryptStatusError(status)) {
sftp_channel = -1; sftp_channel = -1;
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
...@@ -499,27 +518,73 @@ add_public_key(void *vpriv) ...@@ -499,27 +518,73 @@ add_public_key(void *vpriv)
} }
} }
} }
if (sftp_channel != -1) { if (new_sftp_channel != -1) {
status = cl.GetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_OPEN, &active); status = cl.GetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_OPEN, &active);
if (cryptStatusError(status) || !active) { if (cryptStatusError(status) || !active) {
cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, 0); cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, 0);
sftp_channel = -1;
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
pthread_mutex_unlock(&ssh_tx_mutex);
free(priv); free(priv);
pubkey_thread_running = false;
return; return;
} }
status = cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, 1); status = cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, 1);
if (cryptStatusError(status)) { if (cryptStatusError(status) && status != CRYPT_ENVELOPE_RESOURCE) {
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
close_sftp_channel(); pthread_mutex_unlock(&ssh_tx_mutex);
close_sftp_channel(new_sftp_channel);
free(priv); free(priv);
pubkey_thread_running = false;
return; return;
} }
pthread_mutex_unlock(&ssh_mutex);
pthread_mutex_unlock(&ssh_tx_mutex);
active = 0;
for (unsigned sleep_count = 0; sleep_count < 500 && conn_api.terminate == 0; sleep_count++) {
pthread_mutex_lock(&ssh_mutex);
status = cl.SetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL, new_sftp_channel);
if (cryptStatusOK(status))
status = cl.GetAttribute(ssh_session, CRYPT_SESSINFO_SSH_CHANNEL_ACTIVE, &active);
pthread_mutex_unlock(&ssh_mutex);
if (cryptStatusOK(status) && active)
break;
SLEEP(10);
}
if (!active) {
close_sftp_channel(sftp_channel);
free(priv);
pubkey_thread_running = false;
return;
}
/*
* Old version of Synchronet will accept the channel, then
* immediately close it. If we then send data on the channel,
* it will get mixed in with the first channels data because
* it doesn't have the channel patches.
*
* To avoid that, we'll sleep for a second to allow
* the remote to close the channel if it wants to.
*/
for (unsigned sleep_count = 0; sleep_count < 100 && conn_api.terminate == 0; sleep_count++) {
SLEEP(10);
}
pthread_mutex_lock(&ssh_tx_mutex);
pthread_mutex_lock(&ssh_mutex);
if (conn_api.terminate || !check_channel_open(&new_sftp_channel)) {
pthread_mutex_unlock(&ssh_tx_mutex);
pthread_mutex_unlock(&ssh_mutex);
free(priv);
pubkey_thread_running = false;
return;
}
sftp_channel = new_sftp_channel;
sftp_state = sftpc_begin(sftp_send, NULL); sftp_state = sftpc_begin(sftp_send, NULL);
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
pthread_mutex_unlock(&ssh_tx_mutex);
if (sftp_state == NULL) { if (sftp_state == NULL) {
close_sftp_channel(); close_sftp_channel(sftp_channel);
free(priv); free(priv);
pubkey_thread_running = false;
return; return;
} }
if (sftpc_init(sftp_state)) { if (sftpc_init(sftp_state)) {
...@@ -544,26 +609,15 @@ add_public_key(void *vpriv) ...@@ -544,26 +609,15 @@ add_public_key(void *vpriv)
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
sftpc_finish(oldstate); sftpc_finish(oldstate);
} }
close_sftp_channel(); close_sftp_channel(sftp_channel);
} }
else { else {
pthread_mutex_unlock(&ssh_mutex); pthread_mutex_unlock(&ssh_mutex);
} }
free(priv); free(priv);
} pubkey_thread_running = false;
#else
static void
add_public_key(void *vpriv)
{
} }
static char *
get_public_key(CRYPT_CONTEXT ctx)
{
return NULL;
}
#endif
static void static void
error_popup(struct bbslist *bbs, const char *blurb, int status) error_popup(struct bbslist *bbs, const char *blurb, int status)
{ {
...@@ -597,6 +651,7 @@ ssh_connect(struct bbslist *bbs) ...@@ -597,6 +651,7 @@ ssh_connect(struct bbslist *bbs)
if (!bbs->hidepopups) if (!bbs->hidepopups)
init_uifc(true, true); init_uifc(true, true);
pthread_mutex_init(&ssh_mutex, NULL); pthread_mutex_init(&ssh_mutex, NULL);
pthread_mutex_init(&ssh_tx_mutex, NULL);
if (!crypt_loaded) { if (!crypt_loaded) {
if (!bbs->hidepopups) { if (!bbs->hidepopups) {
...@@ -913,7 +968,10 @@ ssh_connect(struct bbslist *bbs) ...@@ -913,7 +968,10 @@ ssh_connect(struct bbslist *bbs)
_beginthread(ssh_output_thread, 0, NULL); _beginthread(ssh_output_thread, 0, NULL);
_beginthread(ssh_input_thread, 0, NULL); _beginthread(ssh_input_thread, 0, NULL);
if (bbs->sftp_public_key) {
pubkey_thread_running = true;
_beginthread(add_public_key, 0, pubkey); _beginthread(add_public_key, 0, pubkey);
}
if (!bbs->hidepopups) if (!bbs->hidepopups)
uifc.pop(NULL); // TODO: Why is this called twice? uifc.pop(NULL); // TODO: Why is this called twice?
...@@ -927,10 +985,10 @@ ssh_close(void) ...@@ -927,10 +985,10 @@ ssh_close(void)
char garbage[1024]; char garbage[1024];
conn_api.terminate = 1; conn_api.terminate = 1;
close_sftp_channel(); close_sftp_channel(sftp_channel);
close_ssh_channel(); close_ssh_channel();
ssh_active = false; ssh_active = false;
while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1) { while (conn_api.input_thread_running == 1 || conn_api.output_thread_running == 1 || pubkey_thread_running) {
conn_recv_upto(garbage, sizeof(garbage), 0); conn_recv_upto(garbage, sizeof(garbage), 0);
SLEEP(1); SLEEP(1);
} }
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#endif #endif
#define mswait(x) delay(x) #define mswait(x) delay(x)
#elif defined(_WIN32) #elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <share.h> #include <share.h>
#include <windows.h> #include <windows.h>
#define mswait(x) Sleep(x) #define mswait(x) Sleep(x)
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#if defined(_WIN32) #if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h> /* WINAPI, etc */ #include <windows.h> /* WINAPI, etc */
#include <io.h> /* _findfirst */ #include <io.h> /* _findfirst */
......
...@@ -12,7 +12,9 @@ ...@@ -12,7 +12,9 @@
#define xp_dlsym(handle, name) dlsym(handle, #name) #define xp_dlsym(handle, name) dlsym(handle, #name)
#define xp_dlclose(handle) dlclose(handle) #define xp_dlclose(handle) dlclose(handle)
#elif defined(_WIN32) #elif defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#undef WIN32_LEAN_AND_MEAN
typedef HMODULE dll_handle; typedef HMODULE dll_handle;
DLLEXPORT dll_handle xp_dlopen(const char **name, int mode, int major); DLLEXPORT dll_handle xp_dlopen(const char **name, int mode, int major);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "xp_dl.h" #include "xp_dl.h"
#if defined(_WIN32) #if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#include <mmsystem.h> #include <mmsystem.h>
#elif defined(__unix__) #elif defined(__unix__)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment