Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
#include "dbms_crypto.hpp"
 
#include <cctype>
 
extern "C"
{
 
#include "common/cryptohash.h"
#include "common/md5.h"
#include "common/sha1.h"
#include "common/sha2.h"
#include "utils/builtins.h"
#include "varatt.h"
 
#ifdef USE_OPENSSL
#include <openssl/evp.h>
#include <openssl/opensslv.h>
#endif
 
PG_FUNCTION_INFO_V1(dbms_crypto_hash);
PG_FUNCTION_INFO_V1(dbms_crypto_hash_cfg);
PG_FUNCTION_INFO_V1(dbms_crypto_hash_clob);
PG_FUNCTION_INFO_V1(dbms_crypto_hash_blob);
 
PG_FUNCTION_INFO_V1(dbms_crypto_get_version);
 
/// @brief Версия RUDB пакета DBMS_CRYPTO (Oracle DBMS_CRYPTO пакет)
#define RUDB_DBMS_CRYPTO_VERSION "1.0.0"
 
static inline int
cryptohash_digest_size(pg_cryptohash_type type)
{
    switch (type)
    {
        case PG_MD5:
            return MD5_DIGEST_LENGTH;
        case PG_SHA1:
            return SHA1_DIGEST_LENGTH;
        case PG_SHA224:
            return PG_SHA224_DIGEST_LENGTH;
        case PG_SHA256:
            return PG_SHA256_DIGEST_LENGTH;
        case PG_SHA384:
            return PG_SHA384_DIGEST_LENGTH;
        case PG_SHA512:
            return PG_SHA512_DIGEST_LENGTH;
    }
 
    ereport(ERROR,
            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
             errmsg("ORA-06502: PL/SQL: numeric or value error")));
    return 0;
}
 
static inline bytea *
cryptohash_internal(pg_cryptohash_type type, bytea *input)
{
    pg_cryptohash_ctx *ctx;
    bytea *result;
    int digest_size = cryptohash_digest_size(type);
 
    result = (bytea *) palloc(VARHDRSZ + digest_size);
    SET_VARSIZE(result, VARHDRSZ + digest_size);
 
    ctx = pg_cryptohash_create(type);
    if (ctx == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("out of memory")));
 
    if (pg_cryptohash_init(ctx) < 0 ||
        pg_cryptohash_update(ctx, (const uint8 *) VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input)) < 0 ||
        pg_cryptohash_final(ctx, (uint8 *) VARDATA(result), digest_size) < 0)
    {
        const char *err = pg_cryptohash_error(ctx);
        pg_cryptohash_free(ctx);
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                 errmsg("%s", err ? err : "cryptohash failed")));
    }
 
    pg_cryptohash_free(ctx);
    return result;
}
 
#ifdef USE_OPENSSL
static inline bytea *
openssl_evp_digest_internal(bytea *input, const EVP_MD *md)
{
    EVP_MD_CTX *ctx;
    bytea *result;
    const uint8 *data = (const uint8 *) VARDATA_ANY(input);
    const Size len = VARSIZE_ANY_EXHDR(input);
    const int digest_size = EVP_MD_size(md);
    unsigned int out_len = 0;
 
    if (digest_size <= 0)
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                 errmsg("cryptohash failed")));
 
    ctx = EVP_MD_CTX_new();
    if (ctx == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("out of memory")));
 
    if (EVP_DigestInit_ex(ctx, md, NULL) != 1 ||
        EVP_DigestUpdate(ctx, data, len) != 1)
    {
        EVP_MD_CTX_free(ctx);
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                 errmsg("cryptohash failed")));
    }
 
    result = (bytea *) palloc(VARHDRSZ + digest_size);
    SET_VARSIZE(result, VARHDRSZ + digest_size);
 
    if (EVP_DigestFinal_ex(ctx, (unsigned char *) VARDATA(result), &out_len) != 1 ||
        out_len != (unsigned int) digest_size)
    {
        EVP_MD_CTX_free(ctx);
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                 errmsg("cryptohash failed")));
    }
 
    EVP_MD_CTX_free(ctx);
    return result;
}
 
static inline bytea *
openssl_evp_shake256_internal(bytea *input, Size out_bytes)
{
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
    EVP_MD_CTX *ctx;
    bytea *result;
    const uint8 *data = (const uint8 *) VARDATA_ANY(input);
    const Size len = VARSIZE_ANY_EXHDR(input);
 
    ctx = EVP_MD_CTX_new();
    if (ctx == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("out of memory")));
 
    if (EVP_DigestInit_ex(ctx, EVP_shake256(), NULL) != 1 ||
        EVP_DigestUpdate(ctx, data, len) != 1)
    {
        EVP_MD_CTX_free(ctx);
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                 errmsg("cryptohash failed")));
    }
 
    result = (bytea *) palloc(VARHDRSZ + out_bytes);
    SET_VARSIZE(result, VARHDRSZ + out_bytes);
 
    if (EVP_DigestFinalXOF(ctx, (unsigned char *) VARDATA(result), out_bytes) != 1)
    {
        EVP_MD_CTX_free(ctx);
        ereport(ERROR,
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                 errmsg("cryptohash failed")));
    }
 
    EVP_MD_CTX_free(ctx);
    return result;
#else
    ereport(ERROR,
            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
             errmsg("ORA-06502: PL/SQL: numeric or value error")));
    return NULL;
#endif
}
#endif
 
static inline int
hex_val(unsigned char c)
{
    if (c >= '0' && c <= '9')
        return c - '0';
    if (c >= 'A' && c <= 'F')
        return 10 + (c - 'A');
    if (c >= 'a' && c <= 'f')
        return 10 + (c - 'a');
    return -1;
}
 
static inline bytea *
maybe_decode_ascii_hex_raw(bytea *input)
{
    const unsigned char *p = (const unsigned char *) VARDATA_ANY(input);
    const Size n = VARSIZE_ANY_EXHDR(input);
 
    if (n == 0 || (n % 2) != 0)
        return input;
 
    for (Size i = 0; i < n; ++i)
    {
        if (hex_val(p[i]) < 0)
            return input;
    }
 
    const Size out_n = n / 2;
    bytea *out = (bytea *) palloc(VARHDRSZ + out_n);
    SET_VARSIZE(out, VARHDRSZ + out_n);
    unsigned char *dst = (unsigned char *) VARDATA(out);
 
    for (Size i = 0, j = 0; i < n; i += 2, ++j)
    {
        int hi = hex_val(p[i]);
        int lo = hex_val(p[i + 1]);
        dst[j] = (unsigned char) ((hi << 4) | lo);
    }
 
    return out;
}
 
Datum
dbms_crypto_hash(PG_FUNCTION_ARGS)
{
    LOCAL_FCINFO(fcinfo2, 3);
    InitFunctionCallInfoData(*fcinfo2, fcinfo->flinfo, 3, PG_GET_COLLATION(), fcinfo->context, NULL);
    fcinfo2->args[0].value = fcinfo->args[0].value;
    fcinfo2->args[0].isnull = fcinfo->args[0].isnull;
    fcinfo2->args[1].value = fcinfo->args[1].value;
    fcinfo2->args[1].isnull = fcinfo->args[1].isnull;
    fcinfo2->args[2].value = Int32GetDatum(0);
    fcinfo2->args[2].isnull = false;
 
    Datum result = dbms_crypto_hash_cfg(fcinfo2);
 
    if (fcinfo2->isnull)
        PG_RETURN_NULL();
 
    return result;
}
 
Datum
dbms_crypto_hash_cfg(PG_FUNCTION_ARGS)
{
    bytea *src;
    bytea *src_norm;
    int32 alg;
    int32 options;
    const int32 minimum_options = -256;
    pg_cryptohash_type type;
 
    if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
        PG_RETURN_NULL();
 
    src = PG_GETARG_BYTEA_PP(0);
    alg = PG_GETARG_INT32(1);
    options = PG_ARGISNULL(2) ? 0 : PG_GETARG_INT32(2);
 
    if (options < minimum_options)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-06502: invalid compatibility flags")));
 
    src_norm = maybe_decode_ascii_hex_raw(src);
 
    {
        const int32 supported_option_mask = 0x03;
        bool compatibility_mode = (options & 0x01) != 0;
        bool normalize_hex_input = (options & 0x02) != 0;
        Size effective_len = VARSIZE_ANY_EXHDR(src_norm);
 
        options &= supported_option_mask;
 
        if (compatibility_mode && src_norm != src)
            effective_len = VARSIZE_ANY_EXHDR(src_norm);
 
        if (normalize_hex_input && src_norm == src)
            effective_len = VARSIZE_ANY_EXHDR(src);
 
        (void) effective_len;
        (void) options;
    }
 
    switch (alg)
    {
        case 2:
            type = PG_MD5;
            break;
        case 3:
            type = PG_SHA1;
            break;
        case 4:
            type = PG_SHA256;
            break;
        case 5:
            type = PG_SHA384;
            break;
        case 6:
            type = PG_SHA512;
            break;
        case 7:
#if defined(USE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10101000L
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sm3()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                    errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 8:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sha3_224()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 9:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sha3_256()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 10:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sha3_384()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 11:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_norm, EVP_sha3_512()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 12:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_shake256_internal(src_norm, 64));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        default:
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
    }
 
    PG_RETURN_BYTEA_P(cryptohash_internal(type, src_norm));
}
 
Datum
dbms_crypto_hash_clob(PG_FUNCTION_ARGS)
{
    text *src;
    bytea *src_as_bytea;
    int32 alg;
    pg_cryptohash_type type;
 
    if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
        PG_RETURN_NULL();
 
    src = PG_GETARG_TEXT_PP(0);
    alg = PG_GETARG_INT32(1);
 
    src_as_bytea = (bytea *) src;
 
    switch (alg)
    {
        case 2:
            type = PG_MD5;
            break;
        case 3:
            type = PG_SHA1;
            break;
        case 4:
            type = PG_SHA256;
            break;
        case 5:
            type = PG_SHA384;
            break;
        case 6:
            type = PG_SHA512;
            break;
        case 7:
#if defined(USE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10101000L
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sm3()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 8:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sha3_224()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 9:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sha3_256()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 10:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sha3_384()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 11:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src_as_bytea, EVP_sha3_512()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 12:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_shake256_internal(src_as_bytea, 64));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        default:
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
    }
 
    PG_RETURN_BYTEA_P(cryptohash_internal(type, src_as_bytea));
}
 
Datum
dbms_crypto_hash_blob(PG_FUNCTION_ARGS)
{
    bytea *src;
    int32 alg;
    pg_cryptohash_type type;
 
    if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
        PG_RETURN_NULL();
 
    src = PG_GETARG_BYTEA_PP(0);
    alg = PG_GETARG_INT32(1);
 
    switch (alg)
    {
        case 2:
            type = PG_MD5;
            break;
        case 3:
            type = PG_SHA1;
            break;
        case 4:
            type = PG_SHA256;
            break;
        case 5:
            type = PG_SHA384;
            break;
        case 6:
            type = PG_SHA512;
            break;
        case 7:
#if defined(USE_OPENSSL) && OPENSSL_VERSION_NUMBER >= 0x10101000L
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sm3()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 8:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sha3_224()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 9:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sha3_256()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 10:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sha3_384()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 11:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_digest_internal(src, EVP_sha3_512()));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        case 12:
#ifdef USE_OPENSSL
            PG_RETURN_BYTEA_P(openssl_evp_shake256_internal(src, 64));
#else
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
#endif
        default:
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                     errmsg("ORA-06502: PL/SQL: numeric or value error")));
    }
 
    PG_RETURN_BYTEA_P(cryptohash_internal(type, src));
}
 
/// @brief Возвращает версию RUDB пакета DBMS_CRYPTO (Oracle DBMS_CRYPTO пакет)
Datum dbms_crypto_get_version(PG_FUNCTION_ARGS) {
    PG_RETURN_TEXT_P(cstring_to_text(RUDB_DBMS_CRYPTO_VERSION));
}
 
} // extern "C"