diff --git a/lib/ssh/rsa_key_pair.dart b/lib/ssh/rsa_key_pair.dart index e3434b99..7363886e 100644 --- a/lib/ssh/rsa_key_pair.dart +++ b/lib/ssh/rsa_key_pair.dart @@ -9,6 +9,8 @@ import 'package:steel_crypt/PointyCastleN/random/fortuna_random.dart'; import 'package:steel_crypt/steel_crypt.dart'; import 'package:meta/meta.dart'; +import 'package:ssh_key/ssh_key.dart' as ssh_key; + class RsaKeyPair { RSAPublicKey publicKey; RSAPrivateKey privateKey; @@ -18,8 +20,30 @@ class RsaKeyPair { @required String publicKey, }) { var encrypter = RsaCrypt(); - publicKey = encrypter.parseKeyFromString(publicKey); - privateKey = encrypter.parseKeyFromString(privateKey); + + publicKey = publicKey.trim(); + try { + var key = ssh_key.publicKeyDecode(publicKey); + if (key is ssh_key.RSAPublicKeyWithInfo) { + this.publicKey = RSAPublicKey(key.modulus, key.exponent); + } + } catch (e) { + // Ignore + } + + if (publicKey == null) { + try { + this.publicKey = encrypter.parseKeyFromString(publicKey); + } catch (e) { + // Ignore + } + } + + try { + this.privateKey = encrypter.parseKeyFromString(privateKey); + } catch (e) { + // Ignore + } } RsaKeyPair.generate() { diff --git a/pubspec.lock b/pubspec.lock index 83902105..68d8b78a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -553,6 +553,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.1" + pointycastle: + dependency: transitive + description: + name: pointycastle + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" pool: dependency: transitive description: @@ -733,6 +740,15 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.0+1" + ssh_key: + dependency: "direct main" + description: + path: "." + ref: HEAD + resolved-ref: "60e088a136a62cd1afe553e8858558f76df00f2d" + url: "https://github.com/GitJournal/ssh_key.git" + source: git + version: "0.5.0" stack_trace: dependency: transitive description: @@ -810,6 +826,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.9.5+1" + tuple: + dependency: transitive + description: + name: tuple + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 51163614..db9095e7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -46,6 +46,8 @@ dependencies: equatable: ^1.1.0 purchases_flutter: ^1.1.0 cached_network_image: ^2.1.0+1 + ssh_key: + git: https://github.com/GitJournal/ssh_key.git dev_dependencies: flutter_launcher_icons: "^0.7.2" diff --git a/test/rsa_key_pair_test.dart b/test/rsa_key_pair_test.dart index f4b4c1b3..fbd232fc 100644 --- a/test/rsa_key_pair_test.dart +++ b/test/rsa_key_pair_test.dart @@ -8,7 +8,6 @@ void main() { expect(pair.isValid(), true); }); - /* var publicKeySingleLine = 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAgEAiL0jjDdFqK/kYThqKt7THrjABTPWvXmB3URIpGKCP/jZlSuCUP3Oc+IxuFeXSIMvVIYeW2PZAjXQGTn60XzPHr+M0NoGcPAvzZf2u57aX3YKaL93cZSBHR97H+XhcYdrm7ATwfjMDgfgj7+VTvW4nI46Z+qjxmYifc8uVELolg1TDHWY789ggcdvy92oGjB0VUgMEywrOP+LS0DgG4dmkoUBWGP9dvYcPZDUF4q0XY9ZHhvyPWEZ3o2vETTrEJr9QHYwgjmFfJn2VFNnD/4qeDDHOmSlDgEOfQcZIm+XUOn9eVsv//dAPSY/yMJXf8d0ZSm+VS29QShMjA4R+7yh5WhsIhouBRno2PpEVVb37Xwe3V6U3o9UnQ3ADtL75DbrZ5beNWcmKzlJ7jVX5QzHSBAnePbBx/fyeP/f144xPtJWB3jW/kXjtPyWjpzGndaPQ0WgXkbf8fvIuB3NJTTcZ7PeIKnLaMIzT5XNCR+xobvdC8J9d6k84/q/laJKF3G8KbRGPNwnoVg1cwWFez+dzqo2ypcTtv/20yAmz86EvuohZoWrtoWvkZLCoyxdqO93ymEjgHAn2bsIWyOODtXovxAJqPgk3dxM1f9PAEQwc1bG+Z/Gc1Fd8DncgxyhKSQzLsfWroTnIn8wsnmhPJtaZWNuT5BJa8GhnzX09g6nhbk='; var publicKeySsh2 = '''---- BEGIN SSH2 PUBLIC KEY ---- @@ -27,7 +26,7 @@ AEQwc1bG+Z/Gc1Fd8DncgxyhKSQzLsfWroTnIn8wsnmhPJtaZWNuT5BJa8GhnzX0 9g6nhbk= ---- END SSH2 PUBLIC KEY ----'''; - test('Should write public key string correctly', () async { + test('Should read from OpenSSH format', () async { var pair = RsaKeyPair.fromStrings( publicKey: publicKeySsh2, privateKey: "", @@ -36,7 +35,14 @@ AEQwc1bG+Z/Gc1Fd8DncgxyhKSQzLsfWroTnIn8wsnmhPJtaZWNuT5BJa8GhnzX0 expect(pair.publicKeyString(), publicKeySingleLine); }); - */ + test('Should read from singleline format', () async { + var pair = RsaKeyPair.fromStrings( + publicKey: publicKeySingleLine, + privateKey: "", + ); + + expect(pair.publicKeyString(), publicKeySingleLine); + }); /* var privateOpenSshKey = '''-----BEGIN OPENSSH PRIVATE KEY-----