Skip to content

Commit 70d1c68

Browse files
committed
pkey: PEM password callback
1 parent 2bac24a commit 70d1c68

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/openssl.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3270,9 +3270,15 @@ static void pushbiostring(lua_State *L) {
32703270

32713271

32723272
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)
32743280
return 0;
3275-
char *pass = (char *) u;
3281+
32763282
strncpy(buf, pass, size);
32773283
return MIN(strlen(pass), (unsigned int) size);
32783284
} /* pem_password_cb() */
@@ -3488,7 +3494,7 @@ static int pk_new(lua_State *L) {
34883494
} else if (lua_isstring(L, 1)) {
34893495
int type = optencoding(L, 2, "*", X509_ANY|X509_PEM|X509_DER);
34903496
int pubonly = 0, prvtonly = 0;
3491-
const char *opt, *data, *pass;
3497+
const char *opt, *data;
34923498
size_t len;
34933499
BIO *bio;
34943500
EVP_PKEY *pub = NULL, *prvt = NULL;
@@ -3506,9 +3512,6 @@ static int pk_new(lua_State *L) {
35063512
}
35073513

35083514
data = luaL_checklstring(L, 1, &len);
3509-
pass = luaL_optstring(L, 4, NULL);
3510-
3511-
ud = prepsimple(L, PKEY_CLASS);
35123515

35133516
if (!(bio = BIO_new_mem_buf((void *)data, len)))
35143517
return auxL_error(L, auxL_EOPENSSL, "pkey.new");
@@ -3522,14 +3525,14 @@ static int pk_new(lua_State *L) {
35223525
*/
35233526
BIO_reset(bio);
35243527

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)))
35263529
goterr = 1;
35273530
}
35283531

35293532
if (!pubonly && !prvt) {
35303533
BIO_reset(bio);
35313534

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)))
35333536
goterr = 1;
35343537
}
35353538
}
@@ -3550,6 +3553,8 @@ static int pk_new(lua_State *L) {
35503553
}
35513554
}
35523555

3556+
ud = prepsimple(L, PKEY_CLASS);
3557+
35533558
if (prvt) {
35543559
#if 0
35553560
/* TODO: Determine if this is necessary. */
@@ -3959,17 +3964,16 @@ static int pk_toPEM(lua_State *L) {
39593964
static int pk_getPrivateKey(lua_State *L) {
39603965
EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
39613966
const char *cname = luaL_optstring(L, 2, NULL);
3962-
const char *pass = NULL;
39633967
EVP_CIPHER *cipher = NULL;
3968+
lua_settop(L, 3);
39643969

39653970
if (cname) {
3966-
pass = luaL_checkstring(L, 3);
39673971
cipher = EVP_get_cipherbyname(cname);
39683972
if (!cipher)
39693973
return luaL_error(L, "pkey:getPrivateKey: unknown cipher: %s", cname);
39703974
}
39713975

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))
39733977
return auxL_error(L, auxL_EOPENSSL, "pkey:getPrivateKey");
39743978
pushbiostring(L);
39753979
return 1;

0 commit comments

Comments
 (0)