| 1 | #include <hpke/base64.h> |
| 2 | #include <hpke/digest.h> |
| 3 | #include <hpke/signature.h> |
| 4 | #include <string> |
| 5 | |
| 6 | #include "dhkem.h" |
| 7 | #include "rsa.h" |
| 8 | |
| 9 | #include <dpp/json.h> |
| 10 | #include <openssl/bio.h> |
| 11 | #include <openssl/bn.h> |
| 12 | #include <openssl/ec.h> |
| 13 | #include <openssl/evp.h> |
| 14 | #include <openssl/rsa.h> |
| 15 | |
| 16 | using nlohmann::json; |
| 17 | |
| 18 | namespace mlspp::hpke { |
| 19 | |
| 20 | struct GroupSignature : public Signature |
| 21 | { |
| 22 | struct PrivateKey : public Signature::PrivateKey |
| 23 | { |
| 24 | explicit PrivateKey(Group::PrivateKey* group_priv_in) |
| 25 | : group_priv(group_priv_in) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | std::unique_ptr<Signature::PublicKey> public_key() const override |
| 30 | { |
| 31 | return group_priv->public_key(); |
| 32 | } |
| 33 | |
| 34 | std::unique_ptr<Group::PrivateKey> group_priv; |
| 35 | }; |
| 36 | |
| 37 | static Signature::ID group_to_sig(Group::ID group_id) |
| 38 | { |
| 39 | switch (group_id) { |
| 40 | case Group::ID::P256: |
| 41 | return Signature::ID::P256_SHA256; |
| 42 | case Group::ID::P384: |
| 43 | return Signature::ID::P384_SHA384; |
| 44 | case Group::ID::P521: |
| 45 | return Signature::ID::P521_SHA512; |
| 46 | case Group::ID::Ed25519: |
| 47 | return Signature::ID::Ed25519; |
| 48 | #if !defined(WITH_BORINGSSL) |
| 49 | case Group::ID::Ed448: |
| 50 | return Signature::ID::Ed448; |
| 51 | #endif |
| 52 | default: |
| 53 | throw std::runtime_error("Unsupported group"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | explicit GroupSignature(const Group& group_in) |
| 58 | : Signature(group_to_sig(group_in.id)) |
| 59 | , group(group_in) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | std::unique_ptr<Signature::PrivateKey> generate_key_pair() const override |
| 64 | { |
| 65 | return std::make_unique<PrivateKey>(group.generate_key_pair().release()); |
| 66 | } |
| 67 | |
| 68 | std::unique_ptr<Signature::PrivateKey> derive_key_pair( |
| 69 | const bytes& ikm) const override |
| 70 | { |
| 71 | return std::make_unique<PrivateKey>( |
| 72 | group.derive_key_pair({}, ikm).release()); |
| 73 | } |
| 74 | |
| 75 | bytes serialize(const Signature::PublicKey& pk) const override |
| 76 | { |
| 77 | const auto& rpk = dynamic_cast<const Group::PublicKey&>(pk); |
| 78 | return group.serialize(rpk); |
| 79 | } |
| 80 | |
| 81 | std::unique_ptr<Signature::PublicKey> deserialize( |
| 82 | const bytes& enc) const override |
| 83 | { |
| 84 | return group.deserialize(enc); |
| 85 | } |
| 86 | |
| 87 | bytes serialize_private(const Signature::PrivateKey& sk) const override |
| 88 | { |
| 89 | const auto& rsk = dynamic_cast<const PrivateKey&>(sk); |
| 90 | return group.serialize_private(*rsk.group_priv); |
| 91 | } |
| 92 | |
| 93 | std::unique_ptr<Signature::PrivateKey> deserialize_private( |
| 94 | const bytes& skm) const override |
| 95 | { |
| 96 | return std::make_unique<PrivateKey>( |
| 97 | group.deserialize_private(skm).release()); |
| 98 | } |
| 99 | |
| 100 | bytes sign(const bytes& data, const Signature::PrivateKey& sk) const override |
| 101 | { |
| 102 | const auto& rsk = dynamic_cast<const PrivateKey&>(sk); |
| 103 | return group.sign(data, *rsk.group_priv); |
| 104 | } |
| 105 | |
| 106 | bool verify(const bytes& data, |
| 107 | const bytes& sig, |
| 108 | const Signature::PublicKey& pk) const override |
| 109 | { |
| 110 | const auto& rpk = dynamic_cast<const Group::PublicKey&>(pk); |
| 111 | return group.verify(data, sig, rpk); |
| 112 | } |
| 113 | |
| 114 | std::unique_ptr<Signature::PrivateKey> import_jwk_private( |
| 115 | const std::string& jwk_json) const override |
| 116 | { |
| 117 | const auto jwk = validate_jwk_json(jwk_json, true); |
| 118 | |
| 119 | const auto d = from_base64url(jwk.at("d")); |
| 120 | auto gsk = group.deserialize_private(d); |
| 121 | |
| 122 | return std::make_unique<PrivateKey>(gsk.release()); |
| 123 | } |
| 124 | |
| 125 | std::unique_ptr<Signature::PublicKey> import_jwk( |
| 126 | const std::string& jwk_json) const override |
| 127 | { |
| 128 | const auto jwk = validate_jwk_json(jwk_json, false); |
| 129 | |
| 130 | const auto x = from_base64url(jwk.at("x")); |
| 131 | auto y = bytes{}; |
| 132 | if (jwk.contains("y")) { |
| 133 | y = from_base64url(jwk.at("y")); |
| 134 | } |
| 135 | |
| 136 | return group.public_key_from_coordinates(x, y); |
| 137 | } |
| 138 | |
| 139 | std::string export_jwk(const Signature::PublicKey& pk) const override |
| 140 | { |
| 141 | const auto& gpk = dynamic_cast<const Group::PublicKey&>(pk); |
| 142 | const auto jwk_json = export_jwk_json(gpk); |
| 143 | return jwk_json.dump(); |
| 144 | } |
| 145 | |
| 146 | std::string export_jwk_private(const Signature::PrivateKey& sk) const override |
| 147 | { |
| 148 | const auto& gssk = dynamic_cast<const GroupSignature::PrivateKey&>(sk); |
| 149 | const auto& gsk = gssk.group_priv; |
| 150 | const auto gpk = gsk->public_key(); |
| 151 | |
| 152 | auto jwk_json = export_jwk_json(*gpk); |
| 153 | |
| 154 | // encode the private key |
| 155 | const auto enc = serialize_private(sk); |
| 156 | jwk_json.emplace("d", to_base64url(enc)); |
| 157 | |
| 158 | return jwk_json.dump(); |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | const Group& group; |
| 163 | |
| 164 | json validate_jwk_json(const std::string& jwk_json, bool private_key) const |
| 165 | { |
| 166 | json jwk = json::parse(jwk_json); |
| 167 | |
| 168 | if (jwk.empty() || !jwk.contains("kty") || !jwk.contains("crv") || |
| 169 | !jwk.contains("x") || (private_key && !jwk.contains("d"))) { |
| 170 | throw std::runtime_error("malformed JWK"); |
| 171 | } |
| 172 | |
| 173 | if (jwk.at("kty") != group.jwk_key_type) { |
| 174 | throw std::runtime_error("invalid JWK key type"); |
| 175 | } |
| 176 | |
| 177 | if (jwk.at("crv") != group.jwk_curve_name) { |
| 178 | throw std::runtime_error("invalid JWK curve"); |
| 179 | } |
| 180 | |
| 181 | return jwk; |
| 182 | } |
| 183 | |
| 184 | json export_jwk_json(const Group::PublicKey& pk) const |
| 185 | { |
| 186 | const auto [x, y] = group.coordinates(pk); |
| 187 | |
| 188 | json jwk = json::object({ |
| 189 | { "crv", group.jwk_curve_name }, |
| 190 | { "kty", group.jwk_key_type }, |
| 191 | }); |
| 192 | |
| 193 | if (group.jwk_key_type == "EC") { |
| 194 | jwk.emplace("x", to_base64url(x)); |
| 195 | jwk.emplace("y", to_base64url(y)); |
| 196 | } else if (group.jwk_key_type == "OKP") { |
| 197 | jwk.emplace("x", to_base64url(x)); |
| 198 | } else { |
| 199 | throw std::runtime_error("unknown key type"); |
| 200 | } |
| 201 | |
| 202 | return jwk; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | template<> |
| 207 | const Signature& |
| 208 | Signature::get<Signature::ID::P256_SHA256>() |
| 209 | { |
| 210 | static const auto instance = GroupSignature(Group::get<Group::ID::P256>()); |
| 211 | return instance; |
| 212 | } |
| 213 | |
| 214 | template<> |
| 215 | const Signature& |
| 216 | Signature::get<Signature::ID::P384_SHA384>() |
| 217 | { |
| 218 | static const auto instance = GroupSignature(Group::get<Group::ID::P384>()); |
| 219 | return instance; |
| 220 | } |
| 221 | |
| 222 | template<> |
| 223 | const Signature& |
| 224 | Signature::get<Signature::ID::P521_SHA512>() |
| 225 | { |
| 226 | static const auto instance = GroupSignature(Group::get<Group::ID::P521>()); |
| 227 | return instance; |
| 228 | } |
| 229 | |
| 230 | template<> |
| 231 | const Signature& |
| 232 | Signature::get<Signature::ID::Ed25519>() |
| 233 | { |
| 234 | static const auto instance = GroupSignature(Group::get<Group::ID::Ed25519>()); |
| 235 | return instance; |
| 236 | } |
| 237 | |
| 238 | #if !defined(WITH_BORINGSSL) |
| 239 | template<> |
| 240 | const Signature& |
| 241 | Signature::get<Signature::ID::Ed448>() |
| 242 | { |
| 243 | static const auto instance = GroupSignature(Group::get<Group::ID::Ed448>()); |
| 244 | return instance; |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | template<> |
| 249 | const Signature& |
| 250 | Signature::get<Signature::ID::RSA_SHA256>() |
| 251 | { |
| 252 | static const auto instance = RSASignature(Digest::ID::SHA256); |
| 253 | return instance; |
| 254 | } |
| 255 | |
| 256 | template<> |
| 257 | const Signature& |
| 258 | Signature::get<Signature::ID::RSA_SHA384>() |
| 259 | { |
| 260 | static const auto instance = RSASignature(Digest::ID::SHA384); |
| 261 | return instance; |
| 262 | } |
| 263 | |
| 264 | template<> |
| 265 | const Signature& |
| 266 | Signature::get<Signature::ID::RSA_SHA512>() |
| 267 | { |
| 268 | static const auto instance = RSASignature(Digest::ID::SHA512); |
| 269 | return instance; |
| 270 | } |
| 271 | |
| 272 | Signature::Signature(Signature::ID id_in) |
| 273 | : id(id_in) |
| 274 | { |
| 275 | } |
| 276 | |
| 277 | std::unique_ptr<Signature::PrivateKey> |
| 278 | Signature::generate_rsa(size_t bits) |
| 279 | { |
| 280 | return RSASignature::generate_key_pair(bits); |
| 281 | } |
| 282 | |
| 283 | static const Signature& |
| 284 | sig_from_jwk(const std::string& jwk_json) |
| 285 | { |
| 286 | using KeyTypeAndCurve = std::tuple<std::string, std::string>; |
| 287 | static const auto alg_sig_map = std::map<KeyTypeAndCurve, const Signature&> |
| 288 | { |
| 289 | { { "EC", "P-256" }, Signature::get<Signature::ID::P256_SHA256>() }, |
| 290 | { { "EC", "P-384" }, Signature::get<Signature::ID::P384_SHA384>() }, |
| 291 | { { "EC", "P-512" }, Signature::get<Signature::ID::P521_SHA512>() }, |
| 292 | { { "OKP", "Ed25519" }, Signature::get<Signature::ID::Ed25519>() }, |
| 293 | #if !defined(WITH_BORINGSSL) |
| 294 | { { "OKP", "Ed448" }, Signature::get<Signature::ID::Ed448>() }, |
| 295 | #endif |
| 296 | // TODO(RLB): RSA |
| 297 | }; |
| 298 | |
| 299 | const auto jwk = json::parse(jwk_json); |
| 300 | const auto& kty = jwk.at("kty"); |
| 301 | |
| 302 | auto crv = std::string(""); |
| 303 | if (jwk.contains("crv")) { |
| 304 | crv = jwk.at("crv"); |
| 305 | } |
| 306 | |
| 307 | const auto key = KeyTypeAndCurve{ kty, crv }; |
| 308 | return alg_sig_map.at(key); |
| 309 | } |
| 310 | |
| 311 | Signature::PrivateJWK |
| 312 | Signature::parse_jwk_private(const std::string& jwk_json) |
| 313 | { |
| 314 | // XXX(RLB): This JSON-parses the JWK twice. I'm assuming that this is a less |
| 315 | // bad cost than changing the import_jwk method signature to take `json`. |
| 316 | const auto& sig = sig_from_jwk(jwk_json); |
| 317 | const auto jwk = json::parse(jwk_json); |
| 318 | auto priv = sig.import_jwk_private(jwk_json); |
| 319 | |
| 320 | auto kid = std::optional<std::string>{}; |
| 321 | if (jwk.contains("kid")) { |
| 322 | kid = jwk.at("kid").get<std::string>(); |
| 323 | } |
| 324 | |
| 325 | return { sig, kid, std::move(priv) }; |
| 326 | } |
| 327 | |
| 328 | Signature::PublicJWK |
| 329 | Signature::parse_jwk(const std::string& jwk_json) |
| 330 | { |
| 331 | // XXX(RLB): Same double-parsing comment as with `parse_jwk_private` |
| 332 | const auto& sig = sig_from_jwk(jwk_json); |
| 333 | const auto jwk = json::parse(jwk_json); |
| 334 | auto pub = sig.import_jwk(jwk_json); |
| 335 | |
| 336 | auto kid = std::optional<std::string>{}; |
| 337 | if (jwk.contains("kid")) { |
| 338 | kid = jwk.at("kid").get<std::string>(); |
| 339 | } |
| 340 | |
| 341 | return { sig, kid, std::move(pub) }; |
| 342 | } |
| 343 | |
| 344 | } // namespace mlspp::hpke |