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

make kolide info table hw keys consistent on all platforms #1724

Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 23 additions & 20 deletions ee/secureenclavesigner/secureenclavesigner_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -123,11 +125,6 @@ func (ses *secureEnclaveSigner) Sign(rand io.Reader, digest []byte, opts crypto.
return nil, fmt.Errorf("not implemented")
}

type keyData struct {
Uid string `json:"uid"`
PubKey string `json:"pub_key"`
}

func (ses *secureEnclaveSigner) currentConsoleUserKey(ctx context.Context) (*ecdsa.PublicKey, error) {
ctx, span := traces.StartSpan(ctx)
defer span.End()
Expand Down Expand Up @@ -172,41 +169,47 @@ func (ses *secureEnclaveSigner) currentConsoleUserKey(ctx context.Context) (*ecd
}

func (ses *secureEnclaveSigner) MarshalJSON() ([]byte, error) {
var keyDatas []keyData
keyMap := make(map[string]string)

for uid, pubKey := range ses.uidPubKeyMap {
pubKeyBytes, err := echelper.PublicEcdsaToB64Der(pubKey)
pubKeyBytes, err := x509.MarshalPKIXPublicKey(pubKey)
if err != nil {
return nil, fmt.Errorf("converting public key to b64 der: %w", err)
return nil, fmt.Errorf("marshalling to PXIX public key: %w", err)
}

keyDatas = append(keyDatas, keyData{
Uid: uid,
PubKey: string(pubKeyBytes),
})

keyMap[uid] = base64.StdEncoding.EncodeToString(pubKeyBytes)
}

return json.Marshal(keyDatas)
return json.Marshal(keyMap)
}

func (ses *secureEnclaveSigner) UnmarshalJSON(data []byte) error {
if ses.uidPubKeyMap == nil {
ses.uidPubKeyMap = make(map[string]*ecdsa.PublicKey)
}

var keyDatas []keyData
if err := json.Unmarshal(data, &keyDatas); err != nil {
var keyMap map[string]string
if err := json.Unmarshal(data, &keyMap); err != nil {
return fmt.Errorf("unmarshalling key data: %w", err)
}

for _, kd := range keyDatas {
pubKey, err := echelper.PublicB64DerToEcdsaKey([]byte(kd.PubKey))
for k, v := range keyMap {
decoded, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return fmt.Errorf("decoding base64: %w", err)
}

pubKey, err := x509.ParsePKIXPublicKey(decoded)
if err != nil {
return fmt.Errorf("converting public key to ecdsa: %w", err)
return fmt.Errorf("parsing PXIX public key: %w", err)
}

ecdsaPubKey, ok := pubKey.(*ecdsa.PublicKey)
if !ok {
return fmt.Errorf("public key is not ecdsa")
}

ses.uidPubKeyMap[kd.Uid] = pubKey
ses.uidPubKeyMap[k] = ecdsaPubKey
}

return nil
Expand Down
12 changes: 11 additions & 1 deletion pkg/osquery/table/launcher_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,18 @@ func generateLauncherInfoTable(store types.GetterSetter) table.GenerateFunc {
}

if hardwareKeyDer, err := x509.MarshalPKIXPublicKey(agent.HardwareKeys().Public()); err == nil {
// on non-darwin we'll only have 1 key for the entire machine, but we want to keep format consistent with darwin
// so just return a map with 0 as the uid
jsonBytes, err := json.Marshal(map[string]string{
"0": base64.StdEncoding.EncodeToString(hardwareKeyDer),
})

if err != nil {
return nil, fmt.Errorf("marshalling hardware keys: %w", err)
}

// der is a binary format, so convert to b64
results[0]["hardware_key"] = base64.StdEncoding.EncodeToString(hardwareKeyDer)
results[0]["hardware_key"] = string(jsonBytes)
results[0]["hardware_key_source"] = agent.HardwareKeys().Type()
}

Expand Down
Loading