Reads an X.509 certificate into a base64 string from PKCS-12 PFX/.p12 data.
Public Declare Function X509_ReadCertStringFromPFX Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strPfxFile As String, ByVal strPassword As String, ByVal nOptions As Long) As Long
nRet = X509_ReadCertStringFromPFX(strOutput, nOutChars, strPfxFile, strPassword, nOptions) As Long
long __stdcall X509_ReadCertStringFromPFX(char *szOutput, long nOutChars, const char *szPfxFile, const char *szPassword, long nOptions);
""
if certificate is not encryptedIf successful, the return value is a positive number indicating the number of characters in or required for the output string; otherwise it returns a negative error code.
Public Function x509ReadCertStringFromPFX
(szPfxFile As String, szPassword As String, Optional nOptions As Long = 0) As String
X509.ReadCertStringFromPFX Method
static std::string dipki::X509::ReadCertStringFromPFX (const std::string &inputFile, const std::string &password)
static X509.read_cert_string_from_pfx(inputfile, password)
For the "raw" VBA/C function, the user must allocate an output string buffer szOutput of the required length. Specify a zero nOutChars or an empty string for szOutput to find the required length. ANSI C users must add one to this value when allocating memory.
The default behaviour is to extract one certificate from the PFX file and output as a base64 string representation of the extracted X.509 certificate.
It will attempt to find a matching certificate for any private key, otherwise it will save
the first pkcs-12-certBag
found in the PFX file containing a x509Certificate
.
Both unencrypted certificates and certificates encrypted with the weak 40-bit encryption used by "default"
in Microsoft and OpenSSL PKCS-12 files are supported, but not any other encryption algorithm.
If no certificate can be found the function will return a negative NO_DATA_ERROR
error code.
Dim strPfx As String Dim nChars As Long Dim strCert As String Dim strDigest As String * PKI_SHA1_CHARS Dim strQuery As String * 128 Dim strPassword As String ' Input is a PFX file in PEM format ' bob.pfx (password="password") strPfx = "-----BEGIN PKCS12-----" & _ "MIIGhAIBAzCCBkoGCSqGSIb3DQEHAaCCBjsEggY3MIIGMzCCAv8GCSqGSIb3DQEHBqCCAvAwggLsAgEAMIIC5QYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQYwDgQIawU" & _ "AVTFvAiECAggAgIICuNwEuFcRnZamZyMyIn+vH+wC5BV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read in cert as string from PFX/P12 data ' Find required length of buffer nChars = X509_ReadCertStringFromPFX(ByVal 0&, 0, strPfx, "password", 0) Debug.Print "X509_ReadCertStringFromPFX() returns " & nChars ' Allocate memory/pre-dimension strCert = String(nChars, " ") nChars = X509_ReadCertStringFromPFX(strCert, Len(strCert), strPfx, "password", 0) ' Cert is in a base64 string Debug.Print Left(strCert, 150) & "..." ' Query the cert for subject name nChars = X509_QueryCert(strQuery, Len(strQuery), strCert, "subjectName", 0) Debug.Print "subjectName='" & Left(strQuery, nChars) & "'" ' Compute the SHA-1 thumbprint of the cert nChars = X509_CertThumb(strCert, strDigest, Len(strDigest), 0) Debug.Print "SHA-1(cert)=" & Left(strDigest, nChars)
This should result in output as follows:
X509_ReadCertStringFromPFX() returns 740 MIICJzCCAZCgAwIBAgIQRjRrx4AAVrwR024uzV1x0DANBgkqhkiG9w0BAQUFADASMRAwDgYDVQQDEwdDYXJsUlNBMB4XDTk5MDkxOTAxMDkwMloXDTM5MTIzMTIzNTk1OVowETEPMA0GA1UEAxMGQm... subjectName='CN=BobRSA' SHA-1(cert)=63f046d2dd7042e51fdc26a511ef7c81ea622d8b
Dim strCertString As String
strCertString = x509ReadCertStringFromPFX("alice.p12", "password")
Debug.Print strCertString
Debug.Print "CertThumb=" & x509CertThumb(strCertString)
Debug.Print "HashIssuerAndSN=" & x509HashIssuerAndSN(strCertString)