SQL Server
SQL Server
Duplicating OpenSSL rsautl (creating RSA signatures)
See more RSA Examples
Demonstrates how to duplicate OpenSSL rsautil RSA signatures.The Chilkat RSA component's methods for creating RSA signatures (SignBytes, SignBytesENC, SignString, and SignStringENC) are very different from OpenSSL's rsautl command. First, we'll explain what Chilkat's signing methods do, and then what OpenSSL's rsautl does. New signing methods have been added to Chilkat RSA to duplicate OpenSSL rsautl: OpenSslSignBytes, OpenSslSignBytesENC, OpenSslSignString, and OpenSslSignStringENC.
Here's what Chilkat's RSA Sign* methods do:
(Note: Chilkat RSA's Sign* methods generate signatures according to RSA Laboratories' "PKCS #1 v2.0: RSA Cryptography Standard".)
- Input data is hashed using the hashing algorithm specified.
- The hash is padded using either PKCS1 v1.5 or PKCS1 PSS padding. PKCS v1.5 involves encoding to ASN.1 before padding.
- The padded hash is finally RSA signed (i.e. modular exponentiation) and the signature is returned.
OpenSSL rsautl is very different. Here's what it does:
- The input data is not hashed. It must be a small enough amount of data such that it can be padded and signed. PKCS v1.5 padding is always used. The data is not ASN.1 encoded before padding.
- The PKCS v1.5 padded data is RSA signed and the signature is returned.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @privKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Load the private key from an RSA PEM file:
EXEC sp_OAMethod @privKey, 'LoadPemFile', @success OUT, 'private.pem'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @privKey
RETURN
END
DECLARE @rsa int
EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT
EXEC sp_OAMethod @rsa, 'UsePrivateKey', @success OUT, @privKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @rsa
RETURN
END
DECLARE @strData nvarchar(4000)
SELECT @strData = 'secret'
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
EXEC sp_OAMethod @bd, 'AppendString', @success OUT, @strData, 'utf-8'
EXEC sp_OAMethod @rsa, 'SignRawBd', @success OUT, @bd
DECLARE @hexSig nvarchar(4000)
EXEC sp_OAMethod @bd, 'GetEncoded', @hexSig OUT, 'hex'
PRINT @hexSig
-- Recover the data using the corresponding public key:
DECLARE @pubKey int
EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubKey OUT
-- Load the public key from a PEM file:
EXEC sp_OAMethod @pubKey, 'LoadFromFile', @success OUT, 'public.pem'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @pubKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @pubKey
RETURN
END
DECLARE @rsa2 int
EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa2 OUT
EXEC sp_OAMethod @rsa2, 'UsePublicKey', @success OUT, @pubKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rsa2, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @pubKey
EXEC @hr = sp_OADestroy @rsa2
RETURN
END
DECLARE @bd2 int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd2 OUT
EXEC sp_OAMethod @bd2, 'AppendEncoded', @success OUT, @hexSig, 'hex'
-- Recover the original data.
EXEC sp_OAMethod @rsa2, 'VerifyRawBd', @success OUT, @bd2
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rsa2, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @pubKey
EXEC @hr = sp_OADestroy @rsa2
EXEC @hr = sp_OADestroy @bd2
RETURN
END
DECLARE @originalData nvarchar(4000)
EXEC sp_OAMethod @bd2, 'GetString', @originalData OUT, 'utf-8'
PRINT @originalData
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @pubKey
EXEC @hr = sp_OADestroy @rsa2
EXEC @hr = sp_OADestroy @bd2
END
GO