File: MLS.cpp

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (712 lines) | stat: -rw-r--r-- 24,311 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/MLS.h"

#include "MLSGroupView.h"
#include "MLSLogging.h"
#include "MLSTypeUtils.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/Logging.h"
#include "mozilla/dom/MLSGroupView.h"
#include "mozilla/dom/MLSTransactionChild.h"
#include "mozilla/dom/MLSTransactionMessage.h"
#include "mozilla/dom/PMLSTransaction.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/Endpoint.h"
#include "mozilla/ipc/PBackgroundChild.h"
#include "nsCOMPtr.h"
#include "nsDebug.h"
#include "nsIGlobalObject.h"
#include "nsTArray.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MLS, mGlobalObject)

NS_IMPL_CYCLE_COLLECTING_ADDREF(MLS)
NS_IMPL_CYCLE_COLLECTING_RELEASE(MLS)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MLS)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

// Setup logging
mozilla::LazyLogModule gMlsLog("MLS");

/* static */ already_AddRefed<MLS> MLS::Constructor(GlobalObject& aGlobalObject,
                                                    ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::Constructor()"));

  nsCOMPtr<nsIGlobalObject> global(
      do_QueryInterface(aGlobalObject.GetAsSupports()));
  if (NS_WARN_IF(!global)) {
    aRv.Throw(NS_ERROR_FAILURE);
    return nullptr;
  }

  // Get the principal and perform some validation on it.
  // We do not allow MLS in Private Browsing Mode for now.
  nsIPrincipal* principal = global->PrincipalOrNull();
  if (!principal || !principal->GetIsContentPrincipal() ||
      principal->GetIsInPrivateBrowsing()) {
    aRv.ThrowSecurityError("Cannot create MLS store for origin");
    return nullptr;
  }

  // Create the endpoints for the MLS actor
  mozilla::ipc::Endpoint<PMLSTransactionParent> parentEndpoint;
  mozilla::ipc::Endpoint<PMLSTransactionChild> childEndpoint;
  MOZ_ALWAYS_SUCCEEDS(
      PMLSTransaction::CreateEndpoints(&parentEndpoint, &childEndpoint));

  mozilla::ipc::PBackgroundChild* backgroundChild =
      mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
  if (!backgroundChild) {
    aRv.Throw(NS_ERROR_UNEXPECTED);
    return nullptr;
  }

  // Bind the child actor, and send the parent endpoint.
  RefPtr<MLSTransactionChild> actor = new MLSTransactionChild();
  MOZ_ALWAYS_TRUE(childEndpoint.Bind(actor));

  MOZ_ALWAYS_TRUE(backgroundChild->SendCreateMLSTransaction(
      std::move(parentEndpoint), WrapNotNull(principal)));

  return MakeAndAddRef<MLS>(global, actor);
}

MLS::MLS(nsIGlobalObject* aGlobalObject, MLSTransactionChild* aActor)
    : mGlobalObject(aGlobalObject), mTransactionChild(aActor) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::MLS()"));
}

MLS::~MLS() {
  if (mTransactionChild) {
    mTransactionChild->Close();
  }
}

JSObject* MLS::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return MLS_Binding::Wrap(aCx, this, aGivenProto);
}

//
// API
//

already_AddRefed<Promise> MLS::DeleteState(ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::DeleteState()"));

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  mTransactionChild->SendRequestStateDelete(
      [promise](bool result) {
        if (result) {
          promise->MaybeResolveWithUndefined();
        } else {
          promise->MaybeReject(NS_ERROR_FAILURE);
        }
      },
      [promise](::mozilla::ipc::ResponseRejectReason) {
        promise->MaybeRejectWithUnknownError("deleteState failed");
      });

  return promise.forget();
}

already_AddRefed<Promise> MLS::GenerateIdentity(ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::GenerateIdentity()"));

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  mTransactionChild->SendRequestGenerateIdentityKeypair()->Then(
      GetCurrentSerialEventTarget(), __func__,
      [promise, self = RefPtr{this}](Maybe<RawBytes>&& result) {
        // Check if the value is Nothing
        if (result.isNothing()) {
          promise->MaybeRejectWithUnknownError(
              "generateIdentityKeypair failed");
          return;
        }

        // Get the context from the GlobalObject
        AutoJSAPI jsapi;
        if (NS_WARN_IF(!jsapi.Init(self->mGlobalObject))) {
          promise->MaybeRejectWithUnknownError(
              "generateIdentityKeypair failed");
          return;
        }
        JSContext* cx = jsapi.cx();

        // Construct the Uint8Array object
        ErrorResult error;
        JS::Rooted<JSObject*> content(
            cx, Uint8Array::Create(cx, result->data(), error));
        error.WouldReportJSException();
        if (error.Failed()) {
          promise->MaybeReject(std::move(error));
          return;
        }

        // Construct MLSBytes with the client identifer as content
        RootedDictionary<MLSBytes> rvalue(cx);
        rvalue.mType = MLSObjectType::Client_identifier;
        rvalue.mContent.Init(content);

        // Resolve the promise with the MLSBytes object
        promise->MaybeResolve(rvalue);
      },
      [promise](::mozilla::ipc::ResponseRejectReason aReason) {
        promise->MaybeRejectWithUnknownError("generateIdentity failed");
      });

  return promise.forget();
}

already_AddRefed<Promise> MLS::GenerateCredential(
    const MLSBytesOrUint8ArrayOrUTF8String& aJsCredContent, ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug,
          ("MLS::GenerateCredentialBasic()"));

  // Handle the credential content parameter
  nsTArray<uint8_t> credContent = ExtractMLSBytesOrUint8ArrayOrUTF8String(
      MLSObjectType::Credential_basic, aJsCredContent, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the credContent is empty
  if (NS_WARN_IF(credContent.IsEmpty())) {
    aRv.ThrowTypeError("The credential content must not be empty");
    return nullptr;
  }

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  mTransactionChild->SendRequestGenerateCredentialBasic(credContent)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          [promise, self = RefPtr{this}](Maybe<RawBytes>&& result) {
            // Check if the value is Nothing
            if (result.isNothing()) {
              promise->MaybeRejectWithUnknownError(
                  "generateCredentialBasic failed");
              return;
            }

            // Get the context from the GlobalObject
            AutoJSAPI jsapi;
            if (NS_WARN_IF(!jsapi.Init(self->mGlobalObject))) {
              promise->MaybeRejectWithUnknownError(
                  "generateCredentialBasic failed");
              return;
            }
            JSContext* cx = jsapi.cx();

            // Construct the Uint8Array object
            ErrorResult error;
            JS::Rooted<JSObject*> content(
                cx, Uint8Array::Create(cx, result->data(), error));
            error.WouldReportJSException();
            if (error.Failed()) {
              promise->MaybeReject(std::move(error));
              return;
            }

            // Construct MLSBytes with the client identifer as content
            RootedDictionary<MLSBytes> rvalue(cx);
            rvalue.mType = MLSObjectType::Credential_basic;
            rvalue.mContent.Init(content);

            // Resolve the promise
            promise->MaybeResolve(rvalue);
          },
          [promise](::mozilla::ipc::ResponseRejectReason aReason) {
            promise->MaybeRejectWithUnknownError(
                "generateCredentialBasic failed");
          });

  return promise.forget();
}

already_AddRefed<Promise> MLS::GenerateKeyPackage(
    const MLSBytesOrUint8Array& aJsClientIdentifier,
    const MLSBytesOrUint8Array& aJsCredential, ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::GenerateKeyPackage()"));

  // Handle the client identifier parameter
  nsTArray<uint8_t> clientIdentifier = ExtractMLSBytesOrUint8Array(
      MLSObjectType::Client_identifier, aJsClientIdentifier, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the client identifier is empty
  if (NS_WARN_IF(clientIdentifier.IsEmpty())) {
    aRv.ThrowTypeError("The client identifier must not be empty");
    return nullptr;
  }

  // Handle the credential parameter
  nsTArray<uint8_t> credential = ExtractMLSBytesOrUint8Array(
      MLSObjectType::Credential_basic, aJsCredential, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the credential is empty
  if (NS_WARN_IF(credential.IsEmpty())) {
    aRv.ThrowTypeError("The credential must not be empty");
    return nullptr;
  }

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Use the static method or instance to send the IPC message
  mTransactionChild->SendRequestGenerateKeyPackage(clientIdentifier, credential)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          [promise, self = RefPtr{this}](Maybe<RawBytes>&& keyPackage) {
            // Check if the value is Nothing
            if (keyPackage.isNothing()) {
              promise->MaybeReject(NS_ERROR_FAILURE);
              return;
            }

            // Get the context from the GlobalObject
            AutoJSAPI jsapi;
            if (NS_WARN_IF(!jsapi.Init(self->mGlobalObject))) {
              promise->MaybeReject(NS_ERROR_FAILURE);
              return;
            }
            JSContext* cx = jsapi.cx();

            // Construct the Uint8Array object
            ErrorResult error;
            JS::Rooted<JSObject*> content(
                cx, Uint8Array::Create(cx, keyPackage->data(), error));
            error.WouldReportJSException();
            if (error.Failed()) {
              promise->MaybeReject(std::move(error));
              return;
            }

            // Construct MLSBytes with the client identifer as content
            RootedDictionary<MLSBytes> rvalue(cx);
            rvalue.mType = MLSObjectType::Key_package;
            rvalue.mContent.Init(content);

            // Resolve the promise
            promise->MaybeResolve(rvalue);
          },
          [promise](::mozilla::ipc::ResponseRejectReason aReason) {
            promise->MaybeRejectWithUnknownError("generateKeyPackage failed");
          });

  return promise.forget();
}

already_AddRefed<Promise> MLS::GroupCreate(
    const MLSBytesOrUint8Array& aJsClientIdentifier,
    const MLSBytesOrUint8Array& aJsCredential, ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::GroupCreate()"));

  // Handle the client identifier parameter
  nsTArray<uint8_t> clientIdentifier = ExtractMLSBytesOrUint8Array(
      MLSObjectType::Client_identifier, aJsClientIdentifier, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the client identifier is empty
  if (NS_WARN_IF(clientIdentifier.IsEmpty())) {
    aRv.ThrowTypeError("The client identifier must not be empty");
    return nullptr;
  }

  // Handle the credential parameter
  nsTArray<uint8_t> credential = ExtractMLSBytesOrUint8Array(
      MLSObjectType::Credential_basic, aJsCredential, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the credential is empty
  if (NS_WARN_IF(credential.IsEmpty())) {
    aRv.ThrowTypeError("The credential must not be empty");
    return nullptr;
  }

  // Log the hex of clientIdentifier
  if (MOZ_LOG_TEST(gMlsLog, LogLevel::Debug)) {
    nsAutoCString clientIdHex;
    for (uint8_t byte : clientIdentifier) {
      clientIdHex.AppendPrintf("%02X", byte);
    }
    MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug,
            ("clientIdentifier in hex: %s\n", clientIdHex.get()));
  }

  // Initialize jsGroupIdentifier to one byte of value 0xFF.
  // We do not want to allow choosing the GID at this point.
  // This value not being of the correct length will be discarded
  // internally and a fresh GID will be generated.
  //
  // In the future, the caller will allow choosing the GID.
  AutoTArray<uint8_t, 1> groupIdentifier{0xFF};

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Use the static method or instance to send the IPC message
  mTransactionChild
      ->SendRequestGroupCreate(clientIdentifier, credential, groupIdentifier)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          [promise, self = RefPtr{this},
           clientIdentifier(std::move(clientIdentifier))](
              Maybe<mozilla::security::mls::GkGroupIdEpoch>&&
                  groupIdEpoch) mutable {
            // Check if the value is Nothing
            if (groupIdEpoch.isNothing()) {
              promise->MaybeReject(NS_ERROR_FAILURE);
              return;
            }

            RefPtr<MLSGroupView> group =
                new MLSGroupView(self, std::move(groupIdEpoch->group_id),
                                 std::move(clientIdentifier));

            promise->MaybeResolve(group);
          },
          [promise](::mozilla::ipc::ResponseRejectReason aReason) {
            MOZ_LOG(gMlsLog, mozilla::LogLevel::Error,
                    ("IPC message rejected with reason: %d",
                     static_cast<int>(aReason)));
            promise->MaybeRejectWithUnknownError("groupCreate failed");
          });

  return promise.forget();
}

already_AddRefed<mozilla::dom::Promise> MLS::GroupGet(
    const MLSBytesOrUint8Array& aJsGroupIdentifier,
    const MLSBytesOrUint8Array& aJsClientIdentifier, ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::GroupGet()"));

  // Handle the group identifier parameter
  nsTArray<uint8_t> groupIdentifier = ExtractMLSBytesOrUint8Array(
      MLSObjectType::Group_identifier, aJsGroupIdentifier, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the group identifier is empty
  if (NS_WARN_IF(groupIdentifier.IsEmpty())) {
    aRv.ThrowTypeError("The group identifier must not be empty");
    return nullptr;
  }

  // Handle the client identifier parameter
  nsTArray<uint8_t> clientIdentifier = ExtractMLSBytesOrUint8Array(
      MLSObjectType::Client_identifier, aJsClientIdentifier, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the client identifier is empty
  if (NS_WARN_IF(clientIdentifier.IsEmpty())) {
    aRv.ThrowTypeError("The client identifier must not be empty");
    return nullptr;
  }

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Initialize label, context and len
  // We pass this through IPC to be able to reuse the same code for different
  // labels in the future
  AutoTArray<uint8_t, 7> label{'l', 'i', 'v', 'e', 'n', 'e', 's', 's'};
  AutoTArray<uint8_t, 1> context{0x00};
  uint64_t len = 32;

  // Use the static method or instance to send the IPC message
  mTransactionChild
      ->SendRequestExportSecret(groupIdentifier, clientIdentifier, label,
                                context, len)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          [promise, self = RefPtr{this},
           groupIdentifier(std::move(groupIdentifier)),
           clientIdentifier(std::move(clientIdentifier))](
              Maybe<mozilla::security::mls::GkExporterOutput>&&
                  exporterOutput) mutable {
            // Check if the exporterOutput contains a value
            if (exporterOutput.isNothing()) {
              promise->MaybeReject(NS_ERROR_FAILURE);
              return;
            }

            RefPtr<MLSGroupView> group =
                new MLSGroupView(self, std::move(exporterOutput->group_id),
                                 std::move(clientIdentifier));
            promise->MaybeResolve(group);
          },
          [promise](::mozilla::ipc::ResponseRejectReason aReason) {
            promise->MaybeRejectWithUnknownError("exportSecret failed");
          });

  return promise.forget();
}

already_AddRefed<Promise> MLS::GroupJoin(
    const MLSBytesOrUint8Array& aJsClientIdentifier,
    const MLSBytesOrUint8Array& aJsWelcome, ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::GroupJoin()"));

  // Handle the client identifier parameter
  nsTArray<uint8_t> clientIdentifier = ExtractMLSBytesOrUint8Array(
      MLSObjectType::Client_identifier, aJsClientIdentifier, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the client identifier is empty
  if (NS_WARN_IF(clientIdentifier.IsEmpty())) {
    aRv.ThrowTypeError("The client identifier must not be empty");
    return nullptr;
  }

  // Handle the welcome parameter
  nsTArray<uint8_t> welcome =
      ExtractMLSBytesOrUint8Array(MLSObjectType::Welcome, aJsWelcome, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the welcome is empty
  if (NS_WARN_IF(welcome.IsEmpty())) {
    aRv.ThrowTypeError("The welcome must not be empty");
    return nullptr;
  }

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  mTransactionChild->SendRequestGroupJoin(clientIdentifier, welcome)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          [promise, self = RefPtr{this},
           clientIdentifier(std::move(clientIdentifier))](
              Maybe<mozilla::security::mls::GkGroupIdEpoch>&&
                  groupIdEpoch) mutable {
            // Check if the value is Nothing
            if (groupIdEpoch.isNothing()) {
              promise->MaybeReject(NS_ERROR_FAILURE);
              return;
            }

            // Returns groupId and epoch
            RefPtr<MLSGroupView> group =
                new MLSGroupView(self, std::move(groupIdEpoch->group_id),
                                 std::move(clientIdentifier));

            // Resolve the promise
            promise->MaybeResolve(group);
          },
          [promise](::mozilla::ipc::ResponseRejectReason aReason) {
            promise->MaybeRejectWithUnknownError("groupJoin failed");
          });

  return promise.forget();
}

already_AddRefed<Promise> MLS::GetGroupIdFromMessage(
    const MLSBytesOrUint8Array& aJsMessage, ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug, ("MLS::GetGroupIdFromMessage()"));

  // Handle the message parameter
  nsTArray<uint8_t> message =
      ExtractMLSBytesOrUint8ArrayWithUnknownType(aJsMessage, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the message is empty
  if (NS_WARN_IF(message.IsEmpty())) {
    aRv.ThrowTypeError("The message must not be empty");
    return nullptr;
  }

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  mTransactionChild->SendRequestGetGroupIdentifier(message)->Then(
      GetCurrentSerialEventTarget(), __func__,
      [promise, self = RefPtr{this},
       message(std::move(message))](Maybe<RawBytes>&& result) {
        // Check if the value is Nothing
        if (result.isNothing()) {
          promise->MaybeReject(NS_ERROR_FAILURE);
          return;
        }

        // Get the context from the GlobalObject
        AutoJSAPI jsapi;
        if (NS_WARN_IF(!jsapi.Init(self->mGlobalObject))) {
          MOZ_LOG(gMlsLog, mozilla::LogLevel::Error,
                  ("Failed to initialize JSAPI"));
          promise->MaybeReject(NS_ERROR_FAILURE);
          return;
        }
        JSContext* cx = jsapi.cx();

        // Construct the Uint8Array objects based on the tag
        ErrorResult error;
        JS::Rooted<JSObject*> jsGroupId(
            cx, Uint8Array::Create(cx, result->data(), error));
        error.WouldReportJSException();
        if (error.Failed()) {
          promise->MaybeReject(std::move(error));
          return;
        }

        // Construct the MLSBytes object for the groupId
        RootedDictionary<MLSBytes> rvalue(cx);
        rvalue.mType = MLSObjectType::Group_identifier;
        rvalue.mContent.Init(jsGroupId);

        // Log if in debug mode
        if (MOZ_LOG_TEST(gMlsLog, LogLevel::Debug)) {
          MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug,
                  ("Successfully constructed MLSBytes"));
        }

        // Resolve the promise
        promise->MaybeResolve(rvalue);
      },
      [promise](::mozilla::ipc::ResponseRejectReason aReason) {
        MOZ_LOG(
            gMlsLog, mozilla::LogLevel::Error,
            ("IPC call rejected with reason: %d", static_cast<int>(aReason)));
        promise->MaybeRejectWithUnknownError("getGroupIdFromMessage failed");
      });

  return promise.forget();
}

already_AddRefed<Promise> MLS::GetGroupEpochFromMessage(
    const MLSBytesOrUint8Array& aJsMessage, ErrorResult& aRv) {
  MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug,
          ("MLS::GetGroupEpochFromMessage()"));

  // Handle the message parameter
  nsTArray<uint8_t> message =
      ExtractMLSBytesOrUint8ArrayWithUnknownType(aJsMessage, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  // Check if the message is empty
  if (NS_WARN_IF(message.IsEmpty())) {
    aRv.ThrowTypeError("The message must not be empty");
    return nullptr;
  }

  // Create a new Promise object for the result
  RefPtr<Promise> promise = Promise::Create(mGlobalObject, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  mTransactionChild->SendRequestGetGroupEpoch(message)->Then(
      GetCurrentSerialEventTarget(), __func__,
      [promise, self = RefPtr{this},
       message(std::move(message))](Maybe<RawBytes>&& result) {
        // Check if the value is Nothing
        if (result.isNothing()) {
          promise->MaybeReject(NS_ERROR_FAILURE);
          return;
        }

        // Get the context from the GlobalObject
        AutoJSAPI jsapi;
        if (NS_WARN_IF(!jsapi.Init(self->mGlobalObject))) {
          MOZ_LOG(gMlsLog, mozilla::LogLevel::Error,
                  ("Failed to initialize JSAPI"));
          promise->MaybeReject(NS_ERROR_FAILURE);
          return;
        }
        JSContext* cx = jsapi.cx();

        // Construct the Uint8Array objects based on the tag
        ErrorResult error;
        JS::Rooted<JSObject*> jsGroupId(
            cx, Uint8Array::Create(cx, result->data(), error));
        error.WouldReportJSException();
        if (error.Failed()) {
          promise->MaybeReject(std::move(error));
          return;
        }

        // Construct the MLSBytes object for the groupId
        RootedDictionary<MLSBytes> rvalue(cx);
        rvalue.mType = MLSObjectType::Group_epoch;
        rvalue.mContent.Init(jsGroupId);

        // Log if in debug mode
        if (MOZ_LOG_TEST(gMlsLog, LogLevel::Debug)) {
          MOZ_LOG(gMlsLog, mozilla::LogLevel::Debug,
                  ("Successfully constructed MLSBytes"));
        }

        // Resolve the promise
        promise->MaybeResolve(rvalue);
      },
      [promise](::mozilla::ipc::ResponseRejectReason aReason) {
        MOZ_LOG(
            gMlsLog, mozilla::LogLevel::Error,
            ("IPC call rejected with reason: %d", static_cast<int>(aReason)));
        promise->MaybeRejectWithUnknownError("getGroupEpochFromMessage failed");
      });

  return promise.forget();
}

}  // namespace mozilla::dom