Skip to content

Conversation

deuill
Copy link
Contributor

@deuill deuill commented Oct 11, 2021

This commit adds support for all known existing FIPS-certified Yubikey variants, and makes
adding more of these additional variants simpler by ensuring that values for Formfactor
constants follow those for the form-factors emitted by Yubikeys themselves.

piv/key.go Outdated
FormfactorUSBANano
FormfactorUSBCKeychain
FormfactorUSBCNano
FormfactorUSBCLightningKeychain

FormfactorUSBAKeychainFIPS = iota + 0x81
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning for resetting the iota increment here (apart from making the values here explicitly equivalent to their vendor values) is that it's possible that we'll want to add to the middle and end of this list if new regular and FIPS variants appear, in which case keeping compatibility with the implied public interface would require that we mix-and-match the names.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my other comment, let's drop the iota

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the review! I'll give this a do-over and write some tests -- would you rather I rebase or are the intermediate commits good to keep?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure there's only one commit. So rebasing sgtm.

Tests might be overkill? Testing that a constant is a specific value seems like it's covered by the constant declaration :)

piv/key.go Outdated
case 0x05:
a.Formfactor = FormfactorUSBCLightningKeychain
switch {
case e.Value[0] >= FormfactorUSBAKeychain && e.Value[0] <= FormfactorUSBCLightningKeychain:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is terrible, but again the reasoning here is that adding new variants would only require a change to that final FormfactorUSBCLightningKeychain name.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to remove this switch. It was there as a safeguard when I was developing this package, but I think we can let users handle the unknown. :)

Will also prevent users from having to send PRs every time there's a new form factor.

e.Formfactor = Formfactor(e.Value[0])

Copy link
Collaborator

@ericchiang ericchiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit of background. Originally these didn't correspond to the values presented by the YubiKey, later we switched them to match: #73

We probably should remove the "iota" in #74, but let's do that now. Can you update this to not use an iota?

const ( 
    FormfactorUSBAKeychain     = 0x01
    FormfactorUSBANano         = 0x02
    // ...
    FormfactorUSBAKeychainFIPS = 0x81
    FormfactorUSBANanoFIPS     = 0x82
)

Probably good to add a String() method so these print nicely?

var formFactorStrings = map[Formfactor]string{
    FormfactorUSBAKeychain: "USB-A keychain",
    FormfactorUSBANano    : "USB-A nano",
}

func (f Formfactor) String() string {
    if s, ok := formFactorStrings[f]; ok {
        return s
    }
    return fmt.Sprintf("unknown(0x%02x)", int(f))
}

piv/key.go Outdated
case 0x05:
a.Formfactor = FormfactorUSBCLightningKeychain
switch {
case e.Value[0] >= FormfactorUSBAKeychain && e.Value[0] <= FormfactorUSBCLightningKeychain:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to remove this switch. It was there as a safeguard when I was developing this package, but I think we can let users handle the unknown. :)

Will also prevent users from having to send PRs every time there's a new form factor.

e.Formfactor = Formfactor(e.Value[0])

This commit adds support for all known existing FIPS-certified Yubikey
variants, and makes adding more of these additional variants simpler by
ensuring that values for Formfactor constants follow those for the
form-factors emitted by Yubikeys themselves.
@deuill
Copy link
Contributor Author

deuill commented Oct 11, 2021

Fixed and pushed, let's see if tests pass!

Copy link
Collaborator

@ericchiang ericchiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ericchiang ericchiang merged commit 4b80d66 into go-piv:master Oct 11, 2021
@deuill
Copy link
Contributor Author

deuill commented Oct 11, 2021

Thank you! Do you plan on tagging a release soon? I'm looking to enable yubikey-agent support for FIPS keys (which is especially useful, since these don't come with a PGP applet), and will probably need to wait for a tag before updating the respective go.mod file.

@ericchiang
Copy link
Collaborator

Tagged https://github.com/go-piv/piv-go/releases/tag/v1.9.0

@deuill
Copy link
Contributor Author

deuill commented Oct 11, 2021

That's awesome, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants