Skip to content

Commit e5c0a53

Browse files
committed
misc: work on importing certificates
added comment removed unnecessary close() calls early return exit
1 parent e391733 commit e5c0a53

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Piv/PivSession.KeyPairs.cs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ public void ImportCertificate(byte slotNumber, X509Certificate2 certificate, boo
489489
byte[] certDer = certificate.GetRawCertData();
490490
if (compress)
491491
{
492-
certInfo = 0x01;
492+
certInfo = 0x01; // Indicates the certificate is compressed
493493
try
494494
{
495495
certDer = Compress(certDer);
@@ -571,31 +571,32 @@ public X509Certificate2 GetCertificate(byte slotNumber)
571571
var certData = TlvObjects.DecodeDictionary(responseData.Span);
572572

573573
bool hasCertInfo = certData.TryGetValue(PivCertInfoTag, out var certInfo);
574-
if (certData.TryGetValue(PivCertTag, out var certBytes))
574+
if (!certData.TryGetValue(PivCertTag, out var certBytes))
575575
{
576-
bool compressed = hasCertInfo && certInfo.Length > 0 &&
577-
((certInfo.Span[0] & 0x01) == 0x01);
578-
if (compressed)
579-
{
580-
try
581-
{
582-
certBytes = Decompress(certBytes.ToArray());
583-
}
584-
catch (Exception)
585-
{
586-
throw new InvalidOperationException(
587-
string.Format(
588-
CultureInfo.CurrentCulture,
589-
ExceptionMessages.FailedDecompressingCertificate));
590-
}
591-
}
592-
return new X509Certificate2(certBytes.ToArray());
576+
throw new InvalidOperationException(
577+
string.Format(
578+
CultureInfo.CurrentCulture,
579+
ExceptionMessages.FailedParsingCertificate));
593580
}
594581

595-
throw new InvalidOperationException(
596-
string.Format(
597-
CultureInfo.CurrentCulture,
598-
ExceptionMessages.FailedParsingCertificate));
582+
byte[] certBytesCopy = certBytes.ToArray();
583+
bool isCompressed = hasCertInfo && certInfo.Span[0] == 0x01;
584+
if (!isCompressed)
585+
{
586+
return new X509Certificate2(certBytesCopy);
587+
}
588+
589+
try
590+
{
591+
return new X509Certificate2(Decompress(certBytesCopy));
592+
}
593+
catch (Exception)
594+
{
595+
throw new InvalidOperationException(
596+
string.Format(
597+
CultureInfo.CurrentCulture,
598+
ExceptionMessages.FailedDecompressingCertificate));
599+
}
599600
}
600601

601602
// There is a DataTag to use when calling PUT DATA. To put a cert onto
@@ -635,9 +636,11 @@ private void RefreshManagementKeyAuthentication()
635636
static private byte[] Compress(byte[] data)
636637
{
637638
using var compressedStream = new MemoryStream();
638-
using var compressor = new GZipStream(compressedStream, CompressionLevel.Optimal);
639-
compressor.Write(data, 0, data.Length);
640-
compressor.Close();
639+
using (var compressor = new GZipStream(compressedStream, CompressionLevel.Optimal))
640+
{
641+
compressor.Write(data, 0, data.Length);
642+
}
643+
641644
return compressedStream.ToArray();
642645
}
643646

@@ -647,7 +650,7 @@ static private byte[] Decompress(byte[] data)
647650
using var decompressor = new GZipStream(dataStream, CompressionMode.Decompress);
648651
using var decompressedStream = new MemoryStream();
649652
decompressor.CopyTo(decompressedStream);
650-
decompressor.Close();
653+
651654
return decompressedStream.ToArray();
652655
}
653656
}

0 commit comments

Comments
 (0)