-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into lun-UID2-1426-gcp-oidc-attestation-client
- Loading branch information
Showing
19 changed files
with
129 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
package com.uid2.shared.auth; | ||
|
||
import com.uid2.shared.model.KeysetKey; | ||
import com.uid2.shared.store.IKeysetSnapshot; | ||
import com.uid2.shared.store.ACLMode.MissingAclMode; | ||
|
||
import java.util.Map; | ||
|
||
public class KeysetSnapshot implements IKeysetSnapshot { | ||
public class KeysetSnapshot { | ||
private final Map<Integer, Keyset> keysets; | ||
|
||
public KeysetSnapshot(Map<Integer, Keyset> keysets) { this.keysets = keysets; } | ||
@Override | ||
public boolean canClientAccessKey(ClientKey clientKey, KeysetKey key) { | ||
|
||
public boolean canClientAccessKey(ClientKey clientKey, KeysetKey key, MissingAclMode accessMethod) { | ||
Keyset keyset = keysets.get(key.getKeysetId()); | ||
|
||
if(!keyset.isEnabled()) return false; | ||
if (keyset == null || !keyset.isEnabled() || clientKey == null) return false; | ||
|
||
if (keyset.getSiteId() == clientKey.getSiteId()) return true; | ||
|
||
if(keyset.getSiteId() == clientKey.getSiteId()) return true; | ||
if (accessMethod == MissingAclMode.ALLOW_ALL | ||
&& keyset.getAllowedSites() == null) return true; | ||
|
||
return keyset.canBeAccessedBySite(clientKey.getSiteId()); | ||
} | ||
|
||
public Map<Integer, Keyset> getAllKeysets() { return keysets; } | ||
public Map<Integer, Keyset> getAllKeysets() { return this.keysets; } | ||
|
||
public Keyset getKeyset(int keysetId) { return this.keysets.get(keysetId); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
package com.uid2.shared.secure.gcpoidc; | ||
|
||
import com.google.api.client.json.gson.GsonFactory; | ||
import com.google.api.client.json.webtoken.JsonWebSignature; | ||
import com.google.api.client.util.Clock; | ||
import com.google.auth.oauth2.TokenVerifier; | ||
import com.google.common.base.Strings; | ||
import com.uid2.shared.secure.AttestationException; | ||
|
||
import java.io.IOException; | ||
import java.security.PublicKey; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class TokenSignatureValidator implements ITokenSignatureValidator{ | ||
public class TokenSignatureValidator implements ITokenSignatureValidator { | ||
private static final String PUBLIC_CERT_LOCATION = | ||
"https://www.googleapis.com/service_accounts/v1/metadata/jwk/[email protected]"; | ||
|
||
|
@@ -20,19 +22,22 @@ public class TokenSignatureValidator implements ITokenSignatureValidator{ | |
private static final String ISSUER = "https://confidentialcomputing.googleapis.com"; | ||
private final TokenVerifier tokenVerifier; | ||
|
||
public TokenSignatureValidator(){ | ||
// set to true to facilitate local test with self-signed cert. | ||
public static final boolean BYPASS_SIGNATURE_CHECK = false; | ||
|
||
public TokenSignatureValidator() { | ||
this(null, null); | ||
} | ||
|
||
protected TokenSignatureValidator(PublicKey publicKeyOverride, Clock clockOverride){ | ||
protected TokenSignatureValidator(PublicKey publicKeyOverride, Clock clockOverride) { | ||
var verifierBuilder = TokenVerifier.newBuilder(); | ||
verifierBuilder.setCertificatesLocation(PUBLIC_CERT_LOCATION); | ||
|
||
if(publicKeyOverride != null){ | ||
if (publicKeyOverride != null) { | ||
verifierBuilder.setPublicKey(publicKeyOverride); | ||
} | ||
|
||
if(clockOverride != null){ | ||
if (clockOverride != null) { | ||
verifierBuilder.setClock(clockOverride); | ||
} | ||
|
||
|
@@ -50,11 +55,16 @@ public TokenPayload validate(String tokenString) throws AttestationException { | |
|
||
// Validate Signature | ||
JsonWebSignature signature; | ||
try{ | ||
signature = tokenVerifier.verify(tokenString); | ||
} | ||
catch (TokenVerifier.VerificationException e){ | ||
try { | ||
if (BYPASS_SIGNATURE_CHECK) { | ||
signature = JsonWebSignature.parse(GsonFactory.getDefaultInstance(), tokenString); | ||
} else { | ||
signature = tokenVerifier.verify(tokenString); | ||
} | ||
} catch (TokenVerifier.VerificationException e) { | ||
throw new AttestationException("Fail to validate the token signature, error: " + e.getMessage()); | ||
} catch (IOException e) { | ||
throw new AttestationException("Fail to parse token, error: " + e.getMessage()); | ||
} | ||
|
||
// Parse Payload | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,32 @@ | ||
package com.uid2.shared.encryption; | ||
|
||
import com.uid2.shared.model.EncryptedPayload; | ||
import com.uid2.shared.model.EncryptionKey; | ||
import com.uid2.shared.model.KeysetKey; | ||
import junit.framework.TestCase; | ||
import org.junit.Assert; | ||
|
||
import javax.crypto.SecretKey; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Instant; | ||
|
||
public class AesCbcTest extends TestCase { | ||
|
||
public void testEncryptionDecryption() throws Exception { | ||
|
||
final EncryptionKey key = new EncryptionKey(1, Random.getRandomKeyBytes(), Instant.now(), Instant.now(), Instant.now(), -1); | ||
final KeysetKey key = new KeysetKey(1, Random.getRandomKeyBytes(), Instant.now(), Instant.now(), Instant.now(), 123); | ||
final String testString = "[email protected]"; | ||
|
||
final EncryptedPayload payload = AesCbc.encrypt(testString, key); | ||
final byte[] decrypted = AesCbc.decrypt(payload.getPayload(), key); | ||
|
||
final String decryptedString = new String(decrypted, "UTF-8"); | ||
final String decryptedString = new String(decrypted, StandardCharsets.UTF_8); | ||
Assert.assertEquals(testString, decryptedString); | ||
} | ||
|
||
public void testDecryption() { | ||
byte[] keyBytes = new byte[]{-82, -66, -67, -114, 87, 24, -108, 82, -77, 112, 9, 80, 118, 39, 66, -35, 59, -81, -72, -81, 30, -41, 113, 101, -76, 79, 119, -73, 59, -39, 0, 75}; | ||
byte[] encypted = new byte[]{34, -42, -99, 68, 110, 49, 45, 57, 11, 64, 74, -43, 86, -73, 33, -125, -100, 2, -27, 38, 103, 97, -17, -115, -116, 10, 102, -41, -35, -53, 34, 60, -44, 59, 101, 24, -14, 9, -56, -71, 86, -31, -44, -75, -124, -77, 58, -20, 3, 26, -39, 95, 100, 24, -110, 100, 34, 25, -4, 41, -93, -3, -83, -44, 91, -1, 34, 25, 83, -58, 42, -116, 51, -112, 91, 71, 8, -25, 26, 41}; | ||
final EncryptionKey key = new EncryptionKey(1, keyBytes, Instant.now(), Instant.now(), Instant.now(), -1); | ||
byte[] payload = AesCbc.decrypt(encypted, key); | ||
byte[] encrypted = new byte[]{34, -42, -99, 68, 110, 49, 45, 57, 11, 64, 74, -43, 86, -73, 33, -125, -100, 2, -27, 38, 103, 97, -17, -115, -116, 10, 102, -41, -35, -53, 34, 60, -44, 59, 101, 24, -14, 9, -56, -71, 86, -31, -44, -75, -124, -77, 58, -20, 3, 26, -39, 95, 100, 24, -110, 100, 34, 25, -4, 41, -93, -3, -83, -44, 91, -1, 34, 25, 83, -58, 42, -116, 51, -112, 91, 71, 8, -25, 26, 41}; | ||
final KeysetKey key = new KeysetKey(1, keyBytes, Instant.now(), Instant.now(), Instant.now(), 123); | ||
byte[] payload = AesCbc.decrypt(encrypted, key); | ||
String expected = "[email protected]"; | ||
String results = new String(payload); | ||
assertEquals(results, expected); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.