Skip to content

Commit 8733a33

Browse files
authored
Fix net462 build (#2949)
1 parent 62b4f05 commit 8733a33

File tree

7 files changed

+66
-46
lines changed

7 files changed

+66
-46
lines changed

Libraries/Opc.Ua.Security.Certificates/Org.BouncyCastle/CertificateBuilder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public static byte[] CreatePfxWithRSAPrivateKey(
155155
}
156156
}
157157

158+
#if NET472_OR_GREATER
158159
/// <summary>
159160
/// Create a Pfx with a private key by combining
160161
/// an existing X509Certificate2 and a RSA private key.
@@ -175,6 +176,7 @@ public static byte[] CreatePfxWithECdsaPrivateKey(
175176
new SecureRandom(cfrg));
176177
}
177178
}
179+
#endif
178180

179181
/// <summary>
180182
/// Creates a certificate signing request from an

Libraries/Opc.Ua.Security.Certificates/Org.BouncyCastle/PEMReader.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,16 @@ private static AsymmetricAlgorithm ImportPrivateKey(
125125
key = rsa;
126126
break;
127127
}
128+
#if NET472_OR_GREATER
128129
// Check for an EC private key
129130
if (pemObject is ECPrivateKeyParameters ecParams)
130131
{
131132
var ecdsa = CreateECDsaFromECPrivateKey(ecParams);
132133
key = ecdsa;
133134
break;
134135
}
135-
136+
#endif
137+
136138
// read next object
137139
pemObject = pemReader.ReadObject();
138140
}
@@ -149,6 +151,7 @@ private static AsymmetricAlgorithm ImportPrivateKey(
149151
}
150152
}
151153

154+
#if NET472_OR_GREATER
152155
private static ECDsa CreateECDsaFromECPrivateKey(ECPrivateKeyParameters eCPrivateKeyParameters)
153156
{
154157
var domainParams = eCPrivateKeyParameters.Parameters;
@@ -185,6 +188,7 @@ private static ECDsa CreateECDsaFromECPrivateKey(ECPrivateKeyParameters eCPrivat
185188

186189
return ecdsa;
187190
}
191+
#endif
188192

189193
/// <summary>
190194
/// Pads a byte array with leading zeros to reach the specifieed size
@@ -214,7 +218,7 @@ private static byte[] PadWithLeadingZeros(byte[] arrayToPad, int desiredSize)
214218
return paddedArray;
215219

216220
}
217-
#endregion
221+
#endregion
218222

219223
#region Internal class
220224
/// <summary>

Libraries/Opc.Ua.Security.Certificates/Org.BouncyCastle/X509Utils.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,6 @@ internal static RsaPrivateCrtKeyParameters GetRsaPrivateKeyParameter(X509Certifi
145145
return null;
146146
}
147147

148-
/// <summary>
149-
/// Get ECDsa private key parameters from a X509Certificate2.
150-
/// The private key must be exportable.
151-
/// </summary>
152-
internal static ECPrivateKeyParameters GetECDsaPrivateKeyParameter(X509Certificate2 certificate)
153-
{
154-
// try to get signing/private key from certificate passed in
155-
using (ECDsa ecdsa = certificate.GetECDsaPrivateKey())
156-
{
157-
if (ecdsa != null)
158-
{
159-
return GetECDsaPrivateKeyParameter(ecdsa);
160-
}
161-
}
162-
return null;
163-
}
164-
165148
/// <summary>
166149
/// Get private key parameters from a RSA private key.
167150
/// The private key must be exportable.
@@ -180,6 +163,24 @@ internal static RsaPrivateCrtKeyParameters GetRsaPrivateKeyParameter(RSA rsa)
180163
new BigInteger(1, rsaParams.InverseQ));
181164
}
182165

166+
#if NET472_OR_GREATER
167+
/// <summary>
168+
/// Get ECDsa private key parameters from a X509Certificate2.
169+
/// The private key must be exportable.
170+
/// </summary>
171+
internal static ECPrivateKeyParameters GetECDsaPrivateKeyParameter(X509Certificate2 certificate)
172+
{
173+
// try to get signing/private key from certificate passed in
174+
using (ECDsa ecdsa = certificate.GetECDsaPrivateKey())
175+
{
176+
if (ecdsa != null)
177+
{
178+
return GetECDsaPrivateKeyParameter(ecdsa);
179+
}
180+
}
181+
return null;
182+
}
183+
183184
/// <summary>
184185
/// Get BouncyCastle format private key parameters from a System.Security.Cryptography.ECDsa.
185186
/// The private key must be exportable.
@@ -296,8 +297,6 @@ internal static X9ECParameters GetX9ECParameters(ECParameters ecParams)
296297
return null;
297298
}
298299

299-
300-
301300
/// <summary>
302301
/// Get BouncyCastle format public key parameters from a System.Security.Cryptography.ECDsa
303302
/// </summary>
@@ -318,6 +317,7 @@ internal static ECPublicKeyParameters GetECPublicKeyParameters(ECDsa ec)
318317
return new ECPublicKeyParameters(q, domainParameters);
319318

320319
}
320+
#endif
321321

322322
/// <summary>
323323
/// Get the serial number from a certificate as BigInteger.
@@ -370,7 +370,7 @@ internal static RSA SetRSAPublicKey(byte[] publicKey)
370370
rsaPublicKey.ImportParameters(parameters);
371371
return rsaPublicKey;
372372
}
373-
#endregion
373+
#endregion
374374
}
375375
}
376376
#endif

Libraries/Opc.Ua.Security.Certificates/X509Certificate/CertificateBuilderBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ public virtual ICertificateBuilderIssuer SetIssuer(X509Certificate2 issuerCertif
282282
}
283283
#endregion
284284

285+
#if ECC_SUPPORT
285286
#region Private methods
286287
/// <summary>
287288
/// Set the hash algorithm depending on the curve size
@@ -301,6 +302,7 @@ private void SetHashAlgorithmSize(ECCurve curve)
301302
}
302303
}
303304
#endregion
305+
#endif
304306

305307
#region Protected Methods
306308
/// <summary>
@@ -386,7 +388,7 @@ protected virtual void NewSerialNumber()
386388
/// </summary>
387389
private protected ECCurve? m_curve;
388390
#endif
389-
#endregion
391+
#endregion
390392

391393
#region Private Fields
392394
private X509Certificate2 m_issuerCAKeyCert;

Stack/Opc.Ua.Core/Security/Certificates/CertificateFactory.cs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ public static X509Certificate2 CreateCertificateWithPEMPrivateKey(
491491
byte[] pemDataBlob,
492492
string password = null)
493493
{
494+
#if ECC_SUPPORT
494495
if (X509Utils.IsECDsaSignature(certificate))
495496
{
496497
using (ECDsa privateKey = PEMReader.ImportECDsaPrivateKeyFromPEM(pemDataBlob, password))
@@ -507,6 +508,7 @@ public static X509Certificate2 CreateCertificateWithPEMPrivateKey(
507508
}
508509
}
509510
else
511+
#endif
510512
{
511513
using (RSA privateKey = PEMReader.ImportRsaPrivateKeyFromPEM(pemDataBlob, password))
512514
{
@@ -523,26 +525,26 @@ public static X509Certificate2 CreateCertificateWithPEMPrivateKey(
523525
}
524526
}
525527
#endif
526-
#endregion
527-
528-
#region Internal Methods
529-
/// <summary>
530-
/// Creates a self-signed, signed or CA certificate.
531-
/// </summary>
532-
/// <param name="applicationUri">The application uri (created if not specified).</param>
533-
/// <param name="applicationName">Name of the application (optional if subjectName is specified).</param>
534-
/// <param name="subjectName">The subject used to create the certificate (optional if applicationName is specified).</param>
535-
/// <param name="domainNames">The domain names that can be used to access the server machine (defaults to local computer name if not specified).</param>
536-
/// <param name="keySize">Size of the key (1024, 2048 or 4096).</param>
537-
/// <param name="startTime">The start time.</param>
538-
/// <param name="lifetimeInMonths">The lifetime of the key in months.</param>
539-
/// <param name="hashSizeInBits">The hash size in bits.</param>
540-
/// <param name="isCA">if set to <c>true</c> then a CA certificate is created.</param>
541-
/// <param name="issuerCAKeyCert">The CA cert with the CA private key.</param>
542-
/// <param name="publicKey">The public key if no new keypair is created.</param>
543-
/// <param name="pathLengthConstraint">The path length constraint for CA certs.</param>
544-
/// <returns>The certificate with a private key.</returns>
545-
[Obsolete("Use the new CreateCertificate methods with CertificateBuilder.")]
528+
#endregion
529+
530+
#region Internal Methods
531+
/// <summary>
532+
/// Creates a self-signed, signed or CA certificate.
533+
/// </summary>
534+
/// <param name="applicationUri">The application uri (created if not specified).</param>
535+
/// <param name="applicationName">Name of the application (optional if subjectName is specified).</param>
536+
/// <param name="subjectName">The subject used to create the certificate (optional if applicationName is specified).</param>
537+
/// <param name="domainNames">The domain names that can be used to access the server machine (defaults to local computer name if not specified).</param>
538+
/// <param name="keySize">Size of the key (1024, 2048 or 4096).</param>
539+
/// <param name="startTime">The start time.</param>
540+
/// <param name="lifetimeInMonths">The lifetime of the key in months.</param>
541+
/// <param name="hashSizeInBits">The hash size in bits.</param>
542+
/// <param name="isCA">if set to <c>true</c> then a CA certificate is created.</param>
543+
/// <param name="issuerCAKeyCert">The CA cert with the CA private key.</param>
544+
/// <param name="publicKey">The public key if no new keypair is created.</param>
545+
/// <param name="pathLengthConstraint">The path length constraint for CA certs.</param>
546+
/// <returns>The certificate with a private key.</returns>
547+
[Obsolete("Use the new CreateCertificate methods with CertificateBuilder.")]
546548
internal static X509Certificate2 CreateCertificate(
547549
string applicationUri,
548550
string applicationName,

Tests/Opc.Ua.Core.Tests/Security/Certificates/CertificateValidatorTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ namespace Opc.Ua.Core.Tests.Security.Certificates
5757
public class CertificateValidatorTest
5858
{
5959
#region DataPoints
60+
#if ECC_SUPPORT
6061
[DatapointSource]
6162
public static readonly ECCurveHashPair[] ECCurveHashPairs = CertificateTestsForECDsa.GetECCurveHashPairs();
63+
#endif
6264
#endregion
6365

6466
#region Test Setup
@@ -1274,6 +1276,7 @@ public async Task TestMinimumKeyRejected(bool trusted)
12741276
certValidator.CertificateValidation -= approver.OnCertificateValidation;
12751277
}
12761278

1279+
#if ECC_SUPPORT
12771280
/// <summary>
12781281
/// Test that Hash sizes lower than public key sizes of certificates are not valid
12791282
/// </summary>
@@ -1303,6 +1306,7 @@ ECCurveHashPair ecCurveHashPair
13031306
Assert.Null(innerResult);
13041307
}
13051308
}
1309+
#endif
13061310

13071311
/// <summary>
13081312
/// Test auto accept.
@@ -1707,7 +1711,7 @@ public async Task VerifyMissingCRLNoTrust(bool rejectUnknownRevocationStatus)
17071711
}
17081712
#endregion missing revocation list when revocation is enforced
17091713

1710-
#endregion Test Methods
1714+
#endregion Test Methods
17111715

17121716
#region Private Methods
17131717
private void OnCertificateUpdate(object sender, CertificateUpdateEventArgs e)

Tests/Opc.Ua.Security.Certificates.Tests/CRLTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ protected void OneTimeSetUp()
9191
.SetCAConstraint()
9292
.CreateForRSA();
9393
}
94+
#if ECC_SUPPORT
9495
else if (m_certifiateType == nameof(Opc.Ua.ObjectTypeIds.EccNistP256ApplicationCertificateType))
9596
{
9697
m_issuerCert = CertificateBuilder.Create("CN=Root CA, O=OPC Foundation")
9798
.SetCAConstraint()
9899
.SetECCurve(ECCurve.NamedCurves.nistP256)
99100
.CreateForECDsa();
100101
}
102+
#endif
101103
else
102104
{
103105
throw new NotImplementedException();
@@ -111,7 +113,7 @@ protected void OneTimeSetUp()
111113
protected void OneTimeTearDown()
112114
{
113115
}
114-
#endregion
116+
#endregion
115117

116118
#region Test Methods
117119
/// <summary>
@@ -179,12 +181,14 @@ public void CrlBuilderTest(bool empty, bool noExtensions, KeyHashPair keyHashPai
179181
crlBuilder.CrlExtensions.Add(X509Extensions.BuildAuthorityKeyIdentifier(m_issuerCert));
180182
}
181183
IX509CRL i509Crl;
184+
#if ECC_SUPPORT
182185
if (X509PfxUtils.IsECDsaSignature(m_issuerCert))
183186
{
184187

185188
i509Crl = crlBuilder.CreateForECDsa(m_issuerCert);
186189
}
187190
else
191+
#endif
188192
{
189193
i509Crl = crlBuilder.CreateForRSA(m_issuerCert);
190194
}
@@ -245,6 +249,7 @@ public void CrlBuilderTestWithSignatureGenerator(KeyHashPair keyHashPair)
245249
crlBuilder.CrlExtensions.Add(X509Extensions.BuildAuthorityKeyIdentifier(m_issuerCert));
246250

247251
IX509CRL ix509Crl;
252+
#if ECC_SUPPORT
248253
if (X509PfxUtils.IsECDsaSignature(m_issuerCert))
249254
{
250255
using (ECDsa ecdsa = m_issuerCert.GetECDsaPrivateKey())
@@ -254,6 +259,7 @@ public void CrlBuilderTestWithSignatureGenerator(KeyHashPair keyHashPair)
254259
}
255260
}
256261
else
262+
#endif
257263
{
258264
using (RSA rsa = m_issuerCert.GetRSAPrivateKey())
259265
{
@@ -322,7 +328,7 @@ public void CrlUtcAndGeneralizedTimeTest()
322328
Assert.NotNull(crlEncoded);
323329
ValidateCRL(serial, serstring, hash, crlBuilder, crlEncoded);
324330
}
325-
#endregion
331+
#endregion
326332

327333
#region Private Methods
328334
private string WriteCRL(X509CRL x509Crl)

0 commit comments

Comments
 (0)