aboutsummaryrefslogtreecommitdiffstats
path: root/main/lua-ossl/0003-get-named-extension-from-certificate.patch
blob: d1da746a2e2f586e8595986bdc20f20796ffeea8 (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
59
60
61
62
From 1da611092ca5d925020ce4e51aa9e603646ff79f Mon Sep 17 00:00:00 2001
From: Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
Date: Thu, 14 May 2015 14:28:00 +0300
Subject: [PATCH 3/5] get named extension from certificate

---
 src/openssl.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/openssl.c b/src/openssl.c
index 941da9b..8564ce1 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -3632,6 +3632,37 @@ static int xc_addExtension(lua_State *L) {
 } /* xc_addExtension() */
 
 
+static int xc_getExtension(lua_State *L) {
+	X509 *crt = checksimple(L, 1, X509_CERT_CLASS);
+	const char *name = luaL_checkstring(L, 2);
+
+	X509_EXTENSION *ext, **ud;
+	ASN1_OBJECT *obj = NULL;
+
+	if (!(obj = OBJ_txt2obj(name, 0)))
+		goto error;
+
+	int i = X509_get_ext_by_OBJ(crt, obj, -1);
+	if (i > -1) {
+		ud = prepsimple(L, X509_EXT_CLASS);
+		if (!(ext = X509_get_ext(crt, i)))
+			goto error;
+		if (!(*ud = X509_EXTENSION_dup(ext)))
+			goto error;
+	}
+	else lua_pushnil(L);
+
+	ASN1_OBJECT_free(obj);
+	return 1;
+
+error:
+	if (obj)
+		ASN1_OBJECT_free(obj);
+
+	return auxL_error(L, auxL_EOPENSSL, "x509.cert:getExtension");
+} /* xc_getExtension() */
+
+
 static int xc_isIssuedBy(lua_State *L) {
 	X509 *crt = checksimple(L, 1, X509_CERT_CLASS);
 	X509 *issuer = checksimple(L, 2, X509_CERT_CLASS);
@@ -3865,6 +3896,7 @@ static const luaL_Reg xc_methods[] = {
 	{ "getBasicConstraintsCritical", &xc_getBasicConstraintsCritical },
 	{ "setBasicConstraintsCritical", &xc_setBasicConstraintsCritical },
 	{ "addExtension",  &xc_addExtension },
+	{ "getExtension",  &xc_getExtension },
 	{ "isIssuedBy",    &xc_isIssuedBy },
 	{ "getPublicKey",  &xc_getPublicKey },
 	{ "setPublicKey",  &xc_setPublicKey },
-- 
2.1.0