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.roa;
 
 
 import java.util.List;
 
 import static net.ripe.rpki.commons.crypto.util.Asn1Util.*;
 import static net.ripe.rpki.commons.validation.ValidationString.*;
 
 public class RoaCmsParser extends RpkiSignedObjectParser {
 
     private Asn asn;
 
     private List<RoaPrefixprefixes = new ArrayList<RoaPrefix>();
 
     @Override
     public void parse(ValidationResult resultbyte[] encoded) {
         super.parse(resultencoded);
         validateRoa();
     }
 
     public boolean isSuccess() {
         return !getValidationResult().hasFailureForCurrentLocation();
     }
 
     public RoaCms getRoaCms() {
         if (!isSuccess()) {
             throw new IllegalArgumentException("ROA validation failed: " + getValidationResult().getFailuresForCurrentLocation());
         }
 
         return new RoaCms(cmsObjectInfo);
     }
 
     private void validateRoa() {
         ValidationResult validationResult = getValidationResult();
 
         if (!validationResult.rejectIfFalse(getContentType() != null)) {
             return;
         }
         if (!validationResult.rejectIfFalse(..equals(getContentType()), getContentType().toString())) {
             return;
         }
 
         IpResourceSet roaPrefixes = new IpResourceSet();
         for (RoaPrefix prefix : Collections.unmodifiableList()) {
             roaPrefixes.add(prefix.getPrefix());
         }
         try {
             validationResult.rejectIfFalse(getResourceCertificate().getResources().contains(roaPrefixes), );
         } catch (Exception e) {
             validationResult.rejectIfFalse(false);
         }
     }
 
        expect(derASN1Sequence.class);
        ASN1Sequence seq = (ASN1Sequenceder;
        ValidationResult validationResult = getValidationResult();
        if (!validationResult.rejectIfFalse((seq.size() > 0) && (seq.size() <= 2), )) {
            throw new IllegalArgumentException("ip address family sequence length invalid");
        }
        IpRange prefix = parseIpAddressAsPrefix(typeseq.getObjectAt(0));
        BigInteger maxLength = null;
        if (seq.size() > 1) {
            maxLength = expect(seq.getObjectAt(1), ASN1Integer.class).getValue();
            if (!validationResult.rejectIfFalse((maxLength.compareTo(.) >= 0) && (maxLength.compareTo(BigInteger.valueOf(.)) <= 0), )) {
                throw new IllegalArgumentException("prefix max length invalid");
            }
        }
        return new RoaPrefix(prefixmaxLength == null ? null : maxLength.intValue());
    }
        ValidationResult validationResult = getValidationResult();
        try {
            ASN1Sequence seq = expect(derASN1Sequence.class);
            if (!validationResult.rejectIfTrue(seq.size() == 3, seq.getObjectAt(0).toString())) {
                // eContent seems to contain non-standard version (default 0 is omitted in structure)
                return;
            }
            if (!validationResult.rejectIfFalse(seq.size() == 2, )) {
                return;
            }
             = Asn1Util.parseAsId(seq.getObjectAt(0));
             = parseRoaIpAddressFamilySequence(seq.getObjectAt(1));
        } catch (IllegalArgumentException ex) {
            validationResult.error();
        }
    }
    void parseRoaIpAddressFamily(List<RoaPrefixroaPrefixListASN1Encodable der) {
        expect(derASN1Sequence.class);
        ASN1Sequence seq = (ASN1Sequenceder;
        ValidationResult validationResult = getValidationResult();
        if (seq.size() != 2) {
            validationResult.rejectIfFalse(false);
            throw new IllegalArgumentException("ROA sequence does not contain address family and addresses");
        }
        AddressFamily addressFamily = AddressFamily.fromDer(seq.getObjectAt(0));
        if (!(addressFamily.equals(.) || addressFamily.equals(.))) {
            validationResult.rejectIfFalse(false);
            throw new IllegalArgumentException("Address family is neither IPv4 nor IPv6");
        }
        expect(seq.getObjectAt(1), ASN1Sequence.class);
        ASN1Sequence addresses = (ASN1Sequenceseq.getObjectAt(1);
        for (int i = 0; i < addresses.size(); ++i) {
            RoaPrefix roaPrefix;
            try {
                roaPrefix = parseRoaIpAddressFamily(addressFamily.toIpResourceType(), addresses.getObjectAt(i));
            } catch (IllegalArgumentException e) {
                roaPrefix = null;
            }
            if (roaPrefix != null) {
                roaPrefixList.add(roaPrefix);
            }
        }
    }
        expect(derASN1Sequence.class);
        ASN1Sequence seq = (ASN1Sequenceder;
        List<RoaPrefixroaPrefixList = new ArrayList<RoaPrefix>();
        boolean errorOccured = false;
        for (int i = 0; i < seq.size(); ++i) {
            try {
                parseRoaIpAddressFamily(roaPrefixListseq.getObjectAt(i));
            } catch (IllegalArgumentException e) {
                errorOccured = true;
            }
        }
        ValidationResult validationResult = getValidationResult();
        if (!errorOccured) {
            validationResult.rejectIfFalse(true);
            validationResult.rejectIfFalse(true);
        }
        validationResult.rejectIfFalse(roaPrefixList.size() > 0, );
        return roaPrefixList;
    }
    @Override
    public void decodeContent(ASN1Encodable encoded) {
        parseRouteOriginAttestation(encoded);
    }
New to GrepCode? Check out our FAQ X