Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# compiled commands
key-validator
5 changes: 2 additions & 3 deletions enclave/auction.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -58,7 +57,7 @@ func ProcessAuction(attester EnclaveAttester, req enclaveapi.EnclaveAuctionReque
winner := auctionResult.Winner
runnerUp := auctionResult.RunnerUp

teeData, attestationCOSE, err := GenerateTEEProofs(attester, req, unencryptedBids, winner, runnerUp)
teeData, coseAttestation, err := GenerateTEEProofs(attester, req, unencryptedBids, winner, runnerUp)
processingTime := time.Since(startTime).Milliseconds()

log.Printf("INFO: Auction complete: winner=%s (%.2f), runner-up=%s (%.2f), processing=%dms",
Expand All @@ -82,7 +81,7 @@ func ProcessAuction(attester EnclaveAttester, req enclaveapi.EnclaveAuctionReque
Success: true,
Message: fmt.Sprintf("Processed %d bids in enclave", len(req.Bids)),
AttestationDoc: teeData,
AttestationCOSEBase64: base64.StdEncoding.EncodeToString(attestationCOSE),
AttestationCOSEBase64: coseAttestation.EncodeBase64(),
ExcludedBids: excludedBids,
FloorRejectedBidIDs: floorRejectedBidIDs,
ProcessingTime: processingTime,
Expand Down
8 changes: 2 additions & 6 deletions enclave/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"

Expand Down Expand Up @@ -56,18 +55,15 @@ func HandleKeyRequest(attester EnclaveAttester, keyManager *KeyManager, tokenMan

auctionToken := tokenManager.GenerateToken()

keyAttestation, attestationCBOR, err := GenerateKeyAttestation(attester, keyManager.PublicKey, auctionToken)
keyAttestation, coseAttestation, err := GenerateKeyAttestation(attester, keyManager.PublicKey, auctionToken)
if err != nil {
return nil, fmt.Errorf("failed to generate key attestation: %w", err)
}

// Encode COSE bytes to base64 for JSON transport
attestationCOSEBase64 := base64.StdEncoding.EncodeToString(attestationCBOR)

return &enclaveapi.KeyResponse{
Type: "key_response",
PublicKey: publicKeyPEM,
KeyAttestation: keyAttestation,
AttestationCOSEBase64: attestationCOSEBase64,
AttestationCOSEBase64: coseAttestation.EncodeBase64(),
}, nil
}
10 changes: 5 additions & 5 deletions enclave/proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type EnclaveAttester interface {
Attest(options enclave.AttestationOptions) ([]byte, error)
}

func GenerateTEEProofs(attester EnclaveAttester, req enclaveapi.EnclaveAuctionRequest, unencryptedBids []core.CoreBid, winner, runnerUp *core.CoreBid) (*enclaveapi.AuctionAttestationDoc, []byte, error) {
func GenerateTEEProofs(attester EnclaveAttester, req enclaveapi.EnclaveAuctionRequest, unencryptedBids []core.CoreBid, winner, runnerUp *core.CoreBid) (*enclaveapi.AuctionAttestationDoc, enclaveapi.AttestationCOSE, error) {
bidHashNonce, err := generateNonce()
if err != nil {
return nil, nil, fmt.Errorf("failed to generate bid hash nonce: %w", err)
Expand Down Expand Up @@ -132,7 +132,7 @@ func GenerateAttestation(
adjustmentFactorsNonce string,
winner *core.CoreBid,
runnerUp *core.CoreBid,
) (*enclaveapi.AuctionAttestationDoc, []byte, error) {
) (*enclaveapi.AuctionAttestationDoc, enclaveapi.AttestationCOSE, error) {
now := time.Now()

// Create the user data that will be embedded in the attestation
Expand Down Expand Up @@ -183,7 +183,7 @@ func GenerateAttestation(
}

// Return both the parsed attestation and the raw COSE bytes
return attestationDoc, attestationCBOR, nil
return attestationDoc, enclaveapi.AttestationCOSE(attestationCBOR), nil
}

// Deprecated: can delete this once we've migrated to the new attestation format
Expand Down Expand Up @@ -259,7 +259,7 @@ func publicKeyToPEM(publicKey *rsa.PublicKey) (string, error) {

// GenerateKeyAttestation generates an attestation document for E2EE public keys
// Returns the parsed attestation and the raw COSE bytes
func GenerateKeyAttestation(attester EnclaveAttester, publicKey *rsa.PublicKey, auctionToken string) (*enclaveapi.KeyAttestationDoc, []byte, error) {
func GenerateKeyAttestation(attester EnclaveAttester, publicKey *rsa.PublicKey, auctionToken string) (*enclaveapi.KeyAttestationDoc, enclaveapi.AttestationCOSE, error) {
if attester == nil {
return nil, nil, fmt.Errorf("enclave attester is nil")
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func GenerateKeyAttestation(attester EnclaveAttester, publicKey *rsa.PublicKey,
}

// Return both the parsed attestation and the raw COSE bytes
return keyAttestation, attestationCBOR, nil
return keyAttestation, enclaveapi.AttestationCOSE(attestationCBOR), nil
}

// ParseKeyAttestation parses CBOR attestation specifically for key attestation
Expand Down
152 changes: 146 additions & 6 deletions enclaveapi/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package enclaveapi

import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
"time"

"github.com/cloudx-io/openauction/core"
Expand Down Expand Up @@ -144,19 +149,26 @@ type EnclaveAuctionResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
AttestationDoc *AuctionAttestationDoc `json:"attestation_document,omitempty"` // Deprecated: Use attestation_cose_base64
AttestationCOSEBase64 string `json:"attestation_cose_base64,omitempty"` // Base64-encoded COSE_Sign1 attestation
AttestationCOSEBase64 AttestationCOSEBase64 `json:"attestation_cose_base64,omitempty"` // Base64-encoded COSE_Sign1 attestation
ExcludedBids []core.ExcludedBid `json:"excluded_bids,omitempty"` // Decryption failures, validation errors
FloorRejectedBidIDs []string `json:"floor_rejected_bid_ids,omitempty"` // Bid IDs that were below floor
ProcessingTime int64 `json:"processing_time_ms"`
}

// KeyResponse represents the response from a key request to the TEE enclave
type KeyResponse struct {
Type string `json:"type"`
PublicKey string `json:"public_key"` // PEM format
TEEInstanceIP string `json:"tee_instance_ip,omitempty"` // Injected by HTTP bridge
KeyAttestation *KeyAttestationDoc `json:"key_attestation"` // Deprecated: Use attestation_cose_base64 instead
AttestationCOSEBase64 string `json:"attestation_cose_base64,omitempty"` // Base64-encoded COSE_Sign1 attestation
Type string `json:"type"`
PublicKey string `json:"public_key"` // PEM format
TEEInstanceIP string `json:"tee_instance_ip,omitempty"` // Injected by HTTP bridge
KeyAttestation *KeyAttestationDoc `json:"key_attestation"` // Deprecated: Use attestation_cose_base64 instead
AttestationCOSEBase64 AttestationCOSEBase64 `json:"attestation_cose_base64,omitempty"` // Base64-encoded COSE_Sign1 attestation
}

// KeyWithAttestation represents a public key with its TEE attestation
// Used in bid requests and key validation tools
type KeyWithAttestation struct {
PublicKey string `json:"public_key"` // PEM-encoded RSA public key
Attestation AttestationCOSEGzip `json:"attestation_cose_gzip_base64,omitempty"` // Gzipped and base64-encoded COSE_Sign1 attestation
}

// KeyAttestationUserData represents the key-specific data embedded in key attestation
Expand All @@ -165,3 +177,131 @@ type KeyAttestationUserData struct {
PublicKey string `json:"public_key"` // PEM-encoded public key
AuctionToken string `json:"auction_token"` // Single-use token for bid replay protection
}

// AttestationCOSE represents raw COSE_Sign1 bytes from AWS Nitro Enclaves
type AttestationCOSE []byte

// EncodeBase64 converts COSE bytes to standard base64 string
func (a AttestationCOSE) EncodeBase64() AttestationCOSEBase64 {
return AttestationCOSEBase64(base64.StdEncoding.EncodeToString(a))
}

// EncodeURLSafe converts COSE bytes to URL-safe base64 string (no padding)
func (a AttestationCOSE) EncodeURLSafe() AttestationCOSEURLBase64 {
encoded := base64.URLEncoding.EncodeToString(a)
return AttestationCOSEURLBase64(removeBase64Padding(encoded))
}

// CompressGzip compresses COSE bytes with GZIP and encodes as base64url (no padding)
// Used for URL-safe transmission in win/loss notifications and bid responses
func (a AttestationCOSE) CompressGzip() (AttestationCOSEGzip, error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For things that can fail, best to return (*Type, error) instead of (Type, error)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The five functions that return errors in enclaveapi/types.go all return AttestationCOSE which is a reference type (to []byte) so there shouldn't be a need for *.

var buf bytes.Buffer
w, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
if err != nil {
return "", fmt.Errorf("create gzip writer: %w", err)
}
if _, err := w.Write(a); err != nil {
return "", fmt.Errorf("gzip write: %w", err)
}
if err := w.Close(); err != nil {
return "", fmt.Errorf("gzip close: %w", err)
}

// Encode to URL-safe base64 without padding
encoded := base64.URLEncoding.EncodeToString(buf.Bytes())
return AttestationCOSEGzip(removeBase64Padding(encoded)), nil
}

// AttestationCOSEBase64 represents standard base64-encoded COSE bytes
type AttestationCOSEBase64 string

// Decode converts base64 string to raw COSE bytes
func (a AttestationCOSEBase64) Decode() (AttestationCOSE, error) {
data, err := base64.StdEncoding.DecodeString(string(a))
if err != nil {
return nil, fmt.Errorf("decode COSE base64: %w", err)
}
return AttestationCOSE(data), nil
}

// String returns the underlying string value for JSON marshaling
func (a AttestationCOSEBase64) String() string {
return string(a)
}

// CompressGzip decodes, compresses with GZIP, and encodes as base64url (no padding)
// Convenience method that combines Decode() and CompressGzip()
func (a AttestationCOSEBase64) CompressGzip() (AttestationCOSEGzip, error) {
cose, err := a.Decode()
if err != nil {
return "", err
}
return cose.CompressGzip()
}

// AttestationCOSEURLBase64 represents URL-safe base64-encoded COSE bytes (no padding)
type AttestationCOSEURLBase64 string

// addBase64Padding adds padding to a base64 string if needed
func addBase64Padding(str string) string {
if padding := len(str) % 4; padding > 0 {
str += strings.Repeat("=", 4-padding)
}
return str
}

// removeBase64Padding removes padding from a base64 string for URL safety
func removeBase64Padding(str string) string {
return strings.TrimRight(str, "=")
}

// Decode converts URL-safe base64 string to raw COSE bytes
func (a AttestationCOSEURLBase64) Decode() (AttestationCOSE, error) {
str := addBase64Padding(string(a))
data, err := base64.URLEncoding.DecodeString(str)
if err != nil {
return nil, fmt.Errorf("decode COSE URL base64: %w", err)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: padding and decoding is the same as in func Decompress only error message is different, I wonder if it makes sense to create small function for that and avoid repeated code. We can leave TODO and make decision on this later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Great suggestion. Added helper functions for both adding and removing padding: 8e1398c

return AttestationCOSE(data), nil
}

// String returns the underlying string value
func (a AttestationCOSEURLBase64) String() string {
return string(a)
}

// AttestationCOSEGzip represents GZIP-compressed, base64url-encoded (no padding) COSE bytes
// optimized for URL inclusion. Common format for win/loss notifications and bid responses.
// Encoding pipeline: COSE bytes → GZIP compress → base64url encode → trim padding
type AttestationCOSEGzip string

// String returns the underlying string value for JSON marshaling
func (a AttestationCOSEGzip) String() string {
return string(a)
}

// Decompress decompresses and returns raw COSE bytes
func (a AttestationCOSEGzip) Decompress() (AttestationCOSE, error) {
// Add padding if needed for base64url decoding
str := addBase64Padding(string(a))

// Decode base64url
gzipData, err := base64.URLEncoding.DecodeString(str)
if err != nil {
return nil, fmt.Errorf("decode base64url: %w", err)
}

// GZIP decompress
reader, err := gzip.NewReader(bytes.NewReader(gzipData))
if err != nil {
return nil, fmt.Errorf("create gzip reader: %w", err)
}
defer func() { _ = reader.Close() }()

coseBytes, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("decompress: %w", err)
}

return AttestationCOSE(coseBytes), nil
}
Loading