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); } }
private gpg_err_code_t GetKey(string fpr, int secret, out IntPtr rkeyPtr) { // the fingerprint could be a UTF8 encoded name IntPtr fprPtr = Gpgme.StringToCoTaskMemUTF8(fpr); int err = libgpgme.gpgme_get_key(ctx.CtxPtr, fprPtr, out rkeyPtr, secret); // free memory if (fprPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(fprPtr); fprPtr = IntPtr.Zero; } gpg_err_code_t errcode = libgpgme.gpgme_err_code(err); switch (errcode) { case gpg_err_code_t.GPG_ERR_INV_VALUE: throw new ArgumentException("Invalid key fingerprint has been given. Error: " + err.ToString()); case gpg_err_code_t.GPG_ERR_AMBIGUOUS_NAME: throw new AmbiguousKeyException("The key id was not unique. Error: " + err.ToString()); case gpg_err_code_t.GPG_ERR_ENOMEM: throw new OutOfMemoryException("Not enough memory available for this operation."); case gpg_err_code_t.GPG_ERR_EOF: throw new KeyNotFoundException("The key " + fpr + " (secret=" + secret + ") could not be found in the keyring."); } return(errcode); }
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 void Add(string name, string value, SignatureNotationFlags flags) { if (ctx.IsValid) { IntPtr namePtr = IntPtr.Zero; IntPtr valuePtr = IntPtr.Zero; if (name != null) { namePtr = Gpgme.StringToCoTaskMemUTF8(name); } if (value != null) { valuePtr = Gpgme.StringToCoTaskMemUTF8(value); } int err = libgpgme.gpgme_sig_notation_add( ctx.CtxPtr, namePtr, valuePtr, (gpgme_sig_notation_flags_t)flags); if (namePtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(namePtr); namePtr = IntPtr.Zero; } if (valuePtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(valuePtr); valuePtr = IntPtr.Zero; } gpg_err_code_t errcode = libgpgerror.gpg_err_code(err); if (errcode == gpg_err_code_t.GPG_ERR_NO_ERROR) { return; } if (errcode == gpg_err_code_t.GPG_ERR_INV_VALUE) { throw new ArgumentException("NAME and VALUE are in an invalid combination."); } throw new GeneralErrorException("An unexpected error occurred. Error: " + errcode.ToString()); } else { throw new InvalidContextException(); } }
public TrustItem[] GetTrustList(string pattern, int maxlevel) { if (ctx == null || !(ctx.IsValid)) { throw new InvalidContextException(); } if (pattern == null || pattern.Equals(String.Empty)) { throw new ArgumentException("An invalid pattern has been specified."); } IntPtr patternPtr = Gpgme.StringToCoTaskMemUTF8(pattern); List <TrustItem> lst = new List <TrustItem>(); lock (ctx.CtxLock) { int err = libgpgme.gpgme_op_trustlist_start(ctx.CtxPtr, patternPtr, maxlevel); gpg_err_code_t errcode = libgpgerror.gpg_err_code(err); if (errcode != gpg_err_code_t.GPG_ERR_NO_ERROR) { if (patternPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(patternPtr); patternPtr = IntPtr.Zero; } throw new GeneralErrorException("An unexpected error occurred. Error: " + err.ToString()); } while (errcode == gpg_err_code_t.GPG_ERR_NO_ERROR) { IntPtr itemPtr = IntPtr.Zero; err = libgpgme.gpgme_op_trustlist_next(ctx.CtxPtr, out itemPtr); errcode = libgpgerror.gpg_err_code(err); if (errcode == gpg_err_code_t.GPG_ERR_NO_ERROR) { lst.Add(new TrustItem(itemPtr)); } } // Release context if there are any pending trustlist items err = libgpgme.gpgme_op_trustlist_end(ctx.CtxPtr); if (patternPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(patternPtr); patternPtr = IntPtr.Zero; } if (errcode != gpg_err_code_t.GPG_ERR_EOF) { throw new GeneralErrorException("An unexpected error occurred. " + errcode.ToString()); } } if (lst.Count == 0) { return(new TrustItem[0]); } else { return(lst.ToArray()); } }
public GenkeyResult GenerateKey(Protocol protocoltype, KeyParameters keyparms) { if (!ctx.IsValid) { throw new InvalidContextException(); } if (keyparms == null) { throw new ArgumentNullException("No KeyParameters object supplied. Bad programmer! *spank* *spank*"); } if (keyparms.Email == null || keyparms.Email.Equals(String.Empty)) { throw new ArgumentException("No email address has been supplied."); } // Convert the key parameter to an XML string for GPGME string parms = keyparms.GetXmlText(protocoltype); // Convert key parameter XML string to UTF8 and retrieve the memory pointer IntPtr parmsPtr = Gpgme.StringToCoTaskMemUTF8(parms); GenkeyResult keyresult = null; int err = 0; gpg_err_code_t errcode = gpg_err_code_t.GPG_ERR_NO_ERROR; lock (ctx.CtxLock) { // Protocol specific key generation switch (protocoltype) { case Protocol.OpenPGP: err = libgpgme.gpgme_op_genkey(ctx.CtxPtr, parmsPtr, (IntPtr)0, (IntPtr)0); errcode = libgpgme.gpgme_err_code(err); if (errcode == gpg_err_code_t.GPG_ERR_NO_ERROR) { IntPtr resultPtr = libgpgme.gpgme_op_genkey_result(ctx.CtxPtr); if (!resultPtr.Equals((IntPtr)0)) { keyresult = new GenkeyResult(resultPtr); } else { errcode = gpg_err_code_t.GPG_ERR_GENERAL; } } break; default: // free memory if (parmsPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(parmsPtr); parmsPtr = IntPtr.Zero; } throw new NotSupportedException("The protocol " + protocoltype + " is currently not supported for key generation."); } } // free memory if (parmsPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(parmsPtr); parmsPtr = IntPtr.Zero; } if (errcode == gpg_err_code_t.GPG_ERR_INV_VALUE) { throw new ArgumentException("The key parameters are invalid."); } if (errcode == gpg_err_code_t.GPG_ERR_NOT_SUPPORTED) { throw new NotSupportedException("The PUBLIC or SECRET part is invalid. Error: " + err.ToString()); } if (errcode != gpg_err_code_t.GPG_ERR_NO_ERROR) { throw new GeneralErrorException("No key has been created by the backend."); } return(keyresult); }
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()); }
public TrustItem[] GetTrustList(string pattern, int maxlevel) { if (Context == null || !(Context.IsValid)) { throw new InvalidContextException(); } if (pattern == null || pattern.Equals(String.Empty)) { throw new ArgumentException("An invalid pattern has been specified."); } IntPtr pattern_ptr = Gpgme.StringToCoTaskMemUTF8(pattern); var lst = new List <TrustItem>(); lock (Context.CtxLock) { int err = libgpgme.NativeMethods.gpgme_op_trustlist_start(Context.CtxPtr, pattern_ptr, maxlevel); gpg_err_code_t errcode = libgpgerror.gpg_err_code(err); if (errcode != gpg_err_code_t.GPG_ERR_NO_ERROR) { if (pattern_ptr != IntPtr.Zero) { Marshal.FreeCoTaskMem(pattern_ptr); } throw GpgmeError.CreateException(errcode); } while (errcode == gpg_err_code_t.GPG_ERR_NO_ERROR) { IntPtr item_ptr; err = libgpgme.NativeMethods.gpgme_op_trustlist_next(Context.CtxPtr, out item_ptr); errcode = libgpgerror.gpg_err_code(err); if (errcode == gpg_err_code_t.GPG_ERR_NO_ERROR) { lst.Add(new TrustItem(item_ptr)); } } // Release context if there are any pending trustlist items libgpgme.NativeMethods.gpgme_op_trustlist_end(Context.CtxPtr); if (pattern_ptr != IntPtr.Zero) { Marshal.FreeCoTaskMem(pattern_ptr); } if (errcode != gpg_err_code_t.GPG_ERR_EOF) { throw GpgmeError.CreateException(errcode); } } if (lst.Count == 0) { return(new TrustItem[0]); } return(lst.ToArray()); }