Stefan Michev
Stefan Michev

Reputation: 5093

Import client certificate in IE with javascript

I trying to import/install a client certificate into IE but I'm getting following error in my js code.

function ImportClientCertificate() 
      {        
          try {
              var objCertEnrollClassFactory = document.getElementById("objCertEnrollClassFactory");

              var objEnroll = objCertEnrollClassFactory.CreateObject("X509Enrollment.CX509Enrollment");

              var sPKCS7 = "-----BEGIN CERTIFICATE-----" +              
              "MIIDADCCAmkCCQ..." +
              "-----END CERTIFICATE-----"

              objEnroll.Initialize(1);  

              //->this line causes the exception
              objEnroll.InstallResponse(3, sPKCS7, 1, "correctpassword");
          }
          catch (ex) {
              alert(ex.description);
              /*Exception being thrown: CertEnroll::CX509Enrollment::InstallResponse: Access is denied. 0x80070005 (WIN32: 5 ERROR_ACCESS_DENIED)*/
          }
      }

What could be the reason for this exception? I already tried to set the security level in IE to low but it doesn't helped. Manual installation of the cert into the users private cert store works fine.

Any help is highly appreciated.

Upvotes: 1

Views: 873

Answers (2)

Joe Strommen
Joe Strommen

Reputation: 1234

I'm surprised this worked for you!

I found that I had to change the restrictions parameter (3 in your example) to be either 0 or 4. This is based on the MSDN documentation at https://msdn.microsoft.com/en-us/library/windows/desktop/aa378051(v=vs.85).aspx:

E_ACCESSDENIED

This method was called from the web and either AllowNoOutstandingRequest or AllowUntrustedCertificate was specified in the Restrictions parameter.

Upvotes: 1

Stefan Michev
Stefan Michev

Reputation: 5093

After two days of research on the net, I finally find out how to get this script working. The only solution I found for IE 11, is to enable following option in the IE settings box.

ActiveX settings

Upvotes: 0

Related Questions