mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-23 10:23:13 +08:00
improve cert handling of sample apps
- create self signed cert on first start (kudos to bouncy castle) - fix path handling in many areas, - ./ or ../ is treated as rooted dir - special %LocalFolder% for UWP apps to allow to write certs in app temp storage - all batch helpers to create certs removed
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
@echo off
|
||||
REM create the app certificate for NetCoreConsoleClient
|
||||
cd /D %~dp0
|
||||
set CERTSTORE=".\OPC Foundation\CertificateStores\MachineDefault"
|
||||
rd /S/Q %CERTSTORE%
|
||||
md %CERTSTORE%
|
||||
..\..\..\Opc.Ua.CertificateGenerator.exe -cmd issue -sp %CERTSTORE% -ks 2048 -an "UA Sample Client" -dn %COMPUTERNAME% -sn "CN=UA Sample Client/DC=%COMPUTERNAME%" -au "urn:%COMPUTERNAME%:OPCFoundation:SampleClient"
|
||||
set CERTSTORE=
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace NetCoreConsoleClient
|
||||
{
|
||||
@@ -49,31 +50,31 @@ namespace NetCoreConsoleClient
|
||||
Console.WriteLine("1 - Create an Application Configuration.");
|
||||
var config = new ApplicationConfiguration()
|
||||
{
|
||||
ApplicationName = "UA Sample Client",
|
||||
ApplicationName = "UA Core Sample Client",
|
||||
ApplicationType = ApplicationType.Client,
|
||||
ApplicationUri = "urn:"+Utils.GetHostName()+":OPCFoundation:SampleClient",
|
||||
ApplicationUri = "urn:"+Utils.GetHostName()+":OPCFoundation:CoreSampleClient",
|
||||
SecurityConfiguration = new SecurityConfiguration
|
||||
{
|
||||
ApplicationCertificate = new CertificateIdentifier
|
||||
{
|
||||
StoreType = "Directory",
|
||||
StorePath = "./OPC Foundation/CertificateStores/MachineDefault",
|
||||
SubjectName = Utils.Format("CN={0}, DC={1}", "UA Sample Client", Utils.GetHostName())
|
||||
StorePath = "OPC Foundation/CertificateStores/MachineDefault",
|
||||
SubjectName = "UA Core Sample Client"
|
||||
},
|
||||
TrustedPeerCertificates = new CertificateTrustList
|
||||
{
|
||||
StoreType = "Directory",
|
||||
StorePath = "./OPC Foundation/CertificateStores/UA Applications",
|
||||
StorePath = "OPC Foundation/CertificateStores/UA Applications",
|
||||
},
|
||||
TrustedIssuerCertificates = new CertificateTrustList
|
||||
{
|
||||
StoreType = "Directory",
|
||||
StorePath = "./OPC Foundation/CertificateStores/UA Certificate Authorities",
|
||||
StorePath = "OPC Foundation/CertificateStores/UA Certificate Authorities",
|
||||
},
|
||||
RejectedCertificateStore = new CertificateTrustList
|
||||
{
|
||||
StoreType = "Directory",
|
||||
StorePath = "./OPC Foundation/CertificateStores/RejectedCertificates",
|
||||
StorePath = "OPC Foundation/CertificateStores/RejectedCertificates",
|
||||
},
|
||||
NonceLength = 32,
|
||||
AutoAcceptUntrustedCertificates = true
|
||||
@@ -82,10 +83,28 @@ namespace NetCoreConsoleClient
|
||||
TransportQuotas = new TransportQuotas { OperationTimeout = 15000 },
|
||||
ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 }
|
||||
};
|
||||
|
||||
await config.Validate(ApplicationType.Client);
|
||||
|
||||
bool haveAppCertificate = config.SecurityConfiguration.ApplicationCertificate.Certificate != null;
|
||||
|
||||
if (!haveAppCertificate)
|
||||
{
|
||||
Console.WriteLine(" INFO: Create new application certificate: {0}", config.ApplicationName);
|
||||
|
||||
X509Certificate2 certificate = CertificateFactory.CreateCertificate(
|
||||
config.SecurityConfiguration.ApplicationCertificate.StoreType,
|
||||
config.SecurityConfiguration.ApplicationCertificate.StorePath,
|
||||
config.ApplicationUri,
|
||||
config.ApplicationName
|
||||
);
|
||||
|
||||
config.SecurityConfiguration.ApplicationCertificate.Certificate = certificate;
|
||||
|
||||
}
|
||||
|
||||
haveAppCertificate = config.SecurityConfiguration.ApplicationCertificate.Certificate != null;
|
||||
|
||||
if (haveAppCertificate)
|
||||
{
|
||||
config.ApplicationUri = Utils.GetApplicationUriFromCertificate(config.SecurityConfiguration.ApplicationCertificate.Certificate);
|
||||
@@ -95,7 +114,7 @@ namespace NetCoreConsoleClient
|
||||
config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
|
||||
}
|
||||
}
|
||||
if (!haveAppCertificate)
|
||||
else
|
||||
{
|
||||
Console.WriteLine(" WARN: missing application certificate, using unsecure connection.");
|
||||
}
|
||||
@@ -104,6 +123,8 @@ namespace NetCoreConsoleClient
|
||||
Uri endpointURI = new Uri(endpointURL);
|
||||
var endpointCollection = DiscoverEndpoints(config, endpointURI, 10);
|
||||
var selectedEndpoint = SelectUaTcpEndpoint(endpointCollection, haveAppCertificate);
|
||||
Console.WriteLine(" Selected endpoint uses: {0}",
|
||||
selectedEndpoint.SecurityPolicyUri.Substring(selectedEndpoint.SecurityPolicyUri.LastIndexOf('#') + 1));
|
||||
|
||||
Console.WriteLine("3 - Create a session with OPC UA server.");
|
||||
var endpointConfiguration = EndpointConfiguration.Create(config);
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
#!/bin/bash
|
||||
# Create a UA Sample Client cert for OPC UA client
|
||||
#
|
||||
MYHOSTNAME=`echo $HOSTNAME | cut -f1 -d. `
|
||||
echo Create certificate $MYHOSTNAME for UA Sample Client
|
||||
openssl req -newkey rsa:1024 -out cert.crt -keyout cert.key -subj "/DC=$MYHOSTNAME/CN=UA Sample Client" -nodes
|
||||
openssl x509 -days 365 -req -in cert.crt -signkey cert.key -out cert.pem -sha1 -extfile sampleclient.ext
|
||||
openssl pkcs12 -export -in cert.pem -inkey cert.key -out $MYHOSTNAME.pfx -nodes -passout pass:
|
||||
openssl x509 -in cert.pem -out $MYHOSTNAME.der -outform DER
|
||||
openssl x509 -in $MYHOSTNAME.der -inform DER -text
|
||||
rm cert.*
|
||||
rm -rf "./OPC Foundation"
|
||||
mkdir -p "./OPC Foundation/CertificateStores/MachineDefault/certs"
|
||||
mkdir -p "./OPC Foundation/CertificateStores/MachineDefault/private"
|
||||
mv $MYHOSTNAME.der "./OPC Foundation/CertificateStores/MachineDefault/certs"
|
||||
mv $MYHOSTNAME.pfx "./OPC Foundation/CertificateStores/MachineDefault/private"
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
# Extensions to add to a certificate request
|
||||
|
||||
subjectKeyIdentifier=hash
|
||||
subjectAltName=URI:urn:localhost:OPCFoundation:SampleClient
|
||||
authorityKeyIdentifier=critical,keyid,issuer:always
|
||||
basicConstraints = critical, CA:FALSE
|
||||
keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment, dataEncipherment, keyCertSign
|
||||
extendedKeyUsage = critical, serverAuth, clientAuth
|
||||
Reference in New Issue
Block a user