/// <summary> /// Returns a specific secret key in the keyring. /// </summary> /// <param name="openPgp">The <see cref="IOpenPgp"/> implementation.</param> /// <param name="keyIDContainer">An object containing the key ID that identifies the keypair.</param> /// <exception cref="KeyNotFoundException">The specified key could not be found on the system.</exception> /// <seealso cref="IOpenPgp.Sign"/> /// <seealso cref="IOpenPgp.ExportKey"/> public static OpenPgpSecretKey GetSecretKey(this IOpenPgp openPgp, IKeyIDContainer keyIDContainer) { #region Sanity checks if (openPgp == null) { throw new ArgumentNullException(nameof(openPgp)); } if (keyIDContainer == null) { throw new ArgumentNullException(nameof(keyIDContainer)); } #endregion var secretKeys = openPgp.ListSecretKeys().ToList(); if (secretKeys.Count == 0) { throw new KeyNotFoundException(Resources.UnableToFindSecretKey); } try { return(secretKeys.First(x => x.KeyID == keyIDContainer.KeyID)); } catch (InvalidOperationException) { throw new KeyNotFoundException(Resources.UnableToFindSecretKey); } }
/// <summary> /// Formats a key ID as a canonical string. /// </summary> public static string FormatKeyID([NotNull] this IKeyIDContainer keyIDContainer) { #region Sanity checks if (keyIDContainer == null) { throw new ArgumentNullException(nameof(keyIDContainer)); } #endregion return(keyIDContainer.KeyID.ToString("x16").ToUpperInvariant()); }
/// <inheritdoc/> public string ExportKey(IKeyIDContainer keyIDContainer) { #region Sanity checks if (keyIDContainer == null) { throw new ArgumentNullException(nameof(keyIDContainer)); } #endregion return(new CliControl(HomeDir).Execute("--batch", "--no-secmem-warning", "--armor", "--export", keyIDContainer.FormatKeyID()) .Replace(Environment.NewLine, "\n") + "\n"); }
/// <inheritdoc/> public string ExportKey(IKeyIDContainer keyIDContainer) { #region Sanity checks if (keyIDContainer == null) { throw new ArgumentNullException(nameof(keyIDContainer)); } #endregion var publicKey = SecretBundle.GetSecretKey(keyIDContainer.KeyID)?.PublicKey ?? PublicBundle.GetPublicKey(keyIDContainer.KeyID); if (publicKey == null) { throw new KeyNotFoundException("Specified OpenPGP key not found on system"); } var output = new MemoryStream(); using (var armored = new ArmoredOutputStream(output)) publicKey.Encode(armored); return(output.ReadToString(Encoding.ASCII).Replace(Environment.NewLine, "\n")); }
/// <summary> /// Exports an OpenPGP public key to a key file. /// </summary> /// <param name="openPgp">The OpenPGP-compatible system used to manage keys.</param> /// <param name="keyID">The key ID to get the public key for.</param> /// <param name="path">The directory to write the key file to.</param> /// <exception cref="UnauthorizedAccessException">The file could not be read or written.</exception> /// <exception cref="UnauthorizedAccessException">Write access to the directory is not permitted.</exception> /// <exception cref="IOException">The specified <paramref name="keyID"/> could not be found on the system.</exception> public static void DeployPublicKey([NotNull] this IOpenPgp openPgp, [NotNull] IKeyIDContainer keyID, [NotNull] string path) { #region Sanity checks if (openPgp == null) { throw new ArgumentNullException(nameof(openPgp)); } if (keyID == null) { throw new ArgumentNullException(nameof(keyID)); } if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } #endregion File.WriteAllText( path: Path.Combine(path, keyID.FormatKeyID() + ".gpg"), contents: openPgp.ExportKey(keyID), encoding: Encoding.ASCII); }
/// <inheritdoc/> public string ExportKey(IKeyIDContainer keyIDContainer) { #region Sanity checks if (keyIDContainer == null) throw new ArgumentNullException(nameof(keyIDContainer)); #endregion return new CliControl(HomeDir).Execute("--batch", "--no-secmem-warning", "--armor", "--export", keyIDContainer.FormatKeyID()) .Replace(Environment.NewLine, "\n") + "\n"; }