Skip to content

Commit 948c44e

Browse files
committed
more robust error handling
1 parent 8333602 commit 948c44e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

crypsi_mysqludf.c

+22
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ char* mcrypsi_aes_128_gcm_encrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
152152
unsigned long* length, char* is_null, char* error) {
153153
unsigned char* dst = (unsigned char*) initid->ptr;
154154
int ret = 0;
155+
*length = 0;
155156
*is_null = 0;
156157
*error = 0;
157158
char* input_key = args->args[0];
@@ -161,6 +162,7 @@ char* mcrypsi_aes_128_gcm_encrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
161162
int dst_size = 0;
162163
ret = crypsi_aes_128_gcm_encrypt(input_key, input_text, text_size, &dst, &dst_size);
163164
if (ret != 0) {
165+
*is_null = 1;
164166
*error = 1;
165167
return NULL;
166168
}
@@ -197,6 +199,7 @@ char* mcrypsi_aes_128_gcm_decrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
197199
unsigned long* length, char* is_null, char* error) {
198200
unsigned char* dst = (unsigned char*) initid->ptr;
199201
int ret = 0;
202+
*length = 0;
200203
*is_null = 0;
201204
*error = 0;
202205
char* input_key = args->args[0];
@@ -206,6 +209,7 @@ char* mcrypsi_aes_128_gcm_decrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
206209
int dst_size = 0;
207210
ret = crypsi_aes_128_gcm_decrypt(input_key, input_text, text_size, &dst, &dst_size);
208211
if (ret != 0) {
212+
*is_null = 1;
209213
*error = 1;
210214
return NULL;
211215
}
@@ -242,6 +246,7 @@ char* mcrypsi_aes_192_gcm_encrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
242246
unsigned long* length, char* is_null, char* error) {
243247
unsigned char* dst = (unsigned char*) initid->ptr;
244248
int ret = 0;
249+
*length = 0;
245250
*is_null = 0;
246251
*error = 0;
247252
char* input_key = args->args[0];
@@ -251,6 +256,7 @@ char* mcrypsi_aes_192_gcm_encrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
251256
int dst_size = 0;
252257
ret = crypsi_aes_192_gcm_encrypt(input_key, input_text, text_size, &dst, &dst_size);
253258
if (ret != 0) {
259+
*is_null = 1;
254260
*error = 1;
255261
return NULL;
256262
}
@@ -287,6 +293,7 @@ char* mcrypsi_aes_192_gcm_decrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
287293
unsigned long* length, char* is_null, char* error) {
288294
unsigned char* dst = (unsigned char*) initid->ptr;
289295
int ret = 0;
296+
*length = 0;
290297
*is_null = 0;
291298
*error = 0;
292299
char* input_key = args->args[0];
@@ -296,6 +303,7 @@ char* mcrypsi_aes_192_gcm_decrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
296303
int dst_size = 0;
297304
ret = crypsi_aes_192_gcm_decrypt(input_key, input_text, text_size, &dst, &dst_size);
298305
if (ret != 0) {
306+
*is_null = 1;
299307
*error = 1;
300308
return NULL;
301309
}
@@ -332,6 +340,7 @@ char* mcrypsi_aes_256_gcm_encrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
332340
unsigned long* length, char* is_null, char* error) {
333341
unsigned char* dst = (unsigned char*) initid->ptr;
334342
int ret = 0;
343+
*length = 0;
335344
*is_null = 0;
336345
*error = 0;
337346
char* input_key = args->args[0];
@@ -341,6 +350,7 @@ char* mcrypsi_aes_256_gcm_encrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
341350
int dst_size = 0;
342351
ret = crypsi_aes_256_gcm_encrypt(input_key, input_text, text_size, &dst, &dst_size);
343352
if (ret != 0) {
353+
*is_null = 1;
344354
*error = 1;
345355
return NULL;
346356
}
@@ -377,6 +387,7 @@ char* mcrypsi_aes_256_gcm_decrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
377387
unsigned long* length, char* is_null, char* error) {
378388
unsigned char* dst = (unsigned char*) initid->ptr;
379389
int ret = 0;
390+
*length = 0;
380391
*is_null = 0;
381392
*error = 0;
382393
char* input_key = args->args[0];
@@ -386,6 +397,7 @@ char* mcrypsi_aes_256_gcm_decrypt(UDF_INIT* initid, UDF_ARGS* args, char* result
386397
int dst_size = 0;
387398
ret = crypsi_aes_256_gcm_decrypt(input_key, input_text, text_size, &dst, &dst_size);
388399
if (ret != 0) {
400+
*is_null = 1;
389401
*error = 1;
390402
return NULL;
391403
}
@@ -422,6 +434,7 @@ char* mcrypsi_hmac_md5(UDF_INIT* initid, UDF_ARGS* args, char* result,
422434
unsigned long* length, char* is_null, char* error) {
423435
unsigned char* dst = (unsigned char*) initid->ptr;
424436
int ret = 0;
437+
*length = 0;
425438
*is_null = 0;
426439
*error = 0;
427440
char* input_key = args->args[0];
@@ -431,6 +444,7 @@ char* mcrypsi_hmac_md5(UDF_INIT* initid, UDF_ARGS* args, char* result,
431444
int dst_size = 0;
432445
ret = crypsi_hmac_md5(input_key, input_text, text_size, &dst, &dst_size);
433446
if (ret != 0) {
447+
*is_null = 1;
434448
*error = 1;
435449
return NULL;
436450
}
@@ -467,6 +481,7 @@ char* mcrypsi_hmac_sha1(UDF_INIT* initid, UDF_ARGS* args, char* result,
467481
unsigned long* length, char* is_null, char* error) {
468482
unsigned char* dst = (unsigned char*) initid->ptr;
469483
int ret = 0;
484+
*length = 0;
470485
*is_null = 0;
471486
*error = 0;
472487
char* input_key = args->args[0];
@@ -476,6 +491,7 @@ char* mcrypsi_hmac_sha1(UDF_INIT* initid, UDF_ARGS* args, char* result,
476491
int dst_size = 0;
477492
ret = crypsi_hmac_sha1(input_key, input_text, text_size, &dst, &dst_size);
478493
if (ret != 0) {
494+
*is_null = 1;
479495
*error = 1;
480496
return NULL;
481497
}
@@ -512,6 +528,7 @@ char* mcrypsi_hmac_sha256(UDF_INIT* initid, UDF_ARGS* args, char* result,
512528
unsigned long* length, char* is_null, char* error) {
513529
unsigned char* dst = (unsigned char*) initid->ptr;
514530
int ret = 0;
531+
*length = 0;
515532
*is_null = 0;
516533
*error = 0;
517534
char* input_key = args->args[0];
@@ -521,6 +538,7 @@ char* mcrypsi_hmac_sha256(UDF_INIT* initid, UDF_ARGS* args, char* result,
521538
int dst_size = 0;
522539
ret = crypsi_hmac_sha256(input_key, input_text, text_size, &dst, &dst_size);
523540
if (ret != 0) {
541+
*is_null = 1;
524542
*error = 1;
525543
return NULL;
526544
}
@@ -557,6 +575,7 @@ char* mcrypsi_hmac_sha384(UDF_INIT* initid, UDF_ARGS* args, char* result,
557575
unsigned long* length, char* is_null, char* error) {
558576
unsigned char* dst = (unsigned char*) initid->ptr;
559577
int ret = 0;
578+
*length = 0;
560579
*is_null = 0;
561580
*error = 0;
562581
char* input_key = args->args[0];
@@ -566,6 +585,7 @@ char* mcrypsi_hmac_sha384(UDF_INIT* initid, UDF_ARGS* args, char* result,
566585
int dst_size = 0;
567586
ret = crypsi_hmac_sha384(input_key, input_text, text_size, &dst, &dst_size);
568587
if (ret != 0) {
588+
*is_null = 1;
569589
*error = 1;
570590
return NULL;
571591
}
@@ -602,6 +622,7 @@ char* mcrypsi_hmac_sha512(UDF_INIT* initid, UDF_ARGS* args, char* result,
602622
unsigned long* length, char* is_null, char* error) {
603623
unsigned char* dst = (unsigned char*) initid->ptr;
604624
int ret = 0;
625+
*length = 0;
605626
*is_null = 0;
606627
*error = 0;
607628
char* input_key = args->args[0];
@@ -611,6 +632,7 @@ char* mcrypsi_hmac_sha512(UDF_INIT* initid, UDF_ARGS* args, char* result,
611632
int dst_size = 0;
612633
ret = crypsi_hmac_sha512(input_key, input_text, text_size, &dst, &dst_size);
613634
if (ret != 0) {
635+
*is_null = 1;
614636
*error = 1;
615637
return NULL;
616638
}

scripts/test.sql

+11-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ select mcrypsi_aes_192_gcm_encrypt('abc$#128djdyAgbjau&YAnmc', 'hello world') as
4848
select 'hello worldie' != mcrypsi_aes_192_gcm_decrypt('abc$#128djdyAgbjau&YAnmc', mcrypsi_aes_192_gcm_encrypt('abc$#128djdyAgbjau&YAnmc', 'hello world')) as res_mcrypsi_aes_192_gcm_encrypt_invalid;
4949

5050
select mcrypsi_aes_256_gcm_encrypt('abc$#128djdyAgbjau&YAnmcbagryt5x', 'hello world') as res;
51-
select 'hello worldie' != mcrypsi_aes_256_gcm_decrypt('abc$#128djdyAgbjau&YAnmcbagryt5x', mcrypsi_aes_256_gcm_encrypt('abc$#128djdyAgbjau&YAnmcbagryt5x', 'hello world')) as res_mcrypsi_aes_256_gcm_encrypt_invalid;
51+
select 'hello worldie' != mcrypsi_aes_256_gcm_decrypt('abc$#128djdyAgbjau&YAnmcbagryt5x', mcrypsi_aes_256_gcm_encrypt('abc$#128djdyAgbjau&YAnmcbagryt5x', 'hello world')) as res_mcrypsi_aes_256_gcm_encrypt_invalid;
52+
53+
-- AES with invalid key size --
54+
select mcrypsi_aes_128_gcm_encrypt('abc$#', 'hello world') as res;
55+
select 0 = (mcrypsi_aes_128_gcm_encrypt('abc$#', 'hello world') is not null) as res_mcrypsi_aes_128_gcm_encrypt_invalid_key;
56+
57+
select mcrypsi_aes_192_gcm_encrypt('abc$#', 'hello world') as res;
58+
select 0 = (mcrypsi_aes_192_gcm_encrypt('abc$#', 'hello world') is not null) as res_mcrypsi_aes_192_gcm_encrypt_invalid_key;
59+
60+
select mcrypsi_aes_256_gcm_encrypt('abc$#', 'hello world') as res;
61+
select 0 = (mcrypsi_aes_256_gcm_encrypt('abc$#', 'hello world') is not null) as res_mcrypsi_aes_256_gcm_encrypt_invalid_key;

0 commit comments

Comments
 (0)