Queries an X.509 certificate file for selected information.
Public Declare Function X509_QueryCert Lib "diCrPKI.dll"
(ByVal strDataOut As String, ByVal nOutChars As Long,
ByVal strFileIn As String, ByVal strQuery As String, ByVal nOptions As Long) As Long
nRet = X509_QueryCert(strDataOut, nOutChars, strFileIn, strQuery,
nOptions) As Long
long __stdcall X509_QueryCert(char *szOutput, long nOutChars, const char *szCertFile, const char *szQuery, long nOptions);
If successful, the return value is a positive integer giving either the result itself (if the result is a number) or the number of characters in the output string (if the query is looking for a string). If the item queried is not present, the return value is zero. If there is an error (e.g. a missing or invalid certificate file), it returns a negative error code.
Public Function x509QueryCert
(szCertFile As String, szQuery As String, Optional nOptions As Long = 0) As String
static std::string dipki::X509::QueryCert (const std::string &certFile, const std::string &query, OutputOpts outOpts=OutputOpts::Default_OutputOpt)
static X509.query_cert(filename, query, opts=0)
Valid queries are (case-insenitive):
Query String | Returns | Data Type |
---|---|---|
version | X.509 version number, e.g. 1 or 3. | Number |
serialNumber | Serial number in hex-encoded format | String |
signatureAlgorithm | Signature algorithm used, e.g. "sha1WithRSAEncryption" . | String |
sigAlgId | ID of signature algorithm used, see PKI_SIG_* values | Number |
signatureValue | Signature value in hex-encoded format | String |
notBefore | Date on which the certificate validity period begins in format yyyy-mm-ddThh:nn:ssZ | String |
notAfter | Date on which the certificate validity period ends in format yyyy-mm-ddThh:nn:ssZ | String |
issuerName | Distinguished name (DN) of entity who has signed and issued the certificate | String |
subjectName | Distinguished name (DN) of the subject | String |
subjectPublicKeyAlgorithm | Algorithm used in subject's public key,
e.g. "dsa" or "rsaEncryption" . | String |
subjectKeyIdentifier | The subject key identifier extension, if present, in hex-encoded format | String |
authorityKeyIdentifier | The authority key identifier extension, if present, in hex-encoded format | String |
rfc822Name | First internet mail address found contained in a subjectAltName extension, if present | String |
isCA | Returns 1 if the subject type is a CA, otherwise returns 0. | Number |
keyUsageString | keyUsage flags in text format, e.g. "digitalSignature,nonRepudiation" | String |
extKeyUsageString | extKeyUsage purposes in text format, e.g. "codeSigning,timeStamping" | String |
cRLDistributionPointsURI | First URI found in cRLDistributionPoints , if any | String |
authorityInfoAccessURI | First URI found in authorityInfoAccess , if any | String |
subjectAltName | Subject alternative name extension, if present. [New in v10.0] | String |
hashAlgorithm | Hash algorithm used in signature, e.g. "sha256" . [New in v12.0] | String |
pssParams | Parameters used for RSA-PSS (if applicable). [New in v12.0] | String |
Some of these queries duplicate or complement existing functions:
"notBefore"
= X509_CertIssuedOn()
"notAfter"
= X509_CertExpiresOn()
"serialNumber"
= X509_CertSerialNumber()
"subjectName"
= X509_CertSubjectName()
"issuerName"
= X509_CertIssuerName()
The "raw" VBA/C function behaves differently depending on whether the output is a string or a number. If the result data type is a number then it returns the value directly. If the result is a string, then it sets szOutput and returns the number of characters in the string. The required number of characters can be found by passing zero for nOutChars or a null string for szOutput. ANSI C users must add one to this value when allocating memory.
Note that the VBA wrapper function and the C#/VB.NET methods always return a string, which is different from the behaviour of the raw VB6/C function.
To find out the type of data returned for a given query, use the PKI_QUERY_GETTYPE option.
The function will return either PKI_QUERY_NUMBER
(1) or PKI_QUERY_STRING
(2),
or a negative "invalid query" error.
For example
nRet = X509_QueryCert("", 0, "", "version", PKI_QUERY_GETTYPE);
will return PKI_QUERY_NUMBER.
If an attribute value is encoded in a multi-byte-character string format (such as UTF8String or BMPString),
the value will be expressed as a hexadecimal-encoded string [NB changed in v3.9]
preceded by the hash symbol ('#'
U+0023), e.g.
"C=TW;O=E8 A1 8C E6 94 BF E9 99 A2" (v3.8 and earlier)
"C=TW;O=#E8A18CE694BFE999A2" (v3.9 and later)
Use the PKI_X509_LATIN1 option to return the string encoded in Latin-1, if possible, to help with display issues.
The output from the queries "issuerName"
and "subjectName"
using the PKI_X509_LDAP option
are suitable as content for the <X509IssuerName>
and <X509SubjectName>
nodes,
respectively, in the <X509Data>
node of an XML-DSIG document.
This example queries information from a sample X.509 certificate file.
Dim nRet As Long Dim strOutput As String Dim strQuery As String Dim strCertFile As String strCertFile = "CarlRSASelf.cer" ' Make a large buffer to receive output strOutput = String(512, " ") strQuery = "version" nRet = X509_QueryCert(strOutput, Len(strOutput), strCertFile, strQuery, 0) Debug.Print strQuery & "=" & nRet strQuery = "serialNumber" nRet = X509_QueryCert(strOutput, Len(strOutput), strCertFile, strQuery, 0) If nRet <= 0 Then Exit Sub ' catch error Debug.Print strQuery & "=" & Left(strOutput, nRet) strQuery = "signatureAlgorithm" nRet = X509_QueryCert(strOutput, Len(strOutput), strCertFile, strQuery, 0) If nRet <= 0 Then Exit Sub ' catch error Debug.Print strQuery & "=" & Left(strOutput, nRet) strQuery = "notAfter" nRet = X509_QueryCert(strOutput, Len(strOutput), strCertFile, strQuery, 0) If nRet <= 0 Then Exit Sub ' catch error Debug.Print strQuery & "=" & Left(strOutput, nRet)
For the S/MIME test file CarlRSASelf.cer
, the output is as follows
version=3 serialNumber=46346bc7800056bc11d36e2e9ff25020 signatureAlgorithm=sha1WithRSAEncryption notAfter=2039-12-31T23:59:59Z
To query the type of data returned for a given query.
Dim nRet As Long Dim strQuery As String ' Find out the data type for a given query strQuery = "version" nRet = X509_QueryCert("", 0, "", strQuery, PKI_QUERY_GETTYPE) Debug.Print "Type(" & strQuery & ")=" & nRet strQuery = "serialNumber" nRet = X509_QueryCert("", 0, "", strQuery, PKI_QUERY_GETTYPE) Debug.Print "Type(" & strQuery & ")=" & nRet strQuery = "NotAValidQuery" nRet = X509_QueryCert("", 0, "", strQuery, PKI_QUERY_GETTYPE) Debug.Print "Type(" & strQuery & ")=" & nRet
This should produce output
Type(version)=1 Type(serialNumber)=2 Type(NotAValidQuery)=-29
where 1 (PKI_QUERY_NUMBER
) indicates a number, 2 (PKI_QUERY_STRING
) indicates a string, and -29 (BAD_QUERY_ERROR
) indicates an invalid query.
Debug.Print x509TextDumpToString("AliceRSASignByCarl.cer")
Debug.Print x509QueryCert("AliceRSASignByCarl.cer", "subjectName")
Debug.Print asn1TextDumpToString("AliceRSASignByCarl.cer")
X509_KeyUsageFlags X509_CertSubjectName