Skip to content

Commit

Permalink
Introduced buf_append_hex()
Browse files Browse the repository at this point in the history
That is being used by openconnect_bin2hex() for hex-encoding.

[dwmw2: Clean up buf error handling in openconnect_bin2hex()]

Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
  • Loading branch information
Nikos Mavrogiannopoulos authored and dwmw2 committed Dec 13, 2016
1 parent 8d86a17 commit 9723ca9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
4 changes: 1 addition & 3 deletions digest.c
Expand Up @@ -67,15 +67,13 @@ static void buf_append_unq(struct oc_text_buf *buf, const char *str)
static void buf_append_md5(struct oc_text_buf *buf, void *data, int len)
{
unsigned char md5[16];
int i;

if (openconnect_md5(md5, data, len)) {
buf->error = -EIO;
return;
}

for (i = 0; i < 16; i++)
buf_append(buf, "%02x", md5[i]);
buf_append_hex(buf, md5, 16);
}

int digest_authorization(struct openconnect_info *vpninfo, int proxy,
Expand Down
23 changes: 13 additions & 10 deletions dtls.c
Expand Up @@ -68,17 +68,20 @@

char *openconnect_bin2hex(const char *prefix, const uint8_t *data, unsigned len)
{
char *v;
unsigned plen = strlen(prefix);
unsigned i;

v = malloc(len*2+plen+1);
if (v) {
snprintf(v, plen+1, "%s", prefix);
for (i = 0; i < len; i++)
sprintf(&v[i*2 + plen], "%02x", data[i]);
struct oc_text_buf *buf;
char *p = NULL;

buf = buf_alloc();
buf_append(buf, "%s", prefix);
buf_append_hex(buf, data, len);

if (!buf_error(buf)) {
p = buf->data;
buf->data = NULL;
}
return v;
buf_free(buf);

return p;
}

static int connect_dtls_socket(struct openconnect_info *vpninfo)
Expand Down
9 changes: 9 additions & 0 deletions http.c
Expand Up @@ -54,6 +54,15 @@ void buf_append_urlencoded(struct oc_text_buf *buf, char *str)
}
}

void buf_append_hex(struct oc_text_buf *buf, const void *str, unsigned len)
{
const unsigned char *data = str;
unsigned i;

for (i = 0; i < len; i++)
buf_append(buf, "%02x", (unsigned)data[i]);
}

void buf_truncate(struct oc_text_buf *buf)
{
if (!buf)
Expand Down
5 changes: 1 addition & 4 deletions oath.c
Expand Up @@ -489,7 +489,6 @@ static char *regen_hotp_secret(struct openconnect_info *vpninfo)
{
char *new_secret = NULL;
struct oc_text_buf *buf;
int i;

switch (vpninfo->hotp_secret_format) {
case HOTP_SECRET_BASE32:
Expand All @@ -502,9 +501,7 @@ static char *regen_hotp_secret(struct openconnect_info *vpninfo)
case HOTP_SECRET_HEX:
buf = buf_alloc();
buf_append(buf, "0x");
for (i=0; i < vpninfo->oath_secret_len; i++)
buf_append(buf, "%02x",
(unsigned char)vpninfo->oath_secret[i]);
buf_append_hex(buf, vpninfo->oath_secret, vpninfo->oath_secret_len);
break;

case HOTP_SECRET_RAW:
Expand Down
1 change: 1 addition & 0 deletions openconnect-internal.h
Expand Up @@ -991,6 +991,7 @@ int buf_ensure_space(struct oc_text_buf *buf, int len);
void __attribute__ ((format (printf, 2, 3)))
buf_append(struct oc_text_buf *buf, const char *fmt, ...);
void buf_append_bytes(struct oc_text_buf *buf, const void *bytes, int len);
void buf_append_hex(struct oc_text_buf *buf, const void *str, unsigned len);
int buf_append_utf16le(struct oc_text_buf *buf, const char *utf8);
int get_utf8char(const char **utf8);
void buf_append_from_utf16le(struct oc_text_buf *buf, const void *utf16);
Expand Down

0 comments on commit 9723ca9

Please sign in to comment.