Code, JavaEE

Read Certificate Signing Request Using Bouncy Castle


// Constants representing the ASN1ObjectIdentifier's</pre>
private final String COUNTRY = "2.5.4.6";
 private final String STATE = "2.5.4.8";
 private final String LOCALE = "2.5.4.7";
 private final String ORGANIZATION = "2.5.4.10";
 private final String ORGANIZATION_UNIT = "2.5.4.11";
 private final String COMMON_NAME = "2.5.4.3";
<pre>

// Unit Test Method
@Test
 public void testReadCertificateSigningRequest() {
 String csrPEM = "
-----BEGIN CERTIFICATE REQUEST-----
MIIC7TCCAdUCAQAwgacxCzAJBgNVBAYTAlVTMRAwDgYDVQQIDAdGbG9yaWRhMRcw
FQYDVQQHDA5QZW1icm9rZSBQaW5lczETMBEGA1UECgwKRGF2aWQgS2luZzEUMBIG
A1UECwwLRGV2ZWxvcG1lbnQxGjAYBgNVBAMMEXd3dy5kYXZpZGtpbmcuY29tMSYw
JAYJKoZIhvcNAQkBFhdkYXZpZGtpbmdAZGF2aWRraW5nLmNvbTCCASIwDQYJKoZI
hvcNAQEBBQADggEPADCCAQoCggEBANsbrV3Y0ap9rDX4OviX/bl7TtS7goo9EE3Q
84BI1H57L2n6F6CQ5qWIcNMIfXVwYylG4FcUAyJ/9W1zEY+yPpZN8y02DGB+MDTf
CF1JPP/3tf+T5GO1vrT8Ca1Dv1CJBzHIt5e3weqihl+23644SFK6o5p2+2sd2Y7R
A/d/9+/29yLzHysGw43GVO4auFoLEDAnLv8WEUyd9xGMVbD0+LnKLz6D3nOdMKuB
gnKd8D184dDAKr9e/oGxhYPAEnrN5EBZrcvMYGBtBMh2a5gbI5x/3mk0LSctK/RI
doNFtiAm1YjquRHg7+m40TbcAFLzY481nvua2z961niQ1kfJ45UCAwEAAaAAMA0G
CSqGSIb3DQEBBQUAA4IBAQDAS9SEtXymeoAzK7KmfxpxhJeM0zZbA4QYfwTrLRYn
Hgf/jkaCCBQm2Jxc7lNF7Mr68OqfNCQLk4TTt2+j3hz4oRZs070WSoGx7vwk7Lr2
w8lv+SOvhKyUTiozttMptYCNp/CirhbIb2e1GiSt3RedUGItryQaBaStfMiMNGbP
WIG722EEYX4UiUrXtZq5aI70HYOfECYjCOQPhRJbQ/HYNhr1jRXNd2U3kyV4ro/8
uQy6FBoWs4Veq2q2YnNFNdU15b4SMSIYIH/3Fne2ahDU19hT4+XnDI/7RwgemiDh
Co8H8aAPZx2BOR19sizo8ahk8Wx/OBjZEvksQiNnl0jS
-----END CERTIFICATE REQUEST-----";

PKCS10CertificationRequest csr = convertPemToPKCS10CertificationRequest(csrPEM);

X500Name x500Name = csr.getSubject();
 System.out.println("x500Name is: " + x500Name + "\n");

// country is 2.5.4.6
 System.out.println("COUNTRY: " + getX500Field(COUNTRY, x500Name));
 // state is 2.5.4.8
 System.out.println("STATE: " + getX500Field(STATE, x500Name));
 // locale is 2.5.4.7
 System.out.println("LOCALE: " + getX500Field(LOCALE, x500Name));


 }

// Method to get specific field from the X500Name Subject

private String getX500Field(String asn1ObjectIdentifier, X500Name x500Name) {
 RDN[] rdnArray = x500Name.getRDNs(new ASN1ObjectIdentifier(asn1ObjectIdentifier));
 String retVal = null;
 for (RDN item : rdnArray) {
 retVal = item.getFirst().getValue().toString();
 }

return retVal;
 }

// Method to convert PEM to PKCS10CertificationRequest

private PKCS10CertificationRequest convertPemToPKCS10CertificationRequest(String pem) {
 Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
 PKCS10CertificationRequest csr = null;
 ByteArrayInputStream pemStream = null;
 try {
 pemStream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
 } catch (UnsupportedEncodingException ex) {
 LOG.error("UnsupportedEncodingException, convertPemToPublicKey", ex);
 }

Reader pemReader = new BufferedReader(new InputStreamReader(pemStream));
 PEMParser pemParser = new PEMParser(pemReader);

try {
 Object parsedObj = pemParser.readObject();

System.out.println("PemParser returned: " + parsedObj);

if (parsedObj instanceof PKCS10CertificationRequest) {
 csr = (PKCS10CertificationRequest) parsedObj;

}
 } catch (IOException ex) {
 LOG.error("IOException, convertPemToPublicKey", ex);
 }

return csr;
 }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s