Java Code Examples for org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder#build()

The following examples show how to use org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder#build() . 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: TlsHelper.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
public static JcaPKCS10CertificationRequest generateCertificationRequest(String requestedDn, String domainAlternativeNames,
        KeyPair keyPair, String signingAlgorithm) throws OperatorCreationException {
    JcaPKCS10CertificationRequestBuilder jcaPKCS10CertificationRequestBuilder = new JcaPKCS10CertificationRequestBuilder(new X500Name(requestedDn), keyPair.getPublic());

    // add Subject Alternative Name(s)
    if(StringUtils.isNotBlank(domainAlternativeNames)) {
        try {
            jcaPKCS10CertificationRequestBuilder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, createDomainAlternativeNamesExtensions(domainAlternativeNames));
        } catch (IOException e) {
            throw new OperatorCreationException("Error while adding " + domainAlternativeNames + " as Subject Alternative Name.", e);
        }
    }

    JcaContentSignerBuilder jcaContentSignerBuilder = new JcaContentSignerBuilder(signingAlgorithm);
    return new JcaPKCS10CertificationRequest(jcaPKCS10CertificationRequestBuilder.build(jcaContentSignerBuilder.build(keyPair.getPrivate())));
}
 
Example 2
Source File: CertificateUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] createCSR(DistinguishedName distingueshedName, KeyPair keyPair) {
   String csrSignatureAlgorithm = RaPropertiesLoader.getProperty("csr.signature.algorithm");

   try {
      X500Principal x500Principal = new X500Principal(distingueshedName.asNormalizedEhealthDN());
      JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(x500Principal, keyPair.getPublic());
      PKCS10CertificationRequest csr = csrBuilder.build((new JcaContentSignerBuilder(csrSignatureAlgorithm)).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate()));
      return csr.getEncoded();
   } catch (OperatorCreationException var6) {
      throw new IllegalArgumentException(var6);
   } catch (IOException var7) {
      throw new IllegalArgumentException(var7);
   }
}
 
Example 3
Source File: CertificateUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] createCSR(String dn, KeyPair keyPair) {
   String csrSignatureAlgorithm = RaPropertiesLoader.getProperty("csr.signature.algorithm");

   try {
      X500Principal x500Principal = new X500Principal(dn);
      JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(x500Principal, keyPair.getPublic());
      PKCS10CertificationRequest csr = csrBuilder.build((new JcaContentSignerBuilder(csrSignatureAlgorithm)).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate()));
      return csr.getEncoded();
   } catch (OperatorCreationException var6) {
      throw new IllegalArgumentException(var6);
   } catch (IOException var7) {
      throw new IllegalArgumentException(var7);
   }
}
 
Example 4
Source File: CertificateUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] createCSR(DistinguishedName distingueshedName, KeyPair keyPair) {
   String csrSignatureAlgorithm = RaPropertiesLoader.getProperty("csr.signature.algorithm");

   try {
      X500Principal x500Principal = new X500Principal(distingueshedName.asNormalizedEhealthDN());
      JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(x500Principal, keyPair.getPublic());
      PKCS10CertificationRequest csr = csrBuilder.build((new JcaContentSignerBuilder(csrSignatureAlgorithm)).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate()));
      return csr.getEncoded();
   } catch (OperatorCreationException var6) {
      throw new IllegalArgumentException(var6);
   } catch (IOException var7) {
      throw new IllegalArgumentException(var7);
   }
}
 
Example 5
Source File: CertificateUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] createCSR(String dn, KeyPair keyPair) {
   String csrSignatureAlgorithm = RaPropertiesLoader.getProperty("csr.signature.algorithm");

   try {
      X500Principal x500Principal = new X500Principal(dn);
      JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(x500Principal, keyPair.getPublic());
      PKCS10CertificationRequest csr = csrBuilder.build((new JcaContentSignerBuilder(csrSignatureAlgorithm)).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate()));
      return csr.getEncoded();
   } catch (OperatorCreationException var6) {
      throw new IllegalArgumentException(var6);
   } catch (IOException var7) {
      throw new IllegalArgumentException(var7);
   }
}
 
Example 6
Source File: CertificateUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] createCSR(DistinguishedName distingueshedName, KeyPair keyPair) {
   String csrSignatureAlgorithm = RaPropertiesLoader.getProperty("csr.signature.algorithm");

   try {
      X500Principal x500Principal = new X500Principal(distingueshedName.asNormalizedEhealthDN());
      JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(x500Principal, keyPair.getPublic());
      PKCS10CertificationRequest csr = csrBuilder.build((new JcaContentSignerBuilder(csrSignatureAlgorithm)).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate()));
      return csr.getEncoded();
   } catch (OperatorCreationException var6) {
      throw new IllegalArgumentException(var6);
   } catch (IOException var7) {
      throw new IllegalArgumentException(var7);
   }
}
 
Example 7
Source File: CertificateUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] createCSR(DistinguishedName distingueshedName, KeyPair keyPair) {
   String csrSignatureAlgorithm = RaPropertiesLoader.getProperty("csr.signature.algorithm");

   try {
      X500Principal x500Principal = new X500Principal(distingueshedName.asNormalizedEhealthDN());
      JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(x500Principal, keyPair.getPublic());
      PKCS10CertificationRequest csr = csrBuilder.build((new JcaContentSignerBuilder(csrSignatureAlgorithm)).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate()));
      return csr.getEncoded();
   } catch (OperatorCreationException var6) {
      throw new IllegalArgumentException(var6);
   } catch (IOException var7) {
      throw new IllegalArgumentException(var7);
   }
}
 
Example 8
Source File: CertificateUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] createCSR(DistinguishedName distingueshedName, KeyPair keyPair) {
   String csrSignatureAlgorithm = RaPropertiesLoader.getProperty("csr.signature.algorithm");

   try {
      X500Principal x500Principal = new X500Principal(distingueshedName.asNormalizedEhealthDN());
      JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(x500Principal, keyPair.getPublic());
      PKCS10CertificationRequest csr = csrBuilder.build((new JcaContentSignerBuilder(csrSignatureAlgorithm)).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate()));
      return csr.getEncoded();
   } catch (OperatorCreationException | IOException ex) {
      throw new IllegalArgumentException(ex);
   }
}
 
Example 9
Source File: CertificateUtils.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] createCSR(String dn, KeyPair keyPair) {
   String csrSignatureAlgorithm = RaPropertiesLoader.getProperty("csr.signature.algorithm");

   try {
      X500Principal x500Principal = new X500Principal(dn);
      JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(x500Principal, keyPair.getPublic());
      PKCS10CertificationRequest csr = csrBuilder.build((new JcaContentSignerBuilder(csrSignatureAlgorithm)).setProvider(new BouncyCastleProvider()).build(keyPair.getPrivate()));
      return csr.getEncoded();
   } catch (OperatorCreationException | IOException ex) {
      throw new IllegalArgumentException(ex);
   }
}
 
Example 10
Source File: X509CertUtil.java    From portecle with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a PKCS #10 certification request (CSR) using the supplied certificate and private key.
 *
 * @param cert The certificate
 * @param privateKey The private key
 * @throws CryptoException If there was a problem generating the CSR
 * @return The CSR
 */
public static PKCS10CertificationRequest generatePKCS10CSR(X509Certificate cert, PrivateKey privateKey)
    throws CryptoException
{
	X500Name subject = new X500Name(cert.getSubjectDN().toString());

	JcaPKCS10CertificationRequestBuilder csrBuilder =
	    new JcaPKCS10CertificationRequestBuilder(subject, cert.getPublicKey());
	JcaContentSignerBuilder signerBuilder = new JcaContentSignerBuilder(cert.getSigAlgName());

	try
	{
		ContentVerifierProvider prov = new JcaContentVerifierProviderBuilder().build(cert);
		PKCS10CertificationRequest csr = csrBuilder.build(signerBuilder.build(privateKey));

		if (!csr.isSignatureValid(prov))
		{
			throw new CryptoException(RB.getString("NoVerifyGenCsr.exception.message"));
		}

		return csr;
	}
	catch (OperatorCreationException | PKCSException ex)
	{
		throw new CryptoException(RB.getString("NoGenerateCsr.exception.message"), ex);
	}
}
 
Example 11
Source File: DViewCsr.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
	KeyPair keyPair = keyGen.genKeyPair();
	JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(
			new X500Name("cn=test"), keyPair.getPublic());
	PKCS10CertificationRequest csr = csrBuilder
			.build(new JcaContentSignerBuilder("SHA256withRSA").setProvider("BC").build(keyPair.getPrivate()));

	DViewCsr dialog = new DViewCsr(new javax.swing.JFrame(), "Title", csr);
	DialogViewer.run(dialog);
}
 
Example 12
Source File: DSignCsr.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
	keyGen.initialize(1024);
	KeyPair keyPair = keyGen.genKeyPair();
	JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(
			new X500Name("cn=test"), keyPair.getPublic());
	PKCS10CertificationRequest csr = csrBuilder
			.build(new JcaContentSignerBuilder("SHA256withRSA").setProvider("BC").build(keyPair.getPrivate()));

	DSignCsr dialog = new DSignCsr(new javax.swing.JFrame(), csr,
			new File(System.getProperty("user.dir"), "test.csr"), keyPair.getPrivate(), KeyPairType.RSA, null);
	DialogViewer.run(dialog);
}
 
Example 13
Source File: DViewPem.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
	KeyPair keyPair = keyGen.genKeyPair();
	JcaPKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(
			new X500Name("cn=test"), keyPair.getPublic());
	PKCS10CertificationRequest csr = csrBuilder
			.build(new JcaContentSignerBuilder("SHA256withRSA").setProvider("BC").build(keyPair.getPrivate()));

	DViewPem dialog = new DViewPem(new javax.swing.JFrame(), "Title", csr);
	DialogViewer.run(dialog);
}
 
Example 14
Source File: TlsHelper.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static JcaPKCS10CertificationRequest generateCertificationRequest(String requestedDn, List<String> domainAlternativeNames,
                                                                         KeyPair keyPair, String signingAlgorithm) throws OperatorCreationException {
    JcaPKCS10CertificationRequestBuilder jcaPKCS10CertificationRequestBuilder = new JcaPKCS10CertificationRequestBuilder(new X500Name(requestedDn), keyPair.getPublic());

    // add Subject Alternative Name(s)
    try {
        jcaPKCS10CertificationRequestBuilder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, createDomainAlternativeNamesExtensions(domainAlternativeNames, requestedDn));
    } catch (IOException e) {
        throw new OperatorCreationException("Error while adding " + domainAlternativeNames + " as Subject Alternative Name.", e);
    }

    JcaContentSignerBuilder jcaContentSignerBuilder = new JcaContentSignerBuilder(signingAlgorithm);
    return new JcaPKCS10CertificationRequest(jcaPKCS10CertificationRequestBuilder.build(jcaContentSignerBuilder.build(keyPair.getPrivate())));
}