diff --git a/core/c/mpw-marshall.c b/core/c/mpw-marshall.c index 92199600..0c4a6829 100644 --- a/core/c/mpw-marshall.c +++ b/core/c/mpw-marshall.c @@ -94,14 +94,14 @@ MPMarshalledQuestion *mpw_marshal_question( } bool mpw_marshal_info_free( - MPMarshallInfo **info) { + MPMarshalInfo **info) { if (!info || !*info) return true; bool success = true; success &= mpw_free_strings( &(*info)->fullName, &(*info)->keyID, NULL ); - success &= mpw_free( info, sizeof( MPMarshallInfo ) ); + success &= mpw_free( info, sizeof( MPMarshalInfo ) ); return success; } @@ -133,21 +133,21 @@ bool mpw_marshal_free( } 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 )) { - *error = (MPMarshallError){ MPMarshallErrorMissing, "Missing full name." }; + *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing full name." }; return false; } if (!user->masterPassword || !strlen( user->masterPassword )) { - *error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Missing master password." }; + *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Missing master password." }; return false; } MPMasterKey masterKey = NULL; MPAlgorithmVersion masterKeyAlgorithm = user->algorithm - 1; 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; } @@ -186,7 +186,7 @@ static bool mpw_marshall_write_flat( if (!user->redacted) { // Clear Text 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; } @@ -211,27 +211,27 @@ static bool mpw_marshall_write_flat( } mpw_free( &masterKey, MPMasterKeySize ); - *error = (MPMarshallError){ .type = MPMarshallSuccess }; + *error = (MPMarshalError){ .type = MPMarshalSuccess }; return true; } #if MPW_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 )) { - *error = (MPMarshallError){ MPMarshallErrorMissing, "Missing full name." }; + *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing full name." }; return false; } if (!user->masterPassword || !strlen( user->masterPassword )) { - *error = (MPMarshallError){ MPMarshallErrorMasterPassword, "Missing master password." }; + *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Missing master password." }; return false; } MPMasterKey masterKey = NULL; MPAlgorithmVersion masterKeyAlgorithm = user->algorithm - 1; 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; } @@ -272,7 +272,7 @@ static bool mpw_marshall_write_json( if (!user->redacted) { // Clear Text 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; } @@ -340,32 +340,32 @@ static bool mpw_marshall_write_json( mpw_free( &masterKey, MPMasterKeySize ); json_object_put( json_file ); - *error = (MPMarshallError){ .type = MPMarshallSuccess }; + *error = (MPMarshalError){ .type = MPMarshalSuccess }; return true; } #endif 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) { - case MPMarshallFormatNone: - *error = (MPMarshallError){ .type = MPMarshallSuccess }; + case MPMarshalFormatNone: + *error = (MPMarshalError){ .type = MPMarshalSuccess }; return false; - case MPMarshallFormatFlat: + case MPMarshalFormatFlat: return mpw_marshall_write_flat( out, user, error ); #if MPW_JSON - case MPMarshallFormatJSON: + case MPMarshalFormatJSON: return mpw_marshall_write_json( out, user, error ); #endif default: - *error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported output format: %u", outFormat ) }; + *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unsupported output format: %u", outFormat ) }; return false; } } static void mpw_marshall_read_flat_info( - const char *in, MPMarshallInfo *info) { + const char *in, MPMarshalInfo *info) { info->algorithm = MPAlgorithmVersionCurrent; @@ -412,11 +412,11 @@ static void mpw_marshall_read_flat_info( } 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 )) { - error->type = MPMarshallErrorStructure; + error->type = MPMarshalErrorStructure; error->description = mpw_str( "No input data." ); return NULL; } @@ -455,7 +455,7 @@ static MPMarshalledUser *mpw_marshall_read_flat( char *headerName = mpw_get_token( &positionInLine, endOfLine, ":\n" ); char *headerValue = mpw_get_token( &positionInLine, endOfLine, "\n" ); if (!headerName || !headerValue) { - error->type = MPMarshallErrorStructure; + error->type = MPMarshalErrorStructure; error->description = mpw_str( "Invalid header: %s", strndup( positionInLine, (size_t)(endOfLine - positionInLine) ) ); return NULL; } @@ -471,7 +471,7 @@ static MPMarshalledUser *mpw_marshall_read_flat( if (strcmp( headerName, "Algorithm" ) == 0) { int value = atoi( headerValue ); 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; } algorithm = (MPAlgorithmVersion)value; @@ -479,7 +479,7 @@ static MPMarshalledUser *mpw_marshall_read_flat( if (strcmp( headerName, "Default Type" ) == 0) { int value = atoi( headerValue ); 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; } defaultType = (MPResultType)value; @@ -493,7 +493,7 @@ static MPMarshalledUser *mpw_marshall_read_flat( if (!headerEnded) continue; if (!fullName) { - *error = (MPMarshallError){ MPMarshallErrorMissing, "Missing header: Full Name" }; + *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing header: Full Name" }; return NULL; } if (positionInLine >= endOfLine) @@ -501,15 +501,15 @@ static MPMarshalledUser *mpw_marshall_read_flat( if (!user) { 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; } 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; } 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; } @@ -553,7 +553,7 @@ static MPMarshalledUser *mpw_marshall_read_flat( break; } default: { - *error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unexpected import format: %u", format ) }; + *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unexpected import format: %u", format ) }; return NULL; } } @@ -561,31 +561,31 @@ static MPMarshalledUser *mpw_marshall_read_flat( if (siteName && str_type && str_counter && str_algorithm && str_uses && str_lastUsed) { MPResultType siteType = (MPResultType)atoi( str_type ); 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; } long long int value = atoll( str_counter ); 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; } MPCounterValue siteCounter = (MPCounterValue)value; value = atoll( str_algorithm ); 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; } MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value; time_t siteLastUsed = mpw_mktime( str_lastUsed ); 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; } MPMarshalledSite *site = mpw_marshall_site( user, siteName, siteType, siteCounter, siteAlgorithm ); if (!site) { - *error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new site." }; + *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new site." }; return NULL; } @@ -594,7 +594,7 @@ static MPMarshalledUser *mpw_marshall_read_flat( if (!user->redacted) { // Clear Text 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; } @@ -614,7 +614,7 @@ static MPMarshalledUser *mpw_marshall_read_flat( } } else { - error->type = MPMarshallErrorMissing; + error->type = MPMarshalErrorMissing; error->description = mpw_str( "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 ); @@ -627,13 +627,13 @@ static MPMarshalledUser *mpw_marshall_read_flat( mpw_free_strings( &fullName, &keyID, NULL ); mpw_free( &masterKey, MPMasterKeySize ); - *error = (MPMarshallError){ .type = MPMarshallSuccess }; + *error = (MPMarshalError){ .type = MPMarshalSuccess }; return user; } #if MPW_JSON static void mpw_marshall_read_json_info( - const char *in, MPMarshallInfo *info) { + const char *in, MPMarshalInfo *info) { // Parse JSON. 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( - 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 )) { - error->type = MPMarshallErrorStructure; + error->type = MPMarshalErrorStructure; error->description = mpw_str( "No input data." ); return NULL; } @@ -670,7 +670,7 @@ static MPMarshalledUser *mpw_marshall_read_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) { - *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; } @@ -682,7 +682,7 @@ static MPMarshalledUser *mpw_marshall_read_json( // Section: "export" int64_t fileFormat = mpw_get_json_int( json_file, "export.format", 0 ); if (fileFormat < 1) { - *error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported format: %u", fileFormat ) }; + *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unsupported format: %u", fileFormat ) }; return NULL; } 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 ); int64_t value = mpw_get_json_int( json_file, "user.algorithm", MPAlgorithmVersionCurrent ); 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; } MPAlgorithmVersion algorithm = (MPAlgorithmVersion)value; MPResultType defaultType = (MPResultType)mpw_get_json_int( json_file, "user.default_type", MPResultTypeDefault ); 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; } time_t lastUsed = mpw_mktime( str_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; } if (!fullName || !strlen( fullName )) { - *error = (MPMarshallError){ MPMarshallErrorMissing, "Missing value for full name." }; + *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing value for full name." }; return NULL; } 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; } 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; } 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; } user->redacted = fileRedacted; @@ -736,18 +736,18 @@ static MPMarshalledUser *mpw_marshall_read_json( const char *siteName = json_site.key; value = mpw_get_json_int( json_site.val, "algorithm", (int32_t)user->algorithm ); 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; } MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value; MPResultType siteType = (MPResultType)mpw_get_json_int( json_site.val, "type", (int32_t)user->defaultType ); 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; } value = mpw_get_json_int( json_site.val, "counter", 1 ); 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; } 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 ); time_t siteLastUsed = mpw_mktime( str_lastUsed ); 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; } @@ -767,7 +767,7 @@ static MPMarshalledUser *mpw_marshall_read_json( MPMarshalledSite *site = mpw_marshall_site( user, siteName, siteType, siteCounter, siteAlgorithm ); if (!site) { - *error = (MPMarshallError){ MPMarshallErrorInternal, "Couldn't allocate a new site." }; + *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new site." }; return NULL; } @@ -778,7 +778,7 @@ static MPMarshalledUser *mpw_marshall_read_json( if (!user->redacted) { // Clear Text 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; } @@ -819,24 +819,24 @@ static MPMarshalledUser *mpw_marshall_read_json( } json_object_put( json_file ); - *error = (MPMarshallError){ .type = MPMarshallSuccess }; + *error = (MPMarshalError){ .type = MPMarshalSuccess }; return user; } #endif -MPMarshallInfo *mpw_marshall_read_info( +MPMarshalInfo *mpw_marshall_read_info( const char *in) { - MPMarshallInfo *info = malloc( sizeof( MPMarshallInfo ) ); - *info = (MPMarshallInfo){ .format = MPMarshallFormatNone }; + MPMarshalInfo *info = malloc( sizeof( MPMarshalInfo ) ); + *info = (MPMarshalInfo){ .format = MPMarshalFormatNone }; if (in && strlen( in )) { if (in[0] == '#') { - *info = (MPMarshallInfo){ .format = MPMarshallFormatFlat }; + *info = (MPMarshalInfo){ .format = MPMarshalFormatFlat }; mpw_marshall_read_flat_info( in, info ); } else if (in[0] == '{') { - *info = (MPMarshallInfo){ .format = MPMarshallFormatJSON }; + *info = (MPMarshalInfo){ .format = MPMarshalFormatJSON }; #if MPW_JSON mpw_marshall_read_json_info( in, info ); #endif @@ -847,29 +847,29 @@ MPMarshallInfo *mpw_marshall_read_info( } 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) { - case MPMarshallFormatNone: - *error = (MPMarshallError){ .type = MPMarshallSuccess }; + case MPMarshalFormatNone: + *error = (MPMarshalError){ .type = MPMarshalSuccess }; return false; - case MPMarshallFormatFlat: + case MPMarshalFormatFlat: return mpw_marshall_read_flat( in, masterPassword, error ); #if MPW_JSON - case MPMarshallFormatJSON: + case MPMarshalFormatJSON: return mpw_marshall_read_json( in, masterPassword, error ); #endif default: - *error = (MPMarshallError){ MPMarshallErrorFormat, mpw_str( "Unsupported input format: %u", inFormat ) }; + *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unsupported input format: %u", inFormat ) }; return NULL; } } -const MPMarshallFormat mpw_formatWithName( +const MPMarshalFormat mpw_formatWithName( const char *formatName) { if (!formatName || !strlen( formatName )) - return MPMarshallFormatNone; + return MPMarshalFormatNone; // Lower-case to standardize it. size_t stdFormatNameSize = strlen( formatName ); @@ -878,26 +878,26 @@ const MPMarshallFormat mpw_formatWithName( stdFormatName[c] = (char)tolower( formatName[c] ); stdFormatName[stdFormatNameSize] = '\0'; - if (strncmp( mpw_nameForFormat( MPMarshallFormatNone ), stdFormatName, strlen( stdFormatName ) ) == 0) - return MPMarshallFormatNone; - if (strncmp( mpw_nameForFormat( MPMarshallFormatFlat ), stdFormatName, strlen( stdFormatName ) ) == 0) - return MPMarshallFormatFlat; - if (strncmp( mpw_nameForFormat( MPMarshallFormatJSON ), stdFormatName, strlen( stdFormatName ) ) == 0) - return MPMarshallFormatJSON; + if (strncmp( mpw_nameForFormat( MPMarshalFormatNone ), stdFormatName, strlen( stdFormatName ) ) == 0) + return MPMarshalFormatNone; + if (strncmp( mpw_nameForFormat( MPMarshalFormatFlat ), stdFormatName, strlen( stdFormatName ) ) == 0) + return MPMarshalFormatFlat; + if (strncmp( mpw_nameForFormat( MPMarshalFormatJSON ), stdFormatName, strlen( stdFormatName ) ) == 0) + return MPMarshalFormatJSON; dbg( "Not a format name: %s\n", stdFormatName ); - return (MPMarshallFormat)ERR; + return (MPMarshalFormat)ERR; } const char *mpw_nameForFormat( - const MPMarshallFormat format) { + const MPMarshalFormat format) { switch (format) { - case MPMarshallFormatNone: + case MPMarshalFormatNone: return "none"; - case MPMarshallFormatFlat: + case MPMarshalFormatFlat: return "flat"; - case MPMarshallFormatJSON: + case MPMarshalFormatJSON: return "json"; default: { dbg( "Unknown format: %d\n", format ); @@ -907,14 +907,14 @@ const char *mpw_nameForFormat( } const char *mpw_marshall_format_extension( - const MPMarshallFormat format) { + const MPMarshalFormat format) { switch (format) { - case MPMarshallFormatNone: + case MPMarshalFormatNone: return NULL; - case MPMarshallFormatFlat: + case MPMarshalFormatFlat: return "mpsites"; - case MPMarshallFormatJSON: + case MPMarshalFormatJSON: return "mpsites.json"; default: { dbg( "Unknown format: %d\n", format ); diff --git a/core/c/mpw-marshall.h b/core/c/mpw-marshall.h index 3a09c9b4..c5996f50 100644 --- a/core/c/mpw-marshall.h +++ b/core/c/mpw-marshall.h @@ -25,41 +25,41 @@ //// Types. -typedef mpw_enum( unsigned int, MPMarshallFormat ) { +typedef mpw_enum( unsigned int, MPMarshalFormat ) { /** Generate a key for authentication. */ - MPMarshallFormatNone, + MPMarshalFormatNone, /** Generate a key for authentication. */ - MPMarshallFormatFlat, + MPMarshalFormatFlat, /** Generate a name for identification. */ - MPMarshallFormatJSON, + MPMarshalFormatJSON, #if MPW_JSON - MPMarshallFormatDefault = MPMarshallFormatJSON, + MPMarshalFormatDefault = MPMarshalFormatJSON, #else - MPMarshallFormatDefault = MPMarshallFormatFlat, + MPMarshalFormatDefault = MPMarshalFormatFlat, #endif }; -typedef mpw_enum( unsigned int, MPMarshallErrorType ) { +typedef mpw_enum( unsigned int, MPMarshalErrorType ) { /** The marshalling operation completed successfully. */ - MPMarshallSuccess, + MPMarshalSuccess, /** An error in the structure of the marshall file interrupted marshalling. */ - MPMarshallErrorStructure, + MPMarshalErrorStructure, /** The marshall file uses an unsupported format version. */ - MPMarshallErrorFormat, + MPMarshalErrorFormat, /** A required value is missing or not specified. */ - MPMarshallErrorMissing, + MPMarshalErrorMissing, /** The given master password is not valid. */ - MPMarshallErrorMasterPassword, + MPMarshalErrorMasterPassword, /** An illegal value was specified. */ - MPMarshallErrorIllegal, + MPMarshalErrorIllegal, /** An internal system error interrupted marshalling. */ - MPMarshallErrorInternal, + MPMarshalErrorInternal, }; -typedef struct MPMarshallError { - MPMarshallErrorType type; +typedef struct MPMarshalError { + MPMarshalErrorType type; const char *description; -} MPMarshallError; +} MPMarshalError; typedef struct MPMarshalledQuestion { const char *keyword; @@ -99,26 +99,26 @@ typedef struct MPMarshalledUser { MPMarshalledSite *sites; } MPMarshalledUser; -typedef struct MPMarshallInfo { - MPMarshallFormat format; +typedef struct MPMarshalInfo { + MPMarshalFormat format; MPAlgorithmVersion algorithm; const char *fullName; const char *keyID; bool redacted; time_t date; -} MPMarshallInfo; +} MPMarshalInfo; //// Marshalling. /** Write the user and all associated data out to the given output buffer using the given marshalling format. */ 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. */ -MPMarshallInfo *mpw_marshall_read_info( +MPMarshalInfo *mpw_marshall_read_info( const char *in); /** Unmarshall sites in the given input buffer by parsing it using the given marshalling format. */ 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. @@ -134,7 +134,7 @@ MPMarshalledQuestion *mpw_marshal_question( MPMarshalledSite *site, const char *keyword); /** Free the given user object and all associated data. */ bool mpw_marshal_info_free( - MPMarshallInfo **info); + MPMarshalInfo **info); bool mpw_marshal_free( MPMarshalledUser **user); @@ -143,17 +143,17 @@ bool mpw_marshal_free( /** * @return The purpose represented by the given name. */ -const MPMarshallFormat mpw_formatWithName( +const MPMarshalFormat mpw_formatWithName( const char *formatName); /** * @return The standard name for the given purpose. */ 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. */ const char *mpw_marshall_format_extension( - const MPMarshallFormat format); + const MPMarshalFormat format); #endif // _MPW_MARSHALL_H diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalFormat.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalFormat.java new file mode 100644 index 00000000..f3216b28 --- /dev/null +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPMarshalFormat.java @@ -0,0 +1,7 @@ +package com.lyndir.masterpassword.model; + +/** + * @author lhunath, 2017-09-20 + */ +public class MPMarshalFormat { +} diff --git a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteMarshaller.java b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteMarshaller.java index 5210c91d..2c5396c6 100644 --- a/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteMarshaller.java +++ b/core/java/model/src/main/java/com/lyndir/masterpassword/model/MPSiteMarshaller.java @@ -17,11 +17,11 @@ import org.joda.time.format.ISODateTimeFormat; */ public class MPSiteMarshaller { - private static final DateTimeFormatter rfc3339 = ISODateTimeFormat.dateTimeNoMillis(); + protected static final DateTimeFormatter rfc3339 = ISODateTimeFormat.dateTimeNoMillis(); private final StringBuilder export = new StringBuilder(); - private ContentMode contentMode = ContentMode.PROTECTED; - private MasterKey masterKey; + private final ContentMode contentMode = ContentMode.PROTECTED; + private final MasterKey masterKey; public static MPSiteMarshaller marshallSafe(final MPUser user) { MPSiteMarshaller marshaller = new MPSiteMarshaller(); @@ -64,7 +64,6 @@ public class MPSiteMarshaller { header.append( "# Full Name: " ).append( user.getFullName() ).append( '\n' ); header.append( "# Avatar: " ).append( user.getAvatar() ).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( "# Default Type: " ).append( user.getDefaultType().getType() ).append( '\n' ); header.append( "# Passwords: " ).append( this.contentMode.name() ).append( '\n' ); diff --git a/platform-darwin/Source/MPAppDelegate_Store.m b/platform-darwin/Source/MPAppDelegate_Store.m index 11330181..df3b39ff 100644 --- a/platform-darwin/Source/MPAppDelegate_Store.m +++ b/platform-darwin/Source/MPAppDelegate_Store.m @@ -566,10 +566,10 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted ); saveInContext:(NSManagedObjectContext *)context { // Read metadata for the import file. - MPMarshallInfo *info = mpw_marshall_read_info( importData.UTF8String ); - if (info->format == MPMarshallFormatNone) - return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshallCode userInfo:@{ - @"type" : @(MPMarshallErrorFormat), + MPMarshalInfo *info = mpw_marshall_read_info( importData.UTF8String ); + if (info->format == MPMarshalFormatNone) + return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{ + @"type" : @(MPMarshalErrorFormat), NSLocalizedDescriptionKey: @"This is not a Master Password import file.", }]), @"While importing sites." ); @@ -589,13 +589,13 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted ); caseInsensitiveCompare:@(info->keyID)] != NSOrderedSame); // Parse import data. - MPMarshallError importError = { .type = MPMarshallSuccess }; + MPMarshalError importError = { .type = MPMarshalSuccess }; MPMarshalledUser *importUser = mpw_marshall_read( importData.UTF8String, info->format, importMasterPassword.UTF8String, &importError ); mpw_marshal_info_free( &info ); @try { - if (!importUser || importError.type != MPMarshallSuccess) - return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshallCode userInfo:@{ + if (!importUser || importError.type != MPMarshalSuccess) + return MPError( ([NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{ @"type" : @(importError.type), NSLocalizedDescriptionKey: @(importError.description), }]), @"While importing sites." ); @@ -735,15 +735,15 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted ); } char *export = NULL; - MPMarshallError exportError = (MPMarshallError){ .type= MPMarshallSuccess }; - mpw_marshall_write( &export, MPMarshallFormatFlat, exportUser, &exportError ); + MPMarshalError exportError = (MPMarshalError){ .type= MPMarshalSuccess }; + mpw_marshall_write( &export, MPMarshalFormatFlat, exportUser, &exportError ); NSString *mpsites = nil; - if (export && exportError.type == MPMarshallSuccess) + if (export && exportError.type == MPMarshalSuccess) mpsites = [NSString stringWithCString:export encoding:NSUTF8StringEncoding]; mpw_free_string( &export ); - resultBlock( mpsites, exportError.type == MPMarshallSuccess? nil: - [NSError errorWithDomain:MPErrorDomain code:MPErrorMarshallCode userInfo:@{ + resultBlock( mpsites, exportError.type == MPMarshalSuccess? nil: + [NSError errorWithDomain:MPErrorDomain code:MPErrorMarshalCode userInfo:@{ @"type" : @(exportError.type), NSLocalizedDescriptionKey: @(exportError.description), }] ); diff --git a/platform-darwin/Source/MPTypes.h b/platform-darwin/Source/MPTypes.h index b0d6b452..a44b5d3d 100644 --- a/platform-darwin/Source/MPTypes.h +++ b/platform-darwin/Source/MPTypes.h @@ -22,7 +22,7 @@ __BEGIN_DECLS extern NSString *const MPErrorDomain; extern NSInteger const MPErrorHangCode; -extern NSInteger const MPErrorMarshallCode; +extern NSInteger const MPErrorMarshalCode; extern NSString *const MPSignedInNotification; extern NSString *const MPSignedOutNotification; diff --git a/platform-darwin/Source/MPTypes.m b/platform-darwin/Source/MPTypes.m index 1556ce9a..9268c755 100644 --- a/platform-darwin/Source/MPTypes.m +++ b/platform-darwin/Source/MPTypes.m @@ -20,7 +20,7 @@ NSString *const MPErrorDomain = @"MPErrorDomain"; NSInteger const MPErrorHangCode = 1; -NSInteger const MPErrorMarshallCode = 1; +NSInteger const MPErrorMarshalCode = 1; NSString *const MPSignedInNotification = @"MPSignedInNotification"; NSString *const MPSignedOutNotification = @"MPSignedOutNotification"; diff --git a/platform-darwin/Source/iOS/MPiOSAppDelegate.m b/platform-darwin/Source/iOS/MPiOSAppDelegate.m index 07a4cc8a..bb5dfc32 100644 --- a/platform-darwin/Source/iOS/MPiOSAppDelegate.m +++ b/platform-darwin/Source/iOS/MPiOSAppDelegate.m @@ -236,8 +236,8 @@ PearlNotMainQueue( ^{ NSString *importData = [UIPasteboard generalPasteboard].string; - MPMarshallInfo *importInfo = mpw_marshall_read_info( importData.UTF8String ); - if (importInfo->format != MPMarshallFormatNone) + MPMarshalInfo *importInfo = mpw_marshall_read_info( importData.UTF8String ); + if (importInfo->format != MPMarshalFormatNone) [PearlAlert showAlertWithTitle:@"Import Sites?" message: @"We've detected Master Password import sites on your pasteboard, would you like to import them?" viewStyle:UIAlertViewStyleDefault initAlert:nil diff --git a/platform-independent/cli-c/cli/mpw-cli.c b/platform-independent/cli-c/cli/mpw-cli.c index 9f159e26..ca50510c 100644 --- a/platform-independent/cli-c/cli/mpw-cli.c +++ b/platform-independent/cli-c/cli/mpw-cli.c @@ -97,7 +97,7 @@ static void usage() { " n, none | No file\n" " f, flat | ~/.mpw.d/Full Name.%s\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( "" " -R redacted Whether to save the mpsites in redacted format or not.\n" " Redaction omits or encrypts any secrets, making the file safe\n" @@ -142,7 +142,7 @@ typedef struct { const char *masterPassword; const char *identicon; const char *siteName; - MPMarshallFormat sitesFormat; + MPMarshalFormat sitesFormat; MPKeyPurpose keyPurpose; const char *keyContext; const char *sitesPath; @@ -191,7 +191,7 @@ int main(const int argc, char *const argv[]) { Operation operation = { .allowPasswordUpdate = false, .sitesFormatFixed = false, - .sitesFormat = MPMarshallFormatDefault, + .sitesFormat = MPMarshalFormatDefault, .keyPurpose = MPKeyPurposeAuthentication, .resultType = MPResultTypeDefault, .siteCounter = MPCounterValueDefault, @@ -453,9 +453,9 @@ void cli_user(Arguments *args, Operation *operation) { // Try to fall back to the flat format. if (!operation->sitesFormatFixed) { 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" ))) - operation->sitesFormat = MPMarshallFormatFlat; + operation->sitesFormat = MPMarshalFormatFlat; else 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 ); // Parse file. - MPMarshallInfo *sitesInputInfo = mpw_marshall_read_info( sitesInputData ); - MPMarshallFormat sitesInputFormat = args->sitesFormat? operation->sitesFormat: sitesInputInfo->format; - MPMarshallError marshallError = { .type = MPMarshallSuccess }; + MPMarshalInfo *sitesInputInfo = mpw_marshall_read_info( sitesInputData ); + MPMarshalFormat sitesInputFormat = args->sitesFormat? operation->sitesFormat: sitesInputInfo->format; + MPMarshalError marshallError = { .type = MPMarshalSuccess }; mpw_marshal_info_free( &sitesInputInfo ); 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. - while (marshallError.type == MPMarshallErrorMasterPassword) { + while (marshallError.type == MPMarshalErrorMasterPassword) { 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" ); @@ -500,14 +500,14 @@ void cli_user(Arguments *args, Operation *operation) { mpw_free_string( &sitesInputData ); // 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 ); cli_free( args, operation ); exit( EX_DATAERR ); } // 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 ); cli_free( args, operation ); exit( EX_DATAERR ); @@ -757,11 +757,11 @@ void cli_mpw(Arguments *args, Operation *operation) { void cli_save(Arguments __unused *args, Operation *operation) { - if (operation->sitesFormat == MPMarshallFormatNone) + if (operation->sitesFormat == MPMarshalFormatNone) return; if (!operation->sitesFormatFixed) - operation->sitesFormat = MPMarshallFormatDefault; + operation->sitesFormat = MPMarshalFormatDefault; operation->sitesPath = mpw_path( operation->user->fullName, mpw_marshall_format_extension( 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; - MPMarshallError marshallError = { .type = MPMarshallSuccess }; - if (!mpw_marshall_write( &buf, operation->sitesFormat, operation->user, &marshallError ) || marshallError.type != MPMarshallSuccess) + MPMarshalError marshallError = { .type = MPMarshalSuccess }; + 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 ); else if (fwrite( buf, sizeof( char ), strlen( buf ), sitesFile ) != strlen( buf ))