Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uid2.shared.secure;

import com.uid2.shared.secure.gcpoidc.Environment;
import com.uid2.shared.secure.nitro.AttestationDocument;
import com.uid2.shared.secure.nitro.AttestationRequest;

Expand All @@ -22,13 +23,19 @@ public class NitroCoreAttestationService implements ICoreAttestationService {
private final String attestationUrl;
private final Set<NitroEnclaveIdentifier> allowedEnclaveIds;
private final ICertificateProvider certificateProvider;
private final Environment coreEnvironment;

private static final Logger LOGGER = LoggerFactory.getLogger(NitroCoreAttestationService.class);

public NitroCoreAttestationService(ICertificateProvider certificateProvider, String attestationUrl) {
/**
* @param coreEnvironment Core's own environment. Debug-mode enclaves are rejected only when this is
* {@link Environment#Production}; null (not configured) accepts them.
*/
public NitroCoreAttestationService(ICertificateProvider certificateProvider, String attestationUrl, Environment coreEnvironment) {
this.attestationUrl = attestationUrl;
this.allowedEnclaveIds = new HashSet<>();
this.certificateProvider = certificateProvider;
this.coreEnvironment = coreEnvironment;
}

@Override
Expand Down Expand Up @@ -64,7 +71,13 @@ private AttestationResult attestInternal(byte[] publicKey, AttestationRequest aR
}
}

NitroEnclaveIdentifier id = NitroEnclaveIdentifier.fromRaw(aDoc.getPcr(0));
// Nitro zeroes all PCRs for enclaves started with --debug-mode
byte[] pcr0 = aDoc.getPcr(0);
if (coreEnvironment == Environment.Production && isAllZero(pcr0)) {
return new AttestationResult(new AttestationClientException("Debug-mode enclave is not allowed in production", AttestationFailure.BAD_FORMAT));
}

NitroEnclaveIdentifier id = NitroEnclaveIdentifier.fromRaw(pcr0);
if (!allowedEnclaveIds.contains(id)) {
return new AttestationResult(AttestationFailure.FORBIDDEN_ENCLAVE);
}
Expand All @@ -73,6 +86,18 @@ private AttestationResult attestInternal(byte[] publicKey, AttestationRequest aR
return new AttestationResult(aDoc.getPublicKey(), id.toString());
}

private static boolean isAllZero(byte[] bytes) {
if (bytes == null) {
return false;
}
for (byte b : bytes) {
if (b != 0) {
return false;
}
}
return true;
}

@Override
public void registerEnclave(String encodedIdentifier) throws AttestationException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import co.nstant.in.cbor.model.NegativeInteger;
import co.nstant.in.cbor.model.UnicodeString;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.uid2.shared.secure.gcpoidc.Environment;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.x500.X500Name;
Expand Down Expand Up @@ -38,6 +39,7 @@

public class NitroCoreAttestationServiceTest {
private static final String ATTESTATION_URL = "https://core.example.com";
// All zeros, as Nitro reports for a debug-mode enclave
private static final byte[] PCR0 = new byte[48];
private static final AtomicLong SERIAL = new AtomicLong(1);

Expand Down Expand Up @@ -131,12 +133,27 @@ void rejectsDocumentWithWrongFieldType() throws Exception {
assertEquals(AttestationFailure.BAD_FORMAT, attest(signDocument(doc, leafKey)).getFailure());
}

@Test
void rejectsRegisteredDebugEnclaveOnlyInProd() throws Exception {
KeyPair leafKey = newKeyPair();
X509Certificate leaf = issue("CN=test-leaf", leafKey, "CN=test-intermediate", intermediateKey, false, notExpired());
byte[] request = signDocument(document(leaf, List.of(root, intermediate)), leafKey);

assertEquals(AttestationFailure.BAD_FORMAT, attest(request, Environment.Production).getFailure());
assertTrue(attest(request, Environment.Integration).isSuccess());
assertTrue(attest(request, null).isSuccess());
}

private static AttestationResult attest(X509Certificate leaf, KeyPair leafKey, List<X509Certificate> cabundle) throws Exception {
return attest(signDocument(document(leaf, cabundle), leafKey));
}

private static AttestationResult attest(byte[] request) {
NitroCoreAttestationService service = new NitroCoreAttestationService(() -> root, ATTESTATION_URL);
return attest(request, null);
}

private static AttestationResult attest(byte[] request, Environment coreEnvironment) {
NitroCoreAttestationService service = new NitroCoreAttestationService(() -> root, ATTESTATION_URL, coreEnvironment);
service.addIdentifier(NitroEnclaveIdentifier.fromRaw(PCR0));

AtomicReference<AttestationResult> result = new AtomicReference<>();
Expand Down