forked from crtsh/libx509pq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibx509pq.c
3523 lines (3043 loc) · 97.9 KB
/
libx509pq.c
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
/* libx509pq - a certificate parsing library for PostgreSQL
* Written by Rob Stradling
* Copyright (C) 2015-2017 COMODO CA Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define TRUE 1
#define FALSE 0
#include "postgres.h"
#include "plpgsql.h" /* _PG_init() */
#include "funcapi.h"
#include "fmgr.h"
#include "access/htup_details.h"
#include "utils/timestamp.h"
#include "utils/builtins.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#include <string.h>
#include <time.h>
#include "openssl/asn1.h"
#include "openssl/asn1t.h"
#include "openssl/bn.h"
#include "openssl/engine.h"
#include "openssl/err.h"
#include "openssl/evp.h"
#include "openssl/objects.h"
#include "openssl/ocsp.h"
#include "openssl/x509.h"
#include "openssl/x509v3.h"
#define MAX_OIDSTRING_LENGTH 80
#if OPENSSL_VERSION_NUMBER < 0x10100000L /* < 1.1.0 */
#define ASN1_STRING_get0_data ASN1_STRING_data
#define SIGNATURE_ALGORITHM X509_ALGOR
#define SIGNATURE_BIT_STRING ASN1_BIT_STRING
#define X509_get0_extensions(x) (x)->cert_info->extensions
#define X509_get0_notAfter X509_get_notAfter
#define X509_get0_notBefore X509_get_notBefore
#define X509_get0_tbs_sigalg(x) (x)->cert_info->signature
#define EVP_PKEY_get0_RSA(evp_pkey) ((evp_pkey)->pkey.rsa)
static void RSA_get0_key(
const RSA* r,
const BIGNUM** n,
const BIGNUM** e,
const BIGNUM** d
)
{
if (n != NULL)
*n = r->n;
if (e != NULL)
*e = r->e;
if (d != NULL)
*d = r->d;
}
#else /* >= 1.1.0 */
#define SIGNATURE_ALGORITHM const X509_ALGOR
#define SIGNATURE_BIT_STRING const ASN1_BIT_STRING
#endif
#if OPENSSL_VERSION_NUMBER < 0x10002000L /* < 1.0.2 */
#define X509_get_signature_nid(x) (x)->sig_alg->algorithm
#define X509_GET_SIGNATURE(psig, x) (*(psig)) = (x)->signature
#define X509_GET_SIGALGNID(palg, x) (*(palg)) = (x)->sig_alg
static int i2d_re_X509_tbs(
X509* x,
unsigned char** pp
)
{
x->cert_info->enc.modified = 1;
return i2d_X509_CINF(x->cert_info, pp);
}
#else /* >= 1.0.2 */
#define X509_GET_SIGNATURE(psig, x) X509_get0_signature(psig, NULL, x)
#define X509_GET_SIGALGNID(palg, x) X509_get0_signature(NULL, palg, x)
#endif
/* Define the old draft Basic Constraints extension, which is supported by
CryptoAPI and used in the SGC cross-certs.
See http://tools.ietf.org/html/draft-ietf-pkix-ipki-part1-01 */
typedef struct BASIC_CONSTRAINTS_OLD_st {
ASN1_BIT_STRING* subjtype;
ASN1_INTEGER* pathlen;
} BASIC_CONSTRAINTS_OLD;
DECLARE_ASN1_FUNCTIONS(BASIC_CONSTRAINTS_OLD)
static X509V3_EXT_METHOD v3_bcOld = {
NID_undef, 0,
ASN1_ITEM_ref(BASIC_CONSTRAINTS_OLD),
0, 0, 0, 0,
0, 0,
NULL/*(X509V3_EXT_I2V)i2v_BASIC_CONSTRAINTS*/,
NULL/*(X509V3_EXT_V2I)v2i_BASIC_CONSTRAINTS*/,
NULL, NULL,
NULL
};
ASN1_SEQUENCE(BASIC_CONSTRAINTS_OLD) = {
ASN1_OPT(BASIC_CONSTRAINTS_OLD, subjtype, ASN1_BIT_STRING),
ASN1_OPT(BASIC_CONSTRAINTS_OLD, pathlen, ASN1_INTEGER)
} ASN1_SEQUENCE_END(BASIC_CONSTRAINTS_OLD)
IMPLEMENT_ASN1_FUNCTIONS(BASIC_CONSTRAINTS_OLD)
#define CERT_CA_SUBJECT_FLAG 0x80
#define CERT_END_ENTITY_SUBJECT_FLAG 0x40
/* The old "Root SGC Authority" certificate is treated by CryptoAPI as a CA
certificate even though it doesn't contain a Basic Constraints extension.
We need to special case this certificate. We'll identify it by its
signature */
static const unsigned char g_rootSGCAuthority_sig[] = {
0x2b, 0x02, 0x2b, 0x37, 0x66, 0xa5, 0xd1, 0x8c, 0x3e, 0x20, 0x08, 0x1a,
0x0c, 0xb7, 0xf5, 0x63, 0xcb, 0xc6, 0xdd, 0x9b, 0x62, 0x52, 0x32, 0xbc,
0x33, 0x74, 0x7a, 0xde, 0xb0, 0x80, 0x05, 0xfa, 0xe5, 0xb5, 0xe4, 0xf7,
0xf1, 0xd7, 0xa0, 0x95, 0x5c, 0x6c, 0x05, 0x9b, 0x2f, 0x03, 0x4b, 0xb7,
0x8a, 0x95, 0x0e, 0xb0, 0x06, 0x80, 0xa0, 0x2a, 0x1b, 0xa4, 0x09, 0x58,
0xbd, 0x87, 0xd4, 0x38, 0x44, 0xb4, 0x71, 0x7b, 0xfb, 0x74, 0xa2, 0x89,
0x48, 0xe6, 0x5f, 0xab, 0x9a, 0xa4, 0x0a, 0x38, 0xcc, 0x57, 0xa1, 0x14,
0x2c, 0x5c, 0xee, 0xc2, 0x13, 0x81, 0x00, 0xc3, 0x2d, 0xb1, 0x70, 0xde,
0x9f, 0xb1, 0x70, 0x43, 0x7e, 0x22, 0xa0, 0x77, 0x96, 0xc8, 0xdf, 0x99,
0xdc, 0xa6, 0x4e, 0xb3, 0xb5, 0x74, 0x34, 0x13, 0x12, 0x24, 0xa2, 0x6b,
0x95, 0x80, 0xcf, 0xaa, 0x4a, 0x68, 0xb1, 0x77, 0x27, 0x98, 0xef, 0xaa,
0x62, 0xd3, 0x22, 0x81, 0x33, 0x2b, 0x12, 0x50, 0xef, 0x16, 0x86, 0xe6,
0x9a, 0x5a, 0x73, 0x89, 0x6d, 0x83, 0xf2, 0x08, 0xa3, 0x13, 0xab, 0x05,
0xd5, 0x6e, 0x68, 0xf6, 0x90, 0xa4, 0x4a, 0x9f, 0x7c, 0x4c, 0x5d, 0x8f,
0x58, 0xf3, 0x11, 0x4c, 0xc7, 0x08, 0x51, 0xea, 0x76, 0xd1, 0xb5, 0x55,
0x32, 0x3f, 0xff, 0x67, 0xef, 0x35, 0x8c, 0x89, 0xd3, 0xc6, 0x75, 0x15,
0x68, 0x9f, 0x67, 0x46, 0x9c, 0x94, 0x41, 0xf5, 0x76, 0x51, 0x86, 0xac,
0x91, 0x75, 0xec, 0xb6, 0xf7, 0x00, 0x40, 0x5b, 0xfe, 0x61, 0xd8, 0x33,
0x2d, 0x37, 0x65, 0x8b, 0x94, 0xd9, 0x97, 0x21, 0x15, 0x2c, 0x13, 0x49,
0xff, 0xde, 0xb7, 0x83, 0xd9, 0xae, 0xc4, 0xce, 0x24, 0xb2, 0x50, 0xdf,
0x75, 0x14, 0x12, 0x8c, 0x46, 0xa4, 0xac, 0xef, 0x4c, 0x72, 0x00, 0x00,
0xe1, 0x4c, 0x8e, 0xee
};
/* Algorithm names */
typedef struct tAlgorithm {
int m_nid;
char* m_name;
} tAlgorithm;
/* Hash Algorithm Names */
static const tAlgorithm g_hashAlgorithms[] = {
{ NID_md2, "MD2" },
{ NID_md4, "MD4" },
{ NID_md5, "MD5" },
{ NID_sha, "SHA" },
{ NID_sha1, "SHA-1" },
{ NID_sha224, "SHA-224" },
{ NID_sha256, "SHA-256" },
{ NID_sha384, "SHA-384" },
{ NID_sha512, "SHA-512" },
{ NID_ripemd160, "RIPEMD-160" },
{ NID_mdc2, "MDC-2" },
{ NID_id_GostR3411_94, "GOST R 34.11-94" }
};
/* Public Key Algorithm Names */
static const tAlgorithm g_pkeyAlgorithms[] = {
{ NID_rsaEncryption, "RSA" },
{ NID_rsa, "RSA" },
{ NID_dsa, "DSA" },
{ NID_dsa_2, "DSA" },
{ NID_X9_62_id_ecPublicKey, "ECDSA" },
{ NID_id_GostR3410_94, "GOST R 34.10-94" },
{ NID_id_GostR3410_94_cc, "GOST 34.10-94 Cryptocom" },
{ NID_id_GostR3410_2001, "GOST R 34.10-2001" },
{ NID_id_GostR3410_2001_cc, "GOST 34.10-2001 Cryptocom" }
};
static char g_error[] = "ERROR!";
static ENGINE* g_gostEngine = NULL;
static int g_nid_scts = NID_undef;
static int g_nid_poison = NID_undef;
#define ROCA_PRINTS_LENGTH 17
static unsigned char g_primes[ROCA_PRINTS_LENGTH] = {
11, 13, 17, 19, 37, 53, 61, 71, 73, 79, 97, 103, 107, 109, 127, 151, 157
};
BIGNUM* g_prints[ROCA_PRINTS_LENGTH];
BIGNUM* g_one = NULL;
/******************************************************************************
* rocacheck_init() *
******************************************************************************/
static void rocacheck_init()
{
memset(g_prints, '\0', sizeof(BIGNUM*) * ROCA_PRINTS_LENGTH);
(void)BN_dec2bn(&g_prints[0], "1026");
(void)BN_dec2bn(&g_prints[1], "5658");
(void)BN_dec2bn(&g_prints[2], "107286");
(void)BN_dec2bn(&g_prints[3], "199410");
(void)BN_dec2bn(&g_prints[4], "67109890");
(void)BN_dec2bn(&g_prints[5], "5310023542746834");
(void)BN_dec2bn(&g_prints[6], "1455791217086302986");
(void)BN_dec2bn(&g_prints[7], "20052041432995567486");
(void)BN_dec2bn(&g_prints[8], "6041388139249378920330");
(void)BN_dec2bn(&g_prints[9], "207530445072488465666");
(void)BN_dec2bn(&g_prints[10], "79228162521181866724264247298");
(void)BN_dec2bn(&g_prints[11], "1760368345969468176824550810518");
(void)BN_dec2bn(&g_prints[12], "50079290986288516948354744811034");
(void)BN_dec2bn(&g_prints[13], "473022961816146413042658758988474");
(void)BN_dec2bn(&g_prints[14], "144390480366845522447407333004847678774");
(void)BN_dec2bn(&g_prints[15], "1800793591454480341970779146165214289059119882");
(void)BN_dec2bn(&g_prints[16], "126304807362733370595828809000324029340048915994");
(void)BN_dec2bn(&g_one, "1");
}
/******************************************************************************
* rocacheck_cleanup() *
******************************************************************************/
static void rocacheck_cleanup()
{
int i;
BN_free(g_one);
for (i = 0; i < ROCA_PRINTS_LENGTH; i++)
BN_free(g_prints[i]);
}
/******************************************************************************
* _PG_init() *
******************************************************************************/
void _PG_init(void)
{
/* We need MD2 to verify old MD2/RSA certificate signatures, but
OpenSSL_add_all_digests() no longer enables MD2 by default */
OpenSSL_add_all_digests();
#ifndef OPENSSL_NO_MD2
EVP_add_digest(EVP_md2());
#endif
ERR_load_crypto_strings();
/* Define the OID for the draft Basic Constraints extension */
v3_bcOld.ext_nid = OBJ_create(
"2.5.29.10", "bCold",
"draft-ietf-pkix-ipki-part1-01 Basic Constraints"
);
/* Load all built-in engines */
ENGINE_load_builtin_engines();
/* Enable the GOST engine */
g_gostEngine = ENGINE_by_id("gost");
if (g_gostEngine && ENGINE_init(g_gostEngine))
ENGINE_set_default(g_gostEngine, ENGINE_METHOD_ALL);
#ifdef NID_ct_precert_scts
g_nid_scts = NID_ct_precert_scts;
#else
g_nid_scts = OBJ_create(
"1.3.6.1.4.1.11129.2.4.2", "ct_precert_scts",
"CT Precertificate SCTs"
);
#endif
#ifdef NID_ct_poison
g_nid_poison = NID_ct_poison;
#else
g_nid_poison = OBJ_create(
"1.3.6.1.4.1.11129.2.4.3", "ct_precert_poison",
"CT Precertificate Poison"
);
#endif
rocacheck_init();
}
/******************************************************************************
* _PG_fini() *
******************************************************************************/
extern void _PG_fini(void);
void _PG_fini(void)
{
if (g_gostEngine) {
ENGINE_finish(g_gostEngine);
ENGINE_free(g_gostEngine);
}
ENGINE_cleanup();
EVP_cleanup();
OBJ_cleanup();
ERR_free_strings();
rocacheck_cleanup();
}
/******************************************************************************
* ASN1_GENERALIZEDTIME_parse() *
* Parse a GeneralizedTime value into a "struct tm". *
* *
* IN: v_asn1GeneralizedTime - an OpenSSL GeneralizedTime object. *
* v_time - the "struct tm" object to populate. *
* *
* OUT: v_time - the "struct tm" object, populated. *
* *
* Returns: 1 = Successful; 0 = An error occurred. *
******************************************************************************/
static int ASN1_GENERALIZEDTIME_parse(
const ASN1_GENERALIZEDTIME* const v_asn1GeneralizedTime,
struct tm* const v_time
)
{
char *t_data;
int i;
i = v_asn1GeneralizedTime->length;
t_data = (char*)v_asn1GeneralizedTime->data;
if (i < 12)
goto label_error;
for (i = 0; i < 12; i++)
if ((t_data[i] > '9') || (t_data[i] < '0'))
goto label_error;
v_time->tm_year = (
((t_data[0] - '0') * 1000) + ((t_data[1] - '0') * 100)
+ ((t_data[2] - '0') * 10) + (t_data[3] - '0')
) - 1900;
v_time->tm_mon = (t_data[4] - '0') * 10 + (t_data[5] - '0');
if ((v_time->tm_mon > 12) || (v_time->tm_mon < 1))
goto label_error;
v_time->tm_mon--;
v_time->tm_mday = (t_data[6] - '0') * 10 + (t_data[7] - '0');
v_time->tm_hour = (t_data[8] - '0') * 10 + (t_data[9] - '0');
v_time->tm_min = (t_data[10] - '0') * 10 + (t_data[11] - '0');
if ((t_data[12] >= '0') && (t_data[12] <= '9')
&& (t_data[13] >= '0') && (t_data[13] <= '9'))
v_time->tm_sec = (t_data[12] - '0') * 10 + (t_data[13] - '0');
else
v_time->tm_sec = 0;
return 1;
label_error:
return 0;
}
/******************************************************************************
* ASN1_UTCTIME_parse() *
* Parse a UTCTime value into a "struct tm". *
* *
* IN: v_asn1GeneralizedTime - an OpenSSL UTCTime object. *
* v_time - the "struct tm" object to populate. *
* *
* OUT: v_time - the "struct tm" object, populated. *
* *
* Returns: 1 = Successful; 0 = An error occurred. *
******************************************************************************/
static int ASN1_UTCTIME_parse(
const ASN1_UTCTIME* const v_asn1UTCTime,
struct tm* const v_time
)
{
char* t_data;
int i;
i = v_asn1UTCTime->length;
t_data = (char*)v_asn1UTCTime->data;
if (i < 10)
goto label_error;
for (i = 0; i < 10; i++)
if ((t_data[i] > '9') || (t_data[i] < '0'))
goto label_error;
v_time->tm_year = (t_data[0] - '0') * 10 + (t_data[1] - '0');
if (v_time->tm_year < 50)
v_time->tm_year += 100;
v_time->tm_mon = (t_data[2] - '0') * 10 + (t_data[3] - '0');
if ((v_time->tm_mon > 12) || (v_time->tm_mon < 1))
goto label_error;
v_time->tm_mon--;
v_time->tm_mday = (t_data[4] - '0') * 10 + (t_data[5] - '0');
v_time->tm_hour = (t_data[6] - '0') * 10 + (t_data[7] - '0');
v_time->tm_min = (t_data[8] - '0') * 10 + (t_data[9] - '0');
if ((t_data[10] >= '0') && (t_data[10] <= '9')
&& (t_data[11] >= '0') && (t_data[11] <= '9'))
v_time->tm_sec = (t_data[10] - '0') * 10 + (t_data[11] - '0');
else
v_time->tm_sec = 0;
return 1;
label_error:
return 0;
}
/******************************************************************************
* ASN1_TIME_parse() *
* Parse a Time (from X.509 AuthenticationFramework) value into a *
* "struct tm". *
* *
* IN: v_asn1Time - a Time object. *
* v_time - the "struct tm" object to populate. *
* *
* OUT: v_time - the "struct tm" object, populated. *
* *
* Returns: 1 = Successful; 0 = An error occurred. *
******************************************************************************/
static int ASN1_TIME_parse(
const ASN1_TIME* const v_asn1Time,
struct tm* const v_time
)
{
if (v_asn1Time->type == V_ASN1_UTCTIME)
return ASN1_UTCTIME_parse(v_asn1Time, v_time);
else if (v_asn1Time->type == V_ASN1_GENERALIZEDTIME)
return ASN1_GENERALIZEDTIME_parse(v_asn1Time, v_time);
return 0;
}
/******************************************************************************
* x509_issuername() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_issuername);
Datum x509_issuername(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
BIO* t_bio;
bytea* t_bytea = NULL;
text* t_text = NULL;
const unsigned char* t_pointer = NULL;
char* t_string = NULL;
long t_size;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509) {
t_text = palloc(strlen(g_error) + VARHDRSZ);
SET_VARSIZE(t_text, strlen(g_error) + VARHDRSZ);
memcpy((void*)VARDATA(t_text), g_error, strlen(g_error));
}
else {
/* Create a memory BIO and tell it to make sure that it clears
up all its memory when we close it later */
t_bio = BIO_new(BIO_s_mem());
(void)BIO_set_close(t_bio, BIO_CLOSE);
/* Express the certificate's Issuer Name as a one-line
string */
(void)X509_NAME_print_ex(
t_bio, X509_get_issuer_name(t_x509), 0,
PG_ARGISNULL(1) ? ((ASN1_STRFLGS_RFC2253
| ASN1_STRFLGS_ESC_QUOTE
| XN_FLAG_SEP_CPLUS_SPC
| XN_FLAG_FN_SN)
& ~ASN1_STRFLGS_ESC_MSB)
: PG_GETARG_INT32(1)
);
/* Get a pointer to the Issuer Name string and its size */
t_size = BIO_get_mem_data(t_bio, &t_string);
/* Copy the Issuer Name string to the return parameter */
t_text = palloc(t_size + VARHDRSZ);
SET_VARSIZE(t_text, t_size + VARHDRSZ);
memcpy((void*)VARDATA(t_text), t_string, t_size);
/* Free stuff */
BIO_free(t_bio);
X509_free(t_x509);
}
PG_RETURN_TEXT_P(t_text);
}
/******************************************************************************
* x509_keyalgorithm() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_keyalgorithm);
Datum x509_keyalgorithm(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
EVP_PKEY* t_publicKey = NULL;
bytea* t_bytea = NULL;
text* t_text = NULL;
const unsigned char* t_pointer = NULL;
char* t_string = NULL;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509)
goto label_error;
/* Extract the Public Key from this Certificate */
t_publicKey = X509_get_pubkey(t_x509);
if (!t_publicKey)
goto label_error;
/* Get the name of the algorithm used by this key */
switch (EVP_PKEY_id(t_publicKey)) {
case EVP_PKEY_RSA: case EVP_PKEY_RSA2:
t_string = "RSA";
break;
case EVP_PKEY_DSA: case EVP_PKEY_DSA1:
case EVP_PKEY_DSA2: case EVP_PKEY_DSA3:
case EVP_PKEY_DSA4:
t_string = "DSA";
break;
case EVP_PKEY_DH:
t_string = "DH";
break;
case EVP_PKEY_EC:
t_string = "EC";
break;
case EVP_PKEY_NONE:
t_string = "NONE";
break;
default:
goto label_error;
}
t_text = palloc(strlen(t_string) + VARHDRSZ);
SET_VARSIZE(t_text, strlen(t_string) + VARHDRSZ);
memcpy((void*)VARDATA(t_text), t_string, strlen(t_string));
goto label_return;
label_error:
t_text = palloc(strlen(g_error) + VARHDRSZ);
SET_VARSIZE(t_text, strlen(g_error) + VARHDRSZ);
memcpy((void*)VARDATA(t_text), g_error, strlen(g_error));
label_return:
if (t_publicKey)
EVP_PKEY_free(t_publicKey);
if (t_x509)
X509_free(t_x509);
PG_RETURN_TEXT_P(t_text);
}
/******************************************************************************
* x509_keysize() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_keysize);
Datum x509_keysize(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
EVP_PKEY* t_publicKey = NULL;
bytea* t_bytea = NULL;
int32 t_int32 = -1;
const unsigned char* t_pointer = NULL;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (t_x509) {
t_publicKey = X509_get_pubkey(t_x509);
if (t_publicKey) {
t_int32 = EVP_PKEY_bits(t_publicKey);
EVP_PKEY_free(t_publicKey);
}
X509_free(t_x509);
}
PG_RETURN_INT32(t_int32);
}
/******************************************************************************
* x509_notafter() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_notafter);
Datum x509_notafter(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
bytea* t_bytea = NULL;
Timestamp t_timestamp = 0;
struct tm t_time;
int t_iResult;
const unsigned char* t_pointer = NULL;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509)
PG_RETURN_NULL();
t_iResult = ASN1_TIME_parse(X509_get0_notAfter(t_x509), &t_time);
X509_free(t_x509);
if (!t_iResult)
PG_RETURN_NULL();
t_timestamp = (timegm(&t_time) - 946684800) * USECS_PER_SEC;
PG_RETURN_TIMESTAMP(t_timestamp);
}
/******************************************************************************
* x509_notbefore() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_notbefore);
Datum x509_notbefore(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
bytea* t_bytea = NULL;
Timestamp t_timestamp = 0;
struct tm t_time;
int t_iResult;
const unsigned char* t_pointer = NULL;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509)
PG_RETURN_NULL();
t_iResult = ASN1_TIME_parse(X509_get0_notBefore(t_x509), &t_time);
X509_free(t_x509);
if (!t_iResult)
PG_RETURN_NULL();
t_timestamp = (timegm(&t_time) - 946684800) * USECS_PER_SEC;
PG_RETURN_TIMESTAMP(t_timestamp);
}
/******************************************************************************
* x509_publickeymd5() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_publickeymd5);
Datum x509_publickeymd5(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
EVP_PKEY* t_publicKey = NULL;
bytea* t_bytea = NULL;
bytea* t_publicKeyMD5 = NULL;
const unsigned char* t_pointer = NULL;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509)
goto label_error;
t_publicKey = X509_get_pubkey(t_x509);
if (!t_publicKey)
goto label_error;
t_publicKeyMD5 = palloc(VARHDRSZ + 16);
SET_VARSIZE(t_publicKeyMD5, VARHDRSZ + 16);
if (!X509_pubkey_digest(t_x509, EVP_md5(),
(unsigned char*)t_publicKeyMD5 + VARHDRSZ,
NULL))
goto label_error;
EVP_PKEY_free(t_publicKey);
X509_free(t_x509);
PG_RETURN_BYTEA_P(t_publicKeyMD5);
label_error:
if (t_publicKey)
EVP_PKEY_free(t_publicKey);
if (t_x509)
X509_free(t_x509);
PG_RETURN_NULL();
}
/******************************************************************************
* x509_publickey() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_publickey);
Datum x509_publickey(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
EVP_PKEY* t_publicKey = NULL;
bytea* t_bytea = NULL;
bytea* t_derPublicKey = NULL;
const unsigned char* t_pointer = NULL;
unsigned char* t_pointer2 = NULL;
int t_derPublicKey_size;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509)
goto label_error;
t_publicKey = X509_get_pubkey(t_x509);
if (!t_publicKey)
goto label_error;
t_derPublicKey_size = i2d_PUBKEY(t_publicKey, NULL);
if (t_derPublicKey_size < 0)
goto label_error;
t_derPublicKey = palloc(VARHDRSZ + t_derPublicKey_size);
SET_VARSIZE(t_derPublicKey, VARHDRSZ + t_derPublicKey_size);
t_pointer2 = (unsigned char*)VARDATA(t_derPublicKey);
if (i2d_PUBKEY(t_publicKey, &t_pointer2) < 0)
goto label_error;
EVP_PKEY_free(t_publicKey);
X509_free(t_x509);
PG_RETURN_BYTEA_P(t_derPublicKey);
label_error:
if (t_publicKey)
EVP_PKEY_free(t_publicKey);
if (t_x509)
X509_free(t_x509);
PG_RETURN_NULL();
}
/******************************************************************************
* x509_rsamodulus() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_rsamodulus);
Datum x509_rsamodulus(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
EVP_PKEY* t_publicKey = NULL;
const BIGNUM* t_modulus = NULL;
bytea* t_bytea = NULL;
bytea* t_derModulus = NULL;
const unsigned char* t_pointer = NULL;
int t_derModulus_size;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509)
goto label_error;
t_publicKey = X509_get_pubkey(t_x509);
if (!t_publicKey || (EVP_PKEY_id(t_publicKey) != EVP_PKEY_RSA))
goto label_error;
RSA_get0_key(EVP_PKEY_get0_RSA(t_publicKey), &t_modulus, NULL, NULL);
t_derModulus_size = BN_num_bytes(t_modulus);
t_derModulus = palloc(VARHDRSZ + t_derModulus_size);
SET_VARSIZE(t_derModulus, VARHDRSZ + t_derModulus_size);
if (BN_bn2bin(t_modulus, (unsigned char*)VARDATA(t_derModulus)) != t_derModulus_size)
goto label_error;
EVP_PKEY_free(t_publicKey);
X509_free(t_x509);
PG_RETURN_BYTEA_P(t_derModulus);
label_error:
if (t_publicKey)
EVP_PKEY_free(t_publicKey);
if (t_x509)
X509_free(t_x509);
PG_RETURN_NULL();
}
/******************************************************************************
* x509_serialnumber() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_serialnumber);
Datum x509_serialnumber(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
ASN1_INTEGER* t_asn1Integer;
bytea* t_bytea = NULL;
bytea* t_serialNumber = NULL;
unsigned char* t_pointer = NULL;
int t_size;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(
NULL, (const unsigned char**)&t_pointer,
VARSIZE(t_bytea) - VARHDRSZ
);
if (!t_x509)
PG_RETURN_NULL();
t_asn1Integer = X509_get_serialNumber(t_x509);
t_size = i2d_ASN1_INTEGER(t_asn1Integer, NULL);
if ((t_size < 0) || (t_size > 129)) /* Maximum 1 length octet */
PG_RETURN_NULL();
t_serialNumber = palloc(VARHDRSZ + t_size - 2);
t_pointer = (unsigned char*)t_serialNumber + VARHDRSZ - 2;
/* The tag octet and length octet are decoded into the last 2 bytes of
the VARHDRSZ section... */
(void)i2d_ASN1_INTEGER(t_asn1Integer, &t_pointer);
/* ...and then overwritten */
SET_VARSIZE(t_serialNumber, VARHDRSZ + t_size - 2);
X509_free(t_x509);
PG_RETURN_BYTEA_P(t_serialNumber);
}
/******************************************************************************
* x509_signaturehashalgorithm() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_signaturehashalgorithm);
Datum x509_signaturehashalgorithm(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
SIGNATURE_ALGORITHM* t_sigAlg;
bytea* t_bytea = NULL;
text* t_text = NULL;
const unsigned char* t_pointer = NULL;
char* t_string = g_error;
int t_iResult;
int t_sigAlgNID;
int t_sigHashAlgNID;
int t_sigKeyAlgNID;
int l_algNo;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509)
goto label_return;
/* Get the names of the algorithms used to generate the signature */
X509_GET_SIGALGNID(&t_sigAlg, t_x509);
t_sigAlgNID = OBJ_obj2nid(t_sigAlg->algorithm);
t_iResult = OBJ_find_sigid_algs(
t_sigAlgNID, &t_sigHashAlgNID, &t_sigKeyAlgNID
);
if (!t_iResult)
goto label_return;
/* Get the signature's hash algorithm name */
for (l_algNo = 0; l_algNo < (sizeof(g_hashAlgorithms)
/ sizeof(tAlgorithm)); l_algNo++)
if (g_hashAlgorithms[l_algNo].m_nid == t_sigHashAlgNID) {
t_string = g_hashAlgorithms[l_algNo].m_name;
break;
}
label_return:
t_text = palloc(strlen(t_string) + VARHDRSZ);
SET_VARSIZE(t_text, strlen(t_string) + VARHDRSZ);
memcpy((void*)VARDATA(t_text), t_string, strlen(t_string));
if (t_x509)
X509_free(t_x509);
PG_RETURN_TEXT_P(t_text);
}
/******************************************************************************
* x509_signaturekeyalgorithm() *
******************************************************************************/
PG_FUNCTION_INFO_V1(x509_signaturekeyalgorithm);
Datum x509_signaturekeyalgorithm(
PG_FUNCTION_ARGS
)
{
X509* t_x509 = NULL;
SIGNATURE_ALGORITHM* t_sigAlg;
bytea* t_bytea = NULL;
text* t_text = NULL;
const unsigned char* t_pointer = NULL;
char* t_string = g_error;
int t_iResult;
int t_sigAlgNID;
int t_sigHashAlgNID;
int t_sigKeyAlgNID;
int l_algNo;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
t_bytea = PG_GETARG_BYTEA_P(0);
t_pointer = (unsigned char*)VARDATA(t_bytea);
t_x509 = d2i_X509(NULL, &t_pointer, VARSIZE(t_bytea) - VARHDRSZ);
if (!t_x509)
goto label_return;
/* Get the names of the algorithms used to generate the signature */
X509_GET_SIGALGNID(&t_sigAlg, t_x509);
t_sigAlgNID = OBJ_obj2nid(t_sigAlg->algorithm);
t_iResult = OBJ_find_sigid_algs(
t_sigAlgNID, &t_sigHashAlgNID, &t_sigKeyAlgNID
);
if (!t_iResult)
goto label_return;
/* Get the signature's key algorithm name */
for (l_algNo = 0; l_algNo < (sizeof(g_pkeyAlgorithms)
/ sizeof(tAlgorithm)); l_algNo++)
if (g_pkeyAlgorithms[l_algNo].m_nid == t_sigKeyAlgNID) {
t_string = g_pkeyAlgorithms[l_algNo].m_name;
break;
}
label_return:
t_text = palloc(strlen(t_string) + VARHDRSZ);
SET_VARSIZE(t_text, strlen(t_string) + VARHDRSZ);
memcpy((void*)VARDATA(t_text), t_string, strlen(t_string));
if (t_x509)
X509_free(t_x509);