lua/lzio.h
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: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $
icculus@0
     3
** Buffered streams
icculus@0
     4
** See Copyright Notice in lua.h
icculus@0
     5
*/
icculus@0
     6
icculus@0
     7
icculus@0
     8
#ifndef lzio_h
icculus@0
     9
#define lzio_h
icculus@0
    10
icculus@0
    11
#include "lua.h"
icculus@0
    12
icculus@0
    13
#include "lmem.h"
icculus@0
    14
icculus@0
    15
icculus@0
    16
#define EOZ	(-1)			/* end of stream */
icculus@0
    17
icculus@0
    18
typedef struct Zio ZIO;
icculus@0
    19
icculus@0
    20
#define zgetc(z)  (((z)->n--)>0 ?  cast_uchar(*(z)->p++) : luaZ_fill(z))
icculus@0
    21
icculus@0
    22
icculus@0
    23
typedef struct Mbuffer {
icculus@0
    24
  char *buffer;
icculus@0
    25
  size_t n;
icculus@0
    26
  size_t buffsize;
icculus@0
    27
} Mbuffer;
icculus@0
    28
icculus@0
    29
#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
icculus@0
    30
icculus@0
    31
#define luaZ_buffer(buff)	((buff)->buffer)
icculus@0
    32
#define luaZ_sizebuffer(buff)	((buff)->buffsize)
icculus@0
    33
#define luaZ_bufflen(buff)	((buff)->n)
icculus@0
    34
icculus@0
    35
#define luaZ_resetbuffer(buff) ((buff)->n = 0)
icculus@0
    36
icculus@0
    37
icculus@0
    38
#define luaZ_resizebuffer(L, buff, size) \
icculus@0
    39
	(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
icculus@0
    40
	(buff)->buffsize = size)
icculus@0
    41
icculus@0
    42
#define luaZ_freebuffer(L, buff)	luaZ_resizebuffer(L, buff, 0)
icculus@0
    43
icculus@0
    44
icculus@0
    45
LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
icculus@0
    46
LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
icculus@0
    47
                                        void *data);
icculus@0
    48
LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n);	/* read next n bytes */
icculus@0
    49
icculus@0
    50
icculus@0
    51
icculus@0
    52
/* --------- Private Part ------------------ */
icculus@0
    53
icculus@0
    54
struct Zio {
icculus@0
    55
  size_t n;			/* bytes still unread */
icculus@0
    56
  const char *p;		/* current position in buffer */
icculus@0
    57
  lua_Reader reader;		/* reader function */
icculus@0
    58
  void* data;			/* additional data */
icculus@0
    59
  lua_State *L;			/* Lua state (for reader) */
icculus@0
    60
};
icculus@0
    61
icculus@0
    62
icculus@0
    63
LUAI_FUNC int luaZ_fill (ZIO *z);
icculus@0
    64
icculus@0
    65
#endif