From 7f7c6a229b691bd060ee1bf4aeb2cea4c5e52168 Mon Sep 17 00:00:00 2001 From: Kevin Jones Date: Fri, 27 May 2022 11:44:45 -0400 Subject: [PATCH] Remove a few more unnecessary allocations in X.509 --- .../X509Certificates/ChainPal.Apple.cs | 5 +- .../X509Certificates/X509Certificate2.cs | 48 +++++-------------- 2 files changed, 13 insertions(+), 40 deletions(-) diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/ChainPal.Apple.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/ChainPal.Apple.cs index e52360bd27eb0..abcfad67c6206 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/ChainPal.Apple.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/ChainPal.Apple.cs @@ -118,7 +118,7 @@ public void Dispose() private SafeCreateHandle PreparePoliciesArray(bool checkRevocation) { - IntPtr[] policies = new IntPtr[checkRevocation ? 2 : 1]; + Span policies = checkRevocation ? stackalloc IntPtr[2] : stackalloc IntPtr[1]; SafeHandle defaultPolicy = Interop.AppleCrypto.X509ChainCreateDefaultPolicy(); @@ -138,8 +138,7 @@ private SafeCreateHandle PreparePoliciesArray(bool checkRevocation) policies[1] = revPolicy.DangerousGetHandle(); } - SafeCreateHandle policiesArray = - Interop.CoreFoundation.CFArrayCreate(policies, (UIntPtr)policies.Length); + SafeCreateHandle policiesArray = Interop.CoreFoundation.CFArrayCreate(policies); _extraHandles.Push(policiesArray); return policiesArray; diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs index aa0f5b8f9e407..06b48e797386f 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs @@ -288,22 +288,12 @@ public X500DistinguishedName IssuerName { ThrowIfInvalid(); - X500DistinguishedName? issuerName = _lazyIssuerName; - if (issuerName == null) - issuerName = _lazyIssuerName = Pal.IssuerName; - return issuerName; + return _lazyIssuerName ??= Pal.IssuerName; } } - public DateTime NotAfter - { - get { return GetNotAfter(); } - } - - public DateTime NotBefore - { - get { return GetNotBefore(); } - } + public DateTime NotAfter => GetNotAfter(); + public DateTime NotBefore => GetNotBefore(); public PublicKey PublicKey { @@ -312,14 +302,16 @@ public PublicKey PublicKey ThrowIfInvalid(); PublicKey? publicKey = _lazyPublicKey; + if (publicKey == null) { string keyAlgorithmOid = GetKeyAlgorithm(); - byte[] parameters = GetKeyAlgorithmParameters(); - byte[] keyValue = GetPublicKey(); + byte[] parameters = Pal.KeyAlgorithmParameters; + byte[] keyValue = Pal.PublicKeyValue; Oid oid = new Oid(keyAlgorithmOid); publicKey = _lazyPublicKey = new PublicKey(oid, new AsnEncodedData(oid, parameters), new AsnEncodedData(oid, keyValue)); } + return publicKey; } } @@ -343,13 +335,7 @@ public ReadOnlyMemory RawDataMemory } } - public string SerialNumber - { - get - { - return GetSerialNumberString(); - } - } + public string SerialNumber => GetSerialNumberString(); public Oid SignatureAlgorithm { @@ -357,13 +343,7 @@ public Oid SignatureAlgorithm { ThrowIfInvalid(); - Oid? signatureAlgorithm = _lazySignatureAlgorithm; - if (signatureAlgorithm == null) - { - string oidValue = Pal.SignatureAlgorithm; - signatureAlgorithm = _lazySignatureAlgorithm = new Oid(oidValue, null); - } - return signatureAlgorithm; + return _lazySignatureAlgorithm ??= new Oid(Pal.SignatureAlgorithm, null); } } @@ -373,10 +353,7 @@ public X500DistinguishedName SubjectName { ThrowIfInvalid(); - X500DistinguishedName? subjectName = _lazySubjectName; - if (subjectName == null) - subjectName = _lazySubjectName = Pal.SubjectName; - return subjectName; + return _lazySubjectName ??= Pal.SubjectName; } } @@ -445,10 +422,7 @@ public string GetNameInfo(X509NameType nameType, bool forIssuer) return Pal.GetNameInfo(nameType, forIssuer); } - public override string ToString() - { - return base.ToString(fVerbose: true); - } + public override string ToString() => base.ToString(fVerbose: true); public override string ToString(bool verbose) {