File: ec.c

package info (click to toggle)
lua-sec 0.7-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 808 kB
  • sloc: ansic: 3,332; makefile: 106; sh: 46
file content (110 lines) | stat: -rw-r--r-- 2,262 bytes parent folder | download | duplicates (2)
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
#include <openssl/objects.h>

#include "ec.h"

#ifndef OPENSSL_NO_ECDH

EC_KEY *lsec_find_ec_key(lua_State *L, const char *str)
{
  int nid;
  lua_pushstring(L, "SSL:EC:CURVES");
  lua_rawget(L, LUA_REGISTRYINDEX);
  lua_pushstring(L, str);
  lua_rawget(L, -2);

  if (!lua_isnumber(L, -1))
    return NULL;

  nid = (int)lua_tonumber(L, -1);
  return EC_KEY_new_by_curve_name(nid);
}

void lsec_load_curves(lua_State *L)
{
  size_t i;
  size_t size;
  const char *name;
  EC_builtin_curve *curves = NULL;

  lua_pushstring(L, "SSL:EC:CURVES");
  lua_newtable(L);

  size = EC_get_builtin_curves(NULL, 0);
  if (size > 0) {
    curves = (EC_builtin_curve*)malloc(sizeof(EC_builtin_curve) * size);
    EC_get_builtin_curves(curves, size);
    for (i = 0; i < size; i++) {
      name = OBJ_nid2sn(curves[i].nid);
      if (name != NULL) {
        lua_pushstring(L, name);
        lua_pushnumber(L, curves[i].nid);
        lua_rawset(L, -3);
      }
      switch (curves[i].nid) {
      case NID_X9_62_prime256v1:
        lua_pushstring(L, "P-256");
        lua_pushnumber(L, curves[i].nid);
        lua_rawset(L, -3);
        break;
      case NID_secp384r1:
        lua_pushstring(L, "P-384");
        lua_pushnumber(L, curves[i].nid);
        lua_rawset(L, -3);
        break;
      case NID_secp521r1:
        lua_pushstring(L, "P-521");
        lua_pushnumber(L, curves[i].nid);
        lua_rawset(L, -3);
        break;
#ifdef NID_X25519
     case NID_X25519:
        lua_pushstring(L, "X25519");
        lua_pushnumber(L, curves[i].nid);
        lua_rawset(L, -3);
        break;
#endif
#ifdef NID_X448
     case NID_X448:
        lua_pushstring(L, "X448");
        lua_pushnumber(L, curves[i].nid);
        lua_rawset(L, -3);
        break;
#endif
      }
    }
    free(curves);
  }

  lua_rawset(L,  LUA_REGISTRYINDEX);
}

void lsec_get_curves(lua_State *L)
{
  lua_newtable(L);

  lua_pushstring(L, "SSL:EC:CURVES");
  lua_rawget(L, LUA_REGISTRYINDEX);

  lua_pushnil(L);
  while (lua_next(L, -2) != 0) {
    lua_pop(L, 1);
    lua_pushvalue(L, -1);
    lua_pushboolean(L, 1);
    lua_rawset(L, -5);
  }
  lua_pop(L, 1);
}

#else

void lsec_load_curves(lua_State *L)
{
  // do nothing
}

void lsec_get_curves(lua_State *L)
{
  lua_newtable(L);
}

#endif