Attribute VB_Name = "basCrPKI"
' $Id: basCrPKI.bas $
' The basCrPKI module contains the full list of declaration statements
' for the CryptoSys (tm) PKI Toolkit library.
' VB6/VBA version.
' Last updated:
' $Date: 2006-08-11 13:28:00 $
' $Revision: 2.9.1 $
'************************* COPYRIGHT NOTICE*************************
' Copyright (c) 2002-6 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 PKI_DIR_ENCRYPT As Long = -1
Public Const PKI_DIR_DECRYPT As Long = 0
' Maximum number of bytes in hash digest byte array (updated v2.8)
Public Const PKI_MAX_HASH_BYTES As Long = 32
Public Const PKI_SHA1_BYTES As Long = 20
Public Const PKI_SHA256_BYTES As Long = 32
Public Const PKI_MD5_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_SHA256_CHARS As Long = 2 * PKI_SHA256_BYTES
Public Const PKI_MD5_CHARS As Long = 2 * PKI_MD5_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
' CONSTANTS USED IN OPTION FLAGS
Public Const PKI_DEFAULT As Long = 0
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_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_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 ' Added v2.8
Public Const PKI_HASH_MODE_TEXT As Long = &H10000
Public Const PKI_KEYGEN_INDICATE As Long = &H10
Public Const PKI_KEY_NODELAY As Long = &H20
Public Const PKI_KEY_FORMAT_PEM As Long = &H10000
Public Const PKI_KEY_FORMAT_SSL As Long = &H20000
Public Const PKI_PFX_NO_PRIVKEY As Long = &H10
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_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_LATIN1 As Long = &H400000
Public Const PKI_X509_UTF8 As Long = &H800000
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
' added v2.8...
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
' 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
' RETURN VALUES FOR CNV_CheckUTF (new in v2.9)
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
' 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_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_VerifySigData Lib "diCrPKI.dll" (ByVal strFileIn As String, ByVal strCertFile As String, ByVal strHexDigest 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_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
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
' 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 strEmail 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 strEmail 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
' New in Version 2.8...
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
' 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 aResult As Byte, ByRef aData As Byte, ByVal lngDataLen As Long, ByRef aKey As Byte, ByVal bEncrypt As Boolean, ByVal sMode As String, ByRef aInitV As Byte) As Long
Public Declare Function TDEA_HexMode Lib "diCrPKI.dll" (ByVal sOutput As String, ByVal sInput As String, ByVal sHexKey As String, ByVal bEncrypt As Boolean, ByVal sMode As String, ByVal sHexIV As String) As Long
Public Declare Function TDEA_B64Mode Lib "diCrPKI.dll" (ByVal sOutput As String, ByVal sInput As String, ByVal sKey64 As String, ByVal bEncrypt As Boolean, ByVal sMode As String, ByVal sIV64 As String) As Long
Public Declare Function TDEA_File Lib "diCrPKI.dll" (ByVal sFileOut As String, ByVal sFileIn As String, ByRef aKey As Byte, ByVal bEncrypt As Boolean, ByVal sMode As String, ByRef aInitV As Byte) As Long
' MESSAGE DIGEST HASH FUNCTIONS
Public Declare Function HASH_HexFromBytes Lib "diCrPKI.dll" (ByVal strHexDigest As String, ByVal nLenHex As Long, ByRef abMessage As Byte, ByVal nMsgLen As Long, ByVal nOptions As Long) As Long
' Alternative Alias to cope with ANSI strings...
Public Declare Function HASH_HexFromString Lib "diCrPKI.dll" Alias "HASH_HexFromBytes" (ByVal strHexDigest As String, ByVal nLenHex 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 strHexDigest As String, ByVal nLenHex 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
' ENCODING CONVERSION FUNCTIONS
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
' See cnvHexStrFromBytes below
Public Declare Function CNV_BytesFromHexStr Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strHex As String) As Long
' See cnvBytesFromHexStr below
Public Declare Function CNV_HexFilter Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal strInput As String, ByVal nStrLen As Long) As Long
' See cnvHexFilter below
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
' See cnvB64StrFromBytes below
Public Declare Function CNV_BytesFromB64Str Lib "diCrPKI.dll" (ByRef abData As Byte, ByVal nDataLen As Long, ByVal strB64 As String) As Long
' See cnvBytesFromB64Str below
Public Declare Function CNV_B64Filter Lib "diCrPKI.dll" (ByVal strOutput As String, ByVal strInput As String, ByVal nStrLen As Long) As Long
' See cnvB64Filter below
' UTF-8 CONVERSION/CHECK FUNCTIONS
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
' 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)
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
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 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 to write to an ANSI string...
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 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 to cope with Byte and String types explicitly...
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 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
' *** END OF USEFUL WRAPPER FUNCTIONS