Search in sources :

Example 1 with TSTInfo

use of com.github.zhenwei.pkix.util.asn1.tsp.TSTInfo in project gdmatrix by gdmatrix.

the class P7MUtils method recoverTSTInfo.

public static TSTInfo recoverTSTInfo(ContentInfo contentInfo) throws IOException {
    SignedData sd = SignedData.getInstance(contentInfo.getContent());
    ASN1Encodable content = sd.getEncapContentInfo().getContent();
    // TSTInfo tstInfo = new TSTInfo((ASN1Sequence)
    // new ASN1InputStream(((DEROctetString)content).getOctets()).readObject());
    TSTInfo tstInfo = TSTInfo.getInstance(((ASN1OctetString) content).getOctets());
    return tstInfo;
}
Also used : TSTInfo(org.bouncycastle.asn1.tsp.TSTInfo) SignedData(org.bouncycastle.asn1.cms.SignedData) CMSSignedData(org.bouncycastle.cms.CMSSignedData) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable)

Example 2 with TSTInfo

use of com.github.zhenwei.pkix.util.asn1.tsp.TSTInfo in project gdmatrix by gdmatrix.

the class CMSUtils method recoverTSTInfo.

public static TSTInfo recoverTSTInfo(ContentInfo contentInfo) throws IOException {
    SignedData sd = SignedData.getInstance(contentInfo.getContent());
    ASN1Encodable content = sd.getEncapContentInfo().getContent();
    // TSTInfo tstInfo = new TSTInfo((ASN1Sequence)
    // new ASN1InputStream(((DEROctetString)content).getOctets()).readObject());
    TSTInfo tstInfo = TSTInfo.getInstance(((ASN1OctetString) content).getOctets());
    return tstInfo;
}
Also used : TSTInfo(org.bouncycastle.asn1.tsp.TSTInfo) SignedData(org.bouncycastle.asn1.cms.SignedData) CMSSignedData(org.bouncycastle.cms.CMSSignedData) ASN1Encodable(org.bouncycastle.asn1.ASN1Encodable)

Example 3 with TSTInfo

use of com.github.zhenwei.pkix.util.asn1.tsp.TSTInfo in project LinLong-Java by zhenwei1108.

the class TSPUtil method getSignatureTimestamps.

/**
 * Fetches the signature time-stamp attributes from a SignerInformation object. Checks that the
 * MessageImprint for each time-stamp matches the signature field. (see RFC 3161 Appendix A).
 *
 * @param signerInfo      a SignerInformation to search for time-stamps
 * @param digCalcProvider provider for digest calculators
 * @return a collection of TimeStampToken objects
 * @throws TSPValidationException
 */
public static Collection getSignatureTimestamps(SignerInformation signerInfo, DigestCalculatorProvider digCalcProvider) throws TSPValidationException {
    List timestamps = new ArrayList();
    AttributeTable unsignedAttrs = signerInfo.getUnsignedAttributes();
    if (unsignedAttrs != null) {
        ASN1EncodableVector allTSAttrs = unsignedAttrs.getAll(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken);
        for (int i = 0; i < allTSAttrs.size(); ++i) {
            Attribute tsAttr = (Attribute) allTSAttrs.get(i);
            ASN1Set tsAttrValues = tsAttr.getAttrValues();
            for (int j = 0; j < tsAttrValues.size(); ++j) {
                try {
                    ContentInfo contentInfo = ContentInfo.getInstance(tsAttrValues.getObjectAt(j));
                    TimeStampToken timeStampToken = new TimeStampToken(contentInfo);
                    TimeStampTokenInfo tstInfo = timeStampToken.getTimeStampInfo();
                    DigestCalculator digCalc = digCalcProvider.get(tstInfo.getHashAlgorithm());
                    OutputStream dOut = digCalc.getOutputStream();
                    dOut.write(signerInfo.getSignature());
                    dOut.close();
                    byte[] expectedDigest = digCalc.getDigest();
                    if (!Arrays.constantTimeAreEqual(expectedDigest, tstInfo.getMessageImprintDigest())) {
                        throw new TSPValidationException("Incorrect digest in message imprint");
                    }
                    timestamps.add(timeStampToken);
                } catch (OperatorCreationException e) {
                    throw new TSPValidationException("Unknown hash algorithm specified in timestamp");
                } catch (Exception e) {
                    throw new TSPValidationException("Timestamp could not be parsed");
                }
            }
        }
    }
    return timestamps;
}
Also used : Attribute(com.github.zhenwei.pkix.util.asn1.cms.Attribute) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) AttributeTable(com.github.zhenwei.pkix.util.asn1.cms.AttributeTable) DigestCalculator(com.github.zhenwei.pkix.operator.DigestCalculator) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException) IOException(java.io.IOException) ASN1Set(com.github.zhenwei.core.asn1.ASN1Set) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) ArrayList(java.util.ArrayList) List(java.util.List) OperatorCreationException(com.github.zhenwei.pkix.operator.OperatorCreationException)

Example 4 with TSTInfo

use of com.github.zhenwei.pkix.util.asn1.tsp.TSTInfo in project LinLong-Java by zhenwei1108.

the class TimeStampResponse method validate.

/**
 * Check this response against to see if it a well formed response for the passed in request.
 * Validation will include checking the time stamp token if the response status is GRANTED or
 * GRANTED_WITH_MODS.
 *
 * @param request the request to be checked against
 * @throws TSPException if the request can not match this response.
 */
public void validate(TimeStampRequest request) throws TSPException {
    TimeStampToken tok = this.getTimeStampToken();
    if (tok != null) {
        TimeStampTokenInfo tstInfo = tok.getTimeStampInfo();
        if (request.getNonce() != null && !request.getNonce().equals(tstInfo.getNonce())) {
            throw new TSPValidationException("response contains wrong nonce value.");
        }
        if (this.getStatus() != PKIStatus.GRANTED && this.getStatus() != PKIStatus.GRANTED_WITH_MODS) {
            throw new TSPValidationException("time stamp token found in failed request.");
        }
        if (!Arrays.constantTimeAreEqual(request.getMessageImprintDigest(), tstInfo.getMessageImprintDigest())) {
            throw new TSPValidationException("response for different message imprint digest.");
        }
        if (!tstInfo.getMessageImprintAlgOID().equals(request.getMessageImprintAlgOID())) {
            throw new TSPValidationException("response for different message imprint algorithm.");
        }
        Attribute scV1 = tok.getSignedAttributes().get(PKCSObjectIdentifiers.id_aa_signingCertificate);
        Attribute scV2 = tok.getSignedAttributes().get(PKCSObjectIdentifiers.id_aa_signingCertificateV2);
        if (scV1 == null && scV2 == null) {
            throw new TSPValidationException("no signing certificate attribute present.");
        }
        if (scV1 != null && scV2 != null) {
        /*
         * RFC 5035 5.4. If both attributes exist in a single message,
         * they are independently evaluated.
         */
        }
        if (request.getReqPolicy() != null && !request.getReqPolicy().equals(tstInfo.getPolicy())) {
            throw new TSPValidationException("TSA policy wrong for request.");
        }
    } else if (this.getStatus() == PKIStatus.GRANTED || this.getStatus() == PKIStatus.GRANTED_WITH_MODS) {
        throw new TSPValidationException("no time stamp token found and one expected.");
    }
}
Also used : Attribute(com.github.zhenwei.pkix.util.asn1.cms.Attribute)

Example 5 with TSTInfo

use of com.github.zhenwei.pkix.util.asn1.tsp.TSTInfo in project LinLong-Java by zhenwei1108.

the class ERSArchiveTimeStampGenerator method generateArchiveTimeStamp.

public ERSArchiveTimeStamp generateArchiveTimeStamp(TimeStampResponse tspResponse) throws TSPException, ERSException {
    PartialHashtree[] reducedHashTree = getPartialHashtrees();
    byte[] rootHash = rootNodeCalculator.computeRootHash(digCalc, reducedHashTree);
    TSTInfo tstInfo = tspResponse.getTimeStampToken().getTimeStampInfo().toASN1Structure();
    if (!tstInfo.getMessageImprint().getHashAlgorithm().equals(digCalc.getAlgorithmIdentifier())) {
        throw new ERSException("time stamp imprint for wrong algorithm");
    }
    if (!Arrays.areEqual(tstInfo.getMessageImprint().getHashedMessage(), rootHash)) {
        throw new ERSException("time stamp imprint for wrong root hash");
    }
    ArchiveTimeStamp ats;
    if (reducedHashTree.length == 1) {
        // just include the TimeStamp
        ats = new ArchiveTimeStamp(null, null, tspResponse.getTimeStampToken().toCMSSignedData().toASN1Structure());
    } else {
        ats = new ArchiveTimeStamp(digCalc.getAlgorithmIdentifier(), reducedHashTree, tspResponse.getTimeStampToken().toCMSSignedData().toASN1Structure());
    }
    return new ERSArchiveTimeStamp(ats, digCalc, rootNodeCalculator);
}
Also used : TSTInfo(com.github.zhenwei.pkix.util.asn1.tsp.TSTInfo) ArchiveTimeStamp(com.github.zhenwei.pkix.util.asn1.tsp.ArchiveTimeStamp) PartialHashtree(com.github.zhenwei.pkix.util.asn1.tsp.PartialHashtree)

Aggregations

ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)4 SignedData (org.bouncycastle.asn1.cms.SignedData)4 TSTInfo (org.bouncycastle.asn1.tsp.TSTInfo)4 CMSSignedData (org.bouncycastle.cms.CMSSignedData)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Attribute (com.github.zhenwei.pkix.util.asn1.cms.Attribute)2 TSTInfo (com.github.zhenwei.pkix.util.asn1.tsp.TSTInfo)2 IOException (java.io.IOException)2 X509Certificate (java.security.cert.X509Certificate)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Hashtable (java.util.Hashtable)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)2 ASN1UTCTime (org.bouncycastle.asn1.ASN1UTCTime)2 DEROctetString (org.bouncycastle.asn1.DEROctetString)2 Attribute (org.bouncycastle.asn1.cms.Attribute)2 AttributeTable (org.bouncycastle.asn1.cms.AttributeTable)2 ContentInfo (org.bouncycastle.asn1.cms.ContentInfo)2 X509CertificateHolder (org.bouncycastle.cert.X509CertificateHolder)2