summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorOliver Stöneberg <oliverst@online.de>2011-04-26 16:06:46 -0700
committerAndreas Schneider <asn@cryptomilk.org>2011-05-01 19:42:50 +0200
commit3ae01ff9f5b3e1f2ebded97ff36a705ae3b17b30 (patch)
tree823ecc62dcc3a7e01af2c85a852e5ba882393b94 /src
parent040a543f57107efc267b7b5b7352293dcb7d87d5 (diff)
downloadlibssh-3ae01ff9f5b3e1f2ebded97ff36a705ae3b17b30.tar.gz
libssh-3ae01ff9f5b3e1f2ebded97ff36a705ae3b17b30.tar.xz
libssh-3ae01ff9f5b3e1f2ebded97ff36a705ae3b17b30.zip
init: Some initialization fixes.
- Check result of ssh_init() in privatekey_from_base64() - Moved code from ssh_finalize() to appropriate subroutines - Only initialize sockets once (caused mismatch of WSAStartup() and WSACleanup() calls and potential usage of bsd_poll when win_poll should be used)
Diffstat (limited to 'src')
-rw-r--r--src/dh.c8
-rw-r--r--src/init.c9
-rw-r--r--src/keyfiles.c4
-rw-r--r--src/socket.c24
4 files changed, 28 insertions, 17 deletions
diff --git a/src/dh.c b/src/dh.c
index 62f4f0c..30625db 100644
--- a/src/dh.c
+++ b/src/dh.c
@@ -140,6 +140,7 @@ int ssh_crypto_init(void) {
}
bignum_bin2bn(p_value, P_LEN, p);
OpenSSL_add_all_algorithms();
+
#endif
ssh_crypto_initialized = 1;
@@ -154,8 +155,13 @@ void ssh_crypto_finalize(void) {
g = NULL;
bignum_free(p);
p = NULL;
+#ifdef HAVE_LIBGCRYPT
+ gcry_control(GCRYCTL_TERM_SECMEM);
+#elif defined HAVE_LIBCRYPTO
+ EVP_cleanup();
+ CRYPTO_cleanup_all_ex_data();
+#endif
ssh_crypto_initialized=0;
-
}
}
diff --git a/src/init.c b/src/init.c
index 21505db..241b861 100644
--- a/src/init.c
+++ b/src/init.c
@@ -73,15 +73,6 @@ int ssh_init(void) {
int ssh_finalize(void) {
ssh_crypto_finalize();
ssh_socket_cleanup();
-#ifdef HAVE_LIBGCRYPT
- gcry_control(GCRYCTL_TERM_SECMEM);
-#elif defined HAVE_LIBCRYPTO
- EVP_cleanup();
- CRYPTO_cleanup_all_ex_data();
-#endif
-#ifdef _WIN32
- WSACleanup();
-#endif
/* It is important to finalize threading after CRYPTO because
* it still depends on it */
ssh_threads_finalize();
diff --git a/src/keyfiles.c b/src/keyfiles.c
index 923d6db..6ccc6a2 100644
--- a/src/keyfiles.c
+++ b/src/keyfiles.c
@@ -988,7 +988,9 @@ ssh_private_key privatekey_from_base64(ssh_session session, const char *b64_pkey
}
/* needed for openssl initialization */
- ssh_init();
+ if (ssh_init() < 0) {
+ return NULL;
+ }
ssh_log(session, SSH_LOG_RARE, "Trying to read privkey type=%s, passphase=%s, authcb=%s",
type ? type == SSH_KEYTYPE_DSS ? "ssh-dss" : "ssh-rsa": "unknown",
diff --git a/src/socket.c b/src/socket.c
index ba7db23..6b15e6d 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -91,6 +91,8 @@ struct ssh_socket_struct {
ssh_poll_handle poll_out;
};
+static int sockets_initialized = 0;
+
static int ssh_socket_unbuffered_read(ssh_socket s, void *buffer, uint32_t len);
static int ssh_socket_unbuffered_write(ssh_socket s, const void *buffer,
uint32_t len);
@@ -100,16 +102,20 @@ static int ssh_socket_unbuffered_write(ssh_socket s, const void *buffer,
* \brief inits the socket system (windows specific)
*/
int ssh_socket_init(void) {
+ if (sockets_initialized == 0) {
#ifdef _WIN32
- struct WSAData wsaData;
+ struct WSAData wsaData;
- /* Initiates use of the Winsock DLL by a process. */
- if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
- return -1;
- }
+ /* Initiates use of the Winsock DLL by a process. */
+ if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
+ return -1;
+ }
#endif
- ssh_poll_init();
+ ssh_poll_init();
+
+ sockets_initialized = 1;
+ }
return 0;
}
@@ -118,7 +124,13 @@ int ssh_socket_init(void) {
* @brief Cleanup the socket system.
*/
void ssh_socket_cleanup(void) {
+ if (sockets_initialized == 1) {
ssh_poll_cleanup();
+#ifdef _WIN32
+ WSACleanup();
+#endif
+ sockets_initialized = 0;
+ }
}