2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2014-12-20 19:30:34 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2014-12-20 19:30:34 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2014-12-20 19:30:34 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00:00
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
2014-12-20 19:30:34 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-02-09 20:51:12 +00:00
|
|
|
#ifdef COLOR
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <curses.h>
|
|
|
|
#include <term.h>
|
|
|
|
#endif
|
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
#include <scrypt/sha256.h>
|
|
|
|
#include <scrypt/crypto_scrypt.h>
|
|
|
|
|
|
|
|
#include "mpw-util.h"
|
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
void mpw_push_buf(uint8_t **const buffer, size_t *const bufferSize, const void *pushBuffer, const size_t pushSize) {
|
2014-12-20 19:30:34 +00:00
|
|
|
|
|
|
|
if (*bufferSize == (size_t)-1)
|
2014-12-21 17:37:21 +00:00
|
|
|
// The buffer was marked as broken, it is missing a previous push. Abort to avoid corrupt content.
|
2014-12-20 19:30:34 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
*bufferSize += pushSize;
|
2014-12-22 04:45:19 +00:00
|
|
|
uint8_t *resizedBuffer = realloc( *buffer, *bufferSize );
|
2014-12-21 17:37:21 +00:00
|
|
|
if (!resizedBuffer) {
|
|
|
|
// realloc failed, we can't push. Mark the buffer as broken.
|
|
|
|
mpw_free( *buffer, *bufferSize - pushSize );
|
2014-12-22 04:45:19 +00:00
|
|
|
*bufferSize = (size_t)-1;
|
2014-12-21 17:37:21 +00:00
|
|
|
*buffer = NULL;
|
2014-12-20 19:30:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-22 04:45:19 +00:00
|
|
|
*buffer = resizedBuffer;
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t *pushDst = *buffer + *bufferSize - pushSize;
|
|
|
|
memcpy( pushDst, pushBuffer, pushSize );
|
|
|
|
}
|
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
void mpw_push_string(uint8_t **buffer, size_t *const bufferSize, const char *pushString) {
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_push_buf( buffer, bufferSize, pushString, strlen( pushString ) );
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
void mpw_push_int(uint8_t **const buffer, size_t *const bufferSize, const uint32_t pushInt) {
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
mpw_push_buf( buffer, bufferSize, &pushInt, sizeof( pushInt ) );
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mpw_free(const void *buffer, const size_t bufferSize) {
|
|
|
|
|
2016-07-19 16:00:19 +00:00
|
|
|
if (buffer) {
|
|
|
|
memset( (void *)buffer, 0, bufferSize );
|
|
|
|
free( (void *)buffer );
|
|
|
|
}
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
void mpw_free_string(const char *string) {
|
2014-12-22 04:45:19 +00:00
|
|
|
|
|
|
|
mpw_free( string, strlen( string ) );
|
|
|
|
}
|
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t const *mpw_scrypt(const size_t keySize, const char *secret, const uint8_t *salt, const size_t saltSize,
|
|
|
|
uint64_t N, uint32_t r, uint32_t p) {
|
|
|
|
|
2016-07-06 05:16:10 +00:00
|
|
|
if (!secret || !salt)
|
|
|
|
return NULL;
|
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t *key = malloc( keySize );
|
|
|
|
if (!key)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (crypto_scrypt( (const uint8_t *)secret, strlen( secret ), salt, saltSize, N, r, p, key, keySize ) < 0) {
|
|
|
|
mpw_free( key, keySize );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t const *mpw_hmac_sha256(const uint8_t *key, const size_t keySize, const uint8_t *salt, const size_t saltSize) {
|
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
uint8_t *const buffer = malloc( 32 );
|
2014-12-20 19:30:34 +00:00
|
|
|
if (!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
HMAC_SHA256_Buf( key, keySize, salt, saltSize, buffer );
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
const char *mpw_id_buf(const void *buf, size_t length) {
|
2014-12-20 19:30:34 +00:00
|
|
|
|
|
|
|
uint8_t hash[32];
|
|
|
|
SHA256_Buf( buf, length, hash );
|
|
|
|
|
|
|
|
return mpw_hex( hash, 32 );
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
static char **mpw_hex_buf = NULL;
|
|
|
|
static unsigned int mpw_hex_buf_i = 0;
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
const char *mpw_hex(const void *buf, size_t length) {
|
2015-02-27 13:49:04 +00:00
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
// FIXME
|
2015-03-11 21:30:34 +00:00
|
|
|
if (!mpw_hex_buf) {
|
|
|
|
mpw_hex_buf = malloc( 10 * sizeof( char * ) );
|
|
|
|
for (uint8_t i = 0; i < 10; ++i)
|
|
|
|
mpw_hex_buf[i] = NULL;
|
|
|
|
}
|
|
|
|
mpw_hex_buf_i = (mpw_hex_buf_i + 1) % 10;
|
|
|
|
|
|
|
|
mpw_hex_buf[mpw_hex_buf_i] = realloc( mpw_hex_buf[mpw_hex_buf_i], length * 2 + 1 );
|
|
|
|
for (size_t kH = 0; kH < length; kH++)
|
|
|
|
sprintf( &(mpw_hex_buf[mpw_hex_buf_i][kH * 2]), "%02X", ((const uint8_t *)buf)[kH] );
|
2014-12-20 19:30:34 +00:00
|
|
|
|
2015-03-11 21:30:34 +00:00
|
|
|
return mpw_hex_buf[mpw_hex_buf_i];
|
2015-02-27 13:49:04 +00:00
|
|
|
}
|
2015-03-05 22:28:04 +00:00
|
|
|
|
2015-02-27 13:49:04 +00:00
|
|
|
const char *mpw_hex_l(uint32_t number) {
|
2015-03-05 22:28:04 +00:00
|
|
|
|
2015-02-27 13:49:04 +00:00
|
|
|
return mpw_hex( &number, sizeof( number ) );
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef COLOR
|
|
|
|
static int putvari;
|
|
|
|
static char *putvarc = NULL;
|
2016-11-10 16:15:23 +00:00
|
|
|
static int termsetup;
|
2016-11-06 16:19:52 +00:00
|
|
|
static int initputvar() {
|
|
|
|
if (!isatty(STDERR_FILENO))
|
|
|
|
return 0;
|
2014-12-20 19:30:34 +00:00
|
|
|
if (putvarc)
|
|
|
|
free(putvarc);
|
2016-11-10 16:15:23 +00:00
|
|
|
if (!termsetup) {
|
|
|
|
int status;
|
|
|
|
if (! (termsetup = (setupterm(NULL, STDERR_FILENO, &status) == OK && status == 1))) {
|
|
|
|
wrn( "Terminal doesn't support color (setupterm errno %d).\n", status );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 16:19:52 +00:00
|
|
|
|
2014-12-20 19:30:34 +00:00
|
|
|
putvarc=(char *)calloc(256, sizeof(char));
|
|
|
|
putvari=0;
|
2016-11-06 16:19:52 +00:00
|
|
|
return 1;
|
2014-12-20 19:30:34 +00:00
|
|
|
}
|
|
|
|
static int putvar(int c) {
|
|
|
|
putvarc[putvari++]=c;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char *mpw_identicon(const char *fullName, const char *masterPassword) {
|
|
|
|
|
|
|
|
const char *leftArm[] = { "╔", "╚", "╰", "═" };
|
|
|
|
const char *rightArm[] = { "╗", "╝", "╯", "═" };
|
|
|
|
const char *body[] = { "█", "░", "▒", "▓", "☺", "☻" };
|
|
|
|
const char *accessory[] = {
|
|
|
|
"◈", "◎", "◐", "◑", "◒", "◓", "☀", "☁", "☂", "☃", "☄", "★", "☆", "☎", "☏", "⎈", "⌂", "☘", "☢", "☣",
|
|
|
|
"☕", "⌚", "⌛", "⏰", "⚡", "⛄", "⛅", "☔", "♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟",
|
2015-03-05 22:28:04 +00:00
|
|
|
"♨", "♩", "♪", "♫", "⚐", "⚑", "⚔", "⚖", "⚙", "⚠", "⌘", "⏎", "✄", "✆", "✈", "✉", "✌"
|
|
|
|
};
|
2014-12-20 19:30:34 +00:00
|
|
|
|
|
|
|
uint8_t identiconSeed[32];
|
|
|
|
HMAC_SHA256_Buf( masterPassword, strlen( masterPassword ), fullName, strlen( fullName ), identiconSeed );
|
|
|
|
|
|
|
|
char *colorString, *resetString;
|
|
|
|
#ifdef COLOR
|
2016-11-06 16:19:52 +00:00
|
|
|
if (initputvar()) {
|
2014-12-20 19:30:34 +00:00
|
|
|
uint8_t colorIdentifier = (uint8_t)(identiconSeed[4] % 7 + 1);
|
|
|
|
tputs(tparm(tgetstr("AF", NULL), colorIdentifier), 1, putvar);
|
|
|
|
colorString = calloc(strlen(putvarc) + 1, sizeof(char));
|
|
|
|
strcpy(colorString, putvarc);
|
|
|
|
tputs(tgetstr("me", NULL), 1, putvar);
|
|
|
|
resetString = calloc(strlen(putvarc) + 1, sizeof(char));
|
|
|
|
strcpy(resetString, putvarc);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
colorString = calloc( 1, sizeof( char ) );
|
|
|
|
resetString = calloc( 1, sizeof( char ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
char *identicon = (char *)calloc( 256, sizeof( char ) );
|
|
|
|
snprintf( identicon, 256, "%s%s%s%s%s%s",
|
|
|
|
colorString,
|
|
|
|
leftArm[identiconSeed[0] % (sizeof( leftArm ) / sizeof( leftArm[0] ))],
|
|
|
|
body[identiconSeed[1] % (sizeof( body ) / sizeof( body[0] ))],
|
|
|
|
rightArm[identiconSeed[2] % (sizeof( rightArm ) / sizeof( rightArm[0] ))],
|
|
|
|
accessory[identiconSeed[3] % (sizeof( accessory ) / sizeof( accessory[0] ))],
|
|
|
|
resetString );
|
|
|
|
|
|
|
|
free( colorString );
|
|
|
|
free( resetString );
|
|
|
|
return identicon;
|
|
|
|
}
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2015-02-18 22:32:33 +00:00
|
|
|
/**
|
|
|
|
* @return the amount of bytes used by UTF-8 to encode a single character that starts with the given byte.
|
|
|
|
*/
|
2016-11-03 14:04:18 +00:00
|
|
|
static int mpw_utf8_sizeof(unsigned char utf8Byte) {
|
2015-02-18 22:32:33 +00:00
|
|
|
|
|
|
|
if (!utf8Byte)
|
|
|
|
return 0;
|
|
|
|
if ((utf8Byte & 0x80) == 0)
|
|
|
|
return 1;
|
|
|
|
if ((utf8Byte & 0xC0) != 0xC0)
|
|
|
|
return 0;
|
|
|
|
if ((utf8Byte & 0xE0) == 0xC0)
|
|
|
|
return 2;
|
|
|
|
if ((utf8Byte & 0xF0) == 0xE0)
|
|
|
|
return 3;
|
|
|
|
if ((utf8Byte & 0xF8) == 0xF0)
|
|
|
|
return 4;
|
2015-01-15 22:43:41 +00:00
|
|
|
|
2015-02-18 22:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-03 14:04:18 +00:00
|
|
|
const size_t mpw_utf8_strlen(const char *utf8String) {
|
2015-02-18 22:32:33 +00:00
|
|
|
|
|
|
|
size_t charlen = 0;
|
|
|
|
char *remainingString = (char *)utf8String;
|
2016-11-03 14:04:18 +00:00
|
|
|
for (int charByteSize; (charByteSize = mpw_utf8_sizeof( (unsigned char)*remainingString )); remainingString += charByteSize)
|
2015-02-18 22:32:33 +00:00
|
|
|
++charlen;
|
|
|
|
|
2015-03-05 22:28:04 +00:00
|
|
|
return charlen;
|
2015-01-15 22:43:41 +00:00
|
|
|
}
|