Util { public static void validateKeyset(Keyset keyset) throws GeneralSecurityException { int primaryKeyId = keyset.getPrimaryKeyId(); boolean hasPrimaryKey = false; boolean containsOnlyPublicKeyMaterial = true; int numEnabledKeys = 0; for (Keyset.Key key : keyset.getKeyList()) { if (key.getStatus() != KeyStatusType.ENABLED) { continue; } validateKey(key); if (key.getKeyId() == primaryKeyId) { if (hasPrimaryKey) { throw new GeneralSecurityException("keyset contains multiple primary keys"); } hasPrimaryKey = true; } if (key.getKeyData().getKeyMaterialType() != KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC) { containsOnlyPublicKeyMaterial = false; } numEnabledKeys++; } if (numEnabledKeys == 0) { throw new GeneralSecurityException("keyset must contain at least one ENABLED key"); } if (!hasPrimaryKey && !containsOnlyPublicKeyMaterial) { throw new GeneralSecurityException("keyset doesn't contain a valid primary key"); } }  static KeysetInfo getKeysetInfo(Keyset keyset); static KeysetInfo.KeyInfo getKeyInfo(Keyset.Key key); static void validateKey(Keyset.Key key); static void validateKeyset(Keyset keyset); static byte[] readAll(InputStream inputStream); static final Charset UTF_8; }
@Test public void testValidateKeyset_shouldWork() throws Exception { String keyValue = "01234567890123456"; Keyset keyset = TestUtil.createKeyset( TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), -42, KeyStatusType.ENABLED, OutputPrefixType.TINK)); try { Util.validateKeyset(keyset); } catch (GeneralSecurityException e) { fail("Valid keyset; should not throw Exception: " + e); } }
@Test public void testValidateKeyset_emptyKeyset_shouldFail() throws Exception { try { Util.validateKeyset(Keyset.newBuilder().build()); fail("Invalid keyset. Expect GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "keyset must contain at least one ENABLED key"); } }
@Test public void testValidateKeyset_multiplePrimaryKeys_shouldFail() throws Exception { String keyValue = "01234567890123456"; Keyset invalidKeyset = TestUtil.createKeyset( TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK), TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK)); try { Util.validateKeyset(invalidKeyset); fail("Invalid keyset. Expect GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "keyset contains multiple primary keys"); } }
@Test public void testValidateKeyset_primaryKeyIsDisabled_shouldFail() throws Exception { String keyValue = "01234567890123456"; Keyset invalidKeyset = TestUtil.createKeyset( TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DISABLED, OutputPrefixType.TINK), TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 43, KeyStatusType.ENABLED, OutputPrefixType.TINK)); try { Util.validateKeyset(invalidKeyset); fail("Invalid keyset. Expect GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "keyset doesn't contain a valid primary key"); } }
@Test public void testValidateKeyset_noEnabledKey_shouldFail() throws Exception { String keyValue = "01234567890123456"; Keyset invalidKeyset = TestUtil.createKeyset( TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DISABLED, OutputPrefixType.TINK), TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DESTROYED, OutputPrefixType.TINK)); try { Util.validateKeyset(invalidKeyset); fail("Invalid keyset. Expect GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "keyset must contain at least one ENABLED key"); } }
@Test public void testValidateKeyset_noPrimaryKey_shouldFail() throws Exception { String keyValue = "01234567890123456"; Keyset invalidKeyset = Keyset.newBuilder() .addKey( Keyset.Key.newBuilder() .setKeyData(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16)) .setKeyId(1) .setStatus(KeyStatusType.ENABLED) .setOutputPrefixType(OutputPrefixType.TINK) .build()) .build(); try { Util.validateKeyset(invalidKeyset); fail("Invalid keyset. Expect GeneralSecurityException"); } catch (GeneralSecurityException e) { assertExceptionContains(e, "keyset doesn't contain a valid primary key"); } }
@Test public void testValidateKeyset_noPrimaryKey_keysetContainsOnlyPublicKeys_shouldWork() throws Exception { Keyset validKeyset = Keyset.newBuilder() .addKey( Keyset.Key.newBuilder() .setKeyData( TestUtil.createKeyData( KeyData.newBuilder().build(), "typeUrl", KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC)) .setKeyId(1) .setStatus(KeyStatusType.ENABLED) .setOutputPrefixType(OutputPrefixType.TINK) .build()) .build(); try { Util.validateKeyset(validKeyset); } catch (GeneralSecurityException e) { fail("Valid keyset, should not fail: " + e); } }
@Test public void testValidateKeyset_withDestroyedKey_shouldWork() throws Exception { String keyValue = "01234567890123456"; Keyset validKeyset = TestUtil.createKeyset( TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK), TestUtil.createKey( TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DESTROYED, OutputPrefixType.TINK)); try { Util.validateKeyset(validKeyset); } catch (GeneralSecurityException e) { fail("Valid keyset, should not fail: " + e); } }