Error trying to install Certificate on browser using JS

0

In my code I have a button that calls a JS file, which in turn calls a PHP file that has the API endpoints that I need to generate certificate, and sends the response back that I use in certificate variable below.

I'm generating the CSR myself using the openssl command.

My code to install cert on the browser is as follows (certificate is where I pass the certificate as response that I get from Entrust APIs)-

I'm using the code off of this page- https://blogs.msdn.microsoft.com/alejacma/2009/01/28/how-to-create-a-certificate-request-with-certenroll-javascript/ (the 2nd grey block right below- following Javascript sample shows how to install the response from the CA)

function installCertificate(certificate) {
    try {
        var objEnroll = objCertEnrollClassFactory.CreateObject("X509Enrollment.CX509Enrollment");
        objEnroll.Initialize(1); // ContextUser

        objEnroll.InstallResponse(0, certificate, 6, "");

    } catch (ex) {
        swal('Error', 'Something went wrong installing Client Certificate', 'error');
        console.log("exception- " + ex.description);
    }
}

The error that I have is (from the catch block)-

CertEnroll::CX509Enrollment::InstallResponse: Cannot find object or property. 0x80092004 (-2146885628 CRYPT_E_NOT_FOUND)

I'm not sure what it means by Cannot find object or property as it's not too verbose.

PS: If I save the response from the API as a .crt file and open it (simple double click), the certificate values look correct along with the certificate chain.

javascript
ssl
certificate
certenroll
asked on Stack Overflow Oct 15, 2019 by manishk

1 Answer

0

After a lot of trawling I finally made it work. For anyone wondering, heres my solution-

The part of the code that generates the CSR is the 1st grey block on this page- https://blogs.msdn.microsoft.com/alejacma/2009/01/28/how-to-create-a-certificate-request-with-certenroll-javascript.

The CSR that it comes back with has a -----BEGIN NEW CERTIFICATE REQUEST----- at the top and -----END NEW CERTIFICATE REQUEST----- at the bottom, and line breaks after each line.

I removed the top line and all line breaks using preg_replace like below, and as at this point the CSR does not have a line break and is just a simple string, I used a str_replace to remove the last part of it.

$csr = preg_replace('/^.+\n/', '', $csr);  
$csr = str_replace("-----END NEW CERTIFICATE REQUEST-----","", $csr);

TLDR- Removed the top and bottom line from the CSR and all the line breaks that were there. I think it had to do with how the CSR was formatted.

answered on Stack Overflow Oct 16, 2019 by manishk

User contributions licensed under CC BY-SA 3.0