Skip to content

Commit

Permalink
ConfigSecureAP: add creation of key .dat files
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Nov 7, 2024
1 parent a8fcce4 commit 2fbeb9a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
45 changes: 45 additions & 0 deletions ExtLibs/Utilities/SignedFW.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 };
Expand Down Expand Up @@ -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();
}
}
}
}
23 changes: 18 additions & 5 deletions GCSViews/ConfigurationView/ConfigSecureAP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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.");
}
Expand Down

0 comments on commit 2fbeb9a

Please sign in to comment.