Attribute VB_Name = "basCrPKI"
' $Id: basCrPKI.bas $

' This module contains the full list of declaration statements
' for the CryptoSys (tm) PKI Toolkit library.
' VB6/VBA version.
' Last updated:
'   $Date: 2012-01-14 03:06Z $
'   $Revision: 3.8.0 $

'************************* COPYRIGHT NOTICE*************************
' Copyright (c) 2002-12 DI Management Services Pty Limited.
' All rights reserved.
' This code may only be used by licensed users.
' The latest version of the CryptoSys PKI Toolkit and a licence
' may be obtained from <www.cryptosys.net>.
' Refer to licence for conditions of use.
' This copyright notice must always be left intact.
'****************** END OF COPYRIGHT NOTICE*************************

Option Explicit
Option Base 0

' CONSTANTS
Public Const ENCRYPT As Long = -1
Public Const DECRYPT As Long = 0
Public Const PKI_DIR_ENCRYPT As Long = -1
Public Const PKI_DIR_DECRYPT As Long = 0
' Maximum number of bytes in hash digest byte array
Public Const PKI_MAX_HASH_BYTES As Long = 64
Public Const PKI_SHA1_BYTES     As Long = 20
Public Const PKI_SHA224_BYTES   As Long = 28
Public Const PKI_SHA256_BYTES   As Long = 32
Public Const PKI_SHA384_BYTES   As Long = 48
Public Const PKI_SHA512_BYTES   As Long = 64
Public Const PKI_MD5_BYTES      As Long = 16
Public Const PKI_MD2_BYTES      As Long = 16
' Maximum number of hex characters in hash digest
Public Const PKI_MAX_HASH_CHARS As Long = 2 * PKI_MAX_HASH_BYTES
Public Const PKI_SHA1_CHARS     As Long = 2 * PKI_SHA1_BYTES
Public Const PKI_SHA224_CHARS   As Long = 2 * PKI_SHA224_BYTES
Public Const PKI_SHA256_CHARS   As Long = 2 * PKI_SHA256_BYTES
Public Const PKI_SHA384_CHARS   As Long = 2 * PKI_SHA384_BYTES
Public Const PKI_SHA512_CHARS   As Long = 2 * PKI_SHA512_BYTES
Public Const PKI_MD5_CHARS      As Long = 2 * PKI_MD5_BYTES
Public Const PKI_MD2_CHARS      As Long = 2 * PKI_MD2_BYTES
' Synonym retained for backwards compatibility
Public Const PKI_MAX_HASH_LEN   As Long = PKI_MAX_HASH_CHARS
' Encryption block sizes in bytes
Public Const PKI_BLK_TDEA_BYTES     As Long = 8
Public Const PKI_BLK_AES_BYTES      As Long = 16
' Key size in bytes
Public Const PKI_KEYSIZE_TDEA_BYTES As Long = 24
Public Const PKI_KEYSIZE_MAX_BYTES  As Long = 32
' Required size for RNG seed file
Public Const PKI_RNG_SEED_BYTES     As Long = 64

' CONSTANTS USED IN OPTION FLAGS
Public Const PKI_DEFAULT       As Long = 0
' Signature algorithms
Public Const PKI_SIG_SHA1RSA   As Long = 0    ' default
Public Const PKI_SIG_MD5RSA    As Long = 1
Public Const PKI_SIG_MD2RSA    As Long = 2
Public Const PKI_SIG_SHA256RSA As Long = 3
Public Const PKI_SIG_SHA384RSA As Long = 4
Public Const PKI_SIG_SHA512RSA As Long = 5
Public Const PKI_SIG_SHA224RSA As Long = 6
' PKCS#5 Password-based encryption algorithms
Public Const PKI_PBE_SHA1_3DES As Long = 0    ' default
Public Const PKI_PBE_MD5_DES   As Long = 1
Public Const PKI_PBE_MD2_DES   As Long = 2
Public Const PKI_PBE_SHA_DES   As Long = 3
Public Const PKI_PBES2_3DES    As Long = 4    ' (Deprecated as of v3.2)
Public Const PKI_PBE_PBES2     As Long = &H1000
' Message digest hash algorithms
Public Const PKI_HASH_SHA1     As Long = 0    ' default
Public Const PKI_HASH_MD5      As Long = 1
Public Const PKI_HASH_MD2      As Long = 2
Public Const PKI_HASH_SHA256   As Long = 3
Public Const PKI_HASH_SHA384   As Long = 4
Public Const PKI_HASH_SHA512   As Long = 5
Public Const PKI_HASH_SHA224   As Long = 6
Public Const PKI_HASH_MODE_TEXT  As Long = &H10000
' RSA key generation
Public Const PKI_KEYGEN_INDICATE As Long = &H1000000 ' (Changed from &H10 in v3.3)
Public Const PKI_KEY_NODELAY     As Long = &H20      ' (obsolete as of v3.3)
Public Const PKI_KEY_FORMAT_PEM  As Long = &H10000
Public Const PKI_KEY_FORMAT_SSL  As Long = &H20000

' Return values for RSA_CheckKey
Public Const PKI_VALID_PUBLICKEY   As Long = 1
Public Const PKI_VALID_PRIVATEKEY  As Long = 0

Public Const PKI_PFX_NO_PRIVKEY    As Long = &H10       ' Obsolete as of v3.8
Public Const PKI_PFX_PLAIN_CERT    As Long = &H2000000  ' New in v3.8
Public Const PKI_PFX_CLONE_KEY     As Long = &H4000000  ' New in v3.8
Public Const PKI_PFX_ALT_FORMAT    As Long = &H100000   ' New in v3.8

Public Const PKI_XML_RSAKEYVALUE   As Long = &H1
Public Const PKI_XML_EXCLPRIVATE   As Long = &H10
Public Const PKI_XML_HEXBINARY     As Long = &H100

Public Const PKI_EME_DEFAULT       As Long = &H0
Public Const PKI_EME_PKCSV1_5      As Long = &H0   ' alternate for default
Public Const PKI_EME_OAEP          As Long = &H10
Public Const PKI_EMSIG_DEFAULT     As Long = &H20
Public Const PKI_EMSIG_PKCSV1_5    As Long = &H20  ' alternate for default
Public Const PKI_EMSIG_DIGESTONLY  As Long = &H1000
Public Const PKI_EMSIG_DIGINFO     As Long = &H2000
Public Const PKI_EMSIG_ISO9796     As Long = &H100000

Public Const PKI_X509_FORMAT_PEM    As Long = &H10000
Public Const PKI_X509_FORMAT_BIN    As Long = &H20000
Public Const PKI_X509_REQ_KLUDGE   As Long = &H100000
Public Const PKI_X509_NO_TIMECHECK As Long = &H200000   ' New in v3.5
Public Const PKI_X509_LATIN1       As Long = &H400000
Public Const PKI_X509_UTF8         As Long = &H800000
Public Const PKI_X509_AUTHKEYID   As Long = &H1000000
Public Const PKI_X509_NO_BASIC    As Long = &H2000000
Public Const PKI_X509_CA_TRUE     As Long = &H4000000
Public Const PKI_X509_VERSION1    As Long = &H8000000

Public Const PKI_CMS_FORMAT_BASE64  As Long = &H10000
Public Const PKI_CMS_EXCLUDE_CERTS    As Long = &H100
Public Const PKI_CMS_EXCLUDE_DATA     As Long = &H200
Public Const PKI_CMS_INCLUDE_ATTRS    As Long = &H800
Public Const PKI_CMS_ADD_SIGNTIME    As Long = &H1000
Public Const PKI_CMS_ADD_SMIMECAP    As Long = &H2000
Public Const PKI_CMS_CERTS_ONLY       As Long = &H400
Public Const PKI_CMS_NO_OUTER     As Long = &H2000000
Public Const PKI_CMS_ALT_ALGID    As Long = &H4000000
Public Const PKI_CMS_BIGFILE      As Long = &H8000000   ' New in v3.7

' CONSTANTS USED IN RSA EXPONENT PARAMETER
Public Const PKI_RSAEXP_EQ_3       As Long = 0
Public Const PKI_RSAEXP_EQ_5       As Long = 1
Public Const PKI_RSAEXP_EQ_17      As Long = 2
Public Const PKI_RSAEXP_EQ_257     As Long = 3
Public Const PKI_RSAEXP_EQ_65537   As Long = 4

' CONSTANTS USED IN KEY USAGE FLAG
Public Const PKI_X509_KEYUSAGE_DIGITALSIGNATURE  As Long = &H1
Public Const PKI_X509_KEYUSAGE_NONREPUDIATION    As Long = &H2
Public Const PKI_X509_KEYUSAGE_KEYENCIPHERMENT   As Long = &H4
Public Const PKI_X509_KEYUSAGE_DATAENCIPHERMENT  As Long = &H8
Public Const PKI_X509_KEYUSAGE_KEYAGREEMENT      As Long = &H10
Public Const PKI_X509_KEYUSAGE_KEYCERTSIGN       As Long = &H20
Public Const PKI_X509_KEYUSAGE_CRLSIGN           As Long = &H40
Public Const PKI_X509_KEYUSAGE_ENCIPHERONLY      As Long = &H80
Public Const PKI_X509_KEYUSAGE_DECIPHERONLY      As Long = &H100

' SPECIFIC X509 RETURN VALUES
Public Const PKI_X509_EXPIRED        As Long = -1
Public Const PKI_X509_VALID_NOW      As Long = 0
Public Const PKI_X509_VERIFY_SUCCESS As Long = 0
Public Const PKI_X509_VERIFY_FAILURE As Long = -1
Public Const PKI_X509_REVOKED        As Long = 1
Public Const PKI_X509_INVALID        As Long = 1

' RETURN VALUES FOR CNV_CheckUTF
Public Const PKI_CHRS_NOT_UTF8   As Long = 0
Public Const PKI_CHRS_ALL_ASCII  As Long = 1
Public Const PKI_CHRS_ANSI8      As Long = 2
Public Const PKI_CHRS_MULTIBYTE  As Long = 3
' Options for CNV_ByteEncoding [New in v3.6]
Public Const PKI_CNV_UTF8_FROM_LATIN1 As Long = 1
Public Const PKI_CNV_LATIN1_FROM_UTF8 As Long = 2

' FLAGS AND RETURN VALUES FOR X.509 AND CMS QUERY FUNCTIONS
Public Const PKI_QUERY_GETTYPE    As Long = &H100000
Public Const PKI_QUERY_NUMBER     As Long = &H1
Public Const PKI_QUERY_STRING     As Long = &H2

' OPTIONS FOR RNG FUNCTIONS
Public Const PKI_RNG_STRENGTH_112 As Long = &H0 ' default
Public Const PKI_RNG_STRENGTH_128 As Long = &H1

' Block cipher algorithm options
Public Const PKI_BC_TDEA    As Long = &H10  ' )
Public Const PKI_BC_DESEDE3 As Long = &H10  ' ) equiv. synonyms for Triple DES
Public Const PKI_BC_3DES    As Long = &H10  ' )
Public Const PKI_BC_AES128  As Long = &H20
Public Const PKI_BC_AES192  As Long = &H30
Public Const PKI_BC_AES256  As Long = &H40
' Block cipher mode options
Public Const PKI_MODE_ECB  As Long = &H0
Public Const PKI_MODE_CBC  As Long = &H100
Public Const PKI_MODE_OFB  As Long = &H200
Public Const PKI_MODE_CFB  As Long = &H300
Public Const PKI_MODE_CTR  As Long = &H400
' Key transport algorithms
Public Const PKI_KT_RSAES_PKCS As Long = &H0    ' Default
' Key derivation functions
Public Const PKI_KDF_KDF2    As Long = &H0      ' Default

' FUNCTION DECLARATIONS

' CRYPTOGRAPHIC MESSAGE SYNTAX (CMS) FUNCTIONS
Public Declare Function CMS_MakeEnvData Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strFileIn As String, ByVal strCertList As String, ByVal strSeed As String, ByVal nSeedLen As Long, ByVal nOptions As Long) As Long
Public Declare Function CMS_MakeEnvDataFromString Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strDataIn As String, ByVal strCertList As String, ByVal strSeed As String, ByVal nSeedLen As Long, ByVal nOptions As Long) As Long
Public Declare Function CMS_ReadEnvData Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strFileIn As String, ByVal strCertFile As String, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_ReadEnvDataToString Lib "diCrPKI.dll" (ByVal strDataOut As String, ByVal nDataLen As Long, ByVal strFileIn As String, ByVal strCertFile As String, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_MakeSigData Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strFileIn As String, ByVal strCertList As String, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_MakeSigDataFromString Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strDataIn As String, ByVal strCertList As String, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_MakeSigDataFromSigValue Lib "diCrPKI.dll" (ByVal strFileOut As String, ByRef abSigValue As Byte, ByVal nSigLen As Long, ByRef abData As Byte, ByVal nDataLen As Long, ByVal strCertList As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_MakeDetachedSig Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strHexDigest As String, ByVal strCertList As String, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_ReadSigData Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strFileIn As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_ReadSigDataToString Lib "diCrPKI.dll" (ByVal strDataOut As String, ByVal nDataLen As Long, ByVal strFileIn As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_GetSigDataDigest Lib "diCrPKI.dll" (ByVal strHexDigest As String, ByVal nHexDigestLen As Long, ByVal strFileIn As String, ByVal strCertFile As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_VerifySigData Lib "diCrPKI.dll" (ByVal strFileIn As String, ByVal strCertFile As String, ByVal strHexDigest As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_QuerySigData Lib "diCrPKI.dll" (ByVal strDataOut As String, ByVal nDataLen As Long, ByVal strFileIn As String, ByVal strQuery As String, ByVal nOptions As Long) As Long
Public Declare Function CMS_QueryEnvData Lib "diCrPKI.dll" (ByVal strDataOut As String, ByVal nDataOutLen As Long, ByVal strFileIn As String, ByVal strQuery As String, ByVal nOptions As Long) As Long

' RSA PUBLIC KEY FUNCTIONS
Public Declare Function RSA_MakeKeys Lib "diCrPKI.dll" (ByVal strPubKeyFile As String, ByVal strPvkKeyFile As String, ByVal nBits As Long, ByVal nExpFermat As Long, ByVal nTests As Long, ByVal nCount As Long, ByVal strPassword As String, ByVal strSeed As String, ByVal nSeedLen As Long, ByVal nOptions As Long) As Long
Public Declare Function RSA_ReadEncPrivateKey Lib "diCrPKI.dll" (ByVal strPrivateKey As String, ByVal nKeyMaxLen As Long, ByVal strEpkFileName As String, ByVal strPassword As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_ReadPrivateKeyInfo Lib "diCrPKI.dll" (ByVal strPrivateKey As String, ByVal nKeyMaxLen As Long, ByVal strPRIFileName As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_ReadPublicKey Lib "diCrPKI.dll" (ByVal strPublicKey As String, ByVal nKeyMaxLen As Long, ByVal strKeyFileName As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_SavePublicKey Lib "diCrPKI.dll" (ByVal strOutputFile As String, ByVal strPublicKey As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_GetPublicKeyFromCert Lib "diCrPKI.dll" (ByVal strPublicKey As String, ByVal nKeyMaxLen As Long, ByVal strCertFileName As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_SaveEncPrivateKey Lib "diCrPKI.dll" (ByVal strOutputFile As String, ByVal strPrivateKey As String, ByVal nCount As Long, ByVal strPassword As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_SavePrivateKeyInfo Lib "diCrPKI.dll" (ByVal strOutputFile As String, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_GetPrivateKeyFromPFX Lib "diCrPKI.dll" (ByVal strOutputFile As String, ByVal strPfxFile As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_ToXMLString Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutputLen As Long, ByVal strKeyString As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_FromXMLString Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutputLen As Long, ByVal strXmlString As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_KeyBits Lib "diCrPKI.dll" (ByVal strKey As String) As Long
Public Declare Function RSA_KeyBytes Lib "diCrPKI.dll" (ByVal strKey As String) As Long
Public Declare Function RSA_CheckKey Lib "diCrPKI.dll" (ByVal strKey As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_KeyHashCode Lib "diCrPKI.dll" (ByVal strKeyString As String) As Long
Public Declare Function RSA_KeyMatch Lib "diCrPKI.dll" (ByVal strPrivateKey As String, ByVal strPublicKey As String) As Long
Public Declare Function RSA_RawPublic Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strPublicKey As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_RawPrivate Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strPrivateKey As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_EncodeMsg Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutputLen As Long, ByRef abMessage As Byte, ByVal nMsgLen As Long, ByVal nOptions As Long) As Long
Public Declare Function RSA_DecodeMsg Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutputLen As Long, ByRef abInput As Byte, ByVal nInputLen As Long, ByVal nOptions As Long) As Long
' [new in v3.8]
Public Declare Function RSA_PublicKeyFromPrivate Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strKeyString As String, ByVal nOptions As Long) As Long
Public Declare Function RSA_ReadPrivateKeyFromPFX 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

' X.509 CERTIFICATE FUNCTIONS
Public Declare Function X509_MakeCertSelf Lib "diCrPKI.dll" (ByVal strNewCertFile As String, ByVal strEPKFile As String, ByVal nCertNum As Long, ByVal nYearsValid As Long, ByVal strDistName As String, ByVal strExtensions As String, ByVal KeyUsageFlags As Long, ByVal strPassword As String, ByVal nOptions As Long) As Long
Public Declare Function X509_MakeCert Lib "diCrPKI.dll" (ByVal strNewCertFile As String, ByVal strIssuerCertFile As String, ByVal strSubjectPubKeyFile As String, ByVal strIssuerPvkInfoFile As String, ByVal nCertNum As Long, ByVal nYearsValid As Long, ByVal strDistName As String, ByVal strExtensions As String, ByVal KeyUsageFlags As Long, ByVal strPassword As String, ByVal nOptions As Long) As Long
Public Declare Function X509_CertRequest Lib "diCrPKI.dll" (ByVal strReqFile As String, ByVal strEPKFile As String, ByVal strDistName As String, ByVal strReserved As String, ByVal strPassword As String, ByVal nOptions As Long) As Long
Public Declare Function X509_VerifyCert Lib "diCrPKI.dll" (ByVal strCertToVerify As String, ByVal strIssuerCert As String, ByVal nOptions As Long) As Long
Public Declare Function X509_CertThumb Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal strHexHash As String, ByVal nHexHashLen As Long, ByVal nOptions As Long) As Long
Public Declare Function X509_CertIsValidNow Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal nOptions As Long) As Long
Public Declare Function X509_CertIssuedOn Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal strOutput As String, ByVal nOutputLen As Long, ByVal nOptions As Long) As Long
Public Declare Function X509_CertExpiresOn Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal strOutput As String, ByVal nOutputLen As Long, ByVal nOptions As Long) As Long
Public Declare Function X509_CertSerialNumber Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal strOutput As String, ByVal nOutputLen As Long, ByVal nOptions As Long) As Long
Public Declare Function X509_CertIssuerName Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal strOutput As String, ByVal nOutputLen As Long, ByVal strDelim As String, ByVal nOptions As Long) As Long
Public Declare Function X509_CertSubjectName Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal strOutput As String, ByVal nOutputLen As Long, ByVal strDelim As String, ByVal nOptions As Long) As Long
Public Declare Function X509_HashIssuerAndSN Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal strOutput As String, ByVal nOutputLen As Long, ByVal nOptions As Long) As Long
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
Public Declare Function X509_GetCertFromPFX Lib "diCrPKI.dll" (ByVal strOutputFile As String, ByVal strPfxFile As String, ByVal strReserved As String, ByVal nOptions As Long) As Long
Public Declare Function X509_KeyUsageFlags Lib "diCrPKI.dll" (ByVal strCertFile As String) As Long
Public Declare Function X509_QueryCert Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strFileIn As String, ByVal szQuery As String, ByVal nOptions As Long) As Long
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
Public Declare Function X509_SaveFileFromString Lib "diCrPKI.dll" (ByVal strNewCertFile As String, ByVal strCertString As String, ByVal nOptions As Long) As Long
' [new in v3.5]
Public Declare Function X509_TextDump Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strCertFile As String, ByVal nOptions As Long) As Long
Public Declare Function X509_ValidatePath Lib "diCrPKI.dll" (ByVal strCertListOrP7File As String, ByVal strTrustedCert As String, ByVal nOptions As Long) As Long

' X509 CRL FUNCTIONS [new in v3.5]
Public Declare Function X509_MakeCRL Lib "diCrPKI.dll" (ByVal strCrlFile As String, ByVal strIssuerCert As String, ByVal strIssuerKeyFile As String, ByVal strPassword As String, ByVal strRevokedCertList As String, ByVal strExtensions As String, ByVal nOptions As Long) As Long
Public Declare Function X509_CheckCertInCRL Lib "diCrPKI.dll" (ByVal strCertFile As String, ByVal strCrlFile As String, ByVal strCRLIssuerCert As String, ByVal strDate As String, ByVal nOptions As Long) As Long

' ONLINE CERTIFICATE STATUS PROTOCOL (OCSP) FUNCTIONS [new in v3.5]
Public Declare Function OCSP_MakeRequest Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strIssuerCert As String, ByVal strCertFileOrSerialNum As String, ByVal strExtensions As String, ByVal nOptions As Long) As Long
Public Declare Function OCSP_ReadResponse Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strResponseFile As String, ByVal strIssuerCert As String, ByVal strExtensions As String, ByVal nOptions As Long) As Long

' PFX (PKCS-12) FUNCTIONS
Public Declare Function PFX_MakeFile Lib "diCrPKI.dll" (ByVal strOutputFile As String, ByVal strCertFile As String, ByVal strKeyFile As String, ByVal strPassword As String, ByVal strFriendlyName As String, ByVal nOptions As Long) As Long
Public Declare Function PFX_VerifySig Lib "diCrPKI.dll" (ByVal strFileName As String, ByVal strPassword As String, ByVal nOptions As Long) As Long
    
' TRIPLE DATA ENCRYPTION ALGORITHM (TDEA/3DES/TRIPLE DES) BLOCK CIPHER FUNCTIONS
Public Declare Function TDEA_BytesMode Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByRef abInput As Byte, ByVal nDataLen As Long, ByRef abKey As Byte, ByVal bEncrypt As Long, ByVal strMode As String, ByRef abIV As Byte) As Long
Public Declare Function TDEA_HexMode Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal strInput As String, ByVal strKey As String, ByVal bEncrypt As Long, ByVal strMode As String, ByVal strIV As String) As Long
Public Declare Function TDEA_B64Mode Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal strInput As String, ByVal strKey As String, ByVal bEncrypt As Long, ByVal strMode As String, ByVal strIV As String) As Long
Public Declare Function TDEA_File Lib "diCrPKI.dll" (ByVal strFileOut As String, ByVal strFileIn As String, ByRef abKey As Byte, ByVal bEncrypt As Long, ByVal strMode As String, ByRef abIV As Byte) As Long

' GENERIC BLOCK CIPHER FUNCTIONS
Public Declare Function CIPHER_Bytes Lib "diCrPKI.dll" (ByVal fEncrypt As Long, ByRef abOutput As Byte, ByRef abData As Byte, ByVal nDataLen As Long, ByRef abKey As Byte, ByRef abIV As Byte, ByVal strAlgAndMode As String, ByVal nOptions As Long) As Long
Public Declare Function CIPHER_Hex Lib "diCrPKI.dll" (ByVal fEncrypt As Long, ByVal strOutput As String, ByVal nOutChars As Long, ByVal strData As String, ByVal strKey As String, ByVal strIV As String, ByVal strAlgAndMode As String, ByVal nOptions As Long) As Long
Public Declare Function CIPHER_File Lib "diCrPKI.dll" (ByVal fEncrypt As Long, ByVal strFileOut As String, ByVal strFileIn As String, ByRef abKey As Byte, ByRef abIV As Byte, ByVal strAlgAndMode As String, ByVal nOptions As Long) As Long
Public Declare Function CIPHER_KeyWrap Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutBytes As Long, ByRef abData As Byte, ByVal nDataLen As Long, ByRef abKek As Byte, ByVal nKekLen As Long, ByVal nOptions As Long) As Long
Public Declare Function CIPHER_KeyUnwrap Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutBytes As Long, ByRef abData As Byte, ByVal nDataLen As Long, ByRef abKek As Byte, ByVal nKekLen As Long, ByVal nOptions As Long) As Long

' MESSAGE DIGEST HASH FUNCTIONS
Public Declare Function HASH_HexFromBytes Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByRef abMessage As Byte, ByVal nMsgLen As Long, ByVal nOptions As Long) As Long
' Alternative alias of HASH_HexFromBytes to cope with ANSI strings (VB6/VBA only)...
Public Declare Function HASH_HexFromString Lib "diCrPKI.dll" Alias "HASH_HexFromBytes" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strMessage As String, ByVal nMsgLen As Long, ByVal nOptions As Long) As Long
Public Declare Function HASH_HexFromFile Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strFileName As String, ByVal nOptions As Long) As Long
Public Declare Function HASH_Bytes Lib "diCrPKI.dll" (ByRef abDigest As Byte, ByVal nDigLen As Long, ByRef abMessage As Byte, ByVal nMsgLen As Long, ByVal nOptions As Long) As Long
Public Declare Function HASH_File Lib "diCrPKI.dll" (ByRef abDigest As Byte, ByVal nDigLen As Long, ByVal strFileName As String, ByVal nOptions As Long) As Long
Public Declare Function HASH_HexFromHex Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nMaxChars As Long, ByVal strMsgHex As String, ByVal nOptions As Long) As Long
    
' HMAC FUNCTIONS
Public Declare Function HMAC_Bytes Lib "diCrPKI.dll" (ByRef abDigest As Byte, ByVal nDigLen As Long, ByRef abMessage As Byte, ByVal nMsgLen As Long, ByRef abKey As Byte, ByVal nKeyLen As Long, ByVal nOptions As Long) As Long
Public Declare Function HMAC_HexFromBytes Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByRef abMessage As Byte, ByVal nMsgLen As Long, ByRef abKey As Byte, ByVal nKeyLen As Long, ByVal nOptions As Long) As Long
Public Declare Function HMAC_HexFromHex Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strMsgHex As String, ByVal strKeyHex As String, ByVal nOptions As Long) As Long

' ENCODING CONVERSION FUNCTIONS
' (See cnv* Functions below for VB6-friendly versions of these)
Public Declare Function CNV_HexStrFromBytes Lib "diCrPKI.dll" (ByVal strHex As String, ByVal nHexStrLen As Long, ByRef abData As Byte, ByVal nDataLen As Long) As Long
Public Declare Function CNV_BytesFromHexStr Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strHex As String) As Long
Public Declare Function CNV_HexFilter Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal strInput As String, ByVal nStrLen As Long) As Long
Public Declare Function CNV_B64StrFromBytes Lib "diCrPKI.dll" (ByVal strB64 As String, ByVal nB64StrLen As Long, ByRef abData As Byte, ByVal nDataLen As Long) As Long
Public Declare Function CNV_BytesFromB64Str Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strB64 As String) As Long
Public Declare Function CNV_B64Filter Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal strInput As String, ByVal nStrLen As Long) As Long
   
' UTF-8 CONVERSION/CHECK FUNCTIONS
' [Note: the following three functions are deprecated as of v3.6]
Public Declare Function CNV_UTF8FromLatin1 Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strInput As String) As Long
Public Declare Function CNV_Latin1FromUTF8 Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByVal strInput As String) As Long
Public Declare Function CNV_CheckUTF8 Lib "diCrPKI.dll" (ByVal strInput As String) As Long
' [New in v3.6]
Public Declare Function CNV_UTF8BytesFromLatin1 Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutBytes As Long, ByVal strInput As String) As Long
Public Declare Function CNV_Latin1FromUTF8Bytes Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nOutChars As Long, ByRef abInput As Byte, ByVal nBytes As Long) As Long
Public Declare Function CNV_CheckUTF8Bytes Lib "diCrPKI.dll" (ByRef abInput As Byte, ByVal nBytes As Long) As Long
Public Declare Function CNV_ByteEncoding Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutBytes As Long, ByRef abInput As Byte, ByVal nBytes As Long, ByVal nOptions As Long) As Long
' [New in v3.7]
Public Declare Function CNV_CheckUTF8File Lib "diCrPKI.dll" (ByVal strFileName As String) As Long

' PEM/BINARY FILE CONVERSIONS
Public Declare Function PEM_FileFromBinFile Lib "diCrPKI.dll" (ByVal strOutputFile As String, ByVal strFileIn As String, ByVal strHeader As String, ByVal nLineLen As Long) As Long
Public Declare Function PEM_FileToBinFile Lib "diCrPKI.dll" (ByVal strOutputFile As String, ByVal strFileIn As String) As Long

' ERROR FUNCTIONS
Public Declare Function PKI_LastError Lib "diCrPKI.dll" (ByVal strErrMsg As String, ByVal nMaxMsgLen As Long) As Long
' See also pkiGetLastError() below
Public Declare Function PKI_ErrorCode Lib "diCrPKI.dll" () As Long
Public Declare Function PKI_ErrorLookup Lib "diCrPKI.dll" (ByVal strErrMsg As String, ByVal nMaxMsgLen As Long, ByVal nErrorCode As Long) As Long
Public Declare Function PKI_PowerUpTests Lib "diCrPKI.dll" (ByVal nOptions As Long) As Long
    
' DIAGNOSTIC FUNCTIONS
' (NB PKI_Version changed in version 2.8 to ignore parameters - just use zeros)
Public Declare Function PKI_Version Lib "diCrPKI.dll" (ByVal nReserved1 As Long, ByVal nReserved2 As Long) As Long
Public Declare Function PKI_LicenceType Lib "diCrPKI.dll" (ByVal nReserved As Long) As Long
Public Declare Function PKI_CompileTime Lib "diCrPKI.dll" (ByVal strCompiledOn As String, ByVal nStrLen As Long) As Long
Public Declare Function PKI_ModuleName Lib "diCrPKI.dll" (ByVal strModuleName As String, ByVal nStrLen As Long, ByVal nOptions As Long) As Long
    
' RNG FUNCTIONS
Public Declare Function RNG_Bytes Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strSeed As String, ByVal nSeedLen As Long) As Long
' Alternative alias of RNG_Bytes to write to an ANSI string (VB6/VBA only)...
Public Declare Function RNG_String Lib "diCrPKI.dll" Alias "RNG_Bytes" (ByVal strData As String, ByVal nDataLen As Long, ByVal strSeed As String, ByVal nSeedLen As Long) As Long
Public Declare Function RNG_Number Lib "diCrPKI.dll" (ByVal nLower As Long, ByVal nUpper As Long) As Long
Public Declare Function RNG_BytesWithPrompt Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutputLen As Long, ByVal strPrompt As String, ByVal nOptions As Long) As Long
' Alternative alias of RNG_BytesWithPrompt to write to an ANSI string (VB6/VBA only)...
Public Declare Function RNG_StringWithPrompt Lib "diCrPKI.dll" Alias "RNG_BytesWithPrompt" (ByVal strData As String, ByVal nDataLen As Long, ByVal strPrompt As String, ByVal nOptions As Long) As Long
Public Declare Function RNG_Initialize Lib "diCrPKI.dll" (ByVal strSeedFile As String, ByVal nOptions As Long) As Long
Public Declare Function RNG_MakeSeedFile Lib "diCrPKI.dll" (ByVal strSeedFile As String, ByVal strPrompt As String, ByVal nOptions As Long) As Long
Public Declare Function RNG_UpdateSeedFile Lib "diCrPKI.dll" (ByVal strSeedFile As String, ByVal nOptions As Long) As Long
Public Declare Function RNG_Test Lib "diCrPKI.dll" (ByVal strFileName As String, ByVal nOptions As Long) As Long

' PADDING FUNCTIONS (New in v3.6)
Public Declare Function PAD_BytesBlock Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutputLen As Long, ByRef abInput As Byte, ByVal nInputLen As Long, ByVal nBlockLen As Long, ByVal nOptions As Long) As Long
Public Declare Function PAD_UnpadBytes Lib "diCrPKI.dll" (ByRef abOutput As Byte, ByVal nOutputLen As Long, ByRef abInput As Byte, ByVal nInputLen As Long, ByVal nBlockLen As Long, ByVal nOptions As Long) As Long
Public Declare Function PAD_HexBlock Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nMaxChars As Long, ByVal strInputHex As String, ByVal nBlockLen As Long, ByVal nOptions As Long) As Long
Public Declare Function PAD_UnpadHex Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal nMaxChars As Long, ByVal strInputHex As String, ByVal nBlockLen As Long, ByVal nOptions As Long) As Long

' MISC FUNCTIONS
Public Declare Function PWD_Prompt Lib "diCrPKI.dll" (ByVal strPassword As String, ByVal nPwdLen As Long, ByVal strCaption As String) As Long
Public Declare Function PWD_PromptEx Lib "diCrPKI.dll" (ByVal strPassword As String, ByVal nPwdLen As Long, ByVal strCaption As String, ByVal strPrompt As String, ByVal nOptions As Long) As Long
Public Declare Function WIPE_File Lib "diCrPKI.dll" (ByVal strFileName As String, ByVal nOptions As Long) As Long
Public Declare Function WIPE_Data Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nBytes As Long) As Long
' Alternative aliases of WIPE_Data to cope with Byte and String types explicitly (VB6/VBA only)...
Public Declare Function WIPE_Bytes Lib "diCrPKI.dll" Alias "WIPE_Data" (ByRef abData As Byte, ByVal nBytes As Long) As Long
Public Declare Function WIPE_String Lib "diCrPKI.dll" Alias "WIPE_Data" (ByVal strData As String, ByVal nStrLen As Long) As Long

' *** END OF CRYPTOSYS PKI DECLARATIONS

' SOME USEFUL WRAPPER FUNCTIONS
' [2006-08-11] Conversion functions updated to handle errors better.

Public Function cnvHexStrFromBytes(abData() As Byte) As String
' Returns hex string encoding of bytes in abData or empty string if error
    Dim strHex As String
    Dim nHexLen As Long
    Dim nDataLen As Long
    
    On Error GoTo CatchEmptyData
    nDataLen = UBound(abData) - LBound(abData) + 1
    nHexLen = CNV_HexStrFromBytes(vbNullString, 0, abData(0), nDataLen)
    If nHexLen <= 0 Then
        Exit Function
    End If
    strHex = String$(nHexLen, " ")
    nHexLen = CNV_HexStrFromBytes(strHex, nHexLen, abData(0), nDataLen)
    If nHexLen <= 0 Then
        Exit Function
    End If
    cnvHexStrFromBytes = Left$(strHex, nHexLen)
    
CatchEmptyData:

End Function

Public Function cnvHexStrFromString(strData As String) As String
' Returns hex string encoding of ASCII string or empty string if error
    Dim strHex As String
    Dim nHexLen As Long
    Dim nDataLen As Long
    Dim abData() As Byte
    
    If Len(strData) = 0 Then Exit Function
    abData = StrConv(strData, vbFromUnicode)
    nDataLen = UBound(abData) - LBound(abData) + 1
    nHexLen = CNV_HexStrFromBytes(vbNullString, 0, abData(0), nDataLen)
    If nHexLen <= 0 Then
        Exit Function
    End If
    strHex = String$(nHexLen, " ")
    nHexLen = CNV_HexStrFromBytes(strHex, nHexLen, abData(0), nDataLen)
    If nHexLen <= 0 Then
        Exit Function
    End If
    cnvHexStrFromString = Left$(strHex, nHexLen)
End Function

Public Function cnvBytesFromHexStr(strHex As String) As Variant
' Returns a Variant to an array of bytes decoded from a hex string
    Dim abData() As Byte
    Dim nDataLen As Long
    
    ' Set default return value that won't cause a run-time error
    cnvBytesFromHexStr = StrConv("", vbFromUnicode)
    nDataLen = CNV_BytesFromHexStr(0, 0, strHex)
    If nDataLen <= 0 Then
        Exit Function
    End If
    ReDim abData(nDataLen - 1)
    nDataLen = CNV_BytesFromHexStr(abData(0), nDataLen, strHex)
    If nDataLen <= 0 Then
        Exit Function
    End If
    ReDim Preserve abData(nDataLen - 1)
    cnvBytesFromHexStr = abData
End Function

Public Function cnvStringFromHexStr(ByVal strHex As String) As String
' Converts string <strHex> in hex format to string of ANSI chars
' with value between 0 and 255.
' E.g. "6162632E" will be converted to "abc."
    Dim abData() As Byte
    If Len(strHex) = 0 Then Exit Function
    abData = cnvBytesFromHexStr(strHex)
    cnvStringFromHexStr = StrConv(abData, vbUnicode)
End Function

Public Function cnvHexFilter(strHex As String) As String
' Returns a string stripped of any invalid hex characters
    Dim strFiltered As String
    Dim nLen As Long
    
    strFiltered = String(Len(strHex), " ")
    nLen = CNV_HexFilter(strFiltered, strHex, Len(strHex))
    If nLen > 0 Then
        strFiltered = Left$(strFiltered, nLen)
    Else
        strFiltered = ""
    End If
    cnvHexFilter = strFiltered
End Function

Public Function cnvB64StrFromBytes(abData() As Byte) As String
' Returns base64 string encoding of bytes in abData or empty string if error
    Dim strB64 As String
    Dim nB64Len As Long
    Dim nDataLen As Long
    
    On Error GoTo CatchEmptyData
    nDataLen = UBound(abData) - LBound(abData) + 1
    nB64Len = CNV_B64StrFromBytes(vbNullString, 0, abData(0), nDataLen)
    If nB64Len <= 0 Then
        Exit Function
    End If
    strB64 = String$(nB64Len, " ")
    nB64Len = CNV_B64StrFromBytes(strB64, nB64Len, abData(0), nDataLen)
    If nB64Len <= 0 Then
        Exit Function
    End If
    cnvB64StrFromBytes = Left$(strB64, nB64Len)
    
CatchEmptyData:

End Function

Public Function cnvB64StrFromString(strData As String) As String
' Returns base64 string encoding of ASCII string or empty string if error
    Dim strB64 As String
    Dim nB64Len As Long
    Dim nDataLen As Long
    Dim abData() As Byte
    
    If Len(strData) = 0 Then Exit Function
    abData = StrConv(strData, vbFromUnicode)
    nDataLen = UBound(abData) - LBound(abData) + 1
    nB64Len = CNV_B64StrFromBytes(vbNullString, 0, abData(0), nDataLen)
    If nB64Len <= 0 Then
        Exit Function
    End If
    strB64 = String$(nB64Len, " ")
    nB64Len = CNV_B64StrFromBytes(strB64, nB64Len, abData(0), nDataLen)
    If nB64Len <= 0 Then
        Exit Function
    End If
    cnvB64StrFromString = Left$(strB64, nB64Len)
End Function

Public Function cnvBytesFromB64Str(strB64 As String) As Variant
' Returns a Variant to an array of bytes decoded from a base64 string
    Dim abData() As Byte
    Dim nDataLen As Long
    
    ' Set default return value that won't cause a run-time error
    cnvBytesFromB64Str = StrConv("", vbFromUnicode)
    nDataLen = CNV_BytesFromB64Str(0, 0, strB64)
    If nDataLen <= 0 Then
        Exit Function
    End If
    ReDim abData(nDataLen - 1)
    nDataLen = CNV_BytesFromB64Str(abData(0), nDataLen, strB64)
    If nDataLen <= 0 Then
        Exit Function
    End If
    ReDim Preserve abData(nDataLen - 1)
    cnvBytesFromB64Str = abData
End Function

Public Function cnvB64Filter(strB64 As String) As String
' Returns a string stripped of any invalid base64 characters
    Dim strFiltered As String
    Dim nLen As Long
    
    strFiltered = String(Len(strB64), " ")
    nLen = CNV_B64Filter(strFiltered, strB64, Len(strB64))
    If nLen > 0 Then
        strFiltered = Left$(strFiltered, nLen)
    Else
        strFiltered = ""
    End If
    cnvB64Filter = strFiltered
End Function

Public Function cnvB64StrFromHexStr(strHex As String)
' INPUT:  hex string representing a binary value
' OUTPUT: same value encoded in base64
' REMARKS: New in v3.3
    cnvB64StrFromHexStr = cnvB64StrFromBytes(cnvBytesFromHexStr(strHex))
End Function

Public Function cnvHexStrFromB64Str(strB64 As String)
' INPUT:  base64 string representing a binary value
' OUTPUT: same value encoded in hexadecimal
' REMARKS: New in v3.3
    cnvHexStrFromB64Str = cnvHexStrFromBytes(cnvBytesFromB64Str(strB64))
End Function

Public Function pkiGetLastError() As String
' Returns the last error message as a string, if any
    Dim sErrMsg As String
    Dim nLen As Long
    
    nLen = 511
    sErrMsg = String$(nLen, " ")
    nLen = PKI_LastError(sErrMsg, nLen)
    sErrMsg = Left$(sErrMsg, nLen)
    pkiGetLastError = sErrMsg
End Function

Public Function pkiErrorLookup(nErrCode As Long) As String
' Returns the error message for error code nErrCode
    Dim sErrMsg As String
    Dim nLen As Long
    
    nLen = 127
    sErrMsg = String$(nLen, " ")
    nLen = PKI_ErrorLookup(sErrMsg, nLen, nErrCode)
    sErrMsg = Left$(sErrMsg, nLen)
    pkiErrorLookup = sErrMsg
End Function

Public Function pwdPrompt(Optional sCaption As String) As String
    Dim sPassword As String
    Dim nLen As Long
    
    nLen = 255
    sPassword = String(nLen, " ")
    nLen = PWD_Prompt(sPassword, nLen, sCaption)
    If nLen < 0 Then
        Exit Function
    ElseIf nLen > 0 Then
        pwdPrompt = Left(sPassword, nLen)
    End If
    ' Clean up local variable
    Call WIPE_String(sPassword, nLen)
End Function

Public Function rsaReadPrivateKey(strEPKFile As String, strPassword As String) As String
' Reads the private key from a PKCS-8 EncryptedPrivateKeyInfo file
' (as created by RSA_MakeKeys)
' Returns the key as a base64 string or an empty string on error
    Dim nLen As Long
    Dim lngRet As Long
    ' How long is PrivateKey string?
    nLen = RSA_ReadEncPrivateKey("", 0, strEPKFile, strPassword, 0)
    If nLen <= 0 Then
        Exit Function
    End If
    ' Pre-dimension the string to receive data
    rsaReadPrivateKey = String(nLen, " ")
    ' Read in the Private Key
    lngRet = RSA_ReadEncPrivateKey(rsaReadPrivateKey, nLen, strEPKFile, strPassword, 0)

End Function

Public Function rsaReadPublicKey(strKeyFile As String) As String
' Reads the public key from a PKCS-1 RSAPublicKey file
' (as created by RSA_MakeKeys)
' Returns the key as a base64 string or an empty string on error
    Dim nLen As Long
    Dim lngRet As Long
    ' How long is key string?
    nLen = RSA_ReadPublicKey("", 0, strKeyFile, 0)
    If nLen <= 0 Then
        Exit Function
    End If
    ' Pre-dimension the string to receive data
    rsaReadPublicKey = String(nLen, " ")
    ' Read in the Private Key
    lngRet = RSA_ReadPublicKey(rsaReadPublicKey, nLen, strKeyFile, 0)

End Function

Public Function rsaReadPrivateKeyInfo(strKeyFile As String) As String
' Like rsaReadPrivateKey but for an UNencrypted private key info file
' Returns the key as a base64 string or an empty string on error
    Dim lngKeyLen As Long
    Dim lngRet As Long
    Dim strKey As String
    ' How long is key string?
    lngKeyLen = RSA_ReadPrivateKeyInfo("", 0, strKeyFile, 0)
    If lngKeyLen <= 0 Then
        Exit Function
    End If
    ' Pre-dimension the string to receive data
    strKey = String(lngKeyLen, " ")
    ' Read in the Private Key
    lngRet = RSA_ReadPrivateKeyInfo(strKey, lngKeyLen, strKeyFile, 0)
    rsaReadPrivateKeyInfo = strKey

End Function

Public Function rsaGetPublicKeyFromCert(strCertFile As String) As String
' Reads the public key from an X.509 certificate file
' Returns the key as a base64 string or an empty string on error
    Dim nLen As Long
    Dim lngRet As Long
    ' How long is key string?
    nLen = RSA_GetPublicKeyFromCert("", 0, strCertFile, 0)
    If nLen <= 0 Then
        Exit Function
    End If
    ' Pre-dimension the string to receive data
    rsaGetPublicKeyFromCert = String(nLen, " ")
    ' Read in the Private Key
    lngRet = RSA_GetPublicKeyFromCert(rsaGetPublicKeyFromCert, nLen, strCertFile, 0)

End Function

Public Function wipeString(ByRef strToWipe As String) As String
' Securely wipes a string and returns a blank string
' To call: strToWipe = wipeString(strToWipe)
    Call WIPE_String(strToWipe, Len(strToWipe))
    wipeString = ""
End Function

Public Function wipeBytes(ByRef abToWipe() As Byte)
    Call WIPE_Data(abToWipe(0), UBound(abToWipe) + 1)
    abToWipe = StrConv("", vbFromUnicode)
End Function

Public Function padHexString(ByVal strInputHex As String, nBlockLen As Long) As String
' Adds padding to a hex string up to next multiple of block length.
' Returns a padded hex string or, on error, an empty string.
    Dim nOutChars As Long
    Dim strOutputHex As String
    ' In VB6 an uninitialised empty string is passed to a DLL as a NULL,
    ' so we append a non-null empty string!
    strInputHex = strInputHex & ""
    nOutChars = PAD_HexBlock("", 0, strInputHex, nBlockLen, 0)
    Debug.Print "Required length is " & nOutChars & " characters"
    ' Check for error
    If (nOutChars <= 0) Then Exit Function
    ' Pre-dimension output
    strOutputHex = String(nOutChars, " ")
    nOutChars = PAD_HexBlock(strOutputHex, Len(strOutputHex), strInputHex, nBlockLen, 0)
    If (nOutChars <= 0) Then Exit Function
    Debug.Print "Padded data='" & strOutputHex & "'"
    padHexString = strOutputHex
End Function

Public Function unpadHexString(strInputHex As String, nBlockLen As Long) As String
' Strips padding from a hex string.
' Returns unpadded hex string or, on error, the original input string
' -- we do this because an empty string is a valid result.
' To check for error: a valid output string is *always* shorter than the input.
    Dim nOutChars As Long
    Dim strOutputHex As String
    ' No need to query for length because we know the output will be shorter than input
    ' so make sure output is as long as the input
    strOutputHex = String(Len(strInputHex), " ")
    nOutChars = PAD_UnpadHex(strOutputHex, Len(strOutputHex), strInputHex, nBlockLen, 0)
    Debug.Print "Unpadded length is " & nOutChars & " characters"
    ' Check for error
    If (nOutChars < 0) Then
        ' Return unchanged input to indicate error
        unpadHexString = strInputHex
        Exit Function
    End If
    ' Re-dimension the output to the correct length
    strOutputHex = Left$(strOutputHex, nOutChars)
    Debug.Print "Unpadded data='" & strOutputHex & "'"
    unpadHexString = strOutputHex
End Function

' *** END OF USEFUL WRAPPER FUNCTIONS