diff --git a/ExtLibs/Utilities/SignedFW.cs b/ExtLibs/Utilities/SignedFW.cs index 75eb7079dd..ce04882082 100644 --- a/ExtLibs/Utilities/SignedFW.cs +++ b/ExtLibs/Utilities/SignedFW.cs @@ -8,6 +8,7 @@ using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Crypto.Prng; using Org.BouncyCastle.Security; namespace MissionPlanner.Utilities @@ -29,6 +30,20 @@ public static AsymmetricCipherKeyPair GenerateKey() return keyPairg; } + public static AsymmetricCipherKeyPair GenerateKey(byte[] knownseed) + { + //Creating Random + var secureRandom = new SecureRandom(new preseedrandom(knownseed)); + + //Parameters creation using the random and keysize + var keyGenParam = new KeyGenerationParameters(secureRandom, 256); + + var generator = new Ed25519KeyPairGenerator(); + generator.Init(keyGenParam); + AsymmetricCipherKeyPair keyPairg = generator.GenerateKeyPair(); + return keyPairg; + } + public static byte[] CreateSignedBL(AsymmetricCipherKeyPair keyPair, string filename) { var descriptor = new byte[] { 0x4e, 0xcf, 0x4e, 0xa5, 0xa6, 0xb6, 0xf7, 0x29 }; @@ -124,5 +139,35 @@ public static byte[] CreateSignedAPJ(AsymmetricCipherKeyPair keyPair, string fil return System.Text.ASCIIEncoding.ASCII.GetBytes(JsonConvert.SerializeObject(d, Formatting.Indented)); } + + private class preseedrandom : IRandomGenerator + { + private byte[] knownseed; + + public preseedrandom(byte[] knownseed) + { + this.knownseed = knownseed; + } + + public void AddSeedMaterial(byte[] seed) + { + throw new NotImplementedException(); + } + + public void AddSeedMaterial(long seed) + { + throw new NotImplementedException(); + } + + public void NextBytes(byte[] bytes) + { + Array.Copy(knownseed, bytes, bytes.Length); + } + + public void NextBytes(byte[] bytes, int start, int len) + { + throw new NotImplementedException(); + } + } } } diff --git a/GCSViews/ConfigurationView/ConfigSecureAP.cs b/GCSViews/ConfigurationView/ConfigSecureAP.cs index 1e4662b587..c5bb2aa4c5 100644 --- a/GCSViews/ConfigurationView/ConfigSecureAP.cs +++ b/GCSViews/ConfigurationView/ConfigSecureAP.cs @@ -29,14 +29,23 @@ public ConfigSecureAP() private void but_privkey_Click(object sender, System.EventArgs e) { - openFileDialog1.DefaultExt = ".pem"; - openFileDialog1.Filter = "*.pem|*.pem"; + openFileDialog1.DefaultExt = ".pem;.dat"; + openFileDialog1.Filter = "*.pem;*.dat|*.pem;*.dat"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { var pem = File.ReadAllText(openFileDialog1.FileName); - PemReader pr = new PemReader(new StringReader(pem)); - var key = (Ed25519PrivateKeyParameters)pr.ReadObject(); - keyPair = new AsymmetricCipherKeyPair(key.GeneratePublicKey(), key); + if (pem.Contains("PRIVATE_KEYV1")) + { + pem = pem.Replace("PRIVATE_KEYV1:", ""); + var keyap = Convert.FromBase64String(pem); + keyPair = SignedFW.GenerateKey(keyap); + } + else + { + PemReader pr = new PemReader(new StringReader(pem)); + var key = (Ed25519PrivateKeyParameters)pr.ReadObject(); + keyPair = new AsymmetricCipherKeyPair(key.GeneratePublicKey(), key); + } txt_pubkey.Text = Convert.ToBase64String(((Ed25519PublicKeyParameters)keyPair.Public).GetEncoded()); } } @@ -92,6 +101,10 @@ private void but_generatekey_Click(object sender, EventArgs e) if (sfd.ShowDialog() == DialogResult.OK) { File.WriteAllText(sfd.FileName, privatekey); + + File.WriteAllText(sfd.FileName.Replace(".pem", "_private_key.dat"), "PRIVATE_KEYV1:" + Convert.ToBase64String(((Ed25519PrivateKeyParameters)keyPair.Private).GetEncoded())); + File.WriteAllText(sfd.FileName.Replace(".pem", "_public_key.dat"), "PUBLIC_KEYV1:" + Convert.ToBase64String(((Ed25519PublicKeyParameters)keyPair.Public).GetEncoded())); + txt_pubkey.Text = Convert.ToBase64String(((Ed25519PublicKeyParameters)keyPair.Public).GetEncoded()); CustomMessageBox.Show("Protect your private key, if lost there is no method to get it back."); }