Archived Forum Post

Index of archived forum posts

Question:

Importing public key RSA

Jan 17 '14 at 13:44

Keep getting this error,when importing a publicKey

ChilkatLog:
ImportPublicKey:
DllDate: Aug 15 2013
ChilkatVersion: 9.4.1.42
UnlockPrefix: ANGELORSA
Username: WIN2008R2:WIN2008R2$
Architecture: Little Endian; 64-bit
Language: ActiveX / x64
VerboseLogging: 0
No modulus
Failed.
--ImportPublicKey
--ChilkatLog

My code to import the key public key

  EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'c:\temp\Chave_Publica.pem'
IF @success <> 1
  BEGIN
    EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT

    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @cert
    RETURN
  END

DECLARE @pubKey int

EXEC sp_OAMethod @cert, 'ExportPublicKey', @pubKey OUT

--  Now verify using a separate instance of the RSA object:
DECLARE @rsa2 int
EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa2 OUT
IF @hr <> 0
BEGIN
    PRINT 'Failed to create ActiveX component'
    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @rsa2
    EXEC @hr = sp_OADestroy @cert
    RETURN
END

EXEC sp_OAMethod @rsa2, 'UnlockComponent', @success OUT, 'xxxxxxxxUxxxxxxxxxxxxxxx'
IF @success <> 1
  BEGIN
    PRINT 'RSA component unlock failed'
    EXEC @hr = sp_OADestroy @rsa2
    RETURN
  END

--  Import the public key into the RSA object:
EXEC sp_OAMethod @pubKey, 'GetXml', @sTmp0 OUT    
EXEC sp_OAMethod @rsa2, 'ImportPublicKey', @success OUT, @sTmp0
IF @success <> 1
  BEGIN
    EXEC sp_OAGetProperty @rsa2, 'LastErrorText', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @cert
    EXEC @hr = sp_OADestroy @rsa2
    RETURN
  END

Answer

This would happen if the public key object was empty (i.e. was never loaded from a file).

Chilkat resolved this problem with the customer via email. The problem was that the call to publicKey.LoadOpenSslPem was mistakenly called instead of LoadOpenSslPemFile. The LoadOpenSslPem method is when the PEM data is passed directly, whereas the path of a file containing the PEM data is passed to LoadOpenSslPemFile.