EVP_SignInit_ex - HBSSL problem

181 views
Skip to first unread message

Diego Fazio

unread,
Dec 18, 2017, 12:40:09 PM12/18/17
to Harbour Users
Hi, I'm trying to use "EVP_SignInit_ex" but when I run the program it returns the sig variable empty. The "PEM_READ_BIO_RSAPRIVATEKEY" works fine, but the signing routine does not.

/*
   hbmk2 tip4.prg hbssl.hbc
*/
REQUEST __HBEXTERN__HBSSL__
#include "hbssl.ch"

Function main(  )
   Local  cTx, signed, txt, sig , cString, privatekeym, bioe
setmode(25,80)
clear
#pragma __cstream|cString:=%s
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,7EDF8C06409FC8D1

w2i5vWLCOvrExPC4+FMwGQBwxXdGE7FY5Jgr6UstEN+b6l7UVSdRXEkT/Ng6RDZF
jViFPKBSrTAzEnvyNesqPBZMwRPKSEZSj+XcS/dHoz7hrbFTNBzKmDL8CJ67k2Lw
4UwtTtmTkU6L++NTfP6ImvxfaQYCkFK9D42qB7pzAAA27aGZMJCotECqVrekeCed
Tx7EMa7Vf2ToGTsvHBphy7Nwe/8Kijdb0wQIj0ZQWGa2vkYjICauAz9vfOv1xaXe
+H90c6xhVqRCv0uum3pGkqsrdJm0mBnr1gstFEDI+S1Lwr80WWBSQjCBsytfxc8j
dNXG8qXm59/n01vfHaZTtStd6mVa+eJwNRMHP0pisoxCTBPTtF/LOaZvOlbYB+r4
Hbxs5Bp0+YZp9RIipA3uagvtcTk7oHzx6v72amd3egli/DL1OY/ZjvVzHe9/dh6K
LZE2mpAHXtnHe8Rlg4CSPMvyFgN2OZXfbc64FjZcglldIoLwhr7kuLzb0zWv8sOz
jOO+uKNzyjDl5R2ay9YCANUpWVGOrpRrU6C/TzbltcxyEVKk8riKAKCsYf+De3Ee
SwPgouYGV8RgfzAwhwesibA1By1cLS/alCESH+9P2R39VHPB7MzjLc8FyNe3xAQJ
VGue9TIkMgy7RW1VFWLcLsCArTEAl83bv+BQ+YaPp9aLNq8bL5vfU2od0R7LXIOe
jH09fWcojNNLfmZU0Jzy7viUiScTtNqpqoH0qPI1hkisvELqXKhW1Lpkr56Ij8IL
B0NDIZKbaPJHHPb9Ne7nQECzv0/kzmAley9UMTZ1M7fq6KYemR0LsA==
-----END RSA PRIVATE KEY-----

#pragma __endtext

SSL_INIT()
   
ERR_load_PEM_strings()
OpenSSL_add_all_algorithms()
OpenSSL_add_all_ciphers()

PrivateKey := EVP_PKEY_NEW() 
bioe := BIO_new_fd( 1, HB_BIO_NOCLOSE ) 
keyPtr := PEM_READ_BIO_RSAPRIVATEKEY( bio := BIO_new_mem_buf( cString ),  "test"  ) 
? ; ERR_print_errors( bioe )   //show if any error 
EVP_PKEY_ASSIGN_RSA(PrivateKey,keyptr) 

ctx := EVP_MD_CTX_create() 
EVP_MD_CTX_init( ctx ) 

txt       := "text to sign" 
signed := "" 

EVP_SignInit_ex(ctx, HB_EVP_MD_SHA1) 
EVP_SignUpdate(ctx, txt ) 
EVP_SignFinal(ctx, @signed, PrivateKey) 

sig := HB_BASE64ENCODE(signed) 
? sig
return

Carlos A. Fernández

unread,
Dec 18, 2017, 7:32:25 PM12/18/17
to harbou...@googlegroups.com
Hola Diego.
¿Estas tratando tratando de usar funciones de Harbour para reemplazar las llamadas a Openssl de de las rutinas de factura electrónica?

Saludos.
Carlos Fernández

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-users+unsubscribe@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Carlos A. Fernández
CARDENEU C.E.I.

Diego Fazio

unread,
Dec 18, 2017, 7:34:52 PM12/18/17
to Harbour Users
Exactamente.

Diego

Carlos A. Fernández

unread,
Dec 18, 2017, 7:50:39 PM12/18/17
to harbou...@googlegroups.com
Creo que estas usando unas funciones que hice hace un par de años.
Escribime a mi privado y seguimos la charla por ahí.
Saludos


2017-12-18 21:34 GMT-03:00 Diego Fazio <diego...@gmail.com>:
Exactamente.

Diego

--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-users+unsubscribe@googlegroups.com
Web: http://groups.google.com/group/harbour-users

---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Diego Fazio

unread,
Dec 19, 2017, 9:31:29 AM12/19/17
to Harbour Users
Add more data.....

If I save the result in a var, then I show them.....

ret=EVP_SignInit_ex(ctx, HB_EVP_MD_SHA1) 
? ret
ret=EVP_SignUpdate(ctx, txt ) 
? ret
ret=EVP_SignFinal(ctx, @signed, PrivateKey) 
? ret

The result....
1
1
0


So the problem is in EVP_SignFinal becouse the ret value should be 1 when it's ok.
There is a transformation with the original EVP_SignFinal func C with the one in HB.

\contrib\hbssl\evpmd.c


HB_FUNC( EVP_SIGNFINAL )
{
   if( hb_EVP_MD_CTX_is( 1 ) && hb_EVP_PKEY_is( 3 ) )
   {
      EVP_MD_CTX * ctx = hb_EVP_MD_CTX_par( 1 );

      if( ctx )
      {
         unsigned char * buffer = ( unsigned char * ) hb_xgrab( EVP_PKEY_size( hb_EVP_PKEY_par( 3 ) ) + 1 );
         unsigned int    size   = 0;

         hb_retni( EVP_SignFinal( ctx, buffer, &size, hb_EVP_PKEY_par( 3 ) ) );

         if( size > 0 )
         {
            if( ! hb_storclen_buffer( ( char * ) buffer, ( HB_SIZE ) size, 2 ) )
               hb_xfree( buffer );
         }
.....etc

What is trying to do the HB function?

Thanks
Diego.
Reply all
Reply to author
Forward
0 new messages