sun.security.x509.BasicConstraintsExtension Java Examples

The following examples show how to use sun.security.x509.BasicConstraintsExtension. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Keystores.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private static X509Certificate createSignedCertificate(final X509Certificate cetrificate, final X509Certificate issuerCertificate,
                                                       final PrivateKey issuerPrivateKey) {
    try {
        Principal issuer = issuerCertificate.getSubjectDN();
        String issuerSigAlg = issuerCertificate.getSigAlgName();

        byte[] inCertBytes = cetrificate.getTBSCertificate();
        X509CertInfo info = new X509CertInfo(inCertBytes);
        info.set(X509CertInfo.ISSUER, (X500Name) issuer);

        //No need to add the BasicContraint for leaf cert
        if (!cetrificate.getSubjectDN().getName().equals("CN=TOP")) {
            CertificateExtensions exts = new CertificateExtensions();
            BasicConstraintsExtension bce = new BasicConstraintsExtension(true, -1);
            exts.set(BasicConstraintsExtension.NAME, new BasicConstraintsExtension(false, bce.getExtensionValue()));
            info.set(X509CertInfo.EXTENSIONS, exts);
        }

        final X509CertImpl outCert = new X509CertImpl(info);
        outCert.sign(issuerPrivateKey, issuerSigAlg);

        return outCert;
    } catch (final Exception ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example #2
Source File: BCNull.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #3
Source File: BCNull.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #4
Source File: BCNull.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #5
Source File: BCNull.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #6
Source File: BCNull.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #7
Source File: BCNull.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #8
Source File: BCNull.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #9
Source File: BCNull.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #10
Source File: BCNull.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #11
Source File: BCNull.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #12
Source File: BCNull.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #13
Source File: CertificateGeneration.java    From protect with MIT License 4 votes vote down vote up
public static X509CertInfo createCertificateInfo(final String subjectDn, final String altNameIp,
		final String altNameHost, final PublicKey subjectPublicKey, final long validForDays, final boolean makeCa,
		final String issuerDn, final String certificateSigningAlgorithm) {

	try {

		// Look up algorithm based on CA private key
		final AlgorithmId algorithmId = AlgorithmId.get(certificateSigningAlgorithm);

		// Define validity period
		final Date notBefore = new Date(new Date().getTime() - 300); // 5 minutes prior to avoid clock skew issues
		final Date notAfter = new Date(notBefore.getTime() + (validForDays * 24 * 3600 * 1000));
		final CertificateValidity validity = new CertificateValidity(notBefore, notAfter);

		// Random serial number
		final BigInteger serialNumber = RandomNumberGenerator.generateRandomInteger(128);

		// Define information within certificate
		final X509CertInfo certificateInfo = new X509CertInfo();
		certificateInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
		certificateInfo.set(X509CertInfo.VALIDITY, validity);
		certificateInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(serialNumber));
		certificateInfo.set(X509CertInfo.SUBJECT, new X500Name(subjectDn));
		certificateInfo.set(X509CertInfo.ISSUER, new X500Name(issuerDn));
		certificateInfo.set(X509CertInfo.KEY, new CertificateX509Key(subjectPublicKey));
		certificateInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algorithmId));

		// Process extensions
		final CertificateExtensions extensions = new CertificateExtensions();

		// Make the issued certificate a sub-CA of this one (or self-signed)
		final BasicConstraintsExtension bce = new BasicConstraintsExtension(makeCa, 0);
		extensions.set(BasicConstraintsExtension.NAME,
				new BasicConstraintsExtension(true, bce.getExtensionValue()));

		// Add a subject alternative name (if not null)
		if (altNameIp != null) {
			final GeneralNames generalNames = new GeneralNames();
			generalNames.add(new GeneralName(new IPAddressName(altNameIp)));
			generalNames.add(new GeneralName(new DNSName(altNameHost)));
			final SubjectAlternativeNameExtension san = new SubjectAlternativeNameExtension(false, generalNames);
			extensions.set(SubjectAlternativeNameExtension.NAME, san);
		}

		certificateInfo.set(X509CertInfo.EXTENSIONS, extensions);

		return certificateInfo;

	} catch (GeneralSecurityException | IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #14
Source File: RsaSigningClient.java    From protect with MIT License 4 votes vote down vote up
protected static X509CertInfo createCertificateInfo(final String subjectDn, final String altNameIp,
		final String altNameHost, final PublicKey subjectPublicKey, final long validForDays, final boolean makeCa,
		final String issuerDn) {

	try {

		// Look up algorithm based on CA private key
		final AlgorithmId algorithmId = AlgorithmId.get(CERTIFICATE_SIGNING_ALGORITHM);

		// Define validity period
		final Date notBefore = new Date(new Date().getTime() - 300); // 5 minutes prior to avoid clock skew issues
		final Date notAfter = new Date(notBefore.getTime() + (validForDays * 24 * 3600 * 1000));
		final CertificateValidity validity = new CertificateValidity(notBefore, notAfter);

		// Random serial number
		final BigInteger serialNumber = RandomNumberGenerator.generateRandomInteger(128);

		// Define information within certificate
		final X509CertInfo certificateInfo = new X509CertInfo();
		certificateInfo.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
		certificateInfo.set(X509CertInfo.VALIDITY, validity);
		certificateInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(serialNumber));
		certificateInfo.set(X509CertInfo.SUBJECT, new X500Name(subjectDn));
		certificateInfo.set(X509CertInfo.ISSUER, new X500Name(issuerDn));
		certificateInfo.set(X509CertInfo.KEY, new CertificateX509Key(subjectPublicKey));
		certificateInfo.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algorithmId));

		// Process extensions
		final CertificateExtensions extensions = new CertificateExtensions();

		// Make the issued certificate a sub-CA of this one (or self-signed)
		final BasicConstraintsExtension bce = new BasicConstraintsExtension(makeCa, 0);
		extensions.set(BasicConstraintsExtension.NAME,
				new BasicConstraintsExtension(true, bce.getExtensionValue()));

		// Add a subject alternative name (if not null)
		if (altNameIp != null) {
			final GeneralNames generalNames = new GeneralNames();
			generalNames.add(new GeneralName(new IPAddressName(altNameIp)));
			generalNames.add(new GeneralName(new DNSName(altNameHost)));
			final SubjectAlternativeNameExtension san = new SubjectAlternativeNameExtension(false, generalNames);
			extensions.set(SubjectAlternativeNameExtension.NAME, san);
		}

		certificateInfo.set(X509CertInfo.EXTENSIONS, extensions);

		return certificateInfo;

	} catch (GeneralSecurityException | IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example #15
Source File: BCNull.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #16
Source File: BCNull.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String [] args) throws Exception {
    new BasicConstraintsExtension(false, -1).encode(new ByteArrayOutputStream());
}
 
Example #17
Source File: CertificateBuilder.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Set the Basic Constraints Extension for a certificate.
 *
 * @param crit {@code true} if critical, {@code false} otherwise
 * @param isCA {@code true} if the extension will be on a CA certificate,
 * {@code false} otherwise
 * @param maxPathLen The maximum path length issued by this CA.  Values
 * less than zero will omit this field from the resulting extension and
 * no path length constraint will be asserted.
 *
 * @throws IOException if an encoding error occurs.
 */
public void addBasicConstraintsExt(boolean crit, boolean isCA,
        int maxPathLen) throws IOException {
    addExtension(new BasicConstraintsExtension(crit, isCA, maxPathLen));
}