Migrate from steel_crypt to crypton

SteelCrypt is deprecating asymetric key crypto functions and recommends
using crypton. This also simplifies our code.
This commit is contained in:
Vishesh Handa
2020-11-15 00:25:39 +01:00
parent 215fe07d50
commit a052467ebc
3 changed files with 26 additions and 56 deletions

View File

@ -1,16 +1,11 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';
import 'package:isolate/isolate_runner.dart'; import 'package:isolate/isolate_runner.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:ssh_key/ssh_key.dart' as ssh_key; import 'package:ssh_key/ssh_key.dart' as ssh_key;
import 'package:steel_crypt/PointyCastleN/key_generators/rsa_key_generator.dart';
import 'package:steel_crypt/PointyCastleN/pointycastle.dart';
import 'package:steel_crypt/PointyCastleN/random/fortuna_random.dart';
import 'package:steel_crypt/steel_crypt.dart';
import 'package:crypton/crypton.dart';
import 'package:gitjournal/error_reporting.dart'; import 'package:gitjournal/error_reporting.dart';
import 'package:gitjournal/ssh/binary_length_value.dart'; import 'package:gitjournal/ssh/binary_length_value.dart';
import 'package:gitjournal/utils/logger.dart'; import 'package:gitjournal/utils/logger.dart';
@ -23,8 +18,6 @@ class RsaKeyPair {
@required String privateKey, @required String privateKey,
@required String publicKey, @required String publicKey,
}) { }) {
var encrypter = RsaCrypt();
publicKey = publicKey.trim(); publicKey = publicKey.trim();
try { try {
var key = ssh_key.publicKeyDecode(publicKey); var key = ssh_key.publicKeyDecode(publicKey);
@ -37,41 +30,43 @@ class RsaKeyPair {
if (publicKey == null) { if (publicKey == null) {
try { try {
this.publicKey = encrypter.parseKeyFromString(publicKey); this.publicKey = RSAPublicKey.fromString(publicKey);
} catch (e) { } catch (e) {
// Ignore // Ignore
} }
} }
try { try {
this.privateKey = encrypter.parseKeyFromString(privateKey); this.privateKey = RSAPrivateKey.fromPEM(privateKey);
} catch (e) { } catch (e) {
// Ignore // Ignore
} }
} }
RsaKeyPair.generate() { RsaKeyPair.generate() {
var keyPair = _getRsaKeyPair(_getSecureRandom()); var keyPair = RSAKeypair.fromRandom();
publicKey = keyPair.publicKey as RSAPublicKey;
privateKey = keyPair.privateKey as RSAPrivateKey; publicKey = keyPair.publicKey;
privateKey = keyPair.privateKey;
} }
// Tries to encrypt and decrypt // Tries to encrypt and decrypt
bool isValid() { bool isValid() {
var encrypter = RsaCrypt();
var orig = 'word'; var orig = 'word';
var enc = encrypter.encrypt(orig, publicKey); var enc = publicKey.encrypt(orig);
var dec = encrypter.decrypt(enc, privateKey); var dec = privateKey.decrypt(enc);
return orig == dec; return orig == dec;
} }
// OpenSSH Public Key (single-line format) // OpenSSH Public Key (single-line format)
String publicKeyString({String comment = ""}) { String publicKeyString({String comment = ""}) {
var pk = publicKey.asPointyCastle;
var data = BinaryLengthValue.encode([ var data = BinaryLengthValue.encode([
BinaryLengthValue.fromString("ssh-rsa"), BinaryLengthValue.fromString("ssh-rsa"),
BinaryLengthValue.fromBigInt(publicKey.exponent), BinaryLengthValue.fromBigInt(pk.exponent),
BinaryLengthValue.fromBigInt(publicKey.modulus), BinaryLengthValue.fromBigInt(pk.modulus),
]); ]);
if (comment.isNotEmpty) { if (comment.isNotEmpty) {
@ -84,8 +79,7 @@ class RsaKeyPair {
} }
String privateKeyString() { String privateKeyString() {
var encrypter = RsaCrypt(); return privateKey.toPEM();
return encrypter.encodeKeyToString(privateKey);
} }
static Future<RsaKeyPair> generateAsync() async { static Future<RsaKeyPair> generateAsync() async {
@ -102,30 +96,6 @@ class RsaKeyPair {
} }
} }
SecureRandom _getSecureRandom() {
final secureRandom = FortunaRandom();
final random = Random.secure();
var seeds = List<int>.of([]);
for (var i = 0; i < 32; i++) {
seeds.add(random.nextInt(255));
}
secureRandom.seed(KeyParameter(Uint8List.fromList(seeds)));
return secureRandom;
}
///Create RSA keypair given SecureRandom.
AsymmetricKeyPair<PublicKey, PrivateKey> _getRsaKeyPair(
SecureRandom secureRandom,
) {
// See URL for why these values
// https://crypto.stackexchange.com/questions/15449/rsa-key-generation-parameters-public-exponent-certainty-string-to-key-count/15450#15450?newreg=e734eafab61e42f1b155b62839ccce8f
final rsapars = RSAKeyGeneratorParameters(BigInt.from(65537), 2048 * 2, 5);
final params = ParametersWithRandom(rsapars, secureRandom);
final keyGenerator = RSAKeyGenerator();
keyGenerator.init(params);
return keyGenerator.generateKeyPair();
}
FutureOr<RsaKeyPair> _gen(void _) async { FutureOr<RsaKeyPair> _gen(void _) async {
return RsaKeyPair.generate(); return RsaKeyPair.generate();
} }

View File

@ -35,7 +35,7 @@ packages:
name: asn1lib name: asn1lib
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.6.4" version: "0.8.1"
async: async:
dependency: transitive dependency: transitive
description: description:
@ -162,6 +162,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.5" version: "2.1.5"
crypton:
dependency: "direct main"
description:
name: crypton
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.3"
csslib: csslib:
dependency: transitive dependency: transitive
description: description:
@ -759,7 +766,7 @@ packages:
name: pointycastle name: pointycastle
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.2" version: "2.0.0"
pool: pool:
dependency: transitive dependency: transitive
description: description:
@ -953,7 +960,7 @@ packages:
name: ssh_key name: ssh_key
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.5.1" version: "0.6.0"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
@ -961,13 +968,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.10.0-nullsafety.1" version: "1.10.0-nullsafety.1"
steel_crypt:
dependency: "direct main"
description:
name: steel_crypt
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.1+1"
stream_channel: stream_channel:
dependency: transitive dependency: transitive
description: description:

View File

@ -41,13 +41,13 @@ dependencies:
git_url_parse2: ^0.0.1 git_url_parse2: ^0.0.1
synchronized: ^2.2.0 synchronized: ^2.2.0
mutex: ^1.0.3 mutex: ^1.0.3
steel_crypt: ^1.7.1+1 crypton: ^1.1.3
font_awesome_flutter: ^8.7.0 font_awesome_flutter: ^8.7.0
sentry: ">=3.0.0 <4.0.0" sentry: ">=3.0.0 <4.0.0"
flutter_sentry: ^0.4.4 flutter_sentry: ^0.4.4
equatable: ^1.1.0 equatable: ^1.1.0
cached_network_image: ^2.2.0+1 cached_network_image: ^2.2.0+1
ssh_key: ^0.5.1 ssh_key: ^0.6.0
isolate: ^2.0.3 isolate: ^2.0.3
image_picker: ^0.6.7 image_picker: ^0.6.7
easy_localization: ^2.3.2 easy_localization: ^2.3.2