private int _passphrase_cb(IntPtr Hook, IntPtr uid_hint, IntPtr passphrase_info,
                                   int prev_was_bad, int fd)
        {
            bool   prevbad;
            string hint, info;

            char[] passwd = null;

            hint = Gpgme.PtrToStringUTF8(uid_hint);
            info = Gpgme.PtrToStringUTF8(passphrase_info);
            if (prev_was_bad > 0)
            {
                prevbad = true;
            }
            else
            {
                prevbad = false;
            }

            PassphraseResult result;

            try
            {
                PassphraseInfo pinfo = new PassphraseInfo(Hook, hint, info, prevbad);
                result = passphraseFunc(this, pinfo, ref passwd);
            }
            catch (Exception ex)
            {
                LastCallbackException = ex;
                passwd = "".ToCharArray();
                result = PassphraseResult.Canceled;
            }

            if (fd > 0)
            {
                byte[] utf8passwd = Gpgme.ConvertCharArrayToUTF8(passwd, 0);

                Stream fdstream = Gpgme.ConvertToStream(fd, FileAccess.Write);
                fdstream.Write(utf8passwd, 0, utf8passwd.Length);
                fdstream.Flush();
                fdstream.WriteByte((byte)'\n');
                fdstream.Flush();

                // try to wipe the passwords
                int i;
                for (i = 0; i < utf8passwd.Length; i++)
                {
                    utf8passwd[i] = 0;
                }
            }

            return((int)result);
        }
Beispiel #2
0
        private int PassphraseCb(IntPtr hook, IntPtr uid_hint, IntPtr passphrase_info,
                                 int prev_was_bad, int fd)
        {
            char[] passwd = null;

            string hint = Gpgme.PtrToStringUTF8(uid_hint);
            string info = Gpgme.PtrToStringUTF8(passphrase_info);

            bool prevbad = prev_was_bad > 0;

            PassphraseResult result;

            try {
                var pinfo = new PassphraseInfo(hook, hint, info, prevbad);
                result = _passphrase_delegate(this, pinfo, ref passwd);
            } catch (Exception ex) {
                _last_callback_exception = ex;
                passwd = "".ToCharArray();
                result = PassphraseResult.Canceled;
            }

            if (fd > 0)
            {
                byte[] utf8_passwd = Gpgme.ConvertCharArrayToUTF8(passwd, 0);

                libgpgme.gpgme_io_write(fd, utf8_passwd, (UIntPtr)utf8_passwd.Length);
                libgpgme.gpgme_io_write(fd, new[] { (byte)0 }, (UIntPtr)1);

                // try to wipe the passwords
                int i;
                for (i = 0; i < utf8_passwd.Length; i++)
                {
                    utf8_passwd[i] = 0;
                }
            }

            return((int)result);
        }