cardoso / virgil-e3kit-flutter

🔐 Add end-to-end encryption to your Flutter App - Unofficial E3Kit flutter plugin
Other
45 stars 8 forks source link

Unhandled Exception is thrown when calling decrypt method #1

Closed Cyrus964 closed 4 years ago

Cyrus964 commented 4 years ago

Hi,

I'm facing the below exception when I'm calling the decrypt method.

E/flutter (17137): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(unknown_error, Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference, null)
E/flutter (17137): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (17137): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)
E/flutter (17137): <asynchronous suspension>
E/flutter (17137): #2      EThree._invokeMethod (package:e3kit/src/ethree.dart:81:21)
E/flutter (17137): #3      EThree.decrypt (package:e3kit/src/ethree.dart:55:12)

This is my test class for the e3kit:

import 'dart:async';
import 'package:cloud_functions/cloud_functions.dart';
import 'package:e3kit/e3kit.dart';

class User {
    String id;
    String password;
}

class CryptoService {

  EThree _eThree;

  Future<void> init(User user) async {

    _eThree = await EThree.init(user.id, _tokenCallBack());
    if (_eThree == null) {
      throw Exception("Unable to initialize security keys for user: ${user.id}");
    }

    // Check if the user have valid key pair values on Virgil Cloud
    await _eThree.findUsers([user.id]).catchError((e) {

      print("Create security keys for user: ${user.id}");
      _eThree.register()
          .whenComplete(() { // Backup private key for multi-device use
            _eThree.backupPrivateKey(user.password).catchError((e) => throw Exception("Unable to create a backup of the private key."));
          })
          .catchError((e) => throw Exception("Unable to create security keys for user: ${user.id}"));
    });

    // Check if this app contains the private key loaded
    _eThree.hasLocalPrivateKey().then((hasKey) {
      if (!hasKey) {
        _eThree.restorePrivateKey(user.password).catchError((e) => throw Exception("Unable to restore the private key."));
      }
    });
  }

  Future<String> encrypt(String text) async {
    if (text == null) {
      return null;
    }
    return _eThree.encrypt(text);
  }

  Future<String> decrypt(String text) async {
    if (text == null) {
      return null;
    }
    return _eThree.decrypt(text);
  }

  Function _tokenCallBack() {
    return () async {

      final HttpsCallable jwtFunction = CloudFunctions.instance.getHttpsCallable(
        functionName: 'getVirgilJwt',
      );

      HttpsCallableResult jwtResult = await jwtFunction.call();
      return jwtResult.data['token'];
    };
  }
}

final CryptoService cryptoService = CryptoService();

And below the code I'm using to test that decrypt works with encrypted text for the user.

// do here Firebase authentication process first
User user = User(firebaseUser.uid, 'myBackupPassword');

// Initialize crypto service for the user
cryptoService.init(user);

// This code is triggered by onPressed() method of FlatButton when I'm sure that cryptoService.init() is successfull.
String encrypted = await cryptoService.encrypt("Some clear text.");
String decrypted = await cryptoService.decrypt(encrypted);
print("Decrypted text = $decrypted");

As you can see in the debugger, the encrypted text is not null in the eThree dart module and should be not null in the FlutterEThree kotlin class.

image

I suspect later on the eThree Java/Kotlin SDK that the text argument is lost which triggers this null pointer when trying to call the toCharArray() method.

Cyrus964 commented 4 years ago

After some investigations, it seams that there is no issue on the virgil-e3kit flutter plugin.

My testing Flutter project was created long time ago and has evolved with many Flutter updates. By creating a new one from scratch with the Android Studio wizzard in the latest Flutter version (v1.13.6) everything works now using below dependencies as minimum app integrated with Firebase Auth.

firebase_auth: ^0.15.3 google_sign_in: ^4.1.1 cloud_firestore: ^0.13.0+1 cloud_functions: ^0.4.1+6

I will see by restoring the pages of my app if any plugin can causes troubles to the e3kit.

Issue can be closed.