/// <summary>
        /// New certificate from file
        /// </summary>
        public CertificateDialog(ISession session, FileInfo fi) : this(session, "New certificate", ItemDialogBaseMode.New)
        {
            CertificateBundle cb          = null;
            X509Certificate2  cert        = null;
            ContentType       contentType = ContentTypeUtils.FromExtension(fi.Extension);

            switch (contentType)
            {
            case ContentType.Certificate:
                cert = new X509Certificate2(fi.FullName);
                break;

            case ContentType.Pkcs12:
                string password = null;
                var    pwdDlg   = new PasswordDialog();
                if (pwdDlg.ShowDialog() != DialogResult.OK)
                {
                    DialogResult = DialogResult.Cancel;
                    return;
                }
                password = pwdDlg.Password;
                cert     = new X509Certificate2(fi.FullName, password, X509KeyStorageFlags.UserKeySet | X509KeyStorageFlags.Exportable);
                break;

            case ContentType.KeyVaultCertificate:
                var kvcf = Utils.LoadFromJsonFile <KeyVaultCertificateFile>(fi.FullName);
                cb   = kvcf.Deserialize();
                cert = new X509Certificate2(cb.Cer);
                break;

            default:
                throw new ArgumentException($"Unsupported ContentType {contentType}");
            }
            NewCertificate(cb, cert);
        }
Ejemplo n.º 2
0
        public override void SaveToFile(string fullName)
        {
            Directory.CreateDirectory(Path.GetDirectoryName(fullName));
            switch (ContentTypeUtils.FromExtension(Path.GetExtension(fullName)))
            {
            case ContentType.KeyVaultSecret:
                throw new InvalidOperationException("One can't save key vault certificate as key vault secret");

            case ContentType.KeyVaultCertificate:     // Serialize the entire secret as encrypted JSON for current user
                File.WriteAllText(fullName, new KeyVaultCertificateFile(CertificateBundle).Serialize());
                break;

            case ContentType.KeyVaultLink:
                File.WriteAllText(fullName, GetLinkAsInternetShortcut());
                break;

            case ContentType.Certificate:
                File.WriteAllBytes(fullName, Certificate.Export(X509ContentType.Cert));
                break;

            case ContentType.Pkcs12:
                string password = null;
                var    pwdDlg   = new PasswordDialog();
                pwdDlg.ShowDialog();
                password = pwdDlg.Password;
                File.WriteAllBytes(fullName, Certificate.Export(X509ContentType.Pkcs12, password));
                break;

            default:
                File.WriteAllText(fullName, Certificate.ToString());
                break;
            }
        }
        /// <summary>
        /// New secret from file
        /// </summary>
        public SecretDialog(ISession session, FileInfo fi) : this(session)
        {
            uxTextBoxName.Text = Utils.ConvertToValidSecretName(Path.GetFileNameWithoutExtension(fi.Name));

            PropertyObject.ContentType = ContentTypeUtils.FromExtension(fi.Extension);
            string password = null;
            switch (PropertyObject.ContentType)
            {
            case ContentType.Certificate:
                break;

            case ContentType.Pkcs12:
            case ContentType.Pkcs12Base64:
                var pwdDlg = new PasswordDialog();
                if (pwdDlg.ShowDialog() != DialogResult.OK)
                {
                    DialogResult = DialogResult.Cancel;
                    return;
                }
                password = pwdDlg.Password;
                break;

            case ContentType.KeyVaultSecret:
                var          kvsf = Utils.LoadFromJsonFile <KeyVaultSecretFile>(fi.FullName);
                SecretBundle s    = kvsf.Deserialize();
                uxPropertyGridSecret.SelectedObject = PropertyObject = new PropertyObjectSecret(s, SecretObject_PropertyChanged);
                uxTextBoxName.Text  = s.SecretIdentifier?.Name;
                uxTextBoxValue.Text = s.Value;
                return;

            default:
                uxTextBoxValue.Text = File.ReadAllText(fi.FullName);
                return;
            }
            // Certificate flow
            RefreshCertificate(new CertificateValueObject(fi, password));
            AutoDetectSecretKind();
        }