Commit a9120c7b authored by monty@hundin.mysql.fi's avatar monty@hundin.mysql.fi

Fixes for des_crypt functions.

parent a5f061c7
......@@ -56,7 +56,7 @@ noinst_HEADERS = item.h item_func.h item_sum.h item_cmpfunc.h \
sql_select.h structs.h table.h sql_udf.h hash_filo.h\
lex.h lex_symbol.h sql_acl.h sql_crypt.h \
log_event.h mini_client.h sql_repl.h slave.h \
stacktrace.h sql_sort.h
stacktrace.h sql_sort.h sql_cache.h
mysqld_SOURCES = sql_lex.cc sql_handler.cc \
item.cc item_sum.cc item_buff.cc item_func.cc \
item_cmpfunc.cc item_strfunc.cc item_timefunc.cc \
......
......@@ -25,7 +25,7 @@
*/
struct st_des_keyschedule des_keyschedule[10];
uint default_des_key;
uint des_default_key;
void
load_des_key_file(const char *file_name)
......@@ -43,7 +43,7 @@ load_des_key_file(const char *file_name)
goto error;
bzero((char*) des_keyschedule,sizeof(struct st_des_keyschedule) * 10);
default_des_key=15; // Impossible key
des_default_key=15; // Impossible key
for (;;)
{
char *start, *end;
......@@ -72,8 +72,8 @@ load_des_key_file(const char *file_name)
des_set_key_unchecked(&keyblock.key1,des_keyschedule[(int)offset].ks1);
des_set_key_unchecked(&keyblock.key2,des_keyschedule[(int)offset].ks2);
des_set_key_unchecked(&keyblock.key3,des_keyschedule[(int)offset].ks3);
if (default_des_key == 15)
default_des_key= (uint) offset; // use first as def.
if (des_default_key == 15)
des_default_key= (uint) offset; // use first as def.
}
}
else
......
......@@ -221,8 +221,9 @@ String *Item_func_des_encrypt::val_str(String *str)
des_cblock ivec={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
struct st_des_keyblock keyblock;
struct st_des_keyschedule keyschedule;
struct st_des_keyschedule *keyschedule_ptr=&keyschedule;
uint key_number=15;
struct st_des_keyschedule *keyschedule_ptr;
const char *append_str="********";
uint key_number, res_length, tail;
String *res= args[0]->val_str(str);
if ((null_value=args[0]->null_value))
......@@ -231,23 +232,24 @@ String *Item_func_des_encrypt::val_str(String *str)
return &empty_string;
if (arg_count == 1)
keyschedule_ptr=des_keyschedule[key_number=default_des_key];
else if (args[1]->result_type == INT_RESULT)
keyschedule_ptr= &des_keyschedule[key_number=des_default_key];
else if (args[1]->result_type() == INT_RESULT)
{
key_number= (uint) args[1]->val_int();
if (key_number > 9)
goto error;
keyschedule_ptr= des_keyschedule[key_number];
keyschedule_ptr= &des_keyschedule[key_number];
}
else
{
const char *append_str="********";
uint tail,res_length;
String *keystr=args[1]->val_str(&tmp_value);
if (!keystr)
goto error;
key_number=15; // User key string
/* We make good 24-byte (168 bit) key from given plaintext key with MD5 */
keyschedule_ptr= &keyschedule;
EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL,
(uchar*) keystr->ptr(), (int) keystr->length(),
1, (uchar*) &keyblock,ivec);
......@@ -268,13 +270,13 @@ String *Item_func_des_encrypt::val_str(String *str)
tail= (7-(res->length()+7) % 8); // 0..7 marking extra length
res_length=res->length()+tail+1;
if (tail && res->append(append_str, tail) || tmp_value.alloc(res_length))
goto err;
goto error;
tmp_value.length(res_length);
tmp_value.[0]=(char) (128 | tail << 4 | key_number);
tmp_value[0]=(char) (128 | tail << 4 | key_number);
// Real encryption
des_ede3_cbc_encrypt((const uchar*) (res->ptr()),
(uchar*) (tmp_value->ptr()+1),
(uchar*) (tmp_value.ptr()+1),
res->length(),
keyschedule_ptr->ks1,
keyschedule_ptr->ks2,
......@@ -296,28 +298,30 @@ String *Item_func_des_decrypt::val_str(String *str)
des_cblock ivec={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
struct st_des_keyblock keyblock;
struct st_des_keyschedule keyschedule;
struct st_des_keyschedule *keyschedule_ptr=&keyschedule;
struct st_des_keyschedule *keyschedule_ptr;
String *res= args[0]->val_str(str);
if ((null_value=args[0]->null_value))
return 0;
if (res->length(0) < 9 || (res->length()) % 8 != 1 || !(res->[0] & 128))
if (res->length() < 9 || (res->length() % 8) != 1 || !((*res)[0] & 128))
return res; // Skip decryption if not encrypted
if (arg_count == 1) // If automatic uncompression
{
uint key_number=res->[0] & 15;
uint key_number=(uint) (*res)[0] & 15;
// Check if automatic key and that we have privilege to uncompress using it
if (!(current_thd->master_access & PROCESS_ACL) || key_number > 9)
goto error;
keyschedule_ptr=des_keyschedule[key_number-1];
keyschedule_ptr= &des_keyschedule[key_number];
}
else
{
// We make good 24-byte (168 bit) key from given plaintext key with MD5
String *keystr=args[1]->val_str(&tmp_value);
if (!key_str)
if (!keystr)
goto error;
keyschedule_ptr= &keyschedule;
EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL,
(uchar*) keystr->ptr(),(int) keystr->length(),
1,(uchar*) &keyblock,ivec);
......@@ -327,11 +331,11 @@ String *Item_func_des_decrypt::val_str(String *str)
des_set_key_unchecked(&keyblock.key3,keyschedule_ptr->ks3);
}
if (tmp_value.alloc(res->length()-1))
goto err;
goto error;
/* Restore old length of key */
tmp_value.length(res->length()-1-(((uchar) res->[0] >> 4) & 7));
tmp_value.length(res->length()-1-(((uchar) (*res)[0] >> 4) & 7));
des_ede3_cbc_encrypt((const uchar*) res->ptr()+1,
(uchar*) (tmp_value->ptr()),
(uchar*) (tmp_value.ptr()),
res->length()-1,
keyschedule_ptr->ks1,
keyschedule_ptr->ks2,
......
......@@ -412,8 +412,8 @@ static SYMBOL sql_functions[] = {
{ "DAYOFYEAR", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_dayofyear)},
{ "DECODE", SYM(DECODE_SYM),0,0},
{ "DEGREES", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_degrees)},
{ "DES_ENCRYPT", SYM(DES_ENCRYPT),0,0},
{ "DES_DECRYPT", SYM(DES_DECRYPT),0,0},
{ "DES_ENCRYPT", SYM(DES_ENCRYPT_SYM),0,0},
{ "DES_DECRYPT", SYM(DES_DECRYPT_SYM),0,0},
{ "ELT", SYM(ELT_FUNC),0,0},
{ "ENCODE", SYM(ENCODE_SYM),0,0},
{ "ENCRYPT", SYM(ENCRYPT),0,0},
......
......@@ -405,7 +405,7 @@ struct st_des_keyschedule
{
des_key_schedule ks1, ks2, ks3;
};
extern struct st_des_keyschedule des_keyschedule[9];
extern struct st_des_keyschedule des_keyschedule[10];
extern uint des_default_key;
void load_des_key_file(const char *file_name);
#endif /* HAVE_OPENSSL */
......
......@@ -382,8 +382,8 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize);
%token DAY_SECOND_SYM
%token DAY_SYM
%token DECODE_SYM
%token DES_ENCRYPT
%token DES_DECRYPT
%token DES_ENCRYPT_SYM
%token DES_DECRYPT_SYM
%token ELSE
%token ELT_FUNC
%token ENCODE_SYM
......@@ -1647,13 +1647,13 @@ simple_expr:
{ $$= new Item_func_decode($3,$5.str); }
| ENCODE_SYM '(' expr ',' TEXT_STRING ')'
{ $$= new Item_func_encode($3,$5.str); }
| DES_DECRYPT '(' expr ')'
| DES_DECRYPT_SYM '(' expr ')'
{ $$= new Item_func_des_decrypt($3); }
| DES_DECRYPT '(' expr ',' expr ')'
| DES_DECRYPT_SYM '(' expr ',' expr ')'
{ $$= new Item_func_des_decrypt($3,$5); }
| DES_ENCRYPT '(' expr ')'
| DES_ENCRYPT_SYM '(' expr ')'
{ $$= new Item_func_des_encrypt($3); }
| DES_ENCRYPT '(' expr ',' expr ')'
| DES_ENCRYPT_SYM '(' expr ',' expr ')'
{ $$= new Item_func_des_encrypt($3,$5); }
| EXPORT_SET '(' expr ',' expr ',' expr ')'
{ $$= new Item_func_export_set($3, $5, $7); }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment