public void Export(string[] pattern, GpgmeData keydata)
        {
            if (ctx == null ||
                !(ctx.IsValid))
            {
                throw new InvalidContextException();
            }

            if (keydata == null)
            {
                throw new ArgumentNullException("Invalid data buffer",
                                                new InvalidDataBufferException());
            }

            if (!(keydata.IsValid))
            {
                throw new InvalidDataBufferException();
            }

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

            int  err;
            uint reserved = 0;

            lock (ctx.CtxLock)
            {
                if (parray != null)
                {
                    err = libgpgme.gpgme_op_export_ext(
                        ctx.CtxPtr,
                        parray,
                        reserved,
                        keydata.dataPtr);
                }
                else
                {
                    err = libgpgme.gpgme_op_export(
                        ctx.CtxPtr,
                        IntPtr.Zero,
                        reserved,
                        keydata.dataPtr);
                }
            }

            GC.KeepAlive(keydata);

            // Free memory
            Gpgme.FreeStringArray(parray);

            gpg_err_code_t errcode = libgpgerror.gpg_err_code(err);

            if (errcode != gpg_err_code_t.GPG_ERR_NO_ERROR)
            {
                throw new KeyExportException("Error " + errcode, err);
            }
        }
Exemplo n.º 2
0
        public void Export(string[] pattern, GpgmeData keydata, ExportMode?mode = null)
        {
            if (Context == null ||
                !(Context.IsValid))
            {
                throw new InvalidContextException();
            }

            if (keydata == null)
            {
                throw new ArgumentNullException("Invalid data buffer",
                                                new InvalidDataBufferException());
            }

            if (!(keydata.IsValid))
            {
                throw new InvalidDataBufferException();
            }

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

            int err;
            var apiMode = mode == null ? 0 : (uint)mode;

            lock (Context.CtxLock) {
                if (parray != null)
                {
                    err = libgpgme.NativeMethods.gpgme_op_export_ext(
                        Context.CtxPtr,
                        parray,
                        apiMode,
                        keydata.dataPtr);
                }
                else
                {
                    err = libgpgme.NativeMethods.gpgme_op_export(
                        Context.CtxPtr,
                        IntPtr.Zero,
                        apiMode,
                        keydata.dataPtr);
                }
            }

            GC.KeepAlive(keydata);

            // Free memory
            Gpgme.FreeStringArray(parray);

            GpgmeError.Check(err);
        }
        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());
        }