2929
3030#include <limits.h> /* INT_MAX INT_MIN LLONG_MAX LLONG_MIN UCHAR_MAX ULLONG_MAX */
3131#include <stdint.h> /* uintptr_t */
32- #include <string.h> /* memset(3) strerror_r(3) strlen(3) */
32+ #include <string.h> /* memset(3) strerror_r(3) strlen(3) strncpy(3) */
3333#include <math.h> /* INFINITY fabs(3) floor(3) frexp(3) fmod(3) round(3) isfinite(3) */
3434#include <time.h> /* struct tm time_t strptime(3) time(2) */
3535#include <ctype.h> /* isdigit(3), isxdigit(3), tolower(3) */
@@ -3269,11 +3269,20 @@ static void pushbiostring(lua_State *L) {
32693269} /* pushbiostring() */
32703270
32713271
3272+ static int pem_pw_cb (char * buf , int size , int rwflag , void * u ) {
3273+ if (!u )
3274+ return 0 ;
3275+ char * pass = (char * ) u ;
3276+ strncpy (buf , pass , size );
3277+ return MIN (strlen (pass ), (unsigned int ) size );
3278+ } /* pem_password_cb() */
3279+
3280+
32723281static int pk_new (lua_State * L ) {
32733282 EVP_PKEY * * ud ;
32743283
3275- /* #1 table or key; if key, #2 format and #3 type */
3276- lua_settop (L , 3 );
3284+ /* #1 table or key; if key, #2 format, #3 type and #4 password */
3285+ lua_settop (L , 4 );
32773286
32783287 if (lua_istable (L , 1 ) || lua_isnil (L , 1 )) {
32793288 int type = EVP_PKEY_RSA ;
@@ -3479,7 +3488,7 @@ static int pk_new(lua_State *L) {
34793488 } else if (lua_isstring (L , 1 )) {
34803489 int type = optencoding (L , 2 , "*" , X509_ANY |X509_PEM |X509_DER );
34813490 int pubonly = 0 , prvtonly = 0 ;
3482- const char * opt , * data ;
3491+ const char * opt , * data , * pass ;
34833492 size_t len ;
34843493 BIO * bio ;
34853494 EVP_PKEY * pub = NULL , * prvt = NULL ;
@@ -3497,6 +3506,7 @@ static int pk_new(lua_State *L) {
34973506 }
34983507
34993508 data = luaL_checklstring (L , 1 , & len );
3509+ pass = luaL_optstring (L , 4 , NULL );
35003510
35013511 ud = prepsimple (L , PKEY_CLASS );
35023512
@@ -3512,14 +3522,14 @@ static int pk_new(lua_State *L) {
35123522 */
35133523 BIO_reset (bio );
35143524
3515- if (!(pub = PEM_read_bio_PUBKEY (bio , NULL , 0 , "" )))
3525+ if (!(pub = PEM_read_bio_PUBKEY (bio , NULL , pem_pw_cb , pass )))
35163526 goterr = 1 ;
35173527 }
35183528
35193529 if (!pubonly && !prvt ) {
35203530 BIO_reset (bio );
35213531
3522- if (!(prvt = PEM_read_bio_PrivateKey (bio , NULL , 0 , "" )))
3532+ if (!(prvt = PEM_read_bio_PrivateKey (bio , NULL , pem_pw_cb , pass )))
35233533 goterr = 1 ;
35243534 }
35253535 }
0 commit comments