Skip to content

Commit

Permalink
Add helper for making RSA key from exponent and modulus (#307)
Browse files Browse the repository at this point in the history
* - Added creation of public key from its components
- Split verify function to signature and claims
- Added handling of claims set in json while creating a verifier

* - Added creation of public key from its components
- Split verify function to signature and claims
- Added handling of claims set in json while creating a verifier

* Fixed missing lines

* Added unit tests

* Additional changes

* delete

* refactor free

* remove testing

* align white space

* Update tests/PubKeyTest.cpp

Yes I should have removed it. Used it in testing

Co-authored-by: Chris Mc <[email protected]>

* Add support for 1.0.2

* refactor a bit

* user jwt define for SSL API that is cross-library

* add support for no base64 from jwt

* refactor a bit more

* fixup wolfssl + linters

* missing include for openssl 3

* quick draft of openssl 3 implementation

* linter

* add in a health layer of error handling and memory management

* flip function order

* fix typo

* error tests for openssl

* more all helper tests to one file

* update cmake

* update tests

* more negative test cases

* fixup old api calls depending on version

* linter

* fixup return types

* disable failing tests

* fixup copy passed

* disable more tests

* fixup broke tests

* better error code handling

* test asan with more openssl versions

* fix template render path

* fix cache conflict

running different ubuntu docker images for with different GLIB c and cxx ABIs

* add missing lib to tests

* run both openssl versions with asan

* openssl code always calls new and returns an adress in most cases

so always save the pointer to call free?

* go back to old raw2bn implementation

* Revert "openssl code always calls new and returns an adress in most cases"

This reverts commit 45e85b8.

* remove add new tests (needs to be a seperate pr)

* fix jwks verify example + run with asan+lsan

* some extra backwards compat for ssl1.1

* compat rsa keygen

* fixup copy paste

* fix typo

* linter

* wolfssl compat

* compat 1.1.0i

* more compat

since they changed the API with ever version

* linter

* fix 1.1.0

* more compat

* more compat

working on the matrix of wolfssl and libress and 1.1.0

* updated example with hardcoded values

* action to install openssl now sets ENV vars for CMake

https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/FindOpenSSL.cmake#L233

https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-environment-variable

https://gitlab.kitware.com/cmake/cmake/-/issues/24236

* update example to create a valid token

* update asan to latest compiler

* clang tidy

* fix memory leak in openssl 1.1.1

this is theoretically only because of the test code but better safe then sorry

* clang format

* improve docs

* enable one more test

* fix test impl signature

* more test for bio to std::string

* DRY up code with new helper for write_bio_to_string

* fix copy paste + spell checking

* linter

* fix typo

* fix return type

* BIO_get_mem_data is just a define

so fixup error implementation to match

* put extra tests in too many places

its only for certs not just pubkeys

---------

Co-authored-by: ZOHARGO <[email protected]>
Co-authored-by: zgofer1 <[email protected]>
  • Loading branch information
3 people authored Dec 26, 2023
1 parent ed98dc1 commit 953aab6
Show file tree
Hide file tree
Showing 9 changed files with 545 additions and 76 deletions.
3 changes: 1 addition & 2 deletions .github/actions/install/cmake/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ runs:
id: cache-cmake
uses: actions/cache@v3
with:
path: cmake-${{ inputs.version }}
key: ${{ runner.name }}-${{ runner.os }}-${{ runner.arch }}-cmake-${{ inputs.version }}
path: cmake-${{ inputs.version }}${{ runner.name }}-${{ runner.os }}-${{ runner.arch }}-${{ job.container.id }}-cmake-${{ inputs.version }}
- name: Build cmake
if: steps.cache-cmake.outputs.cache-hit != 'true'
run: |
Expand Down
2 changes: 2 additions & 0 deletions .github/actions/install/openssl/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ runs:
./config --prefix=/tmp --libdir=lib
make -j $(nproc)
sudo make -j $(nproc) install_sw
echo "OPENSSL_ROOT_DIR=/tmp" >> "$GITHUB_ENV"
echo "OpenSSL_ROOT=/tmp" >> "$GITHUB_ENV"
shell: bash
14 changes: 12 additions & 2 deletions .github/workflows/jwt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,21 @@ jobs:
./tests/fuzz/jwt-cpp-fuzz-BaseDecodeFuzz -runs=100000 ../tests/fuzz/decode-corpus
./tests/fuzz/jwt-cpp-fuzz-TokenDecodeFuzz -runs=100000 ../tests/fuzz/token-corpus
asan: ## Based on https://gist.github.com/jlblancoc/44be9d4d466f0a973b1f3808a8e56782
runs-on: ubuntu-20.04
asan: # Based on https://gist.github.com/jlblancoc/44be9d4d466f0a973b1f3808a8e56782
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
openssl:
- { tag: "openssl-3.0.5", name: "3.0.5" }
- { tag: "OpenSSL_1_1_1q", name: "1.1.1q" }
steps:
- uses: actions/checkout@v3
- uses: lukka/get-cmake@latest
- uses: ./.github/actions/install/gtest
- uses: ./.github/actions/install/openssl
with:
version: ${{ matrix.openssl.tag }}

- name: configure
run: |
Expand All @@ -76,6 +85,7 @@ jobs:
export ASAN_OPTIONS=check_initialization_order=true:detect_stack_use_after_return=true:fast_unwind_on_malloc=0
./example/rsa-create
./example/rsa-verify
./example/jwks-verify
./tests/jwt-cpp-test
ubsan:
Expand Down
103 changes: 89 additions & 14 deletions example/jwks-verify.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <jwt-cpp/jwt.h>
#include <openssl/rand.h>

int main() {
std::string raw_jwks =
Expand All @@ -9,11 +10,10 @@ int main() {
"kty": "RSA",
"use": "sig",
"x5c": [
"MIIC+DCCAeCgAwIBAgIJBIGjYW6hFpn2MA0GCSqGSIb3DQEBBQUAMCMxITAfBgNVBAMTGGN1c3RvbWVyLWRlbW9zLmF1dGgwLmNvbTAeFw0xNjExMjIyMjIyMDVaFw0zMDA4MDEyMjIyMDVaMCMxITAfBgNVBAMTGGN1c3RvbWVyLWRlbW9zLmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMnjZc5bm/eGIHq09N9HKHahM7Y31P0ul+A2wwP4lSpIwFrWHzxw88/7Dwk9QMc+orGXX95R6av4GF+Es/nG3uK45ooMVMa/hYCh0Mtx3gnSuoTavQEkLzCvSwTqVwzZ+5noukWVqJuMKNwjL77GNcPLY7Xy2/skMCT5bR8UoWaufooQvYq6SyPcRAU4BtdquZRiBT4U5f+4pwNTxSvey7ki50yc1tG49Per/0zA4O6Tlpv8x7Red6m1bCNHt7+Z5nSl3RX/QYyAEUX1a28VcYmR41Os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o9HMwLVDn7E6q
+grQSrtFfSnickR6i3XrDlspd/khcQ==
-----END PRIVATE KEY-----)";

// https://stackoverflow.com/a/30138974
unsigned char nonce[24];
RAND_bytes(nonce, sizeof(nonce));
std::string jti =
jwt::base::encode<jwt::alphabet::base64url>(std::string{reinterpret_cast<const char*>(nonce), sizeof(nonce)});

std::string token = jwt::create()
.set_issuer("auth0")
.set_type("JWT")
.set_id(jti)
.set_key_id("internal-gateway-jwt.api.sc.net")
.set_subject("jwt-cpp.example.localhost")
.set_issued_at(std::chrono::system_clock::now())
.set_expires_at(std::chrono::system_clock::now() + std::chrono::seconds{36000})
.set_payload_claim("sample", jwt::claim(std::string{"test"}))
.sign(jwt::algorithm::rs256("", pem_priv_key, "", ""));

auto decoded_jwt = jwt::decode(token);
auto jwks = jwt::parse_jwks(raw_jwks);
Expand All @@ -46,12 +105,28 @@ int main() {
auto x5c = jwk.get_x5c_key_value();

if (!x5c.empty() && !issuer.empty()) {
std::cout << "Verifying with 'x5c' key" << std::endl;
auto verifier =
jwt::verify()
.allow_algorithm(jwt::algorithm::rs256(jwt::helper::convert_base64_der_to_pem(x5c), "", "", ""))
.with_issuer(issuer)
.with_id(jti)
.leeway(60UL); // value in seconds, add some to compensate timeout

verifier.verify(decoded_jwt);
}
// else if the optional 'x5c' was not present
{
std::cout << "Verifying with RSA components" << std::endl;
const auto modulus = jwk.get_jwk_claim("n").as_string();
const auto exponent = jwk.get_jwk_claim("e").as_string();
auto verifier = jwt::verify()
.allow_algorithm(jwt::algorithm::rs256(
jwt::helper::create_public_key_from_rsa_components(modulus, exponent)))
.with_issuer(issuer)
.with_id(jti)
.leeway(60UL); // value in seconds, add some to compensate timeout

verifier.verify(decoded_jwt);
}
}
Loading

0 comments on commit 953aab6

Please sign in to comment.