Skip to content

Commit

Permalink
Add libkrb5support hex functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
greghudson committed Mar 2, 2018
1 parent e8e4115 commit 720dea5
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ local.properties

/src/util/support/libkrb5support.exports
/src/util/support/t_base64
/src/util/support/t_hex
/src/util/support/t_json
/src/util/support/t_k5buf
/src/util/support/t_path
Expand Down
53 changes: 53 additions & 0 deletions src/include/k5-hex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* include/k5-hex.h - libkrb5support hex encoding/decoding declarations */
/*
* Copyright (C) 2018 by the Massachusetts Institute of Technology.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef K5_HEX_H
#define K5_HEX_H

#include "k5-platform.h"

/*
* Encode len bytes in hex, placing the result in allocated storage in
* *hex_out. Use uppercase hex digits if uppercase is non-zero. Return 0 on
* success, ENOMEM on error.
*/
int k5_hex_encode(const void *bytes, size_t len, int uppercase,
char **hex_out);

/*
* Decode hex bytes, placing the result in allocated storage in *bytes_out and
* *len_out. Null-terminate the result (primarily for decoding passwords in
* libkdb_ldap). Return 0 on success, ENOMEM or EINVAL on error.
*/
int k5_hex_decode(const char *hex, uint8_t **bytes_out, size_t *len_out);

#endif /* K5_HEX_H */
15 changes: 12 additions & 3 deletions src/util/support/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ STLIBOBJS= \
path.o \
base64.o \
json.o \
hex.o \
bcmp.o \
strerror_r.o \
$(GETTIMEOFDAY_ST_OBJ) \
Expand All @@ -106,6 +107,7 @@ LIBOBJS= \
$(OUTPRE)path.$(OBJEXT) \
$(OUTPRE)base64.$(OBJEXT) \
$(OUTPRE)json.$(OBJEXT) \
$(OUTPRE)hex.$(OBJEXT) \
$(OUTPRE)bcmp.$(OBJEXT) \
$(OUTPRE)strerror_r.$(OBJEXT) \
$(GETTIMEOFDAY_OBJ) \
Expand Down Expand Up @@ -136,10 +138,12 @@ SRCS=\
$(srcdir)/t_unal.c \
$(srcdir)/t_path.c \
$(srcdir)/t_json.c \
$(srcdir)/t_hex.c \
$(srcdir)/zap.c \
$(srcdir)/path.c \
$(srcdir)/base64.c \
$(srcdir)/json.c \
$(srcdir)/hex.c \
$(srcdir)/bcmp.c \
$(srcdir)/strerror_r.c \
$(srcdir)/t_utf8.c \
Expand Down Expand Up @@ -215,6 +219,9 @@ T_JSON_OBJS= t_json.o json.o base64.o k5buf.o $(PRINTF_ST_OBJ)
t_json: $(T_JSON_OBJS)
$(CC_LINK) -o $@ $(T_JSON_OBJS)

t_hex: t_hex.o hex.o
$(CC_LINK) -o $@ t_hex.o hex.o

t_unal: t_unal.o
$(CC_LINK) -o t_unal t_unal.o

Expand All @@ -226,23 +233,25 @@ T_UTF16_OBJS= t_utf16.o utf8_conv.o utf8.o k5buf.o $(PRINTF_ST_OBJ)
t_utf16: $(T_UTF16_OBJS)
$(CC_LINK) -o $@ $(T_UTF16_OBJS)

TEST_PROGS= t_k5buf t_path t_path_win t_base64 t_json t_unal t_utf8 t_utf16
TEST_PROGS= t_k5buf t_path t_path_win t_base64 t_json t_hex t_unal t_utf8 \
t_utf16

check-unix: $(TEST_PROGS)
./t_k5buf
./t_path
./t_path_win
./t_base64
./t_json
./t_hex
./t_unal
./t_utf8
./t_utf16

clean:
$(RM) t_k5buf.o t_k5buf t_unal.o t_unal path_win.o path_win
$(RM) t_path_win.o t_path_win t_path.o t_path t_base64.o t_base64
$(RM) t_json.o t_json libkrb5support.exports t_utf8.o t_utf8
$(RM) t_utf16.o t_utf16
$(RM) t_json.o t_json t_hex.o t_hex libkrb5support.exports
$(RM) t_utf8.o t_utf8 t_utf16.o t_utf16

@lib_frag@
@libobj_frag@
Expand Down
6 changes: 6 additions & 0 deletions src/util/support/deps
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ t_path.so t_path.po $(OUTPRE)t_path.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
t_path.c
t_json.so t_json.po $(OUTPRE)t_json.$(OBJEXT): $(top_srcdir)/include/k5-json.h \
t_json.c
t_hex.so t_hex.po $(OUTPRE)t_hex.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
$(top_srcdir)/include/k5-hex.h $(top_srcdir)/include/k5-platform.h \
$(top_srcdir)/include/k5-thread.h t_hex.c
zap.so zap.po $(OUTPRE)zap.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
$(top_srcdir)/include/k5-platform.h $(top_srcdir)/include/k5-thread.h \
zap.c
Expand All @@ -76,6 +79,9 @@ json.so json.po $(OUTPRE)json.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
$(top_srcdir)/include/k5-base64.h $(top_srcdir)/include/k5-buf.h \
$(top_srcdir)/include/k5-json.h $(top_srcdir)/include/k5-platform.h \
$(top_srcdir)/include/k5-thread.h json.c
hex.so hex.po $(OUTPRE)hex.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
$(top_srcdir)/include/k5-hex.h $(top_srcdir)/include/k5-platform.h \
$(top_srcdir)/include/k5-thread.h hex.c
bcmp.so bcmp.po $(OUTPRE)bcmp.$(OBJEXT): $(BUILDTOP)/include/autoconf.h \
$(top_srcdir)/include/k5-platform.h $(top_srcdir)/include/k5-thread.h \
bcmp.c
Expand Down
116 changes: 116 additions & 0 deletions src/util/support/hex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* util/support/hex.c - hex encoding/decoding implementation */
/*
* Copyright (C) 2018 by the Massachusetts Institute of Technology.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <k5-platform.h>
#include <k5-hex.h>
#include <ctype.h>

static inline char
hex_digit(uint8_t bval, int uppercase)
{
assert(bval >= 0 && bval <= 0xF);
if (bval < 10)
return '0' + bval;
else if (uppercase)
return 'A' + (bval - 10);
else
return 'a' + (bval - 10);
}

int
k5_hex_encode(const void *bytes, size_t len, int uppercase, char **hex_out)
{
size_t i;
const uint8_t *p = bytes;
char *hex;

*hex_out = NULL;

hex = malloc(len * 2 + 1);
if (hex == NULL)
return ENOMEM;

for (i = 0; i < len; i++) {
hex[i * 2] = hex_digit(p[i] >> 4, uppercase);
hex[i * 2 + 1] = hex_digit(p[i] & 0xF, uppercase);
}
hex[len * 2] = '\0';

*hex_out = hex;
return 0;
}

/* Decode a hex digit. Return 0-15 on success, -1 on invalid input. */
static inline int
decode_hexchar(unsigned char c)
{
if (isdigit(c))
return c - '0';
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return -1;
}

int
k5_hex_decode(const char *hex, uint8_t **bytes_out, size_t *len_out)
{
size_t hexlen, i;
int h1, h2;
uint8_t *bytes;

*bytes_out = NULL;
*len_out = 0;

hexlen = strlen(hex);
if (hexlen % 2 != 0)
return EINVAL;
bytes = malloc(hexlen / 2 + 1);
if (bytes == NULL)
return ENOMEM;

for (i = 0; i < hexlen / 2; i++) {
h1 = decode_hexchar(hex[i * 2]);
h2 = decode_hexchar(hex[i * 2 + 1]);
if (h1 == -1 || h2 == -1) {
free(bytes);
return EINVAL;
}
bytes[i] = h1 * 16 + h2;
}
bytes[i] = 0;

*bytes_out = bytes;
*len_out = hexlen / 2;
return 0;
}
2 changes: 2 additions & 0 deletions src/util/support/libkrb5support-fixed.exports
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ k5_get_error
k5_free_error
k5_clear_error
k5_set_error_info_callout_fn
k5_hex_decode
k5_hex_encode
k5_json_array_add
k5_json_array_create
k5_json_array_fmt
Expand Down

0 comments on commit 720dea5

Please sign in to comment.