SignFeed() public static method

Adds a Base64 signature to a feed or catalog stream.
The file is not parsed before signing; invalid XML files are signed as well. The existing file must end with a line break. Old signatures are not removed.
The file could not be read or written. Read or write access to the file is not permitted. The specified could not be found on the system. was incorrect.
public static SignFeed ( [ stream, [ secretKey, [ passphrase, [ openPgp ) : void
stream [ The feed or catalog to sign.
secretKey [ The secret key to use for signing the file.
passphrase [ The passphrase to use to unlock the key.
openPgp [ The OpenPGP-compatible system used to create signatures.
return void
        /// <summary>
        /// Saves <see cref="Catalog"/> to an XML file, adds the default stylesheet and sign it it with <see cref="SecretKey"/> (if specified).
        /// </summary>
        /// <remarks>Writing and signing the catalog file are performed as an atomic operation (i.e. if signing fails an existing file remains unchanged).</remarks>
        /// <param name="path">The file to save in.</param>
        /// <param name="passphrase">The passphrase to use to unlock the secret key; can be <c>null</c> if <see cref="SecretKey"/> is <c>null</c>.</param>
        /// <exception cref="IOException">A problem occurred while writing the file.</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the file is not permitted.</exception>
        /// <exception cref="KeyNotFoundException">The specified <see cref="SecretKey"/> could not be found on the system.</exception>
        /// <exception cref="WrongPassphraseException"><paramref name="passphrase"/> was incorrect.</exception>
        public void Save(string path, string?passphrase = null)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            #endregion

            if (SecretKey == null)
            {
                Catalog.SaveXml(path);
                return;
            }

            using (var stream = new MemoryStream())
            {
                Catalog.SaveXml(stream, stylesheet: @"catalog.xsl");
                stream.Position = 0;

                FeedUtils.SignFeed(stream, SecretKey, passphrase, _openPgp);
                stream.CopyToFile(path);
            }
            string directory = Path.GetDirectoryName(path);
            _openPgp.DeployPublicKey(SecretKey, directory);
            FeedUtils.DeployStylesheet(directory, @"catalog");
        }
示例#2
0
        /// <summary>
        /// Saves <see cref="Catalog"/> to an XML file, adds the default stylesheet and sign it it with <see cref="SecretKey"/> (if specified).
        /// </summary>
        /// <remarks>Writing and signing the catalog file are performed as an atomic operation (i.e. if signing fails an existing file remains unchanged).</remarks>
        /// <param name="path">The file to save in.</param>
        /// <param name="passphrase">The passphrase to use to unlock the secret key; can be <see langword="null"/> if <see cref="SecretKey"/> is <see langword="null"/>.</param>
        /// <exception cref="IOException">A problem occurs while writing the file.</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the file is not permitted.</exception>
        /// <exception cref="WrongPassphraseException">Passphrase was incorrect.</exception>
        public void Save([NotNull] string path, [CanBeNull] string passphrase = null)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }
            #endregion

            if (SecretKey == null)
            {
                Catalog.SaveXml(path);
                return;
            }

            var openPgp = OpenPgpFactory.CreateDefault();
            using (var stream = new MemoryStream())
            {
                Catalog.SaveXml(stream, stylesheet: @"catalog.xsl");
                stream.Position = 0;

                FeedUtils.SignFeed(stream, SecretKey, passphrase, openPgp);
                stream.WriteTo(path);
            }
            string directory = Path.GetDirectoryName(path);
            if (directory != null)
            {
                FeedUtils.DeployPublicKey(directory, SecretKey, openPgp);
                FeedUtils.DeployStylesheet(directory, @"catalog");
            }
        }
示例#3
0
        public void TestSignFeed()
        {
            using (var stream = new MemoryStream())
            {
                var          feed       = FeedTest.CreateTestFeed();
                const string passphrase = "passphrase123";
                var          signature  = new byte[] { 1, 2, 3 };
                var          secretKey  = new OpenPgpSecretKey(keyID: 123, fingerprint: new byte[] { 1, 2, 3 }, userID: "user");

                var openPgpMock = CreateMock <IOpenPgp>();
                openPgpMock.Setup(x => x.Sign(It.IsAny <byte[]>(), secretKey, passphrase))
                .Returns(signature);
                feed.SaveXml(stream);
                FeedUtils.SignFeed(stream, secretKey, passphrase, openPgpMock.Object);

                string signedFeed   = stream.ReadToString();
                string expectedFeed = feed.ToXmlString() + Store.Feeds.FeedUtils.SignatureBlockStart +
                                      Convert.ToBase64String(signature) + "\n" + Store.Feeds.FeedUtils.SignatureBlockEnd;
                signedFeed.Should().Be(expectedFeed, because: "Feed should remain unchanged except for appended XML signatre");
            }
        }
示例#4
0
        public void TestSignFeed()
        {
            using (var stream = new MemoryStream())
            {
                var          feed        = FeedTest.CreateTestFeed();
                var          secretKey   = new OpenPgpSecretKey("fingerprint", "key", "*****@*****.**", new DateTime(2000, 1, 1), OpenPgpAlgorithm.Rsa, 128);
                var          openPgpMock = MockRepository.Create <IOpenPgp>();
                const string passphrase  = "passphrase123";
                const string signature   = "iQEcB";

                openPgpMock.Setup(x => x.DetachSign(It.IsAny <Stream>(), secretKey.Fingerprint, passphrase))
                .Returns(signature);
                feed.SaveXml(stream);
                FeedUtils.SignFeed(stream, secretKey, passphrase, openPgpMock.Object);

                string signedFeed   = stream.ReadToString();
                string expectedFeed = feed.ToXmlString() + Store.Feeds.FeedUtils.SignatureBlockStart +
                                      signature + "\n" + Store.Feeds.FeedUtils.SignatureBlockEnd;
                Assert.AreEqual(expectedFeed, signedFeed,
                                "Feed should remain unchanged except for appended XML signatre");
            }
        }