Search in sources :

Example 41 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class UtilTest method testGetKeysetInfo.

/**
 * Tests that getKeysetInfo doesn't contain key material.
 */
@Test
public void testGetKeysetInfo() throws Exception {
    String keyValue = "01234567890123456";
    Keyset keyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK));
    assertTrue(keyset.toString().contains(keyValue));
    KeysetInfo keysetInfo = Util.getKeysetInfo(keyset);
    assertFalse(keysetInfo.toString().contains(keyValue));
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) KeysetInfo(com.google.crypto.tink.proto.KeysetInfo) Test(org.junit.Test)

Example 42 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class UtilTest method testValidateKeyset_noEnabledKey_shouldFail.

@Test
public void testValidateKeyset_noEnabledKey_shouldFail() throws Exception {
    String keyValue = "01234567890123456";
    // No ENABLED key.
    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));
    GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> Util.validateKeyset(invalidKeyset));
    assertExceptionContains(e, "keyset must contain at least one ENABLED key");
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) Test(org.junit.Test)

Example 43 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class UtilTest method testValidateKeyset_primaryKeyIsDisabled_shouldFail.

@Test
public void testValidateKeyset_primaryKeyIsDisabled_shouldFail() throws Exception {
    String keyValue = "01234567890123456";
    // Primary key is disabled.
    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));
    GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> Util.validateKeyset(invalidKeyset));
    assertExceptionContains(e, "keyset doesn't contain a valid primary key");
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) Test(org.junit.Test)

Example 44 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class UtilTest method testValidateKeyset_noPrimaryKey_shouldFail.

@Test
public void testValidateKeyset_noPrimaryKey_shouldFail() throws Exception {
    String keyValue = "01234567890123456";
    // No primary key.
    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();
    GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> Util.validateKeyset(invalidKeyset));
    assertExceptionContains(e, "keyset doesn't contain a valid primary key");
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) Test(org.junit.Test)

Example 45 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetManagerTest method testAddNewKey_addThenDestroy.

@Test
public void testAddNewKey_addThenDestroy() throws Exception {
    KeysetManager keysetManager = KeysetManager.withEmptyKeyset();
    keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, true);
    int secondaryKeyId = keysetManager.addNewKey(MacKeyTemplates.HMAC_SHA256_128BITTAG, false);
    keysetManager.destroy(secondaryKeyId);
    Keyset keyset = keysetManager.getKeysetHandle().getKeyset();
    assertThat(keyset.getKeyCount()).isEqualTo(2);
    // One of the two keys is destroyed and doesn't have keyData anymore.
    assertTrue(!keyset.getKey(0).hasKeyData() || !keyset.getKey(1).hasKeyData());
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Test(org.junit.Test)

Aggregations

Keyset (com.google.crypto.tink.proto.Keyset)108 Test (org.junit.Test)81 GeneralSecurityException (java.security.GeneralSecurityException)22 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)17 KeysetHandle (com.google.crypto.tink.KeysetHandle)17 KeyData (com.google.crypto.tink.proto.KeyData)17 KeyTemplate (com.google.crypto.tink.KeyTemplate)12 EncryptedKeyset (com.google.crypto.tink.proto.EncryptedKeyset)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteString (com.google.protobuf.ByteString)10 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)10 Key (com.google.crypto.tink.proto.Keyset.Key)9 JsonObject (com.google.gson.JsonObject)9 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)8 KeysetReader (com.google.crypto.tink.KeysetReader)7 IOException (java.io.IOException)7 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)6 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)6 Enums (com.google.crypto.tink.subtle.Enums)6 KeyHandle (com.google.crypto.tink.tinkkey.KeyHandle)6