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

package org.bouncycastle.tsp;

import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.cms.Attribute;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.cmp.PKIFailureInfo;
import org.bouncycastle.asn1.cmp.PKIFreeText;
import org.bouncycastle.asn1.DLSequence;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.ASN1InputStream;
import java.io.InputStream;
import java.io.IOException;
import org.bouncycastle.asn1.tsp.TimeStampResp;

public class TimeStampResponse
{
    private final TimeStampResp resp;
    private final TimeStampToken timeStampToken;
    
    private static TimeStampResp parseTimeStampResp(final byte[] array) throws IOException, TSPException {
        try {
            return TimeStampResp.getInstance(array);
        }
        catch (final IllegalArgumentException obj) {
            throw new TSPException("malformed timestamp response: " + obj, obj);
        }
        catch (final ClassCastException obj2) {
            throw new TSPException("malformed timestamp response: " + obj2, obj2);
        }
    }
    
    private static TimeStampResp parseTimeStampResp(final InputStream inputStream) throws IOException, TSPException {
        try {
            return TimeStampResp.getInstance(new ASN1InputStream(inputStream).readObject());
        }
        catch (final IllegalArgumentException obj) {
            throw new TSPException("malformed timestamp response: " + obj, obj);
        }
        catch (final ClassCastException obj2) {
            throw new TSPException("malformed timestamp response: " + obj2, obj2);
        }
    }
    
    public TimeStampResponse(final TimeStampResp resp) throws TSPException, IOException {
        this.resp = resp;
        final ContentInfo timeStampToken = resp.getTimeStampToken();
        this.timeStampToken = ((timeStampToken == null) ? null : new TimeStampToken(timeStampToken));
    }
    
    public TimeStampResponse(final byte[] array) throws TSPException, IOException {
        this(parseTimeStampResp(array));
    }
    
    public TimeStampResponse(final InputStream inputStream) throws TSPException, IOException {
        this(parseTimeStampResp(inputStream));
    }
    
    TimeStampResponse(final DLSequence dlSequence) throws TSPException, IOException {
        try {
            this.resp = TimeStampResp.getInstance(dlSequence);
            this.timeStampToken = new TimeStampToken(ContentInfo.getInstance(dlSequence.getObjectAt(1)));
        }
        catch (final IllegalArgumentException obj) {
            throw new TSPException("malformed timestamp response: " + obj, obj);
        }
        catch (final ClassCastException obj2) {
            throw new TSPException("malformed timestamp response: " + obj2, obj2);
        }
    }
    
    public int getStatus() {
        return this.resp.getStatus().getStatusObject().intValueExact();
    }
    
    public String getStatusString() {
        if (this.resp.getStatus().getStatusString() == null) {
            return null;
        }
        final StringBuilder sb = new StringBuilder();
        final PKIFreeText statusString = this.resp.getStatus().getStatusString();
        for (int i = 0; i != statusString.size(); ++i) {
            sb.append(statusString.getStringAtUTF8(i).getString());
        }
        return sb.toString();
    }
    
    public PKIFailureInfo getFailInfo() {
        if (this.resp.getStatus().getFailInfo() != null) {
            return new PKIFailureInfo(this.resp.getStatus().getFailInfo());
        }
        return null;
    }
    
    public TimeStampToken getTimeStampToken() {
        return this.timeStampToken;
    }
    
    public void validate(final TimeStampRequest timeStampRequest) throws TSPException {
        final TimeStampToken timeStampToken = this.getTimeStampToken();
        if (timeStampToken != null) {
            final TimeStampTokenInfo timeStampInfo = timeStampToken.getTimeStampInfo();
            if (timeStampRequest.getNonce() != null && !timeStampRequest.getNonce().equals(timeStampInfo.getNonce())) {
                throw new TSPValidationException("response contains wrong nonce value.");
            }
            if (this.getStatus() != 0 && this.getStatus() != 1) {
                throw new TSPValidationException("time stamp token found in failed request.");
            }
            if (!timeStampInfo.getMessageImprintAlgOID().equals(timeStampRequest.getMessageImprintAlgOID())) {
                throw new TSPValidationException("response for different message imprint algorithm.");
            }
            if (!Arrays.constantTimeAreEqual(timeStampRequest.getMessageImprintDigest(), timeStampInfo.getMessageImprintDigest())) {
                throw new TSPValidationException("response for different message imprint digest.");
            }
            final Attribute value = timeStampToken.getSignedAttributes().get(PKCSObjectIdentifiers.id_aa_signingCertificate);
            final Attribute value2 = timeStampToken.getSignedAttributes().get(PKCSObjectIdentifiers.id_aa_signingCertificateV2);
            if (value == null && value2 == null) {
                throw new TSPValidationException("no signing certificate attribute present.");
            }
            if (value == null || value2 != null) {}
            if (timeStampRequest.getReqPolicy() != null && !timeStampRequest.getReqPolicy().equals(timeStampInfo.getPolicy())) {
                throw new TSPValidationException("TSA policy wrong for request.");
            }
        }
        else if (this.getStatus() == 0 || this.getStatus() == 1) {
            throw new TSPValidationException("no time stamp token found and one expected.");
        }
    }
    
    public byte[] getEncoded() throws IOException {
        return this.resp.getEncoded();
    }
    
    public byte[] getEncoded(final String anObject) throws IOException {
        ASN1Object resp = this.resp;
        if ("DL".equals(anObject)) {
            resp = ((this.timeStampToken == null) ? new DLSequence(this.resp.getStatus()) : new DLSequence(this.resp.getStatus(), this.timeStampToken.toCMSSignedData().toASN1Structure()));
        }
        return resp.getEncoded(anObject);
    }
}
