-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
346 additions
and
10 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
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,202 @@ | ||
/*** | ||
This file is part of snapcast | ||
Copyright (C) 2014-2024 Johannes Pohl | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
***/ | ||
|
||
// prototype/interface header file | ||
#include "jwt.hpp" | ||
|
||
// local headers | ||
#include "common/aixlog.hpp" | ||
#include "common/base64.h" | ||
#include "common/utils/string_utils.hpp" | ||
#include <sys/types.h> | ||
|
||
// 3rd party headers | ||
#include <openssl/aes.h> | ||
#include <openssl/bio.h> | ||
#include <openssl/err.h> | ||
#include <openssl/evp.h> | ||
#include <openssl/pem.h> | ||
#include <openssl/rsa.h> | ||
#include <openssl/ssl.h> | ||
#include <vector> | ||
|
||
// standard headers | ||
|
||
|
||
static constexpr auto LOG_TAG = "JWT"; | ||
|
||
|
||
|
||
namespace | ||
{ | ||
|
||
EVP_PKEY* createPrivate(const std::string& key) | ||
{ | ||
// Reads PEM information and retrieves some details | ||
BIO* keybio = BIO_new_mem_buf((void*)key.c_str(), -1); | ||
if (keybio == nullptr) | ||
{ | ||
LOG(ERROR, LOG_TAG) << "BIO_new_mem_buf failed\n"; | ||
return nullptr; | ||
} | ||
|
||
char* name = nullptr; | ||
char* header = nullptr; | ||
uint8_t* data = nullptr; | ||
int64_t datalen = 0; | ||
if (PEM_read_bio(keybio, &name, &header, &data, &datalen) == 1) | ||
{ | ||
// Copies the data pointer. D2I functions update it | ||
const auto* data_pkey = reinterpret_cast<const uint8_t*>(data); | ||
// Detects type and decodes the private key | ||
EVP_PKEY* pkey = d2i_AutoPrivateKey(nullptr, &data_pkey, datalen); | ||
if (pkey == nullptr) | ||
{ | ||
LOG(ERROR, LOG_TAG) << "d2i_AutoPrivateKey failed\n"; | ||
} | ||
|
||
// Free is only required after a PEM_bio_read successful return | ||
if (name != nullptr) | ||
OPENSSL_free(name); | ||
if (header != nullptr) | ||
OPENSSL_free(header); | ||
if (data != nullptr) | ||
OPENSSL_free(data); | ||
return pkey; | ||
} | ||
return nullptr; | ||
} | ||
|
||
|
||
bool Sign(const std::string& pem_key, const std::string& msg, std::vector<u_char>& encoded) | ||
{ | ||
auto* key = createPrivate(pem_key); | ||
EVP_MD_CTX* ctx = EVP_MD_CTX_create(); | ||
if (EVP_DigestSignInit(ctx, nullptr, EVP_sha384(), nullptr, key) <= 0) | ||
{ | ||
LOG(ERROR, LOG_TAG) << "EVP_DigestSignInit failed\n"; | ||
return false; | ||
} | ||
if (EVP_DigestSignUpdate(ctx, msg.c_str(), msg.size()) <= 0) | ||
{ | ||
LOG(ERROR, LOG_TAG) << "EVP_DigestSignUpdate failed\n"; | ||
return false; | ||
} | ||
size_t siglen; | ||
if (EVP_DigestSignFinal(ctx, nullptr, &siglen) <= 0) | ||
{ | ||
LOG(ERROR, LOG_TAG) << "EVP_DigestSignFinal failed\n"; | ||
return false; | ||
} | ||
|
||
encoded.resize(siglen); | ||
if (EVP_DigestSignFinal(ctx, encoded.data(), &siglen) <= 0) | ||
{ | ||
LOG(ERROR, LOG_TAG) << "EVP_DigestSignFinal failed\n"; | ||
return false; | ||
} | ||
EVP_MD_CTX_free(ctx); | ||
return true; | ||
} | ||
|
||
} // namespace | ||
|
||
Jwt::Jwt() | ||
{ | ||
// { | ||
// "alg": "RS256", | ||
// "typ": "JWT" | ||
// } | ||
json header = {{"alg", "RS384"}, {"typ", "JWT"}}; | ||
|
||
// { | ||
// "sub": "1234567890", | ||
// "name": "Johannes", | ||
// "admin": true, | ||
// "iat": 1516239022 | ||
// } | ||
json payload = {{"sub", "1234567890"}, {"name", "Johannes"}, {"admin", true}, {"iat", 1516239022}}; | ||
|
||
LOG(INFO, LOG_TAG) << "Header: " << header << "\n"; | ||
LOG(INFO, LOG_TAG) << "Payload: " << payload << "\n"; | ||
std::string msg = base64url_encode(header.dump()) + "." + base64url_encode(payload.dump()); | ||
LOG(INFO, LOG_TAG) << "Encoded: " << msg << "\n"; | ||
|
||
auto key = "-----BEGIN PRIVATE KEY-----\n" | ||
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCwFxHHKvV6THj1\n" | ||
"VjvZQJIW+OjIKF9MPfIN8wJTXn+4EkBJCoBy0NfKHG+FCb90YCPzvLrjL8jKcfhT\n" | ||
"/jpaStrYYjABXA1sJS9P+9cwiV9RwTxTrfOiss8F7eZdyZI9UogrPmQa7YbevmwB\n" | ||
"XeXIqKzdWQbtQsOJQgoL3vIR7qjUpVQrXPwAYQ6pc7AxS5RHFy8V2Y3sh/+i0aS9\n" | ||
"bH/276OAKZNwVIfUIcSVVbBAPvrheR2ezUVoKSumUzek/2uq3cLb5YNF4XSe1Ikv\n" | ||
"16lRSGTIQ2KflSYn3mJldFjEKL3sgADTmMhKes+TVveNr7eCvynyDCtHdiLanOKY\n" | ||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q4HJLsvJXYETf\n" | ||
"ZxjyeaD5ZCohvkn/sYsVBWG7JieuX5uTQN5xW5dcpOwcXYR7Nfmkj5jKhhh7wyin\n" | ||
"So8QRIPujG6IuvsFbF+HxFpXBWGpUJv2mBZm8PShAoGBALU/KNl1El0HpxTr3wgj\n" | ||
"YY6eU3snn0Oa5Ci3k/FMWd1QTtVsf4Mym0demxshdC75L+qzCS0m5jAo9tm0keY9\n" | ||
"A4F3VRlsuUyuAja+qDU2xMo3jnFKOIyMfN4mVSiFkqnq3eQ4xHgViyIEyr+8AbA4\n" | ||
"ajjiCZsv+OITxQ+TTHeGDsdD\n" | ||
"-----END PRIVATE KEY-----\n"; | ||
|
||
std::vector<u_char> encoded; | ||
if (Sign(key, msg, encoded)) | ||
{ | ||
std::string signature = base64url_encode(encoded.data(), encoded.size()); | ||
LOG(INFO, LOG_TAG) << "Signature: " << signature << "\n"; | ||
} | ||
else | ||
{ | ||
LOG(ERROR, LOG_TAG) << "Failed to sign\n"; | ||
} | ||
} | ||
|
||
|
||
bool Jwt::decode(const std::string& token) | ||
{ | ||
std::vector<std::string> parts = utils::string::split(token, '.'); | ||
if (parts.size() != 3) | ||
{ | ||
return false; | ||
} | ||
|
||
LOG(INFO, LOG_TAG) << "Header: " << base64url_decode(parts[0]) << "\n"; | ||
LOG(INFO, LOG_TAG) << "Payload: " << base64url_decode(parts[1]) << "\n"; | ||
|
||
header_ = json::parse(base64url_decode(parts[0])); | ||
payload_ = json::parse(base64url_decode(parts[1])); | ||
std::string signature = parts[2]; | ||
|
||
LOG(INFO, LOG_TAG) << "Header: " << header_ << "\n"; | ||
LOG(INFO, LOG_TAG) << "Payload: " << payload_ << "\n"; | ||
LOG(INFO, LOG_TAG) << "Signature: " << signature << "\n"; | ||
|
||
return true; | ||
} |
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,71 @@ | ||
/*** | ||
This file is part of snapcast | ||
Copyright (C) 2014-2024 Johannes Pohl | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
***/ | ||
|
||
#pragma once | ||
|
||
// local headers | ||
#include "common/json.hpp" | ||
|
||
// standard headers | ||
#include <string> | ||
|
||
|
||
/* | ||
https://datatracker.ietf.org/doc/html/rfc7518#section-3 | ||
+--------------+-------------------------------+--------------------+ | ||
| "alg" Param | Digital Signature or MAC | Implementation | | ||
| Value | Algorithm | Requirements | | ||
+--------------+-------------------------------+--------------------+ | ||
| HS256 | HMAC using SHA-256 | Required | | ||
| HS384 | HMAC using SHA-384 | Optional | | ||
| HS512 | HMAC using SHA-512 | Optional | | ||
| RS256 | RSASSA-PKCS1-v1_5 using | Recommended | | ||
| | SHA-256 | | | ||
| RS384 | RSASSA-PKCS1-v1_5 using | Optional | | ||
| | SHA-384 | | | ||
| RS512 | RSASSA-PKCS1-v1_5 using | Optional | | ||
| | SHA-512 | | | ||
| ES256 | ECDSA using P-256 and SHA-256 | Recommended+ | | ||
| ES384 | ECDSA using P-384 and SHA-384 | Optional | | ||
| ES512 | ECDSA using P-521 and SHA-512 | Optional | | ||
| PS256 | RSASSA-PSS using SHA-256 and | Optional | | ||
| | MGF1 with SHA-256 | | | ||
| PS384 | RSASSA-PSS using SHA-384 and | Optional | | ||
| | MGF1 with SHA-384 | | | ||
| PS512 | RSASSA-PSS using SHA-512 and | Optional | | ||
| | MGF1 with SHA-512 | | | ||
| none | No digital signature or MAC | Optional | | ||
| | performed | | | ||
+--------------+-------------------------------+--------------------+ | ||
*/ | ||
|
||
// https://techdocs.akamai.com/iot-token-access-control/docs/generate-jwt-rsa-keys | ||
using json = nlohmann::json; | ||
|
||
class Jwt | ||
{ | ||
public: | ||
Jwt(); | ||
|
||
bool decode(const std::string& token); | ||
|
||
private: | ||
json header_; | ||
json payload_; | ||
}; |
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
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
Oops, something went wrong.