public void SetEngineInfo(Protocol proto, string filename, string homedir)
        {
            if (IsValid)
            {
                lock (CtxLock)
                {
                    IntPtr filenamePtr = IntPtr.Zero, homedirPtr = IntPtr.Zero;

                    if (filename != null)
                    {
                        filenamePtr = Marshal.StringToCoTaskMemAnsi(filename);
                    }
                    if (homedir != null)
                    {
                        homedirPtr = Marshal.StringToCoTaskMemAnsi(homedir);
                    }

                    int err = libgpgme.gpgme_ctx_set_engine_info(
                        CtxPtr,
                        (gpgme_protocol_t)proto,
                        filenamePtr,
                        homedirPtr);

                    if (filenamePtr != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(filenamePtr);
                        filenamePtr = IntPtr.Zero;
                    }
                    if (homedirPtr != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(homedirPtr);
                        homedirPtr = IntPtr.Zero;
                    }

                    gpg_err_code_t errcode = libgpgme.gpgme_err_code(err);
                    if (errcode != gpg_err_code_t.GPG_ERR_NO_ERROR)
                    {
                        string errmsg = null;
                        try
                        {
                            Gpgme.GetStrError(err, out errmsg);
                        }
                        catch
                        {
                            errmsg = "No error message available.";
                        }
                        throw new ArgumentException(errmsg + " Error: " + err.ToString());
                    }
                }
            }
            else
            {
                throw new InvalidContextException();
            }
        }
Example #2
0
        /// <summary>
        /// Converts a GPGME error code into an exception
        /// </summary>
        /// <param name="code">GPGME error code</param>
        /// <returns>Exception</returns>
        public static Exception CreateException(gpg_err_code_t code)
        {
            string message;

            Gpgme.GetStrError((int)code, out message);
            message = $"#{code}: ${message ?? "No error message available"}";

            switch (code)
            {
            case gpg_err_code_t.GPG_ERR_AMBIGUOUS_NAME:
                return(new AmbiguousKeyException(message));

            case gpg_err_code_t.GPG_ERR_BAD_PASSPHRASE:
                return(new BadPassphraseException(message));

            case gpg_err_code_t.GPG_ERR_CONFLICT:
                return(new KeyConflictException(message));

            case gpg_err_code_t.GPG_ERR_DECRYPT_FAILED:
                return(new DecryptionFailedException(message));

            case gpg_err_code_t.GPG_ERR_INV_VALUE:
                return(new InvalidPtrException(message));

            case gpg_err_code_t.GPG_ERR_EBADF:
                return(new InvalidDataBufferException(message));

            case gpg_err_code_t.GPG_ERR_ENOMEM:
                return(new OutOfMemoryException(message));

            case gpg_err_code_t.GPG_ERR_NO_DATA:
                return(new NoDataException(message));

            case gpg_err_code_t.GPG_ERR_NO_PUBKEY:
                return(new KeyNotFoundException(message));

            case gpg_err_code_t.GPG_ERR_UNUSABLE_SECKEY:
                return(new InvalidKeyException(message));

            default:
                return(new GeneralErrorException(message));
            }
        }
        public Key[] GetKeyList(string[] pattern, bool secretOnly)
        {
            if (ctx == null ||
                !(ctx.IsValid))
            {
                throw new InvalidContextException();
            }

            List <Key> list = new List <Key>();

            int reserved    = 0;
            int secret_only = 0;

            if (secretOnly)
            {
                secret_only = 1;
            }

            IntPtr[] parray = null;
            if (pattern != null)
            {
                parray = Gpgme.StringToCoTaskMemUTF8(pattern);
            }

            lock (ctx.CtxLock)
            {
                // no deadlock because the query is made by the same thread
                Protocol proto = ctx.Protocol;

                int err = 0;

                if (parray != null)
                {
                    err = libgpgme.gpgme_op_keylist_ext_start(
                        ctx.CtxPtr,
                        parray,
                        secret_only,
                        reserved);
                }
                else
                {
                    err = libgpgme.gpgme_op_keylist_start(
                        ctx.CtxPtr,
                        IntPtr.Zero,
                        secret_only);
                }

                while (err == 0)
                {
                    IntPtr keyPtr = (IntPtr)0;
                    err = libgpgme.gpgme_op_keylist_next(ctx.CtxPtr, out keyPtr);
                    if (err != 0)
                    {
                        break;
                    }

                    Key key = null;

                    if (proto == Protocol.OpenPGP)
                    {
                        key = new PgpKey(keyPtr);
                    }
                    else if (proto == Protocol.CMS)
                    {
                        key = new X509Key(keyPtr);
                    }
                    else
                    {
                        key = new Key(keyPtr);
                    }

                    list.Add(key);

                    //libgpgme.gpgme_key_release(keyPtr);
                }

                // Free memory
                if (parray != null)
                {
                    Gpgme.FreeStringArray(parray);
                }

                gpg_err_code_t errcode = libgpgme.gpgme_err_code(err);
                if (errcode != gpg_err_code_t.GPG_ERR_EOF)
                {
                    libgpgme.gpgme_op_keylist_end(ctx.CtxPtr);
                    throw new GpgmeException(Gpgme.GetStrError(err), err);
                }
            }
            return(list.ToArray());
        }