|
|
@@ -1,46 +1,135 @@
|
|
|
import 'dart:async';
|
|
|
import 'dart:convert';
|
|
|
import 'dart:io';
|
|
|
+import 'dart:math';
|
|
|
|
|
|
+import 'package:cookie_jar/cookie_jar.dart';
|
|
|
+import 'package:crypto/crypto.dart';
|
|
|
+import 'package:encrypt/encrypt.dart' as encrypt;
|
|
|
import 'package:encrypt/encrypt_io.dart';
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
-import 'package:encrypt/encrypt.dart';
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
import 'package:pointycastle/asymmetric/api.dart';
|
|
|
+import 'package:pointycastle/export.dart';
|
|
|
+import 'package:pointycastle/src/platform_check/platform_check.dart';
|
|
|
|
|
|
+import 'api.dart';
|
|
|
import 'global.dart';
|
|
|
|
|
|
-Future<RSAPrivateKey?> getRSAPrivateKey(String username) async {
|
|
|
- try {
|
|
|
- final storage = FlutterSecureStorage();
|
|
|
- final _key = 'e2ee chat private key of $username';
|
|
|
- String? s = await storage.read(key: _key);
|
|
|
- if (s == null) {
|
|
|
- // TODO: 如果没有私钥,需要生成
|
|
|
- storage.write(key: _key, value: s);
|
|
|
+
|
|
|
+class RSAPublicKeyStore extends RSAPublicKey {
|
|
|
+ RSAPublicKeyStore(BigInt modulus, BigInt exponent) : super(modulus, exponent);
|
|
|
+
|
|
|
+ Map<String, dynamic> toJson() => {"modulus": modulus.toString(), "exponent": exponent.toString()};
|
|
|
+
|
|
|
+ factory RSAPublicKeyStore.fromJson(Map<String, dynamic> map) {
|
|
|
+ return RSAPublicKeyStore(BigInt.parse(map["modulus"]), BigInt.parse(map["exponent"]));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Future<void> save(String key) async {
|
|
|
+ try {
|
|
|
+ final storage = FlutterSecureStorage();
|
|
|
+ storage.write(key: key, value: jsonEncode(toJson()));
|
|
|
+ } catch (e) {
|
|
|
+ debug('save $key error: $e');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static Future<RSAPublicKeyStore?> load(String key) async {
|
|
|
+ RSAPublicKeyStore? result;
|
|
|
+ try {
|
|
|
+ final storage = FlutterSecureStorage();
|
|
|
+ final json = await storage.read(key: key);
|
|
|
+ result = RSAPublicKeyStore.fromJson(jsonDecode(json!));
|
|
|
+ } catch (e) {
|
|
|
+ debug('load $key error: $e');
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class RSAPrivateKeyStore extends RSAPrivateKey {
|
|
|
+ RSAPrivateKeyStore(BigInt modulus, BigInt privateExponent, BigInt p, BigInt q)
|
|
|
+ : super(modulus, privateExponent, p, q);
|
|
|
+ Map<String, dynamic> toJson() => {
|
|
|
+ "modulus": modulus.toString(),
|
|
|
+ "privateExponent": privateExponent.toString(),
|
|
|
+ "p": p.toString(),
|
|
|
+ "q": q.toString(),
|
|
|
+ };
|
|
|
+ factory RSAPrivateKeyStore.fromJson(Map<String, dynamic> map) {
|
|
|
+ return RSAPrivateKeyStore(
|
|
|
+ BigInt.parse(map["modulus"]),
|
|
|
+ BigInt.parse(map["privateExponent"]),
|
|
|
+ BigInt.parse(map["p"]),
|
|
|
+ BigInt.parse(map["q"]),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Future<void> save(String key) async {
|
|
|
+ try {
|
|
|
+ final storage = FlutterSecureStorage();
|
|
|
+ storage.write(key: key, value: jsonEncode(toJson()));
|
|
|
+ } catch (e) {
|
|
|
+ debug('save $key error: $e');
|
|
|
}
|
|
|
- Directory tempDir = await getTemporaryDirectory();
|
|
|
- String tempPath = tempDir.path;
|
|
|
- String filePath = '$tempPath/private.pem';
|
|
|
- final file = File(filePath);
|
|
|
- final privateKey = await parseKeyFromFile<RSAPrivateKey>(filePath);
|
|
|
- file.delete();
|
|
|
- return privateKey;
|
|
|
- } catch (e) {
|
|
|
- debug('get rsa private key failed: $e');
|
|
|
+ }
|
|
|
+
|
|
|
+ static Future<RSAPrivateKeyStore?> load(String key) async {
|
|
|
+ RSAPrivateKeyStore? result;
|
|
|
+ try {
|
|
|
+ final storage = FlutterSecureStorage();
|
|
|
+ final json = await storage.read(key: key);
|
|
|
+ result = RSAPrivateKeyStore.fromJson(jsonDecode(json!));
|
|
|
+ } catch (e) {
|
|
|
+ debug('load $key error: $e');
|
|
|
+ }
|
|
|
+ return result;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-Future<RSAPublicKey?> getRSAPublicKey(String contents) async {
|
|
|
+AsymmetricKeyPair<RSAPublicKeyStore, RSAPrivateKeyStore> generateRSAKeyPair({SecureRandom? secureRandom, int bitLength = 2048}) {
|
|
|
+ // Create an RSA key generator and initialize it
|
|
|
+
|
|
|
+ secureRandom ??= getSecureRandom();
|
|
|
+
|
|
|
+ final keyGen = RSAKeyGenerator()
|
|
|
+ ..init(ParametersWithRandom(RSAKeyGeneratorParameters(BigInt.parse('65537'), bitLength, 64), secureRandom));
|
|
|
+
|
|
|
+ // Use the generator
|
|
|
+
|
|
|
+ final pair = keyGen.generateKeyPair();
|
|
|
+
|
|
|
+ // Cast the generated key pair into the RSA key types
|
|
|
+
|
|
|
+ final myPublic = pair.publicKey as RSAPublicKeyStore;
|
|
|
+ final myPrivate = pair.privateKey as RSAPrivateKeyStore;
|
|
|
+
|
|
|
+ return AsymmetricKeyPair<RSAPublicKeyStore, RSAPrivateKeyStore>(myPublic, myPrivate);
|
|
|
+}
|
|
|
+
|
|
|
+SecureRandom getSecureRandom() {
|
|
|
+ final secureRandom = SecureRandom('Fortuna')..seed(KeyParameter(encrypt.SecureRandom(32).bytes));
|
|
|
+ return secureRandom;
|
|
|
+}
|
|
|
+
|
|
|
+Future<AsymmetricKeyPair<RSAPublicKeyStore, RSAPrivateKeyStore>> getRSAKeyPair(String username) async {
|
|
|
+ final _privateKey = 'e2ee chat private key of $username';
|
|
|
+ final _publicKey = 'e2ee chat public key of $username';
|
|
|
try {
|
|
|
- Directory tempDir = await getTemporaryDirectory();
|
|
|
- String tempPath = tempDir.path;
|
|
|
- String filePath = '$tempPath/public.pem';
|
|
|
- final file = File(filePath);
|
|
|
- file.writeAsString(contents);
|
|
|
- final publicKey = await parseKeyFromFile<RSAPublicKey>(filePath);
|
|
|
- return publicKey;
|
|
|
+ final privateKey = await RSAPrivateKeyStore.load(_privateKey);
|
|
|
+ final publicKey = await RSAPublicKeyStore.load(_publicKey);
|
|
|
+ if (privateKey != null && publicKey != null) {
|
|
|
+ return AsymmetricKeyPair<RSAPublicKeyStore, RSAPrivateKeyStore>(publicKey, privateKey);
|
|
|
+ }
|
|
|
} catch (e) {
|
|
|
- debug('get rsa public key failed: $e');
|
|
|
+ debug('get rsa Public key failed: $e');
|
|
|
}
|
|
|
-}
|
|
|
+ final pair = generateRSAKeyPair();
|
|
|
+ pair.publicKey.save(_publicKey);
|
|
|
+ pair.privateKey.save(_privateKey);
|
|
|
+ await Api().addPublicKey(jsonEncode(pair.publicKey.toJson()));
|
|
|
+ return pair;
|
|
|
+}
|