Retrieving Web Certificates with CAPICOM

M. Gallant 07/18/2002

The following code snippet shows how to directly create a CAPICOM certificate object from a certificate deployed from a web-server. The binary array (safearray) object is obtained from the URL for the certificate using XMLHTTP class and the responseBody method. This array is converted to a string with Utils.ByteArrayToBinaryString() which is then used to initialize the certificate object with Certificate.Import. This handles certificates in binary-DER or base64-encoded DER format (including base64 files with enclosing "-----BEGIN/END CERTIFICATE-----" boundary lines. The certificate object can then be imported into any store with standard CAPICOM methods, or written to a file.


Option Explicit Dim oHttp, oUtils, oCert, body, certStr, sSource set oHTTP = CreateObject("Microsoft.XMLHTTP") set oUtils = CreateObject("CAPICOM.Utilities") set oCert = CreateObject("CAPICOM.Certificate") 'sSource = "http://home.istar.ca/~neutron/certs/migssbin.cer" 'sSource = "http://home.istar.ca/~neutron/certs/migssb64.cer" sSource = "http://home.istar.ca/~neutron/certs/migssb64a.cer" oHTTP.open "GET", sSource, False oHTTP.send body = oHTTP.responseBody 'safearray (byte) contents certStr = oUtils.ByteArrayToBinaryString(body) oCert.Import(certStr) oCert.Display() set oHTTP = nothing set oUtils = nothing set oCert = nothing