Skip to content

Unhandled Exception: Assertion failed: Instance of 'InvalidKeyException' #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sky-flutter opened this issue Jan 27, 2021 · 14 comments
Closed

Comments

@sky-flutter
Copy link

I just copy the code from pub.dev in my project and run the project this error I got.

@xni06
Copy link

xni06 commented Jan 27, 2021

What error?

@sky-flutter
Copy link
Author

sky-flutter commented Jan 28, 2021

I changed my code and thereafter I got invalidkeyexception

@woinbo
Copy link

woinbo commented Jan 28, 2021

The same happened to me too!!!

I just copy the code from pub.dev in my project and run the project this error I got.

@woinbo
Copy link

woinbo commented Jan 28, 2021

@xni06 This is the error coming...

E/flutter (23216): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Assertion failed: Instance of 'InvalidKeyException'
E/flutter (23216): #0 SessionState.getSenderRatchetKey
package:libsignal_protocol_dart/…/state/SessionState.dart:103
E/flutter (23216): #1 SessionCipher.encrypt
package:libsignal_protocol_dart/src/SessionCipher.dart:59
E/flutter (23216): #2 install
package:signal/main.dart:40
E/flutter (23216): #3 main
package:signal/main.dart:7
E/flutter (23216): #4 _runMainZoned.. (dart:ui/hooks.dart:136:25)
E/flutter (23216): #5 _rootRun (dart:async/zone.dart:1186:13)
E/flutter (23216): #6 _CustomZone.run (dart:async/zone.dart:1090:19)
E/flutter (23216): #7 _runZoned (dart:async/zone.dart:1626:10)
E/flutter (23216): #8 runZonedGuarded (dart:async/zone.dart:1614:12)
E/flutter (23216): #9 _runMainZoned. (dart:ui/hooks.dart:132:5)
E/flutter (23216): #10 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter (23216): #11 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

This is the code

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:libsignal_protocol_dart/libsignal_protocol_dart.dart';

void main() {
  install();
  runApp(MyApp());
}

void install() {
  var identityKeyPair = KeyHelper.generateIdentityKeyPair();
  var registerationId = KeyHelper.generateRegistrationId(false);

  var preKeys = KeyHelper.generatePreKeys(0, 110);

  var signedPreKey = KeyHelper.generateSignedPreKey(identityKeyPair, 0);

  // ignore: unused_local_variable
  var sessionStore = InMemorySessionStore();
  var preKeyStore = InMemoryPreKeyStore();
  var signedPreKeyStore = InMemorySignedPreKeyStore();
  // ignore: unused_local_variable
  var identityStore =
      InMemoryIdentityKeyStore(identityKeyPair, registerationId);

  for (var p in preKeys) {
    preKeyStore.storePreKey(p.id, p);
  }
  signedPreKeyStore.storeSignedPreKey(signedPreKey.id, signedPreKey);

  var remoteAddress = SignalProtocolAddress("remote", 1);
  var sessionBuilder = SessionBuilder(sessionStore, preKeyStore,
      signedPreKeyStore, identityStore, remoteAddress);

  // sessionBuilder.processPreKeyBundle(retrievedPreKey);

  var sessionCipher = SessionCipher(sessionStore, preKeyStore,
      signedPreKeyStore, identityStore, remoteAddress);
  var ciphertext = sessionCipher.encrypt(utf8.encode("Hello Mixin"));

  print(ciphertext);
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'signal protocall',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: groupSessioin,
        tooltip: "clickme",
        child: Icon(Icons.add),
      ),
    );
  }

  void groupSessioin() {
    var senderKeyName = SenderKeyName("", SignalProtocolAddress("sender", 1));
    var senderKeyStore = InMemorySenderKeyStore();
    var groupSession = GroupCipher(senderKeyStore, senderKeyName);
    print(groupSession.encrypt(utf8.encode("Hello Mixin")).toString());
  }
}

@xni06
Copy link

xni06 commented Jan 28, 2021

It doesn't look like the example app works - see #24

However, the unit tests do work and it is from those that I figured out how to integrate the library into my Flutter app and to also use Firebase as the Serverless backend.

Take a look at how the library has been used in the following tests:

MacBook:libsignal_protocol_dart$ dart test test/session_builder_test.dart 
00:09 +3: All tests passed!              
                                                                                                                                   
MacBook:libsignal_protocol_dart$ dart test test/session_cipher_test.dart 
00:06 +1: testMessageKeyLimits                                                                                                                                              
  Skip: Failing historical test
00:06 +1 ~1: All tests passed!     

@woinbo
Copy link

woinbo commented Jan 28, 2021

It doesn't look like the example app works - see #24

However, the unit tests do work and it is from those that I figured out how to integrate the library into my Flutter app and to also use Firebase as the Serverless backend.

Take a look at how the library has been used in the following tests:

MacBook:libsignal_protocol_dart$ dart test test/session_builder_test.dart 
00:09 +3: All tests passed!              
                                                                                                                                   
MacBook:libsignal_protocol_dart$ dart test test/session_cipher_test.dart 
00:06 +1: testMessageKeyLimits                                                                                                                                              
  Skip: Failing historical test
00:06 +1 ~1: All tests passed!     

All tests pass! but then too it's giving the error. What should be done now ??

@xni06
Copy link

xni06 commented Jan 28, 2021

All tests pass! but then too it's giving the error. What should be done now ??

There's no error when the tests are run - there's an invalid test hence why it's been marked as being skipped.

Look at the runInteraction method in the above tests as they exchange messages in there.

In essence, all this library does is to provide you with the API calls to encrypt/decrypt messages in precisely the same way as its Java counterpart, libsignal-protocol-java.

To make use of these libraries, we have to do some leg work by looking at how the calls are made within the tests.

Once this has been understood, you can then write an SDK to make it easier for the app to consume. I've done this myself but I cannot share it with you as it's not yet open-source. If it were, it would make your life, and a lot of others, a hell of a lot easier!

@woinbo
Copy link

woinbo commented Jan 31, 2021

All tests pass! but then too it's giving the error. What should be done now ??

There's no error when the tests are run - there's an invalid test hence why it's been marked as being skipped.

Look at the runInteraction method in the above tests as they exchange messages in there.

In essence, all this library does is to provide you with the API calls to encrypt/decrypt messages in precisely the same way as its Java counterpart, libsignal-protocol-java.

To make use of these libraries, we have to do some leg work by looking at how the calls are made within the tests.

Once this has been understood, you can then write an SDK to make it easier for the app to consume. I've done this myself but I cannot share it with you as it's not yet open-source. If it were, it would make your life, and a lot of others, a hell of a lot easier!

please sir, do update the readme file or example on how to set up this as I really need signal e2e in my app. It's a humble request

@xni06
Copy link

xni06 commented Jan 31, 2021

please sir, do update the readme file or example on how to set up this as I really need signal e2e in my app. It's a humble request

It doesn't look like the example app works - see #24

Please add your comment to the issue linked above as I do not own this repository - I'm just a consumer like yourself.

@crossle
Copy link
Member

crossle commented Feb 2, 2021

@sky-flutter try the latest example

@woinbo
Copy link

woinbo commented Feb 2, 2021

I tried the latest example and tried to decrypt the "ciphertext", but it shows this error Instance of 'UntrustedIdentityException'

@woinbo
Copy link

woinbo commented Feb 2, 2021

I tried the latest example and tried to decrypt the "ciphertext", but it shows this error Instance of 'UntrustedIdentityException'

import 'dart:convert';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:libsignal_protocol_dart/libsignal_protocol_dart.dart';

void main() {
  runApp(MyApp());
}

var ciphertext;

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  void install() {
    var identityKeyPair = KeyHelper.generateIdentityKeyPair();
    var registerationId = KeyHelper.generateRegistrationId(false);

    var preKeys = KeyHelper.generatePreKeys(0, 110);

    var signedPreKey = KeyHelper.generateSignedPreKey(identityKeyPair, 0);

    var sessionStore = InMemorySessionStore();
    var preKeyStore = InMemoryPreKeyStore();
    var signedPreKeyStore = InMemorySignedPreKeyStore();
    var identityStore =
        InMemoryIdentityKeyStore(identityKeyPair, registerationId);

    for (var p in preKeys) {
      preKeyStore.storePreKey(p.id, p);
    }
    signedPreKeyStore.storeSignedPreKey(signedPreKey.id, signedPreKey);

    var remoteAddress = SignalProtocolAddress('remote', 1);
    var sessionBuilder = SessionBuilder(sessionStore, preKeyStore,
        signedPreKeyStore, identityStore, remoteAddress);

    // Should get remote from the server
    var remoteRegId = KeyHelper.generateRegistrationId(false);
    var remoteIdentityKeyPair = KeyHelper.generateIdentityKeyPair();
    var remotePreKeys = KeyHelper.generatePreKeys(0, 110);
    var remoteSignedPreKey =
        KeyHelper.generateSignedPreKey(remoteIdentityKeyPair, 0);

    var retrievedPreKey = PreKeyBundle(
        remoteRegId,
        1,
        remotePreKeys[0].id,
        remotePreKeys[0].getKeyPair().publicKey,
        remoteSignedPreKey.id,
        remoteSignedPreKey.getKeyPair().publicKey,
        remoteSignedPreKey.signature,
        remoteIdentityKeyPair.getPublicKey());

    sessionBuilder.processPreKeyBundle(retrievedPreKey);

    var sessionCipher = SessionCipher(sessionStore, preKeyStore,
        signedPreKeyStore, identityStore, remoteAddress);
    setState(() {
      ciphertext =
          sessionCipher.encrypt(Uint8List.fromList(utf8.encode('Hello Mixin')));
    });

    PreKeySignalMessage preKeySignalMessage =
        PreKeySignalMessage(ciphertext.serialize());
    print(ciphertext.serialize());
    print((sessionCipher.decrypt(preKeySignalMessage)));
  }

  void groupSessioin() {
    var senderKeyName = SenderKeyName("", SignalProtocolAddress("sender", 1));
    var senderKeyStore = InMemorySenderKeyStore();
    var groupSession = GroupCipher(senderKeyStore, senderKeyName);
    groupSession.encrypt(Uint8List.fromList(utf8.encode('Hello Mixin')));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Container(
            child: Center(
              child: Column(
                children: [
                  Text(
                    ciphertext == null
                        ? "asd"
                        : ciphertext.serialize().toString(),
                  ),
                  TextButton(
                    onPressed: install,
                    child: Text("press me"),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

@xni06
Copy link

xni06 commented Feb 2, 2021

Take a look at this and work your way up the thread: #23 (comment)

@crossle
Copy link
Member

crossle commented Feb 2, 2021

c6326d1

@crossle crossle closed this as completed Feb 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants