|
22 | 22 | using System.Threading;
|
23 | 23 | using System.Globalization;
|
24 | 24 | using System.Linq;
|
| 25 | +using System.Security.Cryptography.X509Certificates; |
25 | 26 | using IEC61850.Client;
|
26 | 27 | using IEC61850.Common;
|
| 28 | +using IEC61850.TLS; |
| 29 | +using System.Net.Security; |
27 | 30 |
|
28 | 31 | namespace IEC61850_Client
|
29 | 32 | {
|
@@ -597,16 +600,54 @@ static void Process(Iec61850Connection srv)
|
597 | 600 | }
|
598 | 601 | else
|
599 | 602 | {
|
600 |
| - IedConnection con = new IedConnection(); |
| 603 | + IedConnection con = null; |
601 | 604 | try
|
602 | 605 | {
|
| 606 | + if (srv.useSecurity) |
| 607 | + { |
| 608 | + Log(srv.name + " Using TLS"); |
| 609 | + TLSConfiguration tlsConfig = new TLSConfiguration(); |
| 610 | + if (srv.localCertFilePath.Trim() != "") |
| 611 | + tlsConfig.SetOwnCertificate(new X509Certificate2(srv.localCertFilePath)); |
| 612 | + if (srv.privateKeyFilePath.Trim() != "") |
| 613 | + tlsConfig.SetOwnKey(srv.privateKeyFilePath, null); |
| 614 | + foreach (var peerCertFilePath in srv.peerCertFilesPaths) |
| 615 | + tlsConfig.AddAllowedCertificate(new X509Certificate2(peerCertFilePath)); |
| 616 | + // Add a CA certificate to check the certificate provided by the server - not required when ChainValidation == false |
| 617 | + if (srv.rootCertFilePath.Trim() != "") |
| 618 | + tlsConfig.AddCACertificate(new X509Certificate2(srv.rootCertFilePath)); |
| 619 | + // Check if the certificate is signed by a provided CA |
| 620 | + tlsConfig.ChainValidation = srv.chainValidation; |
| 621 | + // Check that the shown server certificate is in the list of allowed certificates |
| 622 | + tlsConfig.AllowOnlyKnownCertificates = srv.allowOnlySpecificCertificates; |
| 623 | + tlsConfig.SetMinTlsVersion(TLSConfigVersion.TLS_1_0); |
| 624 | + tlsConfig.SetMaxTlsVersion(TLSConfigVersion.TLS_1_3); |
| 625 | + if (!srv.allowTLSv10) |
| 626 | + tlsConfig.SetMinTlsVersion(TLSConfigVersion.TLS_1_1); |
| 627 | + if (srv.allowTLSv11) |
| 628 | + tlsConfig.SetMaxTlsVersion(TLSConfigVersion.TLS_1_1); |
| 629 | + else |
| 630 | + tlsConfig.SetMinTlsVersion(TLSConfigVersion.TLS_1_2); |
| 631 | + if (srv.allowTLSv12) |
| 632 | + tlsConfig.SetMaxTlsVersion(TLSConfigVersion.TLS_1_2); |
| 633 | + else |
| 634 | + tlsConfig.SetMinTlsVersion(TLSConfigVersion.TLS_1_3); |
| 635 | + if (srv.allowTLSv13) |
| 636 | + tlsConfig.SetMaxTlsVersion(TLSConfigVersion.TLS_1_3); |
| 637 | + con = new IedConnection(tlsConfig); |
| 638 | + } |
| 639 | + else |
| 640 | + { |
| 641 | + con = new IedConnection(); |
| 642 | + } |
| 643 | + |
603 | 644 | if (srv.password != "")
|
604 | 645 | {
|
605 | 646 | IsoConnectionParameters parameters = con.GetConnectionParameters();
|
606 | 647 | parameters.UsePasswordAuthentication(srv.password);
|
607 | 648 | }
|
608 | 649 |
|
609 |
| - Log("Connect to " + srv.name); |
| 650 | + Log(srv.name + " Connecting to " + srv.ipAddresses[0]); |
610 | 651 | var tcpPort = 102;
|
611 | 652 | string[] ipAddrPort = srv.ipAddresses[0].Split(':');
|
612 | 653 | if (ipAddrPort.Length > 1)
|
@@ -1048,10 +1089,13 @@ static void Process(Iec61850Connection srv)
|
1048 | 1089 | Log(e);
|
1049 | 1090 | else
|
1050 | 1091 | Log(e.Message);
|
1051 |
| - if (con.GetState() == IedConnectionState.IED_STATE_CONNECTED) |
1052 |
| - con.Abort(); |
1053 |
| - con.Dispose(); |
1054 |
| - con = null; |
| 1092 | + if (con != null) |
| 1093 | + { |
| 1094 | + if (con.GetState() == IedConnectionState.IED_STATE_CONNECTED) |
| 1095 | + con.Abort(); |
| 1096 | + con.Dispose(); |
| 1097 | + con = null; |
| 1098 | + } |
1055 | 1099 | Thread.Sleep(5000);
|
1056 | 1100 | }
|
1057 | 1101 | }
|
|
0 commit comments