@@ -33,6 +33,16 @@ public sealed partial class PivSession : IDisposable
33
33
private const int PivCertInfoTag = 0x71 ;
34
34
private const int PivLrcTag = 0xFE ;
35
35
36
+ /// <summary>
37
+ /// Indicates the certificate is not compressed
38
+ /// </summary>
39
+ private const byte UncompressedCert = 0 ;
40
+
41
+ /// <summary>
42
+ /// Indicates the certificate is compressed
43
+ /// </summary>
44
+ private const byte CompressedCert = 1 ;
45
+
36
46
[ Obsolete ( "Usage of PivEccPublic/PivEccPrivateKey is deprecated. Use IPublicKey, IPrivateKey instead" , false ) ]
37
47
public PivPublicKey GenerateKeyPair (
38
48
byte slotNumber ,
@@ -483,13 +493,11 @@ public void ImportCertificate(byte slotNumber, X509Certificate2 certificate, boo
483
493
484
494
RefreshManagementKeyAuthentication ( ) ;
485
495
486
- var dataTag = GetCertDataTagFromSlotNumber ( slotNumber ) ;
487
-
488
- byte certInfo = 0x00 ;
496
+ byte certInfo = UncompressedCert ;
489
497
byte [ ] certDer = certificate . GetRawCertData ( ) ;
490
498
if ( compress )
491
499
{
492
- certInfo = 0x01 ; // Indicates the certificate is compressed
500
+ certInfo = CompressedCert ;
493
501
try
494
502
{
495
503
certDer = Compress ( certDer ) ;
@@ -504,7 +512,6 @@ public void ImportCertificate(byte slotNumber, X509Certificate2 certificate, boo
504
512
}
505
513
506
514
var tlvWriter = new TlvWriter ( ) ;
507
-
508
515
using ( tlvWriter . WriteNestedTlv ( PivEncodingTag ) )
509
516
{
510
517
tlvWriter . WriteValue ( PivCertTag , certDer ) ;
@@ -514,6 +521,7 @@ public void ImportCertificate(byte slotNumber, X509Certificate2 certificate, boo
514
521
515
522
byte [ ] encodedCert = tlvWriter . Encode ( ) ;
516
523
524
+ var dataTag = GetCertDataTagFromSlotNumber ( slotNumber ) ;
517
525
var command = new PutDataCommand ( ( int ) dataTag , encodedCert ) ;
518
526
var response = Connection . SendCommand ( command ) ;
519
527
if ( response . Status != ResponseStatus . Success )
@@ -580,7 +588,7 @@ public X509Certificate2 GetCertificate(byte slotNumber)
580
588
}
581
589
582
590
byte [ ] certBytesCopy = certBytes . ToArray ( ) ;
583
- bool isCompressed = hasCertInfo && certInfo . Span [ 0 ] == 0x01 ;
591
+ bool isCompressed = hasCertInfo && certInfo . Length > 0 && certInfo . Span [ 0 ] == CompressedCert ;
584
592
if ( ! isCompressed )
585
593
{
586
594
return new X509Certificate2 ( certBytesCopy ) ;
0 commit comments