dav/pwd.c

Fri, 12 Jul 2019 16:59:08 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Jul 2019 16:59:08 +0200
changeset 608
3e4c0285a868
parent 515
2465dd550bb5
child 653
f503c272bd70
permissions
-rw-r--r--

fix build on windows

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2018 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>

#include "pwd.h"

#include <ucx/buffer.h>
#include <ucx/utils.h>

#ifdef _WIN32
#include <winsock.h>
#else
#include <netinet/in.h>
#endif

PwdStore* pwdstore_open(const char *file) {
    FILE *in = fopen(file, "r");
    if(!in) {
        return NULL;
    }
    
    UcxBuffer *buf = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
    ucx_stream_copy(in, buf, (read_func)fread, (write_func)ucx_buffer_write);
    fclose(in);
    
    if(buf->size < PWDS_HEADER_SIZE || buf->space[0] != PWDS_MAGIC_CHAR) {
        ucx_buffer_free(buf);
        return NULL;
    }
    
    PwdStore *p = malloc(sizeof(PwdStore));
    p->ids = ucx_map_new(16);
    p->locations = NULL;
    p->noloc = NULL;
    p->index = ucx_map_new(16);
    p->content = buf;
    p->key = NULL;
    p->encoffset = PWDS_HEADER_SIZE;
    p->isdecrypted = 0;
    
    if(pwdstore_getindex(p)) {
        pwdstore_free(p);
        return NULL;
    }
    
    return p;
}

PwdStore* pwdstore_new(void) {
    PwdStore *p = calloc(1, sizeof(PwdStore));
    p->ids = ucx_map_new(16);
    p->locations = NULL;
    p->noloc = NULL;
    p->index = ucx_map_new(16);
    p->content = ucx_buffer_new(NULL, PWDS_HEADER_SIZE, UCX_BUFFER_AUTOEXTEND);
    PWDS_MAGIC(p) = PWDS_MAGIC_CHAR;
    PWDS_VERSION(p) = 1;
    PWDS_ENC(p) = DAV_KEY_AES256;
    PWDS_PWFUNC(p) = DAV_PWFUNC_PBKDF2_SHA256;
    dav_rand_bytes((unsigned char*)p->content->space+4, 16);
    p->isdecrypted = 1;
    p->encoffset = PWDS_HEADER_SIZE;
    return p;
}

static int readval(UcxBuffer *in, char **val, int allowzero) {
    *val = NULL;
    uint32_t length = 0;
    if(ucx_buffer_read(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) {
        return 0;
    }
    length = ntohl(length);
    if(length == 0) {
        if(allowzero) {
            return 1;
        } else {
            return 0;
        }
    }
    if(length > PWDSTORE_MAX_LEN) {
        return 0;
    }
    
    char *value = malloc(length + 1);
    value[length] = 0;
    if(ucx_buffer_read(value, 1, length, in) != length) {
        free(value);
        return 0;
    }
    
    *val = value;
    return 1;
}

static int read_indexentry(PwdStore *p, UcxBuffer *in) {
    int type = ucx_buffer_getc(in);
    if(type == EOF || type != 0) {
        // only type 0 supported yet
        return 0;
    }
    
    char *id = NULL;
    UcxList *locations = NULL;
    
    int ret = 0;
    if(readval(in, &id, FALSE)) {
        ret = 1;
        char *location = NULL;
        while((ret = readval(in, &location, TRUE)) == 1) {
            if(!location) {
                break;
            }
            locations = ucx_list_append(locations, location);
        }
    }
    
    if(ret) {
        pwdstore_put_index(p, id, locations);
    } else {
        if(id) free(id);
        ucx_list_free_content(locations, free);
    }
    
    return ret;
}

static int read_pwdentry(PwdStore *p, UcxBuffer *in) {
    int type = ucx_buffer_getc(in);
    if(type == EOF || type != 0) {
        // only type 0 supported yet
        return 0;
    }
    
    char *id = NULL;
    char *location = NULL;
    char *user = NULL;
    char *password = NULL;
    
    int ret = 0;
    if(readval(in, &id, FALSE)) {
        if(readval(in, &user, FALSE)) {
            if(readval(in, &password, FALSE)) {
                pwdstore_put(p, id, user, password);
                ret = 1;
            }
        }
    }
    
    if(id) free(id);
    if(location) free(location);
    if(user) free(user);
    if(password) free(password);
    
    return ret;
}

int pwdstore_getindex(PwdStore *s) {
    uint32_t netindexlen;
    s->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
    if(ucx_buffer_read(&netindexlen, 1, sizeof(uint32_t), s->content) != sizeof(uint32_t)) {
        return 1;
    }
    uint32_t indexlen = ntohl(netindexlen);
    if(UINT32_MAX - PWDS_HEADER_SIZE < indexlen) {
        return 1;
    }
    if(s->content->size < PWDS_HEADER_SIZE + indexlen) {
        return 1;
    }
    s->encoffset += indexlen;
    
    UcxBuffer *index = ucx_buffer_new(s->content->space+PWDS_HEADER_SIZE, indexlen, 0);
    index->size = indexlen;
    while(read_indexentry(s, index)) {}
    
    ucx_buffer_free(index);
    
    return 0;
}

int pwdstore_decrypt(PwdStore *p) {
    if(!p->key) {
        return 1;
    }
    if(p->isdecrypted) {
        return 0;
    }
    
    // decrypt contet
    size_t encsz = p->content->size - p->encoffset;
    UcxBuffer *enc = ucx_buffer_new(p->content->space + p->encoffset, encsz, 0);
    enc->size = encsz;
    enc->size = p->content->size - p->encoffset;
    UcxBuffer *content = aes_decrypt_buffer(enc, p->key);
    ucx_buffer_free(enc);
    if(!content) {
        return 1;
    }
    
    while(read_pwdentry(p, content)) {}
    
    ucx_buffer_free(content);
    
    return 0;
}

int pwdstore_setpassword(PwdStore *p, const char *password) {
    DavKey *key = dav_pw2key(
            password,
            (unsigned char*)(p->content->space + 4),
            16,
            PWDS_PWFUNC(p),
            PWDS_ENC(p));
    if(!key) {
        return 1;
    }
    
    p->key = key;
    return 0;
}

void pwdstore_encsettings(PwdStore *p, uint8_t enc, uint8_t pwfunc) {
    PWDS_ENC(p) = enc;
    PWDS_PWFUNC(p) = pwfunc;
}

void pwdstore_free_entry(PwdEntry *e) {
    if(e->id) free(e->id);
    if(e->user) free(e->user);
    if(e->password) free(e->password);
    free(e);
}

void pwdstore_free(PwdStore* p) {
    ucx_map_free_content(p->ids, (ucx_destructor)pwdstore_free_entry);
    ucx_map_free(p->ids);
    
    ucx_list_free(p->locations);
    
    if(p->content) {
        ucx_buffer_free(p->content);
    }
    
    free(p);
}

int pwdstore_has_id(PwdStore *s, const char *id) {
    return ucx_map_cstr_get(s->index, id) ? 1 : 0;
}

PwdEntry* pwdstore_get(PwdStore *p, const char *id) {
    PwdEntry *e = ucx_map_cstr_get(p->ids, id);
    if(e && e->user && e->password) {
        return e;
    } else {
        return NULL;
    }
}

void pwdstore_put(PwdStore *p, const char *id, const char *username, const char *password) {
    PwdEntry *entry = malloc(sizeof(PwdEntry));
    entry->id = strdup(id);
    entry->user = strdup(username);
    entry->password = strdup(password);
    ucx_map_cstr_put(p->ids, id, entry);
}

void pwdstore_put_index(PwdStore *p, char *id, UcxList *locations) {
    PwdIndexEntry *e = ucx_map_cstr_get(p->index, id);
    if(e) {
        return;
    }
    PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
    newentry->id = id;
    if(locations) {
        newentry->locations = locations;
        p->locations = ucx_list_append(p->locations, newentry);
    } else {
        newentry->locations = NULL;
        p->noloc = ucx_list_append(p->noloc, newentry);
    }
    ucx_map_cstr_put(p->index, id, newentry);
}

void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) {
    uint32_t idlen = strlen(e->id);
    uint32_t netidlen = htonl(idlen);
    
    ucx_buffer_putc(out, 0); // type

    ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out);
    ucx_buffer_write(e->id, 1, idlen, out);
    
    UCX_FOREACH(elm, e->locations) {
        char *location = elm->data;
        uint32_t locationlen = strlen(location);
        uint32_t netlocationlen = htonl(locationlen);
        
        ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out);
        ucx_buffer_write(location, 1, locationlen, out);
    }
    
    uint32_t terminate = 0;
    ucx_buffer_write(&terminate, 1, sizeof(uint32_t), out);
}

int pwdstore_store(PwdStore *p, const char *file) {
    if(!p->key) {
        return 1;
    }
    
    UcxBuffer *index = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
    UcxBuffer *content = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
    
    // create index
    UCX_FOREACH(elm, p->noloc) {
        PwdIndexEntry *e = elm->data;
        write_index_entry(index, e);
    }
    UCX_FOREACH(elm, p->locations) {
        PwdIndexEntry *e = elm->data;
        write_index_entry(index, e);
    }
    
    UcxMapIterator i = ucx_map_iterator(p->ids);
    PwdEntry *value;
    UCX_MAP_FOREACH(key, value, i) {
        if(!value->id || !value->user || !value->password) {
            continue;
        }
        
        uint32_t idlen = strlen(value->id);
        uint32_t ulen = strlen(value->user);
        uint32_t plen = strlen(value->password);
        uint32_t netidlen = htonl(idlen);
        uint32_t netulen = htonl(ulen);
        uint32_t netplen = htonl(plen);
        
        // content buffer
        ucx_buffer_putc(content, 0); // type
        
        ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), content);
        ucx_buffer_write(value->id, 1, idlen, content);
        ucx_buffer_write(&netulen, 1, sizeof(uint32_t), content);
        ucx_buffer_write(value->user, 1, ulen, content);
        ucx_buffer_write(&netplen, 1, sizeof(uint32_t), content);
        ucx_buffer_write(value->password, 1, plen, content);
    }
      
    content->pos = 0;
    UcxBuffer *enc = aes_encrypt_buffer(content, p->key);

    p->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
    p->content->size = PWDS_HEADER_SIZE;
    
    // add index after header
    uint32_t netindexlen = htonl((uint32_t)index->size);
    ucx_buffer_write(&netindexlen, 1, sizeof(uint32_t), p->content);
    ucx_buffer_write(index->space, 1, index->size, p->content);
    
    // add encrypted buffer
    ucx_buffer_write(enc->space, 1, enc->size, p->content);
    
    ucx_buffer_free(enc);
    
    FILE *out = fopen(file, "w");
    if(!out) {
        return 1;
    }
    fwrite(p->content->space, 1, p->content->size, out);
    fclose(out);
    
    return 0;
}

mercurial