From d57e62eaf0d28ef49e9b2df79f17b39cde5d60e8 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 8 Jun 2021 00:04:22 +0200 Subject: [PATCH] ECC: Second attempt at encoding the public string --- lib/ssh/keygen.dart | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/ssh/keygen.dart b/lib/ssh/keygen.dart index 955ec95e..25c4a9de 100644 --- a/lib/ssh/keygen.dart +++ b/lib/ssh/keygen.dart @@ -91,8 +91,38 @@ Future generateSSHEccKeys({required String comment}) async { // FIXME: I need to learn to convert from the public key PEM format to ecdsa-sha2-nistp384 return SshKey( - publicKey: publicPem, + publicKey: publicKeyString(publicKey, comment), privateKey: privatePem, password: "", ); } + +// https://datatracker.ietf.org/doc/html/rfc5656 +String publicKeyString(ECPublicKey publicKey, String comment) { + var publicPem = CryptoUtils.encodeEcPublicKeyToPem(publicKey); + + print('public PEM'); + print(publicPem); + print('\n'); + + var publicKeyBytes2 = CryptoUtils.getBytesFromPEMString(publicPem); + + var publicKeyBytes = publicKey.Q!.getEncoded(false); + if (publicKeyBytes != publicKeyBytes2) { + print("THE BYTES ARE NOT EQAU~L"); + } + print("HUURAY"); + + var data = BinaryLengthValue.encode([ + BinaryLengthValue.fromString("ecdsa-sha2-nistp384"), + BinaryLengthValue(publicKeyBytes), + ]); + + if (comment.isNotEmpty) { + comment = comment.replaceAll('\r', ' '); + comment = comment.replaceAll('\n', ' '); + comment = ' $comment'; + } + + return 'ecdsa-sha2-nistp384 ${base64.encode(data)}$comment'; +}