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

Reduce AOT binary size #3091

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,12 @@ internal SqlConnectionString(string connectionString) : base(connectionString, G
if (_networkLibrary != null)
{ // MDAC 83525
string networkLibrary = _networkLibrary.Trim().ToLower(CultureInfo.InvariantCulture);
Hashtable netlib = NetlibMapping();
Dictionary<string, string> netlib = NetlibMapping();
edwardneal marked this conversation as resolved.
Show resolved Hide resolved
if (!netlib.ContainsKey(networkLibrary))
{
throw ADP.InvalidConnectionOptionValue(KEY.Network_Library);
}
_networkLibrary = (string)netlib[networkLibrary];
_networkLibrary = netlib[networkLibrary];
}
else
{
Expand Down Expand Up @@ -1101,14 +1101,14 @@ internal SqlConnectionEncryptOption ConvertValueToSqlConnectionEncrypt()
}
}

static internal Hashtable NetlibMapping()
static internal Dictionary<string, string> NetlibMapping()
{
const int NetLibCount = 8;

Hashtable hash = s_netlibMapping;
Dictionary<string, string> hash = s_netlibMapping;
if (hash == null)
{
hash = new Hashtable(NetLibCount)
hash = new Dictionary<string, string>(NetLibCount)
{
{ NETLIB.TCPIP, TdsEnums.TCP },
{ NETLIB.NamedPipes, TdsEnums.NP },
Expand Down Expand Up @@ -1150,7 +1150,7 @@ internal static class NETLIB
internal const string VIA = "dbmsgnet";
}

private static Hashtable s_netlibMapping;
private static Dictionary<string, string> s_netlibMapping;

#if NETFRAMEWORK
protected internal override PermissionSet CreatePermissionSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ internal static int GetCurrentProcessIdForTdsLoginOnly()
// Pick up the process Id from the current process instead of randomly generating it.
// This would be helpful while tracing application related issues.
int processId;
#if NET
processId = Environment.ProcessId;
#else
using (System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess())
{
processId = p.Id;
}
#endif
System.Threading.Volatile.Write(ref s_currentProcessId, processId);
}
return s_currentProcessId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Threading;

namespace Microsoft.Data.SqlClient
Expand Down Expand Up @@ -57,7 +57,7 @@ protected override string GetAttestationUrl(string attestationUrl)
}

// Makes a web request to the provided url and returns the response as a byte[]
protected override byte[] MakeRequest(string url)
protected override List<byte> MakeRequest(string url)
{
Exception exception = null;

Expand All @@ -72,8 +72,7 @@ protected override byte[] MakeRequest(string url)

using (Stream stream = s_client.GetStreamAsync(url).ConfigureAwait(false).GetAwaiter().GetResult())
{
var deserializer = new DataContractJsonSerializer(typeof(byte[]));
return (byte[])deserializer.ReadObject(stream);
return JsonSerializer.Deserialize<List<byte>>(stream);
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
edwardneal marked this conversation as resolved.
Show resolved Hide resolved
using System.Diagnostics;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
Expand Down Expand Up @@ -183,8 +184,8 @@ private void VerifyAttestationInfo(string attestationUrl, HealthReport healthRep
VerifyEnclaveReportSignature(enclaveReportPackage, healthReport.Certificate);
}

// Makes a web request to the provided url and returns the response as a byte[]
protected abstract byte[] MakeRequest(string url);
// Makes a web request to the provided url and returns the response as a List<byte>
protected abstract List<byte> MakeRequest(string url);

// Gets the root signing certificate for the provided attestation service.
// If the certificate does not exist in the cache, this will make a call to the
Expand All @@ -197,13 +198,18 @@ private X509Certificate2Collection GetSigningCertificate(string attestationUrl,
X509Certificate2Collection signingCertificates = rootSigningCertificateCache.Get<X509Certificate2Collection>(attestationUrl);
if (forceUpdate || signingCertificates == null || AnyCertificatesExpired(signingCertificates))
{
byte[] data = MakeRequest(attestationUrl);
List<byte> data = MakeRequest(attestationUrl);
var certificateCollection = new X509Certificate2Collection();

try
{
var s = new SignedCms();
s.Decode(data);
SignedCms s = new SignedCms();
#if NET
Span<byte> dataSpan = System.Runtime.InteropServices.CollectionsMarshal.AsSpan(data);
s.Decode(dataSpan);
#else
s.Decode(data.ToArray());
#endif
certificateCollection.AddRange(s.Certificates);
}
catch (CryptographicException exception)
Expand Down Expand Up @@ -263,10 +269,10 @@ private bool VerifyHealthReportAgainstRootCertificate(X509Certificate2Collection
// An Always Encrypted-enabled driver doesn't verify an expiration date or a certificate authority chain.
// A certificate is simply used as a key pair consisting of a public and private key. This is by design.

#pragma warning disable IA5352
#pragma warning disable IA5352
// CodeQL [SM00395] By design. Always Encrypted certificates should not be checked.
chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
#pragma warning restore IA5352
#pragma warning restore IA5352

if (!chain.Build(healthReportCert))
{
Expand Down Expand Up @@ -424,6 +430,6 @@ private byte[] GetSharedSecret(EnclavePublicKey enclavePublicKey, EnclaveDiffieH
return KeyConverter.DeriveKey(clientDHKey, ecdh.PublicKey);
}
}
#endregion
#endregion
}
}