Exemplo n.º 1
0
    static void GenerateCertificate(string name, int keyBitLength)
    {
        Console.WriteLine("Generating Certificate for: {0}", name);
        var    properties         = new CertificateHelper.SelfSignedCertProperties(name, keyBitLength);
        var    cert               = CertificateHelper.CreateSelfSignedCertificate(properties);
        var    priBin             = "Store\\" + name + ".pfx";
        var    pubCer             = "Store\\" + name + ".PublicKey.cer";
        var    priPem             = "Store\\" + name + ".PrivateKey.pem";
        var    priPvk             = "Store\\" + name + ".PrivateKey.pvk";
        string privateKeyPassword = "******";

        byte[] pvkBytes;
        if (cert != null)
        {
            CertificateHelper.ExportPrivateKey(cert, priBin, false, privateKeyPassword);
            CertificateHelper.ExportPrivateKey(cert, priPem, true, privateKeyPassword);
            CertificateHelper.ExportPublicKey(cert, pubCer, false);
            pvkBytes = PrivateKeyHelper.Convert(cert.PrivateKey as RSACryptoServiceProvider, privateKeyPassword);
            System.IO.File.WriteAllBytes(priPvk, pvkBytes);
        }
        // Encryption test.
        var text = "Test";

        Console.WriteLine("Encrypt: {0}", text);
        var encrypted = CertificateHelper.Encrypt(pubCer, "Test", null, false);

        Console.WriteLine("Encrypted:\r\n{0}", encrypted);
        // Decryption test.
        //var decrypted = CertificateHelper.Decrypt(priBin, encrypted, privateKeyPassword);

        pvkBytes = System.IO.File.ReadAllBytes(priPvk);
        var key            = PrivateKeyHelper.Convert(pvkBytes, privateKeyPassword);
        var bytes          = Convert.FromBase64String(encrypted);
        var decryptedBytes = key.Decrypt(bytes, true);
        var decrypted      = Encoding.Unicode.GetString(decryptedBytes);

        Console.WriteLine("Decrypted: {0}", decrypted);
    }
Exemplo n.º 2
0
        private void btnCreateSignerIdentity_Click(object sender, EventArgs e)
        {
            if (ValidateInputs())
            {
                AuthTokenDto auth = null;
                if (txtTenantName.Enabled)
                {
                    var auths = SnapInContext.Instance.AuthTokenManager.GetAuthTokens(_serverDto);
                    auth = auths[0];
                }
                else
                {
                    auth = SnapInContext.Instance.AuthTokenManager.GetAuthToken(_serverDto, _tenantName);
                }

                ActionHelper.Execute(delegate
                {
                    // Get private key
                    var keyText = File.ReadAllText(txtKeyFile.Text);
                    keyText     = PrivateKeyHelper.ExtractBase64EncodedPayload(keyText);
                    EncrptionAlgorithm algo;
                    if (cbAlgo.SelectedIndex > -1)
                    {
                        Enum.TryParse(cbAlgo.SelectedItem.ToString(), false, out algo);
                    }
                    else
                    {
                        algo = EncrptionAlgorithm.RSA;
                    }
                    var privatekey = new PrivateKeyDto {
                        Encoded = keyText, Algorithm = algo
                    };

                    // Get all certificates
                    var certs = new List <CertificateDto>();
                    foreach (var certificate in GetCertificateChain())
                    {
                        var cert = new X509Certificate2();
                        cert.Import(certificate);
                        var certDto = new CertificateDto {
                            Encoded = cert.ExportToPem()
                        };
                        certs.Add(certDto);
                    }
                    var tenantCredentials = new TenantCredentialsDto {
                        Certificates = certs, PrivateKey = privatekey
                    };

                    if (txtTenantName.Enabled)
                    {
                        var tenantDto = new TenantDto()
                        {
                            Name = txtTenantName.Text, Credentials = tenantCredentials, Username = txtUsername.Text, Password = txtPassword.Text
                        };
                        _tenantDto = _service.Tenant.Create(_serverDto, tenantDto, auth.Token);
                    }
                    else
                    {
                        _service.Certificate.SetTenantCredentials(_serverDto, _tenantName, tenantCredentials, auth.Token);
                    }
                }, auth);
                this.DialogResult = DialogResult.OK;
            }
        }
Exemplo n.º 3
0
        public override void AwakeFromNib()
        {
            base.AwakeFromNib();
            _certs = new List <string> ();
            TxtTenantName.Enabled = !UpdateCredentials;
            txtUsername.Enabled   = !UpdateCredentials;
            TxtPassword.Enabled   = !UpdateCredentials;
            if (UpdateCredentials)
            {
                TxtTenantName.StringValue = TenantDto.Name;
            }
            else
            {
                TenantDto = new TenantDto();
            }
            TenantDto.Credentials = new TenantCredentialsDto()
            {
                Certificates = new List <CertificateDto>()
            };

            BtnAddCertificate.Activated += (object sender, EventArgs e) => {
                var openPanel = new NSOpenPanel();
                openPanel.ReleasedWhenClosed = true;
                openPanel.Prompt             = "Select file";

                var result = openPanel.RunModal();
                if (result == 1)
                {
                    var filePath = openPanel.Url.AbsoluteString.Replace("file://", string.Empty);
                    var cert     = new X509Certificate2();
                    ActionHelper.Execute(delegate() {
                        cert.Import(filePath);
                        _certs.Add(filePath);
                        var certfificateDto = new CertificateDto {
                            Encoded = cert.ExportToPem(),
                        };
                        TenantDto.Credentials.Certificates.Add(certfificateDto);
                        ReloadCertificates();
                    });
                }
            };

            BtnRemoveCertificate.Activated += (object sender, EventArgs e) => {
                if (CertificateChainTableView.SelectedRows.Count > 0)
                {
                    foreach (var row in CertificateChainTableView.SelectedRows)
                    {
                        _certs.RemoveAt((int)row);
                        TenantDto.Credentials.Certificates.RemoveAt((int)row);
                    }
                    ReloadCertificates();
                }
            };

            BtnBrowsePrivateKey.Activated += (object sender, EventArgs e) => {
                var openPanel = new NSOpenPanel();
                openPanel.ReleasedWhenClosed = true;
                openPanel.Prompt             = "Select file";
                var result = openPanel.RunModal();
                if (result == 1)
                {
                    var filePath = openPanel.Url.AbsoluteString.Replace("file://", string.Empty);

                    ActionHelper.Execute(delegate() {
                        if (ShaWithRsaSigner.IsPrivateKeyValid(filePath))
                        {
                            var text       = System.IO.File.ReadAllText(filePath);
                            var privateKey = PrivateKeyHelper.ExtractBase64EncodedPayload(text);
                            TxtPrivateKeyPath.StringValue    = filePath;
                            TenantDto.Credentials.PrivateKey = new PrivateKeyDto()
                            {
                                Algorithm = EncrptionAlgorithm.RSA, Encoded = privateKey
                            };
                        }
                        else
                        {
                            UIErrorHelper.ShowAlert("Selected private key is not valid", "Alert");
                        }
                    });
                }
            };

            BtnClose.Activated += (object sender, EventArgs e) => {
                TenantDto = null;
                this.Close();
                NSApplication.SharedApplication.StopModalWithCode(0);
            };
            this.BtnSave.Activated += (object sender, EventArgs e) => {
                if (!UpdateCredentials && string.IsNullOrEmpty(TxtTenantName.StringValue))
                {
                    UIErrorHelper.ShowAlert("Please enter valid tenant name", "Alert");
                }
                else if (!UpdateCredentials && string.IsNullOrEmpty(txtUsername.StringValue))
                {
                    UIErrorHelper.ShowAlert("Please enter valid username", "Alert");
                }
                else if (!UpdateCredentials && string.IsNullOrEmpty(TxtPassword.StringValue))
                {
                    UIErrorHelper.ShowAlert("Please enter valid password", "Alert");
                }
                else if (string.IsNullOrEmpty(TxtPrivateKeyPath.StringValue))
                {
                    UIErrorHelper.ShowAlert("Please enter valid private key", "Alert");
                }
                else if (_certs.Count < 2)
                {
                    UIErrorHelper.ShowAlert("Please enter atleast 2 valid Certificates", "Alert");
                }
                else
                {
                    TenantDto.Name     = TxtTenantName.StringValue;
                    TenantDto.Username = txtUsername.StringValue;
                    TenantDto.Password = TxtPassword.StringValue;
                    this.Close();
                    NSApplication.SharedApplication.StopModalWithCode(1);
                }
            };
        }