1pass.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 23 Dec 2013 15:57:24 -0500
changeset 12 6a2d5b34d5ca
parent 11 b52e0f1798b8
child 15 e2973f4fb366
permissions -rw-r--r--
Whole bunch of GUI work.
icculus@0
     1
#include <stdio.h>
icculus@0
     2
#include <stdlib.h>
icculus@0
     3
#include <string.h>
icculus@0
     4
#include <assert.h>
icculus@0
     5
#include "lua.h"
icculus@0
     6
#include "lauxlib.h"
icculus@0
     7
#include "lualib.h"
icculus@0
     8
#include "pkcs5_pbkdf2.h"
icculus@0
     9
#include "aes.h"
icculus@0
    10
#include "base64.h"
icculus@0
    11
#include "md5.h"
icculus@11
    12
#include <gtk/gtk.h>
icculus@0
    13
icculus@0
    14
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
icculus@0
    15
icculus@0
    16
static lua_State *luaState = NULL;
icculus@0
    17
static const uint8_t zero16[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
icculus@0
    18
static const char saltprefix[] = { 'S', 'a', 'l', 't', 'e', 'd', '_', '_' };
icculus@0
    19
icculus@0
    20
static inline int retvalStringBytes(lua_State *L, const uint8_t *str, size_t len)
icculus@0
    21
{
icculus@0
    22
    if (str != NULL)
icculus@0
    23
        lua_pushlstring(L, (const char *) str, len);
icculus@0
    24
    else
icculus@0
    25
        lua_pushnil(L);
icculus@0
    26
    return 1;
icculus@0
    27
} // retvalStringBytes
icculus@0
    28
icculus@12
    29
static inline int retvalString(lua_State *L, const char *str)
icculus@12
    30
{
icculus@12
    31
    return retvalStringBytes(L, (const uint8_t *) str, strlen(str));
icculus@12
    32
} // retvalString
icculus@12
    33
icculus@11
    34
static inline int retvalPointer(lua_State *L, void *ptr)
icculus@11
    35
{
icculus@11
    36
    if (ptr != NULL)
icculus@11
    37
        lua_pushlightuserdata(L, ptr);
icculus@11
    38
    else
icculus@11
    39
        lua_pushnil(L);
icculus@11
    40
    return 1;
icculus@11
    41
} // retvalPointer
icculus@11
    42
icculus@0
    43
static inline void xorBlock(uint8_t *dst, const uint8_t *src)
icculus@0
    44
{
icculus@0
    45
    int i;
icculus@0
    46
    for (i = 0; i < 16; i++, dst++, src++)
icculus@0
    47
        *dst ^= *src;
icculus@0
    48
} // xorBlock
icculus@0
    49
icculus@0
    50
static int decryptUsingKeyAndIvec(uint8_t *data, size_t *datalen, const uint8_t *key, const uint8_t *iv)
icculus@0
    51
{
icculus@0
    52
    const size_t blocks = *datalen / 16;
icculus@0
    53
    uint8_t *block = data + ((blocks-1) * 16);   // start at final block, work backwards
icculus@0
    54
    const uint8_t *padding = &block[15];
icculus@0
    55
    uint8_t expkey[aesExpandedKeySize];
icculus@0
    56
    size_t i;
icculus@0
    57
icculus@0
    58
    if (blocks == 0)
icculus@0
    59
        return 1;  // nothing to do.
icculus@0
    60
icculus@0
    61
	aesExpandKey(key, expkey);
icculus@0
    62
icculus@0
    63
    for (i = 0; i < blocks-1; i++)
icculus@0
    64
    {
icculus@0
    65
        aesDecrypt(block, expkey, block);   // decrypt in place.
icculus@0
    66
        xorBlock(block, block-16);
icculus@0
    67
        block -= 16;
icculus@0
    68
    }
icculus@0
    69
    aesDecrypt(block, expkey, block);   // decrypt in place.
icculus@0
    70
    xorBlock(block, iv);   // xor against initial vector for final block.
icculus@0
    71
icculus@0
    72
    if (*padding > 16)
icculus@0
    73
        return 0;  // bad data?
icculus@0
    74
icculus@0
    75
    *datalen -= *padding;
icculus@0
    76
icculus@0
    77
    return 1;
icculus@0
    78
} // decryptBinaryUsingKeyAndIvec
icculus@0
    79
icculus@0
    80
icculus@0
    81
static inline int isSalted(const uint8_t *data, const size_t datalen)
icculus@0
    82
{
icculus@0
    83
    return ( (datalen > sizeof (saltprefix)) &&
icculus@0
    84
             (memcmp(data, saltprefix, sizeof (saltprefix)) == 0) );
icculus@0
    85
} // isSalted
icculus@0
    86
icculus@0
    87
icculus@0
    88
static int decryptUsingPBKDF2(lua_State *L)
icculus@0
    89
{
icculus@0
    90
    const char *base64 = luaL_checkstring(L, 1);
icculus@0
    91
    const char *password = luaL_checkstring(L, 2);
icculus@0
    92
    const int iterations = luaL_checkinteger(L, 3);
icculus@0
    93
    size_t datalen = strlen(base64);
icculus@0
    94
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
icculus@0
    95
    uint8_t *data = dataptr;
icculus@0
    96
    base64_decodestate base64state;
icculus@0
    97
icculus@0
    98
    base64_init_decodestate(&base64state);
icculus@0
    99
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
icculus@0
   100
icculus@0
   101
    const uint8_t *salt = zero16;
icculus@0
   102
    int saltlen = sizeof (zero16);
icculus@0
   103
    if (isSalted(data, datalen))
icculus@0
   104
    {
icculus@0
   105
        salt = data + 8;
icculus@0
   106
        saltlen = 8;
icculus@0
   107
        data += 16;
icculus@0
   108
        datalen -= 16;
icculus@0
   109
    } // if
icculus@0
   110
icculus@0
   111
    uint8_t output[32];
icculus@0
   112
    pkcs5_pbkdf2(password, strlen(password), salt, saltlen, output, sizeof (output), (unsigned int) iterations);
icculus@0
   113
icculus@0
   114
    const uint8_t *aeskey = &output[0];
icculus@0
   115
    const uint8_t *aesiv = &output[16];
icculus@0
   116
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
icculus@0
   117
        retvalStringBytes(L, data, datalen);
icculus@0
   118
    else
icculus@0
   119
        lua_pushnil(L);
icculus@0
   120
icculus@0
   121
    free(dataptr);
icculus@0
   122
    return 1;
icculus@0
   123
} // decryptUsingPBKDF2
icculus@0
   124
icculus@0
   125
icculus@0
   126
static int decryptBase64UsingKey(lua_State *L)
icculus@0
   127
{
icculus@0
   128
    size_t keylen = 0;
icculus@0
   129
    const char *base64 = luaL_checkstring(L, 1);
icculus@0
   130
    const uint8_t *key = (const uint8_t *) luaL_checklstring(L, 2, &keylen);
icculus@0
   131
    size_t datalen = strlen(base64);
icculus@0
   132
    uint8_t *dataptr = (uint8_t *) malloc(datalen);
icculus@0
   133
    uint8_t *data = dataptr;
icculus@0
   134
    base64_decodestate base64state;
icculus@0
   135
icculus@0
   136
    base64_init_decodestate(&base64state);
icculus@0
   137
    datalen = base64_decode_block(base64, (int) datalen, data, &base64state);
icculus@0
   138
icculus@0
   139
    uint8_t aeskey[16];
icculus@0
   140
    uint8_t aesiv[16];
icculus@0
   141
    MD5_CTX md5;
icculus@0
   142
icculus@0
   143
    if (isSalted(data, datalen))
icculus@0
   144
    {
icculus@0
   145
        const uint8_t *salt = data + 8;
icculus@0
   146
        const size_t saltlen = 8;
icculus@0
   147
        data += 16;
icculus@0
   148
        datalen -= 16;
icculus@0
   149
icculus@0
   150
        assert(aesNr == 10);  // AES-256 needs more rounds.
icculus@0
   151
        assert(aesNk == 4);   // hashing size is hardcoded later.
icculus@0
   152
        uint8_t hashing[32];
icculus@0
   153
icculus@0
   154
        MD5_init(&md5);
icculus@0
   155
        MD5_append(&md5, key, keylen);
icculus@0
   156
        MD5_append(&md5, salt, saltlen);
icculus@0
   157
        MD5_finish(&md5, hashing);
icculus@0
   158
icculus@0
   159
        MD5_init(&md5);
icculus@0
   160
        MD5_append(&md5, hashing, 16);
icculus@0
   161
        MD5_append(&md5, key, keylen);
icculus@0
   162
        MD5_append(&md5, salt, saltlen);
icculus@0
   163
        MD5_finish(&md5, &hashing[16]);
icculus@0
   164
icculus@0
   165
        memcpy(aeskey, hashing, 4 * aesNk);
icculus@0
   166
        memcpy(aesiv, &hashing[4 * aesNk], 16);
icculus@0
   167
    } // if
icculus@0
   168
    else
icculus@0
   169
    {
icculus@0
   170
        MD5_init(&md5);
icculus@0
   171
        MD5_append(&md5, key, keylen);
icculus@0
   172
        MD5_finish(&md5, aeskey);
icculus@0
   173
        memset(aesiv, '\0', sizeof (aesiv));
icculus@0
   174
    } // else
icculus@0
   175
icculus@0
   176
	if (decryptUsingKeyAndIvec(data, &datalen, aeskey, aesiv))
icculus@0
   177
        retvalStringBytes(L, data, datalen);
icculus@0
   178
    else
icculus@0
   179
        lua_pushnil(L);
icculus@0
   180
icculus@0
   181
    free(dataptr);
icculus@0
   182
    return 1;
icculus@0
   183
} // decryptBase64UsingKey
icculus@0
   184
icculus@0
   185
icculus@12
   186
static int runGuiPasswordPrompt(lua_State *L)
icculus@12
   187
{
icculus@12
   188
    const char *hintstr = lua_tostring(L, 1);
icculus@12
   189
    GtkWidget *dialog = gtk_dialog_new_with_buttons(
icculus@12
   190
                            "Master Password", NULL, GTK_DIALOG_MODAL,
icculus@12
   191
                            GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
icculus@12
   192
                            GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
icculus@12
   193
                            NULL);
icculus@12
   194
icculus@12
   195
    GtkWidget *content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
icculus@12
   196
icculus@12
   197
    if (hintstr != NULL)
icculus@12
   198
    {
icculus@12
   199
        GtkWidget *label = gtk_label_new(hintstr);
icculus@12
   200
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
icculus@12
   201
        gtk_container_add(GTK_CONTAINER(content_area), label);
icculus@12
   202
    } // if
icculus@12
   203
icculus@12
   204
    GtkWidget *entry = gtk_entry_new();
icculus@12
   205
    gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
icculus@12
   206
    gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
icculus@12
   207
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
icculus@12
   208
    gtk_container_add(GTK_CONTAINER(content_area), entry);
icculus@12
   209
icculus@12
   210
    gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
icculus@12
   211
    gtk_widget_show_all(dialog);
icculus@12
   212
    const int ok = (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT);
icculus@12
   213
    retvalString(L, ok ? (const char *) gtk_entry_get_text(GTK_ENTRY(entry)) : NULL);
icculus@12
   214
    gtk_widget_destroy(dialog);
icculus@12
   215
icculus@12
   216
    return 1;
icculus@12
   217
} // runGuiPasswordPrompt
icculus@12
   218
icculus@12
   219
icculus@12
   220
static int copyToClipboard(lua_State *L)
icculus@12
   221
{
icculus@12
   222
    const char *str = luaL_checkstring(L, 1);
icculus@12
   223
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), str, -1);
icculus@12
   224
    gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), str, -1);
icculus@12
   225
} // copyToClipboard
icculus@12
   226
icculus@12
   227
icculus@11
   228
static int makeGuiMenu(lua_State *L)
icculus@11
   229
{
icculus@11
   230
    return retvalPointer(L, gtk_menu_new());
icculus@11
   231
} // makeGuiMenu
icculus@11
   232
icculus@11
   233
icculus@11
   234
static void clickedMenuItem(void *arg)
icculus@11
   235
{
icculus@11
   236
    // This is the callback from GTK+; now call into our actual Lua callback!
icculus@11
   237
    const int callback = (int) ((size_t)arg);
icculus@11
   238
    lua_rawgeti(luaState, LUA_REGISTRYINDEX, callback);
icculus@11
   239
    lua_call(luaState, 0, 0);
icculus@11
   240
} // clickedMenuItem
icculus@11
   241
icculus@12
   242
#if 0  // !!! FIXME: figure out how to fire this.
icculus@12
   243
static void deletedMenuItem(void *arg)
icculus@12
   244
{
icculus@12
   245
    // Clean up the Lua function we referenced in the Registry.
icculus@12
   246
    const int callback = (int) ((size_t)arg);
icculus@12
   247
printf("unref callback %d\n", callback);
icculus@12
   248
    luaL_unref(luaState, LUA_REGISTRYINDEX, callback);
icculus@12
   249
} // deletedMenuItem
icculus@12
   250
#endif
icculus@11
   251
icculus@11
   252
static int appendGuiMenuItem(lua_State *L)
icculus@11
   253
{
icculus@11
   254
    const int argc = lua_gettop(L);
icculus@11
   255
    GtkWidget *menu = (GtkWidget *) lua_touserdata(L, 1);
icculus@11
   256
    const char *label = luaL_checkstring(L, 2);
icculus@11
   257
    GtkWidget *item = gtk_menu_item_new_with_label(label);
icculus@11
   258
icculus@11
   259
    if ((argc >= 3) && (!lua_isnil(L, 3)))
icculus@11
   260
    {
icculus@11
   261
        assert(lua_isfunction(L, 3));
icculus@11
   262
        lua_pushvalue(L, 3);  // copy the Lua callback (luaL_ref() pops it).
icculus@11
   263
        const int callback = luaL_ref(L, LUA_REGISTRYINDEX);
icculus@11
   264
        gtk_signal_connect_object(GTK_OBJECT(item), "activate", GTK_SIGNAL_FUNC(clickedMenuItem), (gpointer) ((size_t)callback));
icculus@11
   265
    } // if
icculus@11
   266
icculus@11
   267
    gtk_widget_show(item);
icculus@11
   268
    gtk_menu_append(menu, item);
icculus@11
   269
    return retvalPointer(L, item);
icculus@11
   270
} // appendGuiMenuItem
icculus@11
   271
icculus@11
   272
icculus@11
   273
static int setGuiMenuItemSubmenu(lua_State *L)
icculus@11
   274
{
icculus@11
   275
    GtkMenuItem *item = (GtkMenuItem *) lua_touserdata(L, 1);
icculus@11
   276
    GtkWidget *submenu = (GtkWidget *) lua_touserdata(L, 2);
icculus@11
   277
    gtk_menu_item_set_submenu(item, submenu);
icculus@11
   278
    return 0;
icculus@11
   279
} // setGuiMenuItemSubmenu
icculus@11
   280
icculus@11
   281
icculus@11
   282
static int popupGuiMenu(lua_State *L)
icculus@11
   283
{
icculus@11
   284
    GtkMenu *menu = (GtkMenu *) lua_touserdata(L, 1);
icculus@11
   285
    gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
icculus@11
   286
    return 0;
icculus@11
   287
} // popupGuiMenu
icculus@11
   288
icculus@11
   289
icculus@11
   290
static int giveControlToGui(lua_State *L)
icculus@11
   291
{
icculus@11
   292
    gtk_main();
icculus@11
   293
    return 0;
icculus@11
   294
} // giveControlToGui
icculus@11
   295
icculus@7
   296
icculus@0
   297
static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize)
icculus@0
   298
{
icculus@0
   299
    if (nsize == 0)
icculus@0
   300
    {
icculus@0
   301
        free(ptr);
icculus@0
   302
        return NULL;
icculus@0
   303
    } // if
icculus@0
   304
    return realloc(ptr, nsize);
icculus@0
   305
} // luaAlloc
icculus@0
   306
icculus@0
   307
icculus@0
   308
static inline void luaSetCFunc(lua_State *L, lua_CFunction f, const char *sym)
icculus@0
   309
{
icculus@0
   310
    lua_pushcfunction(L, f);
icculus@0
   311
    lua_setglobal(luaState, sym);
icculus@0
   312
} // luaSetCFunc
icculus@0
   313
icculus@0
   314
icculus@0
   315
static int luaFatal(lua_State *L)
icculus@0
   316
{
icculus@0
   317
    const char *errstr = lua_tostring(L, -1);
icculus@0
   318
    fprintf(stderr, "Lua panic: %s\n", errstr ? errstr : "(?)");
icculus@0
   319
    fflush(stderr);
icculus@0
   320
    exit(1);
icculus@0
   321
} // luaFatal
icculus@0
   322
icculus@0
   323
icculus@11
   324
static void deinitLua(void)
icculus@11
   325
{
icculus@11
   326
    if (luaState != NULL)
icculus@11
   327
    {
icculus@11
   328
        lua_close(luaState);
icculus@11
   329
        luaState = NULL;
icculus@11
   330
    } // if
icculus@11
   331
} // deinitLua
icculus@11
   332
icculus@11
   333
icculus@7
   334
static int initLua(const int argc, char **argv)
icculus@0
   335
{
icculus@11
   336
    atexit(deinitLua);
icculus@11
   337
icculus@0
   338
    assert(luaState == NULL);
icculus@0
   339
    luaState = lua_newstate(luaAlloc, NULL);
icculus@0
   340
icculus@0
   341
    lua_atpanic(luaState, luaFatal);
icculus@0
   342
    assert(lua_checkstack(luaState, 20));  // Just in case.
icculus@2
   343
    luaL_openlibs(luaState);
icculus@0
   344
icculus@0
   345
    // Set up initial C functions, etc we want to expose to Lua code...
icculus@0
   346
    luaSetCFunc(luaState, decryptUsingPBKDF2, "decryptUsingPBKDF2");
icculus@0
   347
    luaSetCFunc(luaState, decryptBase64UsingKey, "decryptBase64UsingKey");
icculus@11
   348
    luaSetCFunc(luaState, makeGuiMenu, "makeGuiMenu");
icculus@11
   349
    luaSetCFunc(luaState, appendGuiMenuItem, "appendGuiMenuItem");
icculus@11
   350
    luaSetCFunc(luaState, setGuiMenuItemSubmenu, "setGuiMenuItemSubmenu");
icculus@11
   351
    luaSetCFunc(luaState, popupGuiMenu, "popupGuiMenu");
icculus@11
   352
    luaSetCFunc(luaState, giveControlToGui, "giveControlToGui");
icculus@12
   353
    luaSetCFunc(luaState, runGuiPasswordPrompt, "runGuiPasswordPrompt");
icculus@12
   354
    luaSetCFunc(luaState, copyToClipboard, "copyToClipboard");
icculus@0
   355
icculus@7
   356
    // Set up argv table...
icculus@7
   357
    lua_newtable(luaState);
icculus@7
   358
    int i;
icculus@7
   359
    for (i = 0; i < argc; i++)
icculus@7
   360
    {
icculus@7
   361
        lua_pushinteger(luaState, i+1);
icculus@7
   362
        lua_pushstring(luaState, argv[i]);
icculus@7
   363
        lua_settable(luaState, -3);
icculus@7
   364
    } // for
icculus@7
   365
    lua_setglobal(luaState, "argv");
icculus@7
   366
icculus@3
   367
    // Transfer control to Lua...
icculus@0
   368
    if (luaL_dofile(luaState, "1pass.lua") != 0)
icculus@0
   369
    {
icculus@0
   370
        const char *msg = lua_tostring(luaState, -1);
icculus@0
   371
        fprintf(stderr, "1pass.lua didn't run: %s\n", msg);
icculus@0
   372
        lua_pop(luaState, 1);
icculus@0
   373
        return 0;
icculus@0
   374
    } // if
icculus@0
   375
icculus@0
   376
    return 1;
icculus@0
   377
} // initLua
icculus@0
   378
icculus@0
   379
icculus@0
   380
int main(int argc, char **argv)
icculus@0
   381
{
icculus@11
   382
    gtk_init(&argc, &argv);
icculus@0
   383
icculus@7
   384
    if (!initLua(argc, argv))  // this will move control to 1pass.lua
icculus@0
   385
        return 1;
icculus@0
   386
icculus@0
   387
    return 0;
icculus@0
   388
} // main
icculus@0
   389
icculus@0
   390
// end of 1pass.c ...
icculus@0
   391