@@ -24,11 +24,12 @@ import (
2424)
2525
2626type stubPivKey struct {
27- attestCA * minica.CA
28- userCA * minica.CA
29- attestMap map [piv.Slot ]* x509.Certificate
30- certMap map [piv.Slot ]* x509.Certificate
31- signerMap map [piv.Slot ]interface {}
27+ attestCA * minica.CA
28+ userCA * minica.CA
29+ attestMap map [piv.Slot ]* x509.Certificate
30+ certMap map [piv.Slot ]* x509.Certificate
31+ signerMap map [piv.Slot ]interface {}
32+ keyOptionsMap map [piv.Slot ]piv.Key
3233}
3334
3435//nolint:typecheck // ignore deadcode warnings
@@ -83,6 +84,7 @@ func newStubPivKey(t *testing.T) *stubPivKey {
8384 piv .SlotAuthentication : attSigner , // 9a
8485 piv .SlotSignature : userSigner , // 9c
8586 },
87+ keyOptionsMap : map [piv.Slot ]piv.Key {},
8688 }
8789}
8890
@@ -144,6 +146,7 @@ func (s *stubPivKey) GenerateKey(key [24]byte, slot piv.Slot, opts piv.Key) (cry
144146 }
145147
146148 s .signerMap [slot ] = signer
149+ s .keyOptionsMap [slot ] = opts
147150 return signer .Public (), nil
148151}
149152
@@ -539,6 +542,20 @@ func TestYubiKey_CreateKey(t *testing.T) {
539542 },
540543 }
541544 }, false },
545+ {"ok with policies" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
546+ Name : "yubikey:slot-id=82" ,
547+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
548+ PINPolicy : apiv1 .PINPolicyNever ,
549+ TouchPolicy : apiv1 .TouchPolicyAlways ,
550+ }}, func () * apiv1.CreateKeyResponse {
551+ return & apiv1.CreateKeyResponse {
552+ Name : "yubikey:slot-id=82" ,
553+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
554+ CreateSignerRequest : apiv1.CreateSignerRequest {
555+ SigningKey : "yubikey:slot-id=82" ,
556+ },
557+ }
558+ }, false },
542559 {"fail rsa 4096" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
543560 Name : "yubikey:slot-id=82" ,
544561 SignatureAlgorithm : apiv1 .SHA256WithRSA ,
@@ -559,9 +576,152 @@ func TestYubiKey_CreateKey(t *testing.T) {
559576 }
560577 for _ , tt := range tests {
561578 t .Run (tt .name , func (t * testing.T ) {
562- if tt .name == "fail getSlotAndName" {
563- t .Log (tt .name )
579+ k := & YubiKey {
580+ yk : tt .fields .yk ,
581+ pin : tt .fields .pin ,
582+ managementKey : tt .fields .managementKey ,
583+ }
584+ got , err := k .CreateKey (tt .args .req )
585+ if (err != nil ) != tt .wantErr {
586+ t .Errorf ("YubiKey.CreateKey() error = %v, wantErr %v" , err , tt .wantErr )
587+ return
588+ }
589+ want := tt .wantFn ()
590+ if ! reflect .DeepEqual (got , want ) {
591+ t .Errorf ("YubiKey.CreateKey() = %v, want %v" , got , want )
592+ }
593+ })
594+ }
595+ }
596+
597+ func TestYubiKey_CreateKey_policies (t * testing.T ) {
598+ yk := newStubPivKey (t )
599+
600+ type fields struct {
601+ yk pivKey
602+ pin string
603+ managementKey [24 ]byte
604+ }
605+ type args struct {
606+ req * apiv1.CreateKeyRequest
607+ }
608+ tests := []struct {
609+ name string
610+ fields fields
611+ args args
612+ wantSlot piv.Slot
613+ wantPinPolicy piv.PINPolicy
614+ wantTouchPolicy piv.TouchPolicy
615+ wantFn func () * apiv1.CreateKeyResponse
616+ wantErr bool
617+ }{
618+ {"ok" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
619+ Name : "yubikey:slot-id=82" ,
620+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
621+ }}, slotMapping ["82" ], piv .PINPolicyAlways , piv .TouchPolicyNever , func () * apiv1.CreateKeyResponse {
622+ return & apiv1.CreateKeyResponse {
623+ Name : "yubikey:slot-id=82" ,
624+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
625+ CreateSignerRequest : apiv1.CreateSignerRequest {
626+ SigningKey : "yubikey:slot-id=82" ,
627+ },
628+ }
629+ }, false },
630+ {"ok PINPolicyNever" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
631+ Name : "yubikey:slot-id=82" ,
632+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
633+ PINPolicy : apiv1 .PINPolicyNever ,
634+ }}, slotMapping ["82" ], piv .PINPolicyNever , piv .TouchPolicyNever , func () * apiv1.CreateKeyResponse {
635+ return & apiv1.CreateKeyResponse {
636+ Name : "yubikey:slot-id=82" ,
637+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
638+ CreateSignerRequest : apiv1.CreateSignerRequest {
639+ SigningKey : "yubikey:slot-id=82" ,
640+ },
564641 }
642+ }, false },
643+ {"ok PINPolicyOnce" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
644+ Name : "yubikey:slot-id=82" ,
645+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
646+ PINPolicy : apiv1 .PINPolicyOnce ,
647+ }}, slotMapping ["82" ], piv .PINPolicyOnce , piv .TouchPolicyNever , func () * apiv1.CreateKeyResponse {
648+ return & apiv1.CreateKeyResponse {
649+ Name : "yubikey:slot-id=82" ,
650+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
651+ CreateSignerRequest : apiv1.CreateSignerRequest {
652+ SigningKey : "yubikey:slot-id=82" ,
653+ },
654+ }
655+ }, false },
656+ {"ok PINPolicyAlways" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
657+ Name : "yubikey:slot-id=82" ,
658+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
659+ PINPolicy : apiv1 .PINPolicyAlways ,
660+ }}, slotMapping ["82" ], piv .PINPolicyAlways , piv .TouchPolicyNever , func () * apiv1.CreateKeyResponse {
661+ return & apiv1.CreateKeyResponse {
662+ Name : "yubikey:slot-id=82" ,
663+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
664+ CreateSignerRequest : apiv1.CreateSignerRequest {
665+ SigningKey : "yubikey:slot-id=82" ,
666+ },
667+ }
668+ }, false },
669+ {"ok TouchPolicyNever" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
670+ Name : "yubikey:slot-id=82" ,
671+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
672+ TouchPolicy : apiv1 .TouchPolicyNever ,
673+ }}, slotMapping ["82" ], piv .PINPolicyAlways , piv .TouchPolicyNever , func () * apiv1.CreateKeyResponse {
674+ return & apiv1.CreateKeyResponse {
675+ Name : "yubikey:slot-id=82" ,
676+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
677+ CreateSignerRequest : apiv1.CreateSignerRequest {
678+ SigningKey : "yubikey:slot-id=82" ,
679+ },
680+ }
681+ }, false },
682+ {"ok TouchPolicyAlways" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
683+ Name : "yubikey:slot-id=82" ,
684+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
685+ TouchPolicy : apiv1 .TouchPolicyAlways ,
686+ }}, slotMapping ["82" ], piv .PINPolicyAlways , piv .TouchPolicyAlways , func () * apiv1.CreateKeyResponse {
687+ return & apiv1.CreateKeyResponse {
688+ Name : "yubikey:slot-id=82" ,
689+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
690+ CreateSignerRequest : apiv1.CreateSignerRequest {
691+ SigningKey : "yubikey:slot-id=82" ,
692+ },
693+ }
694+ }, false },
695+ {"ok TouchPolicyCached" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
696+ Name : "yubikey:slot-id=82" ,
697+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
698+ TouchPolicy : apiv1 .TouchPolicyCached ,
699+ }}, slotMapping ["82" ], piv .PINPolicyAlways , piv .TouchPolicyCached , func () * apiv1.CreateKeyResponse {
700+ return & apiv1.CreateKeyResponse {
701+ Name : "yubikey:slot-id=82" ,
702+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
703+ CreateSignerRequest : apiv1.CreateSignerRequest {
704+ SigningKey : "yubikey:slot-id=82" ,
705+ },
706+ }
707+ }, false },
708+ {"ok both policies" , fields {yk , "123456" , piv .DefaultManagementKey }, args {& apiv1.CreateKeyRequest {
709+ Name : "yubikey:slot-id=82" ,
710+ SignatureAlgorithm : apiv1 .ECDSAWithSHA256 ,
711+ PINPolicy : apiv1 .PINPolicyNever ,
712+ TouchPolicy : apiv1 .TouchPolicyAlways ,
713+ }}, slotMapping ["82" ], piv .PINPolicyNever , piv .TouchPolicyAlways , func () * apiv1.CreateKeyResponse {
714+ return & apiv1.CreateKeyResponse {
715+ Name : "yubikey:slot-id=82" ,
716+ PublicKey : yk .signerMap [slotMapping ["82" ]].(crypto.Signer ).Public (),
717+ CreateSignerRequest : apiv1.CreateSignerRequest {
718+ SigningKey : "yubikey:slot-id=82" ,
719+ },
720+ }
721+ }, false },
722+ }
723+ for _ , tt := range tests {
724+ t .Run (tt .name , func (t * testing.T ) {
565725 k := & YubiKey {
566726 yk : tt .fields .yk ,
567727 pin : tt .fields .pin ,
@@ -572,10 +732,17 @@ func TestYubiKey_CreateKey(t *testing.T) {
572732 t .Errorf ("YubiKey.CreateKey() error = %v, wantErr %v" , err , tt .wantErr )
573733 return
574734 }
735+ if v := yk .keyOptionsMap [tt .wantSlot ].PINPolicy ; ! reflect .DeepEqual (v , tt .wantPinPolicy ) {
736+ t .Errorf ("YubiKey.CreateKey() PINPolicy = %v, want %v" , v , tt .wantPinPolicy )
737+ }
738+ if v := yk .keyOptionsMap [tt .wantSlot ].TouchPolicy ; ! reflect .DeepEqual (v , tt .wantTouchPolicy ) {
739+ t .Errorf ("YubiKey.CreateKey() TouchPolicy = %v, want %v" , v , tt .wantTouchPolicy )
740+ }
575741 want := tt .wantFn ()
576742 if ! reflect .DeepEqual (got , want ) {
577743 t .Errorf ("YubiKey.CreateKey() = %v, want %v" , got , want )
578744 }
745+
579746 })
580747 }
581748}
0 commit comments