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