2017-07-16 01:13:49 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2017-07-23 01:38:53 +00:00
|
|
|
#include <string.h>
|
2017-08-06 03:19:24 +00:00
|
|
|
#include <ctype.h>
|
2017-07-28 13:50:26 +00:00
|
|
|
|
2017-07-16 01:13:49 +00:00
|
|
|
#include "mpw-marshall.h"
|
|
|
|
#include "mpw-util.h"
|
2017-07-28 13:50:26 +00:00
|
|
|
#include "mpw-marshall-util.h"
|
2017-07-23 03:58:22 +00:00
|
|
|
|
2017-07-23 01:38:53 +00:00
|
|
|
MPMarshalledUser *mpw_marshall_user(
|
2017-07-23 03:45:54 +00:00
|
|
|
const char *fullName, const char *masterPassword, const MPAlgorithmVersion algorithmVersion) {
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 20:49:55 +00:00
|
|
|
MPMarshalledUser *user;
|
|
|
|
if (!fullName || !masterPassword || !(user = malloc( sizeof( MPMarshalledUser ) )))
|
2017-07-23 01:38:53 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*user = (MPMarshalledUser){
|
2017-08-06 03:19:24 +00:00
|
|
|
.fullName = strdup( fullName ),
|
2017-07-23 03:45:54 +00:00
|
|
|
.masterPassword = strdup( masterPassword ),
|
2017-07-23 01:38:53 +00:00
|
|
|
.algorithm = algorithmVersion,
|
2017-07-23 03:45:54 +00:00
|
|
|
.redacted = true,
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
.avatar = 0,
|
2017-08-10 16:30:42 +00:00
|
|
|
.defaultType = MPResultTypeDefault,
|
2017-07-16 01:13:49 +00:00
|
|
|
.lastUsed = 0,
|
|
|
|
|
|
|
|
.sites_count = 0,
|
|
|
|
.sites = NULL,
|
|
|
|
};
|
2017-07-23 01:38:53 +00:00
|
|
|
return user;
|
2017-07-16 01:13:49 +00:00
|
|
|
};
|
|
|
|
|
2017-07-23 01:38:53 +00:00
|
|
|
MPMarshalledSite *mpw_marshall_site(
|
2017-08-10 16:30:42 +00:00
|
|
|
MPMarshalledUser *user, const char *siteName, const MPResultType resultType,
|
2017-08-08 00:27:08 +00:00
|
|
|
const MPCounterValue siteCounter, const MPAlgorithmVersion algorithmVersion) {
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-07 21:42:38 +00:00
|
|
|
if (!siteName || !mpw_realloc( &user->sites, NULL, sizeof( MPMarshalledSite ) * ++user->sites_count ))
|
2017-07-23 01:38:53 +00:00
|
|
|
return NULL;
|
|
|
|
|
2017-08-07 21:42:38 +00:00
|
|
|
MPMarshalledSite *site = &user->sites[user->sites_count - 1];
|
2017-08-05 21:33:45 +00:00
|
|
|
*site = (MPMarshalledSite){
|
2017-07-23 03:45:54 +00:00
|
|
|
.name = strdup( siteName ),
|
|
|
|
.content = NULL,
|
2017-08-10 16:30:42 +00:00
|
|
|
.type = resultType,
|
2017-07-16 01:13:49 +00:00
|
|
|
.counter = siteCounter,
|
2017-07-23 01:38:53 +00:00
|
|
|
.algorithm = algorithmVersion,
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
.loginName = NULL,
|
2017-07-23 01:38:53 +00:00
|
|
|
.loginGenerated = false,
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
.url = NULL,
|
|
|
|
.uses = 0,
|
|
|
|
.lastUsed = 0,
|
|
|
|
|
|
|
|
.questions_count = 0,
|
|
|
|
.questions = NULL,
|
|
|
|
};
|
2017-08-05 21:33:45 +00:00
|
|
|
return site;
|
2017-07-16 01:13:49 +00:00
|
|
|
};
|
|
|
|
|
2017-07-23 01:38:53 +00:00
|
|
|
MPMarshalledQuestion *mpw_marshal_question(
|
2017-08-07 21:42:38 +00:00
|
|
|
MPMarshalledSite *site, const char *keyword) {
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-07 21:42:38 +00:00
|
|
|
if (!keyword || !mpw_realloc( &site->questions, NULL, sizeof( MPMarshalledQuestion ) * ++site->questions_count ))
|
2017-07-23 01:38:53 +00:00
|
|
|
return NULL;
|
|
|
|
|
2017-08-07 21:42:38 +00:00
|
|
|
MPMarshalledQuestion *question = &site->questions[site->questions_count - 1];
|
2017-08-05 21:33:45 +00:00
|
|
|
*question = (MPMarshalledQuestion){
|
2017-07-23 03:45:54 +00:00
|
|
|
.keyword = strdup( keyword ),
|
2017-07-16 01:13:49 +00:00
|
|
|
};
|
2017-08-05 21:33:45 +00:00
|
|
|
return question;
|
2017-07-23 01:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
bool mpw_marshal_info_free(
|
|
|
|
MPMarshallInfo *info) {
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
success &= mpw_free_string( info->fullName );
|
|
|
|
success &= mpw_free_string( info->keyID );
|
|
|
|
success &= mpw_free( info, sizeof( MPMarshallInfo ) );
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-07-23 01:38:53 +00:00
|
|
|
bool mpw_marshal_free(
|
2017-08-07 21:42:38 +00:00
|
|
|
MPMarshalledUser *user) {
|
|
|
|
|
|
|
|
if (!user)
|
|
|
|
return true;
|
2017-07-23 01:38:53 +00:00
|
|
|
|
2017-07-23 03:45:54 +00:00
|
|
|
bool success = true;
|
2017-08-07 21:42:38 +00:00
|
|
|
for (size_t s = 0; s < user->sites_count; ++s) {
|
|
|
|
MPMarshalledSite *site = &user->sites[s];
|
2017-08-06 03:19:24 +00:00
|
|
|
success &= mpw_free_string( site->name );
|
|
|
|
for (size_t q = 0; q < site->questions_count; ++q) {
|
|
|
|
MPMarshalledQuestion *question = &site->questions[q];
|
|
|
|
success &= mpw_free_string( question->keyword );
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
success &= mpw_free( site->questions, sizeof( MPMarshalledQuestion ) * site->questions_count );
|
2017-07-23 01:38:53 +00:00
|
|
|
}
|
2017-08-07 21:42:38 +00:00
|
|
|
success &= mpw_free( user->sites, sizeof( MPMarshalledSite ) * user->sites_count );
|
|
|
|
success &= mpw_free_string( user->fullName );
|
|
|
|
success &= mpw_free_string( user->masterPassword );
|
|
|
|
success &= mpw_free( user, sizeof( MPMarshalledUser ) );
|
2017-07-23 01:38:53 +00:00
|
|
|
|
2017-07-23 03:45:54 +00:00
|
|
|
return success;
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
static bool mpw_marshall_write_flat(
|
2017-07-23 20:49:55 +00:00
|
|
|
char **out, const MPMarshalledUser *user, MPMarshallError *error) {
|
2017-07-23 03:45:54 +00:00
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Unexpected internal error." };
|
2017-08-06 03:19:24 +00:00
|
|
|
if (!user->fullName || !strlen( user->fullName )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorMissing, "Missing full name." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!user->masterPassword || !strlen( user->masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Missing master password." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-23 03:58:22 +00:00
|
|
|
MPMasterKey masterKey = NULL;
|
2017-07-23 20:49:55 +00:00
|
|
|
MPAlgorithmVersion masterKeyAlgorithm = user->algorithm - 1;
|
2017-08-06 03:19:24 +00:00
|
|
|
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, user->algorithm, user->fullName, user->masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." };
|
2017-07-23 03:45:54 +00:00
|
|
|
return false;
|
2017-07-23 20:49:55 +00:00
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
mpw_string_pushf( out, "# Master Password site export\n" );
|
2017-07-23 20:49:55 +00:00
|
|
|
if (user->redacted)
|
2017-08-03 05:07:19 +00:00
|
|
|
mpw_string_pushf( out, "# Export of site names and stored passwords (unless device-private) encrypted with the master key.\n" );
|
2017-08-05 21:33:45 +00:00
|
|
|
else
|
|
|
|
mpw_string_pushf( out, "# Export of site names and passwords in clear-text.\n" );
|
2017-08-03 05:07:19 +00:00
|
|
|
mpw_string_pushf( out, "# \n" );
|
|
|
|
mpw_string_pushf( out, "##\n" );
|
|
|
|
mpw_string_pushf( out, "# Format: %d\n", 1 );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 20:49:55 +00:00
|
|
|
char dateString[21];
|
2017-07-16 01:13:49 +00:00
|
|
|
time_t now = time( NULL );
|
2017-07-23 20:49:55 +00:00
|
|
|
if (strftime( dateString, sizeof( dateString ), "%FT%TZ", gmtime( &now ) ))
|
2017-08-03 05:07:19 +00:00
|
|
|
mpw_string_pushf( out, "# Date: %s\n", dateString );
|
2017-08-06 03:19:24 +00:00
|
|
|
mpw_string_pushf( out, "# User Name: %s\n", user->fullName );
|
|
|
|
mpw_string_pushf( out, "# Full Name: %s\n", user->fullName );
|
2017-08-03 05:07:19 +00:00
|
|
|
mpw_string_pushf( out, "# Avatar: %u\n", user->avatar );
|
|
|
|
mpw_string_pushf( out, "# Key ID: %s\n", mpw_id_buf( masterKey, MPMasterKeySize ) );
|
|
|
|
mpw_string_pushf( out, "# Algorithm: %d\n", user->algorithm );
|
|
|
|
mpw_string_pushf( out, "# Default Type: %d\n", user->defaultType );
|
|
|
|
mpw_string_pushf( out, "# Passwords: %s\n", user->redacted? "PROTECTED": "VISIBLE" );
|
|
|
|
mpw_string_pushf( out, "##\n" );
|
|
|
|
mpw_string_pushf( out, "#\n" );
|
|
|
|
mpw_string_pushf( out, "# Last Times Password Login\t Site\tSite\n" );
|
|
|
|
mpw_string_pushf( out, "# used used type name\t name\tpassword\n" );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
// Sites.
|
2017-07-28 13:50:26 +00:00
|
|
|
for (size_t s = 0; s < user->sites_count; ++s) {
|
2017-08-06 03:19:24 +00:00
|
|
|
MPMarshalledSite *site = &user->sites[s];
|
|
|
|
if (!site->name || !strlen( site->name ))
|
2017-07-23 20:49:55 +00:00
|
|
|
continue;
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-04 14:43:46 +00:00
|
|
|
const char *content = NULL;
|
2017-07-23 20:49:55 +00:00
|
|
|
if (!user->redacted) {
|
2017-08-05 21:33:45 +00:00
|
|
|
// Clear Text
|
2017-08-06 03:19:24 +00:00
|
|
|
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, user->fullName, user->masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." };
|
2017-07-23 03:58:22 +00:00
|
|
|
return false;
|
2017-07-23 20:49:55 +00:00
|
|
|
}
|
2017-07-23 03:45:54 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
if (site->type & MPResultTypeClassTemplate)
|
|
|
|
content = mpw_siteResult( masterKey, site->name, site->counter,
|
|
|
|
MPKeyPurposeAuthentication, NULL, site->type, site->content, site->algorithm );
|
2017-08-05 23:04:42 +00:00
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
else if (site->type & MPSiteFeatureExportContent && site->content && strlen( site->content ))
|
2017-08-05 21:33:45 +00:00
|
|
|
// Redacted
|
2017-08-06 03:19:24 +00:00
|
|
|
content = strdup( site->content );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
if (strftime( dateString, sizeof( dateString ), "%FT%TZ", gmtime( &site->lastUsed ) ))
|
2017-08-03 05:07:19 +00:00
|
|
|
mpw_string_pushf( out, "%s %8ld %lu:%lu:%lu %25s\t%25s\t%s\n",
|
2017-08-06 03:19:24 +00:00
|
|
|
dateString, (long)site->uses, (long)site->type, (long)site->algorithm, (long)site->counter,
|
|
|
|
site->loginName?: "", site->name, content?: "" );
|
2017-08-04 14:43:46 +00:00
|
|
|
mpw_free_string( content );
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
2017-07-23 03:45:54 +00:00
|
|
|
mpw_free( masterKey, MPMasterKeySize );
|
|
|
|
|
2017-08-11 01:29:59 +00:00
|
|
|
*error = (MPMarshallError){ .type = MPMarshallSuccess };
|
2017-07-16 01:13:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
static bool mpw_marshall_write_json(
|
2017-07-23 20:49:55 +00:00
|
|
|
char **out, const MPMarshalledUser *user, MPMarshallError *error) {
|
2017-07-23 03:45:54 +00:00
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Unexpected internal error." };
|
2017-08-06 03:19:24 +00:00
|
|
|
if (!user->fullName || !strlen( user->fullName )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorMissing, "Missing full name." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!user->masterPassword || !strlen( user->masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Missing master password." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-23 03:58:22 +00:00
|
|
|
MPMasterKey masterKey = NULL;
|
2017-07-23 20:49:55 +00:00
|
|
|
MPAlgorithmVersion masterKeyAlgorithm = user->algorithm - 1;
|
2017-08-06 03:19:24 +00:00
|
|
|
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, user->algorithm, user->fullName, user->masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." };
|
2017-07-23 03:45:54 +00:00
|
|
|
return false;
|
2017-07-23 20:49:55 +00:00
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
// Section: "export"
|
2017-07-23 20:49:55 +00:00
|
|
|
json_object *json_file = json_object_new_object();
|
2017-07-16 01:13:49 +00:00
|
|
|
json_object *json_export = json_object_new_object();
|
2017-07-23 13:57:48 +00:00
|
|
|
json_object_object_add( json_file, "export", json_export );
|
2017-07-16 01:13:49 +00:00
|
|
|
json_object_object_add( json_export, "format", json_object_new_int( 1 ) );
|
2017-07-23 20:49:55 +00:00
|
|
|
json_object_object_add( json_export, "redacted", json_object_new_boolean( user->redacted ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 20:49:55 +00:00
|
|
|
char dateString[21];
|
2017-07-16 01:13:49 +00:00
|
|
|
time_t now = time( NULL );
|
2017-07-23 20:49:55 +00:00
|
|
|
if (strftime( dateString, sizeof( dateString ), "%FT%TZ", gmtime( &now ) ))
|
2017-07-16 01:13:49 +00:00
|
|
|
json_object_object_add( json_export, "date", json_object_new_string( dateString ) );
|
|
|
|
|
|
|
|
// Section: "user"
|
|
|
|
json_object *json_user = json_object_new_object();
|
2017-07-23 13:57:48 +00:00
|
|
|
json_object_object_add( json_file, "user", json_user );
|
2017-07-28 13:50:26 +00:00
|
|
|
json_object_object_add( json_user, "avatar", json_object_new_int( (int)user->avatar ) );
|
2017-08-06 03:19:24 +00:00
|
|
|
json_object_object_add( json_user, "full_name", json_object_new_string( user->fullName ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 20:49:55 +00:00
|
|
|
if (strftime( dateString, sizeof( dateString ), "%FT%TZ", gmtime( &user->lastUsed ) ))
|
2017-07-16 01:13:49 +00:00
|
|
|
json_object_object_add( json_user, "last_used", json_object_new_string( dateString ) );
|
2017-07-23 03:45:54 +00:00
|
|
|
json_object_object_add( json_user, "key_id", json_object_new_string( mpw_id_buf( masterKey, MPMasterKeySize ) ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
json_object_object_add( json_user, "algorithm", json_object_new_int( (int)user->algorithm ) );
|
|
|
|
json_object_object_add( json_user, "default_type", json_object_new_int( (int)user->defaultType ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
// Section "sites"
|
|
|
|
json_object *json_sites = json_object_new_object();
|
2017-07-23 13:57:48 +00:00
|
|
|
json_object_object_add( json_file, "sites", json_sites );
|
2017-07-28 13:50:26 +00:00
|
|
|
for (size_t s = 0; s < user->sites_count; ++s) {
|
2017-08-06 03:19:24 +00:00
|
|
|
MPMarshalledSite *site = &user->sites[s];
|
|
|
|
if (!site->name || !strlen( site->name ))
|
2017-07-23 20:49:55 +00:00
|
|
|
continue;
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-04 14:43:46 +00:00
|
|
|
const char *content = NULL;
|
2017-07-23 20:49:55 +00:00
|
|
|
if (!user->redacted) {
|
2017-08-05 21:33:45 +00:00
|
|
|
// Clear Text
|
2017-08-06 03:19:24 +00:00
|
|
|
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, user->fullName, user->masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." };
|
2017-07-23 03:58:22 +00:00
|
|
|
return false;
|
2017-07-23 20:49:55 +00:00
|
|
|
}
|
2017-07-23 03:45:54 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
if (site->type & MPResultTypeClassTemplate)
|
|
|
|
content = mpw_siteResult( masterKey, site->name, site->counter,
|
|
|
|
MPKeyPurposeAuthentication, NULL, site->type, site->content, site->algorithm );
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
else if (site->type & MPSiteFeatureExportContent && site->content && strlen( site->content ))
|
2017-08-05 21:33:45 +00:00
|
|
|
// Redacted
|
2017-08-06 03:19:24 +00:00
|
|
|
content = strdup( site->content );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
json_object *json_site = json_object_new_object();
|
2017-08-06 03:19:24 +00:00
|
|
|
json_object_object_add( json_sites, site->name, json_site );
|
|
|
|
json_object_object_add( json_site, "type", json_object_new_int( (int)site->type ) );
|
|
|
|
json_object_object_add( json_site, "counter", json_object_new_int( (int)site->counter ) );
|
|
|
|
json_object_object_add( json_site, "algorithm", json_object_new_int( (int)site->algorithm ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
if (content)
|
|
|
|
json_object_object_add( json_site, "password", json_object_new_string( content ) );
|
2017-08-06 03:19:24 +00:00
|
|
|
if (site->loginName)
|
|
|
|
json_object_object_add( json_site, "login_name", json_object_new_string( site->loginName ) );
|
|
|
|
json_object_object_add( json_site, "login_generated", json_object_new_boolean( site->loginGenerated ) );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
json_object_object_add( json_site, "uses", json_object_new_int( (int)site->uses ) );
|
|
|
|
if (strftime( dateString, sizeof( dateString ), "%FT%TZ", gmtime( &site->lastUsed ) ))
|
2017-07-16 01:13:49 +00:00
|
|
|
json_object_object_add( json_site, "last_used", json_object_new_string( dateString ) );
|
|
|
|
|
|
|
|
json_object *json_site_questions = json_object_new_object();
|
|
|
|
json_object_object_add( json_site, "questions", json_site_questions );
|
2017-08-06 03:19:24 +00:00
|
|
|
for (size_t q = 0; q < site->questions_count; ++q) {
|
|
|
|
MPMarshalledQuestion *question = &site->questions[q];
|
|
|
|
if (!question->keyword)
|
2017-07-23 20:49:55 +00:00
|
|
|
continue;
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
json_object *json_site_question = json_object_new_object();
|
2017-08-06 03:19:24 +00:00
|
|
|
json_object_object_add( json_site_questions, question->keyword, json_site_question );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 20:49:55 +00:00
|
|
|
if (!user->redacted) {
|
2017-08-05 21:33:45 +00:00
|
|
|
// Clear Text
|
2017-08-10 16:30:42 +00:00
|
|
|
const char *answer = mpw_siteResult( masterKey, site->name, MPCounterValueInitial,
|
|
|
|
MPKeyPurposeRecovery, question->keyword, MPResultTypeTemplatePhrase, NULL, site->algorithm );
|
2017-07-23 20:49:55 +00:00
|
|
|
if (answer)
|
|
|
|
json_object_object_add( json_site_question, "answer", json_object_new_string( answer ) );
|
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json_object *json_site_mpw = json_object_new_object();
|
|
|
|
json_object_object_add( json_site, "_ext_mpw", json_site_mpw );
|
2017-08-06 03:19:24 +00:00
|
|
|
if (site->url)
|
|
|
|
json_object_object_add( json_site_mpw, "url", json_object_new_string( site->url ) );
|
2017-08-04 14:43:46 +00:00
|
|
|
|
|
|
|
mpw_free_string( content );
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
mpw_string_pushf( out, "%s\n", json_object_to_json_string_ext( json_file, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED ) );
|
2017-07-23 03:45:54 +00:00
|
|
|
mpw_free( masterKey, MPMasterKeySize );
|
2017-07-23 13:57:48 +00:00
|
|
|
json_object_put( json_file );
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-08-11 01:29:59 +00:00
|
|
|
*error = (MPMarshallError){ .type = MPMarshallSuccess };
|
2017-07-16 01:13:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mpw_marshall_write(
|
2017-08-07 21:42:38 +00:00
|
|
|
char **out, const MPMarshallFormat outFormat, const MPMarshalledUser *user, MPMarshallError *error) {
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
switch (outFormat) {
|
2017-08-13 01:57:47 +00:00
|
|
|
case MPMarshallFormatNone:
|
|
|
|
*error = (MPMarshallError){ .type = MPMarshallSuccess };
|
|
|
|
return false;
|
2017-07-16 01:13:49 +00:00
|
|
|
case MPMarshallFormatFlat:
|
2017-08-07 21:42:38 +00:00
|
|
|
return mpw_marshall_write_flat( out, user, error );
|
2017-07-16 01:13:49 +00:00
|
|
|
case MPMarshallFormatJSON:
|
2017-08-07 21:42:38 +00:00
|
|
|
return mpw_marshall_write_json( out, user, error );
|
2017-08-10 16:30:42 +00:00
|
|
|
default:
|
|
|
|
*error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported output format: %u", outFormat ) };
|
|
|
|
return false;
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
static void mpw_marshall_read_flat_info(
|
|
|
|
const char *in, MPMarshallInfo *info) {
|
|
|
|
|
|
|
|
info->algorithm = MPAlgorithmVersionCurrent;
|
|
|
|
|
|
|
|
// Parse import data.
|
|
|
|
bool headerStarted = false;
|
|
|
|
for (const char *endOfLine, *positionInLine = in; (endOfLine = strstr( positionInLine, "\n" )); positionInLine = endOfLine + 1) {
|
|
|
|
|
|
|
|
// Comment or header
|
|
|
|
if (*positionInLine == '#') {
|
|
|
|
++positionInLine;
|
|
|
|
|
|
|
|
if (!headerStarted) {
|
|
|
|
if (*positionInLine == '#')
|
|
|
|
// ## starts header
|
|
|
|
headerStarted = true;
|
|
|
|
// Comment before header
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*positionInLine == '#')
|
|
|
|
// ## ends header
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Header
|
|
|
|
char *headerName = mpw_get_token( &positionInLine, endOfLine, ":\n" );
|
|
|
|
char *headerValue = mpw_get_token( &positionInLine, endOfLine, "\n" );
|
|
|
|
if (!headerName || !headerValue)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp( headerName, "Algorithm" ) == 0)
|
|
|
|
info->algorithm = (MPAlgorithmVersion)atoi( headerValue );
|
|
|
|
if (strcmp( headerName, "Full Name" ) == 0 || strcmp( headerName, "User Name" ) == 0)
|
|
|
|
info->fullName = strdup( headerValue );
|
|
|
|
if (strcmp( headerName, "Key ID" ) == 0)
|
|
|
|
info->keyID = strdup( headerValue );
|
|
|
|
if (strcmp( headerName, "Passwords" ) == 0)
|
|
|
|
info->redacted = strcmp( headerValue, "VISIBLE" ) != 0;
|
|
|
|
if (strcmp( headerName, "Date" ) == 0)
|
|
|
|
info->date = mpw_mktime( headerValue );
|
|
|
|
|
|
|
|
mpw_free_string( headerName );
|
|
|
|
mpw_free_string( headerValue );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
static MPMarshalledUser *mpw_marshall_read_flat(
|
2017-08-13 01:57:47 +00:00
|
|
|
const char *in, const char *masterPassword, MPMarshallError *error) {
|
2017-07-23 20:49:55 +00:00
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Unexpected internal error." };
|
2017-08-13 01:57:47 +00:00
|
|
|
if (!in || !strlen( in )) {
|
|
|
|
error->type = MPMarshallErrorStructure;
|
|
|
|
error->description = mpw_str( "No input data." );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
|
|
|
|
// Parse import data.
|
2017-07-23 03:45:54 +00:00
|
|
|
MPMasterKey masterKey = NULL;
|
2017-07-23 01:38:53 +00:00
|
|
|
MPMarshalledUser *user = NULL;
|
2017-08-02 18:26:41 +00:00
|
|
|
unsigned int format = 0, avatar = 0;
|
|
|
|
char *fullName = NULL, *keyID = NULL;
|
|
|
|
MPAlgorithmVersion algorithm = MPAlgorithmVersionCurrent, masterKeyAlgorithm = (MPAlgorithmVersion)-1;
|
2017-08-10 16:30:42 +00:00
|
|
|
MPResultType defaultType = MPResultTypeDefault;
|
2017-07-23 01:38:53 +00:00
|
|
|
bool headerStarted = false, headerEnded = false, importRedacted = false;
|
2017-08-13 01:57:47 +00:00
|
|
|
for (const char *endOfLine, *positionInLine = in; (endOfLine = strstr( positionInLine, "\n" )); positionInLine = endOfLine + 1) {
|
2017-07-23 01:38:53 +00:00
|
|
|
|
|
|
|
// Comment or header
|
|
|
|
if (*positionInLine == '#') {
|
|
|
|
++positionInLine;
|
|
|
|
|
|
|
|
if (!headerStarted) {
|
|
|
|
if (*positionInLine == '#')
|
|
|
|
// ## starts header
|
|
|
|
headerStarted = true;
|
|
|
|
// Comment before header
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (headerEnded)
|
|
|
|
// Comment after header
|
|
|
|
continue;
|
|
|
|
if (*positionInLine == '#') {
|
|
|
|
// ## ends header
|
|
|
|
headerEnded = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header
|
|
|
|
char *headerName = mpw_get_token( &positionInLine, endOfLine, ":\n" );
|
|
|
|
char *headerValue = mpw_get_token( &positionInLine, endOfLine, "\n" );
|
2017-07-23 03:45:54 +00:00
|
|
|
if (!headerName || !headerValue) {
|
2017-08-03 05:07:19 +00:00
|
|
|
error->type = MPMarshallErrorStructure;
|
|
|
|
error->description = mpw_str( "Invalid header: %s", strndup( positionInLine, (size_t)(endOfLine - positionInLine) ) );
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
2017-07-23 01:38:53 +00:00
|
|
|
|
|
|
|
if (strcmp( headerName, "Format" ) == 0)
|
2017-08-02 18:26:41 +00:00
|
|
|
format = (unsigned int)atoi( headerValue );
|
2017-07-23 01:38:53 +00:00
|
|
|
if (strcmp( headerName, "Full Name" ) == 0 || strcmp( headerName, "User Name" ) == 0)
|
2017-08-02 18:26:41 +00:00
|
|
|
fullName = strdup( headerValue );
|
2017-07-23 01:38:53 +00:00
|
|
|
if (strcmp( headerName, "Avatar" ) == 0)
|
2017-08-02 18:26:41 +00:00
|
|
|
avatar = (unsigned int)atoi( headerValue );
|
2017-07-23 03:45:54 +00:00
|
|
|
if (strcmp( headerName, "Key ID" ) == 0)
|
2017-08-02 18:26:41 +00:00
|
|
|
keyID = strdup( headerValue );
|
2017-07-23 01:38:53 +00:00
|
|
|
if (strcmp( headerName, "Algorithm" ) == 0) {
|
2017-08-02 18:26:41 +00:00
|
|
|
int value = atoi( headerValue );
|
|
|
|
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user algorithm version: %s", headerValue ) };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
2017-07-23 03:46:53 +00:00
|
|
|
}
|
2017-08-02 18:26:41 +00:00
|
|
|
algorithm = (MPAlgorithmVersion)value;
|
|
|
|
}
|
|
|
|
if (strcmp( headerName, "Default Type" ) == 0) {
|
|
|
|
int value = atoi( headerValue );
|
2017-08-10 16:30:42 +00:00
|
|
|
if (!mpw_nameForType( (MPResultType)value )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user default type: %s", headerValue ) };
|
2017-08-02 18:26:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-10 16:30:42 +00:00
|
|
|
defaultType = (MPResultType)value;
|
2017-07-23 01:38:53 +00:00
|
|
|
}
|
|
|
|
if (strcmp( headerName, "Passwords" ) == 0)
|
|
|
|
importRedacted = strcmp( headerValue, "VISIBLE" ) != 0;
|
|
|
|
|
2017-07-23 03:45:54 +00:00
|
|
|
mpw_free_string( headerName );
|
|
|
|
mpw_free_string( headerValue );
|
2017-07-23 01:38:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!headerEnded)
|
|
|
|
continue;
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!fullName) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorMissing, "Missing header: Full Name" };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
2017-07-23 01:38:53 +00:00
|
|
|
if (positionInLine >= endOfLine)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!user) {
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, algorithm, fullName, masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-02 18:26:41 +00:00
|
|
|
if (keyID && !mpw_id_buf_equals( keyID, mpw_id_buf( masterKey, MPMasterKeySize ) )) {
|
2017-08-03 05:13:15 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Master password doesn't match key ID." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!(user = mpw_marshall_user( fullName, masterPassword, algorithm ))) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new user." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
2017-07-23 01:38:53 +00:00
|
|
|
|
2017-07-23 03:45:54 +00:00
|
|
|
user->redacted = importRedacted;
|
2017-08-02 18:26:41 +00:00
|
|
|
user->avatar = avatar;
|
|
|
|
user->defaultType = defaultType;
|
2017-07-23 01:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Site
|
2017-07-23 20:49:55 +00:00
|
|
|
char *siteLoginName = NULL, *siteName = NULL, *siteContent = NULL;
|
2017-08-02 18:26:41 +00:00
|
|
|
char *str_lastUsed = NULL, *str_uses = NULL, *str_type = NULL, *str_algorithm = NULL, *str_counter = NULL;
|
|
|
|
switch (format) {
|
2017-07-23 01:38:53 +00:00
|
|
|
case 0: {
|
2017-08-02 18:26:41 +00:00
|
|
|
str_lastUsed = mpw_get_token( &positionInLine, endOfLine, " \t\n" );
|
|
|
|
str_uses = mpw_get_token( &positionInLine, endOfLine, " \t\n" );
|
2017-07-23 01:38:53 +00:00
|
|
|
char *typeAndVersion = mpw_get_token( &positionInLine, endOfLine, " \t\n" );
|
|
|
|
if (typeAndVersion) {
|
2017-08-02 18:26:41 +00:00
|
|
|
str_type = strdup( strtok( typeAndVersion, ":" ) );
|
|
|
|
str_algorithm = strdup( strtok( NULL, "" ) );
|
2017-07-23 01:38:53 +00:00
|
|
|
mpw_free_string( typeAndVersion );
|
|
|
|
}
|
2017-08-02 18:26:41 +00:00
|
|
|
str_counter = strdup( "1" );
|
2017-07-23 20:49:55 +00:00
|
|
|
siteLoginName = NULL;
|
2017-07-23 01:38:53 +00:00
|
|
|
siteName = mpw_get_token( &positionInLine, endOfLine, "\t\n" );
|
2017-07-23 20:49:55 +00:00
|
|
|
siteContent = mpw_get_token( &positionInLine, endOfLine, "\n" );
|
2017-07-23 01:38:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
2017-08-02 18:26:41 +00:00
|
|
|
str_lastUsed = mpw_get_token( &positionInLine, endOfLine, " \t\n" );
|
|
|
|
str_uses = mpw_get_token( &positionInLine, endOfLine, " \t\n" );
|
2017-07-23 01:38:53 +00:00
|
|
|
char *typeAndVersionAndCounter = mpw_get_token( &positionInLine, endOfLine, " \t\n" );
|
|
|
|
if (typeAndVersionAndCounter) {
|
2017-08-02 18:26:41 +00:00
|
|
|
str_type = strdup( strtok( typeAndVersionAndCounter, ":" ) );
|
|
|
|
str_algorithm = strdup( strtok( NULL, ":" ) );
|
|
|
|
str_counter = strdup( strtok( NULL, "" ) );
|
2017-07-23 01:38:53 +00:00
|
|
|
mpw_free_string( typeAndVersionAndCounter );
|
|
|
|
}
|
2017-07-23 20:49:55 +00:00
|
|
|
siteLoginName = mpw_get_token( &positionInLine, endOfLine, "\t\n" );
|
2017-07-23 01:38:53 +00:00
|
|
|
siteName = mpw_get_token( &positionInLine, endOfLine, "\t\n" );
|
2017-07-23 20:49:55 +00:00
|
|
|
siteContent = mpw_get_token( &positionInLine, endOfLine, "\n" );
|
2017-07-23 01:38:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unexpected import format: %u", format ) };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
2017-07-23 01:38:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 18:26:41 +00:00
|
|
|
if (siteName && str_type && str_counter && str_algorithm && str_uses && str_lastUsed) {
|
2017-08-10 16:30:42 +00:00
|
|
|
MPResultType siteType = (MPResultType)atoi( str_type );
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!mpw_nameForType( siteType )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site type: %s: %s", siteName, str_type ) };
|
2017-08-02 18:26:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-04 13:36:03 +00:00
|
|
|
long long int value = atoll( str_counter );
|
2017-08-08 00:27:08 +00:00
|
|
|
if (value < MPCounterValueFirst || value > MPCounterValueLast) {
|
2017-08-04 13:36:03 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site counter: %s: %s", siteName, str_counter ) };
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-08 00:27:08 +00:00
|
|
|
MPCounterValue siteCounter = (MPCounterValue)value;
|
2017-08-04 13:36:03 +00:00
|
|
|
value = atoll( str_algorithm );
|
2017-08-02 18:26:41 +00:00
|
|
|
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site algorithm: %s: %s", siteName, str_algorithm ) };
|
2017-08-02 18:26:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value;
|
|
|
|
time_t siteLastUsed = mpw_mktime( str_lastUsed );
|
|
|
|
if (!siteLastUsed) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site last used: %s: %s", siteName, str_lastUsed ) };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-02 18:26:41 +00:00
|
|
|
MPMarshalledSite *site = mpw_marshall_site(
|
2017-08-04 13:36:03 +00:00
|
|
|
user, siteName, siteType, siteCounter, siteAlgorithm );
|
2017-07-23 20:49:55 +00:00
|
|
|
if (!site) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new site." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-23 13:57:48 +00:00
|
|
|
|
2017-07-23 20:49:55 +00:00
|
|
|
site->loginName = siteLoginName? strdup( siteLoginName ): NULL;
|
2017-08-02 18:26:41 +00:00
|
|
|
site->uses = (unsigned int)atoi( str_uses );
|
|
|
|
site->lastUsed = siteLastUsed;
|
2017-07-23 20:49:55 +00:00
|
|
|
if (siteContent && strlen( siteContent )) {
|
2017-08-05 21:33:45 +00:00
|
|
|
if (!user->redacted) {
|
|
|
|
// Clear Text
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, fullName, masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-23 03:45:54 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
site->content = mpw_siteState( masterKey, site->name, site->counter,
|
|
|
|
MPKeyPurposeAuthentication, NULL, site->type, siteContent, site->algorithm );
|
2017-07-23 03:58:22 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-05 21:33:45 +00:00
|
|
|
// Redacted
|
2017-07-23 20:49:55 +00:00
|
|
|
site->content = strdup( siteContent );
|
2017-07-23 03:45:54 +00:00
|
|
|
}
|
2017-07-23 01:38:53 +00:00
|
|
|
}
|
2017-08-03 05:07:19 +00:00
|
|
|
else {
|
|
|
|
error->type = MPMarshallErrorMissing;
|
|
|
|
error->description = mpw_str(
|
|
|
|
"Missing one of: lastUsed=%s, uses=%s, type=%s, version=%s, counter=%s, loginName=%s, siteName=%s",
|
2017-08-02 18:26:41 +00:00
|
|
|
str_lastUsed, str_uses, str_type, str_algorithm, str_counter, siteLoginName, siteName );
|
2017-08-03 05:07:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-23 20:49:55 +00:00
|
|
|
|
2017-08-02 18:26:41 +00:00
|
|
|
mpw_free_string( str_lastUsed );
|
|
|
|
mpw_free_string( str_uses );
|
|
|
|
mpw_free_string( str_type );
|
|
|
|
mpw_free_string( str_algorithm );
|
|
|
|
mpw_free_string( str_counter );
|
2017-07-23 20:49:55 +00:00
|
|
|
mpw_free_string( siteLoginName );
|
2017-07-23 03:45:54 +00:00
|
|
|
mpw_free_string( siteName );
|
2017-07-23 20:49:55 +00:00
|
|
|
mpw_free_string( siteContent );
|
2017-07-23 01:38:53 +00:00
|
|
|
}
|
2017-08-02 18:26:41 +00:00
|
|
|
mpw_free_string( fullName );
|
|
|
|
mpw_free_string( keyID );
|
2017-07-23 03:58:22 +00:00
|
|
|
mpw_free( masterKey, MPMasterKeySize );
|
2017-07-23 03:45:54 +00:00
|
|
|
|
2017-08-11 01:29:59 +00:00
|
|
|
*error = (MPMarshallError){ .type = MPMarshallSuccess };
|
2017-07-23 01:38:53 +00:00
|
|
|
return user;
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
static void mpw_marshall_read_json_info(
|
|
|
|
const char *in, MPMarshallInfo *info) {
|
|
|
|
|
|
|
|
// Parse JSON.
|
|
|
|
enum json_tokener_error json_error = json_tokener_success;
|
|
|
|
json_object *json_file = json_tokener_parse_verbose( in, &json_error );
|
|
|
|
if (!json_file || json_error != json_tokener_success)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Section: "export"
|
|
|
|
int64_t fileFormat = mpw_get_json_int( json_file, "export.format", 0 );
|
|
|
|
if (fileFormat < 1)
|
|
|
|
return;
|
|
|
|
info->redacted = mpw_get_json_boolean( json_file, "export.redacted", true );
|
|
|
|
info->date = mpw_mktime( mpw_get_json_string( json_file, "export.date", NULL ) );
|
|
|
|
|
|
|
|
// Section: "user"
|
|
|
|
info->algorithm = (MPAlgorithmVersion)mpw_get_json_int( json_file, "user.algorithm", MPAlgorithmVersionCurrent );
|
|
|
|
info->fullName = strdup( mpw_get_json_string( json_file, "user.full_name", NULL ) );
|
|
|
|
info->keyID = strdup( mpw_get_json_string( json_file, "user.key_id", NULL ) );
|
|
|
|
|
|
|
|
json_object_put( json_file );
|
|
|
|
}
|
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
static MPMarshalledUser *mpw_marshall_read_json(
|
2017-08-13 01:57:47 +00:00
|
|
|
const char *in, const char *masterPassword, MPMarshallError *error) {
|
2017-07-23 20:49:55 +00:00
|
|
|
|
2017-08-11 01:29:59 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Unexpected internal error." };
|
2017-08-13 01:57:47 +00:00
|
|
|
if (!in || !strlen( in )) {
|
|
|
|
error->type = MPMarshallErrorStructure;
|
|
|
|
error->description = mpw_str( "No input data." );
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 20:49:55 +00:00
|
|
|
// Parse JSON.
|
|
|
|
enum json_tokener_error json_error = json_tokener_success;
|
|
|
|
json_object *json_file = json_tokener_parse_verbose( in, &json_error );
|
2017-08-03 05:07:19 +00:00
|
|
|
if (!json_file || json_error != json_tokener_success) {
|
|
|
|
*error = (MPMarshallError){ MPMarshallErrorStructure, mpw_str( "JSON error: %s", json_tokener_error_desc( json_error ) ) };
|
2017-07-23 13:57:48 +00:00
|
|
|
return NULL;
|
2017-07-23 20:49:55 +00:00
|
|
|
}
|
2017-07-23 13:57:48 +00:00
|
|
|
|
|
|
|
// Parse import data.
|
|
|
|
MPMasterKey masterKey = NULL;
|
|
|
|
MPAlgorithmVersion masterKeyAlgorithm = (MPAlgorithmVersion)-1;
|
|
|
|
MPMarshalledUser *user = NULL;
|
|
|
|
|
|
|
|
// Section: "export"
|
2017-08-11 01:29:59 +00:00
|
|
|
int64_t fileFormat = mpw_get_json_int( json_file, "export.format", 0 );
|
2017-07-23 20:49:55 +00:00
|
|
|
if (fileFormat < 1) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported format: %u", fileFormat ) };
|
2017-07-23 13:57:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-28 13:50:26 +00:00
|
|
|
bool fileRedacted = mpw_get_json_boolean( json_file, "export.redacted", true );
|
2017-07-23 13:57:48 +00:00
|
|
|
|
|
|
|
// Section: "user"
|
2017-07-28 13:50:26 +00:00
|
|
|
unsigned int avatar = (unsigned int)mpw_get_json_int( json_file, "user.avatar", 0 );
|
|
|
|
const char *fullName = mpw_get_json_string( json_file, "user.full_name", NULL );
|
2017-08-02 18:26:41 +00:00
|
|
|
const char *str_lastUsed = mpw_get_json_string( json_file, "user.last_used", NULL );
|
2017-07-28 13:50:26 +00:00
|
|
|
const char *keyID = mpw_get_json_string( json_file, "user.key_id", NULL );
|
2017-08-11 01:29:59 +00:00
|
|
|
int64_t value = mpw_get_json_int( json_file, "user.algorithm", MPAlgorithmVersionCurrent );
|
2017-08-02 18:26:41 +00:00
|
|
|
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user algorithm version: %u", value ) };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-02 18:26:41 +00:00
|
|
|
MPAlgorithmVersion algorithm = (MPAlgorithmVersion)value;
|
2017-08-10 16:30:42 +00:00
|
|
|
MPResultType defaultType = (MPResultType)mpw_get_json_int( json_file, "user.default_type", MPResultTypeDefault );
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!mpw_nameForType( defaultType )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user default type: %u", defaultType ) };
|
2017-08-02 18:26:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
time_t lastUsed = mpw_mktime( str_lastUsed );
|
|
|
|
if (!lastUsed) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user last used: %s", str_lastUsed ) };
|
2017-08-02 18:26:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-23 20:49:55 +00:00
|
|
|
if (!fullName || !strlen( fullName )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorMissing, "Missing value for full name." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
2017-07-23 13:57:48 +00:00
|
|
|
}
|
2017-07-23 20:49:55 +00:00
|
|
|
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, algorithm, fullName, masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (keyID && !mpw_id_buf_equals( keyID, mpw_id_buf( masterKey, MPMasterKeySize ) )) {
|
2017-08-03 05:13:15 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Master password doesn't match key ID." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!(user = mpw_marshall_user( fullName, masterPassword, algorithm ))) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new user." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
2017-07-23 13:57:48 +00:00
|
|
|
}
|
2017-07-23 20:49:55 +00:00
|
|
|
user->redacted = fileRedacted;
|
|
|
|
user->avatar = avatar;
|
|
|
|
user->defaultType = defaultType;
|
2017-08-02 18:26:41 +00:00
|
|
|
user->lastUsed = lastUsed;
|
2017-07-23 13:57:48 +00:00
|
|
|
|
|
|
|
// Section "sites"
|
|
|
|
json_object_iter json_site;
|
2017-07-28 13:50:26 +00:00
|
|
|
json_object *json_sites = mpw_get_json_section( json_file, "sites" );
|
2017-07-23 13:57:48 +00:00
|
|
|
json_object_object_foreachC( json_sites, json_site ) {
|
2017-08-03 05:07:19 +00:00
|
|
|
const char *siteName = json_site.key;
|
2017-08-11 01:29:59 +00:00
|
|
|
value = mpw_get_json_int( json_site.val, "algorithm", (int32_t)user->algorithm );
|
2017-08-02 18:26:41 +00:00
|
|
|
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
|
2017-08-04 13:36:03 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site algorithm version: %s: %d", siteName, value ) };
|
2017-08-02 18:26:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value;
|
2017-08-11 01:29:59 +00:00
|
|
|
MPResultType siteType = (MPResultType)mpw_get_json_int( json_site.val, "type", (int32_t)user->defaultType );
|
2017-08-02 18:26:41 +00:00
|
|
|
if (!mpw_nameForType( siteType )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site type: %s: %u", siteName, siteType ) };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-04 13:36:03 +00:00
|
|
|
value = mpw_get_json_int( json_site.val, "counter", 1 );
|
2017-08-08 00:27:08 +00:00
|
|
|
if (value < MPCounterValueFirst || value > MPCounterValueLast) {
|
2017-08-04 13:36:03 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site counter: %s: %d", siteName, value ) };
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-08 00:27:08 +00:00
|
|
|
MPCounterValue siteCounter = (MPCounterValue)value;
|
2017-07-28 13:50:26 +00:00
|
|
|
const char *siteContent = mpw_get_json_string( json_site.val, "password", NULL );
|
|
|
|
const char *siteLoginName = mpw_get_json_string( json_site.val, "login_name", NULL );
|
|
|
|
bool siteLoginGenerated = mpw_get_json_boolean( json_site.val, "login_generated", false );
|
|
|
|
unsigned int siteUses = (unsigned int)mpw_get_json_int( json_site.val, "uses", 0 );
|
2017-08-02 18:26:41 +00:00
|
|
|
str_lastUsed = mpw_get_json_string( json_site.val, "last_used", NULL );
|
|
|
|
time_t siteLastUsed = mpw_mktime( str_lastUsed );
|
|
|
|
if (!siteLastUsed) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site last used: %s: %s", siteName, str_lastUsed ) };
|
2017-08-02 18:26:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-23 13:57:48 +00:00
|
|
|
|
2017-07-28 13:50:26 +00:00
|
|
|
json_object *json_site_mpw = mpw_get_json_section( json_site.val, "_ext_mpw" );
|
|
|
|
const char *siteURL = mpw_get_json_string( json_site_mpw, "url", NULL );
|
2017-07-23 13:57:48 +00:00
|
|
|
|
2017-08-03 05:07:19 +00:00
|
|
|
MPMarshalledSite *site = mpw_marshall_site( user, siteName, siteType, siteCounter, siteAlgorithm );
|
2017-07-23 20:49:55 +00:00
|
|
|
if (!site) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new site." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-23 13:57:48 +00:00
|
|
|
|
2017-07-23 20:49:55 +00:00
|
|
|
site->loginName = siteLoginName? strdup( siteLoginName ): NULL;
|
|
|
|
site->loginGenerated = siteLoginGenerated;
|
|
|
|
site->url = siteURL? strdup( siteURL ): NULL;
|
|
|
|
site->uses = siteUses;
|
2017-08-02 18:26:41 +00:00
|
|
|
site->lastUsed = siteLastUsed;
|
2017-07-23 20:49:55 +00:00
|
|
|
if (siteContent && strlen( siteContent )) {
|
2017-08-05 21:33:45 +00:00
|
|
|
if (!user->redacted) {
|
|
|
|
// Clear Text
|
2017-07-23 20:49:55 +00:00
|
|
|
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, fullName, masterPassword )) {
|
2017-08-03 05:07:19 +00:00
|
|
|
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." };
|
2017-07-23 20:49:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-23 13:57:48 +00:00
|
|
|
|
2017-08-10 16:30:42 +00:00
|
|
|
site->content = mpw_siteState( masterKey, site->name, site->counter,
|
|
|
|
MPKeyPurposeAuthentication, NULL, site->type, siteContent, site->algorithm );
|
2017-07-23 13:57:48 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-05 21:33:45 +00:00
|
|
|
// Redacted
|
2017-07-23 20:49:55 +00:00
|
|
|
site->content = strdup( siteContent );
|
2017-07-23 13:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
json_object_iter json_site_question;
|
2017-07-28 13:50:26 +00:00
|
|
|
json_object *json_site_questions = mpw_get_json_section( json_site.val, "questions" );
|
2017-07-23 13:57:48 +00:00
|
|
|
json_object_object_foreachC( json_site_questions, json_site_question )
|
|
|
|
mpw_marshal_question( site, json_site_question.key );
|
|
|
|
}
|
|
|
|
json_object_put( json_file );
|
|
|
|
|
2017-08-11 01:29:59 +00:00
|
|
|
*error = (MPMarshallError){ .type = MPMarshallSuccess };
|
2017-07-23 13:57:48 +00:00
|
|
|
return user;
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
MPMarshallInfo *mpw_marshall_read_info(
|
|
|
|
const char *in) {
|
|
|
|
|
|
|
|
MPMarshallInfo *info = malloc( sizeof( MPMarshallInfo ) );
|
|
|
|
*info = (MPMarshallInfo){ .format = MPMarshallFormatNone };
|
|
|
|
|
|
|
|
if (in && strlen( in )) {
|
|
|
|
if (in[0] == '#') {
|
|
|
|
*info = (MPMarshallInfo){ .format = MPMarshallFormatFlat };
|
|
|
|
mpw_marshall_read_flat_info( in, info );
|
|
|
|
}
|
|
|
|
else if (in[0] == '{') {
|
|
|
|
*info = (MPMarshallInfo){ .format = MPMarshallFormatJSON };
|
|
|
|
mpw_marshall_read_json_info( in, info );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-07-23 01:38:53 +00:00
|
|
|
MPMarshalledUser *mpw_marshall_read(
|
2017-08-13 01:57:47 +00:00
|
|
|
const char *in, const MPMarshallFormat inFormat, const char *masterPassword, MPMarshallError *error) {
|
2017-07-16 01:13:49 +00:00
|
|
|
|
2017-07-23 03:45:54 +00:00
|
|
|
switch (inFormat) {
|
2017-08-13 01:57:47 +00:00
|
|
|
case MPMarshallFormatNone:
|
|
|
|
*error = (MPMarshallError){ .type = MPMarshallSuccess };
|
|
|
|
return false;
|
2017-07-16 01:13:49 +00:00
|
|
|
case MPMarshallFormatFlat:
|
2017-07-23 20:49:55 +00:00
|
|
|
return mpw_marshall_read_flat( in, masterPassword, error );
|
2017-07-16 01:13:49 +00:00
|
|
|
case MPMarshallFormatJSON:
|
2017-07-23 20:49:55 +00:00
|
|
|
return mpw_marshall_read_json( in, masterPassword, error );
|
2017-08-10 16:30:42 +00:00
|
|
|
default:
|
|
|
|
*error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported input format: %u", inFormat ) };
|
|
|
|
return NULL;
|
2017-07-16 01:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-06 03:19:24 +00:00
|
|
|
|
|
|
|
const MPMarshallFormat mpw_formatWithName(
|
|
|
|
const char *formatName) {
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
if (!formatName || !strlen( formatName ))
|
|
|
|
return MPMarshallFormatNone;
|
|
|
|
|
2017-08-06 03:19:24 +00:00
|
|
|
// Lower-case to standardize it.
|
|
|
|
size_t stdFormatNameSize = strlen( formatName );
|
|
|
|
char stdFormatName[stdFormatNameSize + 1];
|
|
|
|
for (size_t c = 0; c < stdFormatNameSize; ++c)
|
|
|
|
stdFormatName[c] = (char)tolower( formatName[c] );
|
|
|
|
stdFormatName[stdFormatNameSize] = '\0';
|
|
|
|
|
2017-08-13 01:57:47 +00:00
|
|
|
if (strncmp( mpw_nameForFormat( MPMarshallFormatNone ), stdFormatName, strlen( stdFormatName ) ) == 0)
|
|
|
|
return MPMarshallFormatNone;
|
2017-08-06 03:19:24 +00:00
|
|
|
if (strncmp( mpw_nameForFormat( MPMarshallFormatFlat ), stdFormatName, strlen( stdFormatName ) ) == 0)
|
|
|
|
return MPMarshallFormatFlat;
|
|
|
|
if (strncmp( mpw_nameForFormat( MPMarshallFormatJSON ), stdFormatName, strlen( stdFormatName ) ) == 0)
|
|
|
|
return MPMarshallFormatJSON;
|
|
|
|
|
|
|
|
dbg( "Not a format name: %s\n", stdFormatName );
|
|
|
|
return (MPMarshallFormat)ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *mpw_nameForFormat(
|
|
|
|
const MPMarshallFormat format) {
|
|
|
|
|
|
|
|
switch (format) {
|
2017-08-13 01:57:47 +00:00
|
|
|
case MPMarshallFormatNone:
|
|
|
|
return "none";
|
2017-08-06 03:19:24 +00:00
|
|
|
case MPMarshallFormatFlat:
|
|
|
|
return "flat";
|
|
|
|
case MPMarshallFormatJSON:
|
|
|
|
return "json";
|
|
|
|
default: {
|
|
|
|
dbg( "Unknown format: %d\n", format );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *mpw_marshall_format_extension(
|
|
|
|
const MPMarshallFormat format) {
|
|
|
|
|
|
|
|
switch (format) {
|
2017-08-13 01:57:47 +00:00
|
|
|
case MPMarshallFormatNone:
|
|
|
|
return NULL;
|
2017-08-06 03:19:24 +00:00
|
|
|
case MPMarshallFormatFlat:
|
|
|
|
return "mpsites";
|
|
|
|
case MPMarshallFormatJSON:
|
|
|
|
return "mpsites.json";
|
|
|
|
default: {
|
|
|
|
dbg( "Unknown format: %d\n", format );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|