Move decode_hex into its own file
- Error detection against strotul(3) has been improved, as done in other places. - New function encode_hex has been implemented, which will be used by future commits.
This commit is contained in:
parent
e0fc222f1d
commit
ad7fb045ad
|
@ -5,6 +5,7 @@ add_executable(${PROJECT_NAME}
|
||||||
base64.c
|
base64.c
|
||||||
cftw.c
|
cftw.c
|
||||||
handler.c
|
handler.c
|
||||||
|
hex.c
|
||||||
html.c
|
html.c
|
||||||
http.c
|
http.c
|
||||||
jwt.c
|
jwt.c
|
||||||
|
|
1
Makefile
1
Makefile
|
@ -14,6 +14,7 @@ OBJECTS = \
|
||||||
base64.o \
|
base64.o \
|
||||||
cftw.o \
|
cftw.o \
|
||||||
handler.o \
|
handler.o \
|
||||||
|
hex.o \
|
||||||
html.o \
|
html.o \
|
||||||
http.o \
|
http.o \
|
||||||
jwt.o \
|
jwt.o \
|
||||||
|
|
29
auth.c
29
auth.c
|
@ -1,4 +1,5 @@
|
||||||
#include "auth.h"
|
#include "auth.h"
|
||||||
|
#include "hex.h"
|
||||||
#include "http.h"
|
#include "http.h"
|
||||||
#include "jwt.h"
|
#include "jwt.h"
|
||||||
#include <cjson/cJSON.h>
|
#include <cjson/cJSON.h>
|
||||||
|
@ -70,22 +71,6 @@ end:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int decode_hex(const char *const hex, unsigned char *buf, size_t n)
|
|
||||||
{
|
|
||||||
for (const char *s = hex; *s; s += 2)
|
|
||||||
{
|
|
||||||
const char nibble[sizeof "00"] = {*s, *(s + 1)};
|
|
||||||
|
|
||||||
if (!n)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
*buf++ = strtoul(nibble, NULL, 16);
|
|
||||||
n--;
|
|
||||||
}
|
|
||||||
|
|
||||||
return n ? -1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int find_cookie(const cJSON *const users, const char *const cookie)
|
static int find_cookie(const cJSON *const users, const char *const cookie)
|
||||||
{
|
{
|
||||||
const cJSON *u;
|
const cJSON *u;
|
||||||
|
@ -107,9 +92,9 @@ static int find_cookie(const cJSON *const users, const char *const cookie)
|
||||||
fprintf(stderr, "%s: missing key\n", __func__);
|
fprintf(stderr, "%s: missing key\n", __func__);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else if (decode_hex(key, dkey, sizeof dkey))
|
else if (hex_decode(key, dkey, sizeof dkey))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: decode_hex failed\n", __func__);
|
fprintf(stderr, "%s: hex_decode failed\n", __func__);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,9 +162,9 @@ static int generate_cookie(const cJSON *const json, const char *const path,
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
char *jwt = NULL;
|
char *jwt = NULL;
|
||||||
|
|
||||||
if (decode_hex(key, dkey, sizeof dkey))
|
if (hex_decode(key, dkey, sizeof dkey))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: decode_hex failed\n", __func__);
|
fprintf(stderr, "%s: hex_decode failed\n", __func__);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
else if (!(jwt = jwt_encode(name, dkey, sizeof dkey)))
|
else if (!(jwt = jwt_encode(name, dkey, sizeof dkey)))
|
||||||
|
@ -220,9 +205,9 @@ static int compare_pwd(const char *const salt, const char *const password,
|
||||||
fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
|
fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno));
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
else if (decode_hex(salt, dec_salt, sizeof dec_salt))
|
else if (hex_decode(salt, dec_salt, sizeof dec_salt))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: decode_hex failed\n", __func__);
|
fprintf(stderr, "%s: hex_decode failed\n", __func__);
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
#include "hex.h"
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int hex_encode(const void *const b, char *hex, const size_t buflen,
|
||||||
|
size_t hexlen)
|
||||||
|
{
|
||||||
|
const char *buf = b;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < buflen; i++)
|
||||||
|
{
|
||||||
|
const int r = snprintf(hex, hexlen, "%02hhx", *(const char *)buf++);
|
||||||
|
|
||||||
|
if (r < 0 || r >= hexlen)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
hexlen -= r;
|
||||||
|
hex += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hex_decode(const char *const hex, void *const b, size_t n)
|
||||||
|
{
|
||||||
|
unsigned char *buf = b;
|
||||||
|
|
||||||
|
for (const char *s = hex; *s; s += 2)
|
||||||
|
{
|
||||||
|
const char nibble[sizeof "00"] = {*s, *(s + 1)};
|
||||||
|
|
||||||
|
if (!n)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
char *end;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
*buf++ = strtoul(nibble, &end, 16);
|
||||||
|
|
||||||
|
if (errno || *end)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
n--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n ? -1 : 0;
|
||||||
|
}
|
Loading…
Reference in New Issue