src/x509.c

Fri, 05 Nov 2010 23:12:50 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 05 Nov 2010 23:12:50 +0000
changeset 20
ad5eb4fd28f5
parent 18
2c6fbfe07883
child 22
c4452dfd6ade
permissions
-rw-r--r--

Move asn1 Lua stack helpers to x509.c from ssl.c

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
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 int meth_decode(lua_State* L)
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;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 int i, j, n_entries;
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
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 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
87
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 lua_newtable(L); /* {} */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 lua_pushvalue(L, -1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 lua_setfield(L, -3, "subject"); /* ret.subject = {} */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 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
92 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 X509_NAME_ENTRY *entry;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 ASN1_OBJECT *object;
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 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
97 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
98
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 luasec_push_asn1_objname(L, object, 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 if(luasec_push_subtable(L, -2))
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 /* Get short/long name of the entry */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 luasec_push_asn1_objname(L, object, 0);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 lua_setfield(L, -2, "name");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 }
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 luasec_push_asn1_string(L, X509_NAME_ENTRY_get_data(entry));
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 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
110
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 lua_pop(L, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 lua_pop(L, 1); /* ret.subject */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 lua_newtable(L); /* {} */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 lua_pushvalue(L, -1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 lua_setfield(L, -3, "extensions"); /* ret.extensions = {} */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 i = -1;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 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
122 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 X509_EXTENSION *extension;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 STACK_OF(GENERAL_NAME) *values;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 int n_general_names;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 extension = X509_get_ext(peer, i);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 if(extension == NULL)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 values = X509V3_EXT_d2i(extension);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 if(values == NULL)
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 /* Push ret.extensions[oid] */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 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
137 luasec_push_subtable(L, -2);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 /* Set ret.extensions[oid].name = name */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 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
140 lua_setfield(L, -2, "name");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 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
143 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
144 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 GENERAL_NAME *general_name;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 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
148
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 switch(general_name->type)
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 case GEN_OTHERNAME:
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 OTHERNAME *otherName = general_name->d.otherName;
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 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
156
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 if(luasec_push_subtable(L, -2))
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 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
160 lua_setfield(L, -2, "name");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 }
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 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
164 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
165
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 lua_pop(L, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 break;
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 case GEN_DNS:
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 lua_pushstring(L, "dNSName");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 luasec_push_subtable(L, -2);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 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
174 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
175 lua_pop(L, 1);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 default:
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 break;
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 }
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 lua_pop(L, 1); /* array */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 i++; /* Next extension */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 }
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 lua_pop(L, 1); /* ret.extensions */
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[] = {
18
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
251 {"decode", meth_decode},
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
252 {"pem", meth_pem},
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
253 {"digest", meth_digest},
2c6fbfe07883 x509: Whitespace tweaking
Matthew Wild <mwild1@gmail.com>
parents: 17
diff changeset
254 {NULL, NULL}
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 };
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 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
258 {
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 /* Register the functions and tables */
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260 luaL_newmetatable(L, "SSL:Certificate");
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 lua_newtable(L);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 luaL_register(L, NULL, meta);
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 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
264
f1de983ff659 src/x509.c: Fix compiler warning, return module table
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
265 lua_newtable(L);
f1de983ff659 src/x509.c: Fix compiler warning, return module table
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
266 return 1;
14
1927b7b32faf Split X509 decoding into a separate module, ssl.x509
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 }

mercurial