public void PreviouslyTrusted()
        {
            RegisterKey();
            TrustKey();

            _trustManager.CheckTrust(_combinedBytes, new FeedUri("http://localhost/test.xml"))
            .Should().Be(OpenPgpUtilsTest.TestSignature);
        }
Example #2
0
        /// <inheritdoc/>
        public void ImportFeed(string path, FeedUri uri, FeedUri mirrorUrl = null)
        {
            #region Sanity checks
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }
            #endregion

            if (uri.IsFile)
            {
                throw new UriFormatException(Resources.FeedUriLocal);
            }
            Log.Debug("Importing feed " + uri.ToStringRfc() + " from: " + path);

            var data = File.ReadAllBytes(path);

            var newSignature = _trustManager.CheckTrust(data, uri, mirrorUrl);
            DetectAttacks(data, uri, newSignature);

            // Add to cache and remember time
            _feedCache.Add(uri, data);
            var preferences = FeedPreferences.LoadForSafe(uri);
            preferences.LastChecked = DateTime.UtcNow;
            preferences.Normalize();
            preferences.SaveFor(uri);
        }
        private Catalog DownloadCatalog([NotNull] FeedUri source)
        {
            if (source.IsFile)
            {
                return(XmlStorage.LoadXml <Catalog>(source.LocalPath));
            }

            Log.Info("Downloading catalog: " + source.ToStringRfc());
            byte[] data;
            using (var webClient = new WebClientTimeout())
                data = webClient.DownloadData(source);
            _trustManager.CheckTrust(data, source);
            return(XmlStorage.LoadXml <Catalog>(new MemoryStream(data)));
        }
Example #4
0
        private void CheckTrust(byte[] data, FeedUri feedUri, string localPath)
        {
            // Detect replay attacks
            var newSignature = _trustManager.CheckTrust(data, feedUri, localPath);

            try
            {
                var oldSignature = _feedCache.GetSignatures(feedUri).OfType <ValidSignature>().FirstOrDefault();
                if (oldSignature != null && newSignature.Timestamp < oldSignature.Timestamp)
                {
                    throw new ReplayAttackException(feedUri, oldSignature.Timestamp, newSignature.Timestamp);
                }
            }
            catch (KeyNotFoundException)
            {
                // No existing feed to be replaced
            }
        }
        /// <inheritdoc/>
        public Catalog DownloadCatalog(FeedUri source)
        {
            #region Sanity checks
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            #endregion

            if (source.IsFile)
            {
                return(XmlStorage.LoadXml <Catalog>(source.LocalPath));
            }

            var download = new DownloadMemory(source);
            _handler.RunTask(download);
            var data = download.GetData();
            _trustManager.CheckTrust(data, source);
            return(XmlStorage.LoadXml <Catalog>(new MemoryStream(data)));
        }