Binary X509 certificate through AJAX to parse in a Java Server Page in Tomcat Server java.security.cert error

63 Views Asked by At

I have a X509 certificate stored in an attribute of the user class in Edirectory and I want to parse its values in order to get the expiration date. As I can see through LDAP, the certificate is stored in binary format.

I have a JAVA function to do that that works perfectly but the thing is that I want to run that on a workflow form which runs on the browser, so I cannot directly use JAVA, I have to send an ajax query to the tomcat server.

I followed that link in order to communicate the form to the JBOSS server (in my server it is a Tomcat but it is the same I guess) through AJAX and that connection works fine but I cannot parse the certificate on the server: https://community.microfocus.com/cyberres/netiq-identity-governance-administration/idm/w/identity_mgr_tips/22902/howto-use-ajax-with-jquery-in-user-application-forms

That is how I get the value from the directory and the ajax call I do:

var cert = IDVault.get(null, {userDN}, "User", "userCertificate");//It is how is it in Microfocus environment
alert(cert.length);//1
alert(cert[0].length);//2111
$.ajax( {
    type:                    'POST',
    url:                        {JSP_URL},
    dataType: "text",
    contentType: "application/octet-stream",
    processData: false,
    data:                    cert,
    success: function( data )
    {
        ajax_display( field, url, data );
    },
    error:    function( xhr, status )
    {
        ajax_display( field, url, xhr.status + " / " + status );
    },
    timeout: 9000
} )

The code in JSP file:

InputStream inputStreamAjax = request.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStreamAjax.read(buffer)) != -1) {
    byteArrayOutputStream.write(buffer, 0, length);
}

byte[] certificateBytes = byteArrayOutputStream.toByteArray();
System.out.println(certificateBytes.length);//2893
ByteArrayInputStream inputStream = new ByteArrayInputStream(certificateBytes);
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");

try {
    X509Certificate cert = (X509Certificate) certFactory.generateCertificate(inputStream);
} catch (Exception e) {
    e.printStackTrace();
}

//Send Certificate back to form....

This error arises on the try catch so it is printed on the catalina.out:

java.security.cert.CertificateException: Could not parse certificate: java.io.IOException: Invalid BER/DER data (too huge?)

What am I doing wrong?

Edited: If I check the length of the bytes in var cert before calling ajax it is 2111, but if I check inside jsp file, prior to parsing, in certificateBytes variable It is 2983. Where is that difference summing? (Code is edited with the position of these checkings)

0

There are 0 best solutions below