2
0

Marshal has only one l.

This commit is contained in:
Maarten Billemont 2017-09-20 10:48:04 -04:00
parent ad00ceb4ce
commit 47164c7a92
9 changed files with 159 additions and 153 deletions

View File

@ -94,14 +94,14 @@ MPMarshalledQuestion *mpw_marshal_question(
} }
bool mpw_marshal_info_free( bool mpw_marshal_info_free(
MPMarshallInfo **info) { MPMarshalInfo **info) {
if (!info || !*info) if (!info || !*info)
return true; return true;
bool success = true; bool success = true;
success &= mpw_free_strings( &(*info)->fullName, &(*info)->keyID, NULL ); success &= mpw_free_strings( &(*info)->fullName, &(*info)->keyID, NULL );
success &= mpw_free( info, sizeof( MPMarshallInfo ) ); success &= mpw_free( info, sizeof( MPMarshalInfo ) );
return success; return success;
} }
@ -133,21 +133,21 @@ bool mpw_marshal_free(
} }
static bool mpw_marshall_write_flat( static bool mpw_marshall_write_flat(
char **out, const MPMarshalledUser *user, MPMarshallError *error) { char **out, const MPMarshalledUser *user, MPMarshalError *error) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Unexpected internal error." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Unexpected internal error." };
if (!user->fullName || !strlen( user->fullName )) { if (!user->fullName || !strlen( user->fullName )) {
*error = (MPMarshallError){ MPMarshallErrorMissing, "Missing full name." }; *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing full name." };
return false; return false;
} }
if (!user->masterPassword || !strlen( user->masterPassword )) { if (!user->masterPassword || !strlen( user->masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Missing master password." }; *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Missing master password." };
return false; return false;
} }
MPMasterKey masterKey = NULL; MPMasterKey masterKey = NULL;
MPAlgorithmVersion masterKeyAlgorithm = user->algorithm - 1; MPAlgorithmVersion masterKeyAlgorithm = user->algorithm - 1;
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, user->algorithm, user->fullName, user->masterPassword )) { if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, user->algorithm, user->fullName, user->masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return false; return false;
} }
@ -186,7 +186,7 @@ static bool mpw_marshall_write_flat(
if (!user->redacted) { if (!user->redacted) {
// Clear Text // Clear Text
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, user->fullName, user->masterPassword )) { if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, user->fullName, user->masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return false; return false;
} }
@ -211,27 +211,27 @@ static bool mpw_marshall_write_flat(
} }
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
*error = (MPMarshallError){ .type = MPMarshallSuccess }; *error = (MPMarshalError){ .type = MPMarshalSuccess };
return true; return true;
} }
#if MPW_JSON #if MPW_JSON
static bool mpw_marshall_write_json( static bool mpw_marshall_write_json(
char **out, const MPMarshalledUser *user, MPMarshallError *error) { char **out, const MPMarshalledUser *user, MPMarshalError *error) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Unexpected internal error." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Unexpected internal error." };
if (!user->fullName || !strlen( user->fullName )) { if (!user->fullName || !strlen( user->fullName )) {
*error = (MPMarshallError){ MPMarshallErrorMissing, "Missing full name." }; *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing full name." };
return false; return false;
} }
if (!user->masterPassword || !strlen( user->masterPassword )) { if (!user->masterPassword || !strlen( user->masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Missing master password." }; *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Missing master password." };
return false; return false;
} }
MPMasterKey masterKey = NULL; MPMasterKey masterKey = NULL;
MPAlgorithmVersion masterKeyAlgorithm = user->algorithm - 1; MPAlgorithmVersion masterKeyAlgorithm = user->algorithm - 1;
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, user->algorithm, user->fullName, user->masterPassword )) { if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, user->algorithm, user->fullName, user->masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return false; return false;
} }
@ -272,7 +272,7 @@ static bool mpw_marshall_write_json(
if (!user->redacted) { if (!user->redacted) {
// Clear Text // Clear Text
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, user->fullName, user->masterPassword )) { if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, user->fullName, user->masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return false; return false;
} }
@ -340,32 +340,32 @@ static bool mpw_marshall_write_json(
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
json_object_put( json_file ); json_object_put( json_file );
*error = (MPMarshallError){ .type = MPMarshallSuccess }; *error = (MPMarshalError){ .type = MPMarshalSuccess };
return true; return true;
} }
#endif #endif
bool mpw_marshall_write( bool mpw_marshall_write(
char **out, const MPMarshallFormat outFormat, const MPMarshalledUser *user, MPMarshallError *error) { char **out, const MPMarshalFormat outFormat, const MPMarshalledUser *user, MPMarshalError *error) {
switch (outFormat) { switch (outFormat) {
case MPMarshallFormatNone: case MPMarshalFormatNone:
*error = (MPMarshallError){ .type = MPMarshallSuccess }; *error = (MPMarshalError){ .type = MPMarshalSuccess };
return false; return false;
case MPMarshallFormatFlat: case MPMarshalFormatFlat:
return mpw_marshall_write_flat( out, user, error ); return mpw_marshall_write_flat( out, user, error );
#if MPW_JSON #if MPW_JSON
case MPMarshallFormatJSON: case MPMarshalFormatJSON:
return mpw_marshall_write_json( out, user, error ); return mpw_marshall_write_json( out, user, error );
#endif #endif
default: default:
*error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported output format: %u", outFormat ) }; *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unsupported output format: %u", outFormat ) };
return false; return false;
} }
} }
static void mpw_marshall_read_flat_info( static void mpw_marshall_read_flat_info(
const char *in, MPMarshallInfo *info) { const char *in, MPMarshalInfo *info) {
info->algorithm = MPAlgorithmVersionCurrent; info->algorithm = MPAlgorithmVersionCurrent;
@ -412,11 +412,11 @@ static void mpw_marshall_read_flat_info(
} }
static MPMarshalledUser *mpw_marshall_read_flat( static MPMarshalledUser *mpw_marshall_read_flat(
const char *in, const char *masterPassword, MPMarshallError *error) { const char *in, const char *masterPassword, MPMarshalError *error) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Unexpected internal error." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Unexpected internal error." };
if (!in || !strlen( in )) { if (!in || !strlen( in )) {
error->type = MPMarshallErrorStructure; error->type = MPMarshalErrorStructure;
error->description = mpw_str( "No input data." ); error->description = mpw_str( "No input data." );
return NULL; return NULL;
} }
@ -455,7 +455,7 @@ static MPMarshalledUser *mpw_marshall_read_flat(
char *headerName = mpw_get_token( &positionInLine, endOfLine, ":\n" ); char *headerName = mpw_get_token( &positionInLine, endOfLine, ":\n" );
char *headerValue = mpw_get_token( &positionInLine, endOfLine, "\n" ); char *headerValue = mpw_get_token( &positionInLine, endOfLine, "\n" );
if (!headerName || !headerValue) { if (!headerName || !headerValue) {
error->type = MPMarshallErrorStructure; error->type = MPMarshalErrorStructure;
error->description = mpw_str( "Invalid header: %s", strndup( positionInLine, (size_t)(endOfLine - positionInLine) ) ); error->description = mpw_str( "Invalid header: %s", strndup( positionInLine, (size_t)(endOfLine - positionInLine) ) );
return NULL; return NULL;
} }
@ -471,7 +471,7 @@ static MPMarshalledUser *mpw_marshall_read_flat(
if (strcmp( headerName, "Algorithm" ) == 0) { if (strcmp( headerName, "Algorithm" ) == 0) {
int value = atoi( headerValue ); int value = atoi( headerValue );
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) { if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user algorithm version: %s", headerValue ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user algorithm version: %s", headerValue ) };
return NULL; return NULL;
} }
algorithm = (MPAlgorithmVersion)value; algorithm = (MPAlgorithmVersion)value;
@ -479,7 +479,7 @@ static MPMarshalledUser *mpw_marshall_read_flat(
if (strcmp( headerName, "Default Type" ) == 0) { if (strcmp( headerName, "Default Type" ) == 0) {
int value = atoi( headerValue ); int value = atoi( headerValue );
if (!mpw_nameForType( (MPResultType)value )) { if (!mpw_nameForType( (MPResultType)value )) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user default type: %s", headerValue ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user default type: %s", headerValue ) };
return NULL; return NULL;
} }
defaultType = (MPResultType)value; defaultType = (MPResultType)value;
@ -493,7 +493,7 @@ static MPMarshalledUser *mpw_marshall_read_flat(
if (!headerEnded) if (!headerEnded)
continue; continue;
if (!fullName) { if (!fullName) {
*error = (MPMarshallError){ MPMarshallErrorMissing, "Missing header: Full Name" }; *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing header: Full Name" };
return NULL; return NULL;
} }
if (positionInLine >= endOfLine) if (positionInLine >= endOfLine)
@ -501,15 +501,15 @@ static MPMarshalledUser *mpw_marshall_read_flat(
if (!user) { if (!user) {
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, algorithm, fullName, masterPassword )) { if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, algorithm, fullName, masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return NULL; return NULL;
} }
if (keyID && !mpw_id_buf_equals( keyID, mpw_id_buf( masterKey, MPMasterKeySize ) )) { if (keyID && !mpw_id_buf_equals( keyID, mpw_id_buf( masterKey, MPMasterKeySize ) )) {
*error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Master password doesn't match key ID." }; *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Master password doesn't match key ID." };
return NULL; return NULL;
} }
if (!(user = mpw_marshall_user( fullName, masterPassword, algorithm ))) { if (!(user = mpw_marshall_user( fullName, masterPassword, algorithm ))) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new user." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new user." };
return NULL; return NULL;
} }
@ -553,7 +553,7 @@ static MPMarshalledUser *mpw_marshall_read_flat(
break; break;
} }
default: { default: {
*error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unexpected import format: %u", format ) }; *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unexpected import format: %u", format ) };
return NULL; return NULL;
} }
} }
@ -561,31 +561,31 @@ static MPMarshalledUser *mpw_marshall_read_flat(
if (siteName && str_type && str_counter && str_algorithm && str_uses && str_lastUsed) { if (siteName && str_type && str_counter && str_algorithm && str_uses && str_lastUsed) {
MPResultType siteType = (MPResultType)atoi( str_type ); MPResultType siteType = (MPResultType)atoi( str_type );
if (!mpw_nameForType( siteType )) { if (!mpw_nameForType( siteType )) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site type: %s: %s", siteName, str_type ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site type: %s: %s", siteName, str_type ) };
return NULL; return NULL;
} }
long long int value = atoll( str_counter ); long long int value = atoll( str_counter );
if (value < MPCounterValueFirst || value > MPCounterValueLast) { if (value < MPCounterValueFirst || value > MPCounterValueLast) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site counter: %s: %s", siteName, str_counter ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site counter: %s: %s", siteName, str_counter ) };
return NULL; return NULL;
} }
MPCounterValue siteCounter = (MPCounterValue)value; MPCounterValue siteCounter = (MPCounterValue)value;
value = atoll( str_algorithm ); value = atoll( str_algorithm );
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) { if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site algorithm: %s: %s", siteName, str_algorithm ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site algorithm: %s: %s", siteName, str_algorithm ) };
return NULL; return NULL;
} }
MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value; MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value;
time_t siteLastUsed = mpw_mktime( str_lastUsed ); time_t siteLastUsed = mpw_mktime( str_lastUsed );
if (!siteLastUsed) { if (!siteLastUsed) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site last used: %s: %s", siteName, str_lastUsed ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site last used: %s: %s", siteName, str_lastUsed ) };
return NULL; return NULL;
} }
MPMarshalledSite *site = mpw_marshall_site( MPMarshalledSite *site = mpw_marshall_site(
user, siteName, siteType, siteCounter, siteAlgorithm ); user, siteName, siteType, siteCounter, siteAlgorithm );
if (!site) { if (!site) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new site." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new site." };
return NULL; return NULL;
} }
@ -594,7 +594,7 @@ static MPMarshalledUser *mpw_marshall_read_flat(
if (!user->redacted) { if (!user->redacted) {
// Clear Text // Clear Text
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, fullName, masterPassword )) { if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, fullName, masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return NULL; return NULL;
} }
@ -614,7 +614,7 @@ static MPMarshalledUser *mpw_marshall_read_flat(
} }
} }
else { else {
error->type = MPMarshallErrorMissing; error->type = MPMarshalErrorMissing;
error->description = mpw_str( error->description = mpw_str(
"Missing one of: lastUsed=%s, uses=%s, type=%s, version=%s, counter=%s, loginName=%s, siteName=%s", "Missing one of: lastUsed=%s, uses=%s, type=%s, version=%s, counter=%s, loginName=%s, siteName=%s",
str_lastUsed, str_uses, str_type, str_algorithm, str_counter, siteLoginName, siteName ); str_lastUsed, str_uses, str_type, str_algorithm, str_counter, siteLoginName, siteName );
@ -627,13 +627,13 @@ static MPMarshalledUser *mpw_marshall_read_flat(
mpw_free_strings( &fullName, &keyID, NULL ); mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
*error = (MPMarshallError){ .type = MPMarshallSuccess }; *error = (MPMarshalError){ .type = MPMarshalSuccess };
return user; return user;
} }
#if MPW_JSON #if MPW_JSON
static void mpw_marshall_read_json_info( static void mpw_marshall_read_json_info(
const char *in, MPMarshallInfo *info) { const char *in, MPMarshalInfo *info) {
// Parse JSON. // Parse JSON.
enum json_tokener_error json_error = json_tokener_success; enum json_tokener_error json_error = json_tokener_success;
@ -657,11 +657,11 @@ static void mpw_marshall_read_json_info(
} }
static MPMarshalledUser *mpw_marshall_read_json( static MPMarshalledUser *mpw_marshall_read_json(
const char *in, const char *masterPassword, MPMarshallError *error) { const char *in, const char *masterPassword, MPMarshalError *error) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Unexpected internal error." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Unexpected internal error." };
if (!in || !strlen( in )) { if (!in || !strlen( in )) {
error->type = MPMarshallErrorStructure; error->type = MPMarshalErrorStructure;
error->description = mpw_str( "No input data." ); error->description = mpw_str( "No input data." );
return NULL; return NULL;
} }
@ -670,7 +670,7 @@ static MPMarshalledUser *mpw_marshall_read_json(
enum json_tokener_error json_error = json_tokener_success; enum json_tokener_error json_error = json_tokener_success;
json_object *json_file = json_tokener_parse_verbose( in, &json_error ); json_object *json_file = json_tokener_parse_verbose( in, &json_error );
if (!json_file || json_error != json_tokener_success) { if (!json_file || json_error != json_tokener_success) {
*error = (MPMarshallError){ MPMarshallErrorStructure, mpw_str( "JSON error: %s", json_tokener_error_desc( json_error ) ) }; *error = (MPMarshalError){ MPMarshalErrorStructure, mpw_str( "JSON error: %s", json_tokener_error_desc( json_error ) ) };
return NULL; return NULL;
} }
@ -682,7 +682,7 @@ static MPMarshalledUser *mpw_marshall_read_json(
// Section: "export" // Section: "export"
int64_t fileFormat = mpw_get_json_int( json_file, "export.format", 0 ); int64_t fileFormat = mpw_get_json_int( json_file, "export.format", 0 );
if (fileFormat < 1) { if (fileFormat < 1) {
*error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported format: %u", fileFormat ) }; *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unsupported format: %u", fileFormat ) };
return NULL; return NULL;
} }
bool fileRedacted = mpw_get_json_boolean( json_file, "export.redacted", true ); bool fileRedacted = mpw_get_json_boolean( json_file, "export.redacted", true );
@ -694,34 +694,34 @@ static MPMarshalledUser *mpw_marshall_read_json(
const char *keyID = mpw_get_json_string( json_file, "user.key_id", NULL ); const char *keyID = mpw_get_json_string( json_file, "user.key_id", NULL );
int64_t value = mpw_get_json_int( json_file, "user.algorithm", MPAlgorithmVersionCurrent ); int64_t value = mpw_get_json_int( json_file, "user.algorithm", MPAlgorithmVersionCurrent );
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) { if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user algorithm version: %u", value ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user algorithm version: %u", value ) };
return NULL; return NULL;
} }
MPAlgorithmVersion algorithm = (MPAlgorithmVersion)value; MPAlgorithmVersion algorithm = (MPAlgorithmVersion)value;
MPResultType defaultType = (MPResultType)mpw_get_json_int( json_file, "user.default_type", MPResultTypeDefault ); MPResultType defaultType = (MPResultType)mpw_get_json_int( json_file, "user.default_type", MPResultTypeDefault );
if (!mpw_nameForType( defaultType )) { if (!mpw_nameForType( defaultType )) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user default type: %u", defaultType ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user default type: %u", defaultType ) };
return NULL; return NULL;
} }
time_t lastUsed = mpw_mktime( str_lastUsed ); time_t lastUsed = mpw_mktime( str_lastUsed );
if (!lastUsed) { if (!lastUsed) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid user last used: %s", str_lastUsed ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user last used: %s", str_lastUsed ) };
return NULL; return NULL;
} }
if (!fullName || !strlen( fullName )) { if (!fullName || !strlen( fullName )) {
*error = (MPMarshallError){ MPMarshallErrorMissing, "Missing value for full name." }; *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing value for full name." };
return NULL; return NULL;
} }
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, algorithm, fullName, masterPassword )) { if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, algorithm, fullName, masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return NULL; return NULL;
} }
if (keyID && !mpw_id_buf_equals( keyID, mpw_id_buf( masterKey, MPMasterKeySize ) )) { if (keyID && !mpw_id_buf_equals( keyID, mpw_id_buf( masterKey, MPMasterKeySize ) )) {
*error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Master password doesn't match key ID." }; *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Master password doesn't match key ID." };
return NULL; return NULL;
} }
if (!(user = mpw_marshall_user( fullName, masterPassword, algorithm ))) { if (!(user = mpw_marshall_user( fullName, masterPassword, algorithm ))) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new user." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new user." };
return NULL; return NULL;
} }
user->redacted = fileRedacted; user->redacted = fileRedacted;
@ -736,18 +736,18 @@ static MPMarshalledUser *mpw_marshall_read_json(
const char *siteName = json_site.key; const char *siteName = json_site.key;
value = mpw_get_json_int( json_site.val, "algorithm", (int32_t)user->algorithm ); value = mpw_get_json_int( json_site.val, "algorithm", (int32_t)user->algorithm );
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) { if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site algorithm version: %s: %d", siteName, value ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site algorithm version: %s: %d", siteName, value ) };
return NULL; return NULL;
} }
MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value; MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value;
MPResultType siteType = (MPResultType)mpw_get_json_int( json_site.val, "type", (int32_t)user->defaultType ); MPResultType siteType = (MPResultType)mpw_get_json_int( json_site.val, "type", (int32_t)user->defaultType );
if (!mpw_nameForType( siteType )) { if (!mpw_nameForType( siteType )) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site type: %s: %u", siteName, siteType ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site type: %s: %u", siteName, siteType ) };
return NULL; return NULL;
} }
value = mpw_get_json_int( json_site.val, "counter", 1 ); value = mpw_get_json_int( json_site.val, "counter", 1 );
if (value < MPCounterValueFirst || value > MPCounterValueLast) { if (value < MPCounterValueFirst || value > MPCounterValueLast) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site counter: %s: %d", siteName, value ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site counter: %s: %d", siteName, value ) };
return NULL; return NULL;
} }
MPCounterValue siteCounter = (MPCounterValue)value; MPCounterValue siteCounter = (MPCounterValue)value;
@ -758,7 +758,7 @@ static MPMarshalledUser *mpw_marshall_read_json(
str_lastUsed = mpw_get_json_string( json_site.val, "last_used", NULL ); str_lastUsed = mpw_get_json_string( json_site.val, "last_used", NULL );
time_t siteLastUsed = mpw_mktime( str_lastUsed ); time_t siteLastUsed = mpw_mktime( str_lastUsed );
if (!siteLastUsed) { if (!siteLastUsed) {
*error = (MPMarshallError){ MPMarshallErrorIllegal, mpw_str( "Invalid site last used: %s: %s", siteName, str_lastUsed ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site last used: %s: %s", siteName, str_lastUsed ) };
return NULL; return NULL;
} }
@ -767,7 +767,7 @@ static MPMarshalledUser *mpw_marshall_read_json(
MPMarshalledSite *site = mpw_marshall_site( user, siteName, siteType, siteCounter, siteAlgorithm ); MPMarshalledSite *site = mpw_marshall_site( user, siteName, siteType, siteCounter, siteAlgorithm );
if (!site) { if (!site) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new site." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new site." };
return NULL; return NULL;
} }
@ -778,7 +778,7 @@ static MPMarshalledUser *mpw_marshall_read_json(
if (!user->redacted) { if (!user->redacted) {
// Clear Text // Clear Text
if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, fullName, masterPassword )) { if (!mpw_update_masterKey( &masterKey, &masterKeyAlgorithm, site->algorithm, fullName, masterPassword )) {
*error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
return NULL; return NULL;
} }
@ -819,24 +819,24 @@ static MPMarshalledUser *mpw_marshall_read_json(
} }
json_object_put( json_file ); json_object_put( json_file );
*error = (MPMarshallError){ .type = MPMarshallSuccess }; *error = (MPMarshalError){ .type = MPMarshalSuccess };
return user; return user;
} }
#endif #endif
MPMarshallInfo *mpw_marshall_read_info( MPMarshalInfo *mpw_marshall_read_info(
const char *in) { const char *in) {
MPMarshallInfo *info = malloc( sizeof( MPMarshallInfo ) ); MPMarshalInfo *info = malloc( sizeof( MPMarshalInfo ) );
*info = (MPMarshallInfo){ .format = MPMarshallFormatNone }; *info = (MPMarshalInfo){ .format = MPMarshalFormatNone };
if (in && strlen( in )) { if (in && strlen( in )) {
if (in[0] == '#') { if (in[0] == '#') {
*info = (MPMarshallInfo){ .format = MPMarshallFormatFlat }; *info = (MPMarshalInfo){ .format = MPMarshalFormatFlat };
mpw_marshall_read_flat_info( in, info ); mpw_marshall_read_flat_info( in, info );
} }
else if (in[0] == '{') { else if (in[0] == '{') {
*info = (MPMarshallInfo){ .format = MPMarshallFormatJSON }; *info = (MPMarshalInfo){ .format = MPMarshalFormatJSON };
#if MPW_JSON #if MPW_JSON
mpw_marshall_read_json_info( in, info ); mpw_marshall_read_json_info( in, info );
#endif #endif
@ -847,29 +847,29 @@ MPMarshallInfo *mpw_marshall_read_info(
} }
MPMarshalledUser *mpw_marshall_read( MPMarshalledUser *mpw_marshall_read(
const char *in, const MPMarshallFormat inFormat, const char *masterPassword, MPMarshallError *error) { const char *in, const MPMarshalFormat inFormat, const char *masterPassword, MPMarshalError *error) {
switch (inFormat) { switch (inFormat) {
case MPMarshallFormatNone: case MPMarshalFormatNone:
*error = (MPMarshallError){ .type = MPMarshallSuccess }; *error = (MPMarshalError){ .type = MPMarshalSuccess };
return false; return false;
case MPMarshallFormatFlat: case MPMarshalFormatFlat:
return mpw_marshall_read_flat( in, masterPassword, error ); return mpw_marshall_read_flat( in, masterPassword, error );
#if MPW_JSON #if MPW_JSON
case MPMarshallFormatJSON: case MPMarshalFormatJSON:
return mpw_marshall_read_json( in, masterPassword, error ); return mpw_marshall_read_json( in, masterPassword, error );
#endif #endif
default: default:
*error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported input format: %u", inFormat ) }; *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unsupported input format: %u", inFormat ) };
return NULL; return NULL;
} }
} }
const MPMarshallFormat mpw_formatWithName( const MPMarshalFormat mpw_formatWithName(
const char *formatName) { const char *formatName) {
if (!formatName || !strlen( formatName )) if (!formatName || !strlen( formatName ))
return MPMarshallFormatNone; return MPMarshalFormatNone;
// Lower-case to standardize it. // Lower-case to standardize it.
size_t stdFormatNameSize = strlen( formatName ); size_t stdFormatNameSize = strlen( formatName );
@ -878,26 +878,26 @@ const MPMarshallFormat mpw_formatWithName(
stdFormatName[c] = (char)tolower( formatName[c] ); stdFormatName[c] = (char)tolower( formatName[c] );
stdFormatName[stdFormatNameSize] = '\0'; stdFormatName[stdFormatNameSize] = '\0';
if (strncmp( mpw_nameForFormat( MPMarshallFormatNone ), stdFormatName, strlen( stdFormatName ) ) == 0) if (strncmp( mpw_nameForFormat( MPMarshalFormatNone ), stdFormatName, strlen( stdFormatName ) ) == 0)
return MPMarshallFormatNone; return MPMarshalFormatNone;
if (strncmp( mpw_nameForFormat( MPMarshallFormatFlat ), stdFormatName, strlen( stdFormatName ) ) == 0) if (strncmp( mpw_nameForFormat( MPMarshalFormatFlat ), stdFormatName, strlen( stdFormatName ) ) == 0)
return MPMarshallFormatFlat; return MPMarshalFormatFlat;
if (strncmp( mpw_nameForFormat( MPMarshallFormatJSON ), stdFormatName, strlen( stdFormatName ) ) == 0) if (strncmp( mpw_nameForFormat( MPMarshalFormatJSON ), stdFormatName, strlen( stdFormatName ) ) == 0)
return MPMarshallFormatJSON; return MPMarshalFormatJSON;
dbg( "Not a format name: %s\n", stdFormatName ); dbg( "Not a format name: %s\n", stdFormatName );
return (MPMarshallFormat)ERR; return (MPMarshalFormat)ERR;
} }
const char *mpw_nameForFormat( const char *mpw_nameForFormat(
const MPMarshallFormat format) { const MPMarshalFormat format) {
switch (format) { switch (format) {
case MPMarshallFormatNone: case MPMarshalFormatNone:
return "none"; return "none";
case MPMarshallFormatFlat: case MPMarshalFormatFlat:
return "flat"; return "flat";
case MPMarshallFormatJSON: case MPMarshalFormatJSON:
return "json"; return "json";
default: { default: {
dbg( "Unknown format: %d\n", format ); dbg( "Unknown format: %d\n", format );
@ -907,14 +907,14 @@ const char *mpw_nameForFormat(
} }
const char *mpw_marshall_format_extension( const char *mpw_marshall_format_extension(
const MPMarshallFormat format) { const MPMarshalFormat format) {
switch (format) { switch (format) {
case MPMarshallFormatNone: case MPMarshalFormatNone:
return NULL; return NULL;
case MPMarshallFormatFlat: case MPMarshalFormatFlat:
return "mpsites"; return "mpsites";
case MPMarshallFormatJSON: case MPMarshalFormatJSON:
return "mpsites.json"; return "mpsites.json";
default: { default: {
dbg( "Unknown format: %d\n", format ); dbg( "Unknown format: %d\n", format );

View File

@ -25,41 +25,41 @@
//// Types. //// Types.
typedef mpw_enum( unsigned int, MPMarshallFormat ) { typedef mpw_enum( unsigned int, MPMarshalFormat ) {
/** Generate a key for authentication. */ /** Generate a key for authentication. */
MPMarshallFormatNone, MPMarshalFormatNone,
/** Generate a key for authentication. */ /** Generate a key for authentication. */
MPMarshallFormatFlat, MPMarshalFormatFlat,
/** Generate a name for identification. */ /** Generate a name for identification. */
MPMarshallFormatJSON, MPMarshalFormatJSON,
#if MPW_JSON #if MPW_JSON
MPMarshallFormatDefault = MPMarshallFormatJSON, MPMarshalFormatDefault = MPMarshalFormatJSON,
#else #else
MPMarshallFormatDefault = MPMarshallFormatFlat, MPMarshalFormatDefault = MPMarshalFormatFlat,
#endif #endif
}; };
typedef mpw_enum( unsigned int, MPMarshallErrorType ) { typedef mpw_enum( unsigned int, MPMarshalErrorType ) {
/** The marshalling operation completed successfully. */ /** The marshalling operation completed successfully. */
MPMarshallSuccess, MPMarshalSuccess,
/** An error in the structure of the marshall file interrupted marshalling. */ /** An error in the structure of the marshall file interrupted marshalling. */
MPMarshallErrorStructure, MPMarshalErrorStructure,
/** The marshall file uses an unsupported format version. */ /** The marshall file uses an unsupported format version. */
MPMarshallErrorFormat, MPMarshalErrorFormat,
/** A required value is missing or not specified. */ /** A required value is missing or not specified. */
MPMarshallErrorMissing, MPMarshalErrorMissing,
/** The given master password is not valid. */ /** The given master password is not valid. */
MPMarshallErrorMasterPassword, MPMarshalErrorMasterPassword,
/** An illegal value was specified. */ /** An illegal value was specified. */
MPMarshallErrorIllegal, MPMarshalErrorIllegal,
/** An internal system error interrupted marshalling. */ /** An internal system error interrupted marshalling. */
MPMarshallErrorInternal, MPMarshalErrorInternal,
}; };
typedef struct MPMarshallError { typedef struct MPMarshalError {
MPMarshallErrorType type; MPMarshalErrorType type;
const char *description; const char *description;
} MPMarshallError; } MPMarshalError;
typedef struct MPMarshalledQuestion { typedef struct MPMarshalledQuestion {
const char *keyword; const char *keyword;
@ -99,26 +99,26 @@ typedef struct MPMarshalledUser {
MPMarshalledSite *sites; MPMarshalledSite *sites;
} MPMarshalledUser; } MPMarshalledUser;
typedef struct MPMarshallInfo { typedef struct MPMarshalInfo {
MPMarshallFormat format; MPMarshalFormat format;
MPAlgorithmVersion algorithm; MPAlgorithmVersion algorithm;
const char *fullName; const char *fullName;
const char *keyID; const char *keyID;
bool redacted; bool redacted;
time_t date; time_t date;
} MPMarshallInfo; } MPMarshalInfo;
//// Marshalling. //// Marshalling.
/** Write the user and all associated data out to the given output buffer using the given marshalling format. */ /** Write the user and all associated data out to the given output buffer using the given marshalling format. */
bool mpw_marshall_write( bool mpw_marshall_write(
char **out, const MPMarshallFormat outFormat, const MPMarshalledUser *user, MPMarshallError *error); char **out, const MPMarshalFormat outFormat, const MPMarshalledUser *user, MPMarshalError *error);
/** Try to read metadata on the sites in the input buffer. */ /** Try to read metadata on the sites in the input buffer. */
MPMarshallInfo *mpw_marshall_read_info( MPMarshalInfo *mpw_marshall_read_info(
const char *in); const char *in);
/** Unmarshall sites in the given input buffer by parsing it using the given marshalling format. */ /** Unmarshall sites in the given input buffer by parsing it using the given marshalling format. */
MPMarshalledUser *mpw_marshall_read( MPMarshalledUser *mpw_marshall_read(
const char *in, const MPMarshallFormat inFormat, const char *masterPassword, MPMarshallError *error); const char *in, const MPMarshalFormat inFormat, const char *masterPassword, MPMarshalError *error);
//// Utilities. //// Utilities.
@ -134,7 +134,7 @@ MPMarshalledQuestion *mpw_marshal_question(
MPMarshalledSite *site, const char *keyword); MPMarshalledSite *site, const char *keyword);
/** Free the given user object and all associated data. */ /** Free the given user object and all associated data. */
bool mpw_marshal_info_free( bool mpw_marshal_info_free(
MPMarshallInfo **info); MPMarshalInfo **info);
bool mpw_marshal_free( bool mpw_marshal_free(
MPMarshalledUser **user); MPMarshalledUser **user);
@ -143,17 +143,17 @@ bool mpw_marshal_free(
/** /**
* @return The purpose represented by the given name. * @return The purpose represented by the given name.
*/ */
const MPMarshallFormat mpw_formatWithName( const MPMarshalFormat mpw_formatWithName(
const char *formatName); const char *formatName);
/** /**
* @return The standard name for the given purpose. * @return The standard name for the given purpose.
*/ */
const char *mpw_nameForFormat( const char *mpw_nameForFormat(
const MPMarshallFormat format); const MPMarshalFormat format);
/** /**
* @return The file extension that's recommended for files that use the given marshalling format. * @return The file extension that's recommended for files that use the given marshalling format.
*/ */
const char *mpw_marshall_format_extension( const char *mpw_marshall_format_extension(
const MPMarshallFormat format); const MPMarshalFormat format);
#endif // _MPW_MARSHALL_H #endif // _MPW_MARSHALL_H

View File

@ -0,0 +1,7 @@
package com.lyndir.masterpassword.model;
/**
* @author lhunath, 2017-09-20
*/
public class MPMarshalFormat {
}

View File

@ -17,11 +17,11 @@ import org.joda.time.format.ISODateTimeFormat;
*/ */
public class MPSiteMarshaller { public class MPSiteMarshaller {
private static final DateTimeFormatter rfc3339 = ISODateTimeFormat.dateTimeNoMillis(); protected static final DateTimeFormatter rfc3339 = ISODateTimeFormat.dateTimeNoMillis();
private final StringBuilder export = new StringBuilder(); private final StringBuilder export = new StringBuilder();
private ContentMode contentMode = ContentMode.PROTECTED; private final ContentMode contentMode = ContentMode.PROTECTED;
private MasterKey masterKey; private final MasterKey masterKey;
public static MPSiteMarshaller marshallSafe(final MPUser user) { public static MPSiteMarshaller marshallSafe(final MPUser user) {
MPSiteMarshaller marshaller = new MPSiteMarshaller(); MPSiteMarshaller marshaller = new MPSiteMarshaller();
@ -64,7 +64,6 @@ public class MPSiteMarshaller {
header.append( "# Full Name: " ).append( user.getFullName() ).append( '\n' ); header.append( "# Full Name: " ).append( user.getFullName() ).append( '\n' );
header.append( "# Avatar: " ).append( user.getAvatar() ).append( '\n' ); header.append( "# Avatar: " ).append( user.getAvatar() ).append( '\n' );
header.append( "# Key ID: " ).append( user.exportKeyID() ).append( '\n' ); header.append( "# Key ID: " ).append( user.exportKeyID() ).append( '\n' );
// header.append( "# Version: " ).append( MasterKey.Version.CURRENT.toBundleVersion() ).append( '\n' );
header.append( "# Algorithm: " ).append( MasterKey.Version.CURRENT.toInt() ).append( '\n' ); header.append( "# Algorithm: " ).append( MasterKey.Version.CURRENT.toInt() ).append( '\n' );
header.append( "# Default Type: " ).append( user.getDefaultType().getType() ).append( '\n' ); header.append( "# Default Type: " ).append( user.getDefaultType().getType() ).append( '\n' );
header.append( "# Passwords: " ).append( this.contentMode.name() ).append( '\n' ); header.append( "# Passwords: " ).append( this.contentMode.name() ).append( '\n' );

View File

@ -566,10 +566,10 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
saveInContext:(NSManagedObjectContext *)context { saveInContext:(NSManagedObjectContext *)context {
// Read metadata for the import file. // Read metadata for the import file.
MPMarshallInfo *info = mpw_marshall_read_info( importData.UTF8String ); MPMarshalInfo *info = mpw_marshall_read_info( importData.UTF8String );
if (info->format == MPMarshallFormatNone) if (info->format == MPMarshalFormatNone)
return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshallCode userInfo:@{ return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{
@"type" : @(MPMarshallErrorFormat), @"type" : @(MPMarshalErrorFormat),
NSLocalizedDescriptionKey: @"This is not a Master Password import file.", NSLocalizedDescriptionKey: @"This is not a Master Password import file.",
}]), @"While importing sites." ); }]), @"While importing sites." );
@ -589,13 +589,13 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
caseInsensitiveCompare:@(info->keyID)] != NSOrderedSame); caseInsensitiveCompare:@(info->keyID)] != NSOrderedSame);
// Parse import data. // Parse import data.
MPMarshallError importError = { .type = MPMarshallSuccess }; MPMarshalError importError = { .type = MPMarshalSuccess };
MPMarshalledUser *importUser = mpw_marshall_read( importData.UTF8String, info->format, importMasterPassword.UTF8String, &importError ); MPMarshalledUser *importUser = mpw_marshall_read( importData.UTF8String, info->format, importMasterPassword.UTF8String, &importError );
mpw_marshal_info_free( &info ); mpw_marshal_info_free( &info );
@try { @try {
if (!importUser || importError.type != MPMarshallSuccess) if (!importUser || importError.type != MPMarshalSuccess)
return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshallCode userInfo:@{ return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{
@"type" : @(importError.type), @"type" : @(importError.type),
NSLocalizedDescriptionKey: @(importError.description), NSLocalizedDescriptionKey: @(importError.description),
}]), @"While importing sites." ); }]), @"While importing sites." );
@ -735,15 +735,15 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
} }
char *export = NULL; char *export = NULL;
MPMarshallError exportError = (MPMarshallError){ .type= MPMarshallSuccess }; MPMarshalError exportError = (MPMarshalError){ .type= MPMarshalSuccess };
mpw_marshall_write( &export, MPMarshallFormatFlat, exportUser, &exportError ); mpw_marshall_write( &export, MPMarshalFormatFlat, exportUser, &exportError );
NSString *mpsites = nil; NSString *mpsites = nil;
if (export && exportError.type == MPMarshallSuccess) if (export && exportError.type == MPMarshalSuccess)
mpsites = [NSString stringWithCString:export encoding:NSUTF8StringEncoding]; mpsites = [NSString stringWithCString:export encoding:NSUTF8StringEncoding];
mpw_free_string( &export ); mpw_free_string( &export );
resultBlock( mpsites, exportError.type == MPMarshallSuccess? nil: resultBlock( mpsites, exportError.type == MPMarshalSuccess? nil:
[NSError errorWithDomain:MPErrorDomain code:MPErrorMarshallCode userInfo:@{ [NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{
@"type" : @(exportError.type), @"type" : @(exportError.type),
NSLocalizedDescriptionKey: @(exportError.description), NSLocalizedDescriptionKey: @(exportError.description),
}] ); }] );

View File

@ -22,7 +22,7 @@
__BEGIN_DECLS __BEGIN_DECLS
extern NSString *const MPErrorDomain; extern NSString *const MPErrorDomain;
extern NSInteger const MPErrorHangCode; extern NSInteger const MPErrorHangCode;
extern NSInteger const MPErrorMarshallCode; extern NSInteger const MPErrorMarshalCode;
extern NSString *const MPSignedInNotification; extern NSString *const MPSignedInNotification;
extern NSString *const MPSignedOutNotification; extern NSString *const MPSignedOutNotification;

View File

@ -20,7 +20,7 @@
NSString *const MPErrorDomain = @"MPErrorDomain"; NSString *const MPErrorDomain = @"MPErrorDomain";
NSInteger const MPErrorHangCode = 1; NSInteger const MPErrorHangCode = 1;
NSInteger const MPErrorMarshallCode = 1; NSInteger const MPErrorMarshalCode = 1;
NSString *const MPSignedInNotification = @"MPSignedInNotification"; NSString *const MPSignedInNotification = @"MPSignedInNotification";
NSString *const MPSignedOutNotification = @"MPSignedOutNotification"; NSString *const MPSignedOutNotification = @"MPSignedOutNotification";

View File

@ -236,8 +236,8 @@
PearlNotMainQueue( ^{ PearlNotMainQueue( ^{
NSString *importData = [UIPasteboard generalPasteboard].string; NSString *importData = [UIPasteboard generalPasteboard].string;
MPMarshallInfo *importInfo = mpw_marshall_read_info( importData.UTF8String ); MPMarshalInfo *importInfo = mpw_marshall_read_info( importData.UTF8String );
if (importInfo->format != MPMarshallFormatNone) if (importInfo->format != MPMarshalFormatNone)
[PearlAlert showAlertWithTitle:@"Import Sites?" message: [PearlAlert showAlertWithTitle:@"Import Sites?" message:
@"We've detected Master Password import sites on your pasteboard, would you like to import them?" @"We've detected Master Password import sites on your pasteboard, would you like to import them?"
viewStyle:UIAlertViewStyleDefault initAlert:nil viewStyle:UIAlertViewStyleDefault initAlert:nil

View File

@ -97,7 +97,7 @@ static void usage() {
" n, none | No file\n" " n, none | No file\n"
" f, flat | ~/.mpw.d/Full Name.%s\n" " f, flat | ~/.mpw.d/Full Name.%s\n"
" j, json | ~/.mpw.d/Full Name.%s\n\n", " j, json | ~/.mpw.d/Full Name.%s\n\n",
MP_ENV_format, mpw_marshall_format_extension( MPMarshallFormatFlat ), mpw_marshall_format_extension( MPMarshallFormatJSON ) ); MP_ENV_format, mpw_marshall_format_extension( MPMarshalFormatFlat ), mpw_marshall_format_extension( MPMarshalFormatJSON ) );
inf( "" inf( ""
" -R redacted Whether to save the mpsites in redacted format or not.\n" " -R redacted Whether to save the mpsites in redacted format or not.\n"
" Redaction omits or encrypts any secrets, making the file safe\n" " Redaction omits or encrypts any secrets, making the file safe\n"
@ -142,7 +142,7 @@ typedef struct {
const char *masterPassword; const char *masterPassword;
const char *identicon; const char *identicon;
const char *siteName; const char *siteName;
MPMarshallFormat sitesFormat; MPMarshalFormat sitesFormat;
MPKeyPurpose keyPurpose; MPKeyPurpose keyPurpose;
const char *keyContext; const char *keyContext;
const char *sitesPath; const char *sitesPath;
@ -191,7 +191,7 @@ int main(const int argc, char *const argv[]) {
Operation operation = { Operation operation = {
.allowPasswordUpdate = false, .allowPasswordUpdate = false,
.sitesFormatFixed = false, .sitesFormatFixed = false,
.sitesFormat = MPMarshallFormatDefault, .sitesFormat = MPMarshalFormatDefault,
.keyPurpose = MPKeyPurposeAuthentication, .keyPurpose = MPKeyPurposeAuthentication,
.resultType = MPResultTypeDefault, .resultType = MPResultTypeDefault,
.siteCounter = MPCounterValueDefault, .siteCounter = MPCounterValueDefault,
@ -453,9 +453,9 @@ void cli_user(Arguments *args, Operation *operation) {
// Try to fall back to the flat format. // Try to fall back to the flat format.
if (!operation->sitesFormatFixed) { if (!operation->sitesFormatFixed) {
mpw_free_string( &operation->sitesPath ); mpw_free_string( &operation->sitesPath );
operation->sitesPath = mpw_path( operation->fullName, mpw_marshall_format_extension( MPMarshallFormatFlat ) ); operation->sitesPath = mpw_path( operation->fullName, mpw_marshall_format_extension( MPMarshalFormatFlat ) );
if (operation->sitesPath && (sitesFile = fopen( operation->sitesPath, "r" ))) if (operation->sitesPath && (sitesFile = fopen( operation->sitesPath, "r" )))
operation->sitesFormat = MPMarshallFormatFlat; operation->sitesFormat = MPMarshalFormatFlat;
else else
dbg( "Couldn't open configuration file:\n %s: %s\n", operation->sitesPath, strerror( errno ) ); dbg( "Couldn't open configuration file:\n %s: %s\n", operation->sitesPath, strerror( errno ) );
} }
@ -473,14 +473,14 @@ void cli_user(Arguments *args, Operation *operation) {
fclose( sitesFile ); fclose( sitesFile );
// Parse file. // Parse file.
MPMarshallInfo *sitesInputInfo = mpw_marshall_read_info( sitesInputData ); MPMarshalInfo *sitesInputInfo = mpw_marshall_read_info( sitesInputData );
MPMarshallFormat sitesInputFormat = args->sitesFormat? operation->sitesFormat: sitesInputInfo->format; MPMarshalFormat sitesInputFormat = args->sitesFormat? operation->sitesFormat: sitesInputInfo->format;
MPMarshallError marshallError = { .type = MPMarshallSuccess }; MPMarshalError marshallError = { .type = MPMarshalSuccess };
mpw_marshal_info_free( &sitesInputInfo ); mpw_marshal_info_free( &sitesInputInfo );
operation->user = mpw_marshall_read( sitesInputData, sitesInputFormat, operation->masterPassword, &marshallError ); operation->user = mpw_marshall_read( sitesInputData, sitesInputFormat, operation->masterPassword, &marshallError );
if (marshallError.type == MPMarshallErrorMasterPassword && operation->allowPasswordUpdate) { if (marshallError.type == MPMarshalErrorMasterPassword && operation->allowPasswordUpdate) {
// Update master password in mpsites. // Update master password in mpsites.
while (marshallError.type == MPMarshallErrorMasterPassword) { while (marshallError.type == MPMarshalErrorMasterPassword) {
inf( "Given master password does not match configuration.\n" ); inf( "Given master password does not match configuration.\n" );
inf( "To update the configuration with this new master password, first confirm the old master password.\n" ); inf( "To update the configuration with this new master password, first confirm the old master password.\n" );
@ -500,14 +500,14 @@ void cli_user(Arguments *args, Operation *operation) {
mpw_free_string( &sitesInputData ); mpw_free_string( &sitesInputData );
// Incorrect master password. // Incorrect master password.
if (marshallError.type == MPMarshallErrorMasterPassword) { if (marshallError.type == MPMarshalErrorMasterPassword) {
ftl( "Incorrect master password according to configuration:\n %s: %s\n", operation->sitesPath, marshallError.description ); ftl( "Incorrect master password according to configuration:\n %s: %s\n", operation->sitesPath, marshallError.description );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
} }
// Any other parse error. // Any other parse error.
if (!operation->user || marshallError.type != MPMarshallSuccess) { if (!operation->user || marshallError.type != MPMarshalSuccess) {
err( "Couldn't parse configuration file:\n %s: %s\n", operation->sitesPath, marshallError.description ); err( "Couldn't parse configuration file:\n %s: %s\n", operation->sitesPath, marshallError.description );
cli_free( args, operation ); cli_free( args, operation );
exit( EX_DATAERR ); exit( EX_DATAERR );
@ -757,11 +757,11 @@ void cli_mpw(Arguments *args, Operation *operation) {
void cli_save(Arguments __unused *args, Operation *operation) { void cli_save(Arguments __unused *args, Operation *operation) {
if (operation->sitesFormat == MPMarshallFormatNone) if (operation->sitesFormat == MPMarshalFormatNone)
return; return;
if (!operation->sitesFormatFixed) if (!operation->sitesFormatFixed)
operation->sitesFormat = MPMarshallFormatDefault; operation->sitesFormat = MPMarshalFormatDefault;
operation->sitesPath = mpw_path( operation->user->fullName, mpw_marshall_format_extension( operation->sitesFormat ) ); operation->sitesPath = mpw_path( operation->user->fullName, mpw_marshall_format_extension( operation->sitesFormat ) );
dbg( "Updating: %s (%s)\n", operation->sitesPath, mpw_nameForFormat( operation->sitesFormat ) ); dbg( "Updating: %s (%s)\n", operation->sitesPath, mpw_nameForFormat( operation->sitesFormat ) );
@ -772,8 +772,8 @@ void cli_save(Arguments __unused *args, Operation *operation) {
} }
char *buf = NULL; char *buf = NULL;
MPMarshallError marshallError = { .type = MPMarshallSuccess }; MPMarshalError marshallError = { .type = MPMarshalSuccess };
if (!mpw_marshall_write( &buf, operation->sitesFormat, operation->user, &marshallError ) || marshallError.type != MPMarshallSuccess) if (!mpw_marshall_write( &buf, operation->sitesFormat, operation->user, &marshallError ) || marshallError.type != MPMarshalSuccess)
wrn( "Couldn't encode updated configuration file:\n %s: %s\n", operation->sitesPath, marshallError.description ); wrn( "Couldn't encode updated configuration file:\n %s: %s\n", operation->sitesPath, marshallError.description );
else if (fwrite( buf, sizeof( char ), strlen( buf ), sitesFile ) != strlen( buf )) else if (fwrite( buf, sizeof( char ), strlen( buf ), sitesFile ) != strlen( buf ))