Extracts an X.509 certificate from a PKCS-7 "certs-only" certificate chain file
(typically saved with extension .p7c
or .p7b
),
saving the output directly as a new file.
Public Declare Function X509_GetCertFromP7Chain Lib "diCrPKI.dll"
(ByVal strOutputFile As String, ByVal strP7cFile As String, ByVal nIndex As Long, ByVal nOptions As Long) As Long
nRet = X509_GetCertFromP7Chain(strOutputFile, strP7cFile, nIndex, nOptions) As Long
long __stdcall X509_GetCertFromP7Chain(const char *szNewCertFile, const char *szP7cFile, long nIndex, long nOptions);
If successful and nIndex is greater than zero, it returns the number of bytes written to the output file, which may be zero if no certificate could be found at the given index. If an error occurred, it returns a negative error code.
X509.GetCertFromP7Chain Method
static bool dipki::X509::GetCertFromP7Chain (const std::string &outputFile, const std::string &inputFile, int index)
static X509.get_cert_from_p7(outfile, p7file, index=1)
If nIndex is specified as a number greater than zero, the nIndex'th certificate found in the list, if any, will be extracted and saved directly as a DER-encoded X.509 certificate file. This function will also extract certificates from CMS signed data objects, too.
[New in v12.2] To find the number of certificates in the chain, use X509_GetCertCountInP7Chain. The old (deprecated) way to find the count of certificates was to set nIndex to zero.
nBytes = X509_GetCertFromP7Chain("cert2.cer", "certs.p7c", 2, 0)
will extract the second certificate in certs.p7c
and create a new X.509 certificate file called cert2.cer
containing nBytes
bytes.
The following example shows how to extract all the certificates from a PKCS-7 CertList file
Dim nRet As Long Dim strListFile As String Dim strCertFile As String Dim nCerts As Long Dim iCert As Long strListFile = "bob.p7b" ' How many certificates? - NB new function in [v12.2] nCerts = X509_GetCertCountInP7Chain(strListFile, 0) Debug.Print "X509_GetCertCountInP7Chain returns " & nCerts & " for " & strListFile ' Enumerate through them all If nCerts > 0 Then For iCert = 1 To nCerts strCertFile = "bobcert" & iCert & ".cer" nRet = X509_GetCertFromP7Chain(strCertFile, strListFile, iCert, 0) Debug.Print "X509_GetCertFromP7Chain(" & iCert & ") returns " _ & nRet & "->" & strCertFile Next End If
This should result in output as follows:
X509_GetCertCountInP7Chain() returns 2 for bob.p7b X509_GetCertFromP7Chain(1) returns 555->bobcert1.cer X509_GetCertFromP7Chain(2) returns 495->bobcert2.cer
where, in this example, the file bob.p7b
contains two X.509 certificates
of size 555 and 495 bytes respectively.
X509_ReadCertStringFromP7Chain X509_GetCertCountInP7Chain CMS_QuerySigData