Skip to content

Commit

Permalink
Add a test case for full JWK parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Barnes committed Sep 11, 2023
1 parent 099b02e commit b06484a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
6 changes: 2 additions & 4 deletions include/mls/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,6 @@ struct PublicJWK;

struct SignaturePublicKey
{
// XXX(RLB) It would be nice to wrap this return value as a struct, but that
// results in a compiler error "field has incomplete type".
static PublicJWK parse_jwk(const std::string& jwk_json);

static SignaturePublicKey from_jwk(CipherSuite suite,
const std::string& json_str);

Expand All @@ -235,6 +231,8 @@ struct PublicJWK {
SignatureScheme signature_scheme;
std::optional<std::string> key_id;
SignaturePublicKey public_key;

static PublicJWK parse(const std::string& jwk_json);
};

struct SignaturePrivateKey
Expand Down
18 changes: 9 additions & 9 deletions src/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,6 @@ SignaturePublicKey::verify(const CipherSuite& suite,
return suite.sig().verify(content, signature, *pub);
}

PublicJWK
SignaturePublicKey::parse_jwk(const std::string& jwk_json)
{
const auto parsed = Signature::parse_jwk(jwk_json);
const auto scheme = tls_signature_scheme(parsed.sig.id);
const auto pub_data = parsed.sig.serialize(*parsed.key);
return { scheme, parsed.key_id, { pub_data } };
}

SignaturePublicKey
SignaturePublicKey::from_jwk(CipherSuite suite, const std::string& json_str)
{
Expand All @@ -409,6 +400,15 @@ SignaturePublicKey::to_jwk(CipherSuite suite) const
return suite.sig().export_jwk(*pub);
}

PublicJWK
PublicJWK::parse(const std::string& jwk_json)
{
const auto parsed = Signature::parse_jwk(jwk_json);
const auto scheme = tls_signature_scheme(parsed.sig.id);
const auto pub_data = parsed.sig.serialize(*parsed.key);
return { scheme, parsed.key_id, { pub_data } };
}

SignaturePrivateKey
SignaturePrivateKey::generate(CipherSuite suite)
{
Expand Down
17 changes: 17 additions & 0 deletions test/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ TEST_CASE("Signature Key JWK Import/Export")
const auto decoded_pub = SignaturePublicKey::from_jwk(suite, encoded_pub);
REQUIRE(decoded_pub == pub);
}

// Test PublicJWK parsing
const auto full_jwk = R"({
"kty": "OKP",
"crv": "Ed25519",
"kid": "059fc2ee-5ef6-456a-91d8-49c422c772b2",
"x": "miljqilAZV2yFkqIBhrxhvt2wIMvPtkNEFzuziEGOtI"
})";

const auto known_scheme = SignatureScheme::ed25519;
const auto known_key_id = std::string("059fc2ee-5ef6-456a-91d8-49c422c772b2");
const auto knwon_pub_data = from_hex("9a2963aa2940655db2164a88061af186fb76c0832f3ed90d105ceece21063ad2");

const auto jwk = PublicJWK::parse(full_jwk);
REQUIRE(jwk.signature_scheme == known_scheme);
REQUIRE(jwk.key_id == known_key_id);
REQUIRE(jwk.public_key == SignaturePublicKey{ knwon_pub_data });
}

TEST_CASE("Crypto Interop")
Expand Down

0 comments on commit b06484a

Please sign in to comment.