package com.google.crypto.tink.aead;
import com.google.crypto.tink.Aead;
import com.google.crypto.tink.KeyTypeManager;
import com.google.crypto.tink.KmsClient;
import com.google.crypto.tink.KmsClients;
import com.google.crypto.tink.Registry;
import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
import com.google.crypto.tink.proto.KmsAeadKey;
import com.google.crypto.tink.proto.KmsAeadKeyFormat;
import com.google.crypto.tink.subtle.Validators;
import com.google.protobuf.ByteString;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.InvalidProtocolBufferException;
import java.security.GeneralSecurityException;
public class KmsAeadKeyManager extends KeyTypeManager<KmsAeadKey> {
KmsAeadKeyManager() {
super(
KmsAeadKey.class,
new PrimitiveFactory<Aead, KmsAeadKey>(Aead.class) {
@Override
public Aead getPrimitive(KmsAeadKey keyProto) throws GeneralSecurityException {
String keyUri = keyProto.getParams().getKeyUri();
KmsClient kmsClient = KmsClients.get(keyUri);
return kmsClient.getAead(keyUri);
}
});
}
@Override
public String getKeyType() {
return "type.googleapis.com/google.crypto.tink.KmsAeadKey";
}
@Override
public int getVersion() {
return 0;
}
@Override
public KeyMaterialType keyMaterialType() {
return KeyMaterialType.REMOTE;
}
@Override
public void validateKey(KmsAeadKey key) throws GeneralSecurityException {
Validators.validateVersion(key.getVersion(), getVersion());
}
@Override
public KmsAeadKey parseKey(ByteString byteString) throws InvalidProtocolBufferException {
return KmsAeadKey.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
}
@Override
public KeyFactory<KmsAeadKeyFormat, KmsAeadKey> keyFactory() {
return new KeyFactory<KmsAeadKeyFormat, KmsAeadKey>(KmsAeadKeyFormat.class) {
@Override
public void validateKeyFormat(KmsAeadKeyFormat format) throws GeneralSecurityException {}
@Override
public KmsAeadKeyFormat parseKeyFormat(ByteString byteString)
throws InvalidProtocolBufferException {
return KmsAeadKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
}
@Override
public KmsAeadKey createKey(KmsAeadKeyFormat format) throws GeneralSecurityException {
return KmsAeadKey.newBuilder().setParams(format).setVersion(getVersion()).build();
}
};
}
public static void register(boolean newKeyAllowed) throws GeneralSecurityException {
Registry.registerKeyManager(new KmsAeadKeyManager(), newKeyAllowed);
}
}