@@ -707,7 +707,7 @@ sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv)
707707}
708708
709709SQLITE_PRIVATE int
710- sqlite3mcConfigureFromUri (sqlite3 * db , const char * zDbName , int configDefault )
710+ sqlite3mcConfigureFromUri (sqlite3 * db , const char * zDbName , int configDefault )
711711{
712712 int rc = SQLITE_OK ;
713713
@@ -717,6 +717,15 @@ sqlite3mcConfigureFromUri(sqlite3* db, const char *zDbName, int configDefault)
717717 {
718718 /* Check whether cipher is specified */
719719 const char * cipherName = sqlite3_uri_parameter (dbFileName , "cipher" );
720+ if (cipherName == NULL )
721+ {
722+ int defaultCipherIndex = sqlite3mc_config (db , "cipher" , -1 );
723+ if (defaultCipherIndex > 0 )
724+ {
725+ cipherName = sqlite3mc_cipher_name (defaultCipherIndex );
726+ sqlite3mc_config (db , "cipher" , defaultCipherIndex );
727+ }
728+ }
720729 if (cipherName != NULL )
721730 {
722731 int j = 0 ;
@@ -929,6 +938,69 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
929938 ((char * * )pArg )[0 ] = sqlite3_mprintf ("%d" , value );
930939 rc = SQLITE_OK ;
931940 }
941+ else if (sqlite3StrICmp (pragmaName , "cipher_salt" ) == 0 )
942+ {
943+ Codec * codec = sqlite3mcGetCodec (db , (zDbName ) ? zDbName : "main" );
944+ if (codec == NULL )
945+ {
946+ /* Codec not yet set up */
947+ if (pragmaValue && * pragmaValue != 0 )
948+ {
949+ /* Save given cipher salt */
950+ if (sqlite3Strlen30 (pragmaValue ) >= 2 * KEYSALT_LENGTH &&
951+ sqlite3mcIsHexKey (pragmaValue , 2 * KEYSALT_LENGTH ))
952+ {
953+ char * cipherSalt = sqlite3_mprintf ("%s" , pragmaValue );
954+ if (sqlite3_set_clientdata (db , "sqlite3mc_cipher_salt" , cipherSalt , sqlite3_free ) != SQLITE_OK )
955+ {
956+ ((char * * )pArg )[0 ] = sqlite3_mprintf ("Out of memory. Cipher salt not saved." );
957+ rc = SQLITE_ERROR ;
958+ }
959+ else
960+ {
961+ ((char * * )pArg )[0 ] = sqlite3_mprintf ("ok" );
962+ rc = SQLITE_OK ;
963+ }
964+ }
965+ else
966+ {
967+ ((char * * )pArg )[0 ] = sqlite3_mprintf ("Invalid cipher salt. Length < %d or invalid hex digits." , 2 * KEYSALT_LENGTH );
968+ rc = SQLITE_ERROR ;
969+ }
970+ }
971+ else
972+ {
973+ char * cipherSalt = sqlite3_get_clientdata (db , "sqlite3mc_cipher_salt" );
974+ if (cipherSalt )
975+ {
976+ ((char * * )pArg )[0 ] = sqlite3_mprintf ("%s" , cipherSalt );
977+ }
978+ }
979+ }
980+ else if (sqlite3mcIsEncrypted (codec ) && sqlite3mcHasWriteCipher (codec ))
981+ {
982+ /* Database encrypted */
983+ if (pragmaValue && * pragmaValue != 0 )
984+ {
985+ ((char * * )pArg )[0 ] = sqlite3_mprintf ("Cipher salt can't be changed." );
986+ rc = SQLITE_ERROR ;
987+ }
988+ else
989+ {
990+ char * cipherSalt = (char * ) sqlite3mc_codec_data (db , (zDbName ) ? zDbName : "main" , "cipher_salt" );
991+ if (cipherSalt )
992+ {
993+ ((char * * )pArg )[0 ] = cipherSalt ;
994+ }
995+ rc = SQLITE_OK ;
996+ }
997+ }
998+ else
999+ {
1000+ ((char * * )pArg )[0 ] = sqlite3_mprintf ("Database not encrypted." );
1001+ rc = SQLITE_ERROR ;
1002+ }
1003+ }
9321004 else if (sqlite3StrICmp (pragmaName , "key" ) == 0 )
9331005 {
9341006 rc = sqlite3_key_v2 (db , zDbName , pragmaValue , -1 );
0 commit comments