-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
item_strfunc.cc
5294 lines (4627 loc) · 143 KB
/
item_strfunc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
@file
@brief
This file defines all string functions
@warning
Some string functions don't always put and end-null on a String.
(This shouldn't be needed)
*/
/* May include caustic 3rd-party defs. Use early, so it can override nothing. */
#include <openssl/sha.h>
#include "item_strfunc.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#include "base64.h" // base64_encode_max_arg_length
#include "my_aes.h" // MY_AES_IV_SIZE
#include "my_md5.h" // MD5_HASH_SIZE
#include "my_rnd.h" // my_rand_buffer
#include "sha1.h" // SHA1_HASH_SIZE
#include "auth_common.h" // check_password_policy
#include "des_key_file.h" // st_des_keyblock
#include "item_geofunc.h" // Item_func_geomfromgeojson
#include "password.h" // my_make_scrambled_password
#include "sql_class.h" // THD
#include "strfunc.h" // hexchar_to_int
C_MODE_START
#include "../mysys/my_static.h" // For soundex_map
C_MODE_END
#include "template_utils.h"
#include "pfs_file_provider.h"
#include "mysql/psi/mysql_file.h"
extern uint *my_aes_opmode_key_sizes;
using std::min;
using std::max;
using std::string;
using std::vector;
/*
For the Items which have only val_str_ascii() method
and don't have their own "native" val_str(),
we provide a "wrapper" method to convert from ASCII
to Item character set when it's necessary.
Conversion happens only in case of "tricky" Item character set (e.g. UCS2).
Normally conversion does not happen, and val_str_ascii() is immediately
returned instead.
*/
String *Item_str_func::val_str_from_val_str_ascii(String *str, String *str2)
{
assert(fixed == 1);
if (!(collation.collation->state & MY_CS_NONASCII))
{
String *res= val_str_ascii(str);
if (res)
res->set_charset(collation.collation);
return res;
}
assert(str != str2);
uint errors;
String *res= val_str_ascii(str);
if (!res)
return 0;
if ((null_value= str2->copy(res->ptr(), res->length(),
&my_charset_latin1, collation.collation,
&errors)))
return 0;
return str2;
}
bool Item_str_func::fix_fields(THD *thd, Item **ref)
{
bool res= Item_func::fix_fields(thd, ref);
/*
In Item_str_func::check_well_formed_result() we may set null_value
flag on the same condition as in test() below.
*/
maybe_null= (maybe_null || thd->is_strict_mode());
return res;
}
my_decimal *Item_str_func::val_decimal(my_decimal *decimal_value)
{
assert(fixed == 1);
char buff[64];
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
res= val_str(&tmp);
if (!res)
return 0;
(void)str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
res->length(), res->charset(), decimal_value);
return decimal_value;
}
double Item_str_func::val_real()
{
assert(fixed == 1);
int err_not_used;
char *end_not_used, buff[64];
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
res= val_str(&tmp);
return res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
&end_not_used, &err_not_used) : 0.0;
}
longlong Item_str_func::val_int()
{
assert(fixed == 1);
int err;
char buff[22];
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
res= val_str(&tmp);
return (res ?
my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
&err) :
(longlong) 0);
}
String *Item_func_md5::val_str_ascii(String *str)
{
assert(fixed == 1);
String * sptr= args[0]->val_str(str);
str->set_charset(&my_charset_bin);
if (sptr)
{
uchar digest[MD5_HASH_SIZE];
null_value=0;
compute_md5_hash((char *) digest, sptr->ptr(), sptr->length());
if (str->alloc(32)) // Ensure that memory is free
{
null_value=1;
return 0;
}
array_to_hex((char *) str->ptr(), digest, MD5_HASH_SIZE);
str->length((uint) 32);
return str;
}
null_value=1;
return 0;
}
/*
The MD5()/SHA() functions treat their parameter as being a case sensitive.
Thus we set binary collation on it so different instances of MD5() will be
compared properly.
*/
static CHARSET_INFO *get_checksum_charset(const char *csname)
{
CHARSET_INFO *cs= get_charset_by_csname(csname, MY_CS_BINSORT, MYF(0));
if (!cs)
{
// Charset has no binary collation: use my_charset_bin.
cs= &my_charset_bin;
}
return cs;
}
void Item_func_md5::fix_length_and_dec()
{
CHARSET_INFO *cs= get_checksum_charset(args[0]->collation.collation->csname);
args[0]->collation.set(cs, DERIVATION_COERCIBLE);
fix_length_and_charset(32, default_charset());
}
String *Item_func_sha::val_str_ascii(String *str)
{
assert(fixed == 1);
String * sptr= args[0]->val_str(str);
str->set_charset(&my_charset_bin);
if (sptr) /* If we got value different from NULL */
{
/* Temporary buffer to store 160bit digest */
uint8 digest[SHA1_HASH_SIZE];
compute_sha1_hash(digest, sptr->ptr(), sptr->length());
/* Ensure that memory is free */
if (!(str->alloc(SHA1_HASH_SIZE * 2)))
{
array_to_hex((char *) str->ptr(), digest, SHA1_HASH_SIZE);
str->length((uint) SHA1_HASH_SIZE*2);
null_value=0;
return str;
}
}
null_value=1;
return 0;
}
void Item_func_sha::fix_length_and_dec()
{
CHARSET_INFO *cs= get_checksum_charset(args[0]->collation.collation->csname);
args[0]->collation.set(cs, DERIVATION_COERCIBLE);
// size of hex representation of hash
fix_length_and_charset(SHA1_HASH_SIZE * 2, default_charset());
}
/*
SHA2(str, hash_length)
The second argument indicates the desired bit length of the
result, which must have a value of 224, 256, 384, 512, or 0
(which is equivalent to 256).
*/
String *Item_func_sha2::val_str_ascii(String *str)
{
assert(fixed == 1);
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
unsigned char digest_buf[SHA512_DIGEST_LENGTH];
uint digest_length= 0;
String *input_string= args[0]->val_str(str);
str->set_charset(&my_charset_bin);
if (input_string == NULL)
{
null_value= TRUE;
return (String *) NULL;
}
null_value= args[0]->null_value;
if (null_value)
return NULL;
const unsigned char *input_ptr=
pointer_cast<const unsigned char*>(input_string->ptr());
size_t input_len= input_string->length();
longlong hash_length= args[1]->val_int();
null_value= args[1]->null_value;
// Give error message in switch below.
if (null_value)
hash_length= -1;
switch (hash_length) {
#ifndef OPENSSL_NO_SHA512
case 512:
digest_length= SHA512_DIGEST_LENGTH;
(void) SHA512(input_ptr, input_len, digest_buf);
break;
case 384:
digest_length= SHA384_DIGEST_LENGTH;
(void) SHA384(input_ptr, input_len, digest_buf);
break;
#endif
#ifndef OPENSSL_NO_SHA256
case 224:
digest_length= SHA224_DIGEST_LENGTH;
(void) SHA224(input_ptr, input_len, digest_buf);
break;
case 256:
case 0: // SHA-256 is the default
digest_length= SHA256_DIGEST_LENGTH;
(void) SHA256(input_ptr, input_len, digest_buf);
break;
#endif
default:
// For const values we have already warned in fix_length_and_dec.
if (!args[1]->const_item())
push_warning_printf(current_thd,
Sql_condition::SL_WARNING,
ER_WRONG_PARAMETERS_TO_NATIVE_FCT,
ER(ER_WRONG_PARAMETERS_TO_NATIVE_FCT), "sha2");
null_value= TRUE;
return NULL;
}
/*
Since we're subverting the usual String methods, we must make sure that
the destination has space for the bytes we're about to write.
*/
str->mem_realloc(digest_length*2 + 1); /* Each byte as two nybbles */
/* Convert the large number to a string-hex representation. */
array_to_hex((char *) str->ptr(), digest_buf, digest_length);
/* We poked raw bytes in. We must inform the the String of its length. */
str->length(digest_length*2); /* Each byte as two nybbles */
null_value= FALSE;
return str;
#else
push_warning_printf(current_thd,
Sql_condition::SL_WARNING,
ER_FEATURE_DISABLED,
ER(ER_FEATURE_DISABLED),
"sha2", "--with-ssl");
null_value= TRUE;
return (String *) NULL;
#endif /* defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) */
}
void Item_func_sha2::fix_length_and_dec()
{
maybe_null = 1;
max_length = 0;
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
longlong sha_variant;
if (args[1]->const_item())
{
sha_variant= args[1]->val_int();
// Give error message in switch below.
if (args[1]->null_value)
sha_variant= -1;
}
else
{
sha_variant= 512;
}
switch (sha_variant) {
#ifndef OPENSSL_NO_SHA512
case 512:
fix_length_and_charset(SHA512_DIGEST_LENGTH * 2, default_charset());
break;
case 384:
fix_length_and_charset(SHA384_DIGEST_LENGTH * 2, default_charset());
break;
#endif
#ifndef OPENSSL_NO_SHA256
case 256:
case 0: // SHA-256 is the default
fix_length_and_charset(SHA256_DIGEST_LENGTH * 2, default_charset());
break;
case 224:
fix_length_and_charset(SHA224_DIGEST_LENGTH * 2, default_charset());
break;
#endif
default:
fix_length_and_charset(SHA256_DIGEST_LENGTH * 2, default_charset());
push_warning_printf(current_thd,
Sql_condition::SL_WARNING,
ER_WRONG_PARAMETERS_TO_NATIVE_FCT,
ER(ER_WRONG_PARAMETERS_TO_NATIVE_FCT), "sha2");
}
CHARSET_INFO *cs= get_checksum_charset(args[0]->collation.collation->csname);
args[0]->collation.set(cs, DERIVATION_COERCIBLE);
#else
push_warning_printf(current_thd,
Sql_condition::SL_WARNING,
ER_FEATURE_DISABLED,
ER(ER_FEATURE_DISABLED),
"sha2", "--with-ssl");
#endif /* defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) */
}
/* Implementation of AES encryption routines */
/** Helper class to retrieve KDF options for aes_encrypt/aes_decrypt. */
const int max_kdf_option_size = 256;
const int max_kdf_iterations_size = 65535;
const int min_kdf_iterations_size =1000;
class kdf_argument {
char tmp_option_buff[max_kdf_option_size];
String tmp_option_value;
public:
kdf_argument()
: tmp_option_value(tmp_option_buff, sizeof(tmp_option_buff),
system_charset_info) {
memset(tmp_option_buff, '\0', max_kdf_option_size);
}
bool parse_kdf_option(String *kdf_option_value, string &kdf_option,
my_bool *error_generated, const size_t max_size_allowed) {
/*
For large KDF option value, KDF option value will be set as NULL by
function callers.
It gives warning: Warning | 1301 | Result of repeat() was
larger than max_allowed_packet (16777216) - truncated Here arg_count >
KDF option value as NULL will be treated as invalid KDF option value.
*/
if (!kdf_option_value) {
my_error(ER_AES_INVALID_KDF_OPTION_SIZE, MYF(0), max_size_allowed);
*error_generated = TRUE;
return FALSE;
}
if (kdf_option_value->length() > (max_size_allowed - 1)) {
my_error(ER_AES_INVALID_KDF_OPTION_SIZE, MYF(0), max_size_allowed);
*error_generated = TRUE;
return FALSE;
}
kdf_option = kdf_option_value->ptr();
return TRUE;
}
/**
Validate the options and retrieve the KDF options value.
@param arg_count number of parameters passed to the function
@param args array of arguments passed to the function
@param func_name the name of the function (for errors)
@param [out] error_generated set to true if error was generated.
@return retrieved KDF option values
*/
vector<string> retrieve_kdf_options(uint arg_count, Item **args,
const char *func_name,
my_bool *error_generated) {
vector<string> kdf_options;
String *kdf_option_value = NULL;
string kdf_option;
*error_generated = FALSE;
if (arg_count > 3) {
kdf_option_value = args[3]->val_str(&tmp_option_value);
} else {
return kdf_options;
}
// KDF funtion name
if (!parse_kdf_option(kdf_option_value, kdf_option, error_generated,
max_kdf_option_size))
return kdf_options;
// KDF function name should be valid
#if OPENSSL_VERSION_NUMBER < 0x10100000L
if (kdf_option == "pbkdf2_hmac") {
#else
if (kdf_option == "hkdf" || kdf_option == "pbkdf2_hmac") {
#endif
kdf_options.push_back(kdf_option);
} else {
my_error(ER_AES_INVALID_KDF_NAME, MYF(0), func_name);
*error_generated = TRUE;
return kdf_options;
}
kdf_option_value = NULL;
if (arg_count > 4) {
kdf_option_value = args[4]->val_str(&tmp_option_value);
} else {
return kdf_options;
}
// For hkdf and pbkdf2_hmac option 1 is salt
if (!parse_kdf_option(kdf_option_value, kdf_option, error_generated,
max_kdf_option_size))
return kdf_options;
kdf_options.push_back(kdf_option);
kdf_option_value = NULL;
if (arg_count > 5) {
kdf_option_value = args[5]->val_str(&tmp_option_value);
} else {
return kdf_options;
}
// For hkdf option 2 is info
// For pbkdf2_hmac option 2 is iterations
size_t max_size_allowed = max_kdf_option_size;
if (kdf_options[0] == "pbkdf2_hmac") {
// 4 bytes for integer (65535).
max_size_allowed = 6;
}
if (!parse_kdf_option(kdf_option_value, kdf_option, error_generated,
max_size_allowed))
return kdf_options;
kdf_options.push_back(kdf_option);
if ((kdf_options[0] == "pbkdf2_hmac") && (kdf_options.size() > 2)) {
int iter = atoi(kdf_options[2].c_str());
if (iter < min_kdf_iterations_size || iter > max_kdf_iterations_size) {
*error_generated = true;
my_error(ER_AES_INVALID_KDF_ITERATIONS, MYF(0), func_name);
}
}
return kdf_options;
}
};
/** helper class to process an IV argument to aes_encrypt/aes_decrypt */
class iv_argument
{
char iv_buff[MY_AES_IV_SIZE + 1]; // +1 to cater for the terminating NULL
String tmp_iv_value;
public:
iv_argument() :
tmp_iv_value(iv_buff, sizeof(iv_buff), system_charset_info)
{}
/**
Validate the arguments and retrieve the IV value.
Processes a 3d optional IV argument to an Item_func function.
Contains all the necessary stack buffers etc.
@param aes_opmode the encryption mode
@param arg_count number of parameters passed to the function
@param args array of arguments passed to the function
@param func_name the name of the function (for errors)
@param thd the current thread (for errors)
@param [out] error_generated set to true if error was generated.
@return a pointer to the retrived validated IV or NULL
*/
const unsigned char *retrieve_iv_ptr(enum my_aes_opmode aes_opmode,
uint arg_count,
Item **args,
const char *func_name,
THD *thd,
my_bool *error_generated)
{
const unsigned char *iv_str= NULL;
*error_generated= FALSE;
if (my_aes_needs_iv(aes_opmode))
{
/* we only enforce the need for IV */
if (arg_count > 2)
{
String *iv= args[2]->val_str(&tmp_iv_value);
if (!iv || iv->length() < MY_AES_IV_SIZE)
{
my_error(ER_AES_INVALID_IV, MYF(0), func_name, (long long) MY_AES_IV_SIZE);
*error_generated= TRUE;
return NULL;
}
iv_str= (unsigned char *) iv->ptr();
}
else
{
my_error(ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT, MYF(0), func_name);
*error_generated= TRUE;
return NULL;
}
}
else
{
if (arg_count == 3)
{
push_warning_printf(thd, Sql_condition::SL_WARNING,
WARN_OPTION_IGNORED,
ER(WARN_OPTION_IGNORED), "IV");
}
}
return iv_str;
}
};
bool Item_func_aes_encrypt::itemize(Parse_context *pc, Item **res)
{
if (skip_itemize(res))
return false;
if (super::itemize(pc, res))
return true;
/* Unsafe for SBR since result depends on a session variable */
pc->thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
/* Not safe to cache either */
pc->thd->lex->set_uncacheable(pc->select, UNCACHEABLE_SIDEEFFECT);
return false;
}
String *Item_func_aes_encrypt::val_str(String *str)
{
assert(fixed == 1);
char key_buff[80] = {'\0'};
String tmp_key_value(key_buff, sizeof(key_buff), system_charset_info);
String *sptr, *key;
int aes_length;
THD *thd= current_thd;
ulong aes_opmode;
iv_argument iv_arg;
DBUG_ENTER("Item_func_aes_encrypt::val_str");
sptr= args[0]->val_str(str); // String to encrypt
key= args[1]->val_str(&tmp_key_value); // key
aes_opmode= thd->variables.my_aes_mode;
assert(aes_opmode <= MY_AES_END);
if (sptr && key) // we need both arguments to be not NULL
{
const unsigned char *iv_str=
iv_arg.retrieve_iv_ptr((enum my_aes_opmode) aes_opmode, arg_count, args,
func_name(), thd, &null_value);
if (null_value)
DBUG_RETURN(NULL);
vector<string> kdf_options;
kdf_argument kdf_arg;
kdf_options =
kdf_arg.retrieve_kdf_options(arg_count, args, func_name(), &null_value);
if (null_value) {
DBUG_RETURN(NULL);
}
// Calculate result length
aes_length= my_aes_get_size(sptr->length(),
(enum my_aes_opmode) aes_opmode);
str_value.set_charset(&my_charset_bin);
const uint rkey_size = my_aes_opmode_key_sizes[aes_opmode] / 8;
uint key_size = key->length();
if ((key_size > rkey_size) && (kdf_options.size() == 0)) {
push_warning_printf(thd, Sql_condition::SL_WARNING, WARN_AES_KEY_SIZE,
ER_THD(thd, WARN_AES_KEY_SIZE), rkey_size);
}
if (!str_value.alloc(aes_length)) // Ensure that memory is free
{
// finally encrypt directly to allocated buffer.
if (my_aes_encrypt((unsigned char *)sptr->ptr(), sptr->length(),
(unsigned char *)str_value.ptr(),
(unsigned char *)key->ptr(), key->length(),
(enum my_aes_opmode)aes_opmode, iv_str, true,
(kdf_options.size() > 0) ? &kdf_options : NULL) ==
aes_length)
{
// We got the expected result length
str_value.length((uint) aes_length);
DBUG_RETURN(&str_value);
}
}
}
null_value=1;
DBUG_RETURN(0);
}
void Item_func_aes_encrypt::fix_length_and_dec()
{
ulong aes_opmode= current_thd->variables.my_aes_mode;
assert(aes_opmode <= MY_AES_END);
max_length=my_aes_get_size(args[0]->max_length,
(enum my_aes_opmode) aes_opmode);
}
bool Item_func_aes_decrypt::itemize(Parse_context *pc, Item **res)
{
if (skip_itemize(res))
return false;
if (super::itemize(pc, res))
return true;
/* Unsafe for SBR since result depends on a session variable */
pc->thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
/* Not safe to cache either */
pc->thd->lex->set_uncacheable(pc->select, UNCACHEABLE_SIDEEFFECT);
return false;
}
String *Item_func_aes_decrypt::val_str(String *str)
{
assert(fixed == 1);
char key_buff[80];
String tmp_key_value(key_buff, sizeof(key_buff), system_charset_info);
String *sptr, *key;
THD *thd= current_thd;
ulong aes_opmode;
iv_argument iv_arg;
DBUG_ENTER("Item_func_aes_decrypt::val_str");
sptr= args[0]->val_str(str); // String to decrypt
key= args[1]->val_str(&tmp_key_value); // Key
aes_opmode= thd->variables.my_aes_mode;
assert(aes_opmode <= MY_AES_END);
if (sptr && key) // Need to have both arguments not NULL
{
const unsigned char *iv_str=
iv_arg.retrieve_iv_ptr((enum my_aes_opmode) aes_opmode, arg_count, args,
func_name(), thd, &null_value);
if (null_value)
DBUG_RETURN(NULL);
str_value.set_charset(&my_charset_bin);
if (!str_value.alloc(sptr->length())) // Ensure that memory is free
{
vector<string> kdf_options;
kdf_argument kdf_arg;
kdf_options = kdf_arg.retrieve_kdf_options(arg_count, args, func_name(),
&null_value);
if (null_value) {
DBUG_RETURN(NULL);
}
// finally decrypt directly to allocated buffer.
int length;
length = my_aes_decrypt(
(unsigned char *)sptr->ptr(), sptr->length(),
(unsigned char *)str_value.ptr(), (unsigned char *)key->ptr(),
key->length(), (enum my_aes_opmode)aes_opmode, iv_str, true,
(kdf_options.size() > 0) ? &kdf_options : NULL);
if (length >= 0) // if we got correct data data
{
str_value.length((uint) length);
DBUG_RETURN(&str_value);
}
}
}
// Bad parameters. No memory or bad data will all go here
null_value=1;
DBUG_RETURN(0);
}
void Item_func_aes_decrypt::fix_length_and_dec()
{
max_length=args[0]->max_length;
maybe_null= 1;
}
bool Item_func_random_bytes::itemize(Parse_context *pc, Item **res)
{
if (skip_itemize(res))
return false;
if (super::itemize(pc, res))
return true;
/* it is unsafe for SBR since it uses crypto random from the ssl library */
pc->thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
/* Not safe to cache either */
pc->thd->lex->set_uncacheable(pc->select, UNCACHEABLE_RAND);
return false;
}
/*
Artificially limited to 1k to avoid excessive memory usage.
The SSL lib supports up to INT_MAX.
*/
const longlong Item_func_random_bytes::MAX_RANDOM_BYTES_BUFFER= 1024;
void Item_func_random_bytes::fix_length_and_dec()
{
collation.set(&my_charset_bin);
max_length= MAX_RANDOM_BYTES_BUFFER;
}
String *Item_func_random_bytes::val_str(String *a)
{
assert(fixed == 1);
longlong n_bytes= args[0]->val_int();
null_value= args[0]->null_value;
if (null_value)
return NULL;
str_value.set_charset(&my_charset_bin);
if (n_bytes <= 0 || n_bytes > MAX_RANDOM_BYTES_BUFFER)
{
my_error(ER_DATA_OUT_OF_RANGE, MYF(0), "length", func_name());
null_value= TRUE;
return NULL;
}
if (str_value.alloc(n_bytes))
{
my_error(ER_OUTOFMEMORY, n_bytes);
null_value= TRUE;
return NULL;
}
str_value.set_charset(&my_charset_bin);
if (my_rand_buffer((unsigned char *) str_value.ptr(), n_bytes))
{
my_error(ER_ERROR_WHEN_EXECUTING_COMMAND, MYF(0), func_name(),
"SSL library can't generate random bytes");
null_value= TRUE;
return NULL;
}
str_value.length(n_bytes);
return &str_value;
}
void Item_func_to_base64::fix_length_and_dec()
{
maybe_null= args[0]->maybe_null;
collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
if (args[0]->max_length > (uint) base64_encode_max_arg_length())
{
maybe_null= 1;
fix_char_length_ulonglong((ulonglong) base64_encode_max_arg_length());
}
else
{
uint64 length= base64_needed_encoded_length((uint64) args[0]->max_length);
assert(length > 0);
fix_char_length_ulonglong((ulonglong) length - 1);
}
}
String *Item_func_to_base64::val_str_ascii(String *str)
{
String *res= args[0]->val_str(str);
bool too_long= false;
uint64 length;
if (!res ||
res->length() > (uint) base64_encode_max_arg_length() ||
(too_long=
((length= base64_needed_encoded_length((uint64) res->length())) >
current_thd->variables.max_allowed_packet)) ||
tmp_value.alloc((uint) length))
{
null_value= 1; // NULL input, too long input, or OOM.
if (too_long)
{
push_warning_printf(current_thd, Sql_condition::SL_WARNING,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
current_thd->variables.max_allowed_packet);
}
return 0;
}
base64_encode(res->ptr(), (int) res->length(), (char*) tmp_value.ptr());
assert(length > 0);
tmp_value.length((uint) length - 1); // Without trailing '\0'
null_value= 0;
return &tmp_value;
}
void Item_func_from_base64::fix_length_and_dec()
{
if (args[0]->max_length > (uint) base64_decode_max_arg_length())
{
fix_char_length_ulonglong((ulonglong) base64_decode_max_arg_length());
}
else
{
uint64 length= base64_needed_decoded_length((uint64) args[0]->max_length);
fix_char_length_ulonglong((ulonglong) length);
}
maybe_null= 1; // Can be NULL, e.g. in case of badly formed input string
}
String *Item_func_from_base64::val_str(String *str)
{
String *res= args[0]->val_str_ascii(str);
bool too_long= false;
int64 length;
const char *end_ptr;
if (!res ||
res->length() > (uint) base64_decode_max_arg_length() ||
(too_long=
((uint64) (length= base64_needed_decoded_length((uint64) res->length())) >
current_thd->variables.max_allowed_packet)) ||
tmp_value.alloc((uint) length) ||
(length= base64_decode(res->ptr(), (uint64) res->length(),
(char *) tmp_value.ptr(), &end_ptr, 0)) < 0 ||
end_ptr < res->ptr() + res->length())
{
null_value= 1; // NULL input, too long input, OOM, or badly formed input
if (too_long)
{
push_warning_printf(current_thd, Sql_condition::SL_WARNING,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
current_thd->variables.max_allowed_packet);
}
return 0;
}
tmp_value.length((uint) length);
null_value= 0;
return &tmp_value;
}
/**
Concatenate args with the following premises:
If only one arg (which is ok), return value of arg;
Don't reallocate val_str() if not absolute necessary.
*/
String *Item_func_concat::val_str(String *str) {
assert(fixed == 1);
String *res;
THD *thd = current_thd;
null_value = false;
tmp_value.length(0);
for (uint i = 0; i < arg_count; ++i) {
if (!(res = args[i]->val_str(str))) {
null_value = 1;
return 0;
}
if (res->length() + tmp_value.length() >
thd->variables.max_allowed_packet) {
push_warning_printf(thd, Sql_condition::SL_WARNING,
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
thd->variables.max_allowed_packet);
null_value = 1;
return 0;
}
if (tmp_value.append(*res)) {
null_value = 1;
return 0;
}
}
res = &tmp_value;
res->set_charset(collation.collation);
return res;
}
void Item_func_concat::fix_length_and_dec()
{
ulonglong char_length= 0;
if (agg_arg_charsets_for_string_result(collation, args, arg_count))
return;
for (uint i=0 ; i < arg_count ; i++) {
args[i]->cmp_context = STRING_RESULT;
char_length+= args[i]->max_char_length();
}
fix_char_length_ulonglong(char_length);
}
/**
@details
Function des_encrypt() by [email protected] & monty
Works only if compiled with OpenSSL library support.
@return
A binary string where first character is CHAR(128 | key-number).
If one uses a string key key_number is 127.
Encryption result is longer than original by formula:
@code new_length= org_length + (8-(org_length % 8))+1 @endcode
*/
String *Item_func_des_encrypt::val_str(String *str)
{
assert(fixed == 1);
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
uint code= ER_WRONG_PARAMETERS_TO_PROCEDURE;
DES_cblock ivec;
struct st_des_keyblock keyblock;
struct st_des_keyschedule keyschedule;
const char *append_str="********";
uint key_number, tail;
size_t res_length;
String *res= args[0]->val_str(str);