@@ -3436,9 +3436,15 @@ static BIO *getbio(lua_State *L) {
3436
3436
3437
3437
3438
3438
static int pem_pw_cb (char * buf , int size , int rwflag , void * u ) {
3439
- if (!u )
3439
+ lua_State * L = (lua_State * ) u ;
3440
+
3441
+ if (lua_isnil (L , -1 ) || (lua_isfunction (L , -1 ) && lua_pcall (L , 0 , 1 , 0 )))
3442
+ return 0 ;
3443
+
3444
+ const char * pass = lua_tostring (L , -1 );
3445
+ if (!pass )
3440
3446
return 0 ;
3441
- char * pass = ( char * ) u ;
3447
+
3442
3448
strncpy (buf , pass , size );
3443
3449
return MIN (strlen (pass ), (unsigned int ) size );
3444
3450
} /* pem_password_cb() */
@@ -3654,7 +3660,7 @@ static int pk_new(lua_State *L) {
3654
3660
} else if (lua_isstring (L , 1 )) {
3655
3661
int format ;
3656
3662
int pubonly = 0 , prvtonly = 0 ;
3657
- const char * type , * data , * pass ;
3663
+ const char * type , * data ;
3658
3664
size_t len ;
3659
3665
BIO * bio ;
3660
3666
EVP_PKEY * pub = NULL , * prvt = NULL ;
@@ -3680,8 +3686,7 @@ static int pk_new(lua_State *L) {
3680
3686
}
3681
3687
}
3682
3688
3683
- pass = luaL_optstring (L , -1 , NULL );
3684
- if (pass )
3689
+ if (!lua_isnil (L , -1 ))
3685
3690
if (format == X509_DER )
3686
3691
return luaL_error (L , "decryption supported only for PEM keys" );
3687
3692
else format = X509_PEM ;
@@ -3694,6 +3699,8 @@ static int pk_new(lua_State *L) {
3694
3699
return auxL_error (L , auxL_EOPENSSL , "pkey.new" );
3695
3700
3696
3701
if (format == X509_PEM || format == X509_ANY ) {
3702
+ lua_pushvalue (L , -2 );
3703
+
3697
3704
if (!prvtonly && !pub ) {
3698
3705
/*
3699
3706
* BIO_reset is a rewind for read-only
@@ -3702,16 +3709,18 @@ static int pk_new(lua_State *L) {
3702
3709
*/
3703
3710
BIO_reset (bio );
3704
3711
3705
- if (!(pub = PEM_read_bio_PUBKEY (bio , NULL , pem_pw_cb , pass )))
3712
+ if (!(pub = PEM_read_bio_PUBKEY (bio , NULL , pem_pw_cb , L )))
3706
3713
goterr = 1 ;
3707
3714
}
3708
3715
3709
3716
if (!pubonly && !prvt ) {
3710
3717
BIO_reset (bio );
3711
3718
3712
- if (!(prvt = PEM_read_bio_PrivateKey (bio , NULL , pem_pw_cb , pass )))
3719
+ if (!(prvt = PEM_read_bio_PrivateKey (bio , NULL , pem_pw_cb , L )))
3713
3720
goterr = 1 ;
3714
3721
}
3722
+
3723
+ lua_pop (L , 1 );
3715
3724
}
3716
3725
3717
3726
if (format == X509_DER || format == X509_ANY ) {
@@ -4052,7 +4061,6 @@ static int pk_toPEM(lua_State *L) {
4052
4061
int type ;
4053
4062
const char * cname = NULL ;
4054
4063
EVP_CIPHER * cipher = NULL ;
4055
- const char * pass = NULL ;
4056
4064
4057
4065
if (lua_istable (L , i )) {
4058
4066
loadfield (L , i , "cipher" , LUA_TSTRING , & cname );
@@ -4079,13 +4087,16 @@ static int pk_toPEM(lua_State *L) {
4079
4087
cipher = EVP_get_cipherbyname (cname );
4080
4088
if (!cipher )
4081
4089
return luaL_error (L , "pkey:toPEM: unknown cipher: %s" , cname );
4082
- if (!loadfield (L , i , "password" , LUA_TSTRING , & pass ))
4090
+ if (!getfield (L , i , "password" ))
4083
4091
return luaL_error (L , "pkey:toPEM: password not defined" );
4084
4092
}
4085
4093
4086
- if (!PEM_write_bio_PrivateKey (bio , key , cipher , NULL , 0 , pem_pw_cb , pass ))
4094
+ if (!PEM_write_bio_PrivateKey (bio , key , cipher , NULL , 0 , pem_pw_cb , L ))
4087
4095
return auxL_error (L , auxL_EOPENSSL , "pkey:__tostring" );
4088
4096
4097
+ if (cname )
4098
+ lua_pop (L , 1 );
4099
+
4089
4100
len = BIO_get_mem_data (bio , & pem );
4090
4101
lua_pushlstring (L , pem , len );
4091
4102
BIO_reset (bio );
0 commit comments