コード例 #1
0
        /// <summary>
        /// Saves a catalog.
        /// </summary>
        /// <param name="catalog">The catalog to save.</param>
        /// <exception cref="IOException">A file could not be read or written or the GnuPG could not be launched or the catalog file could not be written.</exception>
        /// <exception cref="UnauthorizedAccessException">Read or write access to a catalog file is not permitted.</exception>
        /// <exception cref="KeyNotFoundException">An OpenPGP key could not be found.</exception>
        private void SaveCatalog(Catalog catalog)
        {
            if (_xmlSign)
            {
                var openPgp       = OpenPgpFactory.CreateDefault();
                var signedCatalog = new SignedCatalog(catalog, openPgp.GetSecretKey(_key));

                while (true)
                {
                    try
                    {
                        signedCatalog.Save(_catalogFile, _openPgpPassphrase);
                        break; // Exit loop if passphrase is correct
                    }
                    catch (WrongPassphraseException ex)
                    {
                        // Continue loop if passhrase is incorrect
                        if (!string.IsNullOrEmpty(_openPgpPassphrase))
                        {
                            Log.Error(ex);
                        }
                    }

                    // Ask for passphrase to unlock secret key if we were unable to save without it
                    _openPgpPassphrase = CliUtils.ReadPassword(string.Format(Resources.AskForPassphrase, signedCatalog.SecretKey));
                }
            }
            else
            {
                catalog.SaveXml(_catalogFile);
            }
        }
コード例 #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
 private void MassSignDialog_Load(object sender, EventArgs e)
 {
     comboBoxSecretKey.Items.Add("");
     foreach (var secretKey in OpenPgpFactory.CreateDefault().ListSecretKeys())
     {
         comboBoxSecretKey.Items.Add(secretKey);
     }
 }
コード例 #4
0
        /// <summary>
        /// Saves a feed.
        /// </summary>
        /// <exception cref="IOException">A file could not be read or written or the GnuPG could not be launched or the feed file could not be read or written.</exception>
        /// <exception cref="UnauthorizedAccessException">Read or write access to a feed file is not permitted.</exception>
        /// <exception cref="KeyNotFoundException">An OpenPGP key could not be found.</exception>
        private void SaveFeed(FeedEditing feedEditing)
        {
            if (_unsign)
            {
                // Remove any existing signatures
                feedEditing.SignedFeed.SecretKey = null;
            }
            else
            {
                var openPgp = OpenPgpFactory.CreateDefault();
                if (_xmlSign)
                {     // Signing explicitly requested
                    if (feedEditing.SignedFeed.SecretKey == null)
                    { // No previous signature
                        // Use user-specified key or default key
                        feedEditing.SignedFeed.SecretKey = openPgp.GetSecretKey(_key);
                    }
                    else
                    {                                    // Existing siganture
                        if (!string.IsNullOrEmpty(_key)) // Use new user-specified key
                        {
                            feedEditing.SignedFeed.SecretKey = openPgp.GetSecretKey(_key);
                        }
                        //else resign implied
                    }
                }
                //else resign implied
            }

            // If no signing or unsigning was explicitly requested and the content did not change
            // there is no need to overwrite (and potentiall resign) the file
            if (!_xmlSign && !_unsign && !feedEditing.Changed)
            {
                return;
            }

            while (true)
            {
                try
                {
                    Debug.Assert(feedEditing.Path != null);
                    feedEditing.SignedFeed.Save(feedEditing.Path, _openPgpPassphrase);
                    break; // Exit loop if passphrase is correct
                }
                catch (WrongPassphraseException ex)
                {
                    // Continue loop if passhrase is incorrect
                    if (!string.IsNullOrEmpty(_openPgpPassphrase))
                    {
                        Log.Error(ex);
                    }
                }

                // Ask for passphrase to unlock secret key if we were unable to save without it
                _openPgpPassphrase = CliUtils.ReadPassword(string.Format(Resources.AskForPassphrase, feedEditing.SignedFeed.SecretKey));
            }
        }
コード例 #5
0
        [STAThread] // Required for WinForms
        private static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ErrorReportForm.SetupMonitoring(new Uri("https://0install.de/error-report/"));
            NetUtils.ApplyProxy();

            var openPgp = OpenPgpFactory.CreateDefault();

            if (args == null || args.Length == 0)
            {
                Application.Run(new WelcomeForm(openPgp));
            }
            else
            {
                try
                {
                    var files = ArgumentUtils.GetFiles(args, "*.xml");
                    if (files.Count == 1)
                    {
                        string path = files.First().FullName;
                        Application.Run(new MainForm(FeedEditing.Load(path), openPgp));
                    }
                    else
                    {
                        MassSignForm.Show(files);
                    }
                }
                #region Error handling
                catch (ArgumentException ex)
                {
                    Msg.Inform(null, ex.Message, MsgSeverity.Warn);
                }
                catch (IOException ex)
                {
                    Msg.Inform(null, ex.Message, MsgSeverity.Warn);
                }
                catch (UnauthorizedAccessException ex)
                {
                    Msg.Inform(null, ex.Message, MsgSeverity.Warn);
                }
                catch (InvalidDataException ex)
                {
                    Msg.Inform(null, ex.Message + (ex.InnerException == null ? "" : Environment.NewLine + ex.InnerException.Message), MsgSeverity.Warn);
                }
                #endregion
            }
        }
コード例 #6
0
        public static SignedCatalog Load([NotNull] string path)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }
            #endregion

            return(new SignedCatalog(XmlStorage.LoadXml <Catalog>(path), FeedUtils.GetKey(path, OpenPgpFactory.CreateDefault())));
        }