-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement base64url decode using OpenSSL
- Loading branch information
Rudy VILLENEUVE
committed
Jul 15, 2019
1 parent
1347d60
commit ccc2481
Showing
6 changed files
with
73 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
Oops, something went wrong.