| 1 | #include <hpke/certificate.h> |
| 2 | #include <hpke/userinfo_vc.h> |
| 3 | #include <mls/credential.h> |
| 4 | #include <tls/tls_syntax.h> |
| 5 | |
| 6 | namespace mlspp { |
| 7 | |
| 8 | /// |
| 9 | /// X509Credential |
| 10 | /// |
| 11 | |
| 12 | using mlspp::hpke::Certificate; // NOLINT(misc-unused-using-decls) |
| 13 | using mlspp::hpke::Signature; // NOLINT(misc-unused-using-decls) |
| 14 | using mlspp::hpke::UserInfoVC; // NOLINT(misc-unused-using-decls) |
| 15 | |
| 16 | static const Signature& |
| 17 | find_signature(Signature::ID id) |
| 18 | { |
| 19 | switch (id) { |
| 20 | case Signature::ID::P256_SHA256: |
| 21 | return Signature::get<Signature::ID::P256_SHA256>(); |
| 22 | case Signature::ID::P384_SHA384: |
| 23 | return Signature::get<Signature::ID::P384_SHA384>(); |
| 24 | case Signature::ID::P521_SHA512: |
| 25 | return Signature::get<Signature::ID::P521_SHA512>(); |
| 26 | case Signature::ID::Ed25519: |
| 27 | return Signature::get<Signature::ID::Ed25519>(); |
| 28 | #if !defined(WITH_BORINGSSL) |
| 29 | case Signature::ID::Ed448: |
| 30 | return Signature::get<Signature::ID::Ed448>(); |
| 31 | #endif |
| 32 | case Signature::ID::RSA_SHA256: |
| 33 | return Signature::get<Signature::ID::RSA_SHA256>(); |
| 34 | default: |
| 35 | throw InvalidParameterError("Unsupported algorithm"); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static std::vector<X509Credential::CertData> |
| 40 | bytes_to_x509_credential_data(const std::vector<bytes>& data_in) |
| 41 | { |
| 42 | return stdx::transform<X509Credential::CertData>( |
| 43 | data_in, [](const bytes& der) { return X509Credential::CertData{ der }; }); |
| 44 | } |
| 45 | |
| 46 | X509Credential::X509Credential(const std::vector<bytes>& der_chain_in) |
| 47 | : der_chain(bytes_to_x509_credential_data(der_chain_in)) |
| 48 | { |
| 49 | if (der_chain.empty()) { |
| 50 | throw std::invalid_argument("empty certificate chain"); |
| 51 | } |
| 52 | |
| 53 | // Parse the chain |
| 54 | auto parsed = std::vector<Certificate>(); |
| 55 | for (const auto& cert : der_chain) { |
| 56 | parsed.emplace_back(cert.data); |
| 57 | } |
| 58 | |
| 59 | // first element represents leaf cert |
| 60 | const auto& sig = find_signature(parsed[0].public_key_algorithm()); |
| 61 | const auto pub_data = sig.serialize(*parsed[0].public_key); |
| 62 | _signature_scheme = tls_signature_scheme(parsed[0].public_key_algorithm()); |
| 63 | _public_key = SignaturePublicKey{ pub_data }; |
| 64 | |
| 65 | // verify chain for valid signatures |
| 66 | for (size_t i = 0; i < der_chain.size() - 1; i++) { |
| 67 | if (!parsed[i].valid_from(parsed[i + 1])) { |
| 68 | throw std::runtime_error("Certificate Chain validation failure"); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | SignatureScheme |
| 74 | X509Credential::signature_scheme() const |
| 75 | { |
| 76 | return _signature_scheme; |
| 77 | } |
| 78 | |
| 79 | SignaturePublicKey |
| 80 | X509Credential::public_key() const |
| 81 | { |
| 82 | return _public_key; |
| 83 | } |
| 84 | |
| 85 | bool |
| 86 | X509Credential::valid_for(const SignaturePublicKey& pub) const |
| 87 | { |
| 88 | return pub == public_key(); |
| 89 | } |
| 90 | |
| 91 | tls::ostream& |
| 92 | operator<<(tls::ostream& str, const X509Credential& obj) |
| 93 | { |
| 94 | return str << obj.der_chain; |
| 95 | } |
| 96 | |
| 97 | tls::istream& |
| 98 | operator>>(tls::istream& str, X509Credential& obj) |
| 99 | { |
| 100 | auto der_chain = std::vector<X509Credential::CertData>{}; |
| 101 | str >> der_chain; |
| 102 | |
| 103 | auto der_in = stdx::transform<bytes>( |
| 104 | der_chain, [](const auto& cert_data) { return cert_data.data; }); |
| 105 | obj = X509Credential(der_in); |
| 106 | |
| 107 | return str; |
| 108 | } |
| 109 | |
| 110 | bool |
| 111 | operator==(const X509Credential& lhs, const X509Credential& rhs) |
| 112 | { |
| 113 | return lhs.der_chain == rhs.der_chain; |
| 114 | } |
| 115 | |
| 116 | /// |
| 117 | /// UserInfoVCCredential |
| 118 | /// |
| 119 | UserInfoVCCredential::UserInfoVCCredential(std::string userinfo_vc_jwt_in) |
| 120 | : userinfo_vc_jwt(std::move(userinfo_vc_jwt_in)) |
| 121 | , _vc(std::make_shared<hpke::UserInfoVC>(userinfo_vc_jwt)) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | bool |
| 126 | // NOLINTNEXTLINE(readability-convert-member-functions-to-static) |
| 127 | UserInfoVCCredential::valid_for(const SignaturePublicKey& pub) const |
| 128 | { |
| 129 | const auto& vc_pub = _vc->public_key(); |
| 130 | return pub.data == vc_pub.sig.serialize(*vc_pub.key); |
| 131 | } |
| 132 | |
| 133 | bool |
| 134 | UserInfoVCCredential::valid_from(const PublicJWK& pub) const |
| 135 | { |
| 136 | const auto& sig = _vc->signature_algorithm(); |
| 137 | if (pub.signature_scheme != tls_signature_scheme(sig.id)) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | const auto sig_pub = sig.deserialize(pub.public_key.data); |
| 142 | return _vc->valid_from(*sig_pub); |
| 143 | } |
| 144 | |
| 145 | tls::ostream |
| 146 | operator<<(tls::ostream& str, const UserInfoVCCredential& obj) |
| 147 | { |
| 148 | return str << from_ascii(obj.userinfo_vc_jwt); |
| 149 | } |
| 150 | |
| 151 | tls::istream |
| 152 | operator>>(tls::istream& str, UserInfoVCCredential& obj) |
| 153 | { |
| 154 | auto jwt = bytes{}; |
| 155 | str >> jwt; |
| 156 | obj = UserInfoVCCredential(to_ascii(jwt)); |
| 157 | return str; |
| 158 | } |
| 159 | |
| 160 | bool |
| 161 | operator==(const UserInfoVCCredential& lhs, const UserInfoVCCredential& rhs) |
| 162 | { |
| 163 | return lhs.userinfo_vc_jwt == rhs.userinfo_vc_jwt; |
| 164 | } |
| 165 | |
| 166 | bool |
| 167 | operator!=(const UserInfoVCCredential& lhs, const UserInfoVCCredential& rhs) |
| 168 | { |
| 169 | return !(lhs == rhs); |
| 170 | } |
| 171 | |
| 172 | /// |
| 173 | /// CredentialBinding and MultiCredential |
| 174 | /// |
| 175 | |
| 176 | struct CredentialBindingTBS |
| 177 | { |
| 178 | const CipherSuite& cipher_suite; |
| 179 | const Credential& credential; |
| 180 | const SignaturePublicKey& credential_key; |
| 181 | const SignaturePublicKey& signature_key; |
| 182 | |
| 183 | TLS_SERIALIZABLE(cipher_suite, credential, credential_key, signature_key) |
| 184 | }; |
| 185 | |
| 186 | CredentialBinding::CredentialBinding(CipherSuite cipher_suite_in, |
| 187 | Credential credential_in, |
| 188 | const SignaturePrivateKey& credential_priv, |
| 189 | const SignaturePublicKey& signature_key) |
| 190 | : cipher_suite(cipher_suite_in) |
| 191 | , credential(std::move(credential_in)) |
| 192 | , credential_key(credential_priv.public_key) |
| 193 | { |
| 194 | if (credential.type() == CredentialType::multi_draft_00) { |
| 195 | throw InvalidParameterError("Multi-credentials cannot be nested"); |
| 196 | } |
| 197 | |
| 198 | if (!credential.valid_for(credential_key)) { |
| 199 | throw InvalidParameterError("Credential key does not match credential"); |
| 200 | } |
| 201 | |
| 202 | signature = credential_priv.sign( |
| 203 | cipher_suite, sign_label::multi_credential, to_be_signed(signature_key)); |
| 204 | } |
| 205 | |
| 206 | bytes |
| 207 | CredentialBinding::to_be_signed(const SignaturePublicKey& signature_key) const |
| 208 | { |
| 209 | return tls::marshal(CredentialBindingTBS{ |
| 210 | cipher_suite, credential, credential_key, signature_key }); |
| 211 | } |
| 212 | |
| 213 | bool |
| 214 | CredentialBinding::valid_for(const SignaturePublicKey& signature_key) const |
| 215 | { |
| 216 | auto valid_self = credential.valid_for(credential_key); |
| 217 | auto valid_other = credential_key.verify(cipher_suite, |
| 218 | sign_label::multi_credential, |
| 219 | to_be_signed(signature_key), |
| 220 | signature); |
| 221 | |
| 222 | return valid_self && valid_other; |
| 223 | } |
| 224 | |
| 225 | MultiCredential::MultiCredential( |
| 226 | const std::vector<CredentialBindingInput>& binding_inputs, |
| 227 | const SignaturePublicKey& signature_key) |
| 228 | { |
| 229 | bindings = |
| 230 | stdx::transform<CredentialBinding>(binding_inputs, [&](auto&& input) { |
| 231 | return CredentialBinding(input.cipher_suite, |
| 232 | input.credential, |
| 233 | input.credential_priv, |
| 234 | signature_key); |
| 235 | }); |
| 236 | } |
| 237 | |
| 238 | bool |
| 239 | MultiCredential::valid_for(const SignaturePublicKey& pub) const |
| 240 | { |
| 241 | return stdx::all_of( |
| 242 | bindings, [&](const auto& binding) { return binding.valid_for(pub); }); |
| 243 | } |
| 244 | |
| 245 | /// |
| 246 | /// Credential |
| 247 | /// |
| 248 | |
| 249 | CredentialType |
| 250 | Credential::type() const |
| 251 | { |
| 252 | return tls::variant<CredentialType>::type(_cred); |
| 253 | } |
| 254 | |
| 255 | Credential |
| 256 | Credential::basic(const bytes& identity) |
| 257 | { |
| 258 | return { BasicCredential{ identity } }; |
| 259 | } |
| 260 | |
| 261 | Credential |
| 262 | Credential::x509(const std::vector<bytes>& der_chain) |
| 263 | { |
| 264 | return { X509Credential{ der_chain } }; |
| 265 | } |
| 266 | |
| 267 | Credential |
| 268 | Credential::multi(const std::vector<CredentialBindingInput>& binding_inputs, |
| 269 | const SignaturePublicKey& signature_key) |
| 270 | { |
| 271 | return { MultiCredential{ binding_inputs, signature_key } }; |
| 272 | } |
| 273 | |
| 274 | Credential |
| 275 | Credential::userinfo_vc(const std::string& userinfo_vc_jwt) |
| 276 | { |
| 277 | return { UserInfoVCCredential{ userinfo_vc_jwt } }; |
| 278 | } |
| 279 | |
| 280 | bool |
| 281 | Credential::valid_for(const SignaturePublicKey& pub) const |
| 282 | { |
| 283 | const auto pub_key_match = overloaded{ |
| 284 | [&](const X509Credential& x509) { return x509.valid_for(pub); }, |
| 285 | [](const BasicCredential& /* basic */) { return true; }, |
| 286 | [&](const UserInfoVCCredential& vc) { return vc.valid_for(pub); }, |
| 287 | [&](const MultiCredential& multi) { return multi.valid_for(pub); }, |
| 288 | }; |
| 289 | |
| 290 | return var::visit(pub_key_match, _cred); |
| 291 | } |
| 292 | |
| 293 | Credential::Credential(SpecificCredential specific) |
| 294 | : _cred(std::move(specific)) |
| 295 | { |
| 296 | } |
| 297 | |
| 298 | } // namespace mlspp |