Files
cocos/pkg/attestation/eat/cbor_encoder.go
T
Sammy Kerata Oina de50b6d2d4 COCOS-560 - EAT (#561)
* feat: Implement EAT (Evidence Attestation Token) generation and verification for attestation responses, replacing raw quotes with EAT tokens in the attestation service and protobuf.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* style: standardize comment formatting and fix a debug log format specifier.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* fix pkg test

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Introduce named constants for OEM IDs and use them in attestation claim extraction.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* feat: Implement and test minimum length validation for EAT nonce in `NewEATClaims`.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* feat: Add EATClaims.Sanitize method and integrate it into the validator to enforce claim dependencies.

Signed-off-by: SammyOina <sammyoina@gmail.com>

* feat: Add Signature field to SNPExtensions and TDXExtensions for enhanced claim validation

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Update dependencies and improve code structure in attestation package

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Introduce comprehensive test suites for EAT, ATLS, TDX, Azure SNP, and vTPM attestation, and improve EAT decoder robustness.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Add encryption and admin keys, an encrypted algorithm file, and update go.mod to use go-jose/v4.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: add new encryption and KBS admin keys while improving TDX attestation test error handling.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Add new KBS admin and encryption keys, an encrypted linear regression algorithm, and refactor TDX test error message checks.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Implement Azure SNP attestation policy, update certificate verification, and add key management.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* refactor: replace hardcoded string literals with variables in Azure SNP attestation tests.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

* feat: Refactor TDX EAT claims to use individual RTMR fields with `tdx_` prefixes and add an `IntUse` field.

Signed-off-by: Sammy Oina <sammyoina@gmail.com>

---------

Signed-off-by: Sammy Oina <sammyoina@gmail.com>
Signed-off-by: SammyOina <sammyoina@gmail.com>
2026-02-11 16:16:35 +01:00

75 lines
2.0 KiB
Go

// Copyright (c) Ultraviolet
// SPDX-License-Identifier: Apache-2.0
package eat
import (
"crypto/ecdsa"
"crypto/rand"
"fmt"
"time"
"github.com/fxamacker/cbor/v2"
"github.com/veraison/go-cose"
)
// CBOREncoder encodes EAT claims to CBOR format (CWT - CBOR Web Token).
type CBOREncoder struct {
signingKey *ecdsa.PrivateKey
issuer string
}
// NewCBOREncoder creates a new CBOR encoder.
func NewCBOREncoder(signingKey *ecdsa.PrivateKey, issuer string) *CBOREncoder {
return &CBOREncoder{
signingKey: signingKey,
issuer: issuer,
}
}
// Encode encodes EAT claims to CBOR bytes with COSE_Sign1 signature.
func (e *CBOREncoder) Encode(claims *EATClaims) ([]byte, error) {
// Set standard CWT claims
now := time.Now()
claims.Issuer = e.issuer
claims.IssuedAt = now.Unix()
claims.ExpiresAt = now.Add(5 * time.Minute).Unix() // 5 minute validity
// Encode claims to CBOR (this will be the payload)
payload, err := cbor.Marshal(claims)
if err != nil {
return nil, fmt.Errorf("failed to encode CBOR payload: %w", err)
}
// Create COSE Sign1 message
msg := cose.NewSign1Message()
msg.Payload = payload
msg.Headers.Protected.SetAlgorithm(cose.AlgorithmES256)
msg.Headers.Unprotected[cose.HeaderLabelKeyID] = []byte(e.issuer)
// Create signer from ECDSA private key
signer, err := cose.NewSigner(cose.AlgorithmES256, e.signingKey)
if err != nil {
return nil, fmt.Errorf("failed to create COSE signer: %w", err)
}
// Sign the message
if err := msg.Sign(rand.Reader, nil, signer); err != nil {
return nil, fmt.Errorf("failed to sign COSE message: %w", err)
}
// Encode the signed message to CBOR
signed, err := msg.MarshalCBOR()
if err != nil {
return nil, fmt.Errorf("failed to marshal COSE_Sign1: %w", err)
}
return signed, nil
}
// EncodeToCBOR is a convenience function to encode EAT claims to CBOR.
func EncodeToCBOR(claims *EATClaims, signingKey *ecdsa.PrivateKey, issuer string) ([]byte, error) {
encoder := NewCBOREncoder(signingKey, issuer)
return encoder.Encode(claims)
}