Skip to content

Commit

Permalink
certmap: add bin_to_hex() helper function
Browse files Browse the repository at this point in the history
This patch adds a helper function to format hexadecimal strings of
binary data.

Resolves: #6403

Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
Reviewed-by: Justin Stephenson <jstephen@redhat.com>
  • Loading branch information
sumit-bose authored and alexey-tikhonov committed Dec 2, 2022
1 parent f293507 commit c4085c9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/lib/certmap/sss_certmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,58 @@ void sss_debug_fn(const char *file,
return;
}

int bin_to_hex(TALLOC_CTX *mem_ctx, bool upper_case, bool colon_sep,
bool reverse, uint8_t *buf, size_t len, char **out)
{
char *o;
size_t c;
const char *fmt = NULL;
size_t s;
size_t chop_end = 0;

if (len == 0 || buf == NULL) {
return EINVAL;
}

if (upper_case) {
if (colon_sep) {
fmt = "%02X:";
s = 3;
chop_end =1;
} else {
fmt = "%02X";
s = 2;
}
} else {
if (colon_sep) {
fmt = "%02x:";
s = 3;
chop_end =1;
} else {
fmt = "%02x";
s = 2;
}
}

o = talloc_size(mem_ctx, (len * s) + 1);
if (o == NULL) {
return ENOMEM;
}

for (c = 0; c < len; c++) {
if (reverse) {
snprintf(o+(c*s), s+1, fmt, buf[len -1 -c]);
} else {
snprintf(o+(c*s), s+1, fmt, buf[c]);
}
}
o[(len * s) - chop_end] = '\0';

*out = o;

return 0;
}

static int get_type_prefix(TALLOC_CTX *mem_ctx, const char *match_rule,
char **type, const char **rule_start)
{
Expand Down
3 changes: 3 additions & 0 deletions src/lib/certmap/sss_certmap_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,7 @@ int add_principal_to_san_list(TALLOC_CTX *mem_ctx, enum san_opt san_opt,

int rdn_list_2_dn_str(TALLOC_CTX *mem_ctx, const char *conversion,
const char **rdn_list, char **result);

int bin_to_hex(TALLOC_CTX *mem_ctx, bool upper_case, bool colon_sep,
bool reverse, uint8_t *buf, size_t len, char **out);
#endif /* __SSS_CERTMAP_INT_H__ */

0 comments on commit c4085c9

Please sign in to comment.