package defpackage;

import com.hierynomus.ntlm.NtlmException;
import com.hierynomus.protocol.commons.buffer.Buffer$BufferException;
import com.hierynomus.security.SecurityException;
import com.hierynomus.smbj.common.SMBRuntimeException;
import com.hierynomus.spnego.SpnegoException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Random;

/* renamed from: ᵕᵗᵦ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C2298 implements InterfaceC3968 {

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public static final InterfaceC0567 f8985 = AbstractC1385.m4255(C2298.class);

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public static final C3760 f8986 = new C3760("1.3.6.1.4.1.311.2.2.10");

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public boolean f8987;

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public InterfaceC2894 f8988;

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public Random f8989;

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public boolean f8990;

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public static byte[] m6000(C4350 c4350) throws Exception {
        C4330 c4330 = new C4330();
        c4350.m9328(c4330);
        if (c4350.f16279) {
            byte[] bArr = c4350.f16287;
            c4330.mo5106(bArr.length, bArr);
        }
        byte[] bArr2 = c4350.f16281;
        c4330.mo5106(bArr2.length, bArr2);
        byte[] bArr3 = c4350.f16280;
        c4330.mo5106(bArr3.length, bArr3);
        byte[] bArr4 = c4350.f16283;
        c4330.mo5106(bArr4.length, bArr4);
        byte[] bArr5 = c4350.f16284;
        c4330.mo5106(bArr5.length, bArr5);
        byte[] bArr6 = c4350.f16286;
        c4330.mo5106(bArr6.length, bArr6);
        byte[] bArr7 = c4350.f16282;
        c4330.mo5106(bArr7.length, bArr7);
        byte[] bArrM9345 = c4330.m9345();
        C4330 c43302 = new C4330();
        ArrayList arrayList = new ArrayList();
        try {
            int length = bArrM9345.length;
            EnumC4215 enumC4215 = EnumC4215.f15802;
            EnumC4278 enumC4278 = EnumC4278.f16052;
            if (length > 0) {
                arrayList.add(new C3647(AbstractC1728.m4925(enumC4215, 2).m4926(enumC4278), (AbstractC2943) new C2105(bArrM9345), true));
            }
            C3647 c3647 = new C3647(AbstractC1728.m4925(enumC4215, 1).m4926(enumC4278), (AbstractC2943) new C5443(arrayList), true);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            C3692 c3692 = new C3692(new ˊᵻ(16), byteArrayOutputStream);
            try {
                c3692.m8285(c3647);
                C0287.m1623(c3692, null);
                byte[] byteArray = byteArrayOutputStream.toByteArray();
                c43302.mo5106(byteArray.length, byteArray);
                return c43302.m9345();
            } finally {
            }
        } catch (IOException e) {
            throw new SpnegoException("Could not write NegTokenTarg to buffer", e);
        }
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public static byte[] m6001(C1268 c1268) throws SpnegoException {
        C0287 c0287 = new C0287();
        ((ArrayList) c0287.f1886).add(f8986);
        C4330 c4330 = new C4330();
        c4330.m9339("NTLMSSP\u0000", AbstractC5005.f18585);
        c4330.m9348(1L);
        c4330.m9348(c1268.f5263);
        c4330.m9338(0);
        c4330.m9338(0);
        c4330.m9348(0L);
        c4330.m9338(0);
        c4330.m9338(0);
        c4330.m9348(0L);
        c0287.f1888 = c4330.m9345();
        C4330 c43302 = new C4330();
        try {
            ArrayList arrayList = new ArrayList();
            ArrayList arrayList2 = (ArrayList) c0287.f1886;
            int size = arrayList2.size();
            EnumC4278 enumC4278 = EnumC4278.f16052;
            EnumC4215 enumC4215 = EnumC4215.f15802;
            if (size > 0) {
                arrayList.add(new C3647(AbstractC1728.m4925(enumC4215, 0).m4926(enumC4278), (AbstractC2943) new C5443(new ArrayList(arrayList2)), true));
            }
            byte[] bArr = c0287.f1888;
            if (bArr != null && bArr.length > 0) {
                arrayList.add(new C3647(AbstractC1728.m4925(enumC4215, 2).m4926(enumC4278), (AbstractC2943) new C2105(c0287.f1888), true));
            }
            c0287.mo1624(c43302, new C5443(arrayList));
            return c43302.m9345();
        } catch (IOException e) {
            throw new SpnegoException("Unable to write NegTokenInit", e);
        }
    }

    @Override // defpackage.InterfaceC3968
    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final C4351 mo6002(ʽᵡ r26, byte[] bArr) throws Exception {
        byte[] bArr2;
        try {
            C4351 c4351 = new C4351();
            if (this.f8990) {
                return null;
            }
            boolean z = this.f8987;
            InterfaceC0567 interfaceC0567 = f8985;
            int i = 1;
            if (!z) {
                interfaceC0567.mo2409((String) r26.ᵢˇᵭ, "Initialized Authentication of {} using NTLM");
                C1268 c1268 = new C1268();
                c1268.f5263 = C1268.f5262;
                this.f8987 = true;
                c4351.f16290 = m6001(c1268);
                return c4351;
            }
            interfaceC0567.mo2409(ˁـᵛ.ᵨᵳʝ(bArr), "Received token: {}");
            Random random = this.f8989;
            InterfaceC2894 interfaceC2894 = this.f8988;
            C0549 c0549 = new C0549(random, interfaceC2894);
            C0287 c0287 = new C0287(i, "NegTokenTarg");
            c0287.m1625(bArr);
            C4636 c4636 = new C4636();
            HashMap map = new HashMap();
            c4636.f17170 = map;
            try {
                c4636.m9822(new C4330(c0287.f1888, true, C3253.f12249));
                interfaceC0567.mo2409(c4636.f17176, "Received NTLM challenge from: {}");
                Object obj = map.get(EnumC3441.f12872);
                if (obj != null) {
                    String.valueOf(obj);
                }
                byte[] bArr3 = c4636.f17174;
                char[] cArr = (char[]) r26.ᵾᵬ;
                String str = (String) r26.ᵢˇᵭ;
                String strValueOf = String.valueOf(cArr);
                String str2 = (String) r26.ᵿʗᵝʽ;
                byte[] bytes = strValueOf == null ? new byte[0] : strValueOf.getBytes(C0549.f2851);
                try {
                    InterfaceC4769 interfaceC4769M7061 = interfaceC2894.m7061();
                    interfaceC4769M7061.update(bytes);
                    byte[] bArrMo7276 = interfaceC4769M7061.mo7276();
                    String upperCase = str.toUpperCase();
                    byte[] bArrM2345 = c0549.m2345(bArrMo7276, upperCase == null ? new byte[0] : upperCase.getBytes(C0549.f2851), str2 == null ? new byte[0] : str2.getBytes(C0549.f2851));
                    byte[] bArrM2344 = c0549.m2344(c4636.f17178);
                    byte[] bArrM23452 = c0549.m2345(bArrM2345, bArr3, bArrM2344);
                    byte[] bArr4 = new byte[bArrM23452.length + bArrM2344.length];
                    System.arraycopy(bArrM23452, 0, bArr4, 0, bArrM23452.length);
                    System.arraycopy(bArrM2344, 0, bArr4, bArrM23452.length, bArrM2344.length);
                    byte[] bArrM23453 = c0549.m2345(bArrM2345, Arrays.copyOfRange(bArr4, 0, 16));
                    EnumSet enumSet = c4636.f17175;
                    if (enumSet.contains(EnumC4862.f18070) && (enumSet.contains(EnumC4862.f18067) || enumSet.contains(EnumC4862.f18059) || enumSet.contains(EnumC4862.f18062))) {
                        byte[] bArr5 = new byte[16];
                        this.f8989.nextBytes(bArr5);
                        try {
                            C1470 c1470M7063 = interfaceC2894.m7063();
                            c1470M7063.m4377(bArrM23453);
                            byte[] bArr6 = new byte[16];
                            try {
                                c1470M7063.m4376(c1470M7063.m4375(16, bArr5, bArr6), bArr6);
                                c4351.f16288 = bArr5;
                                bArr2 = bArr6;
                            } catch (SecurityException e) {
                                throw new NtlmException(e);
                            }
                        } catch (SecurityException e2) {
                            throw new NtlmException(e2);
                        }
                    } else {
                        c4351.f16288 = bArrM23453;
                        bArr2 = bArrM23453;
                    }
                    this.f8990 = true;
                    Object obj2 = map.get(EnumC3441.f12874);
                    if (!(obj2 instanceof Long) || (((Long) obj2).longValue() & 2) <= 0) {
                        c4351.f16290 = m6000(new C4350(new byte[0], bArr4, str, (String) r26.ᵿʗᵝʽ, bArr2, ˉᵜˆ.ˀᵗᵝᵭ(enumSet), false));
                        return c4351;
                    }
                    C4350 c4350 = new C4350(new byte[0], bArr4, str, (String) r26.ᵿʗᵝʽ, bArr2, ˉᵜˆ.ˀᵗᵝᵭ(enumSet), true);
                    C4330 c4330 = new C4330();
                    byte[] bArr7 = c0287.f1888;
                    c4330.mo5106(bArr7.length, bArr7);
                    byte[] bArr8 = c4636.f17174;
                    c4330.mo5106(bArr8.length, bArr8);
                    c4350.m9328(c4330);
                    c4350.f16287 = c0549.m2345(bArrM23453, c4330.m9345());
                    c4351.f16290 = m6000(c4350);
                    return c4351;
                } catch (SecurityException e3) {
                    throw new NtlmException(e3);
                }
            } catch (Buffer$BufferException e4) {
                throw new IOException(e4);
            }
        } catch (SpnegoException e5) {
            throw new SMBRuntimeException(e5);
        }
    }

    @Override // defpackage.InterfaceC3968
    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final boolean mo6003(ʽᵡ r1) {
        return r1.getClass().equals(ʽᵡ.class);
    }

    @Override // defpackage.InterfaceC3968
    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final void mo6004(C3900 c3900) {
        this.f8988 = c3900.f14540;
        this.f8989 = c3900.f14552;
    }
}
