@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"); } }
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"); } }
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"); } } }
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"); } }  }
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); }
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; }