CryptoSys PKI Toolkit Manual

RSA_GetPublicKeyFromCert

Extracts a public key from an X.509 certificate into an "internal" public key string.

VB6/VBA Syntax

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

Parameters

strPublicKey
[out] String to receive encoded public key data.
nOutChars
[in] Long specifying the maximum number of characters to be received.
strCertFileName
[in] String specifying the filename of an X.509 certificate file (or base64 representation).
nOptions
[in] Long option flags: not used in this release. Specify zero.

C/C++ Syntax

long _stdcall RSA_GetPublicKeyFromCert(char *szOutput, long nOutChars, const char *szCertFile, long flags);

Returns (VB6/C)

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

.NET Equivalent

Rsa.GetPublicKeyFromCert Method

Remarks

Call the function with an empty or NULL strOutput 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.

Example

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'

See Also

RSA_SavePublicKey RSA_GetPublicKeyFromCert

[Contents] [Index]

[HOME]   [NEXT: RSA_KeyBits...]

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