diff options
author | Schanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de> | 2018-07-21 10:10:22 +0200 |
---|---|---|
committer | Schanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de> | 2018-07-21 10:10:22 +0200 |
commit | 51ace4c06634efe9fd7edbb39f91f754befccd5e (patch) | |
tree | 9b421438297071b4998b27d4919197341196806e | |
parent | 8b5994b08ab970ba291b1d39847d0284cd9b323e (diff) | |
download | gnunet-51ace4c06634efe9fd7edbb39f91f754befccd5e.tar.gz gnunet-51ace4c06634efe9fd7edbb39f91f754befccd5e.zip |
fix base64urlencode for JWT to adhere to RFC4648
-rw-r--r-- | src/reclaim/jwt.c | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/src/reclaim/jwt.c b/src/reclaim/jwt.c index d9c2447bc..9885bf467 100644 --- a/src/reclaim/jwt.c +++ b/src/reclaim/jwt.c | |||
@@ -54,6 +54,32 @@ create_jwt_header(void) | |||
54 | return json_str; | 54 | return json_str; |
55 | } | 55 | } |
56 | 56 | ||
57 | static void | ||
58 | replace_char(char* str, char find, char replace){ | ||
59 | char *current_pos = strchr(str,find); | ||
60 | while (current_pos){ | ||
61 | *current_pos = replace; | ||
62 | current_pos = strchr(current_pos,find); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | //RFC4648 | ||
67 | static void | ||
68 | fix_base64(char* str) { | ||
69 | char *padding; | ||
70 | //First, remove trailing padding '=' | ||
71 | padding = strtok(str, "="); | ||
72 | while (NULL != padding) | ||
73 | padding = strtok(NULL, "="); | ||
74 | |||
75 | //Replace + with - | ||
76 | replace_char (str, '+', '-'); | ||
77 | |||
78 | //Replace / with _ | ||
79 | replace_char (str, '/', '_'); | ||
80 | |||
81 | } | ||
82 | |||
57 | /** | 83 | /** |
58 | * Create a JWT from attributes | 84 | * Create a JWT from attributes |
59 | * | 85 | * |
@@ -73,7 +99,6 @@ jwt_create_from_list (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key, | |||
73 | char* audience; | 99 | char* audience; |
74 | char* subject; | 100 | char* subject; |
75 | char* header; | 101 | char* header; |
76 | char* padding; | ||
77 | char* body_str; | 102 | char* body_str; |
78 | char* result; | 103 | char* result; |
79 | char* header_base64; | 104 | char* header_base64; |
@@ -121,19 +146,12 @@ jwt_create_from_list (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key, | |||
121 | GNUNET_STRINGS_base64_encode (header, | 146 | GNUNET_STRINGS_base64_encode (header, |
122 | strlen (header), | 147 | strlen (header), |
123 | &header_base64); | 148 | &header_base64); |
124 | //Remove GNUNET padding of base64 | 149 | fix_base64(header_base64); |
125 | padding = strtok(header_base64, "="); | ||
126 | while (NULL != padding) | ||
127 | padding = strtok(NULL, "="); | ||
128 | 150 | ||
129 | GNUNET_STRINGS_base64_encode (body_str, | 151 | GNUNET_STRINGS_base64_encode (body_str, |
130 | strlen (body_str), | 152 | strlen (body_str), |
131 | &body_base64); | 153 | &body_base64); |
132 | 154 | fix_base64(body_base64); | |
133 | //Remove GNUNET padding of base64 | ||
134 | padding = strtok(body_base64, "="); | ||
135 | while (NULL != padding) | ||
136 | padding = strtok(NULL, "="); | ||
137 | 155 | ||
138 | GNUNET_free (subject); | 156 | GNUNET_free (subject); |
139 | GNUNET_free (audience); | 157 | GNUNET_free (audience); |
@@ -147,11 +165,7 @@ jwt_create_from_list (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key, | |||
147 | GNUNET_STRINGS_base64_encode ((const char*)&signature, | 165 | GNUNET_STRINGS_base64_encode ((const char*)&signature, |
148 | sizeof (struct GNUNET_HashCode), | 166 | sizeof (struct GNUNET_HashCode), |
149 | &signature_base64); | 167 | &signature_base64); |
150 | 168 | fix_base64(signature_base64); | |
151 | //Remove GNUNET padding of base64 | ||
152 | padding = strtok(signature_base64, "="); | ||
153 | while (NULL != padding) | ||
154 | padding = strtok(NULL, "="); | ||
155 | 169 | ||
156 | GNUNET_asprintf (&result, "%s.%s.%s", | 170 | GNUNET_asprintf (&result, "%s.%s.%s", |
157 | header_base64, body_base64, signature_base64); | 171 | header_base64, body_base64, signature_base64); |