Repository navigation
use type definitions to represent different attestation encodings #13
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
Changes from all commits
f3f3723
b49568d
99c0ab9
170ebfa
8e1398c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # compiled commands | ||
| key-validator |
| 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" | ||
|
|
@@ -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 | ||
|
|
@@ -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) { | ||
| 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) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| } | ||
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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.goall returnAttestationCOSEwhich is a reference type (to[]byte) so there shouldn't be a need for*.