~ubuntu-branches/ubuntu/trusty/lua-sec/trusty

« back to all changes in this revision

Viewing changes to src/x509.c

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2014-02-01 14:21:36 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20140201142136-a3y41pzungz2w06i
Tags: 0.5-1
* New upstream release 
* Bump Standards-Version to 3.9.5, no changes
* Copyright updated and ported to format 1.0 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*--------------------------------------------------------------------------
2
 
 * LuaSec 0.4.1
3
 
 * Copyright (C) 2012
 
2
 * LuaSec 0.5
 
3
 *
 
4
 * Copyright (C) 2014 Kim Alvefur, Paul Aurich, Tobias Markmann
 
5
 *                    Matthew Wild, Bruno Silvestre.
4
6
 *
5
7
 *--------------------------------------------------------------------------*/
6
8
 
32
34
{
33
35
  p_x509 cert_obj = (p_x509)lua_newuserdata(L, sizeof(t_x509));
34
36
  cert_obj->cert = cert;
 
37
  cert_obj->encode = LSEC_AI5_STRING;
35
38
  luaL_getmetatable(L, "SSL:Certificate");
36
39
  lua_setmetatable(L, -2);
37
40
}
44
47
  return ((p_x509)luaL_checkudata(L, idx, "SSL:Certificate"))->cert;
45
48
}
46
49
 
 
50
/**
 
51
 * Return LuaSec certificate X509 representation.
 
52
 */
 
53
p_x509 lsec_checkp_x509(lua_State* L, int idx)
 
54
{
 
55
  return (p_x509)luaL_checkudata(L, idx, "SSL:Certificate");
 
56
}
 
57
 
47
58
/*---------------------------------------------------------------------------*/
48
59
 
49
60
/**
73
84
/**
74
85
 * Push the ASN1 string on the stack.
75
86
 */
76
 
static void push_asn1_string(lua_State* L, ASN1_STRING *string)
 
87
static void push_asn1_string(lua_State* L, ASN1_STRING *string, int encode)
77
88
{
78
 
  if (string)
 
89
  size_t len;
 
90
  unsigned char *data;
 
91
  if (!string)
 
92
    lua_pushnil(L);
 
93
  switch (encode) {
 
94
  case LSEC_AI5_STRING:
79
95
    lua_pushlstring(L, (char*)ASN1_STRING_data(string),
80
96
                       ASN1_STRING_length(string));
81
 
  else
82
 
    lua_pushnil(L);
 
97
    break;
 
98
  case LSEC_UTF8_STRING:
 
99
    len = ASN1_STRING_to_UTF8(&data, string);
 
100
    if (len >= 0) {
 
101
      lua_pushlstring(L, (char*)data, len);
 
102
      OPENSSL_free(data);
 
103
    }
 
104
  }
83
105
}
84
106
 
85
107
/**
120
142
/**
121
143
 * Retrive the general names from the object.
122
144
 */
123
 
static int push_x509_name(lua_State* L, X509_NAME *name)
 
145
static int push_x509_name(lua_State* L, X509_NAME *name, int encode)
124
146
{
125
147
  int i;
126
148
  int n_entries;
136
158
    lua_setfield(L, -2, "oid");
137
159
    push_asn1_objname(L, object, 0);
138
160
    lua_setfield(L, -2, "name");
139
 
    push_asn1_string(L, X509_NAME_ENTRY_get_data(entry));
 
161
    push_asn1_string(L, X509_NAME_ENTRY_get_data(entry), encode);
140
162
    lua_setfield(L, -2, "value");
141
163
    lua_rawseti(L, -2, i+1);
142
164
  }
150
172
 */
151
173
static int meth_subject(lua_State* L)
152
174
{
153
 
  return push_x509_name(L, X509_get_subject_name(lsec_checkx509(L, 1)));
 
175
  p_x509 px = lsec_checkp_x509(L, 1);
 
176
  return push_x509_name(L, X509_get_subject_name(px->cert), px->encode);
154
177
}
155
178
 
156
179
/**
158
181
 */
159
182
static int meth_issuer(lua_State* L)
160
183
{
161
 
  return push_x509_name(L, X509_get_issuer_name(lsec_checkx509(L, 1)));
 
184
  p_x509 px = lsec_checkp_x509(L, 1);
 
185
  return push_x509_name(L, X509_get_issuer_name(px->cert), px->encode);
162
186
}
163
187
 
164
188
/**
173
197
  X509_EXTENSION *extension;
174
198
  GENERAL_NAME *general_name;
175
199
  STACK_OF(GENERAL_NAME) *values;
176
 
  X509 *peer = lsec_checkx509(L, 1);
 
200
  p_x509 px  = lsec_checkp_x509(L, 1);
 
201
  X509 *peer = px->cert;
177
202
 
178
203
  /* Return (ret) */
179
204
  lua_newtable(L);
205
230
          push_asn1_objname(L, otherName->type_id, 0);
206
231
          lua_setfield(L, -2, "name");
207
232
        }
208
 
        push_asn1_string(L, otherName->value->value.asn1_string);
 
233
        push_asn1_string(L, otherName->value->value.asn1_string, px->encode);
209
234
        lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
210
235
        lua_pop(L, 1);
211
236
        break;
212
237
      case GEN_DNS:
213
238
        lua_pushstring(L, "dNSName");
214
239
        push_subtable(L, -2);
215
 
        push_asn1_string(L, general_name->d.dNSName);
 
240
        push_asn1_string(L, general_name->d.dNSName, px->encode);
216
241
        lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
217
242
        lua_pop(L, 1);
218
243
        break;
219
244
      case GEN_EMAIL:
220
245
        lua_pushstring(L, "rfc822Name");
221
246
        push_subtable(L, -2);
222
 
        push_asn1_string(L, general_name->d.rfc822Name);
 
247
        push_asn1_string(L, general_name->d.rfc822Name, px->encode);
223
248
        lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
224
249
        lua_pop(L, 1);
225
250
        break;
226
251
      case GEN_URI:
227
252
        lua_pushstring(L, "uniformResourceIdentifier");
228
253
        push_subtable(L, -2);
229
 
        push_asn1_string(L, general_name->d.uniformResourceIdentifier);
 
254
        push_asn1_string(L, general_name->d.uniformResourceIdentifier, px->encode);
230
255
        lua_rawseti(L, -2, lua_rawlen(L, -2)+1);
231
256
        lua_pop(L, 1);
232
257
        break;
233
258
      case GEN_IPADD:
234
259
        lua_pushstring(L, "iPAddress");
235
260
        push_subtable(L, -2);
236
 
        push_asn1_string(L, general_name->d.iPAddress);
 
261
        push_asn1_string(L, general_name->d.iPAddress, px->encode);
237
262
        lua_rawseti(L, -2, lua_rawlen(L, -2)+1);
238
263
        lua_pop(L, 1);
239
264
        break;
306
331
  }
307
332
  if (!digest) {
308
333
    lua_pushnil(L);
309
 
    lua_pushstring(L, "digest algorithm not supported");
 
334
    lua_pushfstring(L, "digest algorithm not supported (%s)", str);
310
335
    return 2;
311
336
  }
312
337
  if (!X509_digest(cert, digest, buffer, &bytes)) {
313
338
    lua_pushnil(L);
314
 
    lua_pushstring(L, "error processing the certificate");
 
339
    lua_pushfstring(L, "error processing the certificate (%s)",
 
340
      ERR_reason_error_string(ERR_get_error()));
315
341
    return 2;
316
342
  }
317
343
  to_hex((char*)buffer, bytes, hex_buffer);
383
409
  return 1;
384
410
}
385
411
 
 
412
/**
 
413
 * Set the encode for ASN.1 string.
 
414
 */
 
415
static int meth_set_encode(lua_State* L)
 
416
{
 
417
  int succ = 0;
 
418
  p_x509 px = lsec_checkp_x509(L, 1);
 
419
  const char *enc = luaL_checkstring(L, 2);
 
420
  if (strncmp(enc, "ai5", 3) == 0) {
 
421
    succ = 1;
 
422
    px->encode = LSEC_AI5_STRING;
 
423
  } else if (strncmp(enc, "utf8", 4) == 0) {
 
424
    succ = 1;
 
425
    px->encode = LSEC_UTF8_STRING;
 
426
  }
 
427
  lua_pushboolean(L, succ);
 
428
  return 1;
 
429
}
 
430
 
386
431
/*---------------------------------------------------------------------------*/
387
432
 
388
433
static int load_cert(lua_State* L)
409
454
 */
410
455
static luaL_Reg methods[] = {
411
456
  {"digest",     meth_digest},
 
457
  {"setencode",  meth_set_encode},
412
458
  {"extensions", meth_extensions},
413
459
  {"issuer",     meth_issuer},
414
460
  {"notbefore",  meth_notbefore},