CryptoSys PKI Toolkit Manual

X509_QueryCert

X509_QueryCert queries an X.509 certificate file for selected information.

VB6/VBA Syntax

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

Parameters

strDataOut
[out] String to receive the output.
nOutChars
[in] Long specifying the length of the output string.
strFileIn
[in] String with name of X.509 certificate file (or base64 representation).
strQuery
[in] String specifying the query (see Remarks below).
nOptions
[in] Long option flags:
PKI_DEFAULT (0) for default options
PKI_QUERY_GETTYPE to return the type of data returned for a given query.

C/C++ Syntax

long _stdcall X509_QueryCert(char *szOutput, long nOutChars, const char *szCertFile, const char *szQuery, long nOptions);

Returns (VB6/C)

Long: If successful, the return value is a positive integer indicating 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.

.NET Equivalent

X509.QueryCert Method (String, String)
X509.QueryCert Method (String, String, X509.Options)

Remarks

This function queries a given X.509 certificate file for selected information. Both binary BER-encoded and PEM-encoded files can be read automatically. The VB6/C functions behave 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 strDataOut 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 strDataOut. However, note that the C#/VB.NET methods always return a string. The query string is case-insensitive, so "version", "Version" and "VeRsIoN" are all valid.

Valid queries are:

Query StringSearches forData Type
versionX.509 version number, e.g. 3. Number
serialNumberSerial number in hex-encoded formatString
signatureAlgorithmSignature algorithm used, e.g. "sha1WithRSAEncryption". String
sigAlgIdID of signature algorithm used, see PKI_SIG_ valuesNumber
signatureValueSignature value in hex-encoded formatString
notBeforeDate on which the certificate validity period begins in format yyyy-mm-ddThh:nn:ssZString
notAfterDate on which the certificate validity period ends in format yyyy-mm-ddThh:nn:ssZString
issuerNameDistinguished name (DN) of entity who has signed and issued the certificateString
subjectNameDistinguished name (DN) of the subjectString
subjectPublicKeyAlgorithmAlgorithm used in subject's public key, e.g. "dsa". String
subjectKeyIdentifierThe subject key identifier extension, if present, in hex-encoded formatString
authorityKeyIdentifierThe authority key identifier extension, if present, in hex-encoded formatString
rfc822NameInternet mail address contained in a subjectAltName extension, if presentString
isCAReturns 1 if the subject type is a CA, otherwise returns 0. Number
keyUsageStringkeyUsage flags in text format, e.g. "digitalSignature,nonRepudiation"String
extKeyUsageStringextKeyUsage purposes in text format, e.g. "codeSigning,timeStamping"String
cRLDistributionPointsURIFirst URI found in cRLDistributionPoints, if anyString
authorityInfoAccessURIFirst URI found in authorityInfoAccess, if anyString
[ New in version 3.5]

Some of these queries duplicate existing functions, e.g. the query "notAfter" produces the same result as X509_CertExpiresOn.

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)

Example

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 indicates a number, 2 indicates a string, and -29 indicates an invalid query.

See Also

X509_KeyUsageFlags X509_CertSubjectName

[Contents] [Index]

[HOME]   [NEXT: X509_ReadStringFromFile...]

Copyright © 2004-10 D.I. Management Services Pty Ltd. All rights reserved.