package a.b.a.h;

import android.content.Context;
import android.text.TextUtils;
import com.google.protobuf.ByteString;
import com.ucar.databus.proto.UCarProto;
import java.nio.charset.StandardCharsets;

public class c extends a {
    private static final String j = "ServerKeyNegotiator";
    private static final String k = "server_auth_key_for_client_";
    private static final int l = 32;
    private static UCarProto.AuthResponse m;

    public static class b {

        /* renamed from: a  reason: collision with root package name */
        public byte[] f27a;
        public int b;
        public byte[] c;
        public byte[] d;
        public byte[] e;
        public byte[] f;
        public byte[] g;
        public boolean h;

        private b() {
        }
    }

    static {
        try {
            m = (UCarProto.AuthResponse) UCarProto.AuthResponse.newBuilder().setVersion(a.g()).setAuthPk(ByteString.EMPTY).setAuthPkHmac(ByteString.EMPTY).setAgreementPk(ByteString.EMPTY).setAgreementPkSig(ByteString.EMPTY).setRandom(ByteString.EMPTY).setResult(3).build();
        } catch (Exception e) {
            a.a.a.b.b(j, "build AuthResponse Exception", e);
        }
    }

    public c(Context context) {
        super(context);
    }

    private b a(UCarProto.AuthRequest authRequest) {
        b bVar = new b();
        bVar.f27a = authRequest.getId().toByteArray();
        bVar.b = authRequest.getVersion();
        bVar.c = authRequest.getAuthPk().toByteArray();
        bVar.d = authRequest.getAuthPkHmac().toByteArray();
        bVar.e = authRequest.getAgreementPk().toByteArray();
        bVar.f = authRequest.getAgreementPkSig().toByteArray();
        bVar.g = authRequest.getRandom().toByteArray();
        bVar.h = authRequest.getUserConfirmed();
        return bVar;
    }

    public static void a(Context context, String str) {
        if (TextUtils.isEmpty(str)) {
            a.a.a.b.e(j, "deletePhoneById received empty deviceId");
            return;
        }
        try {
            byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
            a.a(context);
            a.a(bytes, g(bytes));
        } catch (Exception e) {
            a.a.a.b.b(j, "removePeer Exception", e);
        }
    }

    private static String g(byte[] bArr) {
        return k + new String(bArr, StandardCharsets.UTF_8);
    }

    /* JADX WARNING: Removed duplicated region for block: B:54:0x0284  */
    public UCarProto.AuthResponse a(UCarProto.AuthRequest authRequest, int i, int i2, String str) {
        Exception e;
        if (f() == null || f().length < 6 || i == 0) {
            return m;
        }
        b bVar = null;
        try {
            b a2 = a(authRequest);
            try {
                if (!(a2.e.length == 0 || a2.g.length == 0)) {
                    if (a2.f27a.length != 0) {
                        byte[] a3 = a(a());
                        byte[] a4 = a(32);
                        if (a2.c.length != 0) {
                            if (a2.d.length != 0) {
                                a.a.a.b.c(j, "normal connection initiated by the phone");
                                if (a(a(a2.e, a2.g, a2.f, a(a2.c, new byte[0], a2.g, a2.d)), a.b.a.i.a.a(a2.g, e()))) {
                                    if (b(a2.f27a, a2.c)) {
                                        byte[] a5 = a(c(g(a2.f27a)));
                                        a.a.a.b.c(j, "normal connection succeeded");
                                        return (UCarProto.AuthResponse) UCarProto.AuthResponse.newBuilder().setVersion(a.g()).setAuthPk(ByteString.copyFrom(a5)).setAuthPkHmac(ByteString.copyFrom(a(a.b.a.i.a.a(a5, a2.c), a.b.a.i.a.a(a4, a2.g)))).setAgreementPk(ByteString.copyFrom(a3)).setAgreementPkSig(ByteString.copyFrom(a(g(a2.f27a), a3, a.b.a.i.a.a(a2.e, a4, a2.g)))).setRandom(ByteString.copyFrom(a4)).setResult(0).setWorkModes(i).setDayOrNightMode(i2).setScreenInfo(str).build();
                                    }
                                }
                                a.a.a.b.b(j, "normal connection failed");
                                byte[] bArr = a2.f27a;
                                a.a(bArr, g(bArr));
                                return m;
                            }
                        }
                        b c = c(a2.f27a);
                        if (c == null) {
                            a.a.a.b.c(j, "quick connection rejected, need normal connection");
                            return (UCarProto.AuthResponse) UCarProto.AuthResponse.newBuilder().setVersion(a.g()).setAuthPk(ByteString.EMPTY).setAuthPkHmac(ByteString.EMPTY).setAgreementPk(ByteString.EMPTY).setAgreementPkSig(ByteString.EMPTY).setRandom(ByteString.EMPTY).setResult(1).setWorkModes(i).setDayOrNightMode(i2).setScreenInfo(str).build();
                        }
                        b d = d();
                        if (d == null || d.f26a.equals(c.f26a) || a2.h) {
                            if (!a(a(a2.e, a2.g, a2.f, d(a(c.b))), a.b.a.i.a.a(a2.g, e()))) {
                                a.a.a.b.b(j, "quick connection failed");
                                byte[] bArr2 = a2.f27a;
                                a.a(bArr2, g(bArr2));
                                return m;
                            }
                            a.a.a.b.c(j, "quick connection succeeded");
                            f(a2.f27a);
                            return (UCarProto.AuthResponse) UCarProto.AuthResponse.newBuilder().setVersion(a.g()).setAuthPk(ByteString.EMPTY).setAuthPkHmac(ByteString.EMPTY).setAgreementPk(ByteString.copyFrom(a3)).setAgreementPkSig(ByteString.copyFrom(a(g(a2.f27a), a3, a.b.a.i.a.a(a2.e, a4, a2.g)))).setRandom(ByteString.copyFrom(a4)).setResult(0).setWorkModes(i).setDayOrNightMode(i2).setScreenInfo(str).build();
                        }
                        a.a.a.b.c(j, "the incoming phone wants quick connection but is not the last connected one, the phone needs to disconnect and ask for user confirmation");
                        return (UCarProto.AuthResponse) UCarProto.AuthResponse.newBuilder().setVersion(a.g()).setAuthPk(ByteString.EMPTY).setAuthPkHmac(ByteString.EMPTY).setAgreementPk(ByteString.EMPTY).setAgreementPkSig(ByteString.EMPTY).setRandom(ByteString.EMPTY).setResult(2).setWorkModes(i).setDayOrNightMode(i2).setScreenInfo(str).build();
                    }
                }
                return null;
            } catch (Exception e2) {
                e = e2;
                bVar = a2;
                a.a.a.b.b(j, "generateKeyNegotiationInfo Exception", e);
                if (bVar != null) {
                }
                return m;
            }
        } catch (Exception e3) {
            e = e3;
            a.a.a.b.b(j, "generateKeyNegotiationInfo Exception", e);
            if (bVar != null) {
                byte[] bArr3 = bVar.f27a;
                a.a(bArr3, g(bArr3));
            }
            return m;
        }
    }
}
