Sample code for 30+ languages & platforms
SQL Server

Convert a PFX to a Java KeyStore

See more PFX/P12 Examples

Demonstrates Pfx.ToJksObj, which populates a JavaKeyStore with a JKS representation of the PFX, then writes it to a file.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. One private-key entry is created for each private key, with certificate chains built from the PFX certificates. The password protects the generated Java KeyStore.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @pfx int
    EXEC @hr = sp_OACreate 'Chilkat.Pfx', @pfx OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  The file path is relative to the application's current working directory.  An absolute path
    --  may also be used.  Supply the path appropriate to your own environment.
    --  The PFX password should come from a secure source rather than being hard-coded.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'myPfxPassword'
    EXEC sp_OAMethod @pfx, 'LoadPfxFile', @success OUT, 'qa_data/identity.pfx', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        RETURN
      END

    --  Populate a Java KeyStore (JKS) representation of this PFX.  One private-key entry is created for
    --  each private key, and the PFX certificates are used to build the certificate chains.  The 1st
    --  argument is the alias for the first private-key entry, and the 2nd protects the generated JKS.
    --  The JKS password should come from a secure source rather than being hard-coded.
    DECLARE @jksPassword nvarchar(4000)
    SELECT @jksPassword = 'myJksPassword'
    DECLARE @jks int
    EXEC @hr = sp_OACreate 'Chilkat.JavaKeyStore', @jks OUT

    EXEC sp_OAMethod @pfx, 'ToJksObj', @success OUT, 'myalias', @jksPassword, @jks
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        EXEC @hr = sp_OADestroy @jks
        RETURN
      END

    --  The Java KeyStore object can then be written to a .jks file.
    EXEC sp_OAMethod @jks, 'ToFile', @success OUT, @jksPassword, 'qa_output/identity.jks'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @jks, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        EXEC @hr = sp_OADestroy @jks
        RETURN
      END

    EXEC sp_OAGetProperty @jks, 'NumPrivateKeys', @iTmp0 OUT

    PRINT 'Wrote a Java KeyStore with ' + @iTmp0 + ' private key(s).'

    EXEC @hr = sp_OADestroy @pfx
    EXEC @hr = sp_OADestroy @jks


END
GO