@@ -701,35 +701,47 @@ static VALUE
701
701
ossl_pkey_sign (VALUE self , VALUE digest , VALUE data )
702
702
{
703
703
EVP_PKEY * pkey ;
704
- const EVP_MD * md ;
704
+ const EVP_MD * md = NULL ;
705
705
EVP_MD_CTX * ctx ;
706
- unsigned int buf_len ;
707
- VALUE str ;
708
- int result ;
706
+ size_t siglen ;
707
+ int state ;
708
+ VALUE sig ;
709
709
710
710
pkey = GetPrivPKeyPtr (self );
711
- md = ossl_evp_get_digestbyname (digest );
711
+ if (!NIL_P (digest ))
712
+ md = ossl_evp_get_digestbyname (digest );
712
713
StringValue (data );
713
- str = rb_str_new (0 , EVP_PKEY_size (pkey ));
714
714
715
715
ctx = EVP_MD_CTX_new ();
716
716
if (!ctx )
717
- ossl_raise (ePKeyError , "EVP_MD_CTX_new" );
718
- if (!EVP_SignInit_ex (ctx , md , NULL )) {
719
- EVP_MD_CTX_free (ctx );
720
- ossl_raise (ePKeyError , "EVP_SignInit_ex" );
717
+ ossl_raise (ePKeyError , "EVP_MD_CTX_new" );
718
+ if (EVP_DigestSignInit (ctx , NULL , md , /* engine */ NULL , pkey ) < 1 ) {
719
+ EVP_MD_CTX_free (ctx );
720
+ ossl_raise (ePKeyError , "EVP_DigestSignInit" );
721
+ }
722
+ if (EVP_DigestSignUpdate (ctx , RSTRING_PTR (data ), RSTRING_LEN (data )) < 1 ) {
723
+ EVP_MD_CTX_free (ctx );
724
+ ossl_raise (ePKeyError , "EVP_DigestSignUpdate" );
725
+ }
726
+ if (EVP_DigestSignFinal (ctx , NULL , & siglen ) < 1 ) {
727
+ EVP_MD_CTX_free (ctx );
728
+ ossl_raise (ePKeyError , "EVP_DigestSignFinal" );
729
+ }
730
+ if (siglen > LONG_MAX )
731
+ rb_raise (ePKeyError , "signature would be too large" );
732
+ sig = ossl_str_new (NULL , (long )siglen , & state );
733
+ if (state ) {
734
+ EVP_MD_CTX_free (ctx );
735
+ rb_jump_tag (state );
721
736
}
722
- if (!EVP_SignUpdate (ctx , RSTRING_PTR (data ), RSTRING_LEN (data ))) {
723
- EVP_MD_CTX_free (ctx );
724
- ossl_raise (ePKeyError , "EVP_SignUpdate" );
737
+ if (EVP_DigestSignFinal (ctx , (unsigned char * )RSTRING_PTR (sig ),
738
+ & siglen ) < 1 ) {
739
+ EVP_MD_CTX_free (ctx );
740
+ ossl_raise (ePKeyError , "EVP_DigestSignFinal" );
725
741
}
726
- result = EVP_SignFinal (ctx , (unsigned char * )RSTRING_PTR (str ), & buf_len , pkey );
727
742
EVP_MD_CTX_free (ctx );
728
- if (!result )
729
- ossl_raise (ePKeyError , "EVP_SignFinal" );
730
- rb_str_set_len (str , buf_len );
731
-
732
- return str ;
743
+ rb_str_set_len (sig , siglen );
744
+ return sig ;
733
745
}
734
746
735
747
/*
@@ -757,38 +769,38 @@ static VALUE
757
769
ossl_pkey_verify (VALUE self , VALUE digest , VALUE sig , VALUE data )
758
770
{
759
771
EVP_PKEY * pkey ;
760
- const EVP_MD * md ;
772
+ const EVP_MD * md = NULL ;
761
773
EVP_MD_CTX * ctx ;
762
- int siglen , result ;
774
+ int ret ;
763
775
764
776
GetPKey (self , pkey );
765
777
ossl_pkey_check_public_key (pkey );
766
- md = ossl_evp_get_digestbyname (digest );
778
+ if (!NIL_P (digest ))
779
+ md = ossl_evp_get_digestbyname (digest );
767
780
StringValue (sig );
768
- siglen = RSTRING_LENINT (sig );
769
781
StringValue (data );
770
782
771
783
ctx = EVP_MD_CTX_new ();
772
784
if (!ctx )
773
- ossl_raise (ePKeyError , "EVP_MD_CTX_new" );
774
- if (! EVP_VerifyInit_ex (ctx , md , NULL ) ) {
775
- EVP_MD_CTX_free (ctx );
776
- ossl_raise (ePKeyError , "EVP_VerifyInit_ex " );
785
+ ossl_raise (ePKeyError , "EVP_MD_CTX_new" );
786
+ if (EVP_DigestVerifyInit (ctx , NULL , md , /* engine */ NULL , pkey ) < 1 ) {
787
+ EVP_MD_CTX_free (ctx );
788
+ ossl_raise (ePKeyError , "EVP_DigestVerifyInit " );
777
789
}
778
- if (! EVP_VerifyUpdate (ctx , RSTRING_PTR (data ), RSTRING_LEN (data ))) {
779
- EVP_MD_CTX_free (ctx );
780
- ossl_raise (ePKeyError , "EVP_VerifyUpdate " );
790
+ if (EVP_DigestVerifyUpdate (ctx , RSTRING_PTR (data ), RSTRING_LEN (data )) < 1 ) {
791
+ EVP_MD_CTX_free (ctx );
792
+ ossl_raise (ePKeyError , "EVP_DigestVerifyUpdate " );
781
793
}
782
- result = EVP_VerifyFinal (ctx , (unsigned char * )RSTRING_PTR (sig ), siglen , pkey );
794
+ ret = EVP_DigestVerifyFinal (ctx , (unsigned char * )RSTRING_PTR (sig ),
795
+ RSTRING_LEN (sig ));
783
796
EVP_MD_CTX_free (ctx );
784
- switch (result ) {
785
- case 0 :
786
- ossl_clear_error ();
787
- return Qfalse ;
788
- case 1 :
789
- return Qtrue ;
790
- default :
791
- ossl_raise (ePKeyError , "EVP_VerifyFinal" );
797
+ if (ret < 0 )
798
+ ossl_raise (ePKeyError , "EVP_DigestVerifyFinal" );
799
+ if (ret )
800
+ return Qtrue ;
801
+ else {
802
+ ossl_clear_error ();
803
+ return Qfalse ;
792
804
}
793
805
}
794
806
0 commit comments