@@ -3404,9 +3404,15 @@ static BIO *getbio(lua_State *L) {
3404
3404
3405
3405
3406
3406
static int pem_pw_cb (char * buf , int size , int rwflag , void * u ) {
3407
- if (!u )
3407
+ lua_State * L = (lua_State * ) u ;
3408
+
3409
+ if (lua_isnil (L , -1 ) || (lua_isfunction (L , -1 ) && lua_pcall (L , 0 , 1 , 0 )))
3410
+ return 0 ;
3411
+
3412
+ const char * pass = lua_tostring (L , -1 );
3413
+ if (!pass )
3408
3414
return 0 ;
3409
- char * pass = ( char * ) u ;
3415
+
3410
3416
strncpy (buf , pass , size );
3411
3417
return MIN (strlen (pass ), (unsigned int ) size );
3412
3418
} /* pem_password_cb() */
@@ -3622,7 +3628,7 @@ static int pk_new(lua_State *L) {
3622
3628
} else if (lua_isstring (L , 1 )) {
3623
3629
int type = optencoding (L , 2 , "*" , X509_ANY |X509_PEM |X509_DER );
3624
3630
int pubonly = 0 , prvtonly = 0 ;
3625
- const char * opt , * data , * pass ;
3631
+ const char * opt , * data ;
3626
3632
size_t len ;
3627
3633
BIO * bio ;
3628
3634
EVP_PKEY * pub = NULL , * prvt = NULL ;
@@ -3640,9 +3646,6 @@ static int pk_new(lua_State *L) {
3640
3646
}
3641
3647
3642
3648
data = luaL_checklstring (L , 1 , & len );
3643
- pass = luaL_optstring (L , 4 , NULL );
3644
-
3645
- ud = prepsimple (L , PKEY_CLASS );
3646
3649
3647
3650
if (!(bio = BIO_new_mem_buf ((void * )data , len )))
3648
3651
return auxL_error (L , auxL_EOPENSSL , "pkey.new" );
@@ -3656,14 +3659,14 @@ static int pk_new(lua_State *L) {
3656
3659
*/
3657
3660
BIO_reset (bio );
3658
3661
3659
- if (!(pub = PEM_read_bio_PUBKEY (bio , NULL , pem_pw_cb , pass )))
3662
+ if (!(pub = PEM_read_bio_PUBKEY (bio , NULL , pem_pw_cb , L )))
3660
3663
goterr = 1 ;
3661
3664
}
3662
3665
3663
3666
if (!pubonly && !prvt ) {
3664
3667
BIO_reset (bio );
3665
3668
3666
- if (!(prvt = PEM_read_bio_PrivateKey (bio , NULL , pem_pw_cb , pass )))
3669
+ if (!(prvt = PEM_read_bio_PrivateKey (bio , NULL , pem_pw_cb , L )))
3667
3670
goterr = 1 ;
3668
3671
}
3669
3672
}
@@ -3684,6 +3687,10 @@ static int pk_new(lua_State *L) {
3684
3687
}
3685
3688
}
3686
3689
3690
+ BIO_free (bio );
3691
+
3692
+ ud = prepsimple (L , PKEY_CLASS );
3693
+
3687
3694
if (prvt ) {
3688
3695
#if 0
3689
3696
/* TODO: Determine if this is necessary. */
@@ -3709,8 +3716,6 @@ static int pk_new(lua_State *L) {
3709
3716
pub = NULL ;
3710
3717
}
3711
3718
done :
3712
- BIO_free (bio );
3713
-
3714
3719
if (pub )
3715
3720
EVP_PKEY_free (pub );
3716
3721
@@ -4092,11 +4097,10 @@ static int pk_toPEM(lua_State *L) {
4092
4097
static int pk_getPrivateKey (lua_State * L ) {
4093
4098
EVP_PKEY * key = checksimple (L , 1 , PKEY_CLASS );
4094
4099
const char * cname = luaL_optstring (L , 2 , NULL );
4095
- const char * pass = NULL ;
4096
4100
EVP_CIPHER * cipher = NULL ;
4101
+ lua_settop (L , 3 );
4097
4102
4098
4103
if (cname ) {
4099
- pass = luaL_checkstring (L , 3 );
4100
4104
cipher = EVP_get_cipherbyname (cname );
4101
4105
if (!cipher )
4102
4106
return luaL_error (L , "pkey:getPrivateKey: unknown cipher: %s" , cname );
@@ -4106,7 +4110,7 @@ static int pk_getPrivateKey(lua_State *L) {
4106
4110
char * str ;
4107
4111
long len ;
4108
4112
4109
- if (!PEM_write_bio_PrivateKey (bio , key , cipher , NULL , 0 , pem_pw_cb , pass ))
4113
+ if (!PEM_write_bio_PrivateKey (bio , key , cipher , NULL , 0 , pem_pw_cb , L ))
4110
4114
return auxL_error (L , auxL_EOPENSSL , "pkey:getPrivateKey" );
4111
4115
len = BIO_get_mem_data (bio , & str );
4112
4116
lua_pushlstring (L , str , len );
0 commit comments