/// <summary>
        /// Parse the GPG version information from the Stdout of the GPG command line interface
        /// executed with the --version argument.
        /// </summary>
        /// <param name="data">stdout stream data</param>
        /// <returns>GPG version object</returns>
        public static GpgVersion Parse(StreamReader data)
        {
            string     firstLine = ReadFirstStdOutLine(data);
            GpgVersion ver       = ParseVersion(firstLine);

            return(ver);
        }
示例#2
0
        private string GetGpgArgs(ActionTypes action)
        {
            // validate input
            switch (action)
            {
            case ActionTypes.Encrypt:
            case ActionTypes.SignEncrypt:
                if (String.IsNullOrEmpty(_recipient))
                {
                    throw new GpgException("A Recipient is required before encrypting data.  Please specify a valid recipient using the Recipient property on the GnuPG object.");
                }
                break;
            }

            StringBuilder options = new StringBuilder();

            //  set a home directory if the user specifies one
            if (!String.IsNullOrEmpty(_homePath))
            {
                options.Append(String.Format(CultureInfo.InvariantCulture, "--homedir \"{0}\" ", _homePath));
            }

            //  tell gpg to read the passphrase from the standard input so we can automate providing it
            options.Append("--passphrase-fd 0 ");

            // if gpg cli version is >= 2.1 then instruct gpg not to prompt for a password
            // by specifying the pinetnry-mode argument
            GpgVersion ver = GetGpgVersion();

            if ((ver.Major == 2 && ver.Minor >= 1) || ver.Major >= 3)
            {
                options.Append("--pinentry-mode loopback ");
            }

            //  turn off verbose statements
            options.Append("--no-verbose ");

            // use batch mode and never ask or allow interactive commands.
            options.Append("--batch ");

            //  always use the trusted model so we don't get an interactive session with gpg.exe
            options.Append("--trust-model always ");

            // if provided specify the key to use by local user name
            if (!String.IsNullOrEmpty(_localUser))
            {
                options.Append(String.Format(CultureInfo.InvariantCulture, "--local-user {0} ", _localUser));
            }

            // if provided specify the recipient key to use by recipient user name
            if (!String.IsNullOrEmpty(_recipient))
            {
                options.Append(String.Format(CultureInfo.InvariantCulture, "--recipient {0} ", _recipient));
            }

            // add any user specific options if provided
            if (!String.IsNullOrEmpty(_userOptions))
            {
                options.Append(_userOptions);
            }

            //  handle the action
            switch (action)
            {
            case ActionTypes.Encrypt:
                if (_outputType == OutputTypes.AsciiArmor)
                {
                    options.Append("--armor ");
                }

                // if a filename needs to be embedded in the encrypted blob, set it
                if (!String.IsNullOrEmpty(_filename))
                {
                    options.Append(String.Format(CultureInfo.InvariantCulture, "--set-filename \"{0}\" ", _filename));
                }

                options.Append("--encrypt ");
                break;

            case ActionTypes.Decrypt:
                options.Append("--decrypt ");
                break;

            case ActionTypes.Sign:
                switch (_outputSignatureType)
                {
                case OutputSignatureTypes.ClearText:
                    options.Append("--clearsign ");
                    break;

                case OutputSignatureTypes.Detached:
                    options.Append("--detach-sign ");
                    break;

                case OutputSignatureTypes.Signature:
                    options.Append("--sign ");
                    break;
                }
                break;

            case ActionTypes.SignEncrypt:
                if (_outputType == OutputTypes.AsciiArmor)
                {
                    options.Append("--armor ");
                }

                // if a filename needs to be embedded in the encrypted blob, set it
                if (!String.IsNullOrEmpty(_filename))
                {
                    options.Append(String.Format(CultureInfo.InvariantCulture, "--set-filename \"{0}\" ", _filename));
                }

                // determine which type of signature to generate
                switch (_outputSignatureType)
                {
                case OutputSignatureTypes.ClearText:
                    options.Append("--clearsign ");
                    break;

                case OutputSignatureTypes.Detached:
                    options.Append("--detach-sign ");
                    break;

                case OutputSignatureTypes.Signature:
                    options.Append("--sign ");
                    break;
                }
                break;

            case ActionTypes.Verify:
                options.Append("--verify ");
                break;

            case ActionTypes.Import:
                options.Append("--import ");
                break;
            }

            return(options.ToString());
        }
示例#3
0
        /// <summary>
        /// Gets the GPG binary version number as reported by the executable.
        /// </summary>
        /// <returns>GPG version number in the string format x.y.z</returns>
        public GpgVersion GetGpgVersion()
        {
            GpgVersion ver = GpgVersionParser.Parse(ExecuteGpgNoIO("--version"));

            return(ver);
        }