@@ -3270,9 +3270,15 @@ static void pushbiostring(lua_State *L) {
3270
3270
3271
3271
3272
3272
static int pem_pw_cb (char * buf , int size , int rwflag , void * u ) {
3273
- if (!u )
3273
+ lua_State * L = (lua_State * ) u ;
3274
+
3275
+ if (lua_isnil (L , -1 ) || (lua_isfunction (L , -1 ) && lua_pcall (L , 0 , 1 , 0 )))
3276
+ return 0 ;
3277
+
3278
+ const char * pass = lua_tostring (L , -1 );
3279
+ if (!pass )
3274
3280
return 0 ;
3275
- char * pass = ( char * ) u ;
3281
+
3276
3282
strncpy (buf , pass , size );
3277
3283
return MIN (strlen (pass ), (unsigned int ) size );
3278
3284
} /* pem_password_cb() */
@@ -3488,7 +3494,7 @@ static int pk_new(lua_State *L) {
3488
3494
} else if (lua_isstring (L , 1 )) {
3489
3495
int type = optencoding (L , 2 , "*" , X509_ANY |X509_PEM |X509_DER );
3490
3496
int pubonly = 0 , prvtonly = 0 ;
3491
- const char * opt , * data , * pass ;
3497
+ const char * opt , * data ;
3492
3498
size_t len ;
3493
3499
BIO * bio ;
3494
3500
EVP_PKEY * pub = NULL , * prvt = NULL ;
@@ -3506,9 +3512,6 @@ static int pk_new(lua_State *L) {
3506
3512
}
3507
3513
3508
3514
data = luaL_checklstring (L , 1 , & len );
3509
- pass = luaL_optstring (L , 4 , NULL );
3510
-
3511
- ud = prepsimple (L , PKEY_CLASS );
3512
3515
3513
3516
if (!(bio = BIO_new_mem_buf ((void * )data , len )))
3514
3517
return auxL_error (L , auxL_EOPENSSL , "pkey.new" );
@@ -3522,14 +3525,14 @@ static int pk_new(lua_State *L) {
3522
3525
*/
3523
3526
BIO_reset (bio );
3524
3527
3525
- if (!(pub = PEM_read_bio_PUBKEY (bio , NULL , pem_pw_cb , pass )))
3528
+ if (!(pub = PEM_read_bio_PUBKEY (bio , NULL , pem_pw_cb , L )))
3526
3529
goterr = 1 ;
3527
3530
}
3528
3531
3529
3532
if (!pubonly && !prvt ) {
3530
3533
BIO_reset (bio );
3531
3534
3532
- if (!(prvt = PEM_read_bio_PrivateKey (bio , NULL , pem_pw_cb , pass )))
3535
+ if (!(prvt = PEM_read_bio_PrivateKey (bio , NULL , pem_pw_cb , L )))
3533
3536
goterr = 1 ;
3534
3537
}
3535
3538
}
@@ -3550,6 +3553,8 @@ static int pk_new(lua_State *L) {
3550
3553
}
3551
3554
}
3552
3555
3556
+ ud = prepsimple (L , PKEY_CLASS );
3557
+
3553
3558
if (prvt ) {
3554
3559
#if 0
3555
3560
/* TODO: Determine if this is necessary. */
@@ -3959,17 +3964,16 @@ static int pk_toPEM(lua_State *L) {
3959
3964
static int pk_getPrivateKey (lua_State * L ) {
3960
3965
EVP_PKEY * key = checksimple (L , 1 , PKEY_CLASS );
3961
3966
const char * cname = luaL_optstring (L , 2 , NULL );
3962
- const char * pass = NULL ;
3963
3967
EVP_CIPHER * cipher = NULL ;
3968
+ lua_settop (L , 3 );
3964
3969
3965
3970
if (cname ) {
3966
- pass = luaL_checkstring (L , 3 );
3967
3971
cipher = EVP_get_cipherbyname (cname );
3968
3972
if (!cipher )
3969
3973
return luaL_error (L , "pkey:getPrivateKey: unknown cipher: %s" , cname );
3970
3974
}
3971
3975
3972
- if (!PEM_write_bio_PrivateKey (getbio (L ), key , cipher , NULL , 0 , pem_pw_cb , pass ))
3976
+ if (!PEM_write_bio_PrivateKey (getbio (L ), key , cipher , NULL , 0 , pem_pw_cb , L ))
3973
3977
return auxL_error (L , auxL_EOPENSSL , "pkey:getPrivateKey" );
3974
3978
pushbiostring (L );
3975
3979
return 1 ;
0 commit comments