use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project keystore-explorer by kaikramer.
the class SignatureAlgorithmsTest method doTest.
private void doTest(KeyPairType keyPairType, SignatureType signatureType, CsrType csrType, X509CertificateVersion version) throws Exception {
KeyPair keyPair = null;
switch(keyPairType) {
case RSA:
keyPair = rsaKeyPair;
break;
case DSA:
keyPair = dsaKeyPair;
break;
case EC:
keyPair = ecKeyPair;
break;
default:
throw new InvalidParameterException();
}
X500Name name = new X500Name("cn=this");
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
X509Certificate cert = null;
if (version == X509CertificateVersion.VERSION1) {
cert = generatorv1.generateSelfSigned(name, 1000, publicKey, privateKey, signatureType, BigInteger.ONE);
} else {
cert = generatorv3.generateSelfSigned(name, 1000, publicKey, privateKey, signatureType, BigInteger.ONE);
}
if (csrType == CsrType.SPKAC) {
Spkac spkac = new Spkac("whatever", signatureType, new SpkacSubject(name), publicKey, privateKey);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
spkac.output(baos);
spkac = new Spkac(new ByteArrayInputStream(baos.toByteArray()));
assertThat(spkac.verify()).isTrue();
} else {
PKCS10CertificationRequest pkcs10 = Pkcs10Util.generateCsr(cert, privateKey, signatureType, "w/e", "w/e", false, new BouncyCastleProvider());
byte[] encoded = Pkcs10Util.getCsrEncodedDer(pkcs10);
pkcs10 = Pkcs10Util.loadCsr(new ByteArrayInputStream(encoded));
assertThat(Pkcs10Util.verifyCsr(pkcs10)).isTrue();
}
}
use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project keystore-explorer by kaikramer.
the class ExamineClipboardAction method showCsr.
private void showCsr(InputStream is, CryptoFileType fileType) {
if (is == null) {
return;
}
try {
PKCS10CertificationRequest pkcs10Csr = null;
Spkac spkacCsr = null;
try {
if (fileType == CryptoFileType.PKCS10_CSR) {
pkcs10Csr = Pkcs10Util.loadCsr(is);
} else if (fileType == CryptoFileType.SPKAC_CSR) {
spkacCsr = new Spkac(is);
}
} catch (Exception ex) {
String problemStr = res.getString("ExamineClipboardAction.NoOpenCsr.Problem");
String[] causes = new String[] { res.getString("ExamineClipboardAction.NotCsr.Cause"), res.getString("ExamineClipboardAction.CorruptedCsr.Cause") };
Problem problem = new Problem(problemStr, causes, ex);
DProblem dProblem = new DProblem(frame, res.getString("ExamineClipboardAction.ProblemOpeningCsr.Title"), problem);
dProblem.setLocationRelativeTo(frame);
dProblem.setVisible(true);
return;
}
if (pkcs10Csr != null) {
DViewCsr dViewCsr = new DViewCsr(frame, res.getString("ExamineClipboardAction.CsrDetails.Title"), pkcs10Csr);
dViewCsr.setLocationRelativeTo(frame);
dViewCsr.setVisible(true);
} else {
DViewCsr dViewCsr = new DViewCsr(frame, res.getString("ExamineClipboardAction.CsrDetails.Title"), spkacCsr);
dViewCsr.setLocationRelativeTo(frame);
dViewCsr.setVisible(true);
}
} catch (Exception ex) {
DError.displayError(frame, ex);
}
}
use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project keystore-explorer by kaikramer.
the class ExamineFileAction method openCsr.
private void openCsr(File file, CryptoFileType fileType) throws CryptoException {
if (file == null) {
return;
}
PKCS10CertificationRequest pkcs10Csr = null;
Spkac spkacCsr = null;
try {
if (fileType == CryptoFileType.PKCS10_CSR) {
pkcs10Csr = Pkcs10Util.loadCsr(new FileInputStream(file));
} else if (fileType == CryptoFileType.SPKAC_CSR) {
spkacCsr = new Spkac(new FileInputStream(file));
}
} catch (Exception ex) {
String problemStr = MessageFormat.format(res.getString("ExamineFileAction.NoOpenCsr.Problem"), file.getName());
String[] causes = new String[] { res.getString("ExamineFileAction.NotCsr.Cause"), res.getString("ExamineFileAction.CorruptedCsr.Cause") };
Problem problem = new Problem(problemStr, causes, ex);
DProblem dProblem = new DProblem(frame, res.getString("ExamineFileAction.ProblemOpeningCsr.Title"), problem);
dProblem.setLocationRelativeTo(frame);
dProblem.setVisible(true);
return;
}
if (pkcs10Csr != null) {
DViewCsr dViewCsr = new DViewCsr(frame, MessageFormat.format(res.getString("ExamineFileAction.CsrDetailsFile.Title"), file.getName()), pkcs10Csr);
dViewCsr.setLocationRelativeTo(frame);
dViewCsr.setVisible(true);
} else {
DViewCsr dViewCsr = new DViewCsr(frame, MessageFormat.format(res.getString("ExamineFileAction.CsrDetailsFile.Title"), file.getName()), spkacCsr);
dViewCsr.setLocationRelativeTo(frame);
dViewCsr.setVisible(true);
}
}
use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project keystore-explorer by kaikramer.
the class DSignCsr method main.
// for quick testing
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
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, new BouncyCastleProvider());
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
use of org.bouncycastle.pkcs.PKCS10CertificationRequest in project athenz by yahoo.
the class Crypto method generateX509CSR.
public static String generateX509CSR(PrivateKey privateKey, PublicKey publicKey, String x500Principal, GeneralName[] sanArray) throws OperatorCreationException, IOException {
// Create Distinguished Name
X500Principal subject = new X500Principal(x500Principal);
// Create ContentSigner
JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder(Crypto.RSA_SHA256);
ContentSigner signer = csBuilder.build(privateKey);
// Create the CSR
PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(subject, publicKey);
// /CLOVER:OFF
if (sanArray != null) {
// /CLOVER:ON
ExtensionsGenerator extGen = new ExtensionsGenerator();
GeneralNames subjectAltNames = new GeneralNames(sanArray);
extGen.addExtension(Extension.subjectAlternativeName, false, subjectAltNames);
p10Builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extGen.generate());
}
PKCS10CertificationRequest csr = p10Builder.build(signer);
// write to openssl PEM format
PemObject pemObject = new PemObject("CERTIFICATE REQUEST", csr.getEncoded());
StringWriter strWriter;
try (JcaPEMWriter pemWriter = new JcaPEMWriter(strWriter = new StringWriter())) {
pemWriter.writeObject(pemObject);
}
return strWriter.toString();
}
Aggregations