@@ -17,6 +17,7 @@ import (
1717 "crypto/x509/pkix"
1818 "encoding/asn1"
1919 "errors"
20+ "fmt"
2021 "reflect"
2122 "sync"
2223 "testing"
@@ -33,6 +34,7 @@ type stubPivKey struct {
3334 attestCA * minica.CA
3435 attestSigner privateKey
3536 userCA * minica.CA
37+ keyInfoMap map [piv.Slot ]piv.KeyInfo
3638 attestMap map [piv.Slot ]* x509.Certificate
3739 certMap map [piv.Slot ]* x509.Certificate
3840 signerMap map [piv.Slot ]interface {}
@@ -73,8 +75,10 @@ func newStubPivKey(t *testing.T, alg symmetricAlgorithm) *stubPivKey {
7375 t .Fatal (err )
7476 }
7577
78+ var keyInfoAlgo piv.Algorithm
7679 switch alg {
7780 case ECDSA :
81+ keyInfoAlgo = piv .AlgorithmEC256
7882 attSigner , err = ecdsa .GenerateKey (elliptic .P256 (), rand .Reader )
7983 if err != nil {
8084 t .Fatal (err )
@@ -84,6 +88,7 @@ func newStubPivKey(t *testing.T, alg symmetricAlgorithm) *stubPivKey {
8488 t .Fatal (err )
8589 }
8690 case RSA :
91+ keyInfoAlgo = piv .AlgorithmRSA2048
8792 attSigner , err = rsa .GenerateKey (rand .Reader , rsaKeySize )
8893 if err != nil {
8994 t .Fatal (err )
@@ -124,6 +129,15 @@ func newStubPivKey(t *testing.T, alg symmetricAlgorithm) *stubPivKey {
124129 attestCA : attestCA ,
125130 attestSigner : attSigner ,
126131 userCA : userCA ,
132+ keyInfoMap : map [piv.Slot ]piv.KeyInfo {
133+ piv .SlotKeyManagement : {
134+ PublicKey : attSigner .Public (),
135+ Algorithm : keyInfoAlgo ,
136+ PINPolicy : piv .PINPolicyOnce ,
137+ TouchPolicy : piv .TouchPolicyCached ,
138+ Origin : piv .OriginGenerated ,
139+ }, // 9d
140+ },
127141 attestMap : map [piv.Slot ]* x509.Certificate {
128142 piv .SlotAuthentication : attCert , // 9a
129143 },
@@ -140,10 +154,21 @@ func newStubPivKey(t *testing.T, alg symmetricAlgorithm) *stubPivKey {
140154 }
141155}
142156
157+ func (s * stubPivKey ) KeyInfo (slot piv.Slot ) (piv.KeyInfo , error ) {
158+ keyInfo , ok := s .keyInfoMap [slot ]
159+ if ! ok {
160+ return piv.KeyInfo {}, errors .New ("public key not found" )
161+ }
162+ return keyInfo , nil
163+ }
164+
143165func (s * stubPivKey ) Certificate (slot piv.Slot ) (* x509.Certificate , error ) {
144166 cert , ok := s .certMap [slot ]
145167 if ! ok {
146- return nil , errors .New ("certificate not found" )
168+ if slot == slotMapping ["82" ] {
169+ return nil , errors .New ("command failed: some error" )
170+ }
171+ return nil , fmt .Errorf ("command failed: %w" , piv .ErrNotFound )
147172 }
148173 return cert , nil
149174}
@@ -523,13 +548,22 @@ func TestYubiKey_GetPublicKey(t *testing.T) {
523548 want crypto.PublicKey
524549 wantErr bool
525550 }{
526- {"ok" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.GetPublicKeyRequest {
551+ {"ok with keyInfo" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.GetPublicKeyRequest {
552+ Name : "yubikey:slot-id=9d" ,
553+ }}, yk .keyInfoMap [piv .SlotKeyManagement ].PublicKey , false },
554+ {"ok with Attest" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.GetPublicKeyRequest {
555+ Name : "yubikey:slot-id=9a" ,
556+ }}, yk .attestMap [piv .SlotAuthentication ].PublicKey , false },
557+ {"ok with certificate" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.GetPublicKeyRequest {
527558 Name : "yubikey:slot-id=9c" ,
528559 }}, yk .certMap [piv .SlotSignature ].PublicKey , false },
529560 {"fail getSlot" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.GetPublicKeyRequest {
530561 Name : "slot-id=9c" ,
531562 }}, nil , true },
532563 {"fail getPublicKey" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.GetPublicKeyRequest {
564+ Name : "yubikey:slot-id=82" ,
565+ }}, nil , true },
566+ {"fail getPublicKey not found" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.GetPublicKeyRequest {
533567 Name : "yubikey:slot-id=85" ,
534568 }}, nil , true },
535569 }
0 commit comments