Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix build for legacy platforms by disabling ECC on .NET 4.6.2 & NET-Standard2.0 #2811

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Libraries/Opc.Ua.Client/Session/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6539,6 +6539,7 @@ protected virtual void ProcessResponseAdditionalHeader(ResponseHeader responseHe
{
foreach (var ii in parameters.Parameters)
{
#if ECC_SUPPORT
if (ii.Key == "ECDHKey")
{
if (ii.Value.TypeInfo == TypeInfo.Scalars.StatusCode)
Expand Down Expand Up @@ -6566,10 +6567,11 @@ protected virtual void ProcessResponseAdditionalHeader(ResponseHeader responseHe

m_eccServerEphemeralKey = Nonce.CreateNonce(m_userTokenSecurityPolicyUri, key.PublicKey);
}
#endif
}
}
}
#endregion
#endregion

#region Protected Fields
/// <summary>
Expand Down
15 changes: 10 additions & 5 deletions Libraries/Opc.Ua.Configuration/ApplicationConfigurationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -954,8 +954,13 @@ public static CertificateIdentifierCollection CreateDefaultApplicationCertificat
StorePath = storePath,
SubjectName = subjectName,
CertificateType = ObjectTypeIds.RsaSha256ApplicationCertificateType
},
new CertificateIdentifier {
}
};
#if ECC_SUPPORT
certificateIdentifiers.AddRange(
new CertificateIdentifierCollection
{
new CertificateIdentifier {
StoreType = storeType,
StorePath = storePath,
SubjectName = subjectName,
Expand All @@ -967,7 +972,7 @@ public static CertificateIdentifierCollection CreateDefaultApplicationCertificat
SubjectName = subjectName,
CertificateType = ObjectTypeIds.EccNistP384ApplicationCertificateType
}
};
});

if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Expand All @@ -990,11 +995,11 @@ public static CertificateIdentifierCollection CreateDefaultApplicationCertificat
}
});
}

#endif
return certificateIdentifiers;

}
#endregion
#endregion

#region Private Methods
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ private static bool IsSignatureValid(X509Certificate2 cert)
/// </summary>
private static readonly Dictionary<string, int> NamedCurveBitSizes = new Dictionary<string, int>
{
#if ECC_SUPPORT
// NIST Curves
{ ECCurve.NamedCurves.nistP256.Oid.Value ?? "1.2.840.10045.3.1.7", 256 }, // NIST P-256
{ ECCurve.NamedCurves.nistP384.Oid.Value ?? "1.3.132.0.34" , 384 }, // NIST P-384
Expand All @@ -1784,6 +1785,7 @@ private static bool IsSignatureValid(X509Certificate2 cert)
// Brainpool Curves
{ ECCurve.NamedCurves.brainpoolP256r1.Oid.Value ?? "1.3.36.3.3.2.8.1.1.7", 256 }, // BrainpoolP256r1
{ ECCurve.NamedCurves.brainpoolP384r1.Oid.Value ?? "1.3.36.3.3.2.8.1.1.11", 384 }, // BrainpoolP384r1
#endif
};

/// <summary>
Expand Down Expand Up @@ -1844,7 +1846,7 @@ private bool FindDomain(X509Certificate2 serverCertificate, Uri endpointUrl)
}
return domainFound;
}

#if ECC_SUPPORT
/// <summary>
/// Returns if the certificate is secure enough for the profile.
/// </summary>
Expand Down Expand Up @@ -1883,7 +1885,8 @@ public static bool IsECSecureForProfile(X509Certificate2 certificate, int requir
}
}
}
#endregion
#endif
#endregion

#region Private Enum
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Stack/Opc.Ua.Core/Security/Certificates/EccUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ public static ECDsa GetPublicKey(X509Certificate2 certificate)
return GetPublicKey(certificate, out securityPolicyUris);
}

/// <summary>
/// <summary>
/// Returns the hash algorithm for the specified security policy.
/// </summary>
/// <param name="securityPolicyUri"></param>
Expand Down Expand Up @@ -1374,7 +1374,7 @@ public static HashAlgorithmName GetSignatureAlgorithmName(string securityPolicyU
}
}
}

#endif
}
}
25 changes: 11 additions & 14 deletions Stack/Opc.Ua.Core/Security/Certificates/Nonce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,12 @@ public static Nonce CreateNonce(string securityPolicyUri)

switch (securityPolicyUri)
{
#if ECC_SUPPORT
case SecurityPolicies.ECC_nistP256: { return CreateNonce(ECCurve.NamedCurves.nistP256); }
case SecurityPolicies.ECC_nistP384: { return CreateNonce(ECCurve.NamedCurves.nistP384); }
case SecurityPolicies.ECC_brainpoolP256r1: { return CreateNonce(ECCurve.NamedCurves.brainpoolP256r1); }
case SecurityPolicies.ECC_brainpoolP384r1: { return CreateNonce(ECCurve.NamedCurves.brainpoolP384r1); }
#endif
#if CURVE25519
case SecurityPolicies.ECC_curve25519:
{
Expand Down Expand Up @@ -245,11 +247,12 @@ public static Nonce CreateNonce(string securityPolicyUri, byte[] nonceData)

switch (securityPolicyUri)
{
#if ECC_SUPPORT
case SecurityPolicies.ECC_nistP256: { return CreateNonce(ECCurve.NamedCurves.nistP256, nonceData); }
case SecurityPolicies.ECC_nistP384: { return CreateNonce(ECCurve.NamedCurves.nistP384, nonceData); }
case SecurityPolicies.ECC_brainpoolP256r1: { return CreateNonce(ECCurve.NamedCurves.brainpoolP256r1, nonceData); }
case SecurityPolicies.ECC_brainpoolP384r1: { return CreateNonce(ECCurve.NamedCurves.brainpoolP384r1, nonceData); }

#endif
case SecurityPolicies.ECC_curve25519:
{
return CreateNonceForCurve25519(nonceData);
Expand All @@ -268,7 +271,7 @@ public static Nonce CreateNonce(string securityPolicyUri, byte[] nonceData)

return nonce;
}
#endregion
#endregion

#region Utility Methods

Expand Down Expand Up @@ -422,7 +425,7 @@ private static Nonce CreateNonceForCurve448(byte[] nonceData)

return nonce;
}

#if ECC_SUPPORT
/// <summary>
/// Creates a new Nonce instance with the specified ECC curve and nonce data.
/// </summary>
Expand All @@ -431,7 +434,7 @@ private static Nonce CreateNonceForCurve448(byte[] nonceData)
/// <returns>A new Nonce instance with the specified curve and nonce data.</returns>
private static Nonce CreateNonce(ECCurve curve, byte[] nonceData)
{
#if ECC_SUPPORT

Nonce nonce = new Nonce() {
Data = nonceData
};
Expand Down Expand Up @@ -464,9 +467,6 @@ private static Nonce CreateNonce(ECCurve curve, byte[] nonceData)
}

return nonce;
#else
throw new NotSupportedException("Platform does not support ECC curves");
#endif
}

/// <summary>
Expand All @@ -476,7 +476,7 @@ private static Nonce CreateNonce(ECCurve curve, byte[] nonceData)
/// <returns>A new Nonce instance.</returns>
private static Nonce CreateNonce(ECCurve curve)
{
#if ECC_SUPPORT

var ecdh = (ECDiffieHellman)ECDiffieHellman.Create(curve);
var ecdhParameters = ecdh.ExportParameters(false);
int xLen = ecdhParameters.Q.X.Length;
Expand All @@ -492,12 +492,8 @@ private static Nonce CreateNonce(ECCurve curve)
};

return nonce;

#else
throw new NotSupportedException("Platform does not support ECC curves");
#endif
}

#endif


/// <summary>
Expand Down Expand Up @@ -578,9 +574,10 @@ private static Nonce CreateNonceForCurve448()
/// <param name="context"></param>
protected Nonce(SerializationInfo info, StreamingContext context)
{
#if ECC_SUPPORT
var curveName = info.GetString("CurveName");

#if ECC_SUPPORT

if (curveName != null)
{
var ecParams = new ECParameters {
Expand Down
5 changes: 3 additions & 2 deletions Stack/Opc.Ua.Core/Types/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3148,7 +3148,7 @@ public static bool IsSupportedCertificateType(NodeId certificateType)
}
return false;
}

#if ECC_SUPPORT
/// <summary>
/// Check if known curve is supported by platform
/// </summary>
Expand Down Expand Up @@ -3182,6 +3182,7 @@ ex is ArgumentException ||
{ ECCurve.NamedCurves.brainpoolP256r1.Oid.FriendlyName, new Lazy<bool>(() => IsCurveSupported(ECCurve.NamedCurves.brainpoolP256r1)) },
{ ECCurve.NamedCurves.brainpoolP384r1.Oid.FriendlyName, new Lazy<bool>(() => IsCurveSupported(ECCurve.NamedCurves.brainpoolP384r1)) },
};
#endif

/// <summary>
/// Lazy helper to allow runtime check for Mono.
Expand All @@ -3198,6 +3199,6 @@ public static bool IsRunningOnMono()
{
return s_isRunningOnMonoValue.Value;
}
#endregion
#endregion
}
}
5 changes: 3 additions & 2 deletions Tests/Opc.Ua.Client.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1673,7 +1673,7 @@ public async Task OpenSessionECCIssuedIdentityToken(
Assert.NotNull(value1);
}
}

#if ECC_SUPPORT
/// <summary>
/// Open a session on a channel using ECC encrypted UserCertificateIdentityToken
/// </summary>
Expand Down Expand Up @@ -1732,7 +1732,8 @@ public async Task OpenSessionECCUserCertIdentityToken(
}
}
}
#endregion
#endif
#endregion

#region Benchmarks
/// <summary>
Expand Down
Loading