Skip to content

Commit

Permalink
IOP workshop 2021 fixes and build improvements (#1562)
Browse files Browse the repository at this point in the history
-fixes for complex types:
  - a structure which contains an enum from namespace 0 can not be created. fixes #1561.
  - a structure with multidimensional array may lead to a endless loop in `ComplexTypeSystem.Load`
  - a malformed namespace Uri which contains a space character prevents creation of type system.
  - select endpoint doesn't pick the right scheme if the default endpoint must be picked
-prep for net472 or greater and net5.0 or greater build, only build #ifdef
  • Loading branch information
mregen committed Oct 29, 2021
1 parent ed434e9 commit 53ec78f
Show file tree
Hide file tree
Showing 41 changed files with 109 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public Task<Session> Connect()
InternalDisconnect();

// select the best endpoint.
var endpointDescription = CoreClientUtils.SelectEndpoint(serverUrl, useSecurity, DiscoverTimeout);
var endpointDescription = CoreClientUtils.SelectEndpoint(m_configuration, serverUrl, useSecurity, DiscoverTimeout);
var endpointConfiguration = EndpointConfiguration.Create(m_configuration);
var endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);

Expand Down Expand Up @@ -565,7 +565,7 @@ private EndpointDescription SelectEndpoint()
}

// return the selected endpoint.
return CoreClientUtils.SelectEndpoint(discoveryUrl, UseSecurityCK.Checked, DiscoverTimeout);
return CoreClientUtils.SelectEndpoint(m_configuration, discoveryUrl, UseSecurityCK.Checked, DiscoverTimeout);
}
finally
{
Expand Down
3 changes: 1 addition & 2 deletions Applications/ConsoleReferenceClient/UAClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public async Task<bool> ConnectAsync()

// Get the endpoint by connecting to server's discovery endpoint.
// Try to find the first endopint without security.
EndpointDescription endpointDescription = CoreClientUtils.SelectEndpoint(ServerUrl, false);

EndpointDescription endpointDescription = CoreClientUtils.SelectEndpoint(m_configuration, ServerUrl, false);
EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(m_configuration);
ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration);

Expand Down
14 changes: 12 additions & 2 deletions Libraries/Opc.Ua.Client.ComplexTypes/ComplexTypeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,6 @@ IList<INode> serverStructTypes
catch (DataTypeNotSupportedException dtnsex)
{
Utils.Trace(dtnsex, "Skipped the type definition of {0} because it is not supported.", dataTypeNode.BrowseName.Name);
continue;
}
catch
{
Expand Down Expand Up @@ -647,6 +646,10 @@ IList<INode> serverStructTypes
structTypesWorkList = structTypesToDoList;
structTypesToDoList = new List<INode>();
}
else
{
break;
}
} while (retryAddStructType);

// all types loaded
Expand Down Expand Up @@ -897,7 +900,7 @@ private void AddEnumerationOrStructureType(INode dataTypeNode, IList<INode> serv
private IList<INode> RemoveKnownTypes(IList<INode> nodeList)
{
return nodeList.Where(
node => GetSystemType(node.NodeId) == null).ToList();
node => GetSystemType(node.NodeId) == null).Distinct().ToList();
}

/// <summary>
Expand Down Expand Up @@ -1170,6 +1173,13 @@ private NodeId GetBuiltInSuperType(NodeId dataType)
}
if (superType.NamespaceIndex == 0)
{
if (superType == DataTypeIds.Enumeration &&
dataType.NamespaceIndex == 0)
{
// enumerations of namespace 0 in a structure
// which are not in the type system are encoded as UInt32
return new NodeId((uint)BuiltInType.UInt32);
}
if (superType == DataTypeIds.Enumeration ||
superType == DataTypeIds.Structure)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ private string FindModuleName(string moduleName, string targetNamespace, int tar
{
if (String.IsNullOrWhiteSpace(moduleName))
{
Uri uri = new Uri(targetNamespace, UriKind.RelativeOrAbsolute);
// remove space chars in malformed namespace url
var tempNamespace = targetNamespace.Replace(" ", "");
Uri uri = new Uri(tempNamespace, UriKind.RelativeOrAbsolute);
var tempName = uri.IsAbsoluteUri ? uri.AbsolutePath : uri.ToString();

tempName = tempName.Replace("/", "");
Expand Down
3 changes: 2 additions & 1 deletion Libraries/Opc.Ua.Client/CoreClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

using System;
using System.Collections.Generic;
using System.Linq;

namespace Opc.Ua.Client
{
Expand Down Expand Up @@ -296,7 +297,7 @@ int discoverTimeout
// pick the first available endpoint by default.
if (selectedEndpoint == null && endpoints.Count > 0)
{
selectedEndpoint = endpoints[0];
selectedEndpoint = endpoints.FirstOrDefault(e => e.EndpointUrl?.StartsWith(url.Scheme) == true);
}

// return the selected endpoint.
Expand Down
9 changes: 5 additions & 4 deletions Libraries/Opc.Ua.Configuration/ApplicationInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,7 @@ string productUri

return true;
}
#endregion

#region Private Methods
/// <summary>
/// Helper to suppress errors which are allowed for the application certificate validation.
/// </summary>
Expand Down Expand Up @@ -812,9 +810,12 @@ private static async Task DeleteApplicationInstanceCertificate(ApplicationConfig
thumbprint = certificate.Thumbprint;
}

using (ICertificateStore store = configuration.SecurityConfiguration.TrustedPeerCertificates.OpenStore())
if (!string.IsNullOrEmpty(thumbprint))
{
await store.Delete(thumbprint).ConfigureAwait(false);
using (ICertificateStore store = configuration.SecurityConfiguration.TrustedPeerCertificates.OpenStore())
{
await store.Delete(thumbprint).ConfigureAwait(false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ private void RevertPermissions(IUserIdentity oldUser)
}

#endregion

#region Private Fields
private ConfiguredEndpoint m_endpoint;
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ========================================================================
/* ========================================================================
* Copyright (c) 2005-2020 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
Expand Down Expand Up @@ -32,9 +32,11 @@
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

#if !NET5_0_OR_GREATER
namespace Opc.Ua.Gds.Server.Database.Linq
{
[Serializable]
[Obsolete("Do not use. Binary decoding poses a security risk.")]
public class BinaryApplicationsDatabase : LinqApplicationsDatabase
{
#region Constructors
Expand All @@ -60,6 +62,7 @@ static public BinaryApplicationsDatabase Load(string fileName)
}
}
#endregion

#region Public Members
public override void Save()
{
Expand All @@ -71,9 +74,11 @@ public override void Save()
}
public string FileName { get { return m_fileName; } private set { m_fileName = value; } }
#endregion

#region Private Fields
[NonSerialized]
string m_fileName;
#endregion
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private string IPAddressToString(byte[] encodedIPAddress)
}
}

#if NETSTANDARD2_1 || NET472 || NET5_0
#if NETSTANDARD2_1 || NET472_OR_GREATER || NET5_0_OR_GREATER
/// <summary>
/// Encode the Subject Alternative name extension.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
#if !NETSTANDARD2_1 && !NET472 && !NET5_0
#if !NETSTANDARD2_1 && !NET472_OR_GREATER && !NET5_0_OR_GREATER

using System;
using System.Security.Cryptography;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#if !NETSTANDARD2_1 && !NET472 && !NET5_0
#if !NETSTANDARD2_1 && !NET472_OR_GREATER && !NET5_0_OR_GREATER
using System;
using System.Security.Cryptography;
using Org.BouncyCastle.Crypto.Prng;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#if !NETSTANDARD2_1 && !NET472 && !NET5_0
#if !NETSTANDARD2_1 && !NET472_OR_GREATER && !NET5_0_OR_GREATER

using Org.BouncyCastle.Asn1.X509;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

#if !NETSTANDARD2_1 && !NET5_0
#if !NETSTANDARD2_1 && !NET5_0_OR_GREATER
using System;
using System.Security.Cryptography;
using System.IO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

#if !NETSTANDARD2_1 && !NET5_0
#if !NETSTANDARD2_1 && !NET5_0_OR_GREATER

using System;
using System.Security.Cryptography.X509Certificates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

#if !NETSTANDARD2_1 && !NET472 && !NET5_0
#if !NETSTANDARD2_1 && !NET472_OR_GREATER && !NET5_0_OR_GREATER

using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
#if !NETSTANDARD2_1 && !NET472 && !NET5_0
#if !NETSTANDARD2_1 && !NET472_OR_GREATER && !NET5_0_OR_GREATER

using System;
using System.Security.Cryptography;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#if !NETSTANDARD2_1 && !NET5_0
#if !NETSTANDARD2_1 && !NET5_0_OR_GREATER
using System;
using System.IO;
using System.Linq;
Expand All @@ -20,7 +20,6 @@
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;

Expand Down
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Security.Certificates/PEM/PEMReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

#if NETSTANDARD2_1 || NET5_0
#if NETSTANDARD2_1 || NET5_0_OR_GREATER

using System;
using System.Security.Cryptography;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Security.Certificates/PEM/PEMWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

#if NETSTANDARD2_1 || NET5_0
#if NETSTANDARD2_1 || NET5_0_OR_GREATER

using System;
using System.Security.Cryptography.X509Certificates;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NETSTANDARD2_1 && !NET472 && !NET5_0
#if !NETSTANDARD2_1 && !NET472_OR_GREATER && !NET5_0_OR_GREATER

// This source code is intentionally copied from the .NET core runtime to close
// a gap in the .NET 4.6 and the .NET Core 2.x runtime implementations.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NETSTANDARD2_1 && !NET472 && !NET5_0
#if !NETSTANDARD2_1 && !NET472_OR_GREATER && !NET5_0_OR_GREATER

// This source code is intentionally copied from the .NET core runtime to close
// a gap in the .NET 4.6 and the .NET Core 2.x runtime implementations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

#if NETSTANDARD2_1 || NET472 || NET5_0
#if NETSTANDARD2_1 || NET472_OR_GREATER || NET5_0_OR_GREATER

using System;
using System.Security.Cryptography;
Expand Down Expand Up @@ -268,13 +268,14 @@ public override X509Certificate2 CreateForECDsa(X509SignatureGenerator generator
public override ICertificateBuilderCreateForECDsaAny SetECDsaPublicKey(byte[] publicKey)
{
if (publicKey == null) throw new ArgumentNullException(nameof(publicKey));
#if NET472_OR_GREATER
throw new NotSupportedException("Import a ECDsaPublicKey is not supported on this platform.");
#else
int bytes = 0;
try
{
m_ecdsaPublicKey = ECDsa.Create();
#if !NET472 // TODO
m_ecdsaPublicKey.ImportSubjectPublicKeyInfo(publicKey, out bytes);
#endif
}
catch (Exception e)
{
Expand All @@ -286,20 +287,22 @@ public override ICertificateBuilderCreateForECDsaAny SetECDsaPublicKey(byte[] pu
throw new ArgumentException("Decoded the public key but extra bytes were found.");
}
return this;
#endif
}
#endif

/// <inheritdoc/>
public override ICertificateBuilderCreateForRSAAny SetRSAPublicKey(byte[] publicKey)
{
if (publicKey == null) throw new ArgumentNullException(nameof(publicKey));
#if NET472_OR_GREATER
throw new NotSupportedException("Import a ECDsaPublicKey is not supported on this platform.");
#else
int bytes = 0;
try
{
m_rsaPublicKey = RSA.Create();
#if !NET472 // TODO
m_rsaPublicKey.ImportSubjectPublicKeyInfo(publicKey, out bytes);
#endif
}
catch (Exception e)
{
Expand All @@ -311,6 +314,7 @@ public override ICertificateBuilderCreateForRSAAny SetRSAPublicKey(byte[] public
throw new ArgumentException("Decoded the public key but extra bytes were found.");
}
return this;
#endif
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/


using System;
using System.Linq;
using System.Security.Cryptography;
Expand Down
7 changes: 7 additions & 0 deletions Stack/Opc.Ua.Bindings.Https/Opc.Ua.Bindings.Https.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<Reference Include="System.Net.Http" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.1.25" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.1.3" />
<PackageReference Include="System.Io.Pipelines" Version="4.5.4" />
<PackageReference Include="System.Text.Encodings.Web" Version="4.5.1" />

</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
Expand Down
1 change: 1 addition & 0 deletions Stack/Opc.Ua.Core/Opc.Ua.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ DateTime nextUpdate
return new X509CRL(crlBuilder.CreateForRSA(issuerCertificate));
}

#if NETSTANDARD2_1 || NET5_0
#if NETSTANDARD2_1 || NET472_OR_GREATER || NET5_0_OR_GREATER
/// <summary>
/// Creates a certificate signing request from an existing certificate.
/// </summary>
Expand Down

0 comments on commit 53ec78f

Please sign in to comment.