Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
The BSD License Copyright (c) 2010-2012 RIPE NCC All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the RIPE NCC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 package net.ripe.rpki.commons.crypto.cms;
 
 
 
 import static net.ripe.rpki.commons.crypto.cms.RpkiSignedObject.*;
 import static net.ripe.rpki.commons.validation.ValidationString.*;
 
 public abstract class RpkiSignedObjectParser {
 
     private byte[] encoded;
 
 
     private ASN1ObjectIdentifier contentType;
 
     private DateTime signingTime;
 
 
     public final void parse(String locationbyte[] encoded) { // NOPMD - ArrayIsStoredDirectly
         parse(ValidationResult.withLocation(location), encoded);
     }
 
     public void parse(ValidationResult resultbyte[] encoded) {
         this. = result;
         this. = encoded;
         parseCms();
     }
 
     protected byte[] getEncoded() {
         return ;
     }
 
     public ValidationResult getValidationResult() {
         return ;
     }
 
     protected X509ResourceCertificate getCertificate() {
         return ;
     }
 
        return ;
    }
    protected ASN1ObjectIdentifier getContentType() {
        return ;
    }
    protected DateTime getSigningTime() {
        return ;
    }
    public abstract void decodeContent(ASN1Encodable encoded);
    private void parseCms() {
        CMSSignedDataParser sp;
        try {
        } catch (CMSException e) {
            .rejectIfFalse(false);
            return;
        }
        if (!.hasFailures()) {
            parseContent(sp);
        }
        if (!.hasFailures()) {
            parseCmsCertificate(sp);
        }
        if (!.hasFailures()) {
            verifyCmsSigning(sp.getCertificate());
        }
    }
    private void parseContent(CMSSignedDataParser sp) {
         = sp.getSignedContent().getContentType();
        InputStream signedContentStream = sp.getSignedContent().getContentStream();
        ASN1InputStream asn1InputStream = new ASN1InputStream(signedContentStream);
        try {
            decodeContent(asn1InputStream.readObject());
        } catch (IOException e) {
            .rejectIfFalse(false);
            return;
        }
        try {
            .rejectIfFalse(asn1InputStream.readObject() == null);
            asn1InputStream.close();
        } catch (IOException e) {
            .rejectIfFalse(false);
        }
    }
    private void parseCmsCertificate(CMSSignedDataParser sp) {
        Collection<? extends Certificatecertificates = extractCertificate(sp);
        if (!.rejectIfNull(certificates)) {
            return;
        }
        if (!.rejectIfFalse(certificates.size() == 1, )) {
            return;
        }
        if (!.rejectIfFalse(certificates.iterator().next() instanceof X509Certificate)) {
            return;
        }
         = parseCertificate(certificates.iterator().next());
            return;
        }
    }
    private X509ResourceCertificate parseCertificate(Certificate certificate) {
        try {
            X509Certificate x509certificate = (X509Certificatecertificate;
            X509ResourceCertificateParser parser = new X509ResourceCertificateParser();
            parser.parse(x509certificate.getEncoded());
            return parser.isSuccess() ? parser.getCertificate() : null;
        } catch (CertificateEncodingException e) {
            throw new AbstractX509CertificateWrapperException("cannot parse already decoded X509 certificate: " + ee);
        }
    }
    private Collection<? extends CertificateextractCertificate(CMSSignedDataParser sp) {
        try {
            return BouncyCastleUtil.extractCertificates(sp);
        } catch (CMSException e) {
            return null;
        } catch (StoreException e) {
            return null;
        } catch (CertificateException e) {
            return null;
        }
    }
    private void verifyCmsSigning(CMSSignedDataParser spX509Certificate certificate) {
        // Note: validationResult field is updated by methods used here.
        SignerInformation signer = extractSingleCmsSigner(sp);
        if (signer == null) {
            return;
        }
        if (!verifySigner(signercertificate)) {
            return;
        }
        if (!verifyAndStoreSigningTime(signer)) {
            return;
        }
        verifySignature(certificatesigner);
    }
        SignerInformationStore signerStore = getSignerStore(sp);
        if (!.rejectIfNull(signerStore)) {
            return null;
        }
        Collection<?> signers = signerStore.getSigners();
        if (.rejectIfFalse(signers.size() == 1, )) {
            return (SignerInformationsigners.iterator().next();
        } else {
            return null;
        }
    }
        try {
            return sp.getSignerInfos();
        } catch (CMSException e) {
            return null// Caller will validate that the SignerInformationStore is not null
        } catch (RuntimeException e) {
            return null// Caller will validate that the SignerInformationStore is not null
        }
    }
    private boolean verifySigner(SignerInformation signerX509Certificate certificate) {
            return false;
        }
        SignerId signerId = signer.getSID();
        try {
            .rejectIfFalse(signerId.match(new JcaX509CertificateHolder(certificate)), );
        } catch (CertificateEncodingException e) {
            throw new AbstractX509CertificateWrapperException(e);
        }
        return true;
    }
    private boolean verifyAndStoreSigningTime(SignerInformation signer) {
        Attribute signingTimeAttibute = signer.getSignedAttributes().get(.);
        if (!.rejectIfNull(signingTimeAttibute)) {
            return false;
        }
        if (!.rejectIfFalse(signingTimeAttibute.getAttrValues().size() == 1, )) {
            return false;
        }
        Time signingTimeDate = Time.getInstance(signingTimeAttibute.getAttrValues().getObjectAt(0));
         = new DateTime(signingTimeDate.getDate().getTime(), .);
        return true;
    }
    private void verifySignature(X509Certificate certificateSignerInformation signer) {
        String errorMessage = null;
        try {
            /*
			 * Use the public key for the "verifier" not the certificate, because otherwise
             * BC will reject the CMS if the signingTime is outside of the EE certificate validity
             * time. This happens occasionally and is no ground to reject according to standards:
             * http://tools.ietf.org/html/rfc6488#section-2.1.6.4.3
             */
        } catch (OperatorCreationException e) {
            errorMessage = String.valueOf(e.getMessage());
        } catch (CMSException e) {
            errorMessage = String.valueOf(e.getMessage());
        }
        if (errorMessage != null) {
            .rejectIfFalse(falseerrorMessage);
        }
    }
New to GrepCode? Check out our FAQ X