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

Adding example C client application for Veraison challenge response exchange. #2

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

Conversation

tusharkhandelwal2
Copy link
Collaborator

This application uses libcurl for making REST api calls to the veraison services.

Fixes #1

Signed-off-by: Tushar Khandelwal [email protected]

…xchange

This application uses libcurl for making REST api calls to the veraison services.
Fixes #1

Signed-off-by: Tushar Khandelwal <[email protected]>
Copy link
Contributor

@thomas-fossati thomas-fossati left a comment

Choose a reason for hiding this comment

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

Thanks Tushar! This is a great start.
Could we move this to an example/ folder?
We could then (in another PR) work on factoring out a C interface that could be linked from other C/C++ code.

Signed-off-by: Tushar Khandelwal <[email protected]>
Copy link
Contributor

@thomas-fossati thomas-fossati left a comment

Choose a reason for hiding this comment

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

some comments inlined

}
}

sprintf(uri,"http://%s:%s/challenge-response/v1/",address,port);
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to use snprintf here otherwise, depending on how big address and port are, uri could overflow

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


char *new_session="newSession?nonceSize=32";
char url[100];
sprintf(url,"%s%s",uri,new_session);
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto here for snprintf

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


/* Initialize the curl library */
curl_handle = curl_easy_init();
if(curl_handle) {
Copy link
Contributor

Choose a reason for hiding this comment

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

As the elders say: "flat is better than nested" :-)

Do an early return here:

if (!curl_handle) {
   // error path
}

// happy path

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

if(res != CURLE_OK) {
printf("\nerror: %s\n", curl_easy_strerror(res));
return res;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this else is not really needed and can be removed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Comment on lines 91 to 96
if(fp!=NULL) {
num = fread(token, 1 ,sizeof(token),fp);
} else {
printf("\ntoken file not found\n");
return -1;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

unneeded nesting, you can do:

if (fp == NULL) {
  return -1
}
num = fread(...)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

headers = curl_slist_append(headers, "Content-Type: application/psa-attestation-token");
curl_slist_append(headers, "Host: veraison.example");
curl_slist_append(headers, "Accept: application/vnd.veraison.challenge-response-session+json");
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure whether it's required (or the subsequent perform will tell us that in any case) but I'd have thought that the return code from curl_easy_setopt should be checked?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers);
FILE *fp=fopen(filename,"rb");
int num;
if(fp!=NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the dual fclose seems to be missing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Signed-off-by: Tushar Khandelwal <[email protected]>
Copy link
Contributor

@thomas-fossati thomas-fossati left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my comments.

I have a number of notes on resource handling but, at this stage, they are not blocking because once the example code exits the OS will take care of it.

We'll tackle that in a subsequent PR, when we expose the interface.

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.

Add an example C programme that implements the Veraison API for Verification
2 participants