2
0

Add identicon to export + fix memory leaks for marshal failures.

This commit is contained in:
Maarten Billemont 2020-01-23 15:57:27 -05:00
parent ccd9763649
commit 2637e9ba99
5 changed files with 167 additions and 34 deletions

View File

@ -587,7 +587,8 @@ void cli_question(Arguments *args, Operation *operation) {
void cli_operation(Arguments *args, Operation *operation) { void cli_operation(Arguments *args, Operation *operation) {
mpw_free_string( &operation->identicon ); mpw_free_string( &operation->identicon );
operation->identicon = mpw_identicon_render( mpw_identicon( operation->user->fullName, operation->masterPassword ) ); operation->user->identicon = mpw_identicon( operation->user->fullName, operation->masterPassword );
operation->identicon = mpw_identicon_render( operation->user->identicon );
if (!operation->site) if (!operation->site)
abort(); abort();
@ -595,9 +596,9 @@ void cli_operation(Arguments *args, Operation *operation) {
switch (operation->keyPurpose) { switch (operation->keyPurpose) {
case MPKeyPurposeAuthentication: { case MPKeyPurposeAuthentication: {
operation->purposeResult = "password"; operation->purposeResult = "password";
operation->siteCounter = operation->site->counter;
operation->resultType = operation->site->resultType; operation->resultType = operation->site->resultType;
operation->resultState = operation->site->resultState? mpw_strdup( operation->site->resultState ): NULL; operation->resultState = operation->site->resultState? mpw_strdup( operation->site->resultState ): NULL;
operation->siteCounter = operation->site->counter;
break; break;
} }
case MPKeyPurposeIdentification: { case MPKeyPurposeIdentification: {

View File

@ -248,6 +248,9 @@ MPIdenticon mpw_identicon(const char *fullName, const char *masterPassword) {
const char *mpw_identicon_encode( const char *mpw_identicon_encode(
MPIdenticon identicon) { MPIdenticon identicon) {
if (identicon.color == MPIdenticonColorUnset)
return "";
return mpw_str( "%hhu:%s%s%s%s", return mpw_str( "%hhu:%s%s%s%s",
identicon.color, identicon.leftArm, identicon.body, identicon.rightArm, identicon.accessory ); identicon.color, identicon.leftArm, identicon.body, identicon.rightArm, identicon.accessory );
} }
@ -256,11 +259,14 @@ MPIdenticon mpw_identicon_encoded(
const char *encoding) { const char *encoding) {
MPIdenticon identicon = MPIdenticonUnset; MPIdenticon identicon = MPIdenticonUnset;
if (!encoding || !strlen( encoding ))
return identicon;
char *string = calloc( strlen( encoding ), sizeof( *string ) ), *parser = string; char *string = calloc( strlen( encoding ), sizeof( *string ) ), *parser = string;
const char *leftArm = NULL, *body = NULL, *rightArm = NULL, *accessory = NULL; const char *leftArm = NULL, *body = NULL, *rightArm = NULL, *accessory = NULL;
unsigned int color; unsigned int color;
if (encoding && string && sscanf( encoding, "%u:%s", &color, string ) == 2) { if (string && sscanf( encoding, "%u:%s", &color, string ) == 2) {
if (*parser && color) if (*parser && color)
for (int s = 0; s < sizeof( mpw_identicon_leftArms ) / sizeof( *mpw_identicon_leftArms ); ++s) { for (int s = 0; s < sizeof( mpw_identicon_leftArms ) / sizeof( *mpw_identicon_leftArms ); ++s) {
const char *limb = mpw_identicon_leftArms[s]; const char *limb = mpw_identicon_leftArms[s];

View File

@ -69,7 +69,7 @@ const char *mpw_site_state(
/** @return An identicon (static) that represents the user's identity. */ /** @return An identicon (static) that represents the user's identity. */
MPIdenticon mpw_identicon( MPIdenticon mpw_identicon(
const char *fullName, const char *masterPassword); const char *fullName, const char *masterPassword);
/** @return An encoded representation (shared) of the given identicon. */ /** @return An encoded representation (shared) of the given identicon or an empty string if the identicon is unset. */
const char *mpw_identicon_encode( const char *mpw_identicon_encode(
MPIdenticon identicon); MPIdenticon identicon);
/** @return An identicon (static) decoded from the given encoded identicon representation or an identicon with empty fields if the identicon could not be parsed. */ /** @return An identicon (static) decoded from the given encoded identicon representation or an identicon with empty fields if the identicon could not be parsed. */

View File

@ -33,12 +33,13 @@ MPMarshalledUser *mpw_marshal_user(
return NULL; return NULL;
*user = (MPMarshalledUser){ *user = (MPMarshalledUser){
.fullName = mpw_strdup( fullName ),
.masterKeyProvider = masterKeyProvider, .masterKeyProvider = masterKeyProvider,
.algorithm = algorithmVersion, .algorithm = algorithmVersion,
.redacted = true, .redacted = true,
.avatar = 0, .avatar = 0,
.fullName = mpw_strdup( fullName ),
.identicon = MPIdenticonUnset,
.defaultType = MPResultTypeDefault, .defaultType = MPResultTypeDefault,
.lastUsed = 0, .lastUsed = 0,
@ -58,13 +59,14 @@ MPMarshalledSite *mpw_marshal_site(
MPMarshalledSite *site = &user->sites[user->sites_count - 1]; MPMarshalledSite *site = &user->sites[user->sites_count - 1];
*site = (MPMarshalledSite){ *site = (MPMarshalledSite){
.name = mpw_strdup( siteName ), .name = mpw_strdup( siteName ),
.resultState = NULL,
.resultType = resultType,
.counter = siteCounter,
.algorithm = algorithmVersion, .algorithm = algorithmVersion,
.counter = siteCounter,
.resultType = resultType,
.resultState = NULL,
.loginState = NULL,
.loginType = MPResultTypeTemplateName, .loginType = MPResultTypeTemplateName,
.loginState = NULL,
.url = NULL, .url = NULL,
.uses = 0, .uses = 0,
@ -87,8 +89,8 @@ MPMarshalledQuestion *mpw_marshal_question(
MPMarshalledQuestion *question = &site->questions[site->questions_count - 1]; MPMarshalledQuestion *question = &site->questions[site->questions_count - 1];
*question = (MPMarshalledQuestion){ *question = (MPMarshalledQuestion){
.keyword = mpw_strdup( keyword ), .keyword = mpw_strdup( keyword ),
.state = NULL,
.type = MPResultTypeTemplatePhrase, .type = MPResultTypeTemplatePhrase,
.state = NULL,
}; };
return question; return question;
} }
@ -162,6 +164,7 @@ static bool mpw_marshal_write_flat(
mpw_string_pushf( out, "# User Name: %s\n", user->fullName ); mpw_string_pushf( out, "# User Name: %s\n", user->fullName );
mpw_string_pushf( out, "# Full Name: %s\n", user->fullName ); mpw_string_pushf( out, "# Full Name: %s\n", user->fullName );
mpw_string_pushf( out, "# Avatar: %u\n", user->avatar ); mpw_string_pushf( out, "# Avatar: %u\n", user->avatar );
mpw_string_pushf( out, "# Identicon: %s\n", mpw_identicon_encode( user->identicon ) );
mpw_string_pushf( out, "# Key ID: %s\n", mpw_id_buf( masterKey, MPMasterKeySize ) ); 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, "# Algorithm: %d\n", user->algorithm );
mpw_string_pushf( out, "# Default Type: %d\n", user->defaultType ); mpw_string_pushf( out, "# Default Type: %d\n", user->defaultType );
@ -245,6 +248,7 @@ static bool mpw_marshal_write_json(
json_object_object_add( json_user, "avatar", json_object_new_int( (int32_t)user->avatar ) ); json_object_object_add( json_user, "avatar", json_object_new_int( (int32_t)user->avatar ) );
json_object_object_add( json_user, "full_name", json_object_new_string( user->fullName ) ); json_object_object_add( json_user, "full_name", json_object_new_string( user->fullName ) );
json_object_object_add( json_user, "identicon", json_object_new_string( mpw_identicon_encode( user->identicon ) ) );
if (strftime( dateString, sizeof( dateString ), "%FT%TZ", gmtime( &user->lastUsed ) )) if (strftime( dateString, sizeof( dateString ), "%FT%TZ", gmtime( &user->lastUsed ) ))
json_object_object_add( json_user, "last_used", json_object_new_string( dateString ) ); json_object_object_add( json_user, "last_used", json_object_new_string( dateString ) );
json_object_object_add( json_user, "key_id", json_object_new_string( mpw_id_buf( masterKey, MPMasterKeySize ) ) ); json_object_object_add( json_user, "key_id", json_object_new_string( mpw_id_buf( masterKey, MPMasterKeySize ) ) );
@ -266,6 +270,7 @@ static bool mpw_marshal_write_json(
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
if (!(masterKey = user->masterKeyProvider( site->algorithm, user->fullName ))) { if (!(masterKey = user->masterKeyProvider( site->algorithm, user->fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
json_object_put( json_file );
return false; return false;
} }
@ -402,6 +407,8 @@ static void mpw_marshal_read_flat_info(
info->avatar = (unsigned int)atoi( headerValue ); info->avatar = (unsigned int)atoi( headerValue );
if (strcmp( headerName, "Full Name" ) == 0 || strcmp( headerName, "User Name" ) == 0) if (strcmp( headerName, "Full Name" ) == 0 || strcmp( headerName, "User Name" ) == 0)
info->fullName = mpw_strdup( headerValue ); info->fullName = mpw_strdup( headerValue );
if (strcmp( headerName, "Identicon" ) == 0)
info->identicon = mpw_identicon_encoded( headerValue );
if (strcmp( headerName, "Key ID" ) == 0) if (strcmp( headerName, "Key ID" ) == 0)
info->keyID = mpw_strdup( headerValue ); info->keyID = mpw_strdup( headerValue );
@ -427,6 +434,7 @@ static MPMarshalledUser *mpw_marshal_read_flat(
unsigned int format = 0, avatar = 0; unsigned int format = 0, avatar = 0;
char *fullName = NULL, *keyID = NULL; char *fullName = NULL, *keyID = NULL;
MPAlgorithmVersion algorithm = MPAlgorithmVersionCurrent; MPAlgorithmVersion algorithm = MPAlgorithmVersionCurrent;
MPIdenticon identicon = MPIdenticonUnset;
MPResultType defaultType = MPResultTypeDefault; MPResultType defaultType = MPResultTypeDefault;
time_t exportDate = 0; time_t exportDate = 0;
bool headerStarted = false, headerEnded = false, importRedacted = false; bool headerStarted = false, headerEnded = false, importRedacted = false;
@ -452,18 +460,28 @@ static MPMarshalledUser *mpw_marshal_read_flat(
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
if (!(masterKey = masterKeyProvider( algorithm, fullName ))) { if (!(masterKey = masterKeyProvider( algorithm, fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
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 = (MPMarshalError){ MPMarshalErrorMasterPassword, "Master password doesn't match key ID." }; *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Master password doesn't match key ID." };
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
if (!user && !(user = mpw_marshal_user( fullName, masterKeyProvider, algorithm ))) { if (!user && !(user = mpw_marshal_user( fullName, masterKeyProvider, algorithm ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new user." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new user." };
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
user->redacted = importRedacted; user->redacted = importRedacted;
user->avatar = avatar; user->avatar = avatar;
user->identicon = identicon;
user->defaultType = defaultType; user->defaultType = defaultType;
user->lastUsed = exportDate; user->lastUsed = exportDate;
continue; continue;
@ -475,6 +493,10 @@ static MPMarshalledUser *mpw_marshal_read_flat(
if (!headerName || !headerValue) { if (!headerName || !headerValue) {
error->type = MPMarshalErrorStructure; error->type = MPMarshalErrorStructure;
error->description = mpw_str( "Invalid header: %s", mpw_strndup( positionInLine, (size_t)(endOfLine - positionInLine) ) ); error->description = mpw_str( "Invalid header: %s", mpw_strndup( positionInLine, (size_t)(endOfLine - positionInLine) ) );
mpw_free_strings( &headerName, &headerValue, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
@ -488,6 +510,10 @@ static MPMarshalledUser *mpw_marshal_read_flat(
int value = atoi( headerValue ); int value = atoi( headerValue );
if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) { if (value < MPAlgorithmVersionFirst || value > MPAlgorithmVersionLast) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user algorithm version: %s", headerValue ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user algorithm version: %s", headerValue ) };
mpw_free_strings( &headerName, &headerValue, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
algorithm = (MPAlgorithmVersion)value; algorithm = (MPAlgorithmVersion)value;
@ -496,12 +522,18 @@ static MPMarshalledUser *mpw_marshal_read_flat(
avatar = (unsigned int)atoi( headerValue ); avatar = (unsigned int)atoi( headerValue );
if (strcmp( headerName, "Full Name" ) == 0 || strcmp( headerName, "User Name" ) == 0) if (strcmp( headerName, "Full Name" ) == 0 || strcmp( headerName, "User Name" ) == 0)
fullName = mpw_strdup( headerValue ); fullName = mpw_strdup( headerValue );
if (strcmp( headerName, "Identicon" ) == 0)
identicon = mpw_identicon_encoded( headerValue );
if (strcmp( headerName, "Key ID" ) == 0) if (strcmp( headerName, "Key ID" ) == 0)
keyID = mpw_strdup( headerValue ); keyID = mpw_strdup( headerValue );
if (strcmp( headerName, "Default Type" ) == 0) { if (strcmp( headerName, "Default Type" ) == 0) {
int value = atoi( headerValue ); int value = atoi( headerValue );
if (!mpw_type_short_name( (MPResultType)value )) { if (!mpw_type_short_name( (MPResultType)value )) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user default type: %s", headerValue ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user default type: %s", headerValue ) };
mpw_free_strings( &headerName, &headerValue, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
defaultType = (MPResultType)value; defaultType = (MPResultType)value;
@ -514,6 +546,9 @@ static MPMarshalledUser *mpw_marshal_read_flat(
continue; continue;
if (!fullName) { if (!fullName) {
*error = (MPMarshalError){ MPMarshalErrorMissing, "Missing header: Full Name" }; *error = (MPMarshalError){ MPMarshalErrorMissing, "Missing header: Full Name" };
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
if (positionInLine >= endOfLine) if (positionInLine >= endOfLine)
@ -532,7 +567,7 @@ static MPMarshalledUser *mpw_marshal_read_flat(
str_algorithm = mpw_strdup( strtok( NULL, "" ) ); str_algorithm = mpw_strdup( strtok( NULL, "" ) );
mpw_free_string( &typeAndVersion ); mpw_free_string( &typeAndVersion );
} }
str_counter = mpw_strdup( "1" ); str_counter = mpw_strdup( mpw_str( "%u", MPCounterValueDefault ) );
siteLoginState = NULL; siteLoginState = NULL;
siteName = mpw_get_token( &positionInLine, endOfLine, "\t\n" ); siteName = mpw_get_token( &positionInLine, endOfLine, "\t\n" );
siteResultState = mpw_get_token( &positionInLine, endOfLine, "\n" ); siteResultState = mpw_get_token( &positionInLine, endOfLine, "\n" );
@ -555,6 +590,9 @@ static MPMarshalledUser *mpw_marshal_read_flat(
} }
default: { default: {
*error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unexpected import format: %u", format ) }; *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unexpected import format: %u", format ) };
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
} }
@ -563,30 +601,54 @@ static MPMarshalledUser *mpw_marshal_read_flat(
MPResultType siteType = (MPResultType)atoi( str_type ); MPResultType siteType = (MPResultType)atoi( str_type );
if (!mpw_type_short_name( siteType )) { if (!mpw_type_short_name( siteType )) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site type: %s: %s", siteName, str_type ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site type: %s: %s", siteName, str_type ) };
mpw_free_strings( &str_lastUsed, &str_uses, &str_type, &str_algorithm, &str_counter, NULL );
mpw_free_strings( &siteLoginState, &siteName, &siteResultState, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
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 = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site counter: %s: %s", siteName, str_counter ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site counter: %s: %s", siteName, str_counter ) };
mpw_free_strings( &str_lastUsed, &str_uses, &str_type, &str_algorithm, &str_counter, NULL );
mpw_free_strings( &siteLoginState, &siteName, &siteResultState, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
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 = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site algorithm: %s: %s", siteName, str_algorithm ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site algorithm: %s: %s", siteName, str_algorithm ) };
mpw_free_strings( &str_lastUsed, &str_uses, &str_type, &str_algorithm, &str_counter, NULL );
mpw_free_strings( &siteLoginState, &siteName, &siteResultState, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value; MPAlgorithmVersion siteAlgorithm = (MPAlgorithmVersion)value;
time_t siteLastUsed = mpw_timegm( str_lastUsed ); time_t siteLastUsed = mpw_timegm( str_lastUsed );
if (!siteLastUsed) { if (!siteLastUsed) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, 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 ) };
mpw_free_strings( &str_lastUsed, &str_uses, &str_type, &str_algorithm, &str_counter, NULL );
mpw_free_strings( &siteLoginState, &siteName, &siteResultState, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
MPMarshalledSite *site = mpw_marshal_site( MPMarshalledSite *site = mpw_marshal_site( user, siteName, siteType, siteCounter, siteAlgorithm );
user, siteName, siteType, siteCounter, siteAlgorithm );
if (!site) { if (!site) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new site." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new site." };
mpw_free_strings( &str_lastUsed, &str_uses, &str_type, &str_algorithm, &str_counter, NULL );
mpw_free_strings( &siteLoginState, &siteName, &siteResultState, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
@ -597,6 +659,11 @@ static MPMarshalledUser *mpw_marshal_read_flat(
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
if (!(masterKey = masterKeyProvider( site->algorithm, user->fullName ))) { if (!(masterKey = masterKeyProvider( site->algorithm, user->fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
mpw_free_strings( &str_lastUsed, &str_uses, &str_type, &str_algorithm, &str_counter, NULL );
mpw_free_strings( &siteLoginState, &siteName, &siteResultState, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
@ -620,6 +687,11 @@ static MPMarshalledUser *mpw_marshal_read_flat(
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, siteLoginState, siteName ); str_lastUsed, str_uses, str_type, str_algorithm, str_counter, siteLoginState, siteName );
mpw_free_strings( &str_lastUsed, &str_uses, &str_type, &str_algorithm, &str_counter, NULL );
mpw_free_strings( &siteLoginState, &siteName, &siteResultState, NULL );
mpw_free_strings( &fullName, &keyID, NULL );
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
return NULL; return NULL;
} }
@ -655,6 +727,7 @@ static void mpw_marshal_read_json_info(
info->algorithm = (MPAlgorithmVersion)mpw_get_json_int( json_file, "user.algorithm", MPAlgorithmVersionCurrent ); info->algorithm = (MPAlgorithmVersion)mpw_get_json_int( json_file, "user.algorithm", MPAlgorithmVersionCurrent );
info->avatar = (unsigned int)mpw_get_json_int( json_file, "user.avatar", 0 ); info->avatar = (unsigned int)mpw_get_json_int( json_file, "user.avatar", 0 );
info->fullName = mpw_strdup( mpw_get_json_string( json_file, "user.full_name", NULL ) ); info->fullName = mpw_strdup( mpw_get_json_string( json_file, "user.full_name", NULL ) );
info->identicon = mpw_identicon_encoded( mpw_get_json_string( json_file, "user.identicon", NULL ) );
info->keyID = mpw_strdup( mpw_get_json_string( json_file, "user.key_id", NULL ) ); info->keyID = mpw_strdup( mpw_get_json_string( json_file, "user.key_id", NULL ) );
info->lastUsed = mpw_timegm( mpw_get_json_string( json_file, "user.last_used", NULL ) ); info->lastUsed = mpw_timegm( mpw_get_json_string( json_file, "user.last_used", NULL ) );
@ -676,6 +749,7 @@ static MPMarshalledUser *mpw_marshal_read_json(
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 = (MPMarshalError){ MPMarshalErrorStructure, 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 ) ) };
json_object_put( json_file );
return NULL; return NULL;
} }
@ -687,49 +761,75 @@ static MPMarshalledUser *mpw_marshal_read_json(
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 = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unsupported format: %u", fileFormat ) }; *error = (MPMarshalError){ MPMarshalErrorFormat, mpw_str( "Unsupported format: %u", fileFormat ) };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
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 );
// Section: "user" // Section: "user"
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 );
const char *str_lastUsed = mpw_get_json_string( json_file, "user.last_used", 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 = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user algorithm version: %u", value ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user algorithm version: %u", value ) };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL; return NULL;
} }
MPAlgorithmVersion algorithm = (MPAlgorithmVersion)value; MPAlgorithmVersion algorithm = (MPAlgorithmVersion)value;
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 );
if (!fullName || !strlen( fullName )) {
*error = (MPMarshalError){ MPMarshalErrorMissing, "Missing value for full name." };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL;
}
MPIdenticon identicon = mpw_identicon_encoded( mpw_get_json_string( json_file, "user.identicon", NULL ) );
const char *keyID = mpw_get_json_string( json_file, "user.key_id", NULL );
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_type_short_name( defaultType )) { if (!mpw_type_short_name( defaultType )) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user default type: %u", defaultType ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user default type: %u", defaultType ) };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL; return NULL;
} }
const char *str_lastUsed = mpw_get_json_string( json_file, "user.last_used", NULL );
time_t lastUsed = mpw_timegm( str_lastUsed ); time_t lastUsed = mpw_timegm( str_lastUsed );
if (!lastUsed) { if (!lastUsed) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user last used: %s", str_lastUsed ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user last used: %s", str_lastUsed ) };
return NULL; mpw_free( &masterKey, MPMasterKeySize );
} mpw_marshal_free( &user );
if (!fullName || !strlen( fullName )) { json_object_put( json_file );
*error = (MPMarshalError){ MPMarshalErrorMissing, "Missing value for full name." };
return NULL; return NULL;
} }
if (!(masterKey = masterKeyProvider( algorithm, fullName ))) { if (!(masterKey = masterKeyProvider( algorithm, fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
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 = (MPMarshalError){ MPMarshalErrorMasterPassword, "Master password doesn't match key ID." }; *error = (MPMarshalError){ MPMarshalErrorMasterPassword, "Master password doesn't match key ID." };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL; return NULL;
} }
if (!(user = mpw_marshal_user( fullName, masterKeyProvider, algorithm ))) { if (!(user = mpw_marshal_user( fullName, masterKeyProvider, algorithm ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new user." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new user." };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL; return NULL;
} }
user->redacted = fileRedacted; user->redacted = fileRedacted;
user->avatar = avatar; user->avatar = avatar;
user->identicon = identicon;
user->defaultType = defaultType; user->defaultType = defaultType;
user->lastUsed = lastUsed; user->lastUsed = lastUsed;
@ -741,28 +841,47 @@ static MPMarshalledUser *mpw_marshal_read_json(
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 = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site algorithm version: %s: %d", siteName, value ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site algorithm version: %s: %d", siteName, value ) };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
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 ); value = mpw_get_json_int( json_site.val, "counter", MPCounterValueDefault );
if (!mpw_type_short_name( 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) { if (value < MPCounterValueFirst || value > MPCounterValueLast) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site counter: %s: %d", siteName, value ) }; *error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site counter: %s: %d", siteName, value ) };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL; return NULL;
} }
MPCounterValue siteCounter = (MPCounterValue)value; MPCounterValue siteCounter = (MPCounterValue)value;
MPResultType siteType = (MPResultType)mpw_get_json_int( json_site.val, "type", (int32_t)user->defaultType );
if (!mpw_type_short_name( siteType )) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site type: %s: %u", siteName, siteType ) };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL;
}
const char *siteResultState = mpw_get_json_string( json_site.val, "password", NULL ); const char *siteResultState = mpw_get_json_string( json_site.val, "password", NULL );
const char *siteLoginState = mpw_get_json_string( json_site.val, "login_name", NULL );
MPResultType siteLoginType = (MPResultType)mpw_get_json_int( json_site.val, "login_type", MPResultTypeTemplateName ); MPResultType siteLoginType = (MPResultType)mpw_get_json_int( json_site.val, "login_type", MPResultTypeTemplateName );
if (!mpw_type_short_name( siteLoginType )) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid site login type: %s: %u", siteName, siteLoginType ) };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL;
}
const char *siteLoginState = mpw_get_json_string( json_site.val, "login_name", NULL );
unsigned int siteUses = (unsigned int)mpw_get_json_int( json_site.val, "uses", 0 ); unsigned int siteUses = (unsigned int)mpw_get_json_int( json_site.val, "uses", 0 );
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_timegm( str_lastUsed ); time_t siteLastUsed = mpw_timegm( str_lastUsed );
if (!siteLastUsed) { if (!siteLastUsed) {
*error = (MPMarshalError){ MPMarshalErrorIllegal, 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 ) };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL; return NULL;
} }
@ -772,6 +891,9 @@ static MPMarshalledUser *mpw_marshal_read_json(
MPMarshalledSite *site = mpw_marshal_site( user, siteName, siteType, siteCounter, siteAlgorithm ); MPMarshalledSite *site = mpw_marshal_site( user, siteName, siteType, siteCounter, siteAlgorithm );
if (!site) { if (!site) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new site." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't allocate a new site." };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL; return NULL;
} }
@ -784,6 +906,9 @@ static MPMarshalledUser *mpw_marshal_read_json(
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
if (!(masterKey = masterKeyProvider( site->algorithm, user->fullName ))) { if (!(masterKey = masterKeyProvider( site->algorithm, user->fullName ))) {
*error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." }; *error = (MPMarshalError){ MPMarshalErrorInternal, "Couldn't derive master key." };
mpw_free( &masterKey, MPMasterKeySize );
mpw_marshal_free( &user );
json_object_put( json_file );
return NULL; return NULL;
} }
@ -824,8 +949,8 @@ static MPMarshalledUser *mpw_marshal_read_json(
} }
} }
} }
json_object_put( json_file );
mpw_free( &masterKey, MPMasterKeySize ); mpw_free( &masterKey, MPMasterKeySize );
json_object_put( json_file );
*error = (MPMarshalError){ .type = MPMarshalSuccess }; *error = (MPMarshalError){ .type = MPMarshalSuccess };
return user; return user;

View File

@ -68,20 +68,20 @@ typedef struct MPMarshalError {
typedef struct MPMarshalledQuestion { typedef struct MPMarshalledQuestion {
const char *keyword; const char *keyword;
const char *state;
MPResultType type; MPResultType type;
const char *state;
} MPMarshalledQuestion; } MPMarshalledQuestion;
typedef struct MPMarshalledSite { typedef struct MPMarshalledSite {
const char *name; const char *name;
MPAlgorithmVersion algorithm; MPAlgorithmVersion algorithm;
const char *resultState;
MPResultType resultType;
MPCounterValue counter; MPCounterValue counter;
const char *loginState; MPResultType resultType;
const char *resultState;
MPResultType loginType; MPResultType loginType;
const char *loginState;
const char *url; const char *url;
unsigned int uses; unsigned int uses;
@ -98,6 +98,7 @@ typedef struct MPMarshalledUser {
unsigned int avatar; unsigned int avatar;
const char *fullName; const char *fullName;
MPIdenticon identicon;
MPResultType defaultType; MPResultType defaultType;
time_t lastUsed; time_t lastUsed;