src/x509.c

Sun, 07 Nov 2010 17:12:33 -0800

author
Paul Aurich <paul@darkrain42.org>
date
Sun, 07 Nov 2010 17:12:33 -0800
changeset 27
3e0325d39a61
parent 26
bbff42d46512
child 29
a812bd8f1e6c
permissions
-rw-r--r--

x509: valid_at matches "not after" and "not before"

14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 /*--------------------------------------------------------------------------
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 * LuaSec 0.4
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2006-2009 Bruno Silvestre
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 *
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 *--------------------------------------------------------------------------*/
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 #include <string.h>
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 #include <openssl/ssl.h>
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 #include <openssl/x509v3.h>
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 #include <openssl/err.h>
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 #include <lua.h>
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 #include <lauxlib.h>
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 #include "io.h"
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 #include "buffer.h"
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 #include "timeout.h"
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 #include "socket.h"
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 #include "ssl.h"
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 #include "x509.h"
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 #define min(a, b) (a<b)?a:b
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 void luasec_push_x509(lua_State* L, X509 *cert)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 p_x509 cert_obj = (p_x509) lua_newuserdata(L, sizeof(t_x509));
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 cert_obj->cert = cert;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 luaL_getmetatable(L, "SSL:Certificate");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 lua_setmetatable(L, -2);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 X509* luasec_to_x509(lua_State* L, int idx)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return ((p_x509)luaL_checkudata(L, idx, "SSL:Certificate"))->cert;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
20
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
38 void luasec_push_asn1_objname(lua_State* L, ASN1_OBJECT *object, int no_name)
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
39 {
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
40 char buffer[256];
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
41 int len = OBJ_obj2txt(buffer, sizeof(buffer), object, no_name);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
42 lua_pushlstring(L, buffer, min(sizeof(buffer),len));
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
43 }
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
44
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
45 void luasec_push_asn1_string(lua_State* L, ASN1_STRING *string)
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
46 {
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
47 if(string)
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
48 lua_pushlstring(L, (char*)ASN1_STRING_data(string), ASN1_STRING_length(string));
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
49 else
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
50 lua_pushnil(L);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
51 }
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
52
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
53 int luasec_push_subtable(lua_State* L, int idx)
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
54 {
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
55
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
56 lua_pushvalue(L, -1);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
57 lua_gettable(L, idx-1);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
58
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
59 if(lua_isnil(L, -1))
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
60 {
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
61 lua_pop(L, 1);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
62 lua_newtable(L);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
63 lua_pushvalue(L, -2);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
64 lua_pushvalue(L, -2);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
65 lua_settable(L, idx-3);
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
66
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
67 lua_replace(L, -2); /* Replace key with table */
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
68 return 1;
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
69 }
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
70 lua_replace(L, -2); /* Replace key with table */
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
71 return 0;
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
72 }
ad5eb4fd28f5 Move asn1 Lua stack helpers to x509.c from ssl.c
Matthew Wild <mwild1@gmail.com>
parents: 18
diff changeset
73
23
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
74 void luasec_push_x509_name(lua_State* L, X509_NAME *name)
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 {
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
76 int i, n_entries;
23
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
77 lua_newtable(L);
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
78 n_entries = X509_NAME_entry_count(name);
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 for(i = 0; i <= n_entries; i++)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 X509_NAME_ENTRY *entry;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 ASN1_OBJECT *object;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
23
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
85 entry = X509_NAME_get_entry(name, i);
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 object = X509_NAME_ENTRY_get_object(entry);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
88 lua_newtable(L);
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
90 luasec_push_asn1_objname(L, object, 1);
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
91 lua_setfield(L, -2, "oid");
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
92
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
93 luasec_push_asn1_objname(L, object, 0);
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
94 lua_setfield(L, -2, "name");
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 luasec_push_asn1_string(L, X509_NAME_ENTRY_get_data(entry));
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
97 lua_setfield(L, -2, "value");
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
98
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 lua_rawseti(L, -2, lua_objlen(L, -2)+1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 }
23
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
102 }
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
103
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
104
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
105 int meth_subject(lua_State* L)
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
106 {
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
107 luasec_push_x509_name(L, X509_get_subject_name(luasec_to_x509(L, 1)));
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
108 return 1;
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
109 }
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
110
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
111 int meth_issuer(lua_State* L)
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
112 {
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
113 luasec_push_x509_name(L, X509_get_issuer_name(luasec_to_x509(L, 1)));
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
114 return 1;
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
115 }
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
117 int meth_extensions(lua_State* L)
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
118 {
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
119 X509 *peer;
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
120 int i, j;
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
122 peer = luasec_to_x509(L, 1);
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
123
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
124 lua_newtable(L); /* ret */
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 i = -1;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 while((i = X509_get_ext_by_NID(peer, NID_subject_alt_name, i)) != -1)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 X509_EXTENSION *extension;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 STACK_OF(GENERAL_NAME) *values;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 int n_general_names;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 extension = X509_get_ext(peer, i);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 if(extension == NULL)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 values = X509V3_EXT_d2i(extension);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 if(values == NULL)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
141 /* Push ret[oid] */
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 luasec_push_asn1_objname(L, extension->object, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 luasec_push_subtable(L, -2);
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
144 /* Set ret[oid].name = name */
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 luasec_push_asn1_objname(L, extension->object, 0);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 lua_setfield(L, -2, "name");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 n_general_names = sk_GENERAL_NAME_num(values);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 for(j = 0; j < n_general_names; j++)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 GENERAL_NAME *general_name;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 general_name = sk_GENERAL_NAME_value(values, j);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 switch(general_name->type)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 case GEN_OTHERNAME:
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 OTHERNAME *otherName = general_name->d.otherName;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 luasec_push_asn1_objname(L, otherName->type_id, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 if(luasec_push_subtable(L, -2))
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 luasec_push_asn1_objname(L, otherName->type_id, 0);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 lua_setfield(L, -2, "name");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 luasec_push_asn1_string(L, otherName->value->value.asn1_string);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 lua_rawseti(L, -2, lua_objlen(L, -2)+1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 lua_pop(L, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 case GEN_DNS:
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 lua_pushstring(L, "dNSName");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 luasec_push_subtable(L, -2);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 luasec_push_asn1_string(L, general_name->d.dNSName);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 lua_rawseti(L, -2, lua_objlen(L, -2)+1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 lua_pop(L, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 default:
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
189 lua_pop(L, 1); /* ret[oid] */
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 i++; /* Next extension */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 return 1;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194
24
bbf12f9be71c x509: Add :valid_at() method to discover whether a certificate would be valid at the given timestamp
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
195 int meth_valid_at(lua_State* L)
bbf12f9be71c x509: Add :valid_at() method to discover whether a certificate would be valid at the given timestamp
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
196 {
bbf12f9be71c x509: Add :valid_at() method to discover whether a certificate would be valid at the given timestamp
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
197 X509* cert = luasec_to_x509(L, 1);
bbf12f9be71c x509: Add :valid_at() method to discover whether a certificate would be valid at the given timestamp
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
198 time_t time = luaL_checkinteger(L, 2);
27
3e0325d39a61 x509: valid_at matches "not after" and "not before"
Paul Aurich <paul@darkrain42.org>
parents: 26
diff changeset
199 lua_pushboolean(L, (X509_cmp_time(X509_get_notAfter(cert), &time) >= 0
3e0325d39a61 x509: valid_at matches "not after" and "not before"
Paul Aurich <paul@darkrain42.org>
parents: 26
diff changeset
200 && X509_cmp_time(X509_get_notBefore(cert), &time) <= 0));
24
bbf12f9be71c x509: Add :valid_at() method to discover whether a certificate would be valid at the given timestamp
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
201 return 1;
bbf12f9be71c x509: Add :valid_at() method to discover whether a certificate would be valid at the given timestamp
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
202 }
bbf12f9be71c x509: Add :valid_at() method to discover whether a certificate would be valid at the given timestamp
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
203
16
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
204 int meth_pem(lua_State* L)
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
205 {
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
206 X509* cert = luasec_to_x509(L, 1);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
207 BIO *bio = BIO_new(BIO_s_mem());
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
208 char* data; long bytes;
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
209 if(!PEM_write_bio_X509(bio, cert))
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
210 {
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
211 lua_pushnil(L);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
212 return 1;
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
213 }
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
214 bytes = BIO_get_mem_data(bio, &data);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
215 if(bytes > 0)
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
216 lua_pushlstring(L, data, bytes);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
217 else
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
218 lua_pushnil(L);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
219 BIO_free(bio);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
220 return 1;
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
221 }
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
222
17
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
223 const char* hex_tab = "0123456789abcdef";
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
224 void to_hex(const char* in, int length, char* out) {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
225 int i;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
226 for (i = 0; i < length; i++) {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
227 out[i*2] = hex_tab[(in[i] >> 4) & 0xF];
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
228 out[i*2+1] = hex_tab[(in[i]) & 0xF];
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
229 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
230 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
231
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
232 int meth_digest(lua_State* L)
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
233 {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
234 X509 *cert;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
235 unsigned int bytes;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
236 unsigned char buffer[EVP_MAX_MD_SIZE];
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
237 char hex_buffer[EVP_MAX_MD_SIZE*2];
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
238 const EVP_MD *digest;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
239 cert = luasec_to_x509(L, 1);
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
240 if(lua_gettop(L) < 2 || strcmp(luaL_checkstring(L, 1), "sha1") == 0)
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
241 {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
242 digest = EVP_sha1();
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
243 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
244 else
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
245 {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
246 lua_pushnil(L);
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
247 lua_pushstring(L, "digest algorithm not supported");
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
248 return 2;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
249 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
250 if(!X509_digest(cert, digest, buffer, &bytes))
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
251 {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
252 lua_pushnil(L);
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
253 lua_pushstring(L, "out of memory");
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
254 return 2;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
255 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
256 to_hex((char*)buffer, bytes, hex_buffer);
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
257 lua_pushlstring(L, hex_buffer, bytes*2);
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
258 return 1;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
259 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
260
25
4bc25168aa1c x509: Add __gc to free X509 object on destruction
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
261 int meth_destroy(lua_State* L)
4bc25168aa1c x509: Add __gc to free X509 object on destruction
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
262 {
4bc25168aa1c x509: Add __gc to free X509 object on destruction
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
263 X509_free(luasec_to_x509(L, 1));
4bc25168aa1c x509: Add __gc to free X509 object on destruction
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
264 return 0;
4bc25168aa1c x509: Add __gc to free X509 object on destruction
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
265 }
4bc25168aa1c x509: Add __gc to free X509 object on destruction
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
266
26
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
267 int cert_from_pem(lua_State* L)
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
268 {
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
269 X509 *cert;
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
270 BIO *bio = BIO_new(BIO_s_mem());
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
271 const char* data; size_t bytes;
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
272 data = luaL_checklstring(L, 1, &bytes);
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
273 BIO_write(bio, data, bytes);
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
274 cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
275 if(cert)
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
276 luasec_push_x509(L, cert);
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
277 else
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
278 lua_pushnil(L);
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
279 BIO_free(bio);
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
280 return 1;
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
281 }
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
282
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 /**
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284 * Certificate metamethods
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 static luaL_Reg meta[] = {
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
287 {"subject", meth_subject},
23
20528cb40c4a x509: Add :issuer() method, change returned format for both :subject() and :issuer() to preserve order
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
288 {"issuer", meth_issuer},
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
289 {"extensions", meth_extensions},
24
bbf12f9be71c x509: Add :valid_at() method to discover whether a certificate would be valid at the given timestamp
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
290 {"valid_at", meth_valid_at},
18
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
291 {"pem", meth_pem},
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
292 {"digest", meth_digest},
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
293 {NULL, NULL}
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 };
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295
26
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
296 /**
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
297 * ssl.x509 functions
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
298 */
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
299 static luaL_Reg funcs[] = {
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
300 {"cert_from_pem", cert_from_pem},
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
301 {NULL, NULL}
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
302 };
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
303
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 LUASEC_API int luaopen_ssl_x509(lua_State *L)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 /* Register the functions and tables */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 luaL_newmetatable(L, "SSL:Certificate");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 lua_newtable(L);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 luaL_register(L, NULL, meta);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 lua_setfield(L, -2, "__index");
25
4bc25168aa1c x509: Add __gc to free X509 object on destruction
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
311 lua_pushcfunction(L, meth_destroy);
4bc25168aa1c x509: Add __gc to free X509 object on destruction
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
312 lua_setfield(L, -2, "__gc");
26
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
313
bbff42d46512 x509: Add ssl.cert_from_pem()
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
314 luaL_register(L, "ssl.x509", funcs);
15
f1de983ff659 src/x509.c: Fix compiler warning, return module table
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
315 return 1;
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316 }

mercurial