| 1 |
/* |
| 2 |
Copyright (c) 2017 Arun Muralidharan |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
of this software and associated documentation files (the "Software"), to deal |
| 6 |
in the Software without restriction, including without limitation the rights |
| 7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
copies of the Software, and to permit persons to whom the Software is |
| 9 |
furnished to do so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef CPP_JWT_BASE64_HPP |
| 24 |
#define CPP_JWT_BASE64_HPP |
| 25 |
|
| 26 |
#include <array> |
| 27 |
#include <cassert> |
| 28 |
#include <cstring> |
| 29 |
#include <ostream> |
| 30 |
#include "jwt/config.hpp" |
| 31 |
#include "jwt/string_view.hpp" |
| 32 |
|
| 33 |
namespace jwt { |
| 34 |
|
| 35 |
// Returns the maximum number of bytes required to |
| 36 |
// encode an input byte string of length `n` to base64. |
| 37 |
inline constexpr |
| 38 |
size_t encoding_size(size_t n) |
| 39 |
{ |
| 40 |
return 4 * ((n + 2) / 3); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
// Returns the maximum number of bytes required |
| 45 |
// to store a decoded base64 byte string. |
| 46 |
inline constexpr |
| 47 |
size_t decoding_size(size_t n) |
| 48 |
{ |
| 49 |
return n / 4 * 3; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Encoding map. |
| 54 |
* A constexpr helper class for performing base64 |
| 55 |
* encoding on the input byte string. |
| 56 |
*/ |
| 57 |
class EMap |
| 58 |
{ |
| 59 |
public: |
| 60 |
constexpr EMap() = default; |
| 61 |
|
| 62 |
public: |
| 63 |
constexpr char at(size_t pos) const noexcept |
| 64 |
{ |
| 65 |
return X_ASSERT(pos < chars_.size()), chars_.at(pos); |
| 66 |
} |
| 67 |
|
| 68 |
private: |
| 69 |
std::array<char, 64> chars_ = {{ |
| 70 |
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', |
| 71 |
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', |
| 72 |
'0','1','2','3','4','5','6','7','8','9', |
| 73 |
'+','/', |
| 74 |
}}; |
| 75 |
}; |
| 76 |
|
| 77 |
/** |
| 78 |
* Encodes a sequence of octet into base64 string. |
| 79 |
* Returns std::string resized to contain only the |
| 80 |
* encoded data (as usual without null terminator). |
| 81 |
* |
| 82 |
* The encoded string is atleast `encoding_size(input len)` |
| 83 |
* in size. |
| 84 |
* |
| 85 |
* Arguments: |
| 86 |
* @in : Input byte string to be encoded. |
| 87 |
* @len : Length of the input byte string. |
| 88 |
*/ |
| 89 |
inline std::string base64_encode(const char* in, size_t len) |
| 90 |
{ |
| 91 |
std::string result; |
| 92 |
const auto encoded_siz = encoding_size(len); |
| 93 |
result.resize(encoded_siz); |
| 94 |
|
| 95 |
constexpr static const EMap emap{}; |
| 96 |
|
| 97 |
int i = 0; |
| 98 |
int j = 0; |
| 99 |
for (; i < static_cast<int>(len) - 2; i += 3) { |
| 100 |
const auto first = in[i]; |
| 101 |
const auto second = in[i+1]; |
| 102 |
const auto third = in[i+2]; |
| 103 |
|
| 104 |
result[j++] = emap.at( (first >> 2) & 0x3F ); |
| 105 |
result[j++] = emap.at(((first & 0x03) << 4) | ((second & 0xF0) >> 4)); |
| 106 |
result[j++] = emap.at(((second & 0x0F) << 2) | ((third & 0xC0) >> 6)); |
| 107 |
result[j++] = emap.at( (third & 0x3F) ); |
| 108 |
} |
| 109 |
|
| 110 |
switch (len % 3) { |
| 111 |
case 2: |
| 112 |
{ |
| 113 |
const auto first = in[i]; |
| 114 |
const auto second = in[i+1]; |
| 115 |
|
| 116 |
result[j++] = emap.at( (first >> 2) & 0x3F ); |
| 117 |
result[j++] = emap.at(((first & 0x03) << 4) | ((second & 0xF0) >> 4)); |
| 118 |
result[j++] = emap.at( (second & 0x0F) << 2 ); |
| 119 |
result[j++] = '='; |
| 120 |
break; |
| 121 |
} |
| 122 |
case 1: |
| 123 |
{ |
| 124 |
const auto first = in[i]; |
| 125 |
|
| 126 |
result[j++] = emap.at((first >> 2) & 0x3F); |
| 127 |
result[j++] = emap.at((first & 0x03) << 4); |
| 128 |
result[j++] = '='; |
| 129 |
result[j++] = '='; |
| 130 |
break; |
| 131 |
} |
| 132 |
case 0: |
| 133 |
break; |
| 134 |
}; |
| 135 |
|
| 136 |
result.resize(j); |
| 137 |
|
| 138 |
return result; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
//======================= Decoder ========================== |
| 144 |
|
| 145 |
/** |
| 146 |
* Decoding map. |
| 147 |
* A helper constexpr class for providing interface |
| 148 |
* to the decoding map for base64. |
| 149 |
*/ |
| 150 |
class DMap |
| 151 |
{ |
| 152 |
public: |
| 153 |
constexpr DMap() = default; |
| 154 |
|
| 155 |
public: |
| 156 |
constexpr signed char at(size_t pos) const noexcept |
| 157 |
{ |
| 158 |
return X_ASSERT(pos < map_.size()), map_[pos]; |
| 159 |
} |
| 160 |
|
| 161 |
private: |
| 162 |
std::array<signed char, 256> map_ = {{ |
| 163 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15 |
| 164 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31 |
| 165 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32-47 |
| 166 |
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 48-63 |
| 167 |
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79 |
| 168 |
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80-95 |
| 169 |
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111 |
| 170 |
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112-127 |
| 171 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143 |
| 172 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159 |
| 173 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175 |
| 174 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191 |
| 175 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207 |
| 176 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223 |
| 177 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239 |
| 178 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 240-255 |
| 179 |
}}; |
| 180 |
}; |
| 181 |
|
| 182 |
/** |
| 183 |
* Decodes octet of base64 encoded byte string. |
| 184 |
* |
| 185 |
* Returns a std::string with the decoded byte string. |
| 186 |
* |
| 187 |
* Arguments: |
| 188 |
* @in : Encoded base64 byte string. |
| 189 |
* @len : Length of the encoded input byte string. |
| 190 |
*/ |
| 191 |
inline std::string base64_decode(const char* in, size_t len) |
| 192 |
{ |
| 193 |
std::string result; |
| 194 |
const auto decoded_siz = decoding_size(len); |
| 195 |
result.resize(decoded_siz); |
| 196 |
|
| 197 |
int i = 0; |
| 198 |
size_t bytes_rem = len; |
| 199 |
size_t bytes_wr = 0; |
| 200 |
|
| 201 |
constexpr static const DMap dmap{}; |
| 202 |
|
| 203 |
while (bytes_rem > 0 && dmap.at(in[bytes_rem - 1]) == -1) { bytes_rem--; } |
| 204 |
|
| 205 |
while (bytes_rem > 4) |
| 206 |
{ |
| 207 |
// Error case in input |
| 208 |
if (dmap.at(*in) == -1) return result; |
| 209 |
|
| 210 |
const auto first = dmap.at(in[0]); |
| 211 |
const auto second = dmap.at(in[1]); |
| 212 |
const auto third = dmap.at(in[2]); |
| 213 |
const auto fourth = dmap.at(in[3]); |
| 214 |
|
| 215 |
result[i] = (first << 2) | (second >> 4); |
| 216 |
result[i + 1] = (second << 4) | (third >> 2); |
| 217 |
result[i + 2] = (third << 6) | fourth; |
| 218 |
|
| 219 |
bytes_rem -= 4; |
| 220 |
i += 3; |
| 221 |
in += 4; |
| 222 |
} |
| 223 |
bytes_wr = i; |
| 224 |
|
| 225 |
switch(bytes_rem) { |
| 226 |
case 4: |
| 227 |
{ |
| 228 |
const auto third = dmap.at(in[2]); |
| 229 |
const auto fourth = dmap.at(in[3]); |
| 230 |
result[i + 2] = (third << 6) | fourth; |
| 231 |
bytes_wr++; |
| 232 |
} |
| 233 |
//FALLTHROUGH |
| 234 |
case 3: |
| 235 |
{ |
| 236 |
const auto second = dmap.at(in[1]); |
| 237 |
const auto third = dmap.at(in[2]); |
| 238 |
result[i + 1] = (second << 4) | (third >> 2); |
| 239 |
bytes_wr++; |
| 240 |
} |
| 241 |
//FALLTHROUGH |
| 242 |
case 2: |
| 243 |
{ |
| 244 |
const auto first = dmap.at(in[0]); |
| 245 |
const auto second = dmap.at(in[1]); |
| 246 |
result[i] = (first << 2) | (second >> 4); |
| 247 |
bytes_wr++; |
| 248 |
} |
| 249 |
}; |
| 250 |
|
| 251 |
result.resize(bytes_wr); |
| 252 |
|
| 253 |
return result; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Makes the base64 encoded byte string URL safe. |
| 258 |
* Overwrites/skips few URL unsafe characters |
| 259 |
* from the input sequence. |
| 260 |
* |
| 261 |
* Arguments: |
| 262 |
* @data : Base64 encoded byte string. |
| 263 |
* @len : Length of the base64 byte string. |
| 264 |
* |
| 265 |
* Returns: |
| 266 |
* Length of the URL safe base64 encoded byte string. |
| 267 |
*/ |
| 268 |
inline size_t base64_uri_encode(char* data, size_t len) noexcept |
| 269 |
{ |
| 270 |
size_t i = 0; |
| 271 |
size_t j = 0; |
| 272 |
|
| 273 |
for (; i < len; ++i) { |
| 274 |
switch (data[i]) { |
| 275 |
case '+': |
| 276 |
data[j++] = '-'; |
| 277 |
break; |
| 278 |
case '/': |
| 279 |
data[j++] = '_'; |
| 280 |
break; |
| 281 |
case '=': |
| 282 |
break; |
| 283 |
default: |
| 284 |
data[j++] = data[i]; |
| 285 |
}; |
| 286 |
} |
| 287 |
|
| 288 |
return j; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Decodes an input URL safe base64 encoded byte string. |
| 293 |
* |
| 294 |
* NOTE: To be used only for decoding URL safe base64 encoded |
| 295 |
* byte string. |
| 296 |
* |
| 297 |
* Arguments: |
| 298 |
* @data : URL safe base64 encoded byte string. |
| 299 |
* @len : Length of the input byte string. |
| 300 |
*/ |
| 301 |
inline std::string base64_uri_decode(const char* data, size_t len) |
| 302 |
{ |
| 303 |
std::string uri_dec; |
| 304 |
uri_dec.resize(len + 4); |
| 305 |
|
| 306 |
size_t i = 0; |
| 307 |
|
| 308 |
for (; i < len; ++i) |
| 309 |
{ |
| 310 |
switch (data[i]) { |
| 311 |
case '-': |
| 312 |
uri_dec[i] = '+'; |
| 313 |
break; |
| 314 |
case '_': |
| 315 |
uri_dec[i] = '/'; |
| 316 |
break; |
| 317 |
default: |
| 318 |
uri_dec[i] = data[i]; |
| 319 |
}; |
| 320 |
} |
| 321 |
|
| 322 |
size_t trailer = 4 - (i % 4); |
| 323 |
if (trailer && trailer < 4) { |
| 324 |
while (trailer--) { |
| 325 |
uri_dec[i++] = '='; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
return base64_decode(uri_dec.c_str(), uri_dec.length()); |
| 330 |
} |
| 331 |
|
| 332 |
} // END namespace jwt |
| 333 |
|
| 334 |
|
| 335 |
#endif |