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

COCOS-192 - Attested TLS #279

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

danko-miladinovic
Copy link
Contributor

What type of PR is this?

This feature is an enhancement for attested TLS. The idea is to have the attestation fetched and forwarded as a TLS extension. This way, we can guarantee the freshness of the evidence.

What does this do?

Adds custom TLS extensions that transfer the nonce and the attestation report between the client (CLI) and server (Agent).

Which issue(s) does this PR fix/relate to?

Have you included tests for your changes?

No, the process is tested as before. There is no need for new tests.

Did you document any new/modified feature?

No, the documentation will be added upon completion.

Notes

This PR is still a draft.

if err != nil {
return fmt.Errorf("failed to listen on port %s: %w", s.Address, err)
}
// listener, err := net.Listen("tcp", s.Address)
Copy link
Contributor

Choose a reason for hiding this comment

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

Dead code?

@@ -17,7 +17,7 @@ import (
)

const (
ManagerVsockPort = 9997
ManagerVsockPort = 9998 // CHANGED BY ME
Copy link
Contributor

Choose a reason for hiding this comment

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

Take care about these comments

#define CLIENT_RANDOM_SIZE 32
#define TLS_CLIENT_CTX 0
#define TLS_SERVER_CTX 1
#define SSL_ERROR_WANT_READ_WRITE 2
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#define SSL_ERROR_WANT_READ_WRITE 2
#define SSL_ERROR_WANT_WRITE 2
#define SSL_ERROR_WANT_READ 2

#define TLS_SERVER_CTX 1
#define SSL_ERROR_WANT_READ_WRITE 2

typedef struct tls_server_c
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
typedef struct tls_server_c
typedef struct tls_server_connection

struct sockaddr_storage addr;
} tls_server_connection;

typedef struct tls_c
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
typedef struct tls_c
typedef struct tls_connection

const unsigned char *out,
void *add_arg)
{
// fprintf(stderr, "evidence_request_ext_free_cb called\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// fprintf(stderr, "evidence_request_ext_free_cb called\n");
free((void *)out);
// fprintf(stderr, "evidence_request_ext_free_cb called\n");

const unsigned char *out,
void *add_arg)
{
// fprintf(stderr, "attestation_certificate_ext_free_cb from server called\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// fprintf(stderr, "attestation_certificate_ext_free_cb from server called\n");
free((void *)out);
// fprintf(stderr, "attestation_certificate_ext_free_cb from server called\n");

{
unsigned char* client_random_buffer = malloc(CLIENT_RANDOM_SIZE);
unsigned char* client_random_print_buffer = malloc(CLIENT_RANDOM_SIZE * 2 + 1);

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (client_random_buffer == NULL || client_random_print_buffer == NULL) {
free(client_random_buffer);
free(client_random_print_buffer);
*al = SSL_AD_INTERNAL_ERROR;
return 0;
}

{
unsigned char *hash = malloc(SHA256_DIGEST_LENGTH);
// fprintf(stderr, "attestation_certificate_ext_add_cb called\n");

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (hash == NULL) {
*al = SSL_AD_INTERNAL_ERROR;
return 0;
}

/*
Attestation Certificate extension
- Contains the attestation report
- The attestation report contains the hash of the nonce and the Publik Key of the x.509 Agent certificate
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- The attestation report contains the hash of the nonce and the Publik Key of the x.509 Agent certificate
- The attestation report contains the hash of the nonce and the Public Key of the x.509 Agent certificate

sprint_string_hex(hex_buffer, hash, 32);
// fprintf(stderr, "Receiving sha256 of public key from server: %s\n", hex_buffer);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
free(hex_buffer);

void *parse_arg)
{
char* hex_buffer = malloc(inlen*2 + 1);

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (hex_buffer == NULL) {
*al = SSL_AD_INTERNAL_ERROR;
return 0;
}


sec, usec := timeToTimeout(t)
if C.set_socket_timeout(c.tlsConn, C.int(sec), C.int(usec)) < 0 {
return fmt.Errorf("could not set deadline")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("could not set deadline")
return fmt.Errorf("could not set deadline, socket timeout failed")

func (c *CustomTLSConn) SetReadDeadline(t time.Time) error {
fmt.Fprintf(os.Stderr, "SetReadDeadline - SetDeadline\n")
if c.SetDeadline(t) != nil {
return fmt.Errorf("could not set write deadline")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("could not set write deadline")
return fmt.Errorf("could not set read deadline")

return 0, 0
}

d := time.Until(t)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
d := time.Until(t)
d := time.Until(t)
if d<=0 {
return 0,0
}

Comment on lines 333 to 334
free(conn);
close(conn->socket_fd);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
free(conn);
close(conn->socket_fd);
close(conn->socket_fd);
free(conn);

Comment on lines 275 to 276
free(conn);
SSL_free(conn->ssl);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
free(conn);
SSL_free(conn->ssl);
SSL_free(conn->ssl);
free(conn);

@dborovcanin dborovcanin marked this pull request as ready for review October 23, 2024 07:30
return nil
}

func FetchAttestation(reportDataSlice []byte) []byte {
Copy link
Contributor

Choose a reason for hiding this comment

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

propagate error

func VerifyAttestationReportTLS(attestationBytes []byte, reportData []byte) int {
logger.Init("", false, false, io.Discard)

AttConfigurationSEVSNP.SNPPolicy.ReportData = reportData[:]
Copy link
Contributor

Choose a reason for hiding this comment

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

SNPPolicy could be nil, this could panic

Copy link
Contributor

Choose a reason for hiding this comment

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

try combining with exisiting code in cli, to avoid repetition

RootOfTrust *check.RootOfTrust `json:"root_of_trust,omitempty"`
}

func VerifyAttestationReportTLS(attestationBytes []byte, reportData []byte) int {
Copy link
Contributor

Choose a reason for hiding this comment

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

the idiomatic golang way is to return an error

)

var (
AttConfigurationSEVSNP = AttestationConfiguration{}
Copy link
Contributor

Choose a reason for hiding this comment

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

avoid package level variable, this is not thread safe

defer c.fdReadMutex.Unlock()

if c.tlsConn == nil {
return 0, fmt.Errorf("tls connection is NULL")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return 0, fmt.Errorf("tls connection is NULL")
return 0, fmt.Errorf("tls connection is nil")

also declare errors using vars

func (l *ATLSServerListener) Accept() (net.Conn, error) {
conn := C.tls_server_accept(l.tlsListener)
if conn == nil {
return &ATLSConn{tlsConn: nil}, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

if conn is nil, return an error

defer c.fdWriteMutex.Unlock()

if c.tlsConn == nil {
return 0, fmt.Errorf("tls connection is NULL")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return 0, fmt.Errorf("tls connection is NULL")
return 0, fmt.Errorf("tls connection is nil")

}

if (ext_data != NULL) {
if (RAND_bytes(ext_data->er.data, CLIENT_RANDOM_SIZE) < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

probably != 1 rather than <0

return 0;
}

attestation_report = triggerFetchAttestationCallback(ext_data->fetch_attestation_handler, hash);
Copy link
Contributor

Choose a reason for hiding this comment

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

probably requires some nil check before assignment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Enhance attested TLS
3 participants