Skip to content

Commit

Permalink
main/lua-ossl: align with latest PEM encryption patch set
Browse files Browse the repository at this point in the history
wahern/luaossl#128

backwards incompatible
  • Loading branch information
kunkku committed Jan 12, 2020
1 parent e07ef0d commit b9dd8d8
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 256 deletions.
45 changes: 0 additions & 45 deletions main/lua-ossl/0001-pkey-getPrivateKey-method.patch

This file was deleted.

33 changes: 33 additions & 0 deletions main/lua-ossl/0001-pkey.toPEM-opts-rename-const-array.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
From 4589f5b1a5c2cbeab2069f9cdce605bc3a3096fb Mon Sep 17 00:00:00 2001
From: Kaarle Ritvanen <[email protected]>
Date: Sun, 2 Sep 2018 13:07:20 +0300
Subject: [PATCH 01/10] pkey.toPEM: opts: rename const array

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

diff --git a/src/openssl.c b/src/openssl.c
index 9a9de0f..acb8289 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -4675,14 +4675,14 @@ static int pk_toPEM(lua_State *L) {
bio = getbio(L);

for (i = 2; i <= top; i++) {
- static const char *const opts[] = {
+ static const char *const types[] = {
"public", "PublicKey",
"private", "PrivateKey",
// "params", "Parameters",
NULL,
};

- switch (auxL_checkoption(L, i, NULL, opts, 1)) {
+ switch (auxL_checkoption(L, i, NULL, types, 1)) {
case 0: case 1: /* public, PublicKey */
if (!PEM_write_bio_PUBKEY(bio, key))
return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring");
--
2.24.1

50 changes: 0 additions & 50 deletions main/lua-ossl/0002-pkey.getPrivateKey-encryption.patch

This file was deleted.

35 changes: 35 additions & 0 deletions main/lua-ossl/0002-pkey.toPEM-accept-table-arguments.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
From 2fb6331a5304927fcfe915d5d42535d3d500f540 Mon Sep 17 00:00:00 2001
From: Kaarle Ritvanen <[email protected]>
Date: Sun, 2 Sep 2018 13:18:48 +0300
Subject: [PATCH 02/10] pkey.toPEM: accept table arguments

---
src/openssl.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/openssl.c b/src/openssl.c
index acb8289..1905693 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -4681,8 +4681,17 @@ static int pk_toPEM(lua_State *L) {
// "params", "Parameters",
NULL,
};
+ int type;

- switch (auxL_checkoption(L, i, NULL, types, 1)) {
+ if (!lua_istable(L, i))
+ lua_pushvalue(L, i);
+ else if (!getfield(L, i, "type"))
+ lua_pushliteral(L, "public");
+
+ type = auxL_checkoption(L, -1, NULL, types, 1);
+ lua_pop(L, 1);
+
+ switch (type) {
case 0: case 1: /* public, PublicKey */
if (!PEM_write_bio_PUBKEY(bio, key))
return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring");
--
2.24.1

63 changes: 63 additions & 0 deletions main/lua-ossl/0003-pkey.toPEM-private-key-encryption.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
From 367597c9fbbd8c0179a8b2a75e5b0819ef5bc5d6 Mon Sep 17 00:00:00 2001
From: Kaarle Ritvanen <[email protected]>
Date: Mon, 30 Apr 2018 13:26:16 +0300
Subject: [PATCH 03/10] pkey.toPEM: private key encryption

---
src/openssl.c | 25 +++++++++++++++++++------
1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/openssl.c b/src/openssl.c
index 1905693..9b10165 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -31,7 +31,7 @@

#include <limits.h> /* INT_MAX INT_MIN LLONG_MAX LLONG_MIN UCHAR_MAX ULLONG_MAX */
#include <stdint.h> /* uintptr_t */
-#include <string.h> /* memset(3) strerror_r(3) */
+#include <string.h> /* memset(3) strerror_r(3) strlen(3) */
#include <math.h> /* INFINITY fabs(3) floor(3) frexp(3) fmod(3) round(3) isfinite(3) */
#include <time.h> /* struct tm time_t strptime(3) time(2) */
#include <ctype.h> /* isdigit(3), isxdigit(3), tolower(3) */
@@ -4682,11 +4682,16 @@ static int pk_toPEM(lua_State *L) {
NULL,
};
int type;
-
- if (!lua_istable(L, i))
+ const char *cname = NULL;
+ const EVP_CIPHER *cipher = NULL;
+ const char *pass = NULL;
+
+ if (lua_istable(L, i)) {
+ loadfield(L, i, "cipher", LUA_TSTRING, &cname);
+ if (!getfield(L, i, "type"))
+ lua_pushstring(L, cname ? "private" : "public");
+ } else
lua_pushvalue(L, i);
- else if (!getfield(L, i, "type"))
- lua_pushliteral(L, "public");

type = auxL_checkoption(L, -1, NULL, types, 1);
lua_pop(L, 1);
@@ -4702,7 +4707,15 @@ static int pk_toPEM(lua_State *L) {

break;
case 2: case 3: /* private, PrivateKey */
- if (!PEM_write_bio_PrivateKey(bio, key, 0, 0, 0, 0, 0))
+ if (cname) {
+ cipher = EVP_get_cipherbyname(cname);
+ if (!cipher)
+ return luaL_error(L, "pkey:toPEM: unknown cipher: %s", cname);
+ if (!loadfield(L, i, "password", LUA_TSTRING, &pass))
+ return luaL_error(L, "pkey:toPEM: password not defined");
+ }
+
+ if (!PEM_write_bio_PrivateKey(bio, key, cipher, pass, pass ? strlen(pass) : 0, 0, 0))
return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring");

len = BIO_get_mem_data(bio, &pem);
--
2.24.1

25 changes: 0 additions & 25 deletions main/lua-ossl/0004-pkey.getPrivateKey-use-password-callback.patch

This file was deleted.

43 changes: 43 additions & 0 deletions main/lua-ossl/0004-pkey.new-type-rename-variable.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
From f287bd7a314458a3870ee4e7fbdfc7e0ea41a4b6 Mon Sep 17 00:00:00 2001
From: Kaarle Ritvanen <[email protected]>
Date: Sun, 2 Sep 2018 14:22:43 +0300
Subject: [PATCH 04/10] pkey.new: type: rename variable

---
src/openssl.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/openssl.c b/src/openssl.c
index 9b10165..ed18e60 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -4299,7 +4299,7 @@ static int pk_new(lua_State *L) {
#endif
} /* switch() */
} else if (lua_isstring(L, 1)) {
- int type = optencoding(L, 2, "*", X509_ANY|X509_PEM|X509_DER);
+ int format = optencoding(L, 2, "*", X509_ANY|X509_PEM|X509_DER);
int pubonly = 0, prvtonly = 0;
const char *opt, *data;
size_t len;
@@ -4325,7 +4325,7 @@ static int pk_new(lua_State *L) {
if (!(bio = BIO_new_mem_buf((void *)data, len)))
return auxL_error(L, auxL_EOPENSSL, "pkey.new");

- if (type == X509_PEM || type == X509_ANY) {
+ if (format == X509_PEM || format == X509_ANY) {
if (!prvtonly && !pub) {
/*
* BIO_reset is a rewind for read-only
@@ -4346,7 +4346,7 @@ static int pk_new(lua_State *L) {
}
}

- if (type == X509_DER || type == X509_ANY) {
+ if (format == X509_DER || format == X509_ANY) {
if (!prvtonly && !pub) {
BIO_reset(bio);

--
2.24.1

Loading

0 comments on commit b9dd8d8

Please sign in to comment.