Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use immutable resource set #150

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ next (snapshot) release, e.g. `1.1-SNAPSHOT` after releasing `1.0`.

## Changelog

## 2024-xx-yy 1.38
## 2024-xx-yy 2.0.0
* **breaking**: Use **ImmutableResourceSet** in many situations

## 2024-02-28 1.37
* Use bouncy castle 1.77 (and update API usage accordingly)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.ripe.rpki</groupId>
<artifactId>rpki-commons</artifactId>
<version>1.38-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<inceptionYear>2008</inceptionYear>

<name>RPKI Commmons</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.ripe.rpki.commons.crypto.cms.roa;

import net.ripe.ipresource.Asn;
import net.ripe.ipresource.ImmutableResourceSet;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.rpki.commons.crypto.cms.RpkiSignedObject;
import net.ripe.rpki.commons.crypto.cms.RpkiSignedObjectInfo;
Expand Down Expand Up @@ -31,7 +32,7 @@ public Asn getAsn() {
return asn;
}

public IpResourceSet getResources() {
public ImmutableResourceSet getResources() {
return getCertificate().getResources();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package net.ripe.rpki.commons.crypto.rfc3779;

import net.ripe.ipresource.Asn;
import net.ripe.ipresource.IpAddress;
import net.ripe.ipresource.IpRange;
import net.ripe.ipresource.IpResource;
import net.ripe.ipresource.IpResourceRange;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.ipresource.IpResourceType;
import net.ripe.ipresource.*;
import net.ripe.rpki.commons.crypto.util.Asn1Util;
import org.apache.commons.lang3.Validate;
import org.bouncycastle.asn1.ASN1Encodable;
Expand Down Expand Up @@ -62,8 +56,8 @@ public class ResourceExtensionEncoder {
* @param resources the set of IPv4 and IPv6 resources.
* @return the DER encoding of the IP Address Block Extension.
*/
public ASN1Object encodeIpAddressBlocks(boolean inheritIpv4, boolean inheritIpv6, IpResourceSet resources) {
SortedMap<AddressFamily, IpResourceSet> addressBlocks = new TreeMap<AddressFamily, IpResourceSet>();
public ASN1Object encodeIpAddressBlocks(boolean inheritIpv4, boolean inheritIpv6, ImmutableResourceSet resources) {
SortedMap<AddressFamily, ImmutableResourceSet> addressBlocks = new TreeMap<>();

if (inheritIpv4) {
addressBlocks.put(AddressFamily.IPV4, null);
Expand All @@ -89,9 +83,9 @@ public ASN1Object encodeIpAddressBlocks(boolean inheritIpv4, boolean inheritIpv6
* @param resources the set of ASNs.
* @return the DER encoding of the AS Identifier extension.
*/
public ASN1Object encodeAsIdentifiers(boolean inherit, IpResourceSet resources) {
public ASN1Object encodeAsIdentifiers(boolean inherit, ImmutableResourceSet resources) {
if (inherit || resources.containsType(IpResourceType.ASN)) {
return asIdentifiersToDer(inherit, resources, false, new IpResourceSet());
return asIdentifiersToDer(inherit, resources, false, ImmutableResourceSet.empty());
}
return null;
}
Expand All @@ -104,7 +98,7 @@ public ASN1Object encodeAsIdentifiers(boolean inherit, IpResourceSet resources)
* ASIdentifiers ::= SEQUENCE { asnum [0] EXPLICIT ASIdentifierChoice
* OPTIONAL, rdi [1] EXPLICIT ASIdentifierChoice OPTIONAL}
*/
ASN1Object asIdentifiersToDer(boolean inheritAsn, IpResourceSet asnResources, boolean inheritRdi, IpResourceSet rdiResources) {
ASN1Object asIdentifiersToDer(boolean inheritAsn, ImmutableResourceSet asnResources, boolean inheritRdi, ImmutableResourceSet rdiResources) {
List<ASN1Encodable> seq = new ArrayList<ASN1Encodable>(2);
if (inheritAsn || asnResources.containsType(IpResourceType.ASN)) {
seq.add(new DERTaggedObject(0, asIdentifierChoiceToDer(inheritAsn, asnResources)));
Expand All @@ -119,14 +113,14 @@ ASN1Object asIdentifiersToDer(boolean inheritAsn, IpResourceSet asnResources, bo
* ASIdentifierChoice ::= CHOICE { inherit NULL, -- inherit from issuer --
* asIdsOrRanges SEQUENCE OF ASIdOrRange }
*/
ASN1Encodable asIdentifierChoiceToDer(boolean inherit, IpResourceSet resources) {
ASN1Encodable asIdentifierChoiceToDer(boolean inherit, ImmutableResourceSet resources) {
return inherit ? DERNull.INSTANCE : asIdsOrRangesToDer(resources);
}

/**
* asIdsOrRanges ::= SEQUENCE OF ASIdOrRange
*/
DERSequence asIdsOrRangesToDer(IpResourceSet resources) {
DERSequence asIdsOrRangesToDer(ImmutableResourceSet resources) {
List<ASN1Encodable> seq = new ArrayList<ASN1Encodable>();
for (IpResource resource : resources) {
if (IpResourceType.ASN == resource.getType()) {
Expand Down Expand Up @@ -161,7 +155,7 @@ ASN1Integer asIdToDer(Asn asn) {
/**
* IPAddrBlocks ::= SEQUENCE OF IPAddressFamily
*/
ASN1Object ipAddressBlocksToDer(SortedMap<AddressFamily, IpResourceSet> resources) {
ASN1Object ipAddressBlocksToDer(SortedMap<AddressFamily, ImmutableResourceSet> resources) {
List<ASN1Encodable> seq = new ArrayList<ASN1Encodable>(2);
for (AddressFamily addressFamily : resources.keySet()) {
seq.add(ipAddressFamilyToDer(addressFamily, resources.get(addressFamily)));
Expand All @@ -173,7 +167,7 @@ ASN1Object ipAddressBlocksToDer(SortedMap<AddressFamily, IpResourceSet> resource
* IPAddressFamily ::= SEQUENCE { -- AFI & opt SAFI -- addressFamily OCTET
* STRING (SIZE (2..3)), ipAddressChoice IPAddressChoice }
*/
ASN1Object ipAddressFamilyToDer(AddressFamily addressFamily, IpResourceSet resources) {
ASN1Object ipAddressFamilyToDer(AddressFamily addressFamily, ImmutableResourceSet resources) {
IpResourceType type = addressFamily.toIpResourceType();
ASN1Encodable[] seq = new ASN1Encodable[2];
seq[0] = addressFamily.toDer();
Expand All @@ -185,7 +179,7 @@ ASN1Object ipAddressFamilyToDer(AddressFamily addressFamily, IpResourceSet resou
* IPAddressChoice ::= CHOICE { inherit NULL, -- inherit from issuer --
* addressesOrRanges SEQUENCE OF IPAddressOrRange }
*/
ASN1Encodable ipAddressChoiceToDer(IpResourceType type, IpResourceSet resources) {
ASN1Encodable ipAddressChoiceToDer(IpResourceType type, ImmutableResourceSet resources) {
if (resources == null) {
return DERNull.INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ripe.rpki.commons.crypto.x509cert;

import net.ripe.ipresource.ImmutableResourceSet;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.ipresource.IpResourceType;
import net.ripe.rpki.commons.crypto.ValidityPeriod;
Expand All @@ -17,7 +18,7 @@ public abstract class GenericRpkiCertificateBuilder {
private PublicKey publicKey;
private KeyPair signingKeyPair;
private BigInteger serial;
private IpResourceSet resources = new IpResourceSet();
private ImmutableResourceSet resources = ImmutableResourceSet.empty();
private EnumSet<IpResourceType> inheritedResourceTypes = EnumSet.noneOf(IpResourceType.class);
private X500Principal subject;
private X500Principal issuer;
Expand All @@ -40,7 +41,7 @@ public void withSerial(BigInteger serial) {
this.serial = serial;
}

public void withResources(IpResourceSet resources) {
public void withResources(ImmutableResourceSet resources) {
this.resources = resources;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ripe.rpki.commons.crypto.x509cert;

import net.ripe.ipresource.ImmutableResourceSet;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.ipresource.IpResourceType;
import net.ripe.rpki.commons.crypto.ValidityPeriod;
Expand Down Expand Up @@ -83,7 +84,7 @@ public final class X509CertificateBuilderHelper {

private ValidityPeriod validityPeriod;

private IpResourceSet resources;
private ImmutableResourceSet resources;

private PublicKey publicKey;

Expand Down Expand Up @@ -134,7 +135,7 @@ public X509CertificateBuilderHelper withValidityPeriod(
return this;
}

public X509CertificateBuilderHelper withResources(IpResourceSet resources) {
public X509CertificateBuilderHelper withResources(ImmutableResourceSet resources) {
this.resources = resources;
return this;
}
Expand Down Expand Up @@ -294,7 +295,7 @@ protected X509v3CertificateBuilder createCertificateGenerator() {
* must be present. This means at least one IPvX or ASN must be either set
* explicitly or inherited..
*/
protected void validateResource(IpResourceSet resources) {
protected void validateResource(ImmutableResourceSet resources) {
// at least one resource type must be either set or inherited
final boolean atLeastOneResourceTypeUsed = EnumSet.allOf(IpResourceType.class)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public ImmutableResourceSet resources() {
return resourceExtension.getResources();
}

public IpResourceSet getResources() {
return new IpResourceSet(resources());
public ImmutableResourceSet getResources() {
return resources();
}

public EnumSet<IpResourceType> getInheritedResourceTypes() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ripe.rpki.commons.crypto.x509cert;

import net.ripe.ipresource.ImmutableResourceSet;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.ipresource.IpResourceType;
import net.ripe.rpki.commons.crypto.ValidityPeriod;
Expand All @@ -22,7 +23,7 @@
*/
public class X509ResourceCertificateBuilder {
private final X509CertificateBuilderHelper builderHelper;
private IpResourceSet resources = new IpResourceSet();
private ImmutableResourceSet resources = ImmutableResourceSet.empty();
private EnumSet<IpResourceType> inheritedResourceTypes = EnumSet.noneOf(IpResourceType.class);

public X509ResourceCertificateBuilder() {
Expand Down Expand Up @@ -72,7 +73,7 @@ public X509ResourceCertificateBuilder withKeyUsage(int keyUsage) {
return this;
}

public X509ResourceCertificateBuilder withResources(IpResourceSet resources) {
public X509ResourceCertificateBuilder withResources(ImmutableResourceSet resources) {
this.resources = resources;
builderHelper.withResources(resources);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.ripe.rpki.commons.crypto.x509cert;

import net.ripe.ipresource.Asn;
import net.ripe.ipresource.ImmutableResourceSet;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.ipresource.IpResourceType;
import net.ripe.rpki.commons.crypto.ValidityPeriod;
Expand Down Expand Up @@ -74,7 +75,7 @@ public X509RouterCertificateBuilder withAsns(int[] asns) {
for (int asn : asns) {
resources.add(new Asn(asn));
}
builderHelper.withResources(resources);
builderHelper.withResources(ImmutableResourceSet.of(resources));
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.ripe.rpki.commons.validation.objectvalidators;

import com.google.common.collect.Lists;
import net.ripe.ipresource.ImmutableResourceSet;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.rpki.commons.crypto.x509cert.X509CertificateObject;
import net.ripe.rpki.commons.crypto.x509cert.X509ResourceCertificate;
Expand All @@ -27,15 +28,18 @@ public class CertificateRepositoryObjectValidationContext {

private final X509CertificateObject certificate;

private final IpResourceSet resources;
/**
* Mutable because it can be reduced when overclaiming
*/
private final ImmutableResourceSet resources;

private IpResourceSet overclaiming = new IpResourceSet();
private ImmutableResourceSet overclaiming = ImmutableResourceSet.empty();

public CertificateRepositoryObjectValidationContext(URI location, X509ResourceCertificate certificate) {
this(location, certificate, certificate.getResources(), Lists.newArrayList(certificate.getSubject().getName()));
}

public CertificateRepositoryObjectValidationContext(URI location, X509ResourceCertificate certificate, IpResourceSet resources, List<String> subjectChain) {
public CertificateRepositoryObjectValidationContext(URI location, X509ResourceCertificate certificate, ImmutableResourceSet resources, List<String> subjectChain) {
this.location = location;
this.certificate = certificate;
this.resources = resources;
Expand Down Expand Up @@ -85,28 +89,32 @@ public byte[] getSubjectKeyIdentifier() {
}

public void addOverclaiming(IpResourceSet overclaiming) {
this.overclaiming.addAll(overclaiming);
this.overclaiming = new ImmutableResourceSet.Builder().addAll(this.overclaiming).addAll(overclaiming).build();
}

public CertificateRepositoryObjectValidationContext createChildContext(URI childLocation, X509ResourceCertificate childCertificate) {
IpResourceSet effectiveResources = childCertificate.deriveResources(resources);
var effectiveResources = childCertificate.deriveResources(resources);
removeOverclaimingResources(effectiveResources);
List<String> childSubjects = Lists.newArrayList(subjectChain);
childSubjects.add(childCertificate.getSubject().getName());
return new CertificateRepositoryObjectValidationContext(childLocation, childCertificate, effectiveResources, childSubjects);
}

public IpResourceSet getResources() {
IpResourceSet result = new IpResourceSet(resources);
removeOverclaimingResources(result);
return result;
public ImmutableResourceSet getResources() {
return removeOverclaimingResources(resources);
}

private void removeOverclaimingResources(IpResourceSet resources) {
/**
* Remove the resources that are overclaimed in this context from the passed in resources.
* @param resources resources to clean
* @return resources - overclaiming
*/
private ImmutableResourceSet removeOverclaimingResources(ImmutableResourceSet resources) {
if (overclaiming.isEmpty() || resources.isEmpty()) {
return;
return resources;
}
resources.removeAll(overclaiming);

return new ImmutableResourceSet.Builder().addAll(resources).removeAll(overclaiming).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ripe.rpki.commons.validation.objectvalidators;

import net.ripe.ipresource.ImmutableResourceSet;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.rpki.commons.crypto.crl.X509Crl;
import net.ripe.rpki.commons.crypto.x509cert.X509ResourceCertificate;
Expand All @@ -12,7 +13,7 @@ public static X509ResourceCertificateParentChildValidator getX509ResourceCertifi
CertificateRepositoryObjectValidationContext context,
ValidationOptions options, ValidationResult result, X509Crl crl) {

return new X509ResourceCertificateParentChildValidator(options, result, context.getCertificate(), crl, context.getResources());
return new X509ResourceCertificateParentChildValidator(options, result, context.getCertificate(), crl, ImmutableResourceSet.of(context.getResources()));
}

public static X509ResourceCertificateValidator getX509ResourceCertificateValidator(
Expand All @@ -22,12 +23,12 @@ public static X509ResourceCertificateValidator getX509ResourceCertificateValidat
if (options.isAllowOverclaimParentChild())
return new X509ResourceCertificateParentChildLooseValidator(options, result, crl, context);

return new X509ResourceCertificateParentChildValidator(options, result, context.getCertificate(), crl, context.getResources());
return new X509ResourceCertificateParentChildValidator(options, result, context.getCertificate(), crl, ImmutableResourceSet.of(context.getResources()));
}

public static X509ResourceCertificateParentChildValidator getX509ResourceCertificateParentChildStrictValidator(
ValidationOptions options, ValidationResult result, X509ResourceCertificate parent,
IpResourceSet resources, X509Crl crl) {
ImmutableResourceSet resources, X509Crl crl) {
return new X509ResourceCertificateParentChildValidator(options, result, parent, crl, resources);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void validate(String location, X509ResourceCertificate certificate) {
X509ResourceCertificate parent = certificates.get(0).getCertificate();
certificates.remove(0); // No need to validate the root (1st parent) certificate against itself

IpResourceSet resources = parent.getResources();
var resources = parent.getResources();

for (CertificateWithLocation certificateWithLocation : certificates) {
String childLocation = certificateWithLocation.getLocation().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public void validate(String location, X509ResourceCertificate certificate) {
private void verifyResources() {
final ValidationResult result = getValidationResult();
final X509ResourceCertificate child = getChild();
final IpResourceSet resources = context.getResources();
final IpResourceSet childResourceSet = child.deriveResources(resources);
final var resources = context.getResources();
final var childResourceSet = child.deriveResources(resources);

if (child.isRoot()) {
result.rejectIfTrue(child.isResourceSetInherited(), ROOT_INHERITS_RESOURCES);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.ripe.rpki.commons.validation.objectvalidators;

import net.ripe.ipresource.ImmutableResourceSet;
import net.ripe.ipresource.IpResourceSet;
import net.ripe.rpki.commons.crypto.crl.X509Crl;
import net.ripe.rpki.commons.crypto.x509cert.X509ResourceCertificate;
Expand All @@ -11,13 +12,13 @@

public class X509ResourceCertificateParentChildValidator extends X509CertificateParentChildValidator<X509ResourceCertificate> implements X509ResourceCertificateValidator {

private IpResourceSet resources;
private ImmutableResourceSet resources;

public X509ResourceCertificateParentChildValidator(ValidationOptions options,
ValidationResult result,
X509ResourceCertificate parent,
X509Crl crl,
IpResourceSet resources) {
ImmutableResourceSet resources) {
super(options, result, parent, crl);
this.resources = resources;
}
Expand All @@ -31,7 +32,7 @@ public void validate(String location, X509ResourceCertificate certificate) {
private void verifyResources() {
final ValidationResult result = getValidationResult();
final X509ResourceCertificate child = getChild();
final IpResourceSet childResourceSet = child.deriveResources(resources);
final var childResourceSet = child.deriveResources(resources);

if (child.isRoot()) {
result.rejectIfTrue(child.isResourceSetInherited(), ROOT_INHERITS_RESOURCES);
Expand Down
Loading
Loading