/// <summary> /// Determines the key used to sign a feed or catalog file. Only uses the first signature if more than one is present. /// </summary> /// <param name="path">The feed or catalog file to check for signatures.</param> /// <param name="openPgp">The OpenPGP-compatible system used to validate the signatures.</param> /// <returns>The key used to sign the file; <c>null</c> if the file was not signed.</returns> /// <exception cref="FileNotFoundException">The file file could not be found.</exception> /// <exception cref="IOException">The file could not be read.</exception> /// <exception cref="UnauthorizedAccessException">Read access to the file is not permitted.</exception> public static OpenPgpSecretKey?GetKey(string path, IOpenPgp openPgp) { #region Sanity checks if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } if (openPgp == null) { throw new ArgumentNullException(nameof(openPgp)); } #endregion try { var signature = StoreFeedUtils.GetSignatures(openPgp, File.ReadAllBytes(path)) .OfType <ValidSignature>() .FirstOrDefault(); if (signature == null) { return(null); } return(openPgp.GetSecretKey(signature)); } #region Error handling catch (KeyNotFoundException) { Log.Info(Resources.SecretKeyNotInKeyring); return(null); } catch (SignatureException ex) { // Unable to parse the signature Log.Error(ex); return(null); } #endregion }
public void TestGetSignaturesDataAfterSignature() { string input = FeedText + FeedUtils.SignatureBlockStart + _signatureBase64 + FeedUtils.SignatureBlockEnd + "more data"; Assert.Throws <SignatureException>(() => FeedUtils.GetSignatures(new Mock <IOpenPgp>().Object, Encoding.UTF8.GetBytes(input))); }
public void TestGetSignaturesInvalidChars() { const string input = FeedText + FeedUtils.SignatureBlockStart + "*!?#" + FeedUtils.SignatureBlockEnd; Assert.Throws <SignatureException>(() => FeedUtils.GetSignatures(new Mock <IOpenPgp>().Object, Encoding.UTF8.GetBytes(input))); }
public void TestGetSignaturesMissingNewLine() { string input = "Feed without newline" + FeedUtils.SignatureBlockStart + _signatureBase64 + FeedUtils.SignatureBlockEnd; Assert.Throws <SignatureException>(() => FeedUtils.GetSignatures(new Mock <IOpenPgp>().Object, Encoding.UTF8.GetBytes(input))); }
public void TestGetSignaturesMissingEnd() { string input = FeedText + FeedUtils.SignatureBlockStart + _signatureBase64; Assert.Throws <SignatureException>(() => FeedUtils.GetSignatures(MockRepository.Create <IOpenPgp>().Object, Encoding.UTF8.GetBytes(input))); }