#include <item_strfunc.h>
Inheritance diagram for Item_func_des_encrypt:


Public Member Functions | |
| Item_func_des_encrypt (Item *a) | |
| Item_func_des_encrypt (Item *a, Item *b) | |
| String * | val_str (String *) |
| void | fix_length_and_dec () |
| const char * | func_name () const |
Private Attributes | |
| String | tmp_value |
Definition at line 310 of file item_strfunc.h.
| Item_func_des_encrypt::Item_func_des_encrypt | ( | Item * | a | ) | [inline] |
| void Item_func_des_encrypt::fix_length_and_dec | ( | ) | [inline, virtual] |
Implements Item_result_field.
Definition at line 317 of file item_strfunc.h.
References Item_func::args, Item::max_length, and Item::maybe_null.
00318 { maybe_null=1; max_length = args[0]->max_length+8; }
| const char* Item_func_des_encrypt::func_name | ( | ) | const [inline, virtual] |
Implements Item.
Definition at line 438 of file item_strfunc.cc.
References String::alloc(), String::append(), Item_func::arg_count, Item_func::args, bzero, current_thd, DBUG_ASSERT, DES_ede3_cbc_encrypt, DES_set_key_unchecked, ER, ER_FEATURE_DISABLED, ER_OUT_OF_RESOURCES, ER_WRONG_PARAMETERS_TO_PROCEDURE, error, EVP_BytesToKey, EVP_des_ede3_cbc, EVP_md5, Item::fixed, int(), INT_RESULT, String::length(), my_empty_string(), NULL, Item::null_value, pthread_mutex_lock, pthread_mutex_unlock, String::ptr(), push_warning_printf(), Item_str_func::result_type(), tmp_value, TRUE, Item_str_func::val_int(), Item::val_str(), VOID, and MYSQL_ERROR::WARN_LEVEL_ERROR.
00439 { 00440 DBUG_ASSERT(fixed == 1); 00441 #ifdef HAVE_OPENSSL 00442 uint code= ER_WRONG_PARAMETERS_TO_PROCEDURE; 00443 DES_cblock ivec; 00444 struct st_des_keyblock keyblock; 00445 struct st_des_keyschedule keyschedule; 00446 const char *append_str="********"; 00447 uint key_number, res_length, tail; 00448 String *res= args[0]->val_str(str); 00449 00450 if ((null_value= args[0]->null_value)) 00451 return 0; // ENCRYPT(NULL) == NULL 00452 if ((res_length=res->length()) == 0) 00453 return &my_empty_string; 00454 00455 if (arg_count == 1) 00456 { 00457 /* Protect against someone doing FLUSH DES_KEY_FILE */ 00458 VOID(pthread_mutex_lock(&LOCK_des_key_file)); 00459 keyschedule= des_keyschedule[key_number=des_default_key]; 00460 VOID(pthread_mutex_unlock(&LOCK_des_key_file)); 00461 } 00462 else if (args[1]->result_type() == INT_RESULT) 00463 { 00464 key_number= (uint) args[1]->val_int(); 00465 if (key_number > 9) 00466 goto error; 00467 VOID(pthread_mutex_lock(&LOCK_des_key_file)); 00468 keyschedule= des_keyschedule[key_number]; 00469 VOID(pthread_mutex_unlock(&LOCK_des_key_file)); 00470 } 00471 else 00472 { 00473 String *keystr=args[1]->val_str(&tmp_value); 00474 if (!keystr) 00475 goto error; 00476 key_number=127; // User key string 00477 00478 /* We make good 24-byte (168 bit) key from given plaintext key with MD5 */ 00479 bzero((char*) &ivec,sizeof(ivec)); 00480 EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL, 00481 (uchar*) keystr->ptr(), (int) keystr->length(), 00482 1, (uchar*) &keyblock,ivec); 00483 DES_set_key_unchecked(&keyblock.key1,&keyschedule.ks1); 00484 DES_set_key_unchecked(&keyblock.key2,&keyschedule.ks2); 00485 DES_set_key_unchecked(&keyblock.key3,&keyschedule.ks3); 00486 } 00487 00488 /* 00489 The problem: DES algorithm requires original data to be in 8-bytes 00490 chunks. Missing bytes get filled with '*'s and result of encryption 00491 can be up to 8 bytes longer than original string. When decrypted, 00492 we do not know the size of original string :( 00493 We add one byte with value 0x1..0x8 as the last byte of the padded 00494 string marking change of string length. 00495 */ 00496 00497 tail= (8-(res_length) % 8); // 1..8 marking extra length 00498 res_length+=tail; 00499 code= ER_OUT_OF_RESOURCES; 00500 if (tail && res->append(append_str, tail) || tmp_value.alloc(res_length+1)) 00501 goto error; 00502 (*res)[res_length-1]=tail; // save extra length 00503 tmp_value.length(res_length+1); 00504 tmp_value[0]=(char) (128 | key_number); 00505 // Real encryption 00506 bzero((char*) &ivec,sizeof(ivec)); 00507 DES_ede3_cbc_encrypt((const uchar*) (res->ptr()), 00508 (uchar*) (tmp_value.ptr()+1), 00509 res_length, 00510 &keyschedule.ks1, 00511 &keyschedule.ks2, 00512 &keyschedule.ks3, 00513 &ivec, TRUE); 00514 return &tmp_value; 00515 00516 error: 00517 push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR, 00518 code, ER(code), 00519 "des_encrypt"); 00520 #else 00521 push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR, 00522 ER_FEATURE_DISABLED, ER(ER_FEATURE_DISABLED), 00523 "des_encrypt","--with-openssl"); 00524 #endif /* HAVE_OPENSSL */ 00525 null_value=1; 00526 return 0; 00527 }
Here is the call graph for this function:

String Item_func_des_encrypt::tmp_value [private] |
1.4.7

