@@ -489,7 +489,7 @@ public void ImportCertificate(byte slotNumber, X509Certificate2 certificate, boo
489
489
byte [ ] certDer = certificate . GetRawCertData ( ) ;
490
490
if ( compress )
491
491
{
492
- certInfo = 0x01 ;
492
+ certInfo = 0x01 ; // Indicates the certificate is compressed
493
493
try
494
494
{
495
495
certDer = Compress ( certDer ) ;
@@ -571,31 +571,32 @@ public X509Certificate2 GetCertificate(byte slotNumber)
571
571
var certData = TlvObjects . DecodeDictionary ( responseData . Span ) ;
572
572
573
573
bool hasCertInfo = certData . TryGetValue ( PivCertInfoTag , out var certInfo ) ;
574
- if ( certData . TryGetValue ( PivCertTag , out var certBytes ) )
574
+ if ( ! certData . TryGetValue ( PivCertTag , out var certBytes ) )
575
575
{
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 ) ) ;
593
580
}
594
581
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
+ }
599
600
}
600
601
601
602
// There is a DataTag to use when calling PUT DATA. To put a cert onto
@@ -635,9 +636,11 @@ private void RefreshManagementKeyAuthentication()
635
636
static private byte [ ] Compress ( byte [ ] data )
636
637
{
637
638
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
+
641
644
return compressedStream . ToArray ( ) ;
642
645
}
643
646
@@ -647,7 +650,7 @@ static private byte[] Decompress(byte[] data)
647
650
using var decompressor = new GZipStream ( dataStream , CompressionMode . Decompress ) ;
648
651
using var decompressedStream = new MemoryStream ( ) ;
649
652
decompressor . CopyTo ( decompressedStream ) ;
650
- decompressor . Close ( ) ;
653
+
651
654
return decompressedStream . ToArray ( ) ;
652
655
}
653
656
}
0 commit comments