CryptoSys PKI Toolkit Manual

X509_ReadStringFromFile

X509_ReadStringFromFile creates a base64 string of a X.509 certificate file.

VB6/VBA Syntax

Public Declare Function X509_ReadStringFromFile Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strCertFile As String, ByVal nOptions As Long) As Long

nRet = X509_ReadStringFromFile(strOutput, nOutChars, strCertFile, nOptions) As Long

Parameters

strOutput
[out] String to receive the output.
nOutChars
[in] Long specifying the length of the output string.
strCertFile
[in] String with name of X.509 certificate file.
nOptions
[in] Long option flags: not used. Set to zero.

C/C++ Syntax

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

Returns (VB6/C)

Long: If successful, the return value is a positive number indicating the number of characters in the output string; otherwise it returns a negative error code.

.NET Equivalent

X509.ReadStringFromFile Method

Remarks

This function reads in the complete X.509 certificate as a continuous base64 string. The same certificate will always produce exactly the same string. This string can be used, for example, when creating an XML file which requires the certificate as an attribute, e.g.

<?xml version="1.0" encoding="UTF-8"?>
<Comprobante fecha="2005-09-02T16:30:00" folio="1" noAprobacion="1" 
noCertificado="00001000000000000114" 
certificado="MIIDWjCCAkKgAwIBAgIUMDAwMDExMDAwMDAyMDA..."
serie="A" version="1.0">

The required number of characters can be found by passing zero for nOutChars or a null string for strOutput. The output string must be long enough to receive the complete output or a "buffer too short" error will result.

Example

This example reads in the base64 string from the SAT Mexico test certificate and displays it. It then saves the string as a new certificate and uses the X509_CertThumb function to check that the two files are identical.

Dim nRet As Long
Dim strCertString As String
Dim strCertFile As String
Dim strNewFile As String
Dim strThumb1 As String
Dim strThumb2 As String

strCertFile = "aaa010101aaa_CSD.cer"
' Read in certificate file's data to a string
nRet = X509_ReadStringFromFile("", 0, strCertFile, 0)
Debug.Print "X509_ReadStringFromFile returns " & nRet
If nRet <= 0 Then Exit Sub  ' ERROR
strCertString = String(nRet, " ")
nRet = X509_ReadStringFromFile(strCertString, Len(strCertString), strCertFile, 0)
Debug.Print "For certificate '" & strCertFile & "':"
Debug.Print strCertString

' Save the string to a new certificate file, this time in PEM format
strNewFile = "aaa010101aaa_CSD.pem.cer"
nRet = X509_SaveFileFromString(strNewFile, strCertString, PKI_X509_FORMAT_PEM)
Debug.Print "X509_SaveFileFromString returns " & nRet
If nRet = 0 Then
    Debug.Print "Created new certificate file '" & strNewFile & "'"
End If

' Check that the two certificate files are identical by computing their SHA-1 thumbprints
strThumb1 = String(PKI_SHA1_CHARS, " ")
strThumb2 = String(PKI_SHA1_CHARS, " ")
nRet = X509_CertThumb(strCertFile, strThumb1, Len(strThumb1), 0)
nRet = X509_CertThumb(strNewFile, strThumb2, Len(strThumb2), 0)
Debug.Print "SHA-1(old)=" & strThumb1
Debug.Print "SHA-1(new)=" & strThumb2
If strThumb1 = strThumb2 Then
    Debug.Print "Certificates are identical"
Else
    Debug.Print "ERROR: certificates do not match"
End If

The output should look like this (the 1152-character certificate string has been edited)

X509_ReadStringFromFile returns 1152
For certificate 'aaa010101aaa_CSD.cer':
MIIDWjCCAkKgAwIBAgIUMDAwMDEx...7D5F8SB7Li3zt9vbbMzBc5xGg==
X509_SaveFileFromString returns 0
Created new certificate file 'aaa010101aaa_CSD.pem.cer'
SHA-1(old)=5791650d63340129568c1eecc6566b2fdd8bbd4c
SHA-1(new)=5791650d63340129568c1eecc6566b2fdd8bbd4c
Certificates are identical

See Also

X509_SaveFileFromString

[Contents] [Index]

[HOME]   [NEXT: X509_SaveFileFromString...]

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