Skip to content

Commit

Permalink
Improve SetManagementKey example in readme, fix ykAuthenticate metada…
Browse files Browse the repository at this point in the history
…ta cmd error handling
  • Loading branch information
Quantu authored Jul 8, 2024
1 parent 05f0241 commit 865e746
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ newPUKInt, err := rand.Int(rand.Reader, big.NewInt(100_000_000))
if err != nil {
// ...
}
var newKey [24]byte
if _, err := io.ReadFull(rand.Reader, newKey[:]); err != nil {
newKey := make([]byte, 24)
if _, err := io.ReadFull(rand.Reader, newKey); err != nil {
// ...
}
// Format with leading zeros.
newPIN := fmt.Sprintf("%06d", newPINInt)
newPUK := fmt.Sprintf("%08d", newPUKInt)

// Set all values to a new value.
if err := yk.SetManagementKey(piv.DefaultManagementKey, newKey[:]); err != nil {
if err := yk.SetManagementKey(piv.DefaultManagementKey, newKey); err != nil {
// ...
}
if err := yk.SetPUK(piv.DefaultPUK, newPUK); err != nil {
Expand All @@ -114,8 +114,8 @@ if err := yk.SetPIN(piv.DefaultPIN, newPIN); err != nil {
// ...
}
// Store management key on the YubiKey.
m := piv.Metadata{ManagementKey: &newKey[:]}
if err := yk.SetMetadata(newKey[:], m); err != nil {
m := piv.Metadata{ManagementKey: &newKey}
if err := yk.SetMetadata(newKey, m); err != nil {
// ...
}

Expand Down
5 changes: 3 additions & 2 deletions v2/piv/piv.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,10 @@ func ykAuthenticate(tx *scTx, key []byte, rand io.Reader, version *version) erro
param2: keyCardManagement,
}
resp, err := tx.Transmit(cmd)
if err == nil {
managementKeyType = resp[2:][0]
if err != nil {
return fmt.Errorf("determining key management type: %w", err)
}
managementKeyType = resp[2:][0]
}

// set challengeLength based on managementKeyType
Expand Down

0 comments on commit 865e746

Please sign in to comment.