//  
//   Rebex Sample Code License
// 
//   Copyright 2026, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Drawing;
using System.Windows.Forms;
using Rebex.Security.Certificates;
using System.Security.Cryptography.X509Certificates;

namespace Rebex.Samples
{
    public class ResolveInvalidCertficiateDialog : Form
    {		
        private readonly Certificate _certificate;
        private CertificateChain _certificateChain;

        public ResolveInvalidCertficiateDialog(ResolveDialogMode dialogMode, string failureReasons, CertificateChain certificateChain, string storedThumbprint) 
        {
            if (certificateChain == null)
                throw new ArgumentNullException("certificateChain");

            _certificateChain = certificateChain;
            _certificate = certificateChain.LeafCertificate;


            InitializeComponent();

            lblReasons.Text = failureReasons;
            lblIssuer.Text = _certificate.GetIssuer().GetCommonName();
            lblHostname.Text = _certificate.GetCommonName().ToString();
            lblThumbprint.Text = _certificate.Thumbprint;

            if (dialogMode == ResolveDialogMode.EntryDiffer)
            {
                lblReasons.ForeColor = Color.Red;
                lblStoredThumbprint.Text = storedThumbprint;
            }
            else
            {
                lblStoredThumbprint.Visible = false;
                lblStoredThumbprintLabel.Visible = false;
            }
        }

        public void RemoveRbUseWindows()
        {
            string controlName = "rbUseWindows";
            Control ctrl = pnlLayout.Controls[controlName];
            if (ctrl == null)
                throw new InvalidOperationException(string.Format("Control '{0}' has not been found.", controlName));
            pnlLayout.Controls.RemoveByKey(controlName);
        }

        public CertificateVerifyingMode GetChoosenMode()
        {
            if (DialogResult != DialogResult.OK)
                throw new InvalidOperationException("Cannot resolve choosen mode when the dialog has not been confirmed by the OK button.");

            if (rbAcceptAny.Checked)
                return CertificateVerifyingMode.AcceptAnyCertificate;
            if (rbStoreLocaly.Checked)
                return CertificateVerifyingMode.LocalyStoredThumbprint;
            if (rbUseWindows.Checked)
                return CertificateVerifyingMode.UseWindowsInfrastructure;
            if (rbReject.Checked)
                return CertificateVerifyingMode.RejectCertificate;

            throw new InvalidOperationException("Unexpected state when resolving CertfificateVerifyingMode.");
        }

        public void EnableAddCertificateToCA(bool show)
        {
            btnAddCertToCA.Visible = show;
        }

        private void ResolveInvalidCertficiateDialog_Activated(object sender, EventArgs e)
        {
            // recalculate height
            int delta = this.Height - pnlLayout.Height;
            pnlLayout.Size = new Size(pnlLayout.Width, pnlButtons.Top + pnlButtons.Height);
            this.Size = new Size(this.Width, pnlLayout.Height + delta);
            this.MinimumSize = new Size(this.MinimumSize.Width, this.Height);

            RecalculateAddCertToCAButtonAvailability();
        }

        private void RecalculateAddCertToCAButtonAvailability()
        {
            btnAddCertToCA.Enabled = rbUseWindows.Checked;
        }

        private void rb_CheckedChanged(object sender, EventArgs e)
        {
            RecalculateAddCertToCAButtonAvailability();
        }

        private void llViewCertificate_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            X509Certificate2UI.DisplayCertificate(X509CertificateLoader.LoadCertificate(_certificate.GetRawCertData()));
        }

        private void btnAddCertToCA_Click(object sender, EventArgs e)
        {
            CertificateStore trustedCaStore = new CertificateStore(CertificateStoreName.Root);
            Certificate rootCertificate = _certificateChain.RootCertificate;

            trustedCaStore.Add(rootCertificate);
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.pnlLayout = new System.Windows.Forms.Panel();
            this.pnlButtons = new System.Windows.Forms.Panel();
            this.btnAddCertToCA = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnOk = new System.Windows.Forms.Button();
            this.rbReject = new System.Windows.Forms.RadioButton();
            this.rbAcceptAny = new System.Windows.Forms.RadioButton();
            this.rbStoreLocaly = new System.Windows.Forms.RadioButton();
            this.rbUseWindows = new System.Windows.Forms.RadioButton();
            this.label3 = new System.Windows.Forms.Label();
            this.pnlDetails = new System.Windows.Forms.Panel();
            this.lblStoredThumbprint = new System.Windows.Forms.Label();
            this.lblStoredThumbprintLabel = new System.Windows.Forms.Label();
            this.lblThumbprint = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.llViewCertificate = new System.Windows.Forms.LinkLabel();
            this.label12 = new System.Windows.Forms.Label();
            this.lblHostname = new System.Windows.Forms.Label();
            this.lblIssuer = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.lblReasons = new System.Windows.Forms.Label();
            this.pnlLayout.SuspendLayout();
            this.pnlButtons.SuspendLayout();
            this.pnlDetails.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Dock = System.Windows.Forms.DockStyle.Top;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(166, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Server certificate validation result:";
            // 
            // pnlLayout
            // 
            this.pnlLayout.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.pnlLayout.Controls.Add(this.pnlButtons);
            this.pnlLayout.Controls.Add(this.rbReject);
            this.pnlLayout.Controls.Add(this.rbAcceptAny);
            this.pnlLayout.Controls.Add(this.rbStoreLocaly);
            this.pnlLayout.Controls.Add(this.rbUseWindows);
            this.pnlLayout.Controls.Add(this.label3);
            this.pnlLayout.Controls.Add(this.pnlDetails);
            this.pnlLayout.Controls.Add(this.label2);
            this.pnlLayout.Controls.Add(this.lblReasons);
            this.pnlLayout.Controls.Add(this.label1);
            this.pnlLayout.Location = new System.Drawing.Point(12, 12);
            this.pnlLayout.Name = "pnlLayout";
            this.pnlLayout.Size = new System.Drawing.Size(493, 411);
            this.pnlLayout.TabIndex = 1;
            // 
            // pnlButtons
            // 
            this.pnlButtons.Controls.Add(this.btnAddCertToCA);
            this.pnlButtons.Controls.Add(this.btnCancel);
            this.pnlButtons.Controls.Add(this.btnOk);
            this.pnlButtons.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pnlButtons.Location = new System.Drawing.Point(0, 377);
            this.pnlButtons.Name = "pnlButtons";
            this.pnlButtons.Size = new System.Drawing.Size(493, 34);
            this.pnlButtons.TabIndex = 9;
            // 
            // btnAddCertToCA
            // 
            this.btnAddCertToCA.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnAddCertToCA.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnAddCertToCA.Location = new System.Drawing.Point(6, 6);
            this.btnAddCertToCA.Name = "btnAddCertToCA";
            this.btnAddCertToCA.Size = new System.Drawing.Size(239, 23);
            this.btnAddCertToCA.TabIndex = 12;
            this.btnAddCertToCA.Text = "OK && Always &Trust This Authority";
            this.btnAddCertToCA.UseVisualStyleBackColor = true;
            this.btnAddCertToCA.Visible = false;
            this.btnAddCertToCA.Click += new System.EventHandler(this.btnAddCertToCA_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnCancel.Location = new System.Drawing.Point(418, 6);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 11;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            // 
            // btnOk
            // 
            this.btnOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOk.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.btnOk.Location = new System.Drawing.Point(337, 6);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new System.Drawing.Size(75, 23);
            this.btnOk.TabIndex = 10;
            this.btnOk.Text = "OK";
            this.btnOk.UseVisualStyleBackColor = true;
            // 
            // rbReject
            // 
            this.rbReject.AutoSize = true;
            this.rbReject.Checked = true;
            this.rbReject.Dock = System.Windows.Forms.DockStyle.Top;
            this.rbReject.Location = new System.Drawing.Point(0, 304);
            this.rbReject.Name = "rbReject";
            this.rbReject.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.rbReject.Size = new System.Drawing.Size(493, 17);
            this.rbReject.TabIndex = 8;
            this.rbReject.TabStop = true;
            this.rbReject.Text = "Reject the server certificate";
            this.rbReject.UseVisualStyleBackColor = true;
            this.rbReject.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
            // 
            // rbAcceptAny
            // 
            this.rbAcceptAny.AutoSize = true;
            this.rbAcceptAny.CheckAlign = System.Drawing.ContentAlignment.TopLeft;
            this.rbAcceptAny.Dock = System.Windows.Forms.DockStyle.Top;
            this.rbAcceptAny.Location = new System.Drawing.Point(0, 287);
            this.rbAcceptAny.Name = "rbAcceptAny";
            this.rbAcceptAny.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.rbAcceptAny.Size = new System.Drawing.Size(493, 17);
            this.rbAcceptAny.TabIndex = 7;
            this.rbAcceptAny.Text = "Ignore the failure and accept the certificate";
            this.rbAcceptAny.UseVisualStyleBackColor = true;
            this.rbAcceptAny.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
            // 
            // rbStoreLocaly
            // 
            this.rbStoreLocaly.AutoSize = true;
            this.rbStoreLocaly.CheckAlign = System.Drawing.ContentAlignment.TopLeft;
            this.rbStoreLocaly.Dock = System.Windows.Forms.DockStyle.Top;
            this.rbStoreLocaly.Location = new System.Drawing.Point(0, 270);
            this.rbStoreLocaly.Name = "rbStoreLocaly";
            this.rbStoreLocaly.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.rbStoreLocaly.Size = new System.Drawing.Size(493, 17);
            this.rbStoreLocaly.TabIndex = 6;
            this.rbStoreLocaly.Text = "Accept the server certificate (and remember it)";
            this.rbStoreLocaly.TextAlign = System.Drawing.ContentAlignment.TopLeft;
            this.rbStoreLocaly.UseVisualStyleBackColor = true;
            this.rbStoreLocaly.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
            // 
            // rbUseWindows
            // 
            this.rbUseWindows.AutoSize = true;
            this.rbUseWindows.CheckAlign = System.Drawing.ContentAlignment.TopLeft;
            this.rbUseWindows.Dock = System.Windows.Forms.DockStyle.Top;
            this.rbUseWindows.Location = new System.Drawing.Point(0, 253);
            this.rbUseWindows.Name = "rbUseWindows";
            this.rbUseWindows.Padding = new System.Windows.Forms.Padding(10, 0, 10, 0);
            this.rbUseWindows.Size = new System.Drawing.Size(493, 17);
            this.rbUseWindows.TabIndex = 5;
            this.rbUseWindows.Text = "Validate the certificate using Windows certificate infrastructure";
            this.rbUseWindows.UseVisualStyleBackColor = true;
            this.rbUseWindows.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Dock = System.Windows.Forms.DockStyle.Top;
            this.label3.Location = new System.Drawing.Point(0, 230);
            this.label3.Name = "label3";
            this.label3.Padding = new System.Windows.Forms.Padding(0, 5, 0, 5);
            this.label3.Size = new System.Drawing.Size(159, 23);
            this.label3.TabIndex = 4;
            this.label3.Text = "How would you like to proceed?";
            // 
            // pnlDetails
            // 
            this.pnlDetails.BackColor = System.Drawing.SystemColors.ControlLightLight;
            this.pnlDetails.Controls.Add(this.lblStoredThumbprint);
            this.pnlDetails.Controls.Add(this.lblStoredThumbprintLabel);
            this.pnlDetails.Controls.Add(this.lblThumbprint);
            this.pnlDetails.Controls.Add(this.label5);
            this.pnlDetails.Controls.Add(this.llViewCertificate);
            this.pnlDetails.Controls.Add(this.label12);
            this.pnlDetails.Controls.Add(this.lblHostname);
            this.pnlDetails.Controls.Add(this.lblIssuer);
            this.pnlDetails.Controls.Add(this.label4);
            this.pnlDetails.Dock = System.Windows.Forms.DockStyle.Top;
            this.pnlDetails.Location = new System.Drawing.Point(0, 96);
            this.pnlDetails.Name = "pnlDetails";
            this.pnlDetails.Padding = new System.Windows.Forms.Padding(10);
            this.pnlDetails.Size = new System.Drawing.Size(493, 134);
            this.pnlDetails.TabIndex = 3;
            // 
            // lblStoredThumbprint
            // 
            this.lblStoredThumbprint.Location = new System.Drawing.Point(106, 89);
            this.lblStoredThumbprint.Name = "lblStoredThumbprint";
            this.lblStoredThumbprint.Size = new System.Drawing.Size(381, 30);
            this.lblStoredThumbprint.TabIndex = 15;
            this.lblStoredThumbprint.Text = "00:a0:b0";
            // 
            // lblStoredThumbprintLabel
            // 
            this.lblStoredThumbprintLabel.AutoSize = true;
            this.lblStoredThumbprintLabel.Location = new System.Drawing.Point(3, 89);
            this.lblStoredThumbprintLabel.Name = "lblStoredThumbprintLabel";
            this.lblStoredThumbprintLabel.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.lblStoredThumbprintLabel.Size = new System.Drawing.Size(98, 13);
            this.lblStoredThumbprintLabel.TabIndex = 14;
            this.lblStoredThumbprintLabel.Text = "Stored thumbprint:";
            // 
            // lblThumbprint
            // 
            this.lblThumbprint.Location = new System.Drawing.Point(106, 56);
            this.lblThumbprint.Name = "lblThumbprint";
            this.lblThumbprint.Size = new System.Drawing.Size(381, 30);
            this.lblThumbprint.TabIndex = 13;
            this.lblThumbprint.Text = "00:a0:b0";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(3, 56);
            this.label5.Name = "label5";
            this.label5.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.label5.Size = new System.Drawing.Size(68, 13);
            this.label5.TabIndex = 12;
            this.label5.Text = "Thumbprint:";
            // 
            // llViewCertificate
            // 
            this.llViewCertificate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.llViewCertificate.AutoSize = true;
            this.llViewCertificate.Location = new System.Drawing.Point(390, 39);
            this.llViewCertificate.Name = "llViewCertificate";
            this.llViewCertificate.Size = new System.Drawing.Size(97, 13);
            this.llViewCertificate.TabIndex = 11;
            this.llViewCertificate.TabStop = true;
            this.llViewCertificate.Text = "View the certificate";
            this.llViewCertificate.TextAlign = System.Drawing.ContentAlignment.TopRight;
            this.llViewCertificate.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.llViewCertificate_LinkClicked);
            // 
            // label12
            // 
            this.label12.AutoSize = true;
            this.label12.Location = new System.Drawing.Point(3, 39);
            this.label12.Name = "label12";
            this.label12.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.label12.Size = new System.Drawing.Size(63, 13);
            this.label12.TabIndex = 10;
            this.label12.Text = "Hostname:";
            // 
            // lblHostname
            // 
            this.lblHostname.Location = new System.Drawing.Point(106, 39);
            this.lblHostname.Name = "lblHostname";
            this.lblHostname.Size = new System.Drawing.Size(118, 17);
            this.lblHostname.TabIndex = 9;
            this.lblHostname.Text = "www.rebex.net";
            // 
            // lblIssuer
            // 
            this.lblIssuer.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.lblIssuer.Location = new System.Drawing.Point(106, 4);
            this.lblIssuer.Name = "lblIssuer";
            this.lblIssuer.Size = new System.Drawing.Size(387, 30);
            this.lblIssuer.TabIndex = 1;
            this.lblIssuer.Text = "Issuer...\r\n...issuer";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(3, 4);
            this.label4.Name = "label4";
            this.label4.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.label4.Size = new System.Drawing.Size(43, 13);
            this.label4.TabIndex = 0;
            this.label4.Text = "Issuer:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Dock = System.Windows.Forms.DockStyle.Top;
            this.label2.Location = new System.Drawing.Point(0, 78);
            this.label2.Name = "label2";
            this.label2.Padding = new System.Windows.Forms.Padding(0, 0, 0, 5);
            this.label2.Size = new System.Drawing.Size(90, 18);
            this.label2.TabIndex = 2;
            this.label2.Text = "Certificate details:";
            // 
            // lblReasons
            // 
            this.lblReasons.Dock = System.Windows.Forms.DockStyle.Top;
            this.lblReasons.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.lblReasons.Location = new System.Drawing.Point(0, 13);
            this.lblReasons.Name = "lblReasons";
            this.lblReasons.Padding = new System.Windows.Forms.Padding(10);
            this.lblReasons.Size = new System.Drawing.Size(493, 65);
            this.lblReasons.TabIndex = 1;
            this.lblReasons.Text = "reasons...";
            // 
            // ResolveInvalidCertficiateDialog
            // 
            this.AcceptButton = this.btnOk;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(517, 441);
            this.Controls.Add(this.pnlLayout);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.MinimumSize = new System.Drawing.Size(530, 39);
            this.Name = "ResolveInvalidCertficiateDialog";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Untrusted Server Certificate";
            this.Activated += new System.EventHandler(this.ResolveInvalidCertficiateDialog_Activated);
            this.pnlLayout.ResumeLayout(false);
            this.pnlLayout.PerformLayout();
            this.pnlButtons.ResumeLayout(false);
            this.pnlDetails.ResumeLayout(false);
            this.pnlDetails.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Panel pnlLayout;
        private System.Windows.Forms.Button btnOk;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Label lblReasons;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.RadioButton rbUseWindows;
        private System.Windows.Forms.RadioButton rbAcceptAny;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.RadioButton rbStoreLocaly;
        private System.Windows.Forms.RadioButton rbReject;
        private System.Windows.Forms.Button btnAddCertToCA;
        private System.Windows.Forms.Panel pnlButtons;
        private System.Windows.Forms.Panel pnlDetails;
        private System.Windows.Forms.LinkLabel llViewCertificate;
        private System.Windows.Forms.Label label12;
        private System.Windows.Forms.Label lblHostname;
        private System.Windows.Forms.Label lblIssuer;
        private System.Windows.Forms.Label label4;
        private Label lblStoredThumbprint;
        private Label lblStoredThumbprintLabel;
        private Label lblThumbprint;
        private Label label5;


    }
}
