Skip to content

Commit

Permalink
Implement base64url decode using OpenSSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudy VILLENEUVE committed Jul 15, 2019
1 parent 1347d60 commit ccc2481
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 319 deletions.
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 _BASE64_H_
#define _BASE64_H_

#ifdef __cplusplus
extern "C" {
#endif

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

#ifdef __cplusplus
}
#endif

#endif //_BASE64_H_
Loading

0 comments on commit ccc2481

Please sign in to comment.