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>
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,
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)
}
}
+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)
{
char *new_secret = NULL;
struct oc_text_buf *buf;
- int i;
switch (vpninfo->hotp_secret_format) {
case HOTP_SECRET_BASE32:
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:
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);