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.x509cert;
 
 
 import java.net.URI;
 import java.util.Date;
Fairly generic helper for X509CertificateBuilders. Intended to be used by (delegated to, not extended) specific certificate builders.

Because we want to maintain the pattern where a specific Certificate builder can be chained like: builder.withValidity(val).withSubjectDn(subject) etc... dynamic typing would be required.. hence delegation.

 
 public final class X509CertificateBuilderHelper {
 
     public static final String DEFAULT_SIGNATURE_ALGORITHM = "SHA256withRSA";
 
     public static final String DEFAULT_SIGNATURE_PROVIDER = "SunRsaSign";
 
 
 
     private BigInteger serial;
 
     private X500Principal subjectDN;
 
     private X500Principal issuerDN;
 
     private ValidityPeriod validityPeriod;
 
     private IpResourceSet resources;
 
    private PublicKey publicKey;
    private KeyPair signingKeyPair;
    private int keyUsage;
    private boolean ca;
    private boolean addSubjectKeyIdentifier = true;
    private boolean addAuthorityKeyIdentifier = true;
    private URI[] crlDistributionPoints;
    private PolicyInformation[] policies;
            String signatureProvider) {
        this. = signatureProvider;
        return this;
    }
        this. = serial;
        return this;
    }
        this. = subjectDN;
        return this;
    }
        this. = issuerDN;
        return this;
    }
            ValidityPeriod validityPeriod) {
        this. = validityPeriod;
        return this;
    }
        this. = resources;
        return this;
    }
        this. = publicKey;
        return this;
    }
        this. = signingKey;
        return this;
    }

    
Careful! You probably want to stick to the default. This method is here mainly to allow for testing the parser -> it should reject sig algos not allowed by RFC
            String signatureAlgorithm) {
        this. = signatureAlgorithm;
        return this;
    }
    public X509CertificateBuilderHelper withKeyUsage(int keyUsage) {
        this. = keyUsage;
        return this;
    }
    public X509CertificateBuilderHelper withCa(boolean ca) {
        this. = ca;
        return this;
    }
        this. = add;
        return this;
    }
        this. = add;
        return this;
    }
        this. = uris;
        return this;
    }
            X509CertificateInformationAccessDescriptor... descriptors) {
         = X509CertificateInformationAccessDescriptor
                .convertAccessDescriptors(descriptors);
        return this;
    }
            X509CertificateInformationAccessDescriptor... descriptors) {
         = X509CertificateInformationAccessDescriptor
                .convertAccessDescriptors(descriptors);
        return this;
    }
            PolicyInformation... policies) {
        this. = policies;
        return this;
    }
        this. = EnumSet.copyOf(resourceTypes);
        return this;
    }
        X509v3CertificateBuilder certificateGenerator = createCertificateGenerator();
        try {
            return new JcaX509CertificateConverter().getCertificate(certificateGenerator.build(signer));
        } catch (CertificateEncodingException e) {
            throw new X509ResourceCertificateBuilderException(e);
        } catch (IllegalStateException e) {
            throw new X509ResourceCertificateBuilderException(e);
        } catch (OperatorCreationException e) {
            throw new X509ResourceCertificateBuilderException(e);
        } catch (CertificateException e) {
            throw new X509ResourceCertificateBuilderException(e);
        }
    }

    
Override this to add your extensions to the certificate generator
        try {
            X509v3CertificateBuilder generator = createX509V3CertificateGenerator();
            if () {
                addSubjectKeyIdentifier(generator);
            }
            if () {
                addAuthorityKeyIdentifier(generator);
            }
            if () {
                addCaBit(generator);
            }
            if ( != 0) {
                addKeyUsage(generator);
            }
            if ( != null) {
                addAIA(generator);
            }
            if ( != null) {
                addSIA(generator);
            }
            if ( != null) {
                Validate.noNullElements();
                addCrlDistributionPoints(generator);
            }
            if ( != null && . > 0) {
                addPolicies(generator);
            }
            if ( != null) {
                addResourceExtensions(generator);
            }
            return generator;
        } catch (CertIOException e) {
            throw new X509ResourceCertificateBuilderException(e);
        } catch (InvalidKeyException e) {
            throw new X509ResourceCertificateBuilderException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new X509ResourceCertificateBuilderException(e);
        }
    }
        validateCertificateFields();
        PublicKey key = ;
        X509v3CertificateBuilder generator = new X509v3CertificateBuilder(
                BouncyCastleUtil.principalToName(),
                ,
                new Date(.getNotValidBefore().getMillis()),
                new Date(.getNotValidAfter().getMillis()),
                BouncyCastleUtil.principalToName(),
                BouncyCastleUtil.createSubjectPublicKeyInfo(key));
        return generator;
    }
    private void validateCertificateFields() {
        Validate.notNull("no issuerDN");
        Validate.notNull("no subjectDN");
        Validate.notNull("no serial");
        Validate.notNull("no publicKey");
        Validate.notNull("no signingKeyPair");
        Validate.notNull("no validityPeriod");
        if (!) {
            Validate.isTrue(( & .) == 0,
                    "keyCertSign only allowed for ca");
        }
    }
    }
        generator.addExtension(
                .,
                false,
                BouncyCastleUtil.createAuthorityKeyIdentifier(.getPublic()));
    }
    private void addCaBit(X509v3CertificateBuilder generatorthrows CertIOException {
        generator.addExtension(.truenew BasicConstraints());
    }
    private void addKeyUsage(X509v3CertificateBuilder generatorthrows CertIOException {
        generator.addExtension(.truenew KeyUsage());
    }
    private void addAIA(X509v3CertificateBuilder generatorthrows CertIOException {
        generator.addExtension(.false,
                AuthorityInformationAccess.getInstance(new DERSequence()));
    }
    private void addSIA(X509v3CertificateBuilder generatorthrows CertIOException {
        generator.addExtension(.false,
                AuthorityInformationAccess.getInstance(new DERSequence()));
    }
    private void addCrlDistributionPoints(X509v3CertificateBuilder generatorthrows CertIOException {
        generator.addExtension(.falsecrldp);
    }
    private void addPolicies(X509v3CertificateBuilder generatorthrows CertIOException {
        generator.addExtension(.truenew DERSequence());
    }
    private void addResourceExtensions(X509v3CertificateBuilder generatorthrows CertIOException {
        ResourceExtensionEncoder encoder = new ResourceExtensionEncoder();
        for (IpResourceType inherited : ) {
            if (.containsType(inherited)) {
                throw new IllegalArgumentException("resource set '" +  + "' contains resources of inherited type " + inherited);
            }
        }
        ASN1Encodable encodedIPAddressBlocks = encoder.encodeIpAddressBlocks(
                .contains(.),
                .contains(.),
                );
        if (encodedIPAddressBlocks != null) {
            generator.addExtension(
                    .true,
                    encodedIPAddressBlocks);
        }
        if (encodedASNs != null) {
            generator.addExtension(.trueencodedASNs);
        }
    }

    
Generate a single distribution point where the names contains each URI.
    private CRLDistPoint convertToCrlDistributionPoint(URI[] uris) {
        GeneralName[] seq = new GeneralName[uris.length];
        for (int i = 0; i < uris.length; ++i) {
            seq[i] = new GeneralName(.uris[i].toString());
        }
        GeneralNames names = new GeneralNames(seq);
        DistributionPointName distributionPoint = new DistributionPointName(
                names);
        DistributionPoint[] dps = {new DistributionPoint(distributionPointnullnull)};
        return new CRLDistPoint(dps);
    }
New to GrepCode? Check out our FAQ X