1.修复不同DB的全局加密参数相互影响问题 2.修复attach默认加密参数不生效

Signed-off-by: ryne3366 <chuanrui123@126.com>
This commit is contained in:
lxlxlxl 2024-11-07 21:47:57 +08:00 committed by wanghaishuo
parent 7f7c52ec94
commit 9f6c9046da

View File

@ -17004,6 +17004,17 @@ SQLITE_PRIVATE void sqlite3CryptFunc(sqlite3_context*,int,sqlite3_value**);
typedef void (*sqlite3_xDropTableHandle)(sqlite3*, const char*, const char*);
#if defined(SQLITE_HAS_CODEC) && defined(SQLITE_CODEC_ATTACH_CHANGED)
typedef struct CodecParameter {
int kdfIter;
int pageSize;
u8 cipher;
u8 hmacAlgo;
u8 kdfAlgo;
u8 reserved;
} CodecParameter;
#endif
/*
** Each database connection is an instance of the following structure.
*/
@ -17153,6 +17164,9 @@ struct sqlite3 {
char *mDropSchemaName;
sqlite3_xDropTableHandle xDropTableHandle; /* User drop table callback */
#endif
#if defined(SQLITE_HAS_CODEC) && defined(SQLITE_CODEC_ATTACH_CHANGED)
CodecParameter codecParm;
#endif
};
/*
@ -20128,6 +20142,9 @@ SQLITE_PRIVATE int sqlite3CodecQueryParameters(sqlite3*,const char*,const char
#else
# define sqlite3CodecQueryParameters(A,B,C) 0
#endif
#if defined(SQLITE_HAS_CODEC) && defined(SQLITE_CODEC_ATTACH_CHANGED)
SQLITE_PRIVATE void sqlite3CodecResetParameters(CodecParameter *p);
#endif
SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
#ifdef SQLITE_UNTESTABLE
@ -67209,9 +67226,9 @@ SQLITE_PRIVATE int sqlite3WalFrames(
WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
return rc;
}
#ifdef LOG_DUMP
static sqlite3_int64 g_lastCkptTime = 0;
#endif
/*
** This routine is called to implement sqlite3_wal_checkpoint() and
** related interfaces.
@ -176979,8 +176996,14 @@ opendb_out:
sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
}
#endif
#if defined(SQLITE_HAS_CODEC)
if( rc==SQLITE_OK ) sqlite3CodecQueryParameters(db, 0, zOpen);
if( rc==SQLITE_OK ) {
#ifdef SQLITE_CODEC_ATTACH_CHANGED
sqlite3CodecResetParameters(&db->codecParm);
#endif
sqlite3CodecQueryParameters(db, 0, zOpen);
}
#endif
sqlite3_free_filename(zOpen);
return rc;
@ -245165,28 +245188,31 @@ CODEC_STATIC int sqlite3CodecSetIter(KeyContext *keyCtx, int iter){
#define CIPHER_NAME_AES_256_CBC "aes-256-cbc"
#define CIPHER_NAME_AES_256_GCM "aes-256-gcm"
static int g_defaultAttachKdfIter = 10000;
static int g_defaultAttachCipher = CIPHER_ID_AES_256_GCM;
static int g_defaultAttachHmacAlgo = DEFAULT_HMAC_ALGORITHM;
static int g_defaultAttachKdfAlgo = DEFAULT_KDF_ALGORITHM;
static int g_defaultAttachPageSize = DEFAULT_PAGE_SIZE;
struct CodecCipherNameId {
int cipherId;
const char *cipherName;
};
struct CodecCipherNameId g_cipherNameIdMap[CIPHER_TOTAL_NUM] = {
static const struct CodecCipherNameId g_cipherNameIdMap[CIPHER_TOTAL_NUM] = {
{ CIPHER_ID_AES_256_CBC, CIPHER_NAME_AES_256_CBC },
{ CIPHER_ID_AES_256_GCM, CIPHER_NAME_AES_256_GCM }
};
CODEC_STATIC void sqlite3CodecSetDefaultAttachCipher(const char *cipherName){
SQLITE_PRIVATE void sqlite3CodecResetParameters(CodecParameter *p)
{
p->kdfIter = DEFAULT_ITER;
p->pageSize = DEFAULT_PAGE_SIZE;
p->cipher = CIPHER_ID_AES_256_GCM;
p->hmacAlgo = DEFAULT_HMAC_ALGORITHM;
p->kdfAlgo = DEFAULT_KDF_ALGORITHM;
}
CODEC_STATIC void sqlite3CodecSetDefaultAttachCipher(CodecParameter *parm, const char *cipherName){
int i;
for( i=0; i<CIPHER_TOTAL_NUM; i++ ){
if( sqlite3StrICmp(cipherName, g_cipherNameIdMap[i].cipherName)==0 ){
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
g_defaultAttachCipher = g_cipherNameIdMap[i].cipherId;
parm->cipher = g_cipherNameIdMap[i].cipherId;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
return;
}
@ -245194,67 +245220,67 @@ CODEC_STATIC void sqlite3CodecSetDefaultAttachCipher(const char *cipherName){
sqlite3_log(SQLITE_WARNING, "invalid attach cipher algorithm");
}
CODEC_STATIC const char *sqlite3CodecGetDefaultAttachCipher(){
CODEC_STATIC const char *sqlite3CodecGetDefaultAttachCipher(CodecParameter *parm){
const char *attachedCipher = CIPHER_NAME_AES_256_GCM;
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
if( (g_defaultAttachCipher>=0) && (g_defaultAttachCipher<CIPHER_TOTAL_NUM) ){
attachedCipher = g_cipherNameIdMap[g_defaultAttachCipher].cipherName;
if( (parm->cipher>=0) && (parm->cipher<CIPHER_TOTAL_NUM) ){
attachedCipher = g_cipherNameIdMap[parm->cipher].cipherName;
}
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
return attachedCipher;
}
CODEC_STATIC void sqlite3CodecSetDefaultAttachKdfIter(int iter){
CODEC_STATIC void sqlite3CodecSetDefaultAttachKdfIter(CodecParameter *parm, int iter){
if( iter<=0 ){
return;
}
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
g_defaultAttachKdfIter = iter;
parm->kdfIter = iter;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
}
CODEC_STATIC int sqlite3CodecGetDefaultAttachKdfIter(){
CODEC_STATIC int sqlite3CodecGetDefaultAttachKdfIter(CodecParameter *parm){
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
int iterNum = g_defaultAttachKdfIter;
int iterNum = parm->kdfIter;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
return iterNum;
}
CODEC_STATIC void sqlite3CodecSetDefaultAttachHmacAlgo(int hmacAlgo){
CODEC_STATIC void sqlite3CodecSetDefaultAttachHmacAlgo(CodecParameter *parm, int hmacAlgo){
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
g_defaultAttachHmacAlgo = hmacAlgo;
parm->hmacAlgo = hmacAlgo;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
}
CODEC_STATIC int sqlite3CodecGetDefaultAttachHmacAlgo(){
CODEC_STATIC int sqlite3CodecGetDefaultAttachHmacAlgo(CodecParameter *parm){
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
int hmacAlgo = g_defaultAttachHmacAlgo;
int hmacAlgo = parm->hmacAlgo;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
return hmacAlgo;
}
CODEC_STATIC void sqlite3CodecSetDefaultAttachKdfAlgo(int kdfAlgo){
CODEC_STATIC void sqlite3CodecSetDefaultAttachKdfAlgo(CodecParameter *parm, int kdfAlgo){
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
g_defaultAttachKdfAlgo = kdfAlgo;
parm->kdfAlgo = kdfAlgo;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
}
CODEC_STATIC int sqlite3CodecGetDefaultAttachKdfAlgo(){
CODEC_STATIC int sqlite3CodecGetDefaultAttachKdfAlgo(CodecParameter *parm){
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
int kdfAlgo = g_defaultAttachKdfAlgo;
int kdfAlgo = parm->kdfAlgo;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
return kdfAlgo;
}
CODEC_STATIC void sqlite3CodecSetDefaultAttachPageSize(int pageSize){
CODEC_STATIC void sqlite3CodecSetDefaultAttachPageSize(CodecParameter *parm, int pageSize){
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
g_defaultAttachPageSize = pageSize;
parm->pageSize = pageSize;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
}
CODEC_STATIC int sqlite3CodecGetDefaultAttachPageSize(){
CODEC_STATIC int sqlite3CodecGetDefaultAttachPageSize(CodecParameter *parm){
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
int pageSize = g_defaultAttachPageSize;
int pageSize = parm->pageSize;
sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
return pageSize;
}
@ -245341,8 +245367,7 @@ CODEC_STATIC int sqlite3CodecCopyKeyContext(KeyContext *input, KeyContext *outpu
// You should clear key context before you call this function
#ifdef SQLITE_CODEC_ATTACH_CHANGED
CODEC_STATIC int sqlite3CodecInitKeyContext(CodecContext *ctx, Btree *p, const void *zKey, int nKey, int attachFlag,
int hmacAlgo){
CODEC_STATIC int sqlite3CodecInitKeyContext(CodecContext *ctx, Btree *p, const void *zKey, int nKey, int attachFlag){
#else
CODEC_STATIC int sqlite3CodecInitKeyContext(CodecContext *ctx, Btree *p, const void *zKey, int nKey){
#endif
@ -245350,16 +245375,18 @@ CODEC_STATIC int sqlite3CodecInitKeyContext(CodecContext *ctx, Btree *p, const v
KeyContext *keyCtx = ctx->readCtx;
#ifdef SQLITE_CODEC_ATTACH_CHANGED
if( attachFlag!=0 ){
rc = sqlite3CodecSetCodecConstant(keyCtx, sqlite3CodecGetDefaultAttachCipher());
rc += sqlite3CodecSetIter(keyCtx, sqlite3CodecGetDefaultAttachKdfIter());
CodecParameter *parm = &p->db->codecParm;
int hmacAlgo = sqlite3CodecGetDefaultAttachHmacAlgo(parm);
rc = sqlite3CodecSetCodecConstant(keyCtx, sqlite3CodecGetDefaultAttachCipher(parm));
rc += sqlite3CodecSetIter(keyCtx, sqlite3CodecGetDefaultAttachKdfIter(parm));
if( hmacAlgo!=0 ){
rc += sqlite3CodecSetHmacAlgorithm(keyCtx, hmacAlgo);
}
int attachKdfAlgo = sqlite3CodecGetDefaultAttachKdfAlgo();
int attachKdfAlgo = sqlite3CodecGetDefaultAttachKdfAlgo(parm);
if( attachKdfAlgo!=0 ){
rc += sqlite3CodecSetKdfAlgorithm(keyCtx, attachKdfAlgo);
}
int cipherPageSize = sqlite3CodecGetDefaultAttachPageSize();
int cipherPageSize = sqlite3CodecGetDefaultAttachPageSize(parm);
if( cipherPageSize!=0 ){
rc += sqlite3CodecSetCipherPageSize(ctx, cipherPageSize);
if ( rc != SQLITE_OK ) {
@ -245413,31 +245440,32 @@ CODEC_STATIC void sqlite3CodecFreeContext(CodecContext *ctx){
#ifdef SQLITE_CODEC_ATTACH_CHANGED
CODEC_STATIC int sqlite3CodecInitContext(CodecContext *ctx, Btree *p, const void *zKey, int nKey, int nDb){
int attachFlag = (nDb > 1) ? 1 : 0;
int defaultPageSz = attachFlag ? p->db->codecParm.pageSize : DEFAULT_PAGE_SIZE;
#else
CODEC_STATIC int sqlite3CodecInitContext(CodecContext *ctx, Btree *p, const void *zKey, int nKey){
int defaultPageSz = DEFAULT_PAGE_SIZE;
#endif
sqlite3_file *fd = p->pBt->pPager->fd;
ctx->pBt = p;
ctx->savePassword = 0;
ctx->buffer = (unsigned char *)sqlite3Malloc(DEFAULT_PAGE_SIZE);
ctx->buffer = (unsigned char *)sqlite3Malloc(defaultPageSz);
ctx->readCtx = (KeyContext *)sqlite3Malloc(sizeof(KeyContext));
ctx->writeCtx = (KeyContext *)sqlite3Malloc(sizeof(KeyContext));
if(ctx->buffer == NULL || ctx->readCtx == NULL || ctx->writeCtx == NULL){
sqlite3CodecFreeContext(ctx);
return SQLITE_NOMEM;
}
errno_t memsetRc = memset_s(ctx->buffer, DEFAULT_PAGE_SIZE, 0, DEFAULT_PAGE_SIZE);
errno_t memsetRc = memset_s(ctx->buffer, defaultPageSz, 0, defaultPageSz);
memsetRc += memset_s(ctx->readCtx, sizeof(KeyContext), 0, sizeof(KeyContext));
memsetRc += memset_s(ctx->writeCtx, sizeof(KeyContext), 0, sizeof(KeyContext));
if(memsetRc != EOK){
sqlite3CodecFreeContext(ctx);
return SQLITE_ERROR;
}
ctx->readCtx->codecConst.cipherPageSize = DEFAULT_PAGE_SIZE;
ctx->writeCtx->codecConst.cipherPageSize = DEFAULT_PAGE_SIZE;
ctx->readCtx->codecConst.cipherPageSize = defaultPageSz;
ctx->writeCtx->codecConst.cipherPageSize = defaultPageSz;
#ifdef SQLITE_CODEC_ATTACH_CHANGED
int attachHmacAlgo = sqlite3CodecGetDefaultAttachHmacAlgo();
int rc = sqlite3CodecInitKeyContext(ctx, p, zKey, nKey, attachFlag, attachHmacAlgo);
int rc = sqlite3CodecInitKeyContext(ctx, p, zKey, nKey, attachFlag);
#else
int rc = sqlite3CodecInitKeyContext(ctx, p, zKey, nKey);
#endif
@ -245879,11 +245907,12 @@ int sqlite3CodecPragma(sqlite3 *db, int iDb, Parse *parse, const char *zLeft, co
}
CodecContext *ctx = (CodecContext *)(p->pBt->pPager->pCodec);
#ifdef SQLITE_CODEC_ATTACH_CHANGED
CodecParameter *parm = &db->codecParm;
if(sqlite3StrICmp(zLeft, "cipher_default_attach_cipher") == 0 && zRight != NULL){
(void)sqlite3CodecSetDefaultAttachCipher(zRight);
(void)sqlite3CodecSetDefaultAttachCipher(parm, zRight);
return 1;
}else if(sqlite3StrICmp(zLeft, "cipher_default_attach_kdf_iter") == 0 && zRight != NULL){
(void)sqlite3CodecSetDefaultAttachKdfIter(atoi(zRight));
(void)sqlite3CodecSetDefaultAttachKdfIter(parm, atoi(zRight));
return 1;
}else if( sqlite3StrICmp(zLeft, "cipher_default_attach_hmac_algo")==0 && zRight!=NULL ){
/*
@ -245891,31 +245920,31 @@ int sqlite3CodecPragma(sqlite3 *db, int iDb, Parse *parse, const char *zLeft, co
** This behavior is to ensure backward compatible.
*/
if( sqlite3_stricmp(zRight, CIPHER_HMAC_ALGORITHM_NAME_SHA1)==0 ){
sqlite3CodecSetDefaultAttachHmacAlgo(CIPHER_HMAC_ALGORITHM_SHA1);
sqlite3CodecSetDefaultAttachKdfAlgo(CIPHER_KDF_ALGORITHM_SHA1);
sqlite3CodecSetDefaultAttachHmacAlgo(parm, CIPHER_HMAC_ALGORITHM_SHA1);
sqlite3CodecSetDefaultAttachKdfAlgo(parm, CIPHER_KDF_ALGORITHM_SHA1);
}else if( sqlite3_stricmp(zRight, CIPHER_HMAC_ALGORITHM_NAME_SHA256)==0 ){
sqlite3CodecSetDefaultAttachHmacAlgo(CIPHER_HMAC_ALGORITHM_SHA256);
sqlite3CodecSetDefaultAttachKdfAlgo(CIPHER_KDF_ALGORITHM_SHA256);
sqlite3CodecSetDefaultAttachHmacAlgo(parm, CIPHER_HMAC_ALGORITHM_SHA256);
sqlite3CodecSetDefaultAttachKdfAlgo(parm, CIPHER_KDF_ALGORITHM_SHA256);
}else if( sqlite3_stricmp(zRight, CIPHER_HMAC_ALGORITHM_NAME_SHA512)==0 ){
sqlite3CodecSetDefaultAttachHmacAlgo(CIPHER_HMAC_ALGORITHM_SHA512);
sqlite3CodecSetDefaultAttachKdfAlgo(CIPHER_KDF_ALGORITHM_SHA512);
sqlite3CodecSetDefaultAttachHmacAlgo(parm, CIPHER_HMAC_ALGORITHM_SHA512);
sqlite3CodecSetDefaultAttachKdfAlgo(parm, CIPHER_KDF_ALGORITHM_SHA512);
}else{
return 0;
}
return 1;
}else if( sqlite3StrICmp(zLeft, "cipher_default_attach_kdf_algo")==0 && zRight!=NULL ){
if( sqlite3_stricmp(zRight, CIPHER_KDF_ALGORITHM_NAME_SHA1)==0 ){
sqlite3CodecSetDefaultAttachKdfAlgo(CIPHER_KDF_ALGORITHM_SHA1);
sqlite3CodecSetDefaultAttachKdfAlgo(parm, CIPHER_KDF_ALGORITHM_SHA1);
}else if( sqlite3_stricmp(zRight, CIPHER_KDF_ALGORITHM_NAME_SHA256)==0 ){
sqlite3CodecSetDefaultAttachKdfAlgo(CIPHER_KDF_ALGORITHM_SHA256);
sqlite3CodecSetDefaultAttachKdfAlgo(parm, CIPHER_KDF_ALGORITHM_SHA256);
}else if( sqlite3_stricmp(zRight, CIPHER_KDF_ALGORITHM_NAME_SHA512)==0 ){
sqlite3CodecSetDefaultAttachKdfAlgo(CIPHER_KDF_ALGORITHM_SHA512);
sqlite3CodecSetDefaultAttachKdfAlgo(parm, CIPHER_KDF_ALGORITHM_SHA512);
}else{
return 0;
}
return 1;
}else if( sqlite3StrICmp(zLeft, "cipher_default_attach_page_size")==0 && zRight!=NULL ){
(void)sqlite3CodecSetDefaultAttachPageSize(atoi(zRight));
(void)sqlite3CodecSetDefaultAttachPageSize(parm, atoi(zRight));
return 1;
}
#endif