Exemplo n.º 1
0
        public static EncryptionKey FromStream([NotNull] Stream stream)
        {
            using (var wrapper = new StreamWrapper(stream))
            {
                if (!wrapper.Read(out byte format))
                {
                    throw new EndOfStreamException();
                }

                if (!wrapper.Read(out bool compressed))
                {
                    throw new EndOfStreamException();
                }

                EncryptionKey encryptionKey;

                switch ((KeyFormat)format)
                {
                case KeyFormat.Aes:
                    encryptionKey = new AesKey();

                    break;

                case KeyFormat.Rsa:
                    encryptionKey = new RsaKey();

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                encryptionKey.Compressed = compressed;

                if (!encryptionKey.Read(stream))
                {
                    throw new Exception();
                }

                return(encryptionKey);
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public override bool Execute()
        {
            if (string.IsNullOrWhiteSpace(OutputDirectory))
            {
                Log?.LogError(KeyGenerationTaskStrings.OutputDirectoryInvalid);

                return(false);
            }

            if (!Directory.Exists(OutputDirectory))
            {
                try
                {
                    Directory.CreateDirectory(OutputDirectory);
                }
                catch (Exception exception)
                {
                    Log?.LogError(KeyGenerationTaskStrings.ErrorCreatingOutputDirectory, OutputDirectory);
                    Log?.LogErrorFromException(exception);
                }
            }

            var power = Math.Log(KeySize, 0);

            if (KeySize % 2 != 0 || power < 10 || power - Math.Floor(power) > 0.001)
            {
                Log?.LogError(KeyGenerationTaskStrings.KeySizeInvalid, KeySize);

                return(false);
            }

            var pathPrivateKey = Path.Combine(OutputDirectory, "network.handshake.bkey");
            var pathPublicKey  = Path.Combine(OutputDirectory, "network.handshake.bkey.pub");

            using (var rsa = new RSACryptoServiceProvider(KeySize))
            {
                var writePrivateKey = true;
                if (!GenerateEachBuild)
                {
                    if (File.Exists(pathPrivateKey))
                    {
                        if (File.Exists(pathPublicKey))
                        {
                            return(true);
                        }

                        Log?.LogWarning(KeyGenerationTaskStrings.PublicKeyMissing);
                    }

                    try
                    {
                        using (var stream = File.OpenRead(pathPrivateKey))
                        {
                            var privateKey = new RsaKey();
                            privateKey.Read(stream);

                            if (privateKey.IsPublic || privateKey.Format != KeyFormat.Rsa)
                            {
                                Log?.LogWarning(KeyGenerationTaskStrings.PrivateKeyInvalid);
                            }
                            else
                            {
                                rsa.ImportParameters(privateKey.Parameters);
                                writePrivateKey = false;
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        Log?.LogWarning(KeyGenerationTaskStrings.ErrorReadingPrivateKey);
                        Log?.LogWarningFromException(exception);
                    }
                }

                if (writePrivateKey)
                {
                    var privateKey = new RsaKey(rsa.ExportParameters(true));
                    try
                    {
                        using (var stream = File.OpenWrite(pathPrivateKey))
                        {
                            privateKey.Write(stream);
                        }
                    }
                    catch (Exception exception)
                    {
                        Log?.LogError(KeyGenerationTaskStrings.ErrorWritingPrivateKey);
                        Log?.LogErrorFromException(exception);

                        return(false);
                    }
                }

                var publicKey = new RsaKey(rsa.ExportParameters(false));
                try
                {
                    using (var stream = File.OpenWrite(pathPublicKey))
                    {
                        publicKey.Write(stream);
                    }
                }
                catch (Exception exception)
                {
                    Log?.LogError(KeyGenerationTaskStrings.ErrorWritingPublicKey);
                    Log?.LogErrorFromException(exception);

                    return(false);
                }

                return(true);
            }
        }