lua/lmem.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: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $
icculus@0
     3
** Interface to Memory Manager
icculus@0
     4
** See Copyright Notice in lua.h
icculus@0
     5
*/
icculus@0
     6
icculus@0
     7
icculus@0
     8
#include <stddef.h>
icculus@0
     9
icculus@0
    10
#define lmem_c
icculus@0
    11
#define LUA_CORE
icculus@0
    12
icculus@0
    13
#include "lua.h"
icculus@0
    14
icculus@0
    15
#include "ldebug.h"
icculus@0
    16
#include "ldo.h"
icculus@0
    17
#include "lgc.h"
icculus@0
    18
#include "lmem.h"
icculus@0
    19
#include "lobject.h"
icculus@0
    20
#include "lstate.h"
icculus@0
    21
icculus@0
    22
icculus@0
    23
icculus@0
    24
/*
icculus@0
    25
** About the realloc function:
icculus@0
    26
** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
icculus@0
    27
** (`osize' is the old size, `nsize' is the new size)
icculus@0
    28
**
icculus@0
    29
** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
icculus@0
    30
** matter 'x').
icculus@0
    31
**
icculus@0
    32
** * frealloc(ud, p, x, 0) frees the block `p'
icculus@0
    33
** (in this specific case, frealloc must return NULL);
icculus@0
    34
** particularly, frealloc(ud, NULL, 0, 0) does nothing
icculus@0
    35
** (which is equivalent to free(NULL) in ANSI C)
icculus@0
    36
**
icculus@0
    37
** frealloc returns NULL if it cannot create or reallocate the area
icculus@0
    38
** (any reallocation to an equal or smaller size cannot fail!)
icculus@0
    39
*/
icculus@0
    40
icculus@0
    41
icculus@0
    42
icculus@0
    43
#define MINSIZEARRAY	4
icculus@0
    44
icculus@0
    45
icculus@0
    46
void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,
icculus@0
    47
                     int limit, const char *what) {
icculus@0
    48
  void *newblock;
icculus@0
    49
  int newsize;
icculus@0
    50
  if (*size >= limit/2) {  /* cannot double it? */
icculus@0
    51
    if (*size >= limit)  /* cannot grow even a little? */
icculus@0
    52
      luaG_runerror(L, "too many %s (limit is %d)", what, limit);
icculus@0
    53
    newsize = limit;  /* still have at least one free place */
icculus@0
    54
  }
icculus@0
    55
  else {
icculus@0
    56
    newsize = (*size)*2;
icculus@0
    57
    if (newsize < MINSIZEARRAY)
icculus@0
    58
      newsize = MINSIZEARRAY;  /* minimum size */
icculus@0
    59
  }
icculus@0
    60
  newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
icculus@0
    61
  *size = newsize;  /* update only when everything else is OK */
icculus@0
    62
  return newblock;
icculus@0
    63
}
icculus@0
    64
icculus@0
    65
icculus@0
    66
l_noret luaM_toobig (lua_State *L) {
icculus@0
    67
  luaG_runerror(L, "memory allocation error: block too big");
icculus@0
    68
}
icculus@0
    69
icculus@0
    70
icculus@0
    71
icculus@0
    72
/*
icculus@0
    73
** generic allocation routine.
icculus@0
    74
*/
icculus@0
    75
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
icculus@0
    76
  void *newblock;
icculus@0
    77
  global_State *g = G(L);
icculus@0
    78
  size_t realosize = (block) ? osize : 0;
icculus@0
    79
  lua_assert((realosize == 0) == (block == NULL));
icculus@0
    80
#if defined(HARDMEMTESTS)
icculus@0
    81
  if (nsize > realosize && g->gcrunning)
icculus@0
    82
    luaC_fullgc(L, 1);  /* force a GC whenever possible */
icculus@0
    83
#endif
icculus@0
    84
  newblock = (*g->frealloc)(g->ud, block, osize, nsize);
icculus@0
    85
  if (newblock == NULL && nsize > 0) {
icculus@0
    86
    api_check(L, nsize > realosize,
icculus@0
    87
                 "realloc cannot fail when shrinking a block");
icculus@0
    88
    if (g->gcrunning) {
icculus@0
    89
      luaC_fullgc(L, 1);  /* try to free some memory... */
icculus@0
    90
      newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
icculus@0
    91
    }
icculus@0
    92
    if (newblock == NULL)
icculus@0
    93
      luaD_throw(L, LUA_ERRMEM);
icculus@0
    94
  }
icculus@0
    95
  lua_assert((nsize == 0) == (newblock == NULL));
icculus@0
    96
  g->GCdebt = (g->GCdebt + nsize) - realosize;
icculus@0
    97
  return newblock;
icculus@0
    98
}
icculus@0
    99