Skip to content

Commit

Permalink
Remove a few more unnecessary allocations in X.509
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones committed May 27, 2022
1 parent ecb4e6c commit 7f7c6a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 40 deletions.
Expand Up @@ -118,7 +118,7 @@ public void Dispose()

private SafeCreateHandle PreparePoliciesArray(bool checkRevocation)
{
IntPtr[] policies = new IntPtr[checkRevocation ? 2 : 1];
Span<IntPtr> policies = checkRevocation ? stackalloc IntPtr[2] : stackalloc IntPtr[1];

SafeHandle defaultPolicy = Interop.AppleCrypto.X509ChainCreateDefaultPolicy();

Expand All @@ -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;
Expand Down
Expand Up @@ -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
{
Expand All @@ -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;
}
}
Expand All @@ -343,27 +335,15 @@ public ReadOnlyMemory<byte> RawDataMemory
}
}

public string SerialNumber
{
get
{
return GetSerialNumberString();
}
}
public string SerialNumber => GetSerialNumberString();

public Oid SignatureAlgorithm
{
get
{
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);
}
}

Expand All @@ -373,10 +353,7 @@ public X500DistinguishedName SubjectName
{
ThrowIfInvalid();

X500DistinguishedName? subjectName = _lazySubjectName;
if (subjectName == null)
subjectName = _lazySubjectName = Pal.SubjectName;
return subjectName;
return _lazySubjectName ??= Pal.SubjectName;
}
}

Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 7f7c6a2

Please sign in to comment.