internal static void Check(GpgMeError error) { if (error != GpgMeError.GPG_ERR_NO_ERROR) { throw new GpgMeException(error, GpgMeWrapper.gpgme_strerror(error)); } }
/// <summary> /// Creates a new, empty memory-mapped buffer. /// </summary> /// <returns>The buffer that was created.</returns> public static MemoryGpgBuffer Create() { IntPtr handle; ErrorHandler.Check(GpgMeWrapper.gpgme_data_new(out handle)); return(new MemoryGpgBuffer(handle)); }
/// <summary> /// Creates a new <see cref="GpgContext"/> and returns it. /// </summary> /// <returns>The <see cref="GpgContext"/> that was created.</returns> public static GpgContext CreateContext() { IntPtr handle; ErrorHandler.Check(GpgMeWrapper.gpgme_new(out handle)); return(new GpgContext(handle)); }
/// <summary> /// Attempts to decrypt the contents of a data buffer. /// If required, the user will be prompted for a password using the default pinentry program. /// </summary> /// <param name="cipher">A DataBuffer containing the data to be decrypted.</param> /// <returns>A DataBuffer containing the decrypted data.</returns> public GpgBuffer Decrypt(GpgBuffer cipher) { var output = MemoryGpgBuffer.Create(); ErrorHandler.Check(GpgMeWrapper.gpgme_op_decrypt(Handle, cipher.Handle, output.Handle)); output.Position = 0; return(output); }
/// <summary> /// Attempts to encrypt the contents of a data buffer for multiple recipients. /// </summary> /// <param name="plain">A DataBuffer containing the plaintext data to be encrypted.</param> /// <param name="recipients">The GPG keys for which the data should be encrypted.</param> /// <param name="encryptFlags">The encryption flags to be used.</param> /// <returns>A DataBuffer containing the encrypted data.</returns> public GpgBuffer Encrypt(GpgBuffer plain, IEnumerable <GpgKey> recipients, EncryptFlags encryptFlags = EncryptFlags.None) { // Transform the recipient list into a list of GpgME key handles var rcpHandles = recipients.Select(rcp => rcp.Handle).ToArray(); var output = MemoryGpgBuffer.Create(); ErrorHandler.Check(GpgMeWrapper.gpgme_op_encrypt(Handle, rcpHandles, encryptFlags, plain.Handle, output.Handle)); output.Position = 0; return(output); }
/// <summary> /// Initialises the underlying GpgME library. /// </summary> /// <param name="dllPath">When set, overrides the default GpgME DLL location.</param> /// <param name="installDir">When set, overrides the default GpgME installation directory. /// This should point to the bin directory of the GPG installation directory.</param> /// <param name="minLibraryVersion"> /// The minimum required version of GpgME. Set to null to disable this version check. /// </param> /// <param name="minGpgVersion"> /// The minimum required version of Gpg. Set to null to disable this version check. /// </param> public static void Initialise(string dllPath = null, string installDir = null, string minLibraryVersion = "1.8.0", string minGpgVersion = "2.0.0") { if (Initialised) { throw new InvalidOperationException("GpgME has already been initialised."); } if (dllPath != null) { // Manually load the GpgME DL from a custom path // instead of letting Windows look for it. Kernel32.Load(dllPath); } // Global flags should be set before initialisation if (minGpgVersion != null) { if (GpgMeWrapper.gpgme_set_global_flag("require-gnupg", minGpgVersion) != 0) { throw new GpgNetException("Failed to set minimum required GnuPG version."); } } if (installDir != null) { var result = GpgMeWrapper.gpgme_set_global_flag("w32-inst-dir", installDir); if (result != 0) { throw new GpgNetException("Failed to set Win32 install dir."); } } // The version check is required as it initialises GpgME Version = GpgMeWrapper.gpgme_check_version(minLibraryVersion); if (Version == null) { throw new GpgNetException("Minimum required GpgME version is not met."); } // Get information about the GPG engines available AvailableEngines = GpgMeHelper.GetEngines(); // Get information about the current GPG configuration Homedir = GpgMeWrapper.gpgme_get_dirinfo("homedir"); Sysconfdir = GpgMeWrapper.gpgme_get_dirinfo("sysconfdir"); Bindir = GpgMeWrapper.gpgme_get_dirinfo("bindir"); Libdir = GpgMeWrapper.gpgme_get_dirinfo("libdir"); Libexecdir = GpgMeWrapper.gpgme_get_dirinfo("libexecdir"); Datadir = GpgMeWrapper.gpgme_get_dirinfo("datadir"); Localedir = GpgMeWrapper.gpgme_get_dirinfo("localedir"); AgentSocket = GpgMeWrapper.gpgme_get_dirinfo("agent-socket"); Initialised = true; }
/// <summary> /// Enable GpgME debugging. This should be done before GpgME is initialised. /// </summary> /// <param name="debugPath"> /// The path to the file debug output should be saved to. If this is set to its default of null, /// debug output is saved to a file named <code>gpgme-debug.txt</code> in the current working directory. /// </param> /// <param name="debugLevel">The verbosity level of GpgME. A higher number increases verbosity.</param> public static void EnableDebugging(string debugPath = null, int debugLevel = 9) { if (Initialised) { throw new InvalidOperationException("Debugging needs to be enabled before GpgME is initialised."); } if (debugPath == null) { debugPath = Path.Combine(Environment.CurrentDirectory, "gpgme-debug.txt"); } if (GpgMeWrapper.gpgme_set_global_flag("debug", $"{debugLevel};{debugPath}") != 0) { throw new GpgNetException("Failed to enable debugging."); } }
/// <summary> /// Ensures the given protocol is available. If it isn't, an exception is thrown. /// </summary> /// <param name="protocol">The <see cref="GpgMeProtocol"/> to check for.</param> /// <exception cref="GpgMeException">Thrown if the given protocol is not available, badly configured, or otherwise unusable.</exception> public static void EnsureProtocol(GpgMeProtocol protocol) { ErrorHandler.Check(GpgMeWrapper.gpgme_engine_check_version(protocol)); }
/// <summary> /// Sets the position of the current stream. /// </summary> /// <param name="offset">A byte offset relative to the <paramref name="origin"/> parameter.</param> /// <param name="origin">A value of type <see cref="SeekOrigin"/> indicating the reference point used to obtain the new position.</param> /// <returns></returns> public override long Seek(long offset, SeekOrigin origin) { // The values for SeekOrigin (WinAPI) and SeekPosition (GCC) are the same, // so we can just cast this. Feels dirty though... return(GpgMeWrapper.gpgme_data_seek(Handle, (int)offset, (SeekPosition)origin)); }
/// <summary> /// Requests GPG to release the buffer and free the memory it has taken up. /// </summary> public new void Dispose() { base.Dispose(); GpgMeWrapper.gpgme_data_release(Handle); }
/// <summary> /// Releases the underlying GPGME context. /// </summary> public void Dispose() { GpgMeWrapper.gpgme_release(Handle); }