Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement base64url decode using OpenSSL #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CFLAGS += -I $(OPENSSL) -g -std=gnu99 -O3
LDFLAGS += $(OPENSSL_LIB) -lcrypto -lpthread

NAME = jwtcrack
SRCS = main.c base64.c
SRCS = main.c base64url.c
OBJS = $(SRCS:.c=.o)

all: $(NAME)
Expand Down
209 changes: 0 additions & 209 deletions base64.c

This file was deleted.

101 changes: 0 additions & 101 deletions base64.h

This file was deleted.

55 changes: 55 additions & 0 deletions base64url.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>

// Convert base64url to base64
// Fix b64 alignement
// Replace '-' by '+' and '_' by '/'
static unsigned char *base64url_to_base64(const unsigned char *base64) {
size_t base64len = strlen(base64);
// \0 + possible padding
unsigned char *nbase64 = malloc(base64len + 3);

memset(nbase64, 0, base64len + 3);
strcat(nbase64, base64);

// Fix b64 alignement
while (base64len % 4 != 0) {
nbase64[base64len++] = '=';
}
for (int i = 0; i < base64len; ++i) {
if (nbase64[i] == '-')
nbase64[i] = '+';
if (nbase64[i] == '_')
nbase64[i] = '/';
}
return nbase64;
}

int base64url_decode(const unsigned char *in, unsigned char **out)
{
size_t inlen, outlen;

unsigned char *outbuf;
unsigned char *base64 = base64url_to_base64(in);

inlen = strlen(base64);
outlen = (inlen / 4) * 3;
outbuf = malloc(outlen);
if (outbuf == NULL) {
goto err;
}

outlen = EVP_DecodeBlock(outbuf, (unsigned char *)base64, inlen);
if (outlen < 0) {
goto err;
}

*out = outbuf;
free(base64);
return outlen;
err:
free(base64);
free(outbuf);
return -1;
}
14 changes: 14 additions & 0 deletions base64url.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _BASE64URL_H_
#define _BASE64URL_H_

#ifdef __cplusplus
extern "C" {
#endif

int base64url_decode(const unsigned char *in, unsigned char **out);

#ifdef __cplusplus
}
#endif

#endif //_BASE64URL_H_
Loading