package org.bouncycastle.asn1; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; /* loaded from: classes6.dex */ public class ASN1StreamParser { private final InputStream _in; private final int _limit; private final byte[][] tmpBuffers; /* JADX INFO: Access modifiers changed from: package-private */ public ASN1EncodableVector readVector() throws IOException { ASN1EncodableVector aSN1EncodableVector = new ASN1EncodableVector(); while (true) { ASN1Encodable readObject = readObject(); if (readObject == null) { return aSN1EncodableVector; } aSN1EncodableVector.add(readObject instanceof InMemoryRepresentable ? ((InMemoryRepresentable) readObject).getLoadedObject() : readObject.toASN1Primitive()); } } /* JADX INFO: Access modifiers changed from: package-private */ public ASN1Primitive readTaggedObject(boolean z, int i) throws IOException { if (!z) { return new DERTaggedObject(false, i, new DEROctetString(((DefiniteLengthInputStream) this._in).toByteArray())); } ASN1EncodableVector readVector = readVector(); return this._in instanceof IndefiniteLengthInputStream ? readVector.size() == 1 ? new BERTaggedObject(true, i, readVector.get(0)) : new BERTaggedObject(false, i, BERFactory.createSequence(readVector)) : readVector.size() == 1 ? new DERTaggedObject(true, i, readVector.get(0)) : new DERTaggedObject(false, i, DERFactory.createSequence(readVector)); } public ASN1Encodable readObject() throws IOException { int read = this._in.read(); if (read == -1) { return null; } set00Check(false); int readTagNumber = ASN1InputStream.readTagNumber(this._in, read); boolean z = (read & 32) != 0; int readLength = ASN1InputStream.readLength(this._in, this._limit); if (readLength < 0) { if (!z) { throw new IOException("indefinite-length primitive encoding encountered"); } ASN1StreamParser aSN1StreamParser = new ASN1StreamParser(new IndefiniteLengthInputStream(this._in, this._limit), this._limit); return (read & 64) != 0 ? new BERApplicationSpecificParser(readTagNumber, aSN1StreamParser) : (read & 128) != 0 ? new BERTaggedObjectParser(true, readTagNumber, aSN1StreamParser) : aSN1StreamParser.readIndef(readTagNumber); } DefiniteLengthInputStream definiteLengthInputStream = new DefiniteLengthInputStream(this._in, readLength); if ((read & 64) != 0) { return new DERApplicationSpecific(z, readTagNumber, definiteLengthInputStream.toByteArray()); } if ((read & 128) != 0) { return new BERTaggedObjectParser(z, readTagNumber, new ASN1StreamParser(definiteLengthInputStream)); } if (!z) { if (readTagNumber == 4) { return new DEROctetStringParser(definiteLengthInputStream); } try { return ASN1InputStream.createPrimitiveDERObject(readTagNumber, definiteLengthInputStream, this.tmpBuffers); } catch (IllegalArgumentException e) { throw new ASN1Exception("corrupted stream detected", e); } } if (readTagNumber == 4) { return new BEROctetStringParser(new ASN1StreamParser(definiteLengthInputStream)); } if (readTagNumber == 8) { return new DERExternalParser(new ASN1StreamParser(definiteLengthInputStream)); } if (readTagNumber == 16) { return new DERSequenceParser(new ASN1StreamParser(definiteLengthInputStream)); } if (readTagNumber == 17) { return new DERSetParser(new ASN1StreamParser(definiteLengthInputStream)); } StringBuilder sb = new StringBuilder("unknown tag "); sb.append(readTagNumber); sb.append(" encountered"); throw new IOException(sb.toString()); } ASN1Encodable readIndef(int i) throws IOException { if (i == 4) { return new BEROctetStringParser(this); } if (i == 8) { return new DERExternalParser(this); } if (i == 16) { return new BERSequenceParser(this); } if (i == 17) { return new BERSetParser(this); } StringBuilder sb = new StringBuilder("unknown BER object encountered: 0x"); sb.append(Integer.toHexString(i)); throw new ASN1Exception(sb.toString()); } /* JADX INFO: Access modifiers changed from: package-private */ public ASN1Encodable readImplicit(boolean z, int i) throws IOException { InputStream inputStream = this._in; if (inputStream instanceof IndefiniteLengthInputStream) { if (z) { return readIndef(i); } throw new IOException("indefinite-length primitive encoding encountered"); } if (z) { if (i == 4) { return new BEROctetStringParser(this); } if (i == 16) { return new DERSequenceParser(this); } if (i == 17) { return new DERSetParser(this); } } else { if (i == 4) { return new DEROctetStringParser((DefiniteLengthInputStream) inputStream); } if (i == 16) { throw new ASN1Exception("sets must use constructed encoding (see X.690 8.11.1/8.12.1)"); } if (i == 17) { throw new ASN1Exception("sequences must use constructed encoding (see X.690 8.9.1/8.10.1)"); } } throw new ASN1Exception("implicit tagging not implemented"); } private void set00Check(boolean z) { InputStream inputStream = this._in; if (inputStream instanceof IndefiniteLengthInputStream) { ((IndefiniteLengthInputStream) inputStream).setEofOn00(z); } } public ASN1StreamParser(byte[] bArr) { this(new ByteArrayInputStream(bArr), bArr.length); } public ASN1StreamParser(InputStream inputStream, int i) { this._in = inputStream; this._limit = i; this.tmpBuffers = new byte[11]; } public ASN1StreamParser(InputStream inputStream) { this(inputStream, StreamUtil.findLimit(inputStream)); } }