Reformat some of the code.

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@521 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Andreas Schneider
2009-04-17 13:13:14 +00:00
parent 09fdf0e8e6
commit 3216520b4c

View File

@@ -708,17 +708,18 @@ static struct crypto_struct ssh_ciphertab[] = {
#endif /* OPENSSL_CRYPTO */ #endif /* OPENSSL_CRYPTO */
/* it allocates a new cipher structure based on its offset into the global table */ /* it allocates a new cipher structure based on its offset into the global table */
static struct crypto_struct *cipher_new(int offset){ static struct crypto_struct *cipher_new(int offset) {
struct crypto_struct *cipher; struct crypto_struct *cipher = NULL;
cipher = malloc(sizeof(struct crypto_struct)); cipher = malloc(sizeof(struct crypto_struct));
if (cipher == NULL) { if (cipher == NULL) {
return NULL; return NULL;
} }
/* note the memcpy will copy the pointers : so, you shouldn't free them */ /* note the memcpy will copy the pointers : so, you shouldn't free them */
memcpy(cipher,&ssh_ciphertab[offset],sizeof(*cipher)); memcpy(cipher, &ssh_ciphertab[offset], sizeof(*cipher));
return cipher;
return cipher;
} }
static void cipher_free(struct crypto_struct *cipher) { static void cipher_free(struct crypto_struct *cipher) {
@@ -780,77 +781,94 @@ void crypto_free(CRYPTO *crypto){
} }
static int crypt_set_algorithms2(SSH_SESSION *session){ static int crypt_set_algorithms2(SSH_SESSION *session){
/* we must scan the kex entries to find crypto algorithms and set their appropriate structure */ const char *wanted;
int i=0; int i = 0;
/* out */
char *wanted=session->client_kex.methods[SSH_CRYPT_C_S];
while(ssh_ciphertab[i].name && strcmp(wanted,ssh_ciphertab[i].name))
i++;
if(!ssh_ciphertab[i].name){
ssh_set_error(session,SSH_FATAL,"Crypt_set_algorithms2 : no crypto algorithm function found for %s",wanted);
return SSH_ERROR;
}
ssh_log(session,SSH_LOG_PACKET,"Set output algorithm %s",wanted);
session->next_crypto->out_cipher = cipher_new(i); /* we must scan the kex entries to find crypto algorithms and set their appropriate structure */
if (session->next_crypto->out_cipher == NULL) { /* out */
ssh_set_error(session, SSH_FATAL, "No space left"); wanted = session->client_kex.methods[SSH_CRYPT_C_S];
return SSH_ERROR; while (ssh_ciphertab[i].name && strcmp(wanted, ssh_ciphertab[i].name)) {
} i++;
}
i=0; if (ssh_ciphertab[i].name == NULL) {
/* in */ ssh_set_error(session, SSH_FATAL,
wanted=session->client_kex.methods[SSH_CRYPT_S_C]; "Crypt_set_algorithms2: no crypto algorithm function found for %s",
while(ssh_ciphertab[i].name && strcmp(wanted,ssh_ciphertab[i].name)) wanted);
i++; return SSH_ERROR;
if(!ssh_ciphertab[i].name){ }
ssh_set_error(session,SSH_FATAL,"Crypt_set_algorithms : no crypto algorithm function found for %s",wanted); ssh_log(session, SSH_LOG_PACKET, "Set output algorithm to %s", wanted);
return SSH_ERROR;
}
ssh_log(session,SSH_LOG_PACKET,"Set input algorithm %s",wanted);
session->next_crypto->in_cipher = cipher_new(i); session->next_crypto->out_cipher = cipher_new(i);
if (session->next_crypto->in_cipher == NULL) { if (session->next_crypto->out_cipher == NULL) {
ssh_set_error(session, SSH_FATAL, "No space left"); ssh_set_error(session, SSH_FATAL, "No space left");
return SSH_ERROR; return SSH_ERROR;
} }
i = 0;
/* compression */ /* in */
if(strstr(session->client_kex.methods[SSH_COMP_C_S],"zlib")) wanted = session->client_kex.methods[SSH_CRYPT_S_C];
session->next_crypto->do_compress_out=1; while (ssh_ciphertab[i].name && strcmp(wanted, ssh_ciphertab[i].name)) {
if(strstr(session->client_kex.methods[SSH_COMP_S_C],"zlib")) i++;
session->next_crypto->do_compress_in=1; }
return SSH_OK;
if (ssh_ciphertab[i].name == NULL) {
ssh_set_error(session, SSH_FATAL,
"Crypt_set_algorithms: no crypto algorithm function found for %s",
wanted);
return SSH_ERROR;
}
ssh_log(session, SSH_LOG_PACKET, "Set input algorithm to %s", wanted);
session->next_crypto->in_cipher = cipher_new(i);
if (session->next_crypto->in_cipher == NULL) {
ssh_set_error(session, SSH_FATAL, "Not enough space");
return SSH_ERROR;
}
/* compression */
if (strstr(session->client_kex.methods[SSH_COMP_C_S], "zlib")) {
session->next_crypto->do_compress_out = 1;
}
if (strstr(session->client_kex.methods[SSH_COMP_S_C], "zlib")) {
session->next_crypto->do_compress_in = 1;
}
return SSH_OK;
} }
static int crypt_set_algorithms1(SSH_SESSION *session){ static int crypt_set_algorithms1(SSH_SESSION *session) {
int i=0; int i = 0;
/* right now, we force 3des-cbc to be taken */
while(ssh_ciphertab[i].name && strcmp(ssh_ciphertab[i].name,"3des-cbc-ssh1"))
++i;
if(!ssh_ciphertab[i].name){
ssh_set_error(session,SSH_FATAL,"cipher 3des-cbc-ssh1 not found !");
return -1;
}
session->next_crypto->out_cipher = cipher_new(i); /* right now, we force 3des-cbc to be taken */
if (session->next_crypto->out_cipher == NULL) { while (ssh_ciphertab[i].name && strcmp(ssh_ciphertab[i].name,
ssh_set_error(session, SSH_FATAL, "No space left"); "3des-cbc-ssh1")) {
return SSH_ERROR; i++;
} }
session->next_crypto->in_cipher = cipher_new(i); if (ssh_ciphertab[i].name == NULL) {
if (session->next_crypto->in_cipher == NULL) { ssh_set_error(session, SSH_FATAL, "cipher 3des-cbc-ssh1 not found!");
ssh_set_error(session, SSH_FATAL, "No space left"); return -1;
return SSH_ERROR; }
}
return SSH_OK; session->next_crypto->out_cipher = cipher_new(i);
if (session->next_crypto->out_cipher == NULL) {
ssh_set_error(session, SSH_FATAL, "No space left");
return SSH_ERROR;
}
session->next_crypto->in_cipher = cipher_new(i);
if (session->next_crypto->in_cipher == NULL) {
ssh_set_error(session, SSH_FATAL, "No space left");
return SSH_ERROR;
}
return SSH_OK;
} }
int crypt_set_algorithms(SSH_SESSION *session){ int crypt_set_algorithms(SSH_SESSION *session) {
return session->version==1?crypt_set_algorithms1(session): return (session->version == 1) ? crypt_set_algorithms1(session) :
crypt_set_algorithms2(session); crypt_set_algorithms2(session);
} }
// TODO Obviously too much cut and paste here // TODO Obviously too much cut and paste here