Skip to content

Commit c5e22a6

Browse files
committed
fix: add length check
add constants for compressed and not compressed
1 parent 91fe04c commit c5e22a6

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public sealed partial class PivSession : IDisposable
3333
private const int PivCertInfoTag = 0x71;
3434
private const int PivLrcTag = 0xFE;
3535

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+
3646
[Obsolete("Usage of PivEccPublic/PivEccPrivateKey is deprecated. Use IPublicKey, IPrivateKey instead", false)]
3747
public PivPublicKey GenerateKeyPair(
3848
byte slotNumber,
@@ -483,13 +493,11 @@ public void ImportCertificate(byte slotNumber, X509Certificate2 certificate, boo
483493

484494
RefreshManagementKeyAuthentication();
485495

486-
var dataTag = GetCertDataTagFromSlotNumber(slotNumber);
487-
488-
byte certInfo = 0x00;
496+
byte certInfo = UncompressedCert;
489497
byte[] certDer = certificate.GetRawCertData();
490498
if (compress)
491499
{
492-
certInfo = 0x01; // Indicates the certificate is compressed
500+
certInfo = CompressedCert;
493501
try
494502
{
495503
certDer = Compress(certDer);
@@ -504,7 +512,6 @@ public void ImportCertificate(byte slotNumber, X509Certificate2 certificate, boo
504512
}
505513

506514
var tlvWriter = new TlvWriter();
507-
508515
using (tlvWriter.WriteNestedTlv(PivEncodingTag))
509516
{
510517
tlvWriter.WriteValue(PivCertTag, certDer);
@@ -514,6 +521,7 @@ public void ImportCertificate(byte slotNumber, X509Certificate2 certificate, boo
514521

515522
byte[] encodedCert = tlvWriter.Encode();
516523

524+
var dataTag = GetCertDataTagFromSlotNumber(slotNumber);
517525
var command = new PutDataCommand((int)dataTag, encodedCert);
518526
var response = Connection.SendCommand(command);
519527
if (response.Status != ResponseStatus.Success)
@@ -580,7 +588,7 @@ public X509Certificate2 GetCertificate(byte slotNumber)
580588
}
581589

582590
byte[] certBytesCopy = certBytes.ToArray();
583-
bool isCompressed = hasCertInfo && certInfo.Span[0] == 0x01;
591+
bool isCompressed = hasCertInfo && certInfo.Length > 0 && certInfo.Span[0] == CompressedCert;
584592
if (!isCompressed)
585593
{
586594
return new X509Certificate2(certBytesCopy);

0 commit comments

Comments
 (0)