Reads a public key from an X.509 certificate into an "internal" public key string.
Public Declare Function RSA_GetPublicKeyFromCert Lib "diCrPKI.dll"
(ByVal strPublicKey As String, ByVal nOutChars As Long, ByVal strCertFileName As String,
ByVal nOptions As Long) As Long
nRet = RSA_GetPublicKeyFromCert(strPublicKey, nOutChars,
strCertFileName, nOptions) As Long
long __stdcall RSA_GetPublicKeyFromCert(char *szOutput, long nOutChars, const char *szCertFile, long nOptions);
If successful, the return value is the number of characters in or required for the output string; otherwise it returns a negative error code.
Rsa.GetPublicKeyFromCert Method
Call the function with an empty or NULL szOutput string or zero nOutChars parameter to find out the required length of the output string. C/C++ users should add one to this value when allocating memory. Both binary BER-encoded and PEM-style base64-encoded certificates can be read, or the certificate can be passed directly as a string in base64 representation.
Woops! The name of this function is an exception to our usual convention that "Read" means read-into-internal-string and "Get" means extract-and-save-as-a-file. By rights this function should be called "RSA_ReadPublicKeyFromCert". Sorry.
This code reads Alice's public key from her certificate and saves in a PKCS#1 public key file. The certificate is from [SMIME-EX].
Dim strCertFile As String Dim strKeyFile As String Dim strPublicKey As String Dim nChars As Long Dim nRet As Long strCertFile = "AliceRSASignByCarl.cer" ' First find out the length of string we need nChars = RSA_GetPublicKeyFromCert(vbNullString, 0, strCertFile, 0) Debug.Print "RSA_GetPublicKeyFromCert returns " & nChars & " (expecting +ve)" If nChars <= 0 Then Debug.Print "ERROR: " & pkiErrorLookup(nChars) Exit Sub End If ' Pre-dimension the string to receive data - IMPORTANT strPublicKey = String(nChars, " ") ' Read in the Public Key in our "internal" format nRet = RSA_GetPublicKeyFromCert(strPublicKey, nChars, strCertFile, 0) Debug.Print "Public key is " & RSA_KeyBits(strPublicKey) & " bits long" ' Now save as a PKCS#1 public key file strKeyFile = "AlicePubRSA.pub" nRet = RSA_SavePublicKey(strKeyFile, strPublicKey, 0) Debug.Print "RSA_SavePublicKey returns " & nRet If nRet = 0 Then Debug.Print "Saved as public key file '" & strKeyFile & "'" Else Debug.Print "ERROR: " & pkiErrorLookup(nRet) End If
This should result in the output:
RSA_GetPublicKeyFromCert returns 220 (expecting +ve) Public key is 1024 bits long RSA_SavePublicKey returns 0 Saved as public key file 'AlicePubRSA.pub'
RSA_SavePublicKey RSA_PublicKeyFromPrivate X509_GetCertFromP7Chain X509_GetCertFromPFX