Delete KeymanagerBase.
This isn't needed anymore, all key managers have been migrated to KeyTypeManagers.
PiperOrigin-RevId: 268229301
diff --git a/java/src/main/java/com/google/crypto/tink/KeyManagerBase.java b/java/src/main/java/com/google/crypto/tink/KeyManagerBase.java
deleted file mode 100644
index 2aaab31..0000000
--- a/java/src/main/java/com/google/crypto/tink/KeyManagerBase.java
+++ /dev/null
@@ -1,219 +0,0 @@
-// Copyright 2018 Google Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-////////////////////////////////////////////////////////////////////////////////
-
-package com.google.crypto.tink;
-
-import com.google.crypto.tink.annotations.Alpha;
-import com.google.crypto.tink.proto.KeyData;
-import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
-import com.google.protobuf.ByteString;
-import com.google.protobuf.InvalidProtocolBufferException;
-import com.google.protobuf.MessageLite;
-import java.security.GeneralSecurityException;
-
-/**
- * A utility class to implement a {@code KeyManager}.
- *
- * <p>Implementing many of the methods in the {@code KeyManager} can be repetitive. This is an
- * internal utility class to implement these methods. In order to instantiate it, one calls
- * the constructor with class objects of the protos used and the type URL:
- *
- * <pre> {@code
- * class MyConcreteKeyManager
- * extends KeyManagerBase<ConcretePrimitive, ConcreteKeyProto, ConcreteKeyFormatProto> {
- * public MyConcreteKeyManager() {
- * super(ConcreteKeyProto.class, ConcreteKeyFormatProto.class, TYPE_URL);
- * }
- * [...]
- * }</pre>
- * Furthermore, one implements all the abstract methods in this class.
- *
- * This code is currently Alpha and may change without warning.
- */
-@Alpha
-public abstract class KeyManagerBase<
- P, KeyProto extends MessageLite, KeyFormatProto extends MessageLite>
- implements KeyManager<P> {
- protected KeyManagerBase(
- final Class<P> primitiveClass,
- final Class<KeyProto> keyProtoClass,
- final Class<KeyFormatProto> keyFormatProtoClass,
- final String typeUrl) {
- this.primitiveClass = primitiveClass;
- this.keyProtoClass = keyProtoClass;
- this.keyFormatProtoClass = keyFormatProtoClass;
- this.typeUrl = typeUrl;
- }
-
- private final Class<P> primitiveClass;
- private final Class<KeyProto> keyProtoClass;
- private final Class<KeyFormatProto> keyFormatProtoClass;
- private final String typeUrl;
-
- private static <Casted> Casted castOrSecurityException(
- Object objectToCast, String exceptionText, Class<Casted> classObject)
- throws GeneralSecurityException {
- if (!classObject.isInstance(objectToCast)) {
- throw new GeneralSecurityException(exceptionText);
- }
- @SuppressWarnings("unchecked") // We just checked it manually.
- Casted castedObject = (Casted) objectToCast;
- return castedObject;
- }
-
- @Override
- public final P getPrimitive(ByteString serializedKey) throws GeneralSecurityException {
- try {
- KeyProto keyProto = parseKeyProto(serializedKey);
- return validateKeyAndGetPrimitive(keyProto);
- } catch (InvalidProtocolBufferException e) {
- throw new GeneralSecurityException(
- "Failures parsing proto of type " + keyProtoClass.getName(), e);
- }
- }
-
- @Override
- public final P getPrimitive(MessageLite key) throws GeneralSecurityException {
- return validateKeyAndGetPrimitive(
- castOrSecurityException(
- key, "Expected proto of type " + keyProtoClass.getName(), keyProtoClass));
- }
-
- /**
- * @param serializedKeyFormat serialized {@code AesGcmKeyFormat} proto
- * @return new {@code AesGcmKey} proto
- */
- @Override
- public final MessageLite newKey(ByteString serializedKeyFormat) throws GeneralSecurityException {
- try {
- return validateFormatAndCreateKey(parseKeyFormatProto(serializedKeyFormat));
- } catch (InvalidProtocolBufferException e) {
- throw new GeneralSecurityException(
- "Failures parsing proto of type " + keyFormatProtoClass.getName(), e);
- }
- }
-
- /**
- * @param keyFormat {@code AesGcmKeyFormat} proto
- * @return new {@code AesGcmKey} proto
- */
- @Override
- public final MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
- return validateFormatAndCreateKey(
- castOrSecurityException(
- keyFormat,
- "Expected proto of type " + keyFormatProtoClass.getName(),
- keyFormatProtoClass));
- }
-
- @Override
- public final boolean doesSupport(String typeUrl) {
- return typeUrl.equals(getKeyType());
- }
-
- @Override
- public final String getKeyType() {
- return typeUrl;
- }
-
- /**
- * @param serializedKeyFormat serialized {@code AesGcmKeyFormat} proto
- * @return {@code KeyData} proto with a new {@code AesGcmKey} proto
- */
- @Override
- public final KeyData newKeyData(ByteString serializedKeyFormat) throws GeneralSecurityException {
- KeyFormatProto format;
- try {
- format = parseKeyFormatProto(serializedKeyFormat);
- } catch (InvalidProtocolBufferException e) {
- throw new GeneralSecurityException("Unexpected proto", e);
- }
- KeyProto key = validateFormatAndCreateKey(format);
- return KeyData.newBuilder()
- .setTypeUrl(getKeyType())
- .setValue(key.toByteString())
- .setKeyMaterialType(keyMaterialType())
- .build();
- }
-
- @Override
- public final Class<P> getPrimitiveClass() {
- return primitiveClass;
- }
-
- /**
- * Checks if the given {@code keyProto} is a valid key. Throws a GeneralSecurityException if it is
- * not.
- */
- protected abstract void validateKey(KeyProto keyProto) throws GeneralSecurityException;
-
- /**
- * Checks if the given {@code keyProto} is a valid key format. Throws a GeneralSecurityException
- * if it is not.
- */
- protected abstract void validateKeyFormat(KeyFormatProto keyProto)
- throws GeneralSecurityException;
-
- /** Returns the {@code KeyMaterialType} for this proto. */
- protected abstract KeyMaterialType keyMaterialType();
-
- /**
- * Creates a primitive from a given key. Only called with validated {@code validatedKeyProto}s.
- */
- protected abstract P getPrimitiveFromKey(KeyProto validatedKeyProto)
- throws GeneralSecurityException;
-
- /** Creates a key proto after validating */
- private P validateKeyAndGetPrimitive(KeyProto keyProto) throws GeneralSecurityException {
- validateKey(keyProto);
- return getPrimitiveFromKey(keyProto);
- }
-
- /**
- * Creates a new key for a given format. Only called with validated {@code
- * validatedKeyFormatProto}s. The returned {@code KeyProto} will be validated.
- */
- protected abstract KeyProto newKeyFromFormat(KeyFormatProto validatedKeyFormatProto)
- throws GeneralSecurityException;
-
- /**
- * Validates the given {@code KeyFormatProto}, uses it to create a new key, validates it, then
- * returns
- */
- private KeyProto validateFormatAndCreateKey(KeyFormatProto keyFormatProto)
- throws GeneralSecurityException {
- validateKeyFormat(keyFormatProto);
- KeyProto result = newKeyFromFormat(keyFormatProto);
- validateKey(result);
- return result;
- }
-
- /**
- * Parses a serialized key proto.
- *
- * <p>Should be implemented as {code return MyKeyProto.parseFrom(byteString);}.
- */
- protected abstract KeyProto parseKeyProto(ByteString byteString)
- throws InvalidProtocolBufferException;
-
- /**
- * Parses a serialized key format proto.
- *
- * <p>Should be implemented as {code return MyKeyFormatProto.parseFrom(byteString);}.
- */
- protected abstract KeyFormatProto parseKeyFormatProto(ByteString byteString)
- throws InvalidProtocolBufferException;
-}
diff --git a/java/src/test/java/com/google/crypto/tink/KeyManagerBaseTest.java b/java/src/test/java/com/google/crypto/tink/KeyManagerBaseTest.java
deleted file mode 100644
index 37e4cab..0000000
--- a/java/src/test/java/com/google/crypto/tink/KeyManagerBaseTest.java
+++ /dev/null
@@ -1,246 +0,0 @@
-// Copyright 2018 Google Inc.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-////////////////////////////////////////////////////////////////////////////////
-
-package com.google.crypto.tink;
-
-import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
-
-import com.google.crypto.tink.TestUtil.DummyAead;
-import com.google.crypto.tink.proto.AesGcmKey;
-import com.google.crypto.tink.proto.AesGcmKeyFormat;
-import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
-import com.google.crypto.tink.subtle.Random;
-import com.google.crypto.tink.subtle.Validators;
-import com.google.protobuf.ByteString;
-import com.google.protobuf.InvalidProtocolBufferException;
-import com.google.protobuf.MessageLite;
-import java.security.GeneralSecurityException;
-import java.security.InvalidAlgorithmParameterException;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
-/** Tests the methods implemented in KeyManagerBase using the concrete implementation above. */
-@RunWith(JUnit4.class)
-public final class KeyManagerBaseTest {
- /** Keymanager for testing. Only produces dummy aeads, and wants the key size to be exactly 16. */
- static class TestKeyManager extends KeyManagerBase<Aead, AesGcmKey, AesGcmKeyFormat> {
- public TestKeyManager() {
- super(Aead.class, AesGcmKey.class, AesGcmKeyFormat.class, TYPE_URL);
- }
-
- private static final int VERSION = 0;
-
- public static final String TYPE_URL = "type.googleapis.com/google.crypto.tink.AesGcmKey";
-
- @Override
- protected Aead getPrimitiveFromKey(AesGcmKey key) throws GeneralSecurityException {
- return new DummyAead();
- }
-
- @Override
- protected AesGcmKey newKeyFromFormat(AesGcmKeyFormat format) throws GeneralSecurityException {
- return AesGcmKey.newBuilder()
- .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize())))
- .setVersion(VERSION)
- .build();
- }
-
- @Override
- public int getVersion() {
- return VERSION;
- }
-
- @Override
- protected KeyMaterialType keyMaterialType() {
- return KeyMaterialType.SYMMETRIC;
- }
-
- @Override
- protected AesGcmKey parseKeyProto(ByteString byteString) throws InvalidProtocolBufferException {
- return AesGcmKey.parseFrom(byteString);
- }
-
- @Override
- protected AesGcmKeyFormat parseKeyFormatProto(ByteString byteString)
- throws InvalidProtocolBufferException {
- return AesGcmKeyFormat.parseFrom(byteString);
- }
-
- private void throwIfNot16(int size) throws GeneralSecurityException {
- if (size != 16) {
- throw new InvalidAlgorithmParameterException("invalid key size; only size 16 is good.");
- }
- }
-
- @Override
- protected void validateKey(AesGcmKey key) throws GeneralSecurityException {
- Validators.validateVersion(key.getVersion(), VERSION);
- throwIfNot16(key.getKeyValue().size());
- }
-
- @Override
- protected void validateKeyFormat(AesGcmKeyFormat format) throws GeneralSecurityException {
- throwIfNot16(format.getKeySize());
- }
- }
-
- @Test
- public void getPrimitive_ByteString_works() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- MessageLite key = keyManager.newKey(AesGcmKeyFormat.newBuilder().setKeySize(16).build());
- keyManager.getPrimitive(key.toByteString());
- }
-
- @Test
- public void getPrimitive_ByteString_throwsInvalidKey() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- MessageLite notAKey = AesGcmKey.getDefaultInstance();
- try {
- keyManager.getPrimitive(notAKey.toByteString());
- fail("expected GeneralSecurityException");
- } catch (GeneralSecurityException e) {
- assertThat(e.toString()).contains("invalid key size");
- }
- }
-
- @Test
- public void getPrimitive_MessageLite_works() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- MessageLite key = keyManager.newKey(AesGcmKeyFormat.newBuilder().setKeySize(16).build());
- keyManager.getPrimitive(key);
- }
-
- @Test
- public void getPrimitive_MessageLite_throwsWrongProto() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- MessageLite notAKey = AesGcmKeyFormat.getDefaultInstance();
- try {
- keyManager.getPrimitive(notAKey);
- fail("expected GeneralSecurityException");
- } catch (GeneralSecurityException e) {
- assertThat(e.toString()).contains("Expected proto of type");
- }
- }
-
- @Test
- public void getPrimitive_MessageLite_throwsInvalidKey() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- MessageLite notAKey = AesGcmKey.getDefaultInstance();
- try {
- keyManager.getPrimitive(notAKey);
- fail("expected GeneralSecurityException");
- } catch (GeneralSecurityException e) {
- assertThat(e.toString()).contains("invalid key size");
- }
- }
-
- @Test
- public void newKey_ByteString_works() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- keyManager.newKey(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString());
- }
-
- @Test
- public void newKey_ByteString_throwsInvalidKeySize() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- try {
- keyManager.newKey(AesGcmKeyFormat.newBuilder().setKeySize(17).build().toByteString());
- fail("expected GeneralSecurityException");
- } catch (GeneralSecurityException e) {
- assertThat(e.toString()).contains("invalid key size");
- }
- }
-
- @Test
- public void newKey_MessageLite_works() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- keyManager.newKey(AesGcmKeyFormat.newBuilder().setKeySize(16).build());
- }
-
- @Test
- public void newKey_MessageLite_throwsWrongProto() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- try {
- keyManager.newKey(AesGcmKey.getDefaultInstance());
- fail("expected GeneralSecurityException");
- } catch (GeneralSecurityException e) {
- assertThat(e.toString()).contains("Expected proto of type");
- }
- }
-
- @Test
- public void doesSupport_returnsTrue() throws Exception {
- assertThat(new TestKeyManager().doesSupport("type.googleapis.com/google.crypto.tink.AesGcmKey"))
- .isTrue();
- }
-
- @Test
- public void doesSupport_returnsFalse() throws Exception {
- assertThat(new TestKeyManager().doesSupport("type.googleapis.com/SomeOtherKey")).isFalse();
- }
-
- @Test
- public void getKeyType() throws Exception {
- assertThat(new TestKeyManager().getKeyType())
- .isEqualTo("type.googleapis.com/google.crypto.tink.AesGcmKey");
- }
-
- @Test
- public void newKeyData_works() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- keyManager.newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString());
- }
-
- @Test
- public void newKeyData_typeUrlCorrect() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- assertThat(
- keyManager
- .newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString())
- .getTypeUrl())
- .isEqualTo("type.googleapis.com/google.crypto.tink.AesGcmKey");
- }
-
- @Test
- public void newKeyData_valueLengthCorrect() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- // We allow the keysize to be bigger than 16 since proto serialized adds some overhead.
- assertThat(
- keyManager
- .newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString())
- .getValue()
- .size())
- .isAtLeast(16);
- }
-
- @Test
- public void newKeyData_keyMaterialTypeCorrect() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- assertThat(
- keyManager
- .newKeyData(AesGcmKeyFormat.newBuilder().setKeySize(16).build().toByteString())
- .getKeyMaterialType())
- .isEqualTo(KeyMaterialType.SYMMETRIC);
- }
-
- @Test
- public void getPrimitiveClass() throws Exception {
- TestKeyManager keyManager = new TestKeyManager();
- assertThat(keyManager.getPrimitiveClass()).isEqualTo(Aead.class);
- }
-}
diff --git a/java/src/test/java/com/google/crypto/tink/KeyTypeManagerTest.java b/java/src/test/java/com/google/crypto/tink/KeyTypeManagerTest.java
index a5457e0..1e273ad 100644
--- a/java/src/test/java/com/google/crypto/tink/KeyTypeManagerTest.java
+++ b/java/src/test/java/com/google/crypto/tink/KeyTypeManagerTest.java
@@ -31,14 +31,18 @@
public final class KeyTypeManagerTest {
private static final ByteString TEST_BYTESTRING = ByteString.copyFromUtf8("Some text");
- private static class TestKeyTypeManager extends KeyTypeManager<AesGcmKey> {
+ /**
+ * A KeyTypeManager for testing. It accepts AesGcmKeys and produces primitives as with the passed
+ * in factory.
+ */
+ public static class TestKeyTypeManager extends KeyTypeManager<AesGcmKey> {
public TestKeyTypeManager(PrimitiveFactory<?, AesGcmKey>... factories) {
super(AesGcmKey.class, factories);
}
@Override
public String getKeyType() {
- return "KeyTypeUrl";
+ return "type.googleapis.com/google.crypto.tink.AesGcmKey";
}
@Override
diff --git a/java/src/test/java/com/google/crypto/tink/KeysetHandleTest.java b/java/src/test/java/com/google/crypto/tink/KeysetHandleTest.java
index bc0864b..b512734 100644
--- a/java/src/test/java/com/google/crypto/tink/KeysetHandleTest.java
+++ b/java/src/test/java/com/google/crypto/tink/KeysetHandleTest.java
@@ -23,9 +23,11 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import com.google.crypto.tink.TestUtil.DummyAead;
import com.google.crypto.tink.aead.AeadKeyTemplates;
import com.google.crypto.tink.config.TinkConfig;
import com.google.crypto.tink.mac.MacKeyTemplates;
+import com.google.crypto.tink.proto.AesGcmKey;
import com.google.crypto.tink.proto.EcdsaPrivateKey;
import com.google.crypto.tink.proto.KeyData;
import com.google.crypto.tink.proto.KeyStatusType;
@@ -195,8 +197,20 @@
KeyStatusType.ENABLED,
OutputPrefixType.TINK));
KeysetHandle handle = KeysetHandle.fromKeyset(keyset);
- // The TestKeyManager accepts AES128_GCM keys, but creates a DummyAead which always fails.
- Aead aead = handle.getPrimitive(new KeyManagerBaseTest.TestKeyManager(), Aead.class);
+ // A key manager which accepts AES_GCM keys, but which creates a DummyAead primitive which
+ // always fails.
+ KeyManager<Aead> manager =
+ new KeyManagerImpl<>(
+ new KeyTypeManagerTest.TestKeyTypeManager(
+ new KeyTypeManagerTest.TestKeyTypeManager.PrimitiveFactory<Aead, AesGcmKey>(
+ Aead.class) {
+ @Override
+ public Aead getPrimitive(AesGcmKey key) {
+ return new DummyAead();
+ }
+ }),
+ Aead.class);
+ Aead aead = handle.getPrimitive(manager, Aead.class);
try {
aead.encrypt(new byte[0], new byte[0]);
fail("Expected GeneralSecurityException");