| 1 | #include <hpke/base64.h> |
| 2 | #include <hpke/signature.h> |
| 3 | #include <hpke/userinfo_vc.h> |
| 4 | #include <dpp/json.h> |
| 5 | #include <tls/compat.h> |
| 6 | |
| 7 | using nlohmann::json; |
| 8 | |
| 9 | namespace mlspp::hpke { |
| 10 | |
| 11 | static const std::string name_attr = "name"; |
| 12 | static const std::string sub_attr = "sub"; |
| 13 | static const std::string given_name_attr = "given_name"; |
| 14 | static const std::string family_name_attr = "family_name"; |
| 15 | static const std::string middle_name_attr = "middle_name"; |
| 16 | static const std::string nickname_attr = "nickname"; |
| 17 | static const std::string preferred_username_attr = "preferred_username"; |
| 18 | static const std::string profile_attr = "profile"; |
| 19 | static const std::string picture_attr = "picture"; |
| 20 | static const std::string website_attr = "website"; |
| 21 | static const std::string email_attr = "email"; |
| 22 | static const std::string email_verified_attr = "email_verified"; |
| 23 | static const std::string gender_attr = "gender"; |
| 24 | static const std::string birthdate_attr = "birthdate"; |
| 25 | static const std::string zoneinfo_attr = "zoneinfo"; |
| 26 | static const std::string locale_attr = "locale"; |
| 27 | static const std::string phone_number_attr = "phone_number"; |
| 28 | static const std::string phone_number_verified_attr = "phone_number_verified"; |
| 29 | static const std::string address_attr = "address"; |
| 30 | static const std::string address_formatted_attr = "formatted"; |
| 31 | static const std::string address_street_address_attr = "street_address"; |
| 32 | static const std::string address_locality_attr = "locality"; |
| 33 | static const std::string address_region_attr = "region"; |
| 34 | static const std::string address_postal_code_attr = "postal_code"; |
| 35 | static const std::string address_country_attr = "country"; |
| 36 | static const std::string updated_at_attr = "updated_at"; |
| 37 | |
| 38 | template<typename T> |
| 39 | static std::optional<T> |
| 40 | get_optional(const json& json_object, const std::string& field_name) |
| 41 | { |
| 42 | if (!json_object.contains(field_name)) { |
| 43 | return std::nullopt; |
| 44 | } |
| 45 | |
| 46 | return { json_object.at(field_name).get<T>() }; |
| 47 | } |
| 48 | |
| 49 | /// |
| 50 | /// ParsedCredential |
| 51 | /// |
| 52 | static const Signature& |
| 53 | signature_from_alg(const std::string& alg) |
| 54 | { |
| 55 | static const auto alg_sig_map = std::map<std::string, const Signature&> |
| 56 | { |
| 57 | { "ES256", Signature::get<Signature::ID::P256_SHA256>() }, |
| 58 | { "ES384", Signature::get<Signature::ID::P384_SHA384>() }, |
| 59 | { "ES512", Signature::get<Signature::ID::P521_SHA512>() }, |
| 60 | { "Ed25519", Signature::get<Signature::ID::Ed25519>() }, |
| 61 | #if !defined(WITH_BORINGSSL) |
| 62 | { "Ed448", Signature::get<Signature::ID::Ed448>() }, |
| 63 | #endif |
| 64 | { "RS256", Signature::get<Signature::ID::RSA_SHA256>() }, |
| 65 | { "RS384", Signature::get<Signature::ID::RSA_SHA384>() }, |
| 66 | { "RS512", Signature::get<Signature::ID::RSA_SHA512>() }, |
| 67 | }; |
| 68 | |
| 69 | return alg_sig_map.at(alg); |
| 70 | } |
| 71 | |
| 72 | static std::chrono::system_clock::time_point |
| 73 | epoch_time(int64_t seconds_since_epoch) |
| 74 | { |
| 75 | const auto delta = std::chrono::seconds(seconds_since_epoch); |
| 76 | return std::chrono::system_clock::time_point(delta); |
| 77 | } |
| 78 | |
| 79 | static bool |
| 80 | is_ecdsa(const Signature& sig) |
| 81 | { |
| 82 | return sig.id == Signature::ID::P256_SHA256 || |
| 83 | sig.id == Signature::ID::P384_SHA384 || |
| 84 | sig.id == Signature::ID::P521_SHA512; |
| 85 | } |
| 86 | |
| 87 | // OpenSSL expects ECDSA signatures to be in DER form. JWS provides the |
| 88 | // signature in raw R||S form. So we need to do some manual DER encoding. |
| 89 | static bytes |
| 90 | jws_to_der_sig(const bytes& jws_sig) |
| 91 | { |
| 92 | // Inputs that are too large will result in invalid DER encodings with this |
| 93 | // code. At this size, the combination of the DER integer headers and the |
| 94 | // integer data will overflow the one-byte DER struct length. |
| 95 | static const auto max_sig_size = size_t(250); |
| 96 | if (jws_sig.size() > max_sig_size) { |
| 97 | throw std::runtime_error("JWS signature too large"); |
| 98 | } |
| 99 | |
| 100 | if (jws_sig.size() % 2 != 0) { |
| 101 | throw std::runtime_error("Malformed JWS signature"); |
| 102 | } |
| 103 | |
| 104 | const auto int_size = jws_sig.size() / 2; |
| 105 | const auto jws_sig_cut = |
| 106 | jws_sig.begin() + static_cast<std::ptrdiff_t>(int_size); |
| 107 | |
| 108 | // Compute the encoded size of R and S integer data, adding a zero byte if |
| 109 | // needed to clear the sign bit |
| 110 | const auto r_big = (jws_sig.at(0) >= 0x80); |
| 111 | const auto s_big = (jws_sig.at(int_size) >= 0x80); |
| 112 | |
| 113 | const auto r_size = int_size + (r_big ? 1 : 0); |
| 114 | const auto s_size = int_size + (s_big ? 1 : 0); |
| 115 | |
| 116 | // Compute the size of the DER-encoded signature |
| 117 | static const auto int_header_size = 2; |
| 118 | const auto r_int_size = int_header_size + r_size; |
| 119 | const auto s_int_size = int_header_size + s_size; |
| 120 | |
| 121 | const auto content_size = r_int_size + s_int_size; |
| 122 | const auto content_big = (content_size > 0x80); |
| 123 | |
| 124 | auto der_header_size = 2 + (content_big ? 1 : 0); |
| 125 | const auto der_size = der_header_size + content_size; |
| 126 | |
| 127 | // Allocate the DER buffer |
| 128 | auto der = bytes(der_size, 0); |
| 129 | |
| 130 | // Write the header |
| 131 | der.at(0) = 0x30; |
| 132 | if (content_big) { |
| 133 | der.at(1) = 0x81; |
| 134 | der.at(2) = static_cast<uint8_t>(content_size); |
| 135 | } else { |
| 136 | der.at(1) = static_cast<uint8_t>(content_size); |
| 137 | } |
| 138 | |
| 139 | // Write R, virtually padding with a zero byte if needed |
| 140 | const auto r_start = der_header_size; |
| 141 | const auto r_data_start = r_start + int_header_size + (r_big ? 1 : 0); |
| 142 | const auto r_data_begin = |
| 143 | der.begin() + static_cast<std::ptrdiff_t>(r_data_start); |
| 144 | |
| 145 | der.at(r_start) = 0x02; |
| 146 | der.at(r_start + 1) = static_cast<uint8_t>(r_size); |
| 147 | std::copy(jws_sig.begin(), jws_sig_cut, r_data_begin); |
| 148 | |
| 149 | // Write S, virtually padding with a zero byte if needed |
| 150 | const auto s_start = der_header_size + r_int_size; |
| 151 | const auto s_data_start = s_start + int_header_size + (s_big ? 1 : 0); |
| 152 | const auto s_data_begin = |
| 153 | der.begin() + static_cast<std::ptrdiff_t>(s_data_start); |
| 154 | |
| 155 | der.at(s_start) = 0x02; |
| 156 | der.at(s_start + 1) = static_cast<uint8_t>(s_size); |
| 157 | std::copy(jws_sig_cut, jws_sig.end(), s_data_begin); |
| 158 | |
| 159 | return der; |
| 160 | } |
| 161 | |
| 162 | struct UserInfoVC::ParsedCredential |
| 163 | { |
| 164 | // Header fields |
| 165 | const Signature& signature_algorithm; // `alg` |
| 166 | std::optional<std::string> key_id; // `kid` |
| 167 | |
| 168 | // Top-level Payload fields |
| 169 | std::string issuer; // `iss` |
| 170 | std::chrono::system_clock::time_point not_before; // `nbf` |
| 171 | std::chrono::system_clock::time_point not_after; // `exp` |
| 172 | |
| 173 | // Credential subject fields |
| 174 | UserInfoClaims credential_subject; |
| 175 | Signature::PublicJWK public_key; |
| 176 | |
| 177 | // Signature verification information |
| 178 | bytes to_be_signed; |
| 179 | bytes signature; |
| 180 | |
| 181 | ParsedCredential(const Signature& signature_algorithm_in, |
| 182 | std::optional<std::string> key_id_in, |
| 183 | std::string issuer_in, |
| 184 | std::chrono::system_clock::time_point not_before_in, |
| 185 | std::chrono::system_clock::time_point not_after_in, |
| 186 | UserInfoClaims credential_subject_in, |
| 187 | Signature::PublicJWK&& public_key_in, |
| 188 | bytes to_be_signed_in, |
| 189 | bytes signature_in) |
| 190 | : signature_algorithm(signature_algorithm_in) |
| 191 | , key_id(std::move(key_id_in)) |
| 192 | , issuer(std::move(issuer_in)) |
| 193 | , not_before(not_before_in) |
| 194 | , not_after(not_after_in) |
| 195 | , credential_subject(std::move(credential_subject_in)) |
| 196 | , public_key(std::move(public_key_in)) |
| 197 | , to_be_signed(std::move(to_be_signed_in)) |
| 198 | , signature(std::move(signature_in)) |
| 199 | { |
| 200 | } |
| 201 | |
| 202 | static std::shared_ptr<ParsedCredential> parse(const std::string& jwt) |
| 203 | { |
| 204 | // Split the JWT into its header, payload, and signature |
| 205 | const auto first_dot = jwt.find_first_of('.'); |
| 206 | const auto last_dot = jwt.find_last_of('.'); |
| 207 | if (first_dot == std::string::npos || last_dot == std::string::npos || |
| 208 | first_dot == last_dot || last_dot > jwt.length() - 2) { |
| 209 | throw std::runtime_error("malformed JWT; not enough '.' characters"); |
| 210 | } |
| 211 | |
| 212 | const auto header_b64 = jwt.substr(0, first_dot); |
| 213 | const auto payload_b64 = |
| 214 | jwt.substr(first_dot + 1, last_dot - first_dot - 1); |
| 215 | const auto signature_b64 = jwt.substr(last_dot + 1); |
| 216 | |
| 217 | // Parse the components |
| 218 | const auto header = json::parse(to_ascii(from_base64url(header_b64))); |
| 219 | const auto payload = json::parse(to_ascii(from_base64url(payload_b64))); |
| 220 | |
| 221 | // Prepare the validation inputs |
| 222 | const auto hdr = header.at("alg"); |
| 223 | const auto& sig = signature_from_alg(hdr); |
| 224 | const auto to_be_signed = from_ascii(header_b64 + "." + payload_b64); |
| 225 | auto signature = from_base64url(signature_b64); |
| 226 | if (is_ecdsa(sig)) { |
| 227 | signature = jws_to_der_sig(signature); |
| 228 | } |
| 229 | |
| 230 | auto kid = std::optional<std::string>{}; |
| 231 | if (header.contains("kid")) { |
| 232 | kid = header.at("kid").get<std::string>(); |
| 233 | } |
| 234 | |
| 235 | // Verify the VC parts |
| 236 | const auto& vc = payload.at("vc"); |
| 237 | |
| 238 | static const auto context = |
| 239 | std::vector<std::string>{ { "https://www.w3.org/2018/credentials/v1" } }; |
| 240 | const auto vc_context = vc.at("@context").get<std::vector<std::string>>(); |
| 241 | if (vc_context != context) { |
| 242 | throw std::runtime_error("malformed VC: incorrect context value"); |
| 243 | } |
| 244 | |
| 245 | static const auto type = std::vector<std::string>{ |
| 246 | "VerifiableCredential", |
| 247 | "UserInfoCredential", |
| 248 | }; |
| 249 | if (vc.at("type") != type) { |
| 250 | throw std::runtime_error("malformed VC: incorrect type value"); |
| 251 | } |
| 252 | |
| 253 | // Parse the subject public key |
| 254 | static const std::string did_jwk_prefix = "did:jwk:"; |
| 255 | const auto id = vc.at("credentialSubject").at("id").get<std::string>(); |
| 256 | if (id.find(did_jwk_prefix) != 0) { |
| 257 | throw std::runtime_error("malformed UserInfo VC: ID is not did:jwk"); |
| 258 | } |
| 259 | |
| 260 | const auto jwk = to_ascii(from_base64url(id.substr(did_jwk_prefix.size()))); |
| 261 | auto public_key = Signature::parse_jwk(jwk); |
| 262 | |
| 263 | // Extract the salient parts |
| 264 | return std::make_shared<ParsedCredential>( |
| 265 | sig, |
| 266 | kid, |
| 267 | |
| 268 | payload.at("iss"), |
| 269 | epoch_time(payload.at("nbf").get<int64_t>()), |
| 270 | epoch_time(payload.at("exp").get<int64_t>()), |
| 271 | |
| 272 | UserInfoClaims::from_json(vc.at("credentialSubject").dump()), |
| 273 | std::move(public_key), |
| 274 | |
| 275 | to_be_signed, |
| 276 | signature); |
| 277 | } |
| 278 | |
| 279 | bool verify(const Signature::PublicKey& issuer_key) |
| 280 | { |
| 281 | return signature_algorithm.verify(to_be_signed, signature, issuer_key); |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | /// |
| 286 | /// UserInfoClaims |
| 287 | /// |
| 288 | UserInfoClaims |
| 289 | UserInfoClaims::from_json(const std::string& cred_subject) |
| 290 | { |
| 291 | const auto& cred_subject_json = nlohmann::json::parse(cred_subject); |
| 292 | |
| 293 | std::optional<UserInfoClaimsAddress> address_opt = {}; |
| 294 | |
| 295 | if (cred_subject_json.contains(address_attr)) { |
| 296 | auto address_json = cred_subject_json.at(address_attr); |
| 297 | address_opt = { |
| 298 | get_optional<std::string>(address_json, address_formatted_attr), |
| 299 | get_optional<std::string>(address_json, address_street_address_attr), |
| 300 | get_optional<std::string>(address_json, address_locality_attr), |
| 301 | get_optional<std::string>(address_json, address_region_attr), |
| 302 | get_optional<std::string>(address_json, address_postal_code_attr), |
| 303 | get_optional<std::string>(address_json, address_country_attr) |
| 304 | }; |
| 305 | } |
| 306 | |
| 307 | return { |
| 308 | get_optional<std::string>(cred_subject_json, sub_attr), |
| 309 | get_optional<std::string>(cred_subject_json, name_attr), |
| 310 | get_optional<std::string>(cred_subject_json, given_name_attr), |
| 311 | get_optional<std::string>(cred_subject_json, family_name_attr), |
| 312 | get_optional<std::string>(cred_subject_json, middle_name_attr), |
| 313 | get_optional<std::string>(cred_subject_json, nickname_attr), |
| 314 | get_optional<std::string>(cred_subject_json, preferred_username_attr), |
| 315 | get_optional<std::string>(cred_subject_json, profile_attr), |
| 316 | get_optional<std::string>(cred_subject_json, picture_attr), |
| 317 | get_optional<std::string>(cred_subject_json, website_attr), |
| 318 | get_optional<std::string>(cred_subject_json, email_attr), |
| 319 | get_optional<bool>(cred_subject_json, email_verified_attr), |
| 320 | get_optional<std::string>(cred_subject_json, gender_attr), |
| 321 | get_optional<std::string>(cred_subject_json, birthdate_attr), |
| 322 | get_optional<std::string>(cred_subject_json, zoneinfo_attr), |
| 323 | get_optional<std::string>(cred_subject_json, locale_attr), |
| 324 | get_optional<std::string>(cred_subject_json, phone_number_attr), |
| 325 | get_optional<bool>(cred_subject_json, phone_number_verified_attr), |
| 326 | address_opt, |
| 327 | get_optional<uint64_t>(cred_subject_json, updated_at_attr), |
| 328 | }; |
| 329 | } |
| 330 | |
| 331 | /// |
| 332 | /// UserInfoVC |
| 333 | /// |
| 334 | |
| 335 | UserInfoVC::UserInfoVC(std::string jwt) |
| 336 | : parsed_cred(ParsedCredential::parse(jwt)) |
| 337 | , raw(std::move(jwt)) |
| 338 | { |
| 339 | } |
| 340 | |
| 341 | const Signature& |
| 342 | UserInfoVC::signature_algorithm() const |
| 343 | { |
| 344 | return parsed_cred->signature_algorithm; |
| 345 | } |
| 346 | |
| 347 | std::string |
| 348 | UserInfoVC::issuer() const |
| 349 | { |
| 350 | return parsed_cred->issuer; |
| 351 | } |
| 352 | |
| 353 | std::optional<std::string> |
| 354 | UserInfoVC::key_id() const |
| 355 | { |
| 356 | return parsed_cred->key_id; |
| 357 | } |
| 358 | |
| 359 | bool |
| 360 | UserInfoVC::valid_from(const Signature::PublicKey& issuer_key) const |
| 361 | { |
| 362 | return parsed_cred->verify(issuer_key); |
| 363 | } |
| 364 | |
| 365 | const std::string& |
| 366 | UserInfoVC::raw_credential() const |
| 367 | { |
| 368 | return raw; |
| 369 | } |
| 370 | |
| 371 | const UserInfoClaims& |
| 372 | UserInfoVC::subject() const |
| 373 | { |
| 374 | return parsed_cred->credential_subject; |
| 375 | } |
| 376 | |
| 377 | std::chrono::system_clock::time_point |
| 378 | UserInfoVC::not_before() const |
| 379 | { |
| 380 | return parsed_cred->not_before; |
| 381 | } |
| 382 | |
| 383 | std::chrono::system_clock::time_point |
| 384 | UserInfoVC::not_after() const |
| 385 | { |
| 386 | return parsed_cred->not_after; |
| 387 | } |
| 388 | |
| 389 | const Signature::PublicJWK& |
| 390 | UserInfoVC::public_key() const |
| 391 | { |
| 392 | return parsed_cred->public_key; |
| 393 | } |
| 394 | |
| 395 | bool |
| 396 | operator==(const UserInfoVC& lhs, const UserInfoVC& rhs) |
| 397 | { |
| 398 | return lhs.raw == rhs.raw; |
| 399 | } |
| 400 | |
| 401 | } // namespace mlspp::hpke |