lua/ltm.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 23 Jun 2017 17:28:03 -0400
changeset 58 1390348facc7
parent 0 d7ee4e2ed49d
permissions -rw-r--r--
Command line tool that decrypts an OPVault keychain and dumps it to stdout.

To compile: gcc -o opvault opvault.c cJSON.c -lcrypto

Usage: ./opvault </path/to/mykeychain.opvault> <password>

This is just a proof of concept; I'll be recycling this into proper OPVault
support in 1pass later and deleting this tool.

This uses OpenSSL's libcrypto for the math instead of all the homegrown
crypto this project is otherwise using. I'll probably migrate the rest in
this direction, too, since this wasn't as bad as I expected to use and
gets you all the package-manager mojo of automatic bug fixes and security
patches and shared code, etc.

cJSON parses JSON in C. That is from https://github.com/DaveGamble/cJSON

An example OPVault keychain from AgileBits is available here:

https://cache.agilebits.com/security-kb/
icculus@0
     1
/*
icculus@0
     2
** $Id: ltm.c,v 2.14.1.1 2013/04/12 18:48:47 roberto Exp $
icculus@0
     3
** Tag methods
icculus@0
     4
** See Copyright Notice in lua.h
icculus@0
     5
*/
icculus@0
     6
icculus@0
     7
icculus@0
     8
#include <string.h>
icculus@0
     9
icculus@0
    10
#define ltm_c
icculus@0
    11
#define LUA_CORE
icculus@0
    12
icculus@0
    13
#include "lua.h"
icculus@0
    14
icculus@0
    15
#include "lobject.h"
icculus@0
    16
#include "lstate.h"
icculus@0
    17
#include "lstring.h"
icculus@0
    18
#include "ltable.h"
icculus@0
    19
#include "ltm.h"
icculus@0
    20
icculus@0
    21
icculus@0
    22
static const char udatatypename[] = "userdata";
icculus@0
    23
icculus@0
    24
LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
icculus@0
    25
  "no value",
icculus@0
    26
  "nil", "boolean", udatatypename, "number",
icculus@0
    27
  "string", "table", "function", udatatypename, "thread",
icculus@0
    28
  "proto", "upval"  /* these last two cases are used for tests only */
icculus@0
    29
};
icculus@0
    30
icculus@0
    31
icculus@0
    32
void luaT_init (lua_State *L) {
icculus@0
    33
  static const char *const luaT_eventname[] = {  /* ORDER TM */
icculus@0
    34
    "__index", "__newindex",
icculus@0
    35
    "__gc", "__mode", "__len", "__eq",
icculus@0
    36
    "__add", "__sub", "__mul", "__div", "__mod",
icculus@0
    37
    "__pow", "__unm", "__lt", "__le",
icculus@0
    38
    "__concat", "__call"
icculus@0
    39
  };
icculus@0
    40
  int i;
icculus@0
    41
  for (i=0; i<TM_N; i++) {
icculus@0
    42
    G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
icculus@0
    43
    luaS_fix(G(L)->tmname[i]);  /* never collect these names */
icculus@0
    44
  }
icculus@0
    45
}
icculus@0
    46
icculus@0
    47
icculus@0
    48
/*
icculus@0
    49
** function to be used with macro "fasttm": optimized for absence of
icculus@0
    50
** tag methods
icculus@0
    51
*/
icculus@0
    52
const TValue *luaT_gettm (Table *events, TMS event, TString *ename) {
icculus@0
    53
  const TValue *tm = luaH_getstr(events, ename);
icculus@0
    54
  lua_assert(event <= TM_EQ);
icculus@0
    55
  if (ttisnil(tm)) {  /* no tag method? */
icculus@0
    56
    events->flags |= cast_byte(1u<<event);  /* cache this fact */
icculus@0
    57
    return NULL;
icculus@0
    58
  }
icculus@0
    59
  else return tm;
icculus@0
    60
}
icculus@0
    61
icculus@0
    62
icculus@0
    63
const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
icculus@0
    64
  Table *mt;
icculus@0
    65
  switch (ttypenv(o)) {
icculus@0
    66
    case LUA_TTABLE:
icculus@0
    67
      mt = hvalue(o)->metatable;
icculus@0
    68
      break;
icculus@0
    69
    case LUA_TUSERDATA:
icculus@0
    70
      mt = uvalue(o)->metatable;
icculus@0
    71
      break;
icculus@0
    72
    default:
icculus@0
    73
      mt = G(L)->mt[ttypenv(o)];
icculus@0
    74
  }
icculus@0
    75
  return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);
icculus@0
    76
}
icculus@0
    77