-
Notifications
You must be signed in to change notification settings - Fork 118
/
lua_asn1.c
199 lines (178 loc) · 4.51 KB
/
lua_asn1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**********************************************************************
*
* This file is part of Cardpeek, the smart card reader utility.
*
* Copyright 2009-2013 by Alain Pannetrat <[email protected]>
*
* Cardpeek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cardpeek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cardpeek. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <lauxlib.h>
#include "lua_asn1.h"
#include "asn1.h"
#include "lua_bytes.h"
#include "misc.h"
static int subr_asn1_split(lua_State* L)
{
const bytestring_t *tlv = luaL_check_bytestring(L,1);
unsigned tag;
bytestring_t *value = bytestring_new(8);
bytestring_t *tail = bytestring_new(8);
unsigned pos=0;
if (asn1_decode_tlv(&pos,tlv,&tag,value)==BYTESTRING_OK)
{
lua_pushinteger(L,tag);
lua_push_bytestring(L,value);
if (pos<bytestring_get_size(tlv))
{
bytestring_substr(tail,pos,BYTESTRING_NPOS,tlv);
lua_push_bytestring(L,tail);
}
else
{
bytestring_free(tail);
lua_pushnil(L);
}
}
else
{
bytestring_free(value);
bytestring_free(tail);
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
}
return 3;
}
static int subr_asn1_split_tag(lua_State* L)
{
const bytestring_t *tlv = luaL_check_bytestring(L,1);
unsigned tag;
bytestring_t *tail = bytestring_new(8);
unsigned pos=0;
if (asn1_decode_tag(&pos,tlv,&tag)==BYTESTRING_OK)
{
lua_pushinteger(L,tag);
if (pos<bytestring_get_size(tlv))
{
bytestring_substr(tail,pos,BYTESTRING_NPOS,tlv);
lua_push_bytestring(L,tail);
}
else
{
bytestring_free(tail);
lua_pushnil(L);
}
}
else
{
bytestring_free(tail);
lua_pushnil(L);
lua_pushnil(L);
}
return 2;
}
static int subr_asn1_split_length(lua_State* L)
{
const bytestring_t *tlv = luaL_check_bytestring(L,1);
unsigned len;
bytestring_t *tail = bytestring_new(8);
unsigned pos=0;
if (asn1_decode_length(&pos,tlv,&len)==BYTESTRING_OK)
{
lua_pushinteger(L,len);
if (pos<bytestring_get_size(tlv))
{
bytestring_substr(tail,pos,BYTESTRING_NPOS,tlv);
lua_push_bytestring(L,tail);
}
else
{
bytestring_free(tail);
lua_pushnil(L);
}
}
else
{
bytestring_free(tail);
lua_pushnil(L);
lua_pushnil(L);
}
return 2;
}
static int subr_asn1_join(lua_State* L)
{
unsigned tag;
bytestring_t *value;
bytestring_t *tail = NULL;
bytestring_t *tlv = bytestring_new(8);
int clear_value=0;
int clear_tail=0;
tag = (unsigned) lua_tointeger(L,1);
if (lua_isnoneornil(L,2))
value = NULL;
else
{
if (lua_type(L,2)==LUA_TSTRING)
{
value = bytestring_new(8);
bytestring_assign_digit_string(value,lua_tostring(L,2));
clear_value = 1;
}
else
value = luaL_check_bytestring(L,2);
}
asn1_encode_tlv(tag,value,tlv);
if (!lua_isnoneornil(L,3))
{
if (lua_type(L,3)==LUA_TSTRING)
{
tail = bytestring_new(8);
bytestring_assign_digit_string(tail,lua_tostring(L,3));
clear_tail = 1;
}
else
tail = luaL_check_bytestring(L,3);
bytestring_append(tlv,tail);
}
if (clear_value) bytestring_free(value);
if (clear_tail) bytestring_free(tail);
lua_push_bytestring(L,tlv);
return 1;
}
static int subr_asn1_enable_single_byte_length(lua_State* L)
{
int enable = lua_toboolean(L,1);
asn1_force_single_byte_length_parsing(enable);
if (enable)
log_printf(LOG_INFO,"Single byte length ASN1 decoding enabled");
else
log_printf(LOG_INFO,"Single byte length ASN1 decoding disabled");
return 0;
}
static const struct luaL_Reg asn1lib [] = {
{"join", subr_asn1_join },
{"split", subr_asn1_split },
{"split_tag", subr_asn1_split_tag },
{"split_length", subr_asn1_split_length},
{"enable_single_byte_length", subr_asn1_enable_single_byte_length },
{NULL, NULL} /* sentinel */
};
int luaopen_asn1(lua_State* L)
{
luaL_newlib(L,asn1lib);
lua_setglobal(L,"asn1");
/* luaL_openlib(L,"asn1",asn1lib,0); */
return 1;
}