7
7
#include <time.h>
8
8
#include "base64.h"
9
9
10
-
11
- char * g_header_b64 = NULL ;
12
- char * g_payload_b64 = NULL ;
13
- char * g_signature_b64 = NULL ;
14
- unsigned char * g_to_encrypt = NULL ;
15
- unsigned char * g_signature = NULL ;
16
-
10
+ char * g_header_b64 = NULL ; // Holds the Base64 header of the original JWT
11
+ char * g_payload_b64 = NULL ; // Holds the Base64 payload of the original JWT
12
+ char * g_signature_b64 = NULL ; // Holds the Base64 signature of the original JWT
13
+ unsigned char * g_to_encrypt = NULL ; // Holds the part of the JWT that needs to be hashed
14
+ unsigned char * g_signature = NULL ; // Holds the Base64 *decoded* signature of the original JWT
15
+
16
+ // Some lengths of buffers. Useful to avoid computing it multiple times.
17
+ // Also, not all strings used finish with a '\0' for optimization purposes.
18
+ // In that case, we need to have the length
17
19
size_t g_header_b64_len = 0 ;
18
20
size_t g_payload_b64_len = 0 ;
19
21
size_t g_signature_b64_len = 0 ;
20
22
size_t g_signature_len = 0 ;
21
23
size_t g_to_encrypt_len = 0 ;
22
24
25
+ // The alphabet to use when brute-forcing
23
26
char * g_alphabet = NULL ;
24
27
size_t g_alphabet_len = 0 ;
25
28
26
- unsigned char * g_result = NULL ;
29
+ // Holds the computed signature at each iteration to compare it with the original
30
+ // signature
31
+ unsigned char * g_result = NULL ;
27
32
unsigned int g_result_len = 0 ;
28
33
29
34
char * g_buffer = NULL ;
30
35
36
+ // The hash function to apply the HMAC to
31
37
EVP_MD * g_evp_md = NULL ;
32
38
39
+ /**
40
+ * Check if the signature produced with "secret
41
+ * of size "secrent_len" (without the '\0') matches the original
42
+ * signature.
43
+ * Return true if it matches, false otherwise
44
+ */
33
45
bool check (const char * secret , size_t secret_len ) {
34
- // Hash to_encrypt using HMAC-SHA256 into result
46
+ // Hash to_encrypt using HMAC into result
35
47
HMAC (
36
48
g_evp_md ,
37
49
(const unsigned char * ) secret , secret_len ,
@@ -66,7 +78,7 @@ char *bruteSequential(int start, int maxLen)
66
78
for (int i = start ; i <= maxLen ; ++ i )
67
79
{
68
80
if (bruteImpl (g_buffer , 0 , i ))
69
- return strdup (g_buffer );
81
+ return strdup (g_buffer );
70
82
}
71
83
72
84
return NULL ;
@@ -97,11 +109,10 @@ int main(int argc, char **argv) {
97
109
98
110
g_alphabet_len = strlen (g_alphabet );
99
111
100
- // Split it into header, payload and signature
112
+ // Split the JWT into header, payload and signature
101
113
g_header_b64 = strtok (jwt , "." );
102
114
g_payload_b64 = strtok (NULL , "." );
103
115
g_signature_b64 = strtok (NULL , "." );
104
-
105
116
g_header_b64_len = strlen (g_header_b64 );
106
117
g_payload_b64_len = strlen (g_payload_b64 );
107
118
g_signature_b64_len = strlen (g_signature_b64 );
@@ -120,8 +131,11 @@ int main(int argc, char **argv) {
120
131
// is returned by this function
121
132
g_signature_len = Base64decode ((char * ) g_signature , (const char * ) g_signature_b64 );
122
133
134
+ // Allocate the buffer used to hold the calculated signature
123
135
g_result = malloc (EVP_MAX_MD_SIZE );
124
136
g_buffer = malloc (max_len + 1 );
137
+
138
+ // The chosen hash function is SHA-256
125
139
g_evp_md = (EVP_MD * ) EVP_sha256 ();
126
140
127
141
clock_t start = clock (), diff ;
0 commit comments