src/x509.c

Fri, 05 Nov 2010 23:59:34 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 05 Nov 2010 23:59:34 +0000
changeset 22
c4452dfd6ade
parent 20
ad5eb4fd28f5
child 23
20528cb40c4a
permissions
-rw-r--r--

x509: Split :decode() method into two methods, :subject() and :extensions()

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
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
74 int meth_subject(lua_State* L)
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 X509 *peer;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 X509_NAME *subject;
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
78 int i, n_entries;
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 peer = luasec_to_x509(L, 1);
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 lua_newtable(L); /* ret */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 subject = X509_get_subject_name(peer);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 n_entries = X509_NAME_entry_count(subject);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 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
88 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 X509_NAME_ENTRY *entry;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 ASN1_OBJECT *object;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 entry = X509_NAME_get_entry(subject, i);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 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
94
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
95 lua_newtable(L);
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
97 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
98 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
99
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
100 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
101 lua_setfield(L, -2, "name");
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 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
104 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
105
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 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
107
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 }
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
109 return 1;
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
110 }
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
112 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
113 {
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
114 X509 *peer;
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
115 int i, j;
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 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
118
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
119 lua_newtable(L); /* ret */
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 i = -1;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 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
123 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 X509_EXTENSION *extension;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 STACK_OF(GENERAL_NAME) *values;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 int n_general_names;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 extension = X509_get_ext(peer, i);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 if(extension == NULL)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 values = X509V3_EXT_d2i(extension);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 if(values == NULL)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
136 /* Push ret[oid] */
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 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
138 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
139 /* Set ret[oid].name = name */
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 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
141 lua_setfield(L, -2, "name");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 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
144 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
145 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 GENERAL_NAME *general_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 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
149
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 switch(general_name->type)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 case GEN_OTHERNAME:
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 OTHERNAME *otherName = general_name->d.otherName;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 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
157
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if(luasec_push_subtable(L, -2))
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 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
161 lua_setfield(L, -2, "name");
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
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 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
165 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
166
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 lua_pop(L, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 case GEN_DNS:
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_pushstring(L, "dNSName");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 luasec_push_subtable(L, -2);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 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
175 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
176 lua_pop(L, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 default:
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
22
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
184 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
185 i++; /* Next extension */
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 return 1;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
16
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
190 int meth_pem(lua_State* L)
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
191 {
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
192 X509* cert = luasec_to_x509(L, 1);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
193 BIO *bio = BIO_new(BIO_s_mem());
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
194 char* data; long bytes;
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
195 if(!PEM_write_bio_X509(bio, cert))
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
196 {
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
197 lua_pushnil(L);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
198 return 1;
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
199 }
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
200 bytes = BIO_get_mem_data(bio, &data);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
201 if(bytes > 0)
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
202 lua_pushlstring(L, data, bytes);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
203 else
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
204 lua_pushnil(L);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
205 BIO_free(bio);
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
206 return 1;
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
207 }
0cefcdd5b635 Add :pem() method to certificates
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
208
17
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
209 const char* hex_tab = "0123456789abcdef";
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
210 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
211 int i;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
212 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
213 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
214 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
215 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
216 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
217
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
218 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
219 {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
220 X509 *cert;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
221 unsigned int bytes;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
222 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
223 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
224 const EVP_MD *digest;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
225 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
226 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
227 {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
228 digest = EVP_sha1();
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 else
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 lua_pushnil(L);
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
233 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
234 return 2;
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
235 }
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
236 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
237 {
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
238 lua_pushnil(L);
4e3da35cc9ab x509: Add :digest() method to return cert sha1 fingerprint
Matthew Wild <mwild1@gmail.com>
parents: 16
diff changeset
239 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
240 return 2;
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 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
243 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
244 return 1;
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
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 /**
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 * Certificate metamethods
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 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
251 {"subject", meth_subject},
c4452dfd6ade x509: Split :decode() method into two methods, :subject() and :extensions()
Matthew Wild <mwild1@gmail.com>
parents: 20
diff changeset
252 {"extensions", meth_extensions},
18
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
253 {"pem", meth_pem},
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
254 {"digest", meth_digest},
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
255 {NULL, NULL}
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 };
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 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
259 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 /* Register the functions and tables */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 luaL_newmetatable(L, "SSL:Certificate");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 lua_newtable(L);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 luaL_register(L, NULL, meta);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 lua_setfield(L, -2, "__index");
15
f1de983ff659 src/x509.c: Fix compiler warning, return module table
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
265
f1de983ff659 src/x509.c: Fix compiler warning, return module table
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
266 lua_newtable(L);
f1de983ff659 src/x509.c: Fix compiler warning, return module table
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
267 return 1;
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 }

mercurial