// 
// Decompiled by Procyon v0.6.0
// 

package com.google.crypto.tink.aead;

import com.google.crypto.tink.internal.Util;
import com.google.crypto.tink.internal.MutableSerializationRegistry;
import com.google.crypto.tink.proto.KeyData;
import com.google.crypto.tink.proto.KmsEnvelopeAeadKey;
import javax.annotation.Nullable;
import com.google.crypto.tink.SecretKeyAccess;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.crypto.tink.Parameters;
import com.google.crypto.tink.TinkProtoParametersFormat;
import com.google.crypto.tink.proto.KmsEnvelopeAeadKeyFormat;
import com.google.crypto.tink.AccessesPartialKey;
import com.google.crypto.tink.proto.KeyTemplate;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.crypto.tink.internal.KeyParser;
import com.google.crypto.tink.internal.ProtoKeySerialization;
import com.google.crypto.tink.internal.KeySerializer;
import com.google.crypto.tink.internal.ParametersParser;
import com.google.crypto.tink.internal.ProtoParametersSerialization;
import com.google.crypto.tink.internal.ParametersSerializer;
import com.google.crypto.tink.util.Bytes;

public final class LegacyKmsEnvelopeAeadProtoSerialization
{
    private static final String TYPE_URL = "type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey";
    private static final Bytes TYPE_URL_BYTES;
    private static final ParametersSerializer<LegacyKmsEnvelopeAeadParameters, ProtoParametersSerialization> PARAMETERS_SERIALIZER;
    private static final ParametersParser<ProtoParametersSerialization> PARAMETERS_PARSER;
    private static final KeySerializer<LegacyKmsEnvelopeAeadKey, ProtoKeySerialization> KEY_SERIALIZER;
    private static final KeyParser<ProtoKeySerialization> KEY_PARSER;
    
    private static OutputPrefixType toProtoOutputPrefixType(final LegacyKmsEnvelopeAeadParameters.Variant variant) throws GeneralSecurityException {
        if (LegacyKmsEnvelopeAeadParameters.Variant.TINK.equals(variant)) {
            return OutputPrefixType.TINK;
        }
        if (LegacyKmsEnvelopeAeadParameters.Variant.NO_PREFIX.equals(variant)) {
            return OutputPrefixType.RAW;
        }
        throw new GeneralSecurityException("Unable to serialize variant: " + variant);
    }
    
    private static LegacyKmsEnvelopeAeadParameters.Variant toVariant(final OutputPrefixType outputPrefixType) throws GeneralSecurityException {
        switch (outputPrefixType) {
            case TINK: {
                return LegacyKmsEnvelopeAeadParameters.Variant.TINK;
            }
            case RAW: {
                return LegacyKmsEnvelopeAeadParameters.Variant.NO_PREFIX;
            }
            default: {
                throw new GeneralSecurityException("Unable to parse OutputPrefixType: " + outputPrefixType.getNumber());
            }
        }
    }
    
    @AccessesPartialKey
    private static ProtoParametersSerialization serializeParameters(final LegacyKmsEnvelopeAeadParameters parameters) throws GeneralSecurityException {
        return ProtoParametersSerialization.create(KeyTemplate.newBuilder().setTypeUrl("type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey").setValue(serializeParametersToKmsEnvelopeAeadKeyFormat(parameters).toByteString()).setOutputPrefixType(toProtoOutputPrefixType(parameters.getVariant())).build());
    }
    
    @AccessesPartialKey
    private static KmsEnvelopeAeadKeyFormat serializeParametersToKmsEnvelopeAeadKeyFormat(final LegacyKmsEnvelopeAeadParameters parameters) throws GeneralSecurityException {
        final byte[] serializedDekParameters = TinkProtoParametersFormat.serialize(parameters.getDekParametersForNewKeys());
        try {
            final KeyTemplate dekKeyTemplate = KeyTemplate.parseFrom(serializedDekParameters, ExtensionRegistryLite.getEmptyRegistry());
            return KmsEnvelopeAeadKeyFormat.newBuilder().setKekUri(parameters.getKekUri()).setDekTemplate(dekKeyTemplate).build();
        }
        catch (final InvalidProtocolBufferException e) {
            throw new GeneralSecurityException("Parsing KmsEnvelopeAeadKeyFormat failed: ", e);
        }
    }
    
    @AccessesPartialKey
    private static ProtoKeySerialization serializeKey(final LegacyKmsEnvelopeAeadKey key, @Nullable final SecretKeyAccess access) throws GeneralSecurityException {
        return ProtoKeySerialization.create("type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey", KmsEnvelopeAeadKey.newBuilder().setParams(serializeParametersToKmsEnvelopeAeadKeyFormat(key.getParameters())).build().toByteString(), KeyData.KeyMaterialType.REMOTE, toProtoOutputPrefixType(key.getParameters().getVariant()), key.getIdRequirementOrNull());
    }
    
    @AccessesPartialKey
    private static LegacyKmsEnvelopeAeadParameters parseParameters(final ProtoParametersSerialization serialization) throws GeneralSecurityException {
        if (!serialization.getKeyTemplate().getTypeUrl().equals("type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey")) {
            throw new IllegalArgumentException("Wrong type URL in call to LegacyKmsEnvelopeAeadProtoSerialization.parseParameters: " + serialization.getKeyTemplate().getTypeUrl());
        }
        KmsEnvelopeAeadKeyFormat format;
        try {
            format = KmsEnvelopeAeadKeyFormat.parseFrom(serialization.getKeyTemplate().getValue(), ExtensionRegistryLite.getEmptyRegistry());
        }
        catch (final InvalidProtocolBufferException e) {
            throw new GeneralSecurityException("Parsing KmsEnvelopeAeadKeyFormat failed: ", e);
        }
        return parseParameters(format, serialization.getKeyTemplate().getOutputPrefixType());
    }
    
    @AccessesPartialKey
    private static LegacyKmsEnvelopeAeadParameters parseParameters(final KmsEnvelopeAeadKeyFormat format, final OutputPrefixType outputPrefixType) throws GeneralSecurityException {
        final Parameters aeadParameters = TinkProtoParametersFormat.parse(KeyTemplate.newBuilder().setTypeUrl(format.getDekTemplate().getTypeUrl()).setValue(format.getDekTemplate().getValue()).setOutputPrefixType(OutputPrefixType.RAW).build().toByteArray());
        LegacyKmsEnvelopeAeadParameters.DekParsingStrategy strategy;
        if (aeadParameters instanceof AesGcmParameters) {
            strategy = LegacyKmsEnvelopeAeadParameters.DekParsingStrategy.ASSUME_AES_GCM;
        }
        else if (aeadParameters instanceof ChaCha20Poly1305Parameters) {
            strategy = LegacyKmsEnvelopeAeadParameters.DekParsingStrategy.ASSUME_CHACHA20POLY1305;
        }
        else if (aeadParameters instanceof XChaCha20Poly1305Parameters) {
            strategy = LegacyKmsEnvelopeAeadParameters.DekParsingStrategy.ASSUME_XCHACHA20POLY1305;
        }
        else if (aeadParameters instanceof AesCtrHmacAeadParameters) {
            strategy = LegacyKmsEnvelopeAeadParameters.DekParsingStrategy.ASSUME_AES_CTR_HMAC;
        }
        else if (aeadParameters instanceof AesEaxParameters) {
            strategy = LegacyKmsEnvelopeAeadParameters.DekParsingStrategy.ASSUME_AES_EAX;
        }
        else {
            if (!(aeadParameters instanceof AesGcmSivParameters)) {
                throw new GeneralSecurityException("Unsupported DEK parameters when parsing " + aeadParameters);
            }
            strategy = LegacyKmsEnvelopeAeadParameters.DekParsingStrategy.ASSUME_AES_GCM_SIV;
        }
        return LegacyKmsEnvelopeAeadParameters.builder().setVariant(toVariant(outputPrefixType)).setKekUri(format.getKekUri()).setDekParametersForNewKeys((AeadParameters)aeadParameters).setDekParsingStrategy(strategy).build();
    }
    
    @AccessesPartialKey
    private static LegacyKmsEnvelopeAeadKey parseKey(final ProtoKeySerialization serialization, @Nullable final SecretKeyAccess access) throws GeneralSecurityException {
        if (!serialization.getTypeUrl().equals("type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey")) {
            throw new IllegalArgumentException("Wrong type URL in call to LegacyKmsEnvelopeAeadProtoSerialization.parseKey");
        }
        try {
            final KmsEnvelopeAeadKey protoKey = KmsEnvelopeAeadKey.parseFrom(serialization.getValue(), ExtensionRegistryLite.getEmptyRegistry());
            if (protoKey.getVersion() != 0) {
                throw new GeneralSecurityException("KmsEnvelopeAeadKeys are only accepted with version 0, got " + protoKey);
            }
            final LegacyKmsEnvelopeAeadParameters parameters = parseParameters(protoKey.getParams(), serialization.getOutputPrefixType());
            return LegacyKmsEnvelopeAeadKey.create(parameters, serialization.getIdRequirementOrNull());
        }
        catch (final InvalidProtocolBufferException e) {
            throw new GeneralSecurityException("Parsing KmsEnvelopeAeadKey failed: ", e);
        }
    }
    
    public static void register() throws GeneralSecurityException {
        register(MutableSerializationRegistry.globalInstance());
    }
    
    public static void register(final MutableSerializationRegistry registry) throws GeneralSecurityException {
        registry.registerParametersSerializer(LegacyKmsEnvelopeAeadProtoSerialization.PARAMETERS_SERIALIZER);
        registry.registerParametersParser(LegacyKmsEnvelopeAeadProtoSerialization.PARAMETERS_PARSER);
        registry.registerKeySerializer(LegacyKmsEnvelopeAeadProtoSerialization.KEY_SERIALIZER);
        registry.registerKeyParser(LegacyKmsEnvelopeAeadProtoSerialization.KEY_PARSER);
    }
    
    private LegacyKmsEnvelopeAeadProtoSerialization() {
    }
    
    static {
        TYPE_URL_BYTES = Util.toBytesFromPrintableAscii("type.googleapis.com/google.crypto.tink.KmsEnvelopeAeadKey");
        PARAMETERS_SERIALIZER = ParametersSerializer.create(LegacyKmsEnvelopeAeadProtoSerialization::serializeParameters, LegacyKmsEnvelopeAeadParameters.class, ProtoParametersSerialization.class);
        PARAMETERS_PARSER = ParametersParser.create(LegacyKmsEnvelopeAeadProtoSerialization::parseParameters, LegacyKmsEnvelopeAeadProtoSerialization.TYPE_URL_BYTES, ProtoParametersSerialization.class);
        KEY_SERIALIZER = KeySerializer.create(LegacyKmsEnvelopeAeadProtoSerialization::serializeKey, LegacyKmsEnvelopeAeadKey.class, ProtoKeySerialization.class);
        KEY_PARSER = KeyParser.create(LegacyKmsEnvelopeAeadProtoSerialization::parseKey, LegacyKmsEnvelopeAeadProtoSerialization.TYPE_URL_BYTES, ProtoKeySerialization.class);
    }
}
