aboutsummaryrefslogtreecommitdiffstats
path: root/main/lua-ossl/0007-pkey.new-accept-option-table.patch
blob: b78818ee6e0737055903e0cc0c837e80ee72804f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
From 07e298a09b20f24d7a1efede41c04f398bb1022a Mon Sep 17 00:00:00 2001
From: Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
Date: Sun, 2 Sep 2018 14:44:46 +0300
Subject: [PATCH 07/10] pkey.new: accept option table

---
 src/openssl.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/openssl.c b/src/openssl.c
index cb38f2f..2aaa5d8 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -4073,7 +4073,7 @@ static BIO *getbio(lua_State *L) {
 static int pk_new(lua_State *L) {
 	EVP_PKEY **ud;
 
-	/* #1 table or key; if key, #2 format and #3 type */
+	/* #1 table or key; if key, #2 option table or format; if format, #3 type */
 	lua_settop(L, 3);
 
 	if (lua_istable(L, 1) || lua_isnil(L, 1)) {
@@ -4307,7 +4307,7 @@ static int pk_new(lua_State *L) {
 #endif
 		} /* switch() */
 	} else if (lua_isstring(L, 1)) {
-		int format = optencoding(L, 2, "*", X509_ANY|X509_PEM|X509_DER);
+		int format;
 		int pubonly = 0, prvtonly = 0;
 		const char *type, *data;
 		size_t len;
@@ -4315,14 +4315,21 @@ static int pk_new(lua_State *L) {
 		EVP_PKEY *pub = NULL, *prvt = NULL;
 		int goterr = 0;
 
+		if (lua_istable(L, 2)) {
+			lua_getfield(L, 2, "format");
+			lua_getfield(L, 2, "type");
+		}
+
+		format = optencoding(L, -2, "*", X509_ANY|X509_PEM|X509_DER);
+
 		/* check if specified publickey or privatekey */
-		if ((type = luaL_optstring(L, 3, NULL))) {
+		if ((type = luaL_optstring(L, -1, NULL))) {
 			if (xtolower(type[0]) == 'p' && xtolower(type[1]) == 'u') {
 				pubonly = 1;
 			} else if (xtolower(type[0]) == 'p' && xtolower(type[1]) == 'r') {
 				prvtonly = 1;
 			} else {
-				return luaL_argerror(L, 3, lua_pushfstring(L, "invalid type: %s", type));
+				return luaL_error(L, "invalid key type: %s", type);
 			}
 		}
 
-- 
2.24.1