Beispiel #1
0
 private void UpdateSecuritySettings()
 {
     translator.SecuritySuite     = Ciphering.SecuritySuite;
     translator.Security          = Ciphering.Security;
     translator.SystemTitle       = Ciphering.SystemTitle;
     translator.ServerSystemTitle = Ciphering.ServerSystemTitle;
     translator.BlockCipherKey    = Ciphering.BlockCipherKey;
     translator.AuthenticationKey = Ciphering.AuthenticationKey;
     translator.InvocationCounter = Ciphering.InvocationCounter;
     translator.DedicatedKey      = Ciphering.DedicatedKey;
     translator.Keys.Clear();
     translator.Keys.AddRange(Ciphering.KeyPairs);
     if (!string.IsNullOrEmpty(Ciphering.ClientSigningKey))
     {
         KeyValuePair <GXPkcs8, GXx509Certificate> it = FindKey(Ciphering.ClientSigningKey);
         GXPrivateKey pk = null;
         if (it.Key != null)
         {
             pk             = it.Key.PrivateKey;
             pk.SystemTitle = it.Value != null?GXAsn1Converter.SystemTitleFromSubject(it.Value.Subject) : null;
         }
         translator.SigningKeyPair = new KeyValuePair <GXPublicKey, GXPrivateKey>(translator.SigningKeyPair.Key, pk);
     }
     if (!string.IsNullOrEmpty(Ciphering.ServerSigningKey))
     {
         KeyValuePair <GXPkcs8, GXx509Certificate> it = FindKey(Ciphering.ServerSigningKey);
         GXPublicKey pub = null;
         if (it.Value != null)
         {
             pub             = it.Value.PublicKey;
             pub.SystemTitle = it.Value != null?GXAsn1Converter.SystemTitleFromSubject(it.Value.Subject) : null;
         }
         translator.SigningKeyPair = new KeyValuePair <GXPublicKey, GXPrivateKey>(pub, translator.SigningKeyPair.Value);
     }
 }
        private void AddCertificate(GXx509Certificate cert, string path, string st)
        {
            ListViewItem li  = new ListViewItem(cert.PublicKey.Scheme.ToString());
            string       tmp = GXDLMSTranslator.ToHex(GXAsn1Converter.SystemTitleFromSubject(cert.Subject));

            tmp += ((int)cert.KeyUsage).ToString();
            object tmp2 = keys[tmp];

            //Show duplicate certificates.
            if (tmp2 == null)
            {
                keys[tmp]      = cert.KeyUsage;
                duplicate[tmp] = li;
            }
            else
            {
                ((ListViewItem)duplicate[tmp]).BackColor = Color.Yellow;
                li.BackColor = Color.Yellow;
            }
            li.StateImageIndex = li.ImageIndex = 0;
            li.SubItems.Add(cert.SerialNumber.ToString());
            li.SubItems.Add(cert.Subject);
            li.SubItems.Add(cert.ValidFrom + "-" + cert.ValidTo);
            StringBuilder sb = new StringBuilder();

            foreach (KeyUsage it in Enum.GetValues(typeof(KeyUsage)))
            {
                if (((int)it & (int)cert.KeyUsage) != 0)
                {
                    sb.Append(it);
                    sb.Append(", ");
                }
            }
            if (sb.Length != 0)
            {
                sb.Length -= 2;
            }
            li.SubItems.Add(sb.ToString());
            li.SubItems.Add(Path.GetFileNameWithoutExtension(path));
            li.SubItems.Add(cert.Description);
            CertificatesList.Items.Add(li);
            li.Tag = path;
            if (st != null && cert.Subject.Contains(st))
            {
                li.Selected = true;
            }
            Certificates.Add(cert);
        }
Beispiel #3
0
        /// <summary>
        /// Update private and public keys to the translator.
        /// </summary>
        private void GetKeys()
        {
            if (updateUI)
            {
                if (ClientSigningKeysCb.SelectedItem is KeyValuePair <GXPkcs8, GXx509Certificate> cs)
                {
                    ClientSigningKey = cs.Key.ToDer();
                }
                else
                {
                    ClientSigningKey = null;
                }
                if (ClientAgreementKeysCb.SelectedItem is KeyValuePair <GXPkcs8, GXx509Certificate> ca)
                {
                    ClientAgreementKey = ca.Key.ToDer();
                }
                else
                {
                    ClientAgreementKey = null;
                }
                if (ServerSigningKeysCb.SelectedItem is KeyValuePair <GXPkcs8, GXx509Certificate> ss)
                {
                    ServerSigningKey = ss.Value.ToDer();
                }
                else
                {
                    ServerSigningKey = null;
                }
                if (ServerAgreementKeysCb.SelectedItem is KeyValuePair <GXPkcs8, GXx509Certificate> sa)
                {
                    ServerAgreementKey = sa.Value.ToDer();
                }
                else
                {
                    ServerAgreementKey = null;
                }
                bool check = _checkSystemTitle;
                if (check)
                {
                    string st;
                    if (SystemTitleAsciiCb.Checked)
                    {
                        st = GXDLMSTranslator.ToHex(ASCIIEncoding.ASCII.GetBytes(SystemTitleTb.Text), false);
                    }
                    else
                    {
                        st = SystemTitleTb.Text.Replace(" ", "");
                    }
                    if (check && (ClientSigningKeysCb.SelectedItem is KeyValuePair <GXPkcs8, GXx509Certificate> cv) && cv.Value != null)
                    {
                        string certificateSt = GXDLMSTranslator.ToHex(GXAsn1Converter.SystemTitleFromSubject(cv.Value.Subject), false);
                        if (st != certificateSt)
                        {
                            if (MessageBox.Show(Parent, string.Format("System title '{0}' of the client is different than in the certificate '{1}'. Do you want to update the system title from the certificate?", SystemTitleTb.Text, certificateSt), "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                SystemTitleAsciiCb.Checked = false;
                                SystemTitleTb.Text         = certificateSt;
                                check = false;
                            }
                        }
                    }
                    if (check && (ClientAgreementKeysCb.SelectedItem is KeyValuePair <GXPkcs8, GXx509Certificate> ck) && ck.Value != null)
                    {
                        string certificateSt = GXDLMSTranslator.ToHex(GXAsn1Converter.SystemTitleFromSubject(ck.Value.Subject), false);
                        if (st != certificateSt)
                        {
                            if (MessageBox.Show(Parent, string.Format("System title '{0}' of the client is different than in the certificate '{1}'. Do you want to update the system title from the certificate?", SystemTitleTb.Text, certificateSt), "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                SystemTitleAsciiCb.Checked = false;
                                SystemTitleTb.Text         = certificateSt;
                                check = false;
                            }
                        }
                    }

                    if (check && ServerSigningKeysCb.SelectedItem is KeyValuePair <GXPkcs8, GXx509Certificate> sv)
                    {
                        string certificateSt = GXDLMSTranslator.ToHex(GXAsn1Converter.SystemTitleFromSubject(sv.Value.Subject), false);
                        if (ServerSystemTitleTb.Text.Replace(" ", "") != certificateSt)
                        {
                            if (MessageBox.Show(Parent, string.Format("System title '{0}' of the server is different than in the certificate '{1}'. Do you want to update the system title from the certificate?", ServerSystemTitleTb.Text, certificateSt), "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                ServerSystemTitleTb.Text = certificateSt;
                                check = false;
                            }
                        }
                    }
                    if (check && ServerAgreementKeysCb.SelectedItem is KeyValuePair <GXPkcs8, GXx509Certificate> sk)
                    {
                        string certificateSt = GXDLMSTranslator.ToHex(GXAsn1Converter.SystemTitleFromSubject(sk.Value.Subject), false);
                        if (ServerSystemTitleTb.Text.Replace(" ", "") != certificateSt)
                        {
                            if (MessageBox.Show(Parent, string.Format("System title '{0}' of the server is different than in the certificate '{1}'. Do you want to update the system title from the certificate?", ServerSystemTitleTb.Text, certificateSt), "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                            {
                                ServerSystemTitleTb.Text = certificateSt;
                                check = false;
                            }
                        }
                    }
                }
            }
        }