Skip to content

Commit

Permalink
Merge pull request #15 from IABTechLab/tjm-UID2-2761-add-core-url-att…
Browse files Browse the repository at this point in the history
…estation-request

Add user data to the attestation reques
  • Loading branch information
thomasm-ttd authored Feb 16, 2024
2 parents d6abfaf + db2242f commit a068940
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
13 changes: 11 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>1.5.25-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>2.0.0-f968aec0e3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down Expand Up @@ -83,13 +83,22 @@
</dependency>
</dependencies>

<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>


<build>
<plugins>
<plugin>
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 a068940

Please sign in to comment.