Skip to content

Commit

Permalink
Adding url to attestation data
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasm-ttd committed Feb 15, 2024
1 parent d6abfaf commit 11e7e6d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.uid2</groupId>
<artifactId>attestation-azure</artifactId>
<version>1.5.16-d43239058c</version>
<version>2.0.0-SNAPSHOT</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>Azure Enclave attestation</description>
Expand Down Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>com.uid2</groupId>
<artifactId>uid2-attestation-api</artifactId>
<version>1.5.0-676519b018</version>
<version>1.6.10-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ public AzureCCAttestationProvider(String maaServerBaseUrl, String skrUrl, HttpCl
}

@Override
public byte[] getAttestationRequest(byte[] publicKey) throws AttestationException {
public byte[] getAttestationRequest(byte[] publicKey, byte[] userData) throws AttestationException {
var base64Encoder = Base64.getEncoder();
var gson = new Gson();

var runtimeData = new RuntimeData();
runtimeData.location = this.location;
runtimeData.publicKey = base64Encoder.encodeToString(publicKey);
runtimeData.userData = base64Encoder.encodeToString(userData);
String runtimeDataJson = gson.toJson(runtimeData);

var skrRequest = new SkrRequest();
Expand Down Expand Up @@ -130,6 +131,7 @@ private String getLocation() {
private static class RuntimeData {
private String location;
private String publicKey;
private String userData;
}

private static class SkrRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import java.util.Map;

public class AzureCCAttestationProviderTest {
final private byte[] publicTokenMock = new byte[] {0x01, 0x02};
final private byte[] userDataMock = new byte[] {0x03, 0x04};

@Test
public void testGetAttestationRequestSuccess() throws Exception {
var gson = new Gson();

// Mock response
final var publicTokenMock = new byte[] {0x01, 0x02};
final var skrUrlMock = "http://skr";
final var maaTokenMock = "abc";
final var httpResponseMock = mock(HttpResponse.class);
Expand All @@ -35,7 +37,7 @@ public void testGetAttestationRequestSuccess() throws Exception {

// Verify output
final var provider = new AzureCCAttestationProvider(null, skrUrlMock, httpClientMock);
var output = provider.getAttestationRequest(publicTokenMock);
var output = provider.getAttestationRequest(publicTokenMock, userDataMock);
Assert.assertArrayEquals(maaTokenMock.getBytes(), output);

// Verify sent request
Expand All @@ -47,36 +49,33 @@ public void testGetAttestationRequestSuccess() throws Exception {

@Test
public void testGetAttestationRequestFailure_InvalidStatusCode() throws Exception {
final var publicTokenMock = new byte[] {0x01, 0x02};
final var httpResponseMock = mock(HttpResponse.class);
when(httpResponseMock.statusCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);

final var httpClientMock = mock(HttpClient.class);
when(httpClientMock.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(httpResponseMock);

final var provider = new AzureCCAttestationProvider(null, null, httpClientMock);
var thrown = Assert.assertThrows(AttestationException.class, () -> provider.getAttestationRequest(publicTokenMock));
var thrown = Assert.assertThrows(AttestationException.class, () -> provider.getAttestationRequest(publicTokenMock, userDataMock));
Assert.assertTrue(thrown.getMessage().startsWith("Skr failed with status code: " + HttpURLConnection.HTTP_INTERNAL_ERROR));
}

@Test
public void testGetAttestationRequestFailure_EmptyResponseBody() throws Exception {
final var publicTokenMock = new byte[] {0x01, 0x02};
final var httpResponseMock = mock(HttpResponse.class);
when(httpResponseMock.statusCode()).thenReturn(HttpURLConnection.HTTP_OK);

final var httpClientMock = mock(HttpClient.class);
when(httpClientMock.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(httpResponseMock);

final var provider = new AzureCCAttestationProvider(null, null, httpClientMock);
var thrown = Assert.assertThrows(AttestationException.class, () -> provider.getAttestationRequest(publicTokenMock));
var thrown = Assert.assertThrows(AttestationException.class, () -> provider.getAttestationRequest(publicTokenMock, userDataMock));
Assert.assertEquals("response is null", thrown.getMessage());
}

@Test
public void testGetAttestationRequestFailure_InvalidResponseBody() throws Exception {
var gson = new Gson();
final var publicTokenMock = new byte[] {0x01, 0x02};
final var httpResponseMock = mock(HttpResponse.class);
when(httpResponseMock.statusCode()).thenReturn(HttpURLConnection.HTTP_OK);
when(httpResponseMock.body()).thenReturn(gson.toJson(Map.of("key", 123)));
Expand All @@ -85,7 +84,7 @@ public void testGetAttestationRequestFailure_InvalidResponseBody() throws Except
when(httpClientMock.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(httpResponseMock);

final var provider = new AzureCCAttestationProvider(null, null, httpClientMock);
var thrown = Assert.assertThrows(AttestationException.class, () -> provider.getAttestationRequest(publicTokenMock));
var thrown = Assert.assertThrows(AttestationException.class, () -> provider.getAttestationRequest(publicTokenMock, userDataMock));
Assert.assertEquals("token field not exist in Skr response", thrown.getMessage());
}
}

0 comments on commit 11e7e6d

Please sign in to comment.