Skip to content

Commit

Permalink
Remove RSA dispose special handling for Mono (#1347)
Browse files Browse the repository at this point in the history
-Issue has been fixed in Mono, no more workarounds
-Fixes #1214 
-fix some Roslynator and other warnings
  • Loading branch information
mregen committed Apr 1, 2021
1 parent d7c7cbc commit 1d5e87c
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 265 deletions.
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Client/ReverseConnectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public void StartService(ReverseConnectClientConfiguration configuration)
{
Utils.Trace(e, "Unexpected error starting reverse connect manager.");
m_state = ReverseConnectManagerState.Errored;
ServiceResult error = ServiceResult.Create(e, StatusCodes.BadInternalError, "Unexpected error starting application");
ServiceResult error = ServiceResult.Create(e, StatusCodes.BadInternalError, "Unexpected error starting reverse connect manager");
throw new ServiceResultException(error);
}
}
Expand Down
12 changes: 6 additions & 6 deletions Libraries/Opc.Ua.Configuration/ApplicationInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void Stop()
await MessageDlg.ShowAsync().ConfigureAwait(false);
}

throw e;
throw;
}

return null;
Expand All @@ -285,14 +285,14 @@ public async Task<ApplicationConfiguration> LoadApplicationConfiguration(string
try
{
configuration = await LoadAppConfig(
silent, filePath, ApplicationType, ConfigurationType, true, CertificatePasswordProvider)
.ConfigureAwait(false);
silent, filePath, ApplicationType, ConfigurationType, true, CertificatePasswordProvider)
.ConfigureAwait(false);
}
catch (Exception e)
catch (Exception)
{
if (!silent)
{
throw e;
throw;
}
}

Expand Down Expand Up @@ -454,7 +454,7 @@ private void CertificateValidator_CertificateValidation(CertificateValidator val
&& e.Error != null && e.Error.Code == StatusCodes.BadCertificateUntrusted)
{
e.Accept = true;
Utils.Trace((int)Utils.TraceMasks.Security, "Automatically accepted certificate: {0}", e.Certificate.Subject);
Utils.Trace(Utils.TraceMasks.Security, "Automatically accepted certificate: {0}", e.Certificate.Subject);
}
}
catch (Exception exception)
Expand Down
57 changes: 0 additions & 57 deletions Libraries/Opc.Ua.Security.Certificates/Common/RsaUtils.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,10 @@ internal static string GetRSAHashAlgorithm(HashAlgorithmName hashAlgorithmName)
/// </summary>
internal static RsaKeyParameters GetPublicKeyParameter(X509Certificate2 certificate)
{
RSA rsa = null;
try
using (RSA rsa = certificate.GetRSAPublicKey())
{
rsa = certificate.GetRSAPublicKey();
return GetPublicKeyParameter(rsa);
}
finally
{
RsaUtils.RSADispose(rsa);
}
}

/// <summary>
Expand All @@ -120,17 +114,11 @@ internal static RsaKeyParameters GetPublicKeyParameter(RSA rsa)
/// </summary>
internal static RsaPrivateCrtKeyParameters GetPrivateKeyParameter(X509Certificate2 certificate)
{
RSA rsa = null;
try
// try to get signing/private key from certificate passed in
using (RSA rsa = certificate.GetRSAPrivateKey())
{
// try to get signing/private key from certificate passed in
rsa = certificate.GetRSAPrivateKey();
return GetPrivateKeyParameter(rsa);
}
finally
{
RsaUtils.RSADispose(rsa);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ private static X509KeyUsageFlags GetKeyUsage(X509Certificate2 cert)
bool throwOnError = false)
{
bool result = false;
RSA rsaPrivateKey = null;
RSA rsaPublicKey = null;
try
{
// verify the public and private key match
rsaPrivateKey = certWithPrivateKey.GetRSAPrivateKey();
rsaPublicKey = certWithPublicKey.GetRSAPublicKey();
X509KeyUsageFlags keyUsage = GetKeyUsage(certWithPublicKey);
if ((keyUsage & X509KeyUsageFlags.DataEncipherment) != 0)
using (RSA rsaPrivateKey = certWithPrivateKey.GetRSAPrivateKey())
using (RSA rsaPublicKey = certWithPublicKey.GetRSAPublicKey())
{
result = VerifyRSAKeyPairCrypt(rsaPublicKey, rsaPrivateKey);
}
else if ((keyUsage & X509KeyUsageFlags.DigitalSignature) != 0)
{
result = VerifyRSAKeyPairSign(rsaPublicKey, rsaPrivateKey);
}
else
{
throw new CryptographicException("Don't know how to verify the public/private key pair.");
X509KeyUsageFlags keyUsage = GetKeyUsage(certWithPublicKey);
if ((keyUsage & X509KeyUsageFlags.DataEncipherment) != 0)
{
result = VerifyRSAKeyPairCrypt(rsaPublicKey, rsaPrivateKey);
}
else if ((keyUsage & X509KeyUsageFlags.DigitalSignature) != 0)
{
result = VerifyRSAKeyPairSign(rsaPublicKey, rsaPrivateKey);
}
else
{
throw new CryptographicException("Don't know how to verify the public/private key pair.");
}
}
}
catch (Exception)
Expand All @@ -81,8 +81,6 @@ private static X509KeyUsageFlags GetKeyUsage(X509Certificate2 cert)
}
finally
{
RsaUtils.RSADispose(rsaPrivateKey);
RsaUtils.RSADispose(rsaPublicKey);
if (!result && throwOnError)
{
throw new CryptographicException("The public/private key pair in the certficates do not match.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,10 @@ public bool Verify(X509Certificate2 certificate)
/// </summary>
private bool VerifyForRSA(X509Certificate2 certificate, RSASignaturePadding padding)
{
RSA rsa = null;
try
using (RSA rsa = certificate.GetRSAPublicKey())
{
rsa = certificate.GetRSAPublicKey();
return rsa.VerifyData(Tbs, Signature, Name, padding);
}
finally
{
RsaUtils.RSADispose(rsa);
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Opc.Ua.Server/Subscription/MonitoredItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public bool IsReadyToPublish

if (m_nextSamplingTime > now)
{
Utils.Trace((int)TraceMasks.OperationDetail, "IsReadyToPublish[{0}] FALSE {1}", m_id, new TimeSpan(m_nextSamplingTime - now).TotalSeconds);
Utils.Trace((int)TraceMasks.OperationDetail, "IsReadyToPublish[{0}] FALSE {1}ms", m_id, m_nextSamplingTime - now);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public void Configure(IApplicationBuilder appBuilder)
context.Response.ContentLength = 0;
context.Response.ContentType = "text/plain";
context.Response.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
await context.Response.WriteAsync(string.Empty);
await context.Response.WriteAsync(string.Empty).ConfigureAwait(false);
}
else
{
await Listener.SendAsync(context);
await Listener.SendAsync(context).ConfigureAwait(false);
}
});
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public async Task SendAsync(HttpContext context)
context.Response.ContentLength = 0;
context.Response.ContentType = "text/plain";
context.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
await context.Response.WriteAsync(string.Empty);
await context.Response.WriteAsync(string.Empty).ConfigureAwait(false);
return;
}

Expand All @@ -280,19 +280,19 @@ public async Task SendAsync(HttpContext context)
context.Response.ContentLength = 0;
context.Response.ContentType = "text/plain";
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
await context.Response.WriteAsync("HTTPSLISTENER - Unsupported content type.");
await context.Response.WriteAsync("HTTPSLISTENER - Unsupported content type.").ConfigureAwait(false);
return;
}

int length = (int)context.Request.ContentLength;
byte[] buffer = await ReadBodyAsync(context.Request);
byte[] buffer = await ReadBodyAsync(context.Request).ConfigureAwait(false);

if (buffer.Length != length)
{
context.Response.ContentLength = 0;
context.Response.ContentType = "text/plain";
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
await context.Response.WriteAsync("HTTPSLISTENER - Couldn't decode buffer.");
await context.Response.WriteAsync("HTTPSLISTENER - Couldn't decode buffer.").ConfigureAwait(false);
return;
}

Expand Down Expand Up @@ -342,15 +342,15 @@ public async Task SendAsync(HttpContext context)
context.Response.ContentLength = response.Length;
context.Response.ContentType = context.Request.ContentType;
context.Response.StatusCode = (int)HttpStatusCode.OK;
await context.Response.Body.WriteAsync(response, 0, response.Length);
await context.Response.Body.WriteAsync(response, 0, response.Length).ConfigureAwait(false);
}
catch (Exception e)
{
Utils.Trace(e, "HTTPSLISTENER - Unexpected error processing request.");
context.Response.ContentLength = e.Message.Length;
context.Response.ContentType = "text/plain";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
await context.Response.WriteAsync(e.Message);
await context.Response.WriteAsync(e.Message).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -382,7 +382,7 @@ private async Task<byte[]> ReadBodyAsync(HttpRequest req)
using (var memory = new MemoryStream())
using (var reader = new StreamReader(req.Body))
{
await reader.BaseStream.CopyToAsync(memory);
await reader.BaseStream.CopyToAsync(memory).ConfigureAwait(false);
return memory.ToArray();
}
}
Expand Down
10 changes: 2 additions & 8 deletions Stack/Opc.Ua.Core/Security/Certificates/CertificateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ DateTime nextUpdate

// merge all existing revocation list
if (issuerCrls != null)
{
{
foreach (X509CRL issuerCrl in issuerCrls)
{
var extension = X509Extensions.FindExtension<X509CrlNumberExtension>(issuerCrl.CrlExtensions);
Expand Down Expand Up @@ -390,18 +390,12 @@ DateTime nextUpdate
}

string passcode = Guid.NewGuid().ToString();
RSA rsaPrivateKey = null;
try
using (RSA rsaPrivateKey = certificateWithPrivateKey.GetRSAPrivateKey())
{
rsaPrivateKey = certificateWithPrivateKey.GetRSAPrivateKey();
byte[] pfxData = CertificateBuilder.CreatePfxWithRSAPrivateKey(
certificate, certificate.FriendlyName, rsaPrivateKey, passcode);
return X509Utils.CreateCertificateFromPKCS12(pfxData, passcode);
}
finally
{
RsaUtils.RSADispose(rsaPrivateKey);
}
}

/// <summary>
Expand Down

0 comments on commit 1d5e87c

Please sign in to comment.