use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class JwtHmacKeyManagerTest method getRfc7515ExampleKeysetHandle.
private static KeysetHandle getRfc7515ExampleKeysetHandle() throws Exception {
String keyValue = "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow";
JwtHmacKey key = JwtHmacKey.newBuilder().setVersion(0).setAlgorithm(JwtHmacAlgorithm.HS256).setKeyValue(ByteString.copyFrom(Base64.urlSafeDecode(keyValue))).build();
KeyData keyData = KeyData.newBuilder().setTypeUrl("type.googleapis.com/google.crypto.tink.JwtHmacKey").setValue(key.toByteString()).setKeyMaterialType(KeyData.KeyMaterialType.SYMMETRIC).build();
Keyset.Key keySetKey = Keyset.Key.newBuilder().setKeyData(keyData).setKeyId(123).setStatus(KeyStatusType.ENABLED).setOutputPrefixType(OutputPrefixType.RAW).build();
Keyset keyset = Keyset.newBuilder().addKey(keySetKey).setPrimaryKeyId(123).build();
return CleartextKeysetHandle.fromKeyset(keyset);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetHandle method decrypt.
/**
* Decrypts the encrypted keyset with the {@link Aead} master key.
*/
private static Keyset decrypt(EncryptedKeyset encryptedKeyset, Aead masterKey) throws GeneralSecurityException {
try {
Keyset keyset = Keyset.parseFrom(masterKey.decrypt(encryptedKeyset.getEncryptedKeyset().toByteArray(), /* associatedData= */
new byte[0]));
// check emptiness here too, in case the encrypted keys unwrapped to nothing?
assertEnoughKeyMaterial(keyset);
return keyset;
} catch (InvalidProtocolBufferException e) {
throw new GeneralSecurityException("invalid keyset, corrupted key material");
}
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class TestUtil method createKeyset.
/**
* @return a keyset from a list of keys. The first key is primary.
*/
public static Keyset createKeyset(Key primary, Key... keys) throws Exception {
Keyset.Builder builder = Keyset.newBuilder();
builder.addKey(primary).setPrimaryKeyId(primary.getKeyId());
for (Key key : keys) {
builder.addKey(key);
}
return builder.build();
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class UtilTest method testValidateKeyset.
@Test
public void testValidateKeyset() 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);
}
// Empty keyset.
try {
Util.validateKeyset(Keyset.newBuilder().build());
fail("Invalid keyset. Expect GeneralSecurityException");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "empty keyset");
}
// Multiple primary keys.
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");
}
// Primary key is disabled.
invalidKeyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.DISABLED, OutputPrefixType.TINK));
try {
Util.validateKeyset(invalidKeyset);
fail("Invalid keyset. Expect GeneralSecurityException");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "keyset doesn't contain a valid primary key");
}
// No primary key.
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");
}
// No primary key, but contains only public key material.
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);
}
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetHandleTest method testToString.
/**
* Tests that toString doesn't contain key material.
*/
@Test
public void testToString() throws Exception {
String keyValue = "01234567890123456";
Keyset keyset = TestUtil.createKeyset(TestUtil.createKey(TestUtil.createHmacKeyData(keyValue.getBytes("UTF-8"), 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK));
KeysetHandle handle = KeysetHandle.fromKeyset(keyset);
assertEquals(keyset, handle.getKeyset());
String keysetInfo = handle.toString();
assertFalse(keysetInfo.contains(keyValue));
assertTrue(handle.getKeyset().toString().contains(keyValue));
}
Aggregations