public MessageClientIdentity(string systemName, string clientName, ECDiffieHellmanCurve curve) { ECDiffieHellman dh; var dhCurve = curve switch { ECDiffieHellmanCurve.P256 => ECCurve.NamedCurves.nistP256, ECDiffieHellmanCurve.P384 => ECCurve.NamedCurves.nistP384, ECDiffieHellmanCurve.P521 => ECCurve.NamedCurves.nistP521, _ => throw new ArgumentOutOfRangeException(nameof(curve)) }; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { dh = new ECDiffieHellmanCng(dhCurve); ECDsa = new ECDsaCng(); } else { #if (!NET48) dh = new ECDiffieHellmanOpenSsl(); #else throw new NotSupportedException("ECDiffieHellmanOpenSsl is not supported in .NET 4.8"); #endif ECDsa = new ECDsaOpenSsl(); } SystemName = systemName; Name = clientName; ECDiffieHellman = dh; ECDsa.ImportParameters(dh.ExportExplicitParameters(true)); }
internal MessageClientIdentity(string systemName, string clientName, byte[] pkcs8PrivateKey, ECDiffieHellmanCurve curve) { SystemName = systemName; Name = clientName; ECDiffieHellman = CreateDH(curve, false); int bytesRead; ECDiffieHellman.ImportPkcs8PrivateKey(pkcs8PrivateKey, out bytesRead); }
private static void GenerateKey(GenerateClientOptions generateClientOptions, GenerateServerOptions generateServerOptions) { StringBuilder sb = new StringBuilder(); MessageClientIdentity clientInfo = null; GenerateOptionsBase generateOptionsBase = generateClientOptions != null ? (GenerateOptionsBase)generateClientOptions : (GenerateOptionsBase)generateServerOptions; if (generateOptionsBase.Directory == null) { generateOptionsBase.Directory = Directory.GetCurrentDirectory(); } else if (!Directory.Exists(generateOptionsBase.Directory)) { Console.WriteLine($"{generateOptionsBase.Directory} is not a valid directory."); Environment.Exit(-1); } ECDiffieHellmanCurve curve = ECDiffieHellmanCurve.P256; if (generateOptionsBase.KeySize256) { curve = ECDiffieHellmanCurve.P256; } else if (generateOptionsBase.KeySize384) { curve = ECDiffieHellmanCurve.P384; } else if (generateOptionsBase.KeySize521) { curve = ECDiffieHellmanCurve.P521; } if (generateClientOptions != null) { clientInfo = new MessageClientIdentity(generateClientOptions.SystemName, generateClientOptions.ClientName, curve); } else if (generateServerOptions != null) { //TODO: Need to get server name from somehwere else. clientInfo = new MessageClientIdentity(generateServerOptions.SystemName, "Server", curve); } string path = Path.Combine(generateOptionsBase.Directory, clientInfo.SystemName + "-" + clientInfo.Name); string publicFile = path + ".pub"; File.WriteAllText(publicFile, clientInfo.SerializePublicInfoToJson()); sb.AppendLine("\nCreating Client Key"); sb.AppendLine($" Diffie-Hellman Elliptic Curve {curve}"); sb.AppendLine($" System Name: {clientInfo.SystemName}"); sb.AppendLine($" Client Name: {clientInfo.Name}"); sb.AppendLine($" Identity Hash: {clientInfo.IdentityHash}"); sb.AppendLine($" Public Key File: {publicFile}"); //Console.WriteLine($" Private Key File: {privateFile}"); // Private Key saving if (generateOptionsBase.JsonPrivateKey) { string password = ""; bool match = false; string privateFile = path + ".priv"; do { do { Console.Write("Enter password: "******"Re-enter password: "******"Passwords do not match."); match = false; } else { match = true; } } while (!match); if (password == "") { Console.Write("You have entered a blank password. Are you sure you want to save the private key without a password (not recommended!)? Type YES to continue without password: "******"YES") { Console.WriteLine(); File.WriteAllText(privateFile, clientInfo.SerializePrivateInfoToJson(Encryption.None)); sb.AppendLine($" Unencrypted Private Key File: {privateFile}"); break; } } else { File.WriteAllText(privateFile, clientInfo.SerializePrivateInfoToJson(Encryption.Password, password)); sb.AppendLine($" Password-Protected Private Key File: {privateFile}"); break; } } while (true); Console.WriteLine(); } if (generateOptionsBase.LocalMachineEncryptedPrivateKey) { string privateFile = path + ".privl"; File.WriteAllText(privateFile, clientInfo.SerializePrivateInfoToJson(Encryption.LocalMachine)); sb.AppendLine($" Current User-Encrypted Private Key File: {privateFile}"); } if (generateOptionsBase.CurrentUserEncryptedPrivateKey) { string privateFile = path + ".privu"; File.WriteAllText(privateFile, clientInfo.SerializePrivateInfoToJson(Encryption.CurrentUser)); sb.AppendLine($" Local Machine-Encrypted Private Key File: {privateFile}"); } Console.WriteLine(sb.ToString()); // TEMP Test Code //var idu = MessageClientIdentity.ImportFromFile(path + ".privu"); //var idl = MessageClientIdentity.ImportFromFile(path + ".privl"); //var idj = MessageClientIdentity.ImportFromFile(path + ".priv", "duhh"); }