|
15 | 15 |
|
16 | 16 | namespace
|
17 | 17 | {
|
18 |
| - std::vector<uint8_t> try_parse_jwk(const ccf::crypto::JsonWebKeyExtended& jwk) |
| 18 | + std::vector<uint8_t> try_parse_raw_rsa(const ccf::crypto::JsonWebKeyData& jwk) |
19 | 19 | {
|
20 |
| - const auto& kid = jwk.kid.value(); |
21 |
| - if ( |
22 |
| - jwk.e.has_value() && !jwk.e->empty() && jwk.n.has_value() && |
23 |
| - !jwk.n->empty()) |
| 20 | + if (!jwk.e || jwk.e->empty() || !jwk.n || jwk.n->empty()) |
24 | 21 | {
|
25 |
| - std::vector<uint8_t> der; |
26 |
| - ccf::crypto::JsonWebKeyRSAPublic data; |
27 |
| - data.kty = ccf::crypto::JsonWebKeyType::RSA; |
28 |
| - data.kid = jwk.kid; |
29 |
| - data.n = jwk.n.value(); |
30 |
| - data.e = jwk.e.value(); |
31 |
| - try |
32 |
| - { |
33 |
| - const auto pubkey = ccf::crypto::make_rsa_public_key(data); |
34 |
| - return pubkey->public_key_der(); |
35 |
| - } |
36 |
| - catch (const std::invalid_argument& exc) |
37 |
| - { |
38 |
| - throw std::logic_error( |
39 |
| - fmt::format("Failed to construct RSA public key: {}", exc.what())); |
40 |
| - } |
| 22 | + return {}; |
41 | 23 | }
|
42 |
| - else if (jwk.x5c.has_value() && !jwk.x5c->empty()) |
| 24 | + |
| 25 | + std::vector<uint8_t> der; |
| 26 | + ccf::crypto::JsonWebKeyRSAPublic data; |
| 27 | + data.kty = ccf::crypto::JsonWebKeyType::RSA; |
| 28 | + data.kid = jwk.kid.value(); |
| 29 | + data.n = jwk.n.value(); |
| 30 | + data.e = jwk.e.value(); |
| 31 | + try |
43 | 32 | {
|
44 |
| - auto& der_base64 = jwk.x5c.value()[0]; |
45 |
| - ccf::Cert der; |
46 |
| - try |
47 |
| - { |
48 |
| - der = ccf::crypto::raw_from_b64(der_base64); |
49 |
| - } |
50 |
| - catch (const std::invalid_argument& e) |
51 |
| - { |
52 |
| - throw std::logic_error( |
53 |
| - fmt::format("Could not parse x5c of key id {}: {}", kid, e.what())); |
54 |
| - } |
55 |
| - try |
56 |
| - { |
57 |
| - auto verifier = ccf::crypto::make_unique_verifier(der); |
58 |
| - return verifier->public_key_der(); |
59 |
| - } |
60 |
| - catch (std::invalid_argument& exc) |
61 |
| - { |
62 |
| - throw std::logic_error(fmt::format( |
63 |
| - "JWKS kid {} has an invalid X.509 certificate: {}", kid, exc.what())); |
64 |
| - } |
| 33 | + const auto pubkey = ccf::crypto::make_rsa_public_key(data); |
| 34 | + return pubkey->public_key_der(); |
| 35 | + } |
| 36 | + catch (const std::invalid_argument& exc) |
| 37 | + { |
| 38 | + throw std::logic_error( |
| 39 | + fmt::format("Failed to construct RSA public key: {}", exc.what())); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + std::vector<uint8_t> try_parse_raw_ec(const ccf::crypto::JsonWebKeyData& jwk) |
| 44 | + { |
| 45 | + if (!jwk.x || jwk.x->empty() || !jwk.y || jwk.y->empty() || !jwk.crv) |
| 46 | + { |
| 47 | + return {}; |
| 48 | + } |
| 49 | + |
| 50 | + ccf::crypto::JsonWebKeyECPublic data; |
| 51 | + data.kty = ccf::crypto::JsonWebKeyType::EC; |
| 52 | + data.kid = jwk.kid.value(); |
| 53 | + data.crv = jwk.crv.value(); |
| 54 | + data.x = jwk.x.value(); |
| 55 | + data.y = jwk.y.value(); |
| 56 | + try |
| 57 | + { |
| 58 | + const auto pubkey = ccf::crypto::make_public_key(data); |
| 59 | + return pubkey->public_key_der(); |
| 60 | + } |
| 61 | + catch (const std::invalid_argument& exc) |
| 62 | + { |
| 63 | + throw std::logic_error( |
| 64 | + fmt::format("Failed to construct EC public key: {}", exc.what())); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + std::vector<uint8_t> try_parse_x5c(const ccf::crypto::JsonWebKeyData& jwk) |
| 69 | + { |
| 70 | + if (!jwk.x5c || jwk.x5c->empty()) |
| 71 | + { |
| 72 | + return {}; |
65 | 73 | }
|
66 |
| - else |
| 74 | + |
| 75 | + const auto& kid = jwk.kid.value(); |
| 76 | + auto& der_base64 = jwk.x5c.value()[0]; |
| 77 | + ccf::Cert der; |
| 78 | + try |
| 79 | + { |
| 80 | + der = ccf::crypto::raw_from_b64(der_base64); |
| 81 | + } |
| 82 | + catch (const std::invalid_argument& e) |
67 | 83 | {
|
68 | 84 | throw std::logic_error(
|
69 |
| - fmt::format("JWKS kid {} has neither x5c or RSA public key", kid)); |
| 85 | + fmt::format("Could not parse x5c of key id {}: {}", kid, e.what())); |
| 86 | + } |
| 87 | + try |
| 88 | + { |
| 89 | + auto verifier = ccf::crypto::make_unique_verifier(der); |
| 90 | + return verifier->public_key_der(); |
| 91 | + } |
| 92 | + catch (std::invalid_argument& exc) |
| 93 | + { |
| 94 | + throw std::logic_error(fmt::format( |
| 95 | + "JWKS kid {} has an invalid X.509 certificate: {}", kid, exc.what())); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + std::vector<uint8_t> try_parse_jwk(const ccf::crypto::JsonWebKeyData& jwk) |
| 100 | + { |
| 101 | + const auto& kid = jwk.kid.value(); |
| 102 | + auto key = try_parse_raw_rsa(jwk); |
| 103 | + if (!key.empty()) |
| 104 | + { |
| 105 | + return key; |
| 106 | + } |
| 107 | + key = try_parse_raw_ec(jwk); |
| 108 | + if (!key.empty()) |
| 109 | + { |
| 110 | + return key; |
70 | 111 | }
|
| 112 | + key = try_parse_x5c(jwk); |
| 113 | + if (!key.empty()) |
| 114 | + { |
| 115 | + return key; |
| 116 | + } |
| 117 | + |
| 118 | + throw std::logic_error( |
| 119 | + fmt::format("JWKS kid {} has neither RSA/EC public key or x5c", kid)); |
71 | 120 | }
|
72 | 121 | }
|
73 | 122 |
|
|
0 commit comments