Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Read/Write CHUID #67

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions piv/piv.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,3 +846,121 @@ func ykSetProtectedMetadata(tx *scTx, key [24]byte, m *Metadata) error {
}
return nil
}

// CardID is the Card Holder Unique Identifier with settable GUID
// Raw contains the whole object
// GUID contains the Card Universally Unique Identifier.
type CardID struct {
Raw []byte
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make raw unexported and in a future PR add whatever other field you want to be able to inspect. FASC-N, Exp. Date, etc.

Though if you want to use the signature in the API in a follow up PR, I think I'd prefer something like:

func (c *CardID) Verify(pub crypto.PublicKey) error
func (c *CardID) Sign(priv crypto.Signer) error

Copy link
Author

Choose a reason for hiding this comment

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

agreed

GUID [16]byte
}

// CardID returns the card CHUID with the GUID extracted
func (yk *YubiKey) CardID() (CardID, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

For the doc comment: can you call this method before calling SetCardID?

nit: for consistency with other APIs in this package, return a pointer

func (yk *YubiKey) CardID() (*CardID, error)

Copy link
Author

Choose a reason for hiding this comment

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

agreed

return ykGetCardID(yk.tx)

}

/*
* From https://github.com/Yubico/yubico-piv-tool/blob/ebee7f63b85fe4373efc4d8d44cbe5fe321c158c/lib/util.c#L44
* Format defined in SP-800-73-4, Appendix A, Table 9
*
* FASC-N containing S9999F9999F999999F0F1F0000000000300001E encoded in
* 4-bit BCD with 1 bit parity. run through the tools/fasc.pl script to get
* bytes. This CHUID has an expiry of 2030-01-01.
*
* Defined fields:
* - 0x30: FASC-N (hard-coded)
* - 0x34: Card UUID / GUID (settable)
* - 0x35: Exp. Date (hard-coded)
* - 0x3e: Signature (hard-coded, empty)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are these actually not settable or is that just the API that yubico's tools expose?

Copy link
Author

Choose a reason for hiding this comment

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

They can be set - it's hardcoded in the yubico implementation, i was thinking of eventually add these and implement the BCD encoding too in a future PR.

* - 0xfe: Error Detection Code (hard-coded)
*/

var chuidTemplate = []byte{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where does this come from? Would default field values be better?

Copy link
Author

Choose a reason for hiding this comment

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

This is coming straight from yubico tools. they generated that - for now, it would suggest to do the same, and later add the fields in the CardID struct and add the encoding.

0x30, 0x19, 0xd4, 0xe7, 0x39, 0xda, 0x73, 0x9c, 0xed, 0x39, 0xce, 0x73, 0x9d,
0x83, 0x68, 0x58, 0x21, 0x08, 0x42, 0x10, 0x84, 0x21, 0xc8, 0x42, 0x10, 0xc3,
0xeb, 0x34, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35, 0x08, 0x32, 0x30, 0x33, 0x30, 0x30,
0x31, 0x30, 0x31, 0x3e, 0x00, 0xfe, 0x00,
}

const uuidOffset = 29

func ykGetCardID(tx *scTx) (id CardID, err error) {
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=17
// OID for CardId is 5FC102

cmd := apdu{
instruction: insGetData,
param1: 0x3f,
param2: 0xff,
data: []byte{
0x5c, // Tag list
0x03,
0x5f,
0xc1,
0x02,
},
}
resp, err := tx.Transmit(cmd)
if err != nil {
return id, fmt.Errorf("command failed: %w", err)
}
// https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=85
obj, _, err := unmarshalASN1(resp, 1, 0x13) // tag 0x53
if err != nil {
return id, fmt.Errorf("unmarshaling response: %v", err)
}
id.Raw = obj
if obj[27] == 0x34 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Before doing any slice indexes you need to verify the length of the returned object.

if len(obj) < n {
    // return error
}
uuid := obj[n:]

Copy link
Author

Choose a reason for hiding this comment

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

agreed

endPos := uuidOffset + int(obj[28])
Copy link
Collaborator

Choose a reason for hiding this comment

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

UUID length must be 16 bytes? https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-73-4.pdf#page=26

Return an error if it's not?

Copy link
Author

Choose a reason for hiding this comment

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

agreed

copy(id.GUID[:], obj[uuidOffset:endPos])
}
return
}

// SetCardID initialize the CHUID card object using a predefined template defined as
// * Defined fields:
Copy link
Collaborator

Choose a reason for hiding this comment

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

The Defined fields is really helpful for review 😃 but should be in the doc comment. Doc comment should only document what's exposed in the API

Copy link
Author

Choose a reason for hiding this comment

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

agreed

// - 0x30: FASC-N (hard-coded)
// - 0x34: Card UUID / GUID (settable)
// - 0x35: Exp. Date (hard-coded)
// - 0x3e: Signature (hard-coded, empty)
// - 0xfe: Error Detection Code (hard-coded)
func (yk *YubiKey) SetCardID(GUID [16]byte, key [24]byte) (CardID, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be:

func(yk *YubiKey) SetCardID(key [24], cardID *CardID) error
  • All other methods in this package have the management key/PIN/PUK as their first argument
  • If we ever wanted other methods to be settable (e.g. signature) then we don't need to add new APIs
  • We don't return a CardID, a successful SetCardID should imply that the state on the card is equivalent to the passed CardID

Copy link
Author

Choose a reason for hiding this comment

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

agreed

return ykSetCardID(yk.tx, key, GUID)

}

func ykSetCardID(tx *scTx, key [24]byte, guid [16]byte) (id CardID, err error) {

id.Raw = make([]byte, len(chuidTemplate))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just build the fields yourself with default values instead of using a template?

data := marshalASN1(/* tag */, defaultFASCN)
data = append(data, marshalASN1(/* tag */, guid[:]))
data = append(data, marshalASN1(/* tag */, defaultExp))
data = append(data, marshalASN1(/* tag */, defaultSig))
data = append(data, marshalASN1(/* tag */, defaultErrorCorrectionCode))

That way if you ever want to make something like the signature writable, it's clear where to modify the value.

Copy link
Author

Choose a reason for hiding this comment

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

can we do that in a future PR as it would be better to pass a full cardID and not have any default ?

copy(id.Raw, chuidTemplate)
copy(id.Raw[uuidOffset:], guid[:])

data := append([]byte{
0x5c, // Tag list
0x03,
0x5f,
0xc1,
0x02,
}, marshalASN1(0x53, id.Raw)...)

cmd := apdu{
instruction: insPutData,
param1: 0x3f,
param2: 0xff,
data: data,
}

if err := ykAuthenticate(tx, key, rand.Reader); err != nil {
return id, fmt.Errorf("authenticating with key: %w", err)
}

_, err = tx.Transmit(cmd)
if err != nil {
return id, fmt.Errorf("command failed: %w", err)
}

return
}
27 changes: 27 additions & 0 deletions piv/piv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,30 @@ func TestMetadataAdditoinalFields(t *testing.T) {
t.Errorf("(*Metadata.marshal, got=0x%x, want=0x%x", got, want)
}
}

func TestYubiKeyCardId(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
yk, close := newTestYubiKey(t)
defer close()
if err := yk.Reset(); err != nil {
t.Fatalf("resetting yubikey: %v", err)
}
if _, err := yk.CardID(); !errors.Is(err, ErrNotFound) {
t.Fatalf("expecting not found chuid")
}
var guid = [16]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff}
_, err := yk.SetCardID(guid, DefaultManagementKey)
if err != nil {
t.Fatal(err)
}
chuid, err := yk.CardID()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(guid[:], chuid.GUID[:]) {
t.Errorf("(*CardID, got=0x%x, want=0x%x", chuid.GUID, guid)
}
}