| 1 | #include <mls/messages.h> |
| 2 | #include <mls/session.h> |
| 3 | |
| 4 | #include <deque> |
| 5 | |
| 6 | namespace mlspp { |
| 7 | |
| 8 | /// |
| 9 | /// Inner struct declarations for PendingJoin and Session |
| 10 | /// |
| 11 | |
| 12 | struct PendingJoin::Inner |
| 13 | { |
| 14 | const CipherSuite suite; |
| 15 | const HPKEPrivateKey init_priv; |
| 16 | const HPKEPrivateKey leaf_priv; |
| 17 | const SignaturePrivateKey sig_priv; |
| 18 | const KeyPackage key_package; |
| 19 | |
| 20 | Inner(CipherSuite suite_in, |
| 21 | SignaturePrivateKey sig_priv_in, |
| 22 | Credential cred_in); |
| 23 | |
| 24 | static PendingJoin create(CipherSuite suite, |
| 25 | SignaturePrivateKey sig_priv, |
| 26 | Credential cred); |
| 27 | }; |
| 28 | |
| 29 | struct Session::Inner |
| 30 | { |
| 31 | std::deque<State> history; |
| 32 | std::map<bytes, State> outbound_cache; |
| 33 | bool encrypt_handshake{ false }; |
| 34 | |
| 35 | explicit Inner(State state); |
| 36 | |
| 37 | static Session begin(CipherSuite suite, |
| 38 | const bytes& group_id, |
| 39 | const HPKEPrivateKey& leaf_priv, |
| 40 | const SignaturePrivateKey& sig_priv, |
| 41 | const LeafNode& leaf_node); |
| 42 | static Session join(const HPKEPrivateKey& init_priv, |
| 43 | const HPKEPrivateKey& leaf_priv, |
| 44 | const SignaturePrivateKey& sig_priv, |
| 45 | const KeyPackage& key_package, |
| 46 | const bytes& welcome_data); |
| 47 | |
| 48 | bytes fresh_secret() const; |
| 49 | MLSMessage import_handshake(const bytes& encoded) const; |
| 50 | State& for_epoch(epoch_t epoch); |
| 51 | }; |
| 52 | |
| 53 | /// |
| 54 | /// Client |
| 55 | /// |
| 56 | |
| 57 | Client::Client(CipherSuite suite_in, |
| 58 | SignaturePrivateKey sig_priv_in, |
| 59 | Credential cred_in) |
| 60 | : suite(suite_in) |
| 61 | , sig_priv(std::move(sig_priv_in)) |
| 62 | , cred(std::move(cred_in)) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | Session |
| 67 | Client::begin_session(const bytes& group_id) const |
| 68 | { |
| 69 | auto leaf_priv = HPKEPrivateKey::generate(suite); |
| 70 | auto leaf_node = LeafNode(suite, |
| 71 | leaf_priv.public_key, |
| 72 | sig_priv.public_key, |
| 73 | cred, |
| 74 | Capabilities::create_default(), |
| 75 | Lifetime::create_default(), |
| 76 | {}, |
| 77 | sig_priv); |
| 78 | |
| 79 | return Session::Inner::begin(suite, group_id, leaf_priv, sig_priv, leaf_node); |
| 80 | } |
| 81 | |
| 82 | PendingJoin |
| 83 | Client::start_join() const |
| 84 | { |
| 85 | return PendingJoin::Inner::create(suite, sig_priv, cred); |
| 86 | } |
| 87 | |
| 88 | /// |
| 89 | /// PendingJoin |
| 90 | /// |
| 91 | |
| 92 | PendingJoin::Inner::Inner(CipherSuite suite_in, |
| 93 | SignaturePrivateKey sig_priv_in, |
| 94 | Credential cred_in) |
| 95 | : suite(suite_in) |
| 96 | , init_priv(HPKEPrivateKey::generate(suite)) |
| 97 | , leaf_priv(HPKEPrivateKey::generate(suite)) |
| 98 | , sig_priv(std::move(sig_priv_in)) |
| 99 | , key_package(suite, |
| 100 | init_priv.public_key, |
| 101 | LeafNode(suite, |
| 102 | leaf_priv.public_key, |
| 103 | sig_priv.public_key, |
| 104 | std::move(cred_in), |
| 105 | Capabilities::create_default(), |
| 106 | Lifetime::create_default(), |
| 107 | {}, |
| 108 | sig_priv), |
| 109 | {}, |
| 110 | sig_priv) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | PendingJoin |
| 115 | PendingJoin::Inner::create(CipherSuite suite, |
| 116 | SignaturePrivateKey sig_priv, |
| 117 | Credential cred) |
| 118 | { |
| 119 | auto inner = |
| 120 | std::make_unique<Inner>(suite, std::move(sig_priv), std::move(cred)); |
| 121 | return { inner.release() }; |
| 122 | } |
| 123 | |
| 124 | PendingJoin::PendingJoin(PendingJoin&& other) noexcept = default; |
| 125 | |
| 126 | PendingJoin& |
| 127 | PendingJoin::operator=(PendingJoin&& other) noexcept = default; |
| 128 | |
| 129 | PendingJoin::~PendingJoin() = default; |
| 130 | |
| 131 | PendingJoin::PendingJoin(Inner* inner_in) |
| 132 | : inner(inner_in) |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | bytes |
| 137 | PendingJoin::key_package() const |
| 138 | { |
| 139 | return tls::marshal(inner->key_package); |
| 140 | } |
| 141 | |
| 142 | Session |
| 143 | PendingJoin::complete(const bytes& welcome) const |
| 144 | { |
| 145 | return Session::Inner::join(inner->init_priv, |
| 146 | inner->leaf_priv, |
| 147 | inner->sig_priv, |
| 148 | inner->key_package, |
| 149 | welcome); |
| 150 | } |
| 151 | |
| 152 | /// |
| 153 | /// Session |
| 154 | /// |
| 155 | |
| 156 | Session::Inner::Inner(State state) |
| 157 | : history{ std::move(state) } |
| 158 | , encrypt_handshake(true) |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | Session |
| 163 | Session::Inner::begin(CipherSuite suite, |
| 164 | const bytes& group_id, |
| 165 | const HPKEPrivateKey& leaf_priv, |
| 166 | const SignaturePrivateKey& sig_priv, |
| 167 | const LeafNode& leaf_node) |
| 168 | { |
| 169 | auto state = State(group_id, suite, leaf_priv, sig_priv, leaf_node, {}); |
| 170 | auto inner = std::make_unique<Inner>(state); |
| 171 | return { inner.release() }; |
| 172 | } |
| 173 | |
| 174 | Session |
| 175 | Session::Inner::join(const HPKEPrivateKey& init_priv, |
| 176 | const HPKEPrivateKey& leaf_priv, |
| 177 | const SignaturePrivateKey& sig_priv, |
| 178 | const KeyPackage& key_package, |
| 179 | const bytes& welcome_data) |
| 180 | { |
| 181 | auto welcome = tls::get<Welcome>(welcome_data); |
| 182 | |
| 183 | auto state = State( |
| 184 | init_priv, leaf_priv, sig_priv, key_package, welcome, std::nullopt, {}); |
| 185 | auto inner = std::make_unique<Inner>(state); |
| 186 | return { inner.release() }; |
| 187 | } |
| 188 | |
| 189 | bytes |
| 190 | Session::Inner::fresh_secret() const |
| 191 | { |
| 192 | const auto suite = history.front().cipher_suite(); |
| 193 | return random_bytes(suite.secret_size()); |
| 194 | } |
| 195 | |
| 196 | MLSMessage |
| 197 | Session::Inner::import_handshake(const bytes& encoded) const |
| 198 | { |
| 199 | auto msg = tls::get<MLSMessage>(encoded); |
| 200 | |
| 201 | switch (msg.wire_format()) { |
| 202 | case WireFormat::mls_public_message: |
| 203 | if (encrypt_handshake) { |
| 204 | throw ProtocolError("Handshake not encrypted as required"); |
| 205 | } |
| 206 | |
| 207 | return msg; |
| 208 | |
| 209 | case WireFormat::mls_private_message: { |
| 210 | if (!encrypt_handshake) { |
| 211 | throw ProtocolError("Unexpected handshake encryption"); |
| 212 | } |
| 213 | |
| 214 | return msg; |
| 215 | } |
| 216 | |
| 217 | default: |
| 218 | throw InvalidParameterError("Illegal wire format"); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | State& |
| 223 | Session::Inner::for_epoch(epoch_t epoch) |
| 224 | { |
| 225 | for (auto& state : history) { |
| 226 | if (state.epoch() == epoch) { |
| 227 | return state; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | throw MissingStateError("No state for epoch"); |
| 232 | } |
| 233 | |
| 234 | Session::Session(Session&& other) noexcept = default; |
| 235 | |
| 236 | Session& |
| 237 | Session::operator=(Session&& other) noexcept = default; |
| 238 | |
| 239 | Session::~Session() = default; |
| 240 | |
| 241 | Session::Session(Inner* inner_in) |
| 242 | : inner(inner_in) |
| 243 | { |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | Session::encrypt_handshake(bool enabled) |
| 248 | { |
| 249 | inner->encrypt_handshake = enabled; |
| 250 | } |
| 251 | |
| 252 | bytes |
| 253 | Session::add(const bytes& key_package_data) |
| 254 | { |
| 255 | auto key_package = tls::get<KeyPackage>(key_package_data); |
| 256 | auto proposal = inner->history.front().add( |
| 257 | key_package, { inner->encrypt_handshake, {}, 0 }); |
| 258 | return tls::marshal(proposal); |
| 259 | } |
| 260 | |
| 261 | bytes |
| 262 | Session::update() |
| 263 | { |
| 264 | auto leaf_secret = inner->fresh_secret(); |
| 265 | |
| 266 | auto leaf_priv = HPKEPrivateKey::generate(cipher_suite()); |
| 267 | auto proposal = inner->history.front().update( |
| 268 | std::move(leaf_priv), {}, { inner->encrypt_handshake, {}, 0 }); |
| 269 | return tls::marshal(proposal); |
| 270 | } |
| 271 | |
| 272 | bytes |
| 273 | Session::remove(uint32_t index) |
| 274 | { |
| 275 | auto proposal = inner->history.front().remove( |
| 276 | RosterIndex{ index }, { inner->encrypt_handshake, {}, 0 }); |
| 277 | return tls::marshal(proposal); |
| 278 | } |
| 279 | |
| 280 | std::tuple<bytes, bytes> |
| 281 | Session::commit(const bytes& proposal) |
| 282 | { |
| 283 | return commit(std::vector<bytes>{ proposal }); |
| 284 | } |
| 285 | |
| 286 | std::tuple<bytes, bytes> |
| 287 | Session::commit(const std::vector<bytes>& proposals) |
| 288 | { |
| 289 | auto provisional_state = inner->history.front(); |
| 290 | for (const auto& proposal_data : proposals) { |
| 291 | auto msg = inner->import_handshake(proposal_data); |
| 292 | auto maybe_state = provisional_state.handle(msg); |
| 293 | if (maybe_state) { |
| 294 | throw InvalidParameterError("Invalid proposal; actually a commit"); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | inner->history.front() = std::move(provisional_state); |
| 299 | return commit(); |
| 300 | } |
| 301 | |
| 302 | std::tuple<bytes, bytes> |
| 303 | Session::commit() |
| 304 | { |
| 305 | auto commit_secret = inner->fresh_secret(); |
| 306 | auto encrypt = inner->encrypt_handshake; |
| 307 | auto [commit, welcome, new_state] = inner->history.front().commit( |
| 308 | commit_secret, CommitOpts{ {}, true, encrypt, {} }, { encrypt, {}, 0 }); |
| 309 | |
| 310 | auto commit_msg = tls::marshal(commit); |
| 311 | auto welcome_msg = tls::marshal(welcome); |
| 312 | |
| 313 | inner->outbound_cache.insert({ commit_msg, new_state }); |
| 314 | return std::make_tuple(welcome_msg, commit_msg); |
| 315 | } |
| 316 | |
| 317 | bool |
| 318 | Session::handle(const bytes& handshake_data) |
| 319 | { |
| 320 | auto msg = inner->import_handshake(handshake_data); |
| 321 | |
| 322 | auto maybe_cached_state = std::optional<State>{}; |
| 323 | auto node = inner->outbound_cache.extract(handshake_data); |
| 324 | if (!node.empty()) { |
| 325 | maybe_cached_state = node.mapped(); |
| 326 | } |
| 327 | |
| 328 | auto maybe_next_state = |
| 329 | inner->history.front().handle(msg, maybe_cached_state); |
| 330 | if (!maybe_next_state) { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | inner->history.emplace_front(opt::get(maybe_next_state)); |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | epoch_t |
| 339 | Session::epoch() const |
| 340 | { |
| 341 | return inner->history.front().epoch(); |
| 342 | } |
| 343 | |
| 344 | LeafIndex |
| 345 | Session::index() const |
| 346 | { |
| 347 | return inner->history.front().index(); |
| 348 | } |
| 349 | |
| 350 | CipherSuite |
| 351 | Session::cipher_suite() const |
| 352 | { |
| 353 | return inner->history.front().cipher_suite(); |
| 354 | } |
| 355 | |
| 356 | const ExtensionList& |
| 357 | Session::extensions() const |
| 358 | { |
| 359 | return inner->history.front().extensions(); |
| 360 | } |
| 361 | |
| 362 | const TreeKEMPublicKey& |
| 363 | Session::tree() const |
| 364 | { |
| 365 | return inner->history.front().tree(); |
| 366 | } |
| 367 | |
| 368 | bytes |
| 369 | Session::do_export(const std::string& label, |
| 370 | const bytes& context, |
| 371 | size_t size) const |
| 372 | { |
| 373 | return inner->history.front().do_export(label, context, size); |
| 374 | } |
| 375 | |
| 376 | GroupInfo |
| 377 | Session::group_info() const |
| 378 | { |
| 379 | return inner->history.front().group_info(true); |
| 380 | } |
| 381 | |
| 382 | std::vector<LeafNode> |
| 383 | Session::roster() const |
| 384 | { |
| 385 | return inner->history.front().roster(); |
| 386 | } |
| 387 | |
| 388 | bytes |
| 389 | Session::epoch_authenticator() const |
| 390 | { |
| 391 | return inner->history.front().epoch_authenticator(); |
| 392 | } |
| 393 | |
| 394 | bytes |
| 395 | Session::protect(const bytes& plaintext) |
| 396 | { |
| 397 | auto msg = inner->history.front().protect({}, plaintext, 0); |
| 398 | return tls::marshal(msg); |
| 399 | } |
| 400 | |
| 401 | // TODO([email protected]): It would be good to expose identity information |
| 402 | // here, since ciphertexts are authenticated per sender. Who sent |
| 403 | // this ciphertext? |
| 404 | bytes |
| 405 | Session::unprotect(const bytes& ciphertext) |
| 406 | { |
| 407 | auto ciphertext_obj = tls::get<MLSMessage>(ciphertext); |
| 408 | auto& state = inner->for_epoch(ciphertext_obj.epoch()); |
| 409 | auto [aad, pt] = state.unprotect(ciphertext_obj); |
| 410 | silence_unused(aad); |
| 411 | return pt; |
| 412 | } |
| 413 | |
| 414 | bool |
| 415 | operator==(const Session& lhs, const Session& rhs) |
| 416 | { |
| 417 | if (lhs.inner->encrypt_handshake != rhs.inner->encrypt_handshake) { |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | auto size = std::min(lhs.inner->history.size(), rhs.inner->history.size()); |
| 422 | for (size_t i = 0; i < size; i += 1) { |
| 423 | if (lhs.inner->history.at(i) != rhs.inner->history.at(i)) { |
| 424 | return false; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | bool |
| 432 | operator!=(const Session& lhs, const Session& rhs) |
| 433 | { |
| 434 | return !(lhs == rhs); |
| 435 | } |
| 436 | |
| 437 | } // namespace mlspp |