string: Use the struct and array for allocating the struct.
Этот коммит содержится в:
родитель
55c758d079
Коммит
5581323c2c
@ -29,7 +29,7 @@
|
||||
#endif
|
||||
struct ssh_string_struct {
|
||||
uint32_t size;
|
||||
unsigned char string[MAX_PACKET_LEN];
|
||||
unsigned char data[1];
|
||||
}
|
||||
#if !defined(__SUNPRO_C) && !defined(_MSC_VER)
|
||||
__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);
|
||||
#endif /* DEBUG_CRYPTO */
|
||||
/* TODO: fix that crap !! */
|
||||
ptr = malloc(4 + len + pad);
|
||||
ptr = malloc(sizeof(struct ssh_string_struct) + len + pad);
|
||||
if (ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ptr->size = htonl(len + pad);
|
||||
if (pad) {
|
||||
ptr->string[0] = 0;
|
||||
ptr->data[0] = 0;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBGCRYPT
|
||||
bignum_bn2bin(num, len, ptr->string + pad);
|
||||
bignum_bn2bin(num, len, ptr->data + pad);
|
||||
#elif HAVE_LIBCRYPTO
|
||||
bignum_bn2bin(num, ptr->string + pad);
|
||||
bignum_bn2bin(num, ptr->data + pad);
|
||||
#endif
|
||||
|
||||
return ptr;
|
||||
@ -384,9 +384,9 @@ bignum make_string_bn(ssh_string string){
|
||||
#endif /* DEBUG_CRYPTO */
|
||||
|
||||
#ifdef HAVE_LIBGCRYPT
|
||||
bignum_bin2bn(string->string, len, &bn);
|
||||
bignum_bin2bn(string->data, len, &bn);
|
||||
#elif defined HAVE_LIBCRYPTO
|
||||
bn = bignum_bin2bn(string->string, len, NULL);
|
||||
bn = bignum_bin2bn(string->data, len, NULL);
|
||||
#endif
|
||||
|
||||
return bn;
|
||||
@ -985,7 +985,7 @@ int ssh_get_pubkey_hash(ssh_session session, unsigned char **hash) {
|
||||
|
||||
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);
|
||||
|
||||
*hash = h;
|
||||
|
@ -241,9 +241,9 @@ static int check_public_key(ssh_session session, char **tokens) {
|
||||
/* TODO: fix the hardcoding */
|
||||
tmpstring->size = htonl(len);
|
||||
#ifdef HAVE_LIBGCRYPT
|
||||
bignum_bn2bin(tmpbn, len, tmpstring->string);
|
||||
bignum_bn2bin(tmpbn, len, string_data(tmpstring));
|
||||
#elif defined HAVE_LIBCRYPTO
|
||||
bignum_bn2bin(tmpbn, tmpstring->string);
|
||||
bignum_bn2bin(tmpbn, string_data(tmpstring));
|
||||
#endif
|
||||
bignum_free(tmpbn);
|
||||
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 */
|
||||
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) {
|
||||
ssh_buffer_free(pubkey_buffer);
|
||||
return 0;
|
||||
|
29
src/string.c
29
src/string.c
@ -51,12 +51,14 @@
|
||||
struct ssh_string_struct *ssh_string_new(size_t size) {
|
||||
struct ssh_string_struct *str = NULL;
|
||||
|
||||
str = malloc(size + 4);
|
||||
str = malloc(sizeof(struct ssh_string_struct) + size);
|
||||
if (str == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
str->size = htonl(size);
|
||||
str->data[0] = 0;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
@ -77,7 +79,8 @@ int ssh_string_fill(struct ssh_string_struct *s, const void *data, size_t len) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(s->string, data, len);
|
||||
memcpy(s->data, data, len);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -102,12 +105,12 @@ struct ssh_string_struct *ssh_string_from_char(const char *what) {
|
||||
|
||||
len = strlen(what);
|
||||
|
||||
ptr = malloc(4 + len);
|
||||
ptr = ssh_string_new(len);
|
||||
if (ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ptr->size = htonl(len);
|
||||
memcpy(ptr->string, what, len);
|
||||
|
||||
memcpy(ptr->data, what, len);
|
||||
|
||||
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) {
|
||||
size_t len;
|
||||
char *new;
|
||||
if(s==NULL || s->string == NULL)
|
||||
if (s == NULL || s->data == NULL)
|
||||
return NULL;
|
||||
len = ntohl(s->size) + 1;
|
||||
new = malloc(len);
|
||||
@ -149,7 +152,7 @@ char *ssh_string_to_char(struct ssh_string_struct *s) {
|
||||
if (new == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memcpy(new, s->string, len - 1);
|
||||
memcpy(new, s->data, len - 1);
|
||||
new[len - 1] = '\0';
|
||||
return new;
|
||||
}
|
||||
@ -173,17 +176,17 @@ void ssh_string_free_char(char *s) {
|
||||
*/
|
||||
struct ssh_string_struct *ssh_string_copy(struct ssh_string_struct *s) {
|
||||
struct ssh_string_struct *new;
|
||||
|
||||
if(s == NULL || s->string == NULL) {
|
||||
|
||||
if (s == NULL || s->data == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
new = malloc(ntohl(s->size) + 4);
|
||||
|
||||
new = ssh_string_new(s->size);
|
||||
if (new == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
new->size = s->size;
|
||||
memcpy(new->string, s->string, ntohl(s->size));
|
||||
memcpy(new->data, s->data, ntohl(s->size));
|
||||
|
||||
return new;
|
||||
}
|
||||
@ -197,7 +200,7 @@ void ssh_string_burn(struct ssh_string_struct *s) {
|
||||
if (s == NULL) {
|
||||
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 s->string;
|
||||
return s->data;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Загрузка…
x
Ссылка в новой задаче
Block a user