mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-11-29 01:03:57 +03:00
string: Use the struct and array for allocating the struct.
This commit is contained in:
@@ -29,7 +29,7 @@
|
|||||||
#endif
|
#endif
|
||||||
struct ssh_string_struct {
|
struct ssh_string_struct {
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
unsigned char string[MAX_PACKET_LEN];
|
unsigned char data[1];
|
||||||
}
|
}
|
||||||
#if !defined(__SUNPRO_C) && !defined(_MSC_VER)
|
#if !defined(__SUNPRO_C) && !defined(_MSC_VER)
|
||||||
__attribute__ ((packed))
|
__attribute__ ((packed))
|
||||||
|
|||||||
14
src/dh.c
14
src/dh.c
@@ -356,19 +356,19 @@ ssh_string make_bignum_string(bignum num) {
|
|||||||
fprintf(stderr, "%d bits, %d bytes, %d padding\n", bits, len, pad);
|
fprintf(stderr, "%d bits, %d bytes, %d padding\n", bits, len, pad);
|
||||||
#endif /* DEBUG_CRYPTO */
|
#endif /* DEBUG_CRYPTO */
|
||||||
/* TODO: fix that crap !! */
|
/* TODO: fix that crap !! */
|
||||||
ptr = malloc(4 + len + pad);
|
ptr = malloc(sizeof(struct ssh_string_struct) + len + pad);
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ptr->size = htonl(len + pad);
|
ptr->size = htonl(len + pad);
|
||||||
if (pad) {
|
if (pad) {
|
||||||
ptr->string[0] = 0;
|
ptr->data[0] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
bignum_bn2bin(num, len, ptr->string + pad);
|
bignum_bn2bin(num, len, ptr->data + pad);
|
||||||
#elif HAVE_LIBCRYPTO
|
#elif HAVE_LIBCRYPTO
|
||||||
bignum_bn2bin(num, ptr->string + pad);
|
bignum_bn2bin(num, ptr->data + pad);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
@@ -384,9 +384,9 @@ bignum make_string_bn(ssh_string string){
|
|||||||
#endif /* DEBUG_CRYPTO */
|
#endif /* DEBUG_CRYPTO */
|
||||||
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
bignum_bin2bn(string->string, len, &bn);
|
bignum_bin2bn(string->data, len, &bn);
|
||||||
#elif defined HAVE_LIBCRYPTO
|
#elif defined HAVE_LIBCRYPTO
|
||||||
bn = bignum_bin2bn(string->string, len, NULL);
|
bn = bignum_bin2bn(string->data, len, NULL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return bn;
|
return bn;
|
||||||
@@ -985,7 +985,7 @@ int ssh_get_pubkey_hash(ssh_session session, unsigned char **hash) {
|
|||||||
|
|
||||||
pubkey = session->current_crypto->server_pubkey;
|
pubkey = session->current_crypto->server_pubkey;
|
||||||
|
|
||||||
md5_update(ctx, pubkey->string, ssh_string_len(pubkey));
|
md5_update(ctx, ssh_string_data(pubkey), ssh_string_len(pubkey));
|
||||||
md5_final(h, ctx);
|
md5_final(h, ctx);
|
||||||
|
|
||||||
*hash = h;
|
*hash = h;
|
||||||
|
|||||||
@@ -241,9 +241,9 @@ static int check_public_key(ssh_session session, char **tokens) {
|
|||||||
/* TODO: fix the hardcoding */
|
/* TODO: fix the hardcoding */
|
||||||
tmpstring->size = htonl(len);
|
tmpstring->size = htonl(len);
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
bignum_bn2bin(tmpbn, len, tmpstring->string);
|
bignum_bn2bin(tmpbn, len, string_data(tmpstring));
|
||||||
#elif defined HAVE_LIBCRYPTO
|
#elif defined HAVE_LIBCRYPTO
|
||||||
bignum_bn2bin(tmpbn, tmpstring->string);
|
bignum_bn2bin(tmpbn, string_data(tmpstring));
|
||||||
#endif
|
#endif
|
||||||
bignum_free(tmpbn);
|
bignum_free(tmpbn);
|
||||||
if (buffer_add_ssh_string(pubkey_buffer, tmpstring) < 0) {
|
if (buffer_add_ssh_string(pubkey_buffer, tmpstring) < 0) {
|
||||||
@@ -272,7 +272,7 @@ static int check_public_key(ssh_session session, char **tokens) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* now test that they are identical */
|
/* now test that they are identical */
|
||||||
if (memcmp(buffer_get_rest(pubkey_buffer), pubkey->string,
|
if (memcmp(buffer_get_rest(pubkey_buffer), ssh_string_data(pubkey),
|
||||||
buffer_get_rest_len(pubkey_buffer)) != 0) {
|
buffer_get_rest_len(pubkey_buffer)) != 0) {
|
||||||
ssh_buffer_free(pubkey_buffer);
|
ssh_buffer_free(pubkey_buffer);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
27
src/string.c
27
src/string.c
@@ -51,12 +51,14 @@
|
|||||||
struct ssh_string_struct *ssh_string_new(size_t size) {
|
struct ssh_string_struct *ssh_string_new(size_t size) {
|
||||||
struct ssh_string_struct *str = NULL;
|
struct ssh_string_struct *str = NULL;
|
||||||
|
|
||||||
str = malloc(size + 4);
|
str = malloc(sizeof(struct ssh_string_struct) + size);
|
||||||
if (str == NULL) {
|
if (str == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
str->size = htonl(size);
|
str->size = htonl(size);
|
||||||
|
str->data[0] = 0;
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +79,8 @@ int ssh_string_fill(struct ssh_string_struct *s, const void *data, size_t len) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(s->string, data, len);
|
memcpy(s->data, data, len);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,12 +105,12 @@ struct ssh_string_struct *ssh_string_from_char(const char *what) {
|
|||||||
|
|
||||||
len = strlen(what);
|
len = strlen(what);
|
||||||
|
|
||||||
ptr = malloc(4 + len);
|
ptr = ssh_string_new(len);
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ptr->size = htonl(len);
|
|
||||||
memcpy(ptr->string, what, len);
|
memcpy(ptr->data, what, len);
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
@@ -141,7 +144,7 @@ size_t ssh_string_len(struct ssh_string_struct *s) {
|
|||||||
char *ssh_string_to_char(struct ssh_string_struct *s) {
|
char *ssh_string_to_char(struct ssh_string_struct *s) {
|
||||||
size_t len;
|
size_t len;
|
||||||
char *new;
|
char *new;
|
||||||
if(s==NULL || s->string == NULL)
|
if (s == NULL || s->data == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
len = ntohl(s->size) + 1;
|
len = ntohl(s->size) + 1;
|
||||||
new = malloc(len);
|
new = malloc(len);
|
||||||
@@ -149,7 +152,7 @@ char *ssh_string_to_char(struct ssh_string_struct *s) {
|
|||||||
if (new == NULL) {
|
if (new == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
memcpy(new, s->string, len - 1);
|
memcpy(new, s->data, len - 1);
|
||||||
new[len - 1] = '\0';
|
new[len - 1] = '\0';
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
@@ -174,16 +177,16 @@ void ssh_string_free_char(char *s) {
|
|||||||
struct ssh_string_struct *ssh_string_copy(struct ssh_string_struct *s) {
|
struct ssh_string_struct *ssh_string_copy(struct ssh_string_struct *s) {
|
||||||
struct ssh_string_struct *new;
|
struct ssh_string_struct *new;
|
||||||
|
|
||||||
if(s == NULL || s->string == NULL) {
|
if (s == NULL || s->data == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
new = malloc(ntohl(s->size) + 4);
|
|
||||||
|
|
||||||
|
new = ssh_string_new(s->size);
|
||||||
if (new == NULL) {
|
if (new == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
new->size = s->size;
|
new->size = s->size;
|
||||||
memcpy(new->string, s->string, ntohl(s->size));
|
memcpy(new->data, s->data, ntohl(s->size));
|
||||||
|
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
@@ -197,7 +200,7 @@ void ssh_string_burn(struct ssh_string_struct *s) {
|
|||||||
if (s == NULL) {
|
if (s == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
memset(s->string, 'X', ssh_string_len(s));
|
memset(s->data, 'X', ssh_string_len(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -212,7 +215,7 @@ void *ssh_string_data(struct ssh_string_struct *s) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return s->string;
|
return s->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user