@@ -280,7 +280,7 @@ before deployment.
280280There is a method in the ` PivSession ` class to replace the attestation key and cert.
281281
282282``` csharp
283- public void ReplaceAttestationKeyAndCertificate (PivPrivateKey privateKey , X509Certificate2 certificate )
283+ public void ReplaceAttestationKeyAndCertificate (IPrivateKey privateKey , X509Certificate2 certificate )
284284```
285285
286286If you use this method to replace the key and cert, it will check the certificate to make
@@ -301,28 +301,31 @@ class is not one you should use with sensitive data, so we present this techniqu
301301using System .Security .Cryptography ;
302302using System .Security .Cryptography .X509Certificates ;
303303
304- private static bool IsMatchingKeyAndCert (PivPrivateKey privateKey , X509Certificate2 certificate )
304+ private static bool IsMatchingKeyAndCert (IPrivateKey privateKey , X509Certificate2 certificate )
305305{
306- if (privateKey . Algorithm == PivAlgorithm . Rsa2048 )
306+ if (privateKey is RSAPrivateKey rsaPrivateKey )
307307 {
308- return IsMatchingKeyAndCertRsa (( PivRsaPrivateKey ) privateKey , (RSA )certificate .PublicKey .Key );
308+ return IsMatchingKeyAndCertRsa (rsaPrivateKey , (RSA )certificate .PublicKey .Key );
309309 }
310310
311- return IsMatchingKeyAndCertEcc ((PivEccPrivateKey )privateKey , (byte [])certificate .PublicKey .EncodedKeyValue );
311+ if (privateKey is ECPrivateKey ecPrivateKey )
312+ {
313+ return IsMatchingKeyAndCertEcc (ecPrivateKey , (byte [])certificate .PublicKey .EncodedKeyValue );
314+ }
315+
316+ throw new ArgumentException (" Unsupported key type" );
312317}
313318
314- private static bool IsMatchingKeyAndCertRsa (PivRsaPrivateKey privateKey , RSA publicKey )
319+ private static bool IsMatchingKeyAndCertRsa (RSAPrivateKey privateKey , RSA publicKey )
315320{
316- bool returnValue = isValidCert ;
317-
318321 // In order to build a System.Security.Cryptography.RSA object
319322 // that contains the private key, we must provide all possible
320323 // components: modulus, public exponent, private exponent, CRT
321324 // info.
322325 // We have everything needed from the publicKey (an RSA object)
323- // and privateKey (a PivRsaPrivateKey object) except for the
326+ // and privateKey (an RSAPrivateKey object) except for the
324327 // private exponent. If you have the CRT info, you don't need the
325- // private exponent, so the PivRsaPrivateKey class doesn't keep
328+ // private exponent, so the RSAPrivateKey class doesn't keep
326329 // it (and the YubiKey itself does not keep it).
327330 // But in order to build the RSA private key-containing object we
328331 // need to obtain the private exponent. Except we don't really.
@@ -333,6 +336,7 @@ private static bool IsMatchingKeyAndCertRsa(PivRsaPrivateKey privateKey, RSA pub
333336 // using an arbitrary private exponent.
334337
335338 RSAParameters publicParams = publicKey .ExportParameters (false );
339+ RSAParameters keyParams = privateKey .Parameters ;
336340 byte [] fakeExponent = new byte [publicParams .Modulus .Length ];
337341 byte [] modCopy = new byte [publicParams .Modulus .Length ];
338342 byte [] expCopy = new byte [publicParams .Exponent .Length ];
@@ -358,11 +362,11 @@ private static bool IsMatchingKeyAndCertRsa(PivRsaPrivateKey privateKey, RSA pub
358362 try
359363 {
360364 rsaParams .D = fakeExponent ;
361- rsaParams .DP = privateKey . ExponentP . ToArray () ;
362- rsaParams .DQ = privateKey . ExponentQ . ToArray () ;
363- rsaParams .InverseQ = privateKey . Coefficient . ToArray () ;
364- rsaParams .P = privateKey . PrimeP . ToArray () ;
365- rsaParams .Q = privateKey . PrimeQ . ToArray () ;
365+ rsaParams .DP = keyParams . DP ;
366+ rsaParams .DQ = keyParams . DQ ;
367+ rsaParams .InverseQ = keyParams . InverseQ ;
368+ rsaParams .P = keyParams . P ;
369+ rsaParams .Q = keyParams . Q ;
366370 rsaParams .Modulus = modCopy ;
367371 rsaParams .Exponent = expCopy ;
368372
@@ -385,11 +389,9 @@ private static bool IsMatchingKeyAndCertRsa(PivRsaPrivateKey privateKey, RSA pub
385389 }
386390}
387391
388- private static bool IsMatchingKeyAndCertEcc (PivEccPrivateKey privateKey , byte [] publicKey )
392+ private static bool IsMatchingKeyAndCertEcc (ECPrivateKey privateKey , byte [] publicKey )
389393{
390- bool returnValue = false ;
391-
392- ECCurve eccCurve = privateKey .Algorithm == PivAlgorithm .EccP256 ?
394+ ECCurve eccCurve = privateKey .KeyType == KeyType .ECP256 ?
393395 ECCurve .CreateFromValue (" 1.2.840.10045.3.1.7" ) :
394396 ECCurve .CreateFromValue (" 1.3.132.0.34" );
395397
@@ -407,7 +409,7 @@ private static bool IsMatchingKeyAndCertEcc(PivEccPrivateKey privateKey, byte[]
407409 Array .Copy (publicKey , 1 + coordLength , yCoord , 0 , coordLength );
408410 eccParams .Q .X = xCoord ;
409411 eccParams .Q .Y = yCoord ;
410- eccParams .D = privateKey .PrivateValue . ToArray () ;
412+ eccParams .D = privateKey .Parameters . D ;
411413
412414 // To determine if the public key in the cert is the partner
413415 // to the private key, sign random data using that private
0 commit comments