2
0

Fix a NUL termination issue with aes decryption & add standard trace debugging.

This commit is contained in:
Maarten Billemont 2017-08-05 23:42:47 -04:00
parent e15d01882f
commit 2b660adf00
3 changed files with 13 additions and 11 deletions

View File

@ -156,6 +156,9 @@ static const char *mpw_sitePassword_v0(
const char *mpw_encrypt_v0( const char *mpw_encrypt_v0(
MPMasterKey masterKey, const char *plainText) { MPMasterKey masterKey, const char *plainText) {
trc( "-- mpw_encrypt_v0\n" );
trc( "plainText: %s = %s\n", plainText, mpw_hex( plainText, sizeof( plainText ) ) );
// Encrypt // Encrypt
size_t bufSize = strlen( plainText ); size_t bufSize = strlen( plainText );
const uint8_t *cipherBuf = mpw_aes_encrypt( masterKey, MPMasterKeySize, (const uint8_t *)plainText, bufSize ); const uint8_t *cipherBuf = mpw_aes_encrypt( masterKey, MPMasterKeySize, (const uint8_t *)plainText, bufSize );
@ -163,6 +166,7 @@ const char *mpw_encrypt_v0(
err( "AES encryption error: %s\n", strerror( errno ) ); err( "AES encryption error: %s\n", strerror( errno ) );
return NULL; return NULL;
} }
trc( "cipherBuf: %lu bytes = %s\n", bufSize, mpw_hex( cipherBuf, bufSize ) );
// Base64-encode // Base64-encode
@ -173,6 +177,7 @@ const char *mpw_encrypt_v0(
mpw_free_string( cipherText ); mpw_free_string( cipherText );
cipherText = NULL; cipherText = NULL;
} }
trc( "b64 encoded -> cipherText: %s = %s\n", cipherText, mpw_hex( cipherText, sizeof( cipherText ) ) );
mpw_free( cipherBuf, bufSize ); mpw_free( cipherBuf, bufSize );
return cipherText; return cipherText;
@ -181,6 +186,9 @@ const char *mpw_encrypt_v0(
const char *mpw_decrypt_v0( const char *mpw_decrypt_v0(
MPMasterKey masterKey, const char *cipherText) { MPMasterKey masterKey, const char *cipherText) {
trc( "-- mpw_decrypt_v0\n" );
trc( "cipherText: %s = %s\n", cipherText, mpw_hex( cipherText, sizeof( cipherText ) ) );
// Base64-decode // Base64-decode
size_t bufSize = mpw_base64_decode_max( cipherText ); size_t bufSize = mpw_base64_decode_max( cipherText );
uint8_t *cipherBuf = calloc( 1, bufSize ); uint8_t *cipherBuf = calloc( 1, bufSize );
@ -189,12 +197,15 @@ const char *mpw_decrypt_v0(
mpw_free( cipherBuf, mpw_base64_decode_max( cipherText ) ); mpw_free( cipherBuf, mpw_base64_decode_max( cipherText ) );
return NULL; return NULL;
} }
trc( "b64 decoded: %lu bytes = %s\n", bufSize, mpw_hex( cipherBuf, bufSize ) );
// Decrypt // Decrypt
const char *plainText = (const char *)mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, bufSize ); const uint8_t *plainBytes = mpw_aes_decrypt( masterKey, MPMasterKeySize, cipherBuf, bufSize );
const char *plainText = strndup( (char *)plainBytes, bufSize );
mpw_free( plainBytes, bufSize );
if (!plainText) if (!plainText)
err( "AES decryption error: %s\n", strerror( errno ) ); err( "AES decryption error: %s\n", strerror( errno ) );
trc( "decrypted -> plainText: %s = %s\n", plainText, mpw_hex( plainText, sizeof( plainText ) ) );
mpw_free( cipherBuf, bufSize ); mpw_free( cipherBuf, bufSize );
return plainText; return plainText;

View File

@ -391,10 +391,3 @@ const size_t mpw_utf8_strlen(const char *utf8String) {
return charlen; return charlen;
} }
void printb(const void *p, size_t size) {
for (int i = 0; i < size; ++i)
dbg( "%02hhX ", ((const uint8_t *)p)[i] );
dbg( "\n" );
}

View File

@ -174,6 +174,4 @@ const char *mpw_identicon(const char *fullName, const char *masterPassword);
/** @return The amount of display characters in the given UTF-8 string. */ /** @return The amount of display characters in the given UTF-8 string. */
const size_t mpw_utf8_strlen(const char *utf8String); const size_t mpw_utf8_strlen(const char *utf8String);
void printb(const void *p, size_t size);
#endif // _MPW_UTIL_H #endif // _MPW_UTIL_H