| 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_ALGORITHM_HPP |
| 24 |
#define CPP_JWT_ALGORITHM_HPP |
| 25 |
|
| 26 |
/*! |
| 27 |
* Most of the signing and verification code has been taken |
| 28 |
* and modified for C++ specific use from the C implementation |
| 29 |
* JWT library, libjwt. |
| 30 |
* https://github.com/benmcollins/libjwt/tree/master/libjwt |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <cassert> |
| 34 |
#include <memory> |
| 35 |
#include <system_error> |
| 36 |
|
| 37 |
#include <openssl/bn.h> |
| 38 |
#include <openssl/bio.h> |
| 39 |
#include <openssl/pem.h> |
| 40 |
#include <openssl/evp.h> |
| 41 |
#include <openssl/hmac.h> |
| 42 |
#include <openssl/ecdsa.h> |
| 43 |
#include <openssl/buffer.h> |
| 44 |
#include <openssl/opensslv.h> |
| 45 |
|
| 46 |
#include "jwt/assertions.hpp" |
| 47 |
#include "jwt/exceptions.hpp" |
| 48 |
#include "jwt/string_view.hpp" |
| 49 |
#include "jwt/error_codes.hpp" |
| 50 |
#include "jwt/base64.hpp" |
| 51 |
#include "jwt/config.hpp" |
| 52 |
|
| 53 |
namespace jwt { |
| 54 |
|
| 55 |
/// The result type of the signing function |
| 56 |
using sign_result_t = std::pair<std::string, std::error_code>; |
| 57 |
/// The result type of verification function |
| 58 |
using verify_result_t = std::pair<bool, std::error_code>; |
| 59 |
/// The function pointer type for the signing function |
| 60 |
using sign_func_t = sign_result_t (*) (const jwt::string_view key, |
| 61 |
const jwt::string_view data); |
| 62 |
/// The function pointer type for the verifying function |
| 63 |
using verify_func_t = verify_result_t (*) (const jwt::string_view key, |
| 64 |
const jwt::string_view head, |
| 65 |
const jwt::string_view jwt_sign); |
| 66 |
|
| 67 |
namespace algo { |
| 68 |
|
| 69 |
//Me: TODO: All these can be done using code generaion. |
| 70 |
//Me: NO. NEVER. I hate Macros. |
| 71 |
//Me: You can use templates too. |
| 72 |
//Me: No. I would rather prefer explicit. |
| 73 |
//Me: Ok. You win. |
| 74 |
//Me: Same to you. |
| 75 |
|
| 76 |
/** |
| 77 |
* HS256 algorithm. |
| 78 |
*/ |
| 79 |
struct HS256 |
| 80 |
{ |
| 81 |
const EVP_MD* operator()() noexcept |
| 82 |
{ |
| 83 |
return EVP_sha256(); |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
/** |
| 88 |
* HS384 algorithm. |
| 89 |
*/ |
| 90 |
struct HS384 |
| 91 |
{ |
| 92 |
const EVP_MD* operator()() noexcept |
| 93 |
{ |
| 94 |
return EVP_sha384(); |
| 95 |
} |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* HS512 algorithm. |
| 100 |
*/ |
| 101 |
struct HS512 |
| 102 |
{ |
| 103 |
const EVP_MD* operator()() noexcept |
| 104 |
{ |
| 105 |
return EVP_sha512(); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
/** |
| 110 |
* NONE algorithm. |
| 111 |
*/ |
| 112 |
struct NONE |
| 113 |
{ |
| 114 |
void operator()() noexcept |
| 115 |
{ |
| 116 |
return; |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
/** |
| 121 |
* RS256 algorithm. |
| 122 |
*/ |
| 123 |
struct RS256 |
| 124 |
{ |
| 125 |
static const int type = EVP_PKEY_RSA; |
| 126 |
|
| 127 |
const EVP_MD* operator()() noexcept |
| 128 |
{ |
| 129 |
return EVP_sha256(); |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
/** |
| 134 |
* RS384 algorithm. |
| 135 |
*/ |
| 136 |
struct RS384 |
| 137 |
{ |
| 138 |
static const int type = EVP_PKEY_RSA; |
| 139 |
|
| 140 |
const EVP_MD* operator()() noexcept |
| 141 |
{ |
| 142 |
return EVP_sha384(); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
/** |
| 147 |
* RS512 algorithm. |
| 148 |
*/ |
| 149 |
struct RS512 |
| 150 |
{ |
| 151 |
static const int type = EVP_PKEY_RSA; |
| 152 |
|
| 153 |
const EVP_MD* operator()() noexcept |
| 154 |
{ |
| 155 |
return EVP_sha512(); |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
/** |
| 160 |
* ES256 algorithm. |
| 161 |
*/ |
| 162 |
struct ES256 |
| 163 |
{ |
| 164 |
static const int type = EVP_PKEY_EC; |
| 165 |
|
| 166 |
const EVP_MD* operator()() noexcept |
| 167 |
{ |
| 168 |
return EVP_sha256(); |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
/** |
| 173 |
* ES384 algorithm. |
| 174 |
*/ |
| 175 |
struct ES384 |
| 176 |
{ |
| 177 |
static const int type = EVP_PKEY_EC; |
| 178 |
|
| 179 |
const EVP_MD* operator()() noexcept |
| 180 |
{ |
| 181 |
return EVP_sha384(); |
| 182 |
} |
| 183 |
}; |
| 184 |
|
| 185 |
/** |
| 186 |
* ES512 algorithm. |
| 187 |
*/ |
| 188 |
struct ES512 |
| 189 |
{ |
| 190 |
static const int type = EVP_PKEY_EC; |
| 191 |
|
| 192 |
const EVP_MD* operator()() noexcept |
| 193 |
{ |
| 194 |
return EVP_sha512(); |
| 195 |
} |
| 196 |
}; |
| 197 |
|
| 198 |
} //END Namespace algo |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* JWT signing algorithm types. |
| 203 |
*/ |
| 204 |
enum class algorithm |
| 205 |
{ |
| 206 |
NONE = 0, |
| 207 |
HS256, |
| 208 |
HS384, |
| 209 |
HS512, |
| 210 |
RS256, |
| 211 |
RS384, |
| 212 |
RS512, |
| 213 |
ES256, |
| 214 |
ES384, |
| 215 |
ES512, |
| 216 |
UNKN, |
| 217 |
TERM, |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
/** |
| 222 |
* Convert the algorithm enum class type to |
| 223 |
* its stringified form. |
| 224 |
*/ |
| 225 |
inline jwt::string_view alg_to_str(SCOPED_ENUM algorithm alg) noexcept |
| 226 |
{ |
| 227 |
switch (alg) { |
| 228 |
case algorithm::HS256: return "HS256"; |
| 229 |
case algorithm::HS384: return "HS384"; |
| 230 |
case algorithm::HS512: return "HS512"; |
| 231 |
case algorithm::RS256: return "RS256"; |
| 232 |
case algorithm::RS384: return "RS384"; |
| 233 |
case algorithm::RS512: return "RS512"; |
| 234 |
case algorithm::ES256: return "ES256"; |
| 235 |
case algorithm::ES384: return "ES384"; |
| 236 |
case algorithm::ES512: return "ES512"; |
| 237 |
case algorithm::TERM: return "TERM"; |
| 238 |
case algorithm::NONE: return "NONE"; |
| 239 |
case algorithm::UNKN: return "UNKN"; |
| 240 |
default: assert (0 && "Unknown Algorithm"); |
| 241 |
}; |
| 242 |
return "UNKN"; |
| 243 |
JWT_NOT_REACHED("Code not reached"); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Convert stringified algorithm to enum class. |
| 248 |
* The string comparison is case insesitive. |
| 249 |
*/ |
| 250 |
inline SCOPED_ENUM algorithm str_to_alg(const jwt::string_view alg) noexcept |
| 251 |
{ |
| 252 |
if (!alg.length()) return algorithm::UNKN; |
| 253 |
|
| 254 |
if (!strcasecmp(alg.data(), "NONE")) return algorithm::NONE; |
| 255 |
if (!strcasecmp(alg.data(), "HS256")) return algorithm::HS256; |
| 256 |
if (!strcasecmp(alg.data(), "HS384")) return algorithm::HS384; |
| 257 |
if (!strcasecmp(alg.data(), "HS512")) return algorithm::HS512; |
| 258 |
if (!strcasecmp(alg.data(), "RS256")) return algorithm::RS256; |
| 259 |
if (!strcasecmp(alg.data(), "RS384")) return algorithm::RS384; |
| 260 |
if (!strcasecmp(alg.data(), "RS512")) return algorithm::RS512; |
| 261 |
if (!strcasecmp(alg.data(), "ES256")) return algorithm::ES256; |
| 262 |
if (!strcasecmp(alg.data(), "ES384")) return algorithm::ES384; |
| 263 |
if (!strcasecmp(alg.data(), "ES512")) return algorithm::ES512; |
| 264 |
|
| 265 |
return algorithm::UNKN; |
| 266 |
|
| 267 |
JWT_NOT_REACHED("Code not reached"); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
*/ |
| 272 |
inline void bio_deletor(BIO* ptr) |
| 273 |
{ |
| 274 |
if (ptr) BIO_free_all(ptr); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
*/ |
| 279 |
inline void evp_md_ctx_deletor(EVP_MD_CTX* ptr) |
| 280 |
{ |
| 281 |
if (ptr) EVP_MD_CTX_destroy(ptr); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
*/ |
| 286 |
inline void ec_key_deletor(EC_KEY* ptr) |
| 287 |
{ |
| 288 |
if (ptr) EC_KEY_free(ptr); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
*/ |
| 293 |
inline void ec_sig_deletor(ECDSA_SIG* ptr) |
| 294 |
{ |
| 295 |
if (ptr) ECDSA_SIG_free(ptr); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
*/ |
| 300 |
inline void ev_pkey_deletor(EVP_PKEY* ptr) |
| 301 |
{ |
| 302 |
if (ptr) EVP_PKEY_free(ptr); |
| 303 |
} |
| 304 |
|
| 305 |
/// Useful typedefs |
| 306 |
using bio_deletor_t = decltype(&bio_deletor); |
| 307 |
using BIO_uptr = std::unique_ptr<BIO, bio_deletor_t>; |
| 308 |
|
| 309 |
using evp_mdctx_deletor_t = decltype(&evp_md_ctx_deletor); |
| 310 |
using EVP_MDCTX_uptr = std::unique_ptr<EVP_MD_CTX, evp_mdctx_deletor_t>; |
| 311 |
|
| 312 |
using eckey_deletor_t = decltype(&ec_key_deletor); |
| 313 |
using EC_KEY_uptr = std::unique_ptr<EC_KEY, eckey_deletor_t>; |
| 314 |
|
| 315 |
using ecsig_deletor_t = decltype(&ec_sig_deletor); |
| 316 |
using EC_SIG_uptr = std::unique_ptr<ECDSA_SIG, ecsig_deletor_t>; |
| 317 |
|
| 318 |
using evpkey_deletor_t = decltype(&ev_pkey_deletor); |
| 319 |
using EC_PKEY_uptr = std::unique_ptr<EVP_PKEY, evpkey_deletor_t>; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
/** |
| 324 |
* OpenSSL HMAC based signature and verfication. |
| 325 |
* |
| 326 |
* The template type `Hasher` takes the type representing |
| 327 |
* the HMAC algorithm type from the `jwt::algo` namespace. |
| 328 |
* |
| 329 |
* The struct is specialized for NONE algorithm. See the |
| 330 |
* details of that class as well. |
| 331 |
*/ |
| 332 |
template <typename Hasher> |
| 333 |
struct HMACSign |
| 334 |
{ |
| 335 |
/// The type of Hashing algorithm |
| 336 |
using hasher_type = Hasher; |
| 337 |
|
| 338 |
/** |
| 339 |
* Signs the input using the HMAC algorithm using the |
| 340 |
* provided key. |
| 341 |
* |
| 342 |
* Arguments: |
| 343 |
* @key : The secret/key to use for the signing. |
| 344 |
* Cannot be empty string. |
| 345 |
* @data : The data to be signed. |
| 346 |
* |
| 347 |
* Exceptions: |
| 348 |
* Any allocation failure will result in jwt::MemoryAllocationException |
| 349 |
* being thrown. |
| 350 |
*/ |
| 351 |
static sign_result_t sign(const jwt::string_view key, const jwt::string_view data) |
| 352 |
{ |
| 353 |
std::string sign; |
| 354 |
sign.resize(EVP_MAX_MD_SIZE); |
| 355 |
std::error_code ec{}; |
| 356 |
|
| 357 |
uint32_t len = 0; |
| 358 |
|
| 359 |
unsigned char* res = HMAC(Hasher{}(), |
| 360 |
key.data(), |
| 361 |
static_cast<int>(key.length()), |
| 362 |
reinterpret_cast<const unsigned char*>(data.data()), |
| 363 |
data.length(), |
| 364 |
reinterpret_cast<unsigned char*>(&sign[0]), |
| 365 |
&len); |
| 366 |
if (!res) { |
| 367 |
ec = AlgorithmErrc::SigningErr; |
| 368 |
} |
| 369 |
|
| 370 |
sign.resize(len); |
| 371 |
return { std::move(sign), ec }; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Verifies the JWT string against the signature using |
| 376 |
* the provided key. |
| 377 |
* |
| 378 |
* Arguments: |
| 379 |
* @key : The secret/key to use for the signing. |
| 380 |
* Cannot be empty string. |
| 381 |
* @head : The part of JWT encoded string representing header |
| 382 |
* and the payload claims. |
| 383 |
* @sign : The signature part of the JWT encoded string. |
| 384 |
* |
| 385 |
* Returns: |
| 386 |
* verify_result_t |
| 387 |
* verify_result_t::first set to true if verification succeeds. |
| 388 |
* false otherwise. |
| 389 |
* verify_result_t::second set to relevant error if verification fails. |
| 390 |
* |
| 391 |
* Exceptions: |
| 392 |
* Any allocation failure will result in jwt::MemoryAllocationException |
| 393 |
* being thrown. |
| 394 |
*/ |
| 395 |
static verify_result_t |
| 396 |
verify(const jwt::string_view key, const jwt::string_view head, const jwt::string_view sign); |
| 397 |
|
| 398 |
}; |
| 399 |
|
| 400 |
/** |
| 401 |
* Specialization of `HMACSign` class |
| 402 |
* for NONE algorithm. |
| 403 |
* |
| 404 |
* This specialization is selected for even |
| 405 |
* PEM based algorithms. |
| 406 |
* |
| 407 |
* The signing and verification APIs are |
| 408 |
* basically no-op except that they would |
| 409 |
* set the relevant error code. |
| 410 |
* |
| 411 |
* NOTE: error_code would be set in the case |
| 412 |
* of usage of NONE algorithm. |
| 413 |
* Users of this API are expected to check for |
| 414 |
* the case explicitly. |
| 415 |
*/ |
| 416 |
template <> |
| 417 |
struct HMACSign<algo::NONE> |
| 418 |
{ |
| 419 |
using hasher_type = algo::NONE; |
| 420 |
|
| 421 |
/** |
| 422 |
* Basically a no-op. Sets the error code to NoneAlgorithmUsed. |
| 423 |
*/ |
| 424 |
static sign_result_t sign(const jwt::string_view key, const jwt::string_view data) |
| 425 |
{ |
| 426 |
(void)key; |
| 427 |
(void)data; |
| 428 |
std::error_code ec{}; |
| 429 |
ec = AlgorithmErrc::NoneAlgorithmUsed; |
| 430 |
|
| 431 |
return { std::string{}, ec }; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Basically a no-op. Sets the error code to NoneAlgorithmUsed. |
| 436 |
*/ |
| 437 |
static verify_result_t |
| 438 |
verify(const jwt::string_view key, const jwt::string_view head, const jwt::string_view sign) |
| 439 |
{ |
| 440 |
(void)key; |
| 441 |
(void)head; |
| 442 |
(void)sign; |
| 443 |
std::error_code ec{}; |
| 444 |
ec = AlgorithmErrc::NoneAlgorithmUsed; |
| 445 |
|
| 446 |
return { true, ec }; |
| 447 |
} |
| 448 |
|
| 449 |
}; |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
/** |
| 454 |
* OpenSSL PEM based signature and verfication. |
| 455 |
* |
| 456 |
* The template type `Hasher` takes the type representing |
| 457 |
* the PEM algorithm type from the `jwt::algo` namespace. |
| 458 |
* |
| 459 |
* For NONE algorithm, HMACSign<> specialization is used. |
| 460 |
* See that for more details. |
| 461 |
*/ |
| 462 |
template <typename Hasher> |
| 463 |
struct PEMSign |
| 464 |
{ |
| 465 |
public: |
| 466 |
/// The type of Hashing algorithm |
| 467 |
using hasher_type = Hasher; |
| 468 |
|
| 469 |
/** |
| 470 |
* Signs the input data using PEM encryption algorithm. |
| 471 |
* |
| 472 |
* Arguments: |
| 473 |
* @key : The key/secret to be used for signing. |
| 474 |
* Cannot be an empty string. |
| 475 |
* @data: The data to be signed. |
| 476 |
* |
| 477 |
* Exceptions: |
| 478 |
* Any allocation failure would be thrown out as |
| 479 |
* jwt::MemoryAllocationException. |
| 480 |
*/ |
| 481 |
static sign_result_t sign(const jwt::string_view key, const jwt::string_view data) |
| 482 |
{ |
| 483 |
std::error_code ec{}; |
| 484 |
|
| 485 |
std::string ii{data.data(), data.length()}; |
| 486 |
|
| 487 |
EC_PKEY_uptr pkey{load_key(key, ec), ev_pkey_deletor}; |
| 488 |
if (ec) return { std::string{}, ec }; |
| 489 |
|
| 490 |
//TODO: Use stack string here ? |
| 491 |
std::string sign = evp_digest(pkey.get(), data, ec); |
| 492 |
|
| 493 |
if (ec) return { std::string{}, ec }; |
| 494 |
|
| 495 |
if (Hasher::type == EVP_PKEY_EC) { |
| 496 |
sign = public_key_ser(pkey.get(), sign, ec); |
| 497 |
} |
| 498 |
|
| 499 |
return { std::move(sign), ec }; |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
*/ |
| 504 |
static verify_result_t |
| 505 |
verify(const jwt::string_view key, const jwt::string_view head, const jwt::string_view sign); |
| 506 |
|
| 507 |
private: |
| 508 |
|
| 509 |
/*! |
| 510 |
*/ |
| 511 |
static EVP_PKEY* load_key(const jwt::string_view key, std::error_code& ec); |
| 512 |
|
| 513 |
/*! |
| 514 |
*/ |
| 515 |
static std::string evp_digest(EVP_PKEY* pkey, const jwt::string_view data, std::error_code& ec); |
| 516 |
|
| 517 |
/*! |
| 518 |
*/ |
| 519 |
static std::string public_key_ser(EVP_PKEY* pkey, jwt::string_view sign, std::error_code& ec); |
| 520 |
|
| 521 |
#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L |
| 522 |
|
| 523 |
|
| 524 |
//ATTN: Below 2 functions |
| 525 |
//are Taken from https://github.com/nginnever/zogminer/issues/39 |
| 526 |
|
| 527 |
/** |
| 528 |
*/ |
| 529 |
static void ECDSA_SIG_get0(const ECDSA_SIG* sig, const BIGNUM** pr, const BIGNUM** ps) |
| 530 |
{ |
| 531 |
if (pr != nullptr) *pr = sig->r; |
| 532 |
if (ps != nullptr) *ps = sig->s; |
| 533 |
}; |
| 534 |
|
| 535 |
/** |
| 536 |
*/ |
| 537 |
static int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s) |
| 538 |
{ |
| 539 |
if (r == nullptr || s == nullptr) return 0; |
| 540 |
|
| 541 |
BN_clear_free(sig->r); |
| 542 |
BN_clear_free(sig->s); |
| 543 |
|
| 544 |
sig->r = r; |
| 545 |
sig->s = s; |
| 546 |
return 1; |
| 547 |
} |
| 548 |
|
| 549 |
#endif |
| 550 |
}; |
| 551 |
|
| 552 |
} // END namespace jwt |
| 553 |
|
| 554 |
#include "jwt/impl/algorithm.ipp" |
| 555 |
|
| 556 |
|
| 557 |
#endif |