Skip to content

Commit

Permalink
Add Crl tests for ECC
Browse files Browse the repository at this point in the history
  • Loading branch information
romanett committed Jan 13, 2025
1 parent bef0588 commit 89333d8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Stack/Opc.Ua.Core/Schema/ApplicationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3067,7 +3067,7 @@ public CertificateIdentifier()
public CertificateIdentifier(X509Certificate2 certificate)
{
Initialize();
m_certificate = certificate;
Certificate = certificate;
}

/// <summary>
Expand All @@ -3076,7 +3076,7 @@ public CertificateIdentifier(X509Certificate2 certificate)
public CertificateIdentifier(X509Certificate2 certificate, CertificateValidationOptions validationOptions)
{
Initialize();
m_certificate = certificate;
Certificate = certificate;
m_validationOptions = validationOptions;
}

Expand All @@ -3087,7 +3087,7 @@ public CertificateIdentifier(X509Certificate2 certificate, CertificateValidation
public CertificateIdentifier(byte[] rawData)
{
Initialize();
m_certificate = CertificateFactory.Create(rawData, true);
Certificate = CertificateFactory.Create(rawData, true);

Check warning on line 3090 in Stack/Opc.Ua.Core/Schema/ApplicationConfiguration.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Schema/ApplicationConfiguration.cs#L3090

Added line #L3090 was not covered by tests
}

/// <summary>
Expand Down
68 changes: 61 additions & 7 deletions Tests/Opc.Ua.Security.Certificates.Tests/CRLTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
Expand All @@ -44,6 +45,7 @@ namespace Opc.Ua.Security.Certificates.Tests
/// </summary>
[TestFixture, Category("CRL")]
[Parallelizable]
[TestFixtureSource(nameof(FixtureArgs))]
[SetCulture("en-us")]
public class CRLTests
{
Expand All @@ -58,16 +60,48 @@ public class CRLTests
{ 4096, HashAlgorithmName.SHA512 } }.ToArray();
#endregion

/// <summary>
/// store types to run the tests with
/// </summary>
public static readonly object[] FixtureArgs = {
new object [] { nameof(Opc.Ua.ObjectTypeIds.RsaSha256ApplicationCertificateType)},
new object [] { nameof(Opc.Ua.ObjectTypeIds.EccNistP256ApplicationCertificateType)}
};

public CRLTests(string certificateType)
{
if (certificateType == CertificateStoreType.X509Store && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Ignore("X509 Store with crls is only supported on Windows, skipping test run");
}
m_certifiateType = certificateType;
}


#region Test Setup
/// <summary>
/// Set up a Global Discovery Server and Client instance and connect the session
/// </summary>
[OneTimeSetUp]
protected void OneTimeSetUp()
{
m_issuerCert = CertificateBuilder.Create("CN=Root CA, O=OPC Foundation")
.SetCAConstraint()
.CreateForRSA();
if (m_certifiateType == nameof(Opc.Ua.ObjectTypeIds.RsaSha256ApplicationCertificateType))
{
m_issuerCert = CertificateBuilder.Create("CN=Root CA, O=OPC Foundation")
.SetCAConstraint()
.CreateForRSA();
}
else if (m_certifiateType == nameof(Opc.Ua.ObjectTypeIds.EccNistP256ApplicationCertificateType))
{
m_issuerCert = CertificateBuilder.Create("CN=Root CA, O=OPC Foundation")
.SetCAConstraint()
.SetECCurve(ECCurve.NamedCurves.nistP256)
.CreateForECDsa();
}
else
{
throw new NotImplementedException();
}
}

/// <summary>
Expand Down Expand Up @@ -144,8 +178,16 @@ public void CrlBuilderTest(bool empty, bool noExtensions, KeyHashPair keyHashPai
crlBuilder.CrlExtensions.Add(X509Extensions.BuildCRLNumber(1111));
crlBuilder.CrlExtensions.Add(X509Extensions.BuildAuthorityKeyIdentifier(m_issuerCert));
}
IX509CRL i509Crl;
if (X509PfxUtils.IsECDsaSignature(m_issuerCert))
{

var i509Crl = crlBuilder.CreateForRSA(m_issuerCert);
i509Crl = crlBuilder.CreateForECDsa(m_issuerCert);
}
else
{
i509Crl = crlBuilder.CreateForRSA(m_issuerCert);
}
X509CRL x509Crl = new X509CRL(i509Crl.RawData);
Assert.NotNull(x509Crl);
Assert.NotNull(x509Crl.CrlExtensions);
Expand Down Expand Up @@ -203,10 +245,21 @@ public void CrlBuilderTestWithSignatureGenerator(KeyHashPair keyHashPair)
crlBuilder.CrlExtensions.Add(X509Extensions.BuildAuthorityKeyIdentifier(m_issuerCert));

IX509CRL ix509Crl;
using (RSA rsa = m_issuerCert.GetRSAPrivateKey())
if (X509PfxUtils.IsECDsaSignature(m_issuerCert))
{
X509SignatureGenerator generator = X509SignatureGenerator.CreateForRSA(rsa, RSASignaturePadding.Pkcs1);
ix509Crl = crlBuilder.CreateSignature(generator);
using (ECDsa ecdsa = m_issuerCert.GetECDsaPrivateKey())
{
X509SignatureGenerator generator = X509SignatureGenerator.CreateForECDsa(ecdsa);
ix509Crl = crlBuilder.CreateSignature(generator);
}
}
else
{
using (RSA rsa = m_issuerCert.GetRSAPrivateKey())
{
X509SignatureGenerator generator = X509SignatureGenerator.CreateForRSA(rsa, RSASignaturePadding.Pkcs1);
ix509Crl = crlBuilder.CreateSignature(generator);
}
}
X509CRL x509Crl = new X509CRL(ix509Crl);
Assert.NotNull(x509Crl);
Expand Down Expand Up @@ -322,6 +375,7 @@ private void ValidateCRL(

#region Private Fields
X509Certificate2 m_issuerCert;
private string m_certifiateType;
#endregion
}

Expand Down

0 comments on commit 89333d8

Please sign in to comment.