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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
|
/* certwatch_db - Database schema
* 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/>.
*/
CREATE OR REPLACE FUNCTION web_apis(
name text,
paramNames text[],
paramValues text[]
) RETURNS text
AS $$
DECLARE
c_params text[] := ARRAY[
'd', 'Download Certificate', NULL,
'c', 'ID', 'SHA-1(Certificate)', 'SHA-256(Certificate)', NULL,
'id', 'ID', NULL,
'sha1', 'SHA-1(Certificate)', NULL,
'sha256', 'SHA-256(Certificate)', NULL,
'asn1', 'Certificate ASN.1', NULL,
'ctid', 'CT Entry ID', NULL,
'ca', 'CA ID', 'CA Name', NULL,
'caid', 'CA ID', NULL,
'caname', 'CA Name', NULL,
'serial', 'Serial Number', NULL,
'ski', 'Subject Key Identifier', NULL,
'spkisha1', 'SHA-1(SubjectPublicKeyInfo)', NULL,
'spkisha256', 'SHA-256(SubjectPublicKeyInfo)', NULL,
'subjectsha1', 'SHA-1(Subject)', NULL,
'identity', 'Identity', NULL,
'commonname', 'Common Name', NULL,
'cn', 'Common Name', NULL,
'emailaddress', 'Email Address', NULL,
'e', 'Email Address', NULL,
'organizationalunitname', 'Organizational Unit Name', NULL,
'ou', 'Organizational Unit Name', NULL,
'organizationname', 'Organization Name', NULL,
'o', 'Organization Name', NULL,
'dnsname', 'Domain Name', NULL,
'domain', 'Domain Name', NULL,
'rfc822name', 'Email Address (SAN)', NULL,
'esan', 'Email Address (SAN)', NULL,
'ipaddress', 'IP Address', NULL,
'ip', 'IP Address', NULL,
'q', 'ID', 'SHA-1(Certificate)', 'SHA-256(Certificate)', 'Identity', NULL,
'a', 'Advanced', NULL,
's', 'Simple', NULL,
'cablint', 'CA/B Forum lint', NULL,
'x509lint', 'X.509 lint', NULL,
'zlint', 'ZLint', NULL,
'lint', 'Lint', NULL
];
t_paramNo integer;
t_paramName text;
t_value text;
t_type text := 'Simple';
t_cmd text;
t_bytea bytea;
t_output text;
t_outputType text;
t_title text;
t_summary text;
t_b64Certificate text;
t_certificateID certificate.ID%TYPE;
t_certificateSHA1 bytea;
t_certificateSHA256 bytea;
t_certificate certificate.CERTIFICATE%TYPE;
t_tbsCertificate bytea;
t_certSummary text;
t_caID ca.ID%TYPE;
t_caName ca.NAME%TYPE;
t_serialNumber bytea;
t_spkiSHA256 bytea;
t_nameType name_type;
t_text text;
t_offset integer;
t_pos1 integer;
t_temp0 text;
t_temp text;
t_temp2 text;
t_temp3 text;
t_select text;
t_from text;
t_where text;
t_nameValue text;
t_certID_field text;
t_query text;
t_sort integer;
t_groupBy text := 'none';
t_groupByParameter text := 'none';
t_direction text;
t_oppositeDirection text;
t_dirSymbol text;
t_issuerO text;
t_issuerOParameter text;
t_orderBy text := 'ASC';
t_matchType text := '=';
t_opt text;
t_maxAge timestamp;
t_cacheResponse boolean := FALSE;
t_useCachedResponse boolean := FALSE;
t_linter linter_type;
t_linters text;
t_showCABLint boolean;
t_showX509Lint boolean;
t_showZLint boolean;
t_showMetadata boolean;
t_certType integer;
t_showMozillaDisclosure boolean;
t_ctp ca_trust_purpose%ROWTYPE;
t_ctp2 ca_trust_purpose%ROWTYPE;
t_ctp3 ca_trust_purpose%ROWTYPE;
t_useReverseIndex boolean := FALSE;
t_joinToCertificate_table text;
t_joinToCTLogEntry text;
t_showIdentity boolean;
t_minNotBefore date;
t_minNotBeforeString text;
t_excludeExpired text;
t_excludeAffectedCerts text;
t_excludeCAs integer[];
t_excludeCAsString text;
t_searchProvider text;
t_issuerCAID certificate.ISSUER_CA_ID%TYPE;
t_issuerCAID_table text;
t_feedUpdated timestamp;
t_caPublicKey ca.PUBLIC_KEY%TYPE;
t_count integer;
t_count2 integer;
t_pageNo integer;
t_resultsPerPage integer := 100;
l_record RECORD;
l_record2 RECORD;
t_purposeOID text;
t_purpose text;
t_cacheControlMaxAge integer := 300;
t_versions text[];
t_date date;
t_onlyOneChain boolean;
BEGIN
FOR t_paramNo IN 1..array_length(c_params, 1) LOOP
IF t_cmd IS NULL THEN
t_cmd := c_params[t_paramNo];
END IF;
IF t_value IS NULL THEN
t_paramName := c_params[t_paramNo];
t_value := coalesce(
btrim(get_parameter(t_paramName, paramNames, paramValues)), ''
);
ELSIF t_value = '' THEN
IF c_params[t_paramNo] IS NULL THEN
t_value := NULL;
t_cmd := NULL;
END IF;
ELSE
t_type := c_params[t_paramNo];
BEGIN
t_bytea := decode(translate(t_value, ':', ''), 'hex');
EXCEPTION
WHEN invalid_parameter_value THEN
BEGIN
t_bytea := decode(
'0' || translate(t_value, ':', ''), 'hex'
);
EXCEPTION
WHEN others THEN
t_bytea := NULL;
END;
WHEN others THEN
t_bytea := NULL;
END;
IF t_type = 'Download Certificate' THEN
RETURN download_cert(t_value);
ELSIF t_type IN ('ID', 'Certificate ASN.1', 'CA ID', 'CT Entry ID') THEN
BEGIN
EXIT WHEN t_value::integer IS NOT NULL;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
ELSIF t_type IN (
'Simple', 'Advanced', 'CA/B Forum lint', 'X.509 lint', 'ZLint', 'Lint', 'CA Name',
'Identity', 'Common Name', 'Email Address',
'Organizational Unit Name', 'Organization Name',
'Domain Name', 'Email Address (SAN)', 'IP Address'
) THEN
EXIT;
ELSIF t_type IN (
'SHA-1(Certificate)', 'SHA-1(SubjectPublicKeyInfo)',
'SHA-1(Subject)'
) THEN
EXIT WHEN length(t_bytea) = 20;
ELSIF t_type IN (
'SHA-256(Certificate)', 'SHA-256(SubjectPublicKeyInfo)'
) THEN
EXIT WHEN length(t_bytea) = 32;
ELSIF t_type IN ('Serial Number', 'Subject Key Identifier') THEN
EXIT WHEN t_bytea IS NOT NULL;
ELSE
t_type := 'Invalid value';
EXIT;
END IF;
END IF;
END LOOP;
t_outputType := coalesce(get_parameter('output', paramNames, paramValues), '');
IF t_outputType = '' THEN
t_outputType := 'html';
END IF;
IF lower(t_outputType) IN ('forum', 'gen-add-chain', 'monitored-logs') THEN
t_type := lower(t_outputType);
t_title := t_type;
t_outputType := 'html';
ELSIF lower(t_outputType) LIKE '%.json' THEN
t_type := lower(t_outputType);
t_outputType := 'json';
t_output :=
'[BEGIN_HEADERS]
Content-Type: application/json
[END_HEADERS]
';
ELSIF lower(t_outputType) IN ('revoked-intermediates', 'mozilla-certvalidations', 'mozilla-certvalidations-by-root', 'mozilla-certvalidations-by-owner', 'mozilla-certvalidations-by-version',
'mozilla-disclosures', 'mozilla-onecrl', 'microsoft-disclosures', 'redacted-precertificates') THEN
t_type := lower(t_outputType);
t_title := t_type;
t_outputType := 'html';
t_useCachedResponse := TRUE;
ELSIF lower(t_outputType) IN ('linttbscert', 'lintcert') THEN
t_type := lower(t_outputType);
t_outputType := 'html';
ELSIF lower(t_outputType) IN ('advanced') THEN
t_type := 'Advanced';
t_outputType := 'html';
END IF;
IF t_outputType NOT IN ('html', 'json', 'atom') THEN
RAISE no_data_found USING MESSAGE = 'Unsupported output type: ' || html_escape(t_outputType);
END IF;
IF coalesce(t_type, 'Simple') = 'Simple' THEN
t_type := 'Simple';
t_outputType := 'html';
END IF;
IF t_type IN ('Simple', 'Advanced') THEN
t_title := 'Certificate Search';
ELSIF t_type IN (
'SHA-1(Certificate)',
'SHA-256(Certificate)',
'SHA-1(SubjectPublicKeyInfo)',
'SHA-256(SubjectPublicKeyInfo)',
'SHA-1(Subject)'
) THEN
t_value := encode(t_bytea, 'hex');
ELSIF t_type = 'CT Entry ID' THEN
t_title := 'CT:' || t_value;
ELSIF t_type IN ('CA ID', 'CA Name') THEN
t_title := 'CA:' || t_value;
ELSIF t_type = 'Serial Number' THEN
t_value := encode(t_bytea, 'hex');
t_title := 'Serial#' || t_value;
ELSIF t_type = 'Subject Key Identifier' THEN
t_value := encode(t_bytea, 'hex');
t_title := 'SKI#' || t_value;
ELSIF t_type = 'Identity' THEN
t_nameType := NULL;
ELSIF t_type = 'Common Name' THEN
t_nameType := 'commonName';
ELSIF t_type = 'Email Address' THEN
t_nameType := 'emailAddress';
ELSIF t_type = 'Organizational Unit Name' THEN
t_nameType := 'organizationalUnitName';
ELSIF t_type = 'Organization Name' THEN
t_nameType := 'organizationName';
ELSIF t_type = 'Domain Name' THEN
t_nameType := 'dNSName';
ELSIF t_type = 'Email Address (SAN)' THEN
t_nameType := 'rfc822Name';
ELSIF t_type = 'IP Address' THEN
t_nameType := 'iPAddress';
ELSIF lower(t_type) LIKE '%lint' THEN
IF t_type = 'Lint' THEN
t_linters := 'cablint,x509lint,zlint';
ELSE
t_linters := t_cmd;
t_linter := t_linters::linter_type;
END IF;
BEGIN
IF lower(t_value) = 'issues' THEN
t_type := t_type || ': Issues';
ELSE
t_value := (t_value::integer)::text;
END IF;
EXCEPTION
WHEN OTHERS THEN
t_type := t_type || ': Summary';
END;
END IF;
IF t_title IS NULL THEN
t_title := coalesce(t_value, '');
END IF;
t_temp := get_parameter('minNotBefore', paramNames, paramValues);
IF t_temp IS NULL THEN
t_minNotBefore := (statement_timestamp() at time zone 'UTC' - interval '1 week')::date;
t_minNotBeforeString := '';
ELSE
t_minNotBefore := t_temp::date;
t_minNotBeforeString := '&minNotBefore=' || t_temp;
END IF;
t_temp := get_parameter('exclude', paramNames, paramValues);
IF lower(coalesce(',' || t_temp || ',', 'nothing')) LIKE ',expired,' THEN
t_excludeExpired := '&exclude=expired';
END IF;
t_temp := get_parameter('search', paramNames, paramValues);
IF lower(coalesce(t_temp, 'crt.sh')) = 'censys' THEN
t_searchProvider := '&search=censys';
END IF;
t_opt := coalesce(get_parameter('opt', paramNames, paramValues), '');
IF t_opt != '' THEN
t_opt := t_opt || ',';
END IF;
IF t_outputType = 'html' THEN
IF lower(t_type) LIKE '%lint%' THEN
t_groupBy := coalesce(get_parameter('group', paramNames, paramValues), '');
t_direction := coalesce(get_parameter('dir', paramNames, paramValues), 'v');
ELSE
t_groupBy := coalesce(get_parameter('group', paramNames, paramValues), 'none');
t_direction := coalesce(get_parameter('dir', paramNames, paramValues), '^');
END IF;
t_groupByParameter := t_groupBy;
IF t_groupByParameter != '' THEN
t_groupByParameter := '&group=' || t_groupByParameter;
END IF;
IF t_direction NOT IN ('^', 'v') THEN
t_direction := 'v';
END IF;
IF t_direction = 'v' THEN
t_dirSymbol := '⇩';
t_orderBy := 'ASC';
t_oppositeDirection := '^';
ELSE
t_dirSymbol := '⇧';
t_orderBy := 'DESC';
t_oppositeDirection := 'v';
END IF;
END IF;
t_temp := get_parameter('sort', paramNames, paramValues);
IF coalesce(t_temp, '') = '' THEN
t_sort := 1;
ELSE
t_sort := t_temp::integer;
END IF;
t_excludeCAs := string_to_array(coalesce(get_parameter('excludecas', paramNames, paramValues), ''), ',');
IF array_length(t_excludeCAs, 1) > 0 THEN
t_excludeCAsString := '&excludeCAs=' || array_to_string(t_excludeCAs, ',');
END IF;
IF t_useCachedResponse THEN
t_count := coalesce(get_parameter('maxage', paramNames, paramValues), '14400')::integer;
t_cacheResponse := (t_count = 0);
t_maxAge := statement_timestamp() - (interval '1 second' * t_count);
SELECT cr.RESPONSE_BODY
INTO t_output
FROM cached_response cr
WHERE cr.PAGE_NAME = t_type
AND cr.GENERATED_AT > t_maxAge;
IF FOUND THEN
RETURN t_output;
END IF;
END IF;
-- Generate page header.
t_output := coalesce(t_output, '');
IF t_outputType = 'html' THEN
t_output :=
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>crt.sh | ' || html_escape(t_title) || '</TITLE>
<META name="description" content="Free CT Log Certificate Search Tool from COMODO">
<META name="keywords" content="crt.sh, CT, Certificate Transparency, Certificate Search, SSL Certificate, Comodo CA">
';
IF t_type = 'Certificate ASN.1' THEN
t_output := t_output ||
' <LINK rel="stylesheet" href="/asn1js/index.css" type="text/css">
';
ELSIF t_type = 'mozilla-certvalidations' THEN
t_output := t_output ||
' <SCRIPT src="//cdnjs.cloudflare.com/ajax/libs/dygraph/2.0.0/dygraph.min.js"></SCRIPT>
<LINK rel="stylesheet" src="//cdnjs.cloudflare.com/ajax/libs/dygraph/2.0.0/dygraph.min.css" />
<STYLE type="text/css">
#graph { width: 800px; height: 400px; }
#graph, #graph_toggles, #graph_labels { float: left; margin: 0 1em 1em 0; }
#graph_toggles label { display: block; font-weight: bold; }
.many .dygraph-legend > span { display: none; }
.many .dygraph-legend > span.highlight { display: inline }
</STYLE>
';
ELSIF t_type = 'monitored-logs' THEN
t_output := t_output ||
' <STYLE type="text/css">
table tr:nth-child(2n+5) {
background: #EFEFEF
}
</STYLE>
';
END IF;
t_output := t_output ||
' <STYLE type="text/css">
';
IF t_type NOT IN ('mozilla-disclosures', 'microsoft-disclosures') THEN
t_output := t_output ||
' a {
white-space: nowrap;
}
';
END IF;
t_output := t_output ||
' body {
color: #888888;
font: 12pt Arial, sans-serif;
padding-top: 10px;
text-align: center
}
form {
margin: 0px
}
span.heading {
color: #888888;
font: 12pt Arial, sans-serif
}
span.title {
border: 1px solid;
color: #BF2E1A;
font: bold 18pt Arial, sans-serif;
padding: 0px 5px
}
span.text {
color: #888888;
font: 10pt Arial, sans-serif
}
span.whiteongrey {
background-color: #CCCCCC;
border: 1px solid;
color: #FFFFFF;
font: bold 18pt Arial, sans-serif;
padding: 0px 5px
}
table {
border-collapse: collapse;
color: #222222;
font: 10pt Arial, sans-serif;
margin-left: auto;
margin-right: auto
}
table.options {
border: none;
margin-left: 10px
}
td, th {
border: 1px solid #CCCCCC;
padding: 0px 2px;
text-align: left;
vertical-align: top
}
td.outer, th.outer {
border: 1px solid #CCCCCC;
padding: 2px 20px;
text-align: left
}
th.heading {
color: #888888;
font: bold italic 12pt Arial, sans-serif;
padding: 20px 0px 0px;
text-align: center
}
th.options, td.options {
border: none;
vertical-align: middle
}
td.text {
font: 10pt Courier New, sans-serif;
padding: 2px 20px
}
td.heading {
border: none;
color: #888888;
font: 12pt Arial, sans-serif;
padding-top: 20px;
text-align: center
}
table.lint td, th {
text-align: center
}
.button {
background-color: #BF2E1A;
color: #FFFFFF;
font: 13pt Arial, sans-serif
}
.copyright {
font: 8pt Arial, sans-serif;
color: #DF4F3C
}
.input {
border: 1px solid #888888;
font-weight: bold;
text-align: center
}
.small {
font: 8pt Arial, sans-serif;
color: #888888
}
.error {
background-color: #FFDFDF;
color: #CC0000;
font-weight: bold
}
.fatal {
background-color: #0000AA;
color: #FFFFFF;
font-weight: bold
}
.notice {
background-color: #FFFFDF;
color: #606000
}
.warning {
background-color: #FFEFDF;
color: #DF6000
}
</STYLE>
</HEAD>
<BODY>
<A href="/"><SPAN class="title">crt.sh</SPAN></A>';
END IF;
IF t_type = 'Invalid value' THEN
RAISE no_data_found USING MESSAGE = t_type || ': ''' || html_escape(t_value) || '''';
ELSIF t_type = 'Simple' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Certificate Search</SPAN>
<BR><BR><BR><BR>
Enter an <B>Identity</B> (Domain Name, Organization Name, etc),
<BR>a <B>Certificate Fingerprint</B> (SHA-1 or SHA-256) or a <B>crt.sh ID</B>:
<BR><SPAN class="small" style="color:#BBBBBB">(% = wildcard)</SPAN>
<BR><BR>
<FORM name="search_form" method="GET" onsubmit="return (this.q.value != '')">
<INPUT type="text" class="input" name="q" size="64" maxlength="255">
<BR><BR><BR>
<INPUT type="submit" class="button" value="Search">
<SPAN style="position:absolute">
<A style="font-size:8pt;vertical-align:sub" href="?a=1">Advanced...</A>
</SPAN>
</FORM>
<SCRIPT type="text/javascript">
document.search_form.q.focus();
</SCRIPT>';
ELSIF t_type = 'Advanced' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Certificate Search</SPAN>
<BR><BR><BR>
<SCRIPT type="text/javascript">
function doSearch(
type,
value
)
{
if ((!type) || (!value))
return;
var t_url;
if (document.search_form.searchCensys.checked && (type != "CAID")) {
if ((type == "id") || (type == "ctid") || (type == "ski")
|| (type == "spkisha1") || (type == "spkisha256")
|| (type == "subjectsha1") || (type == "E")) {
alert("Sorry, Censys doesn''t support this search type");
return;
}
t_url = "//www.censys.io/certificates?q=";
var t_field = "";
if (value != "%") {
if (type == "c")
t_url += "parsed.fingerprint_sha1:" + encodeURIComponent("\"" + value.toLowerCase() + "\"")
+ " OR parsed.fingerprint_sha256:" + encodeURIComponent("\"" + value.toLowerCase() + "\"");
else if (type == "serial")
t_field = "parsed.serial_number";
else if (type == "sha1")
t_url += "parsed.fingerprint_sha1:" + encodeURIComponent("\"" + value.toLowerCase() + "\"");
else if (type == "sha256")
t_url += "parsed.fingerprint_sha256:" + encodeURIComponent("\"" + value.toLowerCase() + "\"");
else if ((type == "CA") || (type == "CAName"))
t_field = "parsed.issuer_dn";
else if (type == "Identity")
t_url += "parsed.subject_dn:" + encodeURIComponent("\"" + value + "\"")
+ " OR parsed.extensions.subject_alt_name.dns_names:" + encodeURIComponent("\"" + value + "\"")
+ " OR parsed.extensions.subject_alt_name.email_addresses:" + encodeURIComponent("\"" + value + "\"")
+ " OR parsed.extensions.subject_alt_name.ip_addresses:" + encodeURIComponent("\"" + value + "\"");
else if (type == "CN")
t_field = "parsed.subject.common_name";
else if (type == "OU")
t_field = "parsed.subject.organizational_unit";
else if (type == "O")
t_field = "parsed.subject.organization";
else if (type == "dNSName")
t_field = "parsed.extensions.subject_alt_name.dns_names";
else if (type == "rfc822Name")
t_field = "parsed.extensions.subject_alt_name.email_addresses";
else if (type == "iPAddress")
t_field = "parsed.extensions.subject_alt_name.ip_addresses";
}
if (t_field != "")
t_url += t_field + ":" + encodeURIComponent("\"" + value + "\"");
}
else {
t_url = "?" + encodeURIComponent(type) + "=" + encodeURIComponent(value).replace(/%20/g, "+");
if (document.search_form.excludeExpired.checked)
t_url += "&exclude=expired";
if (document.search_form.searchCensys.checked)
t_url += "&search=censys";
}
window.location = t_url;
}
</SCRIPT>
<FORM name="search_form" method="GET" onsubmit="return false">
Enter search term:
<SPAN class="small" style="position:absolute;padding-top:3px;color:#BBBBBB"> (% = wildcard)</SPAN>
<BR><BR>
<INPUT type="text" class="input" name="q" size="64" maxlength="255">
<BR><BR><BR>
<TABLE class="options" style="margin:auto">
<TR>
<TD style="border:none;text-align:center">
<SPAN class="heading">Select search type:</SPAN>
<BR><SELECT name="searchtype" size="19">
<OPTION value="c">CERTIFICATE</OPTION>
<OPTION value="id"> crt.sh ID</OPTION>
<OPTION value="ctid"> CT Entry ID</OPTION>
<OPTION value="serial"> Serial Number</OPTION>
<OPTION value="ski"> Subject Key Identifier</OPTION>
<OPTION value="spkisha1"> SHA-1(SubjectPublicKeyInfo)</OPTION>
<OPTION value="spkisha256"> SHA-256(SubjectPublicKeyInfo)</OPTION>
<OPTION value="subjectsha1"> SHA-1(Subject)</OPTION>
<OPTION value="sha1"> SHA-1(Certificate)</OPTION>
<OPTION value="sha256"> SHA-256(Certificate)</OPTION>
<OPTION value="ca">CA</OPTION>
<OPTION value="CAID"> ID</OPTION>
<OPTION value="CAName"> Name</OPTION>
<OPTION value="Identity" selected>IDENTITY</OPTION>
<OPTION value="CN"> commonName (Subject)</OPTION>
<OPTION value="E"> emailAddress (Subject)</OPTION>
<OPTION value="OU"> organizationalUnitName (Subject)</OPTION>
<OPTION value="O"> organizationName (Subject)</OPTION>
<OPTION value="dNSName"> dNSName (SAN)</OPTION>
<OPTION value="rfc822Name"> rfc822Name (SAN)</OPTION>
<OPTION value="iPAddress"> iPAddress (SAN)</OPTION>
</SELECT>
</TD>
<TD style="border:none;width:40px"> </TD>
<TD style="border:none;text-align:center">
<SPAN class="heading">Select search options:</SPAN>
<BR><DIV style="border:1px solid #AAAAAA;margin-bottom:5px;padding:5px 0px;text-align:left">
<INPUT type="checkbox" name="excludeExpired"';
IF t_excludeExpired IS NOT NULL THEN
t_output := t_output || ' checked';
END IF;
t_output := t_output || '> Exclude expired certificates?
<BR><INPUT type="checkbox" name="searchCensys"';
IF coalesce(t_searchProvider, '') = '&search=censys' THEN
t_output := t_output || ' checked';
END IF;
t_output := t_output || '> Search on <SPAN style="vertical-align:-30%"><IMG src="/censys.png"></SPAN>?
</DIV>
<BR>
<INPUT type="submit" class="button" value="Search"
onClick="doSearch(document.search_form.searchtype.value,document.search_form.q.value)">
<SPAN style="position:absolute">
<A style="font-size:8pt;vertical-align:sub" href="?">Simple...</A>
</SPAN>
<BR><BR><BR><BR><HR><BR>
<SPAN class="heading">Select linting options:</SPAN>
<BR><SELECT name="linter" size="3">
<OPTION value="cablint" selected>cablint</OPTION>
<OPTION value="x509lint">x509lint</OPTION>
<OPTION value="zlint">zlint</OPTION>
<OPTION value="lint">ALL</OPTION>
</SELECT>
<SELECT name="linttype" size="3">
<OPTION value="1 week" selected>1-week Summary</OPTION>
<OPTION value="issues">Issues</OPTION>
</SELECT>
<BR><BR>
<INPUT type="submit" class="button" value="Lint"
onClick="doSearch(document.search_form.linter.value,document.search_form.linttype.value)">
<BR><BR><A href="/linttbscert">TBSCertificate Linter</A>
<BR><A href="/lintcert">Certificate Linter</A>
</TD>
</TR>
<TR>
<TD colspan="3" style="border:none">
<BR><BR><HR><SPAN class="heading">Other crt.sh pages:</SPAN><BR><BR>
</TD>
</TR>
<TR>
<TD style="border:none">
<TABLE>
<TR>
<TD>crt.sh</TD>
<TD>
<A href="/forum">Forum</A>
<BR><A href="/revoked-intermediates">Revoked Intermediates</A>
</TD>
</TD>
</TR>
<TR>
<TD>Mozilla</TD>
<TD>
<A href="/mozilla-disclosures">CA Certificate Disclosures</A>
<BR><A href="/mozilla-certvalidations">Certificate Validations</A>
<BR><A href="/mozilla-onecrl">OneCRL</A>
</TD>
</TR>
</TABLE>
</TD>
<TD style="border:none"> </TD>
<TD style="border:none">
<TABLE>
<TR>
<TD>CT</TD>
<TD>
<A href="/monitored-logs">Monitored Logs</A>
<BR><A href="/gen-add-chain">Certificate Submission Assistant</A>
<BR><A href="/redacted-precertificates">"Redacted" Precertificates</A>
</TD>
</TR>
</TABLE>
</TD>
<TR>
</TABLE>
</FORM>
<SCRIPT type="text/javascript">
document.search_form.q.focus();
</SCRIPT>';
ELSIF t_type = 'forum' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Forum</SPAN>
<BR><BR>
<IFRAME id="forum_embed"
src="javascript:void(0)"
scrolling="no"
frameborder="0"
width="900"
height="600">
</IFRAME>
<SCRIPT type="text/javascript">
document.getElementById(''forum_embed'').src =
''https://groups.google.com/forum/embed/?place=forum/crtsh''
+ ''&showsearch=true&showpopout=true&showtabs=false''
+ ''&parenturl='' + encodeURIComponent(window.location.href);
</SCRIPT>';
ELSIF t_type = 'logs.json' THEN
t_temp := coalesce(get_parameter('include', paramNames, paramValues), 'active');
t_output := t_output || '{' || chr(10) || ' "logs": [' || chr(10);
FOR l_record IN (
SELECT ctl.NAME, ctl.PUBLIC_KEY, ctl.URL, ctl.MMD_IN_SECONDS
FROM ct_log ctl
WHERE ctl.IS_ACTIVE = CASE WHEN t_temp = 'all' THEN ctl.IS_ACTIVE ELSE 't' END
ORDER BY ctl.NAME
) LOOP
t_output := t_output || ' {' || chr(10)
|| ' "description": "' || l_record.NAME || '",' || chr(10)
|| ' "log_id": "' || encode(digest(l_record.PUBLIC_KEY, 'sha256'), 'base64') || '",' || chr(10)
|| ' "key": "' || replace(encode(l_record.PUBLIC_KEY, 'base64'), chr(10), '') || '",' || chr(10)
|| ' "url": "' || l_record.URL || '",' || chr(10)
|| ' "maximum_merge_delay": ' || coalesce(l_record.MMD_IN_SECONDS::text, '') || chr(10)
|| ' },' || chr(10);
END LOOP;
t_output := rtrim(t_output, ',' || chr(10)) || chr(10) || ' ]' || chr(10) || '}';
ELSIF t_type = 'monitored-logs' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Monitored Logs</SPAN>
<BR><BR>
<TABLE>
<TR><TD colspan="8" class="heading">CT Logs currently monitored:</TD></TR>
<TR>
<TH rowspan="2">Operator</TH>
<TH rowspan="2">URL</TH>
<TH rowspan="2">MMD<BR><SPAN class="small">(hrs)</SPAN></TH>
<TH rowspan="2">Latest STH<BR><SPAN class="small">(UTC)</SPAN></TH>
<TH colspan="2">Entries</TH>
<TH rowspan="2">Last Contacted<BR><SPAN class="small">(UTC)</SPAN></TH>
<TH colspan="2">Google</TH>
</TR>
<TR>
<TH>Tree Size</TH>
<TH>Backlog</TH>
<TH>In Chrome?</TH>
<TH>Uptime %</TH>
</TR>';
FOR l_record IN (
SELECT ctl.ID, ctl.OPERATOR, ctl.URL,
ctl.TREE_SIZE, ctl.LATEST_ENTRY_ID, ctl.LATEST_UPDATE,
ctl.LATEST_STH_TIMESTAMP, ctl.MMD_IN_SECONDS,
CASE WHEN coalesce(ctl.LATEST_STH_TIMESTAMP + (ctl.MMD_IN_SECONDS || ' seconds')::interval, statement_timestamp()) <= statement_timestamp()
THEN ' style="color:#FF0000"'
ELSE ''
END FONT_STYLE,
ctl.INCLUDED_IN_CHROME, ctl.CHROME_ISSUE_NUMBER, ctl.NON_INCLUSION_STATUS,
ctl.GOOGLE_UPTIME,
CASE WHEN coalesce(ctl.GOOGLE_UPTIME::numeric, 100) < 99
THEN ';color:#FF0000'
ELSE ''
END UPTIME_FONT_STYLE
FROM ct_log ctl
WHERE ctl.IS_ACTIVE = 't'
ORDER BY ctl.TREE_SIZE DESC NULLS LAST
) LOOP
SELECT coalesce(l_record.TREE_SIZE, 0) - coalesce(max(ENTRY_ID), -1) - 1
INTO t_count
FROM ct_log_entry ctle
WHERE ctle.CT_LOG_ID = l_record.ID;
IF t_count < 0 THEN
t_count := 0;
END IF;
t_output := t_output || '
<TR>
<TD' || l_record.FONT_STYLE || '>' || l_record.OPERATOR || '</TD>
<TD' || l_record.FONT_STYLE || '>' || l_record.URL || '</TD>
<TD' || l_record.FONT_STYLE || '>' || coalesce((l_record.MMD_IN_SECONDS / 60 / 60)::text, '?') || '</TD>
<TD' || l_record.FONT_STYLE || '>' || coalesce(to_char(l_record.LATEST_STH_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS'), '') || '</TD>
<TD' || l_record.FONT_STYLE || '>' || coalesce(l_record.TREE_SIZE::text, '') || '</TD>
<TD' || l_record.FONT_STYLE || '>' || t_count::text || '</TD>
<TD>' || coalesce(to_char(l_record.LATEST_UPDATE, 'YYYY-MM-DD HH24:MI:SS'), '') || '</TD>
<TD>
';
IF l_record.CHROME_ISSUE_NUMBER IS NOT NULL THEN
t_output := t_output || '<A href="https://bugs.chromium.org/p/chromium/issues/detail?id='
|| l_record.CHROME_ISSUE_NUMBER::text || '" target="_blank">';
IF l_record.INCLUDED_IN_CHROME IS NOT NULL THEN
t_output := t_output || coalesce(l_record.NON_INCLUSION_STATUS, 'M' || l_record.INCLUDED_IN_CHROME::text);
ELSE
t_output := t_output || coalesce(l_record.NON_INCLUSION_STATUS, 'Pending');
END IF;
t_output := t_output || '</A>' || chr(10);
ELSIF l_record.NON_INCLUSION_STATUS IS NOT NULL THEN
t_output := t_output || l_record.NON_INCLUSION_STATUS;
END IF;
t_output := t_output ||
' </TD>
<TD style="text-align:right' || l_record.UPTIME_FONT_STYLE || '">' || coalesce(l_record.GOOGLE_UPTIME, '') || '</TD>
</TR>';
END LOOP;
t_output := t_output || '
</TABLE>
<TABLE>
<TR><TD colspan="8" class="heading">CT Logs no longer monitored:</TD></TR>
<TR>
<TH rowspan="2">Operator</TH>
<TH rowspan="2">URL</TH>
<TH rowspan="2">MMD<BR><SPAN class="small">(hrs)</SPAN></TH>
<TH rowspan="2">Latest STH<BR><SPAN class="small">(UTC)</SPAN></TH>
<TH colspan="2">Entries</TH>
<TH rowspan="2">Last Contacted<BR><SPAN class="small">(UTC)</SPAN></TH>
<TH rowspan="2">In Chrome?</TH>
</TR>
<TR>
<TH>Tree Size</TH>
<TH>Backlog</TH>
</TR>';
FOR l_record IN (
SELECT ctl.ID, ctl.OPERATOR, ctl.URL,
ctl.TREE_SIZE, ctl.LATEST_ENTRY_ID, ctl.LATEST_UPDATE,
ctl.LATEST_STH_TIMESTAMP, ctl.MMD_IN_SECONDS,
ctl.INCLUDED_IN_CHROME, ctl.CHROME_ISSUE_NUMBER, ctl.NON_INCLUSION_STATUS
FROM ct_log ctl
WHERE ctl.IS_ACTIVE = 'f'
AND ctl.LATEST_ENTRY_ID IS NOT NULL
ORDER BY ctl.TREE_SIZE DESC NULLS LAST
) LOOP
SELECT coalesce(l_record.TREE_SIZE, 0) - coalesce(max(ENTRY_ID), -1) - 1
INTO t_count
FROM ct_log_entry ctle
WHERE ctle.CT_LOG_ID = l_record.ID;
IF t_count < 0 THEN
t_count := 0;
END IF;
t_output := t_output || '
<TR>
<TD>' || l_record.OPERATOR || '</TD>
<TD>' || l_record.URL || '</TD>
<TD>' || coalesce((l_record.MMD_IN_SECONDS / 60 / 60)::text, '?') || '</TD>
<TD>' || coalesce(to_char(l_record.LATEST_STH_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS'), '') || '</TD>
<TD>' || coalesce(l_record.TREE_SIZE::text, '') || '</TD>
<TD>' || t_count::text || '</TD>
<TD>' || coalesce(to_char(l_record.LATEST_UPDATE, 'YYYY-MM-DD HH24:MI:SS'), '') || '</TD>
<TD>
';
IF l_record.CHROME_ISSUE_NUMBER IS NOT NULL THEN
t_output := t_output || '<A href="https://code.google.com/p/chromium/issues/detail?id='
|| l_record.CHROME_ISSUE_NUMBER::text || '" target="_blank">';
IF l_record.INCLUDED_IN_CHROME IS NOT NULL THEN
t_output := t_output || coalesce(l_record.NON_INCLUSION_STATUS, 'M' || l_record.INCLUDED_IN_CHROME::text);
ELSE
t_output := t_output || coalesce(l_record.NON_INCLUSION_STATUS, 'Pending');
END IF;
t_output := t_output || '</A>' || chr(10);
ELSIF l_record.NON_INCLUSION_STATUS IS NOT NULL THEN
t_output := t_output || l_record.NON_INCLUSION_STATUS;
END IF;
t_output := t_output ||
' </TD>
</TR>';
END LOOP;
t_output := t_output || '
</TABLE>';
ELSIF t_type = 'redacted-precertificates' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">"Redacted" Precertificates</SPAN>
<BR><SPAN class="small">Generated at ' || TO_CHAR(statement_timestamp() AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS') || ' UTC</SPAN>
<BR><BR>
';
t_temp := '';
FOR l_record IN (
SELECT pc.ID PRECERT_ID,
array_agg(pci.NAME_VALUE) REDACTED_LABELS,
c.ID CERT_ID,
pci.ISSUER_CA_ID,
ca.NAME ISSUER_NAME
FROM certificate_identity pci, ca,
certificate pc
LEFT OUTER JOIN certificate c ON (
pc.ID != c.ID
AND pc.ISSUER_CA_ID = c.ISSUER_CA_ID
AND x509_serialNumber(pc.CERTIFICATE) = x509_serialNumber(c.CERTIFICATE)
AND c.CERTIFICATE IS NOT NULL
)
WHERE lower(pci.NAME_VALUE) LIKE '?%'
AND pci.NAME_TYPE IN ('dNSName', 'commonName')
AND pci.ISSUER_CA_ID = ca.ID
AND pci.CERTIFICATE_ID = pc.ID
GROUP BY pc.ID, c.ID, pci.ISSUER_CA_ID, ca.NAME
ORDER BY pc.ID DESC
) LOOP
t_temp := t_temp ||
' <TR>
<TD><A href="/?id=' || l_record.PRECERT_ID || '">' || l_record.PRECERT_ID || '</A></TD>
<TD>' || array_to_string(l_record.REDACTED_LABELS, '<BR>') || '</TD>
';
IF l_record.CERT_ID IS NULL THEN
t_temp := t_temp ||
' <TD> </TD>
<TD> </TD>
<TD> </TD>
';
ELSE
SELECT string_agg(ci.NAME_VALUE, '<BR>')
INTO t_temp2
FROM certificate_identity ci
WHERE ci.CERTIFICATE_ID = l_record.CERT_ID
AND ci.NAME_TYPE IN ('dNSName', 'commonName');
t_temp := t_temp ||
' <TD><A href="/?id=' || l_record.CERT_ID || '">' || l_record.CERT_ID || '</A></TD>
<TD>' || t_temp2 || '</TD>
<TD><A href="/?id=' || l_record.ISSUER_CA_ID || '">' || l_record.ISSUER_NAME || '</A></TD>
';
END IF;
t_temp := t_temp ||
' </TR>
';
END LOOP;
t_output := t_output ||
'<TABLE>
<TR>
<TH>Precertificate</TH>
<TH>Redacted Labels</TH>
<TH>Certificate</TH>
<TH>Unredacted Labels</TH>
<TH>Issuer Name</TH>
</TR>
' || t_temp || '
</TABLE>';
ELSIF t_type = 'gen-add-chain' THEN
t_temp := get_parameter('b64cert', paramNames, paramValues);
t_onlyOneChain := lower(coalesce(get_parameter('onlyonechain', paramNames, paramValues), 'n')) = 'y';
IF t_temp IS NULL THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Certificate Submission Assistant</SPAN>
<BR><BR>1. Enter a base64 encoded certificate.
<BR><BR>2. Press the button to generate JSON that you can then submit to a log''s /ct/v1/add-chain API.
<BR>(crt.sh will discover the trust chain for you).
<BR><BR><FORM method="post" name="form1">
<TEXTAREA name="b64cert" rows=25 cols=64></TEXTAREA>
<BR><BR><INPUT type="submit" class="button" value="Generate JSON">
</FORM>
<BR><BR><SPAN class="small">Please note: This tool currently finds chains that are trusted by the Mozilla and/or Microsoft and/or Apple root programs.
<BR>FIXME: Look at each log''s /ct/v1/get-roots instead</SPAN>';
ELSE
t_certificate := decode(
replace(replace(t_temp, '-----BEGIN CERTIFICATE-----', ''), '-----END CERTIFICATE-----', ''),
'base64'
);
SELECT c.ID
INTO t_certificateID
FROM certificate c
WHERE digest(c.CERTIFICATE, 'sha256') = digest(t_certificate, 'sha256');
RETURN
'[BEGIN_HEADERS]
Content-Disposition: attachment; filename="' || upper(encode(digest(t_certificate, 'sha256'), 'hex')) || '_' || coalesce(t_certificateID::text, 'UNKNOWN') || '.add-chain.json"
Content-Type: application/json
[END_HEADERS]
' || generate_add_chain_body(t_certificate, t_onlyOneChain);
END IF;
ELSIF t_type = 'linttbscert' THEN
t_temp := get_parameter('b64tbscert', paramNames, paramValues);
IF t_temp IS NULL THEN
t_output := t_output ||
' <SCRIPT>
function handleFiles() {
var reader = new FileReader();
reader.onload = function(e) {
document.form1.b64tbscert.value = reader.result;
}
reader.readAsText(document.getElementById("fil").files[0]);
}
</SCRIPT>
<SPAN class="whiteongrey">TBSCertificate Linter</SPAN>
<BR><BR>Pick a file or Paste a base64 encoded TBSCertificate, then press "Lint":
<BR><BR><INPUT type="file" id="fil" onchange="handleFiles(this.files)" />
<BR><BR><FORM method="post" name="form1">
<TEXTAREA name="b64tbscert" rows=25 cols=64></TEXTAREA>
<BR><BR><INPUT type="submit" class="button" value="Lint">
</FORM>';
ELSE
t_tbsCertificate := decode(
replace(replace(t_temp, '-----BEGIN CERTIFICATE-----', ''), '-----END CERTIFICATE-----', ''),
'base64'
);
RETURN
'[BEGIN_HEADERS]
Content-Type: text/plain; charset=UTF-8
[END_HEADERS]
' || lint_tbscertificate(t_tbsCertificate);
END IF;
ELSIF t_type = 'lintcert' THEN
t_temp := get_parameter('b64cert', paramNames, paramValues);
IF t_temp IS NULL THEN
t_output := t_output ||
' <SCRIPT>
function handleFiles() {
var reader = new FileReader();
reader.onload = function(e) {
document.form1.b64cert.value = reader.result;
}
reader.readAsText(document.getElementById("fil").files[0]);
}
</SCRIPT>
<SPAN class="whiteongrey">Certificate Linter</SPAN>
<BR><BR>Pick a file or Paste a base64 encoded Certificate, then press "Lint":
<BR><BR><INPUT type="file" id="fil" onchange="handleFiles(this.files)" />
<BR><BR><FORM method="post" name="form1">
<TEXTAREA name="b64cert" rows=25 cols=64></TEXTAREA>
<BR><BR><INPUT type="submit" class="button" value="Lint">
</FORM>';
ELSE
t_certificate := decode(
replace(replace(t_temp, '-----BEGIN CERTIFICATE-----', ''), '-----END CERTIFICATE-----', ''),
'base64'
);
RETURN
'[BEGIN_HEADERS]
Content-Type: text/plain; charset=UTF-8
[END_HEADERS]
' || lint_certificate(t_certificate, FALSE);
END IF;
ELSIF t_type = 'revoked-intermediates' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Revoked Intermediate CA Certificates with id-kp-serverAuth Trust</SPAN>
<BR><SPAN class="small">Generated at ' || TO_CHAR(statement_timestamp() AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS') || ' UTC</SPAN>
<BR><BR>
<TABLE>
<TR>
<TH rowspan="2">Issuer</TH>
<TH rowspan="2">Certificate</TH>
<TH>The CA</TH>
<TH>Microsoft</TH>
<TH colspan="2">Mozilla</TH>
<TH>Google</TH>
</TR>
<TR>
<TH>CRL</TH>
<TH>IE/Edge with<BR>disallowedcert.stl</TH>
<TH>Firefox with<BR>OneCRL</TH>
<TH>CCADB disclosure</TH>
<TH>Chrome with<BR>CRLSet / Blacklist</TH>
</TR>
';
FOR l_record IN (
SELECT c.ID, c.ISSUER_CA_ID, x509_notAfter(c.CERTIFICATE) NOT_AFTER,
is_technically_constrained(c.CERTIFICATE) IS_TECHNICALLY_CONSTRAINED,
get_ca_name_attribute(cac.CA_ID) SUBJECT_FRIENDLY_NAME,
get_ca_name_attribute(c.ISSUER_CA_ID) ISSUER_FRIENDLY_NAME,
md.CERTIFICATE_ID MS_CERTIFICATE_ID,
mo.CERTIFICATE_ID MOZ_CERTIFICATE_ID,
gr.ENTRY_TYPE, cr.SERIAL_NUMBER,
cc.MOZILLA_DISCLOSURE_STATUS, cc.MICROSOFT_DISCLOSURE_STATUS,
cc.REVOCATION_STATUS
FROM ca_certificate cac, certificate c
LEFT OUTER JOIN microsoft_disallowedcert md ON (c.ID = md.CERTIFICATE_ID)
LEFT OUTER JOIN mozilla_onecrl mo ON (c.ID = mo.CERTIFICATE_ID)
LEFT OUTER JOIN ccadb_certificate cc ON (c.ID = cc.CERTIFICATE_ID)
LEFT OUTER JOIN google_revoked gr ON (c.ID = gr.CERTIFICATE_ID)
LEFT OUTER JOIN crl_revoked cr ON (
c.ISSUER_CA_ID = cr.CA_ID
AND x509_serialNumber(c.CERTIFICATE) = cr.SERIAL_NUMBER
)
WHERE cac.CERTIFICATE_ID = c.ID
ORDER BY ISSUER_FRIENDLY_NAME, SUBJECT_FRIENDLY_NAME, c.ID
) LOOP
SELECT ctp.*
INTO t_ctp
FROM ca_trust_purpose ctp
WHERE ctp.CA_ID = l_record.ISSUER_CA_ID
AND ctp.TRUST_CONTEXT_ID = 1
AND ctp.TRUST_PURPOSE_ID = 1;
SELECT ctp.*
INTO t_ctp2
FROM ca_trust_purpose ctp
WHERE ctp.CA_ID = l_record.ISSUER_CA_ID
AND ctp.TRUST_CONTEXT_ID = 5
AND ctp.TRUST_PURPOSE_ID = 1;
SELECT ctp.*
INTO t_ctp3
FROM ca_trust_purpose ctp
WHERE ctp.CA_ID = l_record.ISSUER_CA_ID
AND ctp.TRUST_CONTEXT_ID = 12
AND ctp.TRUST_PURPOSE_ID = 1;
t_count := 3;
t_count2 := 0;
t_temp :=
' <TR>
<TD><A href="/?caID=' || l_record.ISSUER_CA_ID::text || '">' || coalesce(l_record.ISSUER_FRIENDLY_NAME, '?') || '</A></TD>
<TD><A href="/?id=' || l_record.ID::text || '">' || coalesce(l_record.SUBJECT_FRIENDLY_NAME, '?') || '</A></TD>
<TD style="color:';
IF l_record.SERIAL_NUMBER IS NOT NULL THEN
t_temp := t_temp || '00CC00">Revoked';
t_count2 := t_count2 + 1;
ELSIF l_record.NOT_AFTER < statement_timestamp() THEN
t_temp := t_temp || '888888">Expired';
ELSE
t_temp := t_temp || 'CC0000">Valid';
END IF;
t_temp := t_temp || '</TD>
<TD style="color:';
IF l_record.MS_CERTIFICATE_ID IS NOT NULL THEN
t_temp := t_temp || '00CC00">Revoked';
t_count2 := t_count2 + 1;
ELSIF t_ctp.CA_ID IS NULL THEN
t_temp := t_temp || '888888">Untrusted';
t_count := t_count - 1;
ELSIF (l_record.NOT_AFTER < statement_timestamp()) OR (NOT t_ctp.IS_TIME_VALID) THEN
t_temp := t_temp || '888888">Expired';
ELSIF t_ctp.ALL_CHAINS_REVOKED_VIA_DISALLOWEDSTL THEN
t_temp := t_temp || '00CC00">ParentRevoked';
ELSIF t_ctp.ALL_CHAINS_TECHNICALLY_CONSTRAINED OR l_record.IS_TECHNICALLY_CONSTRAINED THEN
t_temp := t_temp || '888888">Constrained';
ELSE
t_temp := t_temp || 'CC0000">Valid';
END IF;
t_temp := t_temp || '</TD>
<TD style="color:';
IF l_record.MOZ_CERTIFICATE_ID IS NOT NULL THEN
t_temp := t_temp || '00CC00">Revoked';
t_count2 := t_count2 + 1;
ELSIF t_ctp2.CA_ID IS NULL THEN
t_temp := t_temp || '888888">Untrusted';
t_count := t_count - 1;
ELSIF (l_record.NOT_AFTER < statement_timestamp()) OR (NOT t_ctp2.IS_TIME_VALID) THEN
t_temp := t_temp || '888888">Expired';
ELSIF t_ctp2.ALL_CHAINS_REVOKED_VIA_ONECRL THEN
t_temp := t_temp || '00CC00">ParentRevoked';
ELSIF t_ctp2.ALL_CHAINS_TECHNICALLY_CONSTRAINED OR l_record.IS_TECHNICALLY_CONSTRAINED THEN
t_temp := t_temp || '888888">Constrained';
ELSE
t_temp := t_temp || 'CC0000">Valid';
END IF;
t_temp := t_temp || '</TD>
<TD style="color:';
IF (l_record.MOZILLA_DISCLOSURE_STATUS::text LIKE 'Disclos%')
OR (l_record.MICROSOFT_DISCLOSURE_STATUS::text LIKE 'Disclos%') THEN
t_temp := t_temp || 'CC0000">Disclosed';
ELSIF l_record.REVOCATION_STATUS = 'Revoked' THEN
t_temp := t_temp || '00CC00">Revoked';
t_count2 := t_count2 + 1;
ELSIF l_record.REVOCATION_STATUS = 'Parent Cert Revoked' THEN
t_temp := t_temp || '00CC00">ParentRevoked';
t_count2 := t_count2 + 1;
ELSIF t_ctp2.CA_ID IS NULL THEN
t_temp := t_temp || '888888">Untrusted';
t_count := t_count - 1;
ELSIF (l_record.NOT_AFTER < statement_timestamp()) OR (NOT t_ctp2.IS_TIME_VALID) THEN
t_temp := t_temp || '888888">Expired';
ELSIF t_ctp2.ALL_CHAINS_TECHNICALLY_CONSTRAINED OR l_record.IS_TECHNICALLY_CONSTRAINED THEN
t_temp := t_temp || '888888">Constrained';
ELSE
t_temp := t_temp || 'CC0000">Undisclosed';
END IF;
t_temp := t_temp || '</TD>
<TD style="color:';
IF l_record.ENTRY_TYPE IS NOT NULL THEN
t_temp := t_temp || '00CC00">Revoked';
t_count2 := t_count2 + 1;
ELSIF (t_ctp.CA_ID IS NULL) AND (t_ctp2.CA_ID IS NULL) AND (t_ctp3.CA_ID IS NULL) THEN
t_temp := t_temp || '888888">Untrusted';
t_count := t_count - 1;
ELSIF l_record.NOT_AFTER < statement_timestamp() OR ((NOT t_ctp.IS_TIME_VALID) AND (NOT t_ctp2.IS_TIME_VALID) OR (NOT t_ctp3.IS_TIME_VALID)) THEN
t_temp := t_temp || '888888">Expired';
ELSIF t_ctp.ALL_CHAINS_REVOKED_VIA_CRLSET AND t_ctp2.ALL_CHAINS_REVOKED_VIA_CRLSET AND t_ctp3.ALL_CHAINS_REVOKED_VIA_CRLSET THEN
t_temp := t_temp || '00CC00">ParentRevoked';
ELSIF (t_ctp.ALL_CHAINS_TECHNICALLY_CONSTRAINED AND t_ctp2.ALL_CHAINS_TECHNICALLY_CONSTRAINED AND t_ctp3.ALL_CHAINS_TECHNICALLY_CONSTRAINED) OR l_record.IS_TECHNICALLY_CONSTRAINED THEN
t_temp := t_temp || '888888">Constrained';
ELSE
t_temp := t_temp || 'CC0000">Valid';
END IF;
t_temp := t_temp || '</TD>
</TR>
';
IF (t_count > 0) AND (t_count2 > 0) THEN
t_output := t_output || t_temp;
END IF;
END LOOP;
t_output := t_output ||
'</TABLE>
';
ELSIF t_type = 'mozilla-certvalidations-by-root' THEN
t_outputType := 'csv';
t_output := 'Date';
FOR l_record IN (
SELECT mrh.CERTIFICATE_ID, get_ca_name_attribute(cac.CA_ID) FRIENDLY_NAME, get_ca_name_attribute(cac.CA_ID, 'organizationalUnitName') OU, replace(mrh.CA_OWNER, chr(10), ', ') CA_OWNER
FROM mozilla_root_hashes mrh
LEFT OUTER JOIN ca_certificate cac ON (mrh.CERTIFICATE_ID = cac.CERTIFICATE_ID)
LEFT OUTER JOIN ccadb_certificate cc ON (mrh.CERTIFICATE_ID = cc.CERTIFICATE_ID)
WHERE mrh.DISPLAY_ORDER IS NOT NULL
GROUP BY mrh.DISPLAY_ORDER, mrh.CERTIFICATE_ID, cac.CA_ID, mrh.CA_OWNER
ORDER BY mrh.DISPLAY_ORDER
) LOOP
IF l_record.FRIENDLY_NAME IN ('GlobalSign') THEN
l_record.FRIENDLY_NAME := l_record.OU;
END IF;
t_output := t_output || '|[' || coalesce(l_record.CA_OWNER, 'UNKNOWN') || '] ' || replace(l_record.FRIENDLY_NAME, '|', '\|');
END LOOP;
FOR l_record IN (
SELECT mrh.DISPLAY_ORDER, mcvs.SUBMISSION_DATE, mcvs.COUNT
FROM mozilla_cert_validation_success mcvs, mozilla_root_hashes mrh
WHERE mcvs.BIN_NUMBER = mrh.BIN_NUMBER
AND mrh.DISPLAY_ORDER IS NOT NULL
ORDER BY mcvs.SUBMISSION_DATE, mrh.DISPLAY_ORDER
) LOOP
IF l_record.DISPLAY_ORDER = 1 THEN
t_output := t_output || chr(10) || l_record.SUBMISSION_DATE::text;
END IF;
t_output := t_output || '|' || coalesce(l_record.COUNT, 0);
END LOOP;
ELSIF t_type = 'mozilla-certvalidations-by-owner' THEN
t_outputType := 'csv';
t_output := 'Date';
FOR l_record IN (
SELECT coalesce(replace(mrh.CA_OWNER, chr(10), ', '), 'UNKNOWN') CA_OWNER
FROM mozilla_root_hashes mrh
LEFT OUTER JOIN ccadb_certificate cc ON (mrh.CERTIFICATE_ID = cc.CERTIFICATE_ID)
WHERE mrh.DISPLAY_ORDER IS NOT NULL
GROUP BY mrh.CA_OWNER
ORDER BY min(mrh.DISPLAY_ORDER)
) LOOP
t_output := t_output || '|' || coalesce(l_record.CA_OWNER, 'UNKNOWN');
END LOOP;
t_temp := '';
FOR l_record IN (
SELECT coalesce(replace(mrh.CA_OWNER, chr(10), ', '), 'UNKNOWN') CA_OWNER, min(mrh.DISPLAY_ORDER) DISPLAY_ORDER, mcvs.SUBMISSION_DATE, sum(mcvs.COUNT) COUNT
FROM mozilla_cert_validation_success mcvs, mozilla_root_hashes mrh
LEFT OUTER JOIN ccadb_certificate cc ON (mrh.CERTIFICATE_ID = cc.CERTIFICATE_ID)
WHERE mcvs.BIN_NUMBER = mrh.BIN_NUMBER
AND mrh.DISPLAY_ORDER IS NOT NULL
GROUP BY mrh.CA_OWNER, mcvs.SUBMISSION_DATE
ORDER BY mcvs.SUBMISSION_DATE, min(mrh.DISPLAY_ORDER)
) LOOP
IF l_record.DISPLAY_ORDER = 1 THEN
t_output := t_output || chr(10) || l_record.SUBMISSION_DATE::text;
END IF;
t_output := t_output || '|' || coalesce(l_record.COUNT, 0);
END LOOP;
ELSIF t_type = 'mozilla-certvalidations-by-version' THEN
t_certificateID := coalesce(get_parameter('id', paramNames, paramValues), '0')::integer;
t_outputType := 'csv';
t_output := 'Date';
SELECT array_agg(sub.RELEASE_VERSION)
INTO t_versions
FROM (
SELECT (mcvsi.RELEASE || '/' || mcvsi.VERSION) RELEASE_VERSION
FROM mozilla_root_hashes mrh, mozilla_cert_validation_success_import mcvsi
WHERE mrh.CERTIFICATE_ID = t_certificateID
AND mrh.BIN_NUMBER = mcvsi.BIN_NUMBER
GROUP BY mcvsi.RELEASE, mcvsi.VERSION
ORDER BY mcvsi.RELEASE, mcvsi.VERSION::integer
) sub;
FOR i IN 1..array_length(t_versions, 1) LOOP
t_output := t_output || '|' || t_versions[i];
END LOOP;
t_date := '2000-01-01'::date;
t_pos1 := array_length(t_versions, 1);
FOR l_record IN (
SELECT mcvsi.SUBMISSION_DATE, mcvsi.COUNT, (mcvsi.RELEASE || '/' || mcvsi.VERSION) RELEASE_VERSION
FROM mozilla_root_hashes mrh, mozilla_cert_validation_success_import mcvsi
WHERE mrh.CERTIFICATE_ID = t_certificateID
AND mrh.BIN_NUMBER = mcvsi.BIN_NUMBER
ORDER BY mcvsi.SUBMISSION_DATE, mcvsi.RELEASE, mcvsi.VERSION::integer
) LOOP
IF l_record.SUBMISSION_DATE > t_date THEN
WHILE t_pos1 < array_length(t_versions, 1) LOOP
t_output := t_output || '|0';
t_pos1 := t_pos1 + 1;
END LOOP;
t_date := l_record.SUBMISSION_DATE;
t_output := t_output || chr(10) || l_record.SUBMISSION_DATE::text;
t_pos1 := 1;
END IF;
WHILE l_record.RELEASE_VERSION != t_versions[t_pos1] LOOP
t_output := t_output || '|0';
t_pos1 := t_pos1 + 1;
END LOOP;
t_pos1 := t_pos1 + 1;
t_output := t_output || '|' || coalesce(l_record.COUNT, 0);
END LOOP;
WHILE t_pos1 < array_length(t_versions, 1) LOOP
t_output := t_output || '|X';
t_pos1 := t_pos1 + 1;
END LOOP;
ELSIF t_type = 'mozilla-certvalidations' THEN
t_certificateID := get_parameter('id', paramNames, paramValues)::integer;
t_temp := '';
IF t_certificateID IS NOT NULL THEN
t_temp := 'id=' || t_certificateID::text;
END IF;
IF coalesce(t_groupBy, 'root') NOT IN ('owner', 'version') THEN
t_groupBy := 'root';
END IF;
t_output := t_output ||
' <SPAN class="whiteongrey">Mozilla Certificate Validations</SPAN>';
IF t_groupBy IN ('owner', 'version') THEN
t_output := t_output || '
<A style="font-size:8pt" href="?group=root">Group by Root</A>';
END IF;
IF t_groupBy IN ('root', 'version') THEN
t_output := t_output || '
<A style="font-size:8pt" href="?group=owner">Group by Owner</A>';
END IF;
t_output := t_output || '
<BR><SPAN class="small"><A href="//mzl.la/2nvPgJs" target="_blank">CERT_VALIDATION_SUCCESS_BY_CA telemetry</A> for ';
IF t_groupBy IN ('owner', 'root') THEN
t_output := t_output || 'all Firefox Release versions';
ELSE
SELECT get_ca_name_attribute(cac.CA_ID)
INTO t_temp2
FROM ca_certificate cac
WHERE cac.CERTIFICATE_ID = t_certificateID;
t_output := t_output || '<B>' || t_temp2 || '</B>';
END IF;
t_output := t_output || '</SPAN>
<BR><BR>
<DIV id="root" style="text-align:left;font:8pt Arial;font-weight:normal">
<DIV id="graph" class="many" style="width:100%"></DIV>
<DIV id="options">
<BUTTON onclick="toggleAll(true)">Select All</BUTTON>
<BUTTON onclick="toggleAll(false)">Deselect All</BUTTON>
</DIV>
<DIV style="height:400px;width:500px;overflow:auto"><FORM id="graph_toggles"></FORM></DIV>
<DIV id="graph_labels" style="text-align:left"></DIV>
</DIV>
<SCRIPT type="text/javascript">
var graph = new Dygraph(
document.getElementById("graph"),
"/mozilla-certvalidations-by-' || t_groupBy || '?' || t_temp || '", {
axes: {
x: {
drawGrid: false
},
y: {
drawAxis: true,
drawGrid: true
}
},
connectSeparatedPoints: true,
delimiter: ''|'',
highlightCircleSize: 2,
highlightSeriesOpts: {
strokeWidth: 2,
strokeBorderWidth: 1,
highlightCircleSize: 3
},
includeZero: true,
panEdgeFraction: 0.1,
strokeBorderWidth: 1,
strokeWidth: 1,
labelsKMB: true,
xRangePad: 50
});
var onclick = function(ev) {
if (graph.isSeriesLocked()) {
graph.clearSelection();
} else {
graph.setSelection(graph.getSelection(), graph.getHighlightSeries(), true);
}
};
graph.updateOptions({clickCallback: onclick}, true);
graph.ready(function() {
var toggles_form = document.getElementById("graph_toggles");
var labels = graph.getLabels();
var colors = graph.getColors();
for (var i = 1; i < labels.length; ++i) {
(function(series) {
var label_elt = document.createElement("label");
label_elt.style.color = colors[series];
var checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.onclick = function () {
graph.setVisibility(series, this.checked);
};
checkbox.checked = true;
label_elt.appendChild(checkbox);
var label_span = document.createElement("span");
label_span.innerHTML = " " + labels[series + 1];
label_elt.appendChild(label_span);
toggles_form.appendChild(label_elt);
})(i - 1);
}
});
function toggleAll(clicked) {
var w = document.getElementsByTagName(''input'');
for(var i = 0; i < w.length; i++) {
if ((w[i].type == ''checkbox'') && (w[i].checked != clicked)) {
w[i].click();
}
}
}
</SCRIPT>
';
ELSIF t_type = 'mozilla-disclosures' THEN
t_output := t_output || mozilla_disclosures();
ELSIF t_type = 'mozilla-onecrl' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Mozilla OneCRL</SPAN>
<BR><SPAN class="small">Generated at ' || TO_CHAR(statement_timestamp() AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS') || ' UTC</SPAN>
<BR><BR>
<TABLE>
<TR>
<TH style="white-space:nowrap">crt.sh ID</TH>
<TH>Created</TH>
<TH>Summary</TH>
<TH>Bug</TH>
<TH>Serial Number</TH>
<TH>Issuer Name</TH>
<TH>Subject Name</TH>
<TH>Not After</TH>
</TR>
';
FOR l_record IN (
SELECT mo.CERTIFICATE_ID, mo.CREATED, mo.SUMMARY, mo.BUG_URL, mo.SERIAL_NUMBER,
mo.ISSUER_CA_ID, x509_name_print(mo.ISSUER_NAME) ISSUER_NAME_TEXT,
x509_name_print(mo.SUBJECT_NAME) SUBJECT_NAME_TEXT, mo.NOT_AFTER
FROM mozilla_onecrl mo
ORDER BY mo.CREATED DESC NULLS FIRST, mo.SUMMARY, mo.BUG_URL, ISSUER_NAME_TEXT, mo.SERIAL_NUMBER
) LOOP
t_output := t_output ||
' <TR>
<TD>';
IF l_record.CERTIFICATE_ID IS NOT NULL THEN
t_output := t_output || '<A href="/?id=' || l_record.CERTIFICATE_ID::text || '" target="_blank">' || coalesce(l_record.CERTIFICATE_ID::text, '') || '</A>';
ELSE
t_output := t_output || ' ';
END IF;
t_output := t_output || '</TD>
<TD style="white-space:nowrap">' || coalesce(TO_CHAR(l_record.CREATED, 'YYYY-MM-DD'), 'Unspecified') || '</TD>
<TD>' || l_record.SUMMARY || '</TD>
<TD><A href="' || l_record.BUG_URL || '" target="_blank">' || substring(l_record.BUG_URL from '[0-9]*$') || '</A></TD>
<TD>' || encode(l_record.SERIAL_NUMBER, 'hex') || '</TD>
<TD>';
IF l_record.ISSUER_CA_ID IS NOT NULL THEN
t_output := t_output || '<A href="/?caID=' || l_record.ISSUER_CA_ID::text || '" style="white-space:normal" target="_blank">';
END IF;
t_output := t_output || l_record.ISSUER_NAME_TEXT;
IF l_record.ISSUER_CA_ID IS NOT NULL THEN
t_output := t_output || '</A>';
END IF;
t_output := t_output || '</TD>
<TD>' || coalesce(l_record.SUBJECT_NAME_TEXT, ' ') || '</TD>
<TD style="white-space:nowrap">' || coalesce(TO_CHAR(l_record.NOT_AFTER, 'YYYY-MM-DD'), ' ') || '</TD>
</TR>
';
END LOOP;
t_output := t_output ||
'</TABLE>
';
ELSIF t_type = 'microsoft-disclosures' THEN
t_output := t_output || microsoft_disclosures();
ELSIF t_type IN (
'ID',
'SHA-1(Certificate)',
'SHA-256(Certificate)',
'Certificate ASN.1'
)
OR (
(lower(',' || t_opt) LIKE '%,firstresult,%')
AND (t_type = 'Serial Number')
) THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Certificate Search</SPAN>
<BR><BR>
';
t_certSummary := 'Leaf certificate';
-- Search for a specific Certificate.
IF t_type IN ('ID', 'Certificate ASN.1') THEN
SELECT c.ID, x509_print(c.CERTIFICATE, NULL, 196608), ca.ID, cac.CA_ID,
digest(c.CERTIFICATE, 'sha1'::text),
digest(c.CERTIFICATE, 'sha256'::text),
x509_serialNumber(c.CERTIFICATE),
digest(x509_publicKey(c.CERTIFICATE), 'sha256'::text),
c.CERTIFICATE
INTO t_certificateID, t_text, t_issuerCAID, t_caID,
t_certificateSHA1,
t_certificateSHA256,
t_serialNumber,
t_spkiSHA256,
t_certificate
FROM certificate c
LEFT OUTER JOIN ca ON (c.ISSUER_CA_ID = ca.ID)
LEFT OUTER JOIN ca_certificate cac ON (c.ID = cac.CERTIFICATE_ID)
WHERE c.ID = t_value::integer;
ELSIF t_type = 'SHA-1(Certificate)' THEN
SELECT c.ID, x509_print(c.CERTIFICATE, NULL, 196608), ca.ID, cac.CA_ID,
digest(c.CERTIFICATE, 'sha1'::text),
digest(c.CERTIFICATE, 'sha256'::text),
x509_serialNumber(c.CERTIFICATE),
digest(x509_publicKey(c.CERTIFICATE), 'sha256'::text),
c.CERTIFICATE
INTO t_certificateID, t_text, t_issuerCAID, t_caID,
t_certificateSHA1,
t_certificateSHA256,
t_serialNumber,
t_spkiSHA256,
t_certificate
FROM certificate c
LEFT OUTER JOIN ca ON (c.ISSUER_CA_ID = ca.ID)
LEFT OUTER JOIN ca_certificate cac
ON (c.ID = cac.CERTIFICATE_ID)
WHERE digest(c.CERTIFICATE, 'sha1') = t_bytea;
ELSIF t_type = 'SHA-256(Certificate)' THEN
SELECT c.ID, x509_print(c.CERTIFICATE, NULL, 196608), ca.ID, cac.CA_ID,
digest(c.CERTIFICATE, 'sha1'::text),
digest(c.CERTIFICATE, 'sha256'::text),
x509_serialNumber(c.CERTIFICATE),
digest(x509_publicKey(c.CERTIFICATE), 'sha256'::text),
c.CERTIFICATE
INTO t_certificateID, t_text, t_issuerCAID, t_caID,
t_certificateSHA1,
t_certificateSHA256,
t_serialNumber,
t_spkiSHA256,
t_certificate
FROM certificate c
LEFT OUTER JOIN ca ON (c.ISSUER_CA_ID = ca.ID)
LEFT OUTER JOIN ca_certificate cac
ON (c.ID = cac.CERTIFICATE_ID)
WHERE digest(c.CERTIFICATE, 'sha256') = t_bytea;
ELSIF t_type = 'Serial Number' THEN
SELECT c.ID, x509_print(c.CERTIFICATE, NULL, 196608), ca.ID, cac.CA_ID,
digest(c.CERTIFICATE, 'sha1'::text),
digest(c.CERTIFICATE, 'sha256'::text),
x509_serialNumber(c.CERTIFICATE),
digest(x509_publicKey(c.CERTIFICATE), 'sha256'::text),
c.CERTIFICATE
INTO t_certificateID, t_text, t_issuerCAID, t_caID,
t_certificateSHA1,
t_certificateSHA256,
t_serialNumber,
t_spkiSHA256,
t_certificate
FROM certificate c
LEFT OUTER JOIN ca ON (c.ISSUER_CA_ID = ca.ID)
LEFT OUTER JOIN ca_certificate cac
ON (c.ID = cac.CERTIFICATE_ID)
WHERE x509_serialNumber(c.CERTIFICATE) = t_bytea
LIMIT 1;
END IF;
IF t_text IS NULL THEN
RAISE no_data_found USING MESSAGE = 'Certificate not found ';
END IF;
-- For embedded SCTs, insert the Log Names.
t_offset := 1;
LOOP
t_pos1 := strpos(substr(t_text, t_offset), 'Log ID : ');
EXIT WHEN t_pos1 = 0;
t_pos1 := t_pos1 + t_offset - 1;
t_temp := translate(
substr(t_text, t_pos1 + 12, 128), ': ' || chr(10), ''
);
SELECT ctl.NAME
INTO t_temp
FROM ct_log ctl
WHERE digest(ctl.PUBLIC_KEY, 'sha256') = decode(t_temp, 'hex');
t_temp := 'Log Name : ' || coalesce(html_escape(t_temp), 'Unknown')
|| chr(10) || ' ';
t_text := substr(t_text, 1, t_pos1 - 1) || t_temp
|| substr(t_text, t_pos1);
t_offset := t_pos1 + length(t_temp) + 1;
END LOOP;
t_text := replace(html_escape(t_text), chr(10), '<BR>');
t_text := replace(t_text, ', DNS:', '<BR> DNS:');
t_text := replace(t_text, ', IP Address:', '<BR> IP Address:');
t_text := replace(t_text, ' ', ' ');
t_text := replace(
t_text, 'Certificate:<BR> ',
'<A href="?d=' || t_certificateID::text
|| '">Certificate:</A><BR> '
);
t_text := replace(
t_text, '<BR> Serial Number:',
'<BR> <A href="?serial='
|| encode(t_serialNumber, 'hex')
|| '">Serial Number:</A>'
);
t_temp := '';
IF t_opt != '' THEN
t_temp := '&opt=' || RTRIM(t_opt, ',');
END IF;
IF t_issuerCAID IS NOT NULL THEN
t_text := replace(
t_text, '<BR> Issuer:<BR>',
'<BR> <A href="?caid='
|| t_issuerCAID::text
|| t_temp || '">Issuer:</A><BR>'
);
END IF;
IF t_caID IS NOT NULL THEN
t_text := replace(
t_text, '<BR> Subject:<BR>',
'<BR> <A href="?caid='
|| t_caID::text
|| t_temp || '">Subject:</A><BR>'
);
IF t_caID = coalesce(t_issuerCAID, -1) THEN
t_certSummary := 'Root certificate';
ELSE
t_certSummary := 'Intermediate certificate';
END IF;
END IF;
IF t_spkiSHA256 IS NOT NULL THEN
t_text := replace(
t_text, '<BR> Subject Public Key Info:<BR>',
'<BR> <A href="?spkisha256='
|| encode(t_spkiSHA256, 'hex')
|| '">Subject Public Key Info:</A><BR>'
);
END IF;
t_text := replace(
t_text, '<BR> X509v3 Subject Key Identifier: <BR>',
'<BR> <A href="?ski='
|| coalesce(encode(x509_subjectKeyIdentifier(t_certificate), 'hex'), '')
|| '">X509v3 Subject Key Identifier:</A><BR>'
);
t_offset := strpos(t_text, 'CT Precertificate');
IF t_offset != 0 THEN
IF substr(t_text, t_offset, 34) = 'CT Precertificate Poison' THEN
t_certSummary := 'Precertificate';
END IF;
SELECT c.ID::text
INTO t_temp
FROM certificate c
WHERE x509_serialNumber(c.certificate) = t_serialNumber
AND c.ISSUER_CA_ID = t_issuerCAID
AND c.ID != t_certificateID;
IF t_temp IS NOT NULL THEN
IF t_certSummary = 'Precertificate' THEN
t_text := substr(t_text, 1, t_offset - 1)
|| 'CT Pre<A href="?id=' || t_temp
|| '">certificate</A>'
|| substr(t_text, t_offset + 22);
ELSE
t_text := substr(t_text, 1, t_offset - 1)
|| 'CT <A href="?id=' || t_temp
|| '">Precertificate</A>'
|| substr(t_text, t_offset + 22);
END IF;
END IF;
END IF;
t_output := t_output ||
'<TABLE>
<TR>
<TH class="outer">Criteria</TH>
<TD class="outer">' || html_escape(t_type) || ' = ''' || html_escape(t_value) || '''</TD>
</TR>
</TABLE>
<BR>
<TABLE>
<TR>
<TH class="outer">crt.sh ID</TH>
<TD class="outer">';
IF t_certificateID IS NOT NULL THEN
t_output := t_output || '<A href="?id=' || t_certificateID::text || '">' || t_certificateID::text || '</A>';
ELSE
t_output := t_output || '<I>Not found</I>';
END IF;
t_output := t_output || '</TD>
</TR>
';
t_showMetadata := lower(',' || t_opt) NOT LIKE '%,nometadata,%';
IF t_showMetadata THEN
t_output := t_output ||
' <TR>
<TH class="outer">Summary</TH>
<TD class="outer">' || t_certSummary || '</TD>
</TR>
<TR>
<TH class="outer">Certificate<BR>Transparency</TH>
<TD class="outer">
';
t_temp := '';
FOR l_record IN (
SELECT ctl.NAME, ctl.URL, ctl.OPERATOR, ctle.ENTRY_ID, ctle.ENTRY_TIMESTAMP
FROM ct_log_entry ctle, ct_log ctl
WHERE ctle.CERTIFICATE_ID = t_certificateID
AND ctle.CT_LOG_ID = ctl.ID
ORDER BY ctle.ENTRY_TIMESTAMP
) LOOP
t_temp := t_temp ||
' <TR>
<TD>' || to_char(l_record.ENTRY_TIMESTAMP, 'YYYY-MM-DD')
|| ' <FONT class="small">'
|| to_char(l_record.ENTRY_TIMESTAMP, 'HH24:MI:SS UTC')
|| '</FONT></TD>
<TD>' || l_record.ENTRY_ID::text || '</TD>
<TD>' || html_escape(l_record.OPERATOR) || '</TD>
<TD>' || html_escape(l_record.URL) || '</TD>
</TR>
';
END LOOP;
IF t_temp != '' THEN
t_output := t_output ||
'<TABLE class="options" style="margin-left:0px">
<TR>
<TH>Timestamp</TH>
<TH>Entry #</TH>
<TH>Log Operator</TH>
<TH>Log URL</TH>
</TR>
' || t_temp ||
'</TABLE>
';
ELSE
t_output := t_output ||
' No log entries found
';
END IF;
t_output := t_output ||
' </TD>
</TR>
';
IF t_caID IS NOT NULL THEN
t_output := t_output ||
' <TR>
<TH class="outer">Audit details<BR>
<DIV class="small" style="padding-top:3px">Disclosed via the
<A href="//ccadb-public.secure.force.com/mozilla/PublicAllIntermediateCerts" target="_blank">CCADB</A></DIV>
</TH>
<TD class="outer">
';
t_temp := NULL;
t_temp2 := NULL;
FOR l_record IN (
SELECT *
FROM ccadb_certificate cc
WHERE cc.CCADB_RECORD_ID IS NOT NULL
AND cc.CERTIFICATE_ID = t_certificateID
) LOOP
IF t_temp IS NULL THEN
t_temp :=
'<TABLE class="options" style="margin-left:0px">
<TR>
<TH>Auditor</TH>
<TH>Standard Audit</TH>
<TH>BR Audit</TH>
<TH>Documents</TH>
<TH>CCADB</TH>
<TH>Root Owner / Certificate</TH>
</TR>
';
END IF;
t_temp := t_temp ||
' <TR>
<TD>' || coalesce(l_record.AUDITOR, '') || '</TD>
<TD>';
IF coalesce(l_record.STANDARD_AUDIT_URL, '') NOT LIKE '%://%' THEN
t_temp := t_temp || coalesce(l_record.STANDARD_AUDIT_URL, 'Not disclosed');
ELSE
t_temp := t_temp || '
<A href="' || l_record.STANDARD_AUDIT_URL || '" target="_blank">' || coalesce(l_record.STANDARD_AUDIT_DATE::text, 'Yes') || '</A>
';
END IF;
t_temp := t_temp || '</TD>
<TD>';
IF coalesce(l_record.BRSSL_AUDIT_URL, '') NOT LIKE '%://%' THEN
t_temp := t_temp || coalesce(l_record.BRSSL_AUDIT_URL, 'No');
ELSE
t_temp := t_temp || '
<A href="' || l_record.BRSSL_AUDIT_URL || '" target="_blank">Yes</A>
';
END IF;
t_temp := t_temp || '</TD>
<TD>
';
IF coalesce(l_record.CP_URL, '') != '' THEN
t_temp := t_temp ||
' <A href="' || l_record.CP_URL || '" target="blank">CP</A>
';
END IF;
IF coalesce(l_record.CPS_URL, '') != '' THEN
t_temp := t_temp ||
' <A href="' || l_record.CPS_URL || '" target="blank">CPS</A>
';
END IF;
t_temp := t_temp ||
' </TD>
<TD>';
IF l_record.CCADB_RECORD_ID IS NOT NULL THEN
t_temp := t_temp || '<A href="//ccadb.force.com/' || l_record.CCADB_RECORD_ID || '" target="_blank">' || l_record.CCADB_RECORD_ID || '</A>';
ELSE
t_temp := t_temp || ' ';
END IF;
t_temp := t_temp || '</TD>
<TD>';
IF l_record.INCLUDED_CERTIFICATE_ID IS NULL THEN
t_temp := t_temp || coalesce(html_escape(l_record.INCLUDED_CERTIFICATE_OWNER), ' ');
ELSE
t_temp := t_temp || '<A href="/?id=' || l_record.INCLUDED_CERTIFICATE_ID::text || '">' || coalesce(html_escape(l_record.INCLUDED_CERTIFICATE_OWNER), ' ') || '</A>';
END IF;
IF l_record.CERT_RECORD_TYPE = 'Root Certificate' THEN
t_temp2 :=
' <TR>
<TH class="outer">Telemetry<BR>
<DIV class="small" style="padding-top:3px">Collected by
<A href="//mzl.la/2nvPgJs" target="_blank">Mozilla</A></DIV>
</TH>
<TD class="outer"><A href="mozilla-certvalidations?group=version&id=' || t_certificateID::text || '" target="_blank">CERT_VALIDATION_SUCCESS_BY_CA</A></TD>
</TR>
';
END IF;
END LOOP;
IF t_temp IS NOT NULL THEN
t_temp := t_temp || '</TD>
</TR>
</TABLE>';
ELSE
t_temp := 'Not Disclosed';
END IF;
t_output := t_output || t_temp || '
</TD>
</TR>
' || coalesce(t_temp2, '');
END IF;
SELECT '<SPAN style="color:#CC0000">Revoked'
|| CASE coalesce(cr.REASON_CODE, 0)
WHEN 1 THEN ' (keyCompromise)'
WHEN 2 THEN ' (cACompromise)'
WHEN 3 THEN ' (affiliationChanged)'
WHEN 4 THEN ' (superseded)'
WHEN 5 THEN ' (cessationOfOperation)'
WHEN 6 THEN ' (certificateHold)'
WHEN 7 THEN ' (privilegeWithdrawn)'
WHEN 8 THEN ' (aACompromise)'
ELSE ''
END
|| '</SPAN></TD><TD>'
|| to_char(cr.REVOCATION_DATE, 'YYYY-MM-DD') || ' <FONT class="small">'
|| to_char(cr.REVOCATION_DATE, 'HH24:MI:SS UTC') || '</FONT></TD><TD>'
|| to_char(cr.LAST_SEEN_CHECK_DATE, 'YYYY-MM-DD') || ' <FONT class="small">'
|| to_char(cr.LAST_SEEN_CHECK_DATE, 'HH24:MI:SS UTC') || '</FONT>'
INTO t_temp0
FROM crl_revoked cr
WHERE cr.CA_ID = t_issuerCAID
AND cr.SERIAL_NUMBER = t_serialNumber;
t_count := 1;
IF NOT FOUND THEN
SELECT count(*)
INTO t_count
FROM crl
WHERE crl.CA_ID = t_issuerCAID
AND crl.ERROR_MESSAGE IS NULL
AND crl.NEXT_UPDATE > statement_timestamp();
IF t_count > 0 THEN
t_temp0 := 'Not Revoked</TD><TD><SPAN style="color:#888888">n/a</SPAN></TD><TD><SPAN style="color:#888888">n/a</SPAN>';
ELSE
t_temp0 := '<SPAN style="color:#FF9400">Unknown</SPAN></TD><TD><SPAN style="color:#888888">n/a<SPAN></TD><TD><SPAN style="color:#888888">n/a<SPAN>';
END IF;
END IF;
SELECT to_char(max(crl.LAST_CHECKED), 'YYYY-MM-DD') || ' <FONT class="small">'
|| to_char(max(crl.LAST_CHECKED), 'HH24:MI:SS UTC') || '</FONT>'
INTO t_temp
FROM crl
WHERE crl.CA_ID = t_issuerCAID;
t_temp0 := t_temp0 || '</TD><TD>' || coalesce(t_temp, '');
IF t_count = 0 THEN
SELECT array_to_string(array_agg('<FONT color="#CC0000">' || html_escape(crl.ERROR_MESSAGE) || '</FONT> [' || html_escape(crl.DISTRIBUTION_POINT_URL || ']')), '<BR>')
INTO t_temp
FROM crl
WHERE crl.CA_ID = t_issuerCAID
AND crl.ERROR_MESSAGE IS NOT NULL;
IF t_temp IS NOT NULL THEN
t_temp0 := t_temp0 || '<BR><SPAN style="vertical-align:middle;font-size:70%">' || coalesce(t_temp, '') || '</SPAN>';
END IF;
END IF;
SELECT '<SPAN style="color:#CC0000">Revoked [by ' || gr.ENTRY_TYPE || ']</SPAN>'
INTO t_temp
FROM google_revoked gr
WHERE gr.CERTIFICATE_ID = t_certificateID;
t_temp := coalesce(t_temp, 'Not Revoked');
SELECT '<SPAN style="color:#CC0000">Revoked [by MD5(PublicKey)]</SPAN>'
INTO t_temp2
FROM microsoft_disallowedcert mdc
WHERE mdc.CERTIFICATE_ID = t_certificateID;
t_temp2 := coalesce(t_temp2, 'Not Revoked');
SELECT '<SPAN style="color:#CC0000">Revoked [by Issuer Name, Serial Number]</SPAN></TD><TD>'
|| to_char(mo.CREATED, 'YYYY-MM-DD') || ' <FONT class="small">'
|| to_char(mo.CREATED, 'HH24:MI:SS UTC') || '</FONT>'
INTO t_temp3
FROM mozilla_onecrl mo
WHERE mo.CERTIFICATE_ID = t_certificateID;
t_temp3 := coalesce(t_temp3, 'Not Revoked</TD><TD><SPAN style="color:#888888">n/a</SPAN>');
t_output := t_output ||
' <TR>
<TH class="outer">Revocation';
IF lower(',' || t_opt) NOT LIKE '%,problemreporting,%' THEN
t_output := t_output || '<BR><BR>
<DIV class="small" style="padding-top:3px"><A href="?id=' || t_certificateID::text || '&opt=problemreporting">Report a problem</A> with<BR>this certificate to the CA</DIV>';
END IF;
t_output := t_output || '</TH>
<TD class="outer">
<TABLE class="options" style="margin-left:0px">
<TR>
<TH>Mechanism</TH>
<TH>Provider</TH>
<TH>Status</TH>
<TH>Revocation Date</TH>
<TH>Last Observed in CRL</TH>
<TH>Last Checked <SPAN style="color:#CC0000;vertical-align:middle;font-size:70%;font-weight:normal">(Error)</SPAN></TH>
</TR>
<TR>
<TD>CRL</TD>
<TD>The CA</TD>
<TD>' || t_temp0 || '</TD>
</TR>
<TR>
<TD>CRLSet/Blacklist</TD>
<TD>Google</TD>
<TD>' || t_temp || '</TD>
<TD><SPAN style="color:#888888">n/a</SPAN></TD>
<TD><SPAN style="color:#888888">n/a</SPAN></TD>
<TD><SPAN style="color:#888888">n/a</SPAN></TD>
</TR>
<TR>
<TD>disallowedcert.stl</TD>
<TD>Microsoft</TD>
<TD>' || t_temp2 || '</TD>
<TD><SPAN style="color:#888888">n/a</SPAN></TD>
<TD><SPAN style="color:#888888">n/a</SPAN></TD>
<TD><SPAN style="color:#888888">n/a</SPAN></TD>
</TR>
<TR>
<TD><A href="/mozilla-onecrl" target="_blank">OneCRL</A></TD>
<TD>Mozilla</TD>
<TD>' || t_temp3 || '</TD>
<TD><SPAN style="color:#888888">n/a</SPAN></TD>
<TD><SPAN style="color:#888888">n/a</SPAN></TD>
</TR>
</TABLE>
</TD>
</TR>
';
IF lower(',' || t_opt) LIKE '%,problemreporting,%' THEN
SELECT cco.PROBLEM_REPORTING
INTO t_temp3
FROM ca_certificate cac, ccadb_certificate cc, ccadb_caowner cco, ca_trust_purpose ctp, certificate c
WHERE cac.CA_ID = t_issuerCAID
AND cac.CERTIFICATE_ID = cc.CERTIFICATE_ID
AND cc.INCLUDED_CERTIFICATE_OWNER = cco.CA_OWNER_NAME
AND cac.CA_ID = ctp.CA_ID
AND cac.CERTIFICATE_ID = c.ID
GROUP BY cco.PROBLEM_REPORTING
ORDER BY min(ctp.SHORTEST_CHAIN), max(x509_notAfter(c.CERTIFICATE)) DESC
LIMIT 1;
IF trim(coalesce(t_temp3, '')) = '' THEN
t_temp3 := 'Unknown';
END IF;
t_output := t_output ||
' <TR>
<TH class="outer">Problem Reporting<BR>
<DIV class="small" style="padding-top:3px">Mechanism(s) disclosed<BR>via the
<A href="//ccadb-public.secure.force.com/mozilla/CAInformationReport" target="_blank">CCADB</A></DIV>
</TH>
<TD class="outer">' || replace(html_escape(t_temp3), '. ', '.<BR>') || '</TD>
</TR>
';
END IF;
END IF;
t_output := t_output ||
' <TR>
<TH class="outer">SHA-256(Certificate)</TH>
<TD class="outer"><A href="//censys.io/certificates/' || coalesce(lower(encode(t_certificateSHA256, 'hex')), '') || '">'
|| coalesce(upper(encode(t_certificateSHA256, 'hex')), '<I>Not found</I>') || '</A></TD>
</TR>
<TR>
<TH class="outer">SHA-1(Certificate)</TH>
<TD class="outer">' || coalesce(upper(encode(t_certificateSHA1, 'hex')), '<I>Not found</I>') || '</TD>
</TR>
';
t_showCABLint := (',' || t_opt) LIKE '%,cablint,%';
IF t_showCABLint THEN
t_output := t_output ||
' <TR>
<TH class="outer">CA/B Forum lint<BR>
<DIV class="small" style="padding-top:3px">Powered by <A href="//github.com/awslabs/certlint" target="_blank">certlint</A></DIV>
</TH>
<TD class="text">
';
FOR l_record IN (
SELECT substr(CABLINT, 4) ISSUE_TEXT,
CASE substr(CABLINT, 1, 2)
WHEN 'B:' THEN 1
WHEN 'I:' THEN 2
WHEN 'N:' THEN 3
WHEN 'F:' THEN 4
WHEN 'E:' THEN 5
WHEN 'W:' THEN 6
ELSE 5
END ISSUE_TYPE,
CASE substr(CABLINT, 1, 2)
WHEN 'B:' THEN '<SPAN> BUG:'
WHEN 'I:' THEN '<SPAN> INFO:'
WHEN 'N:' THEN '<SPAN class="notice"> NOTICE:'
WHEN 'F:' THEN '<SPAN class="fatal"> FATAL:'
WHEN 'E:' THEN '<SPAN class="error"> ERROR:'
WHEN 'W:' THEN '<SPAN class="warning"> WARNING:'
ELSE '<SPAN> ' || substr(CABLINT, 1, 2)
END ISSUE_HEADING
FROM cablint_embedded(t_certificate) CABLINT
ORDER BY ISSUE_TYPE, ISSUE_TEXT
) LOOP
t_output := t_output ||
' ' || l_record.ISSUE_HEADING || ' ' || l_record.ISSUE_TEXT || ' </SPAN><BR>';
END LOOP;
t_output := t_output ||
' </TD>
</TR>
';
END IF;
t_showX509Lint := (',' || t_opt) LIKE '%,x509lint,%';
IF t_showX509Lint THEN
IF NOT x509_canIssueCerts(t_certificate) THEN
t_certType := 0;
ELSIF t_caID != t_issuerCAID THEN
t_certType := 1;
ELSE
t_certType := 2;
END IF;
t_output := t_output ||
' <TR>
<TH class="outer">X.509 lint<BR>
<DIV class="small" style="padding-top:3px">Powered by <A href="//github.com/kroeckx/x509lint" target="_blank">x509lint</A></DIV>
</TH>
<TD class="text">
';
FOR l_record IN (
SELECT substr(X509LINT, 4) ISSUE_TEXT,
CASE substr(X509LINT, 1, 2)
WHEN 'B:' THEN 1
WHEN 'I:' THEN 2
WHEN 'N:' THEN 3
WHEN 'F:' THEN 4
WHEN 'E:' THEN 5
WHEN 'W:' THEN 6
ELSE 5
END ISSUE_TYPE,
CASE substr(X509LINT, 1, 2)
WHEN 'B:' THEN '<SPAN> BUG:'
WHEN 'I:' THEN '<SPAN> INFO:'
WHEN 'N:' THEN '<SPAN class="notice"> NOTICE:'
WHEN 'F:' THEN '<SPAN class="fatal"> FATAL:'
WHEN 'E:' THEN '<SPAN class="error"> ERROR:'
WHEN 'W:' THEN '<SPAN class="warning"> WARNING:'
ELSE '<SPAN> ' || substr(X509LINT, 1, 2)
END ISSUE_HEADING
FROM x509lint_embedded(t_certificate, t_certType) X509LINT
ORDER BY ISSUE_TYPE, ISSUE_TEXT
) LOOP
t_output := t_output ||
' ' || l_record.ISSUE_HEADING || ' ' || l_record.ISSUE_TEXT || ' </SPAN><BR>';
END LOOP;
t_output := t_output ||
' </TD>
</TR>
';
END IF;
t_showZLint := (',' || t_opt) LIKE '%,zlint,%';
IF t_showZLint THEN
t_output := t_output ||
' <TR>
<TH class="outer">ZLint<BR>
<DIV class="small" style="padding-top:3px">Powered by <A href="//github.com/zmap/zlint" target="_blank">zlint</A></DIV>
</TH>
<TD class="text">
';
FOR l_record IN (
SELECT substr(ZLINT, 4) ISSUE_TEXT,
CASE substr(ZLINT, 1, 2)
WHEN 'B:' THEN 1
WHEN 'I:' THEN 2
WHEN 'N:' THEN 3
WHEN 'F:' THEN 4
WHEN 'E:' THEN 5
WHEN 'W:' THEN 6
ELSE 5
END ISSUE_TYPE,
CASE substr(ZLINT, 1, 2)
WHEN 'B:' THEN '<SPAN> BUG:'
WHEN 'I:' THEN '<SPAN> INFO:'
WHEN 'N:' THEN '<SPAN class="notice"> NOTICE:'
WHEN 'F:' THEN '<SPAN class="fatal"> FATAL:'
WHEN 'E:' THEN '<SPAN class="error"> ERROR:'
WHEN 'W:' THEN '<SPAN class="warning"> WARNING:'
ELSE '<SPAN> ' || substr(ZLINT, 1, 2)
END ISSUE_HEADING
FROM zlint_embedded(t_certificate) ZLINT
ORDER BY ISSUE_TYPE, ISSUE_TEXT
) LOOP
t_output := t_output ||
' ' || l_record.ISSUE_HEADING || ' ' || l_record.ISSUE_TEXT || ' </SPAN><BR>';
END LOOP;
t_output := t_output ||
' </TD>
</TR>
';
END IF;
t_output := t_output ||
' <TR>
';
IF t_type = 'Certificate ASN.1' THEN
t_output := t_output ||
' <TH class="outer"><A href="?id=' || t_certificateID::text || '">Certificate</A> | ASN.1
<BR><BR><SPAN class="small">Powered by <A href="//lapo.it/asn1js/" target="_blank">asn1js</A><BR>
';
IF t_showMetadata THEN
t_output := t_output ||
' <BR><BR><A href="?asn1=' || t_certificateID::text || '&opt=' || t_opt || 'nometadata">Hide metadata</A>
';
ELSE
IF t_opt = 'nometadata,' THEN
t_temp := '';
ELSE
t_temp := '&opt=' || rtrim(replace(t_opt, 'nometadata,', ''), ',');
END IF;
t_output := t_output ||
' <BR><BR><A href="?asn1=' || t_certificateID::text || t_temp || '">Show metadata</A>
';
END IF;
IF NOT t_showCABLint THEN
t_output := t_output ||
' <BR><BR><A href="?asn1=' || t_certificateID::text || '&opt=' || t_opt || 'cablint">Run cablint</A>
';
END IF;
IF NOT t_showX509Lint THEN
t_output := t_output ||
' <BR><BR><A href="?asn1=' || t_certificateID::text || '&opt=' || t_opt || 'x509lint">Run x509lint</A>
';
END IF;
IF NOT t_showZLint THEN
t_output := t_output ||
' <BR><BR><A href="?asn1=' || t_certificateID::text || '&opt=' || t_opt || 'zlint">Run zlint</A>
';
END IF;
t_output := t_output ||
' <BR><BR><BR>Download Certificate: <A href="?d=' || t_certificateID::text || '">PEM</A>
</SPAN>
</TH>
<TD class="text">
<DIV id="dump" style="position:absolute;right:20px;"></DIV>
<DIV id="tree"></DIV>
<SCRIPT type="text/javascript" src="/asn1js/base64.js"></SCRIPT>
<SCRIPT type="text/javascript" src="/asn1js/oids.js"></SCRIPT>
<SCRIPT type="text/javascript" src="/asn1js/int10.js"></SCRIPT>
<SCRIPT type="text/javascript" src="/asn1js/asn1.js"></SCRIPT>
<SCRIPT type="text/javascript" src="/asn1js/dom.js"></SCRIPT>
<SCRIPT type="text/javascript">
var tree = document.getElementById(''tree'');
var dump = document.getElementById(''dump'');
tree.innerHTML = '''';
dump.innerHTML = '''';
try {
var asn1 = ASN1.decode(Base64.unarmor('''
|| translate(encode(t_certificate, 'base64'), chr(10), '')
|| '''));
tree.appendChild(asn1.toDOM());
dump.appendChild(asn1.toHexDOM());
} catch (e) {
if (''textContent'' in tree)
tree.textContent = e;
else
tree.innerText = e;
}
</SCRIPT>
';
ELSE
t_output := t_output ||
' <TH class="outer">Certificate | <A href="?asn1=' || t_certificateID::text || '">ASN.1</A>
<SPAN class="small"><BR>
';
IF t_showMetadata THEN
t_output := t_output ||
' <BR><BR><A href="?id=' || t_certificateID::text || '&opt=' || t_opt || 'nometadata">Hide metadata</A>
';
ELSE
IF t_opt = 'nometadata,' THEN
t_temp := '';
ELSE
t_temp := '&opt=' || rtrim(replace(t_opt, 'nometadata,', ''), ',');
END IF;
t_output := t_output ||
' <BR><BR><A href="?id=' || t_certificateID::text || t_temp || '">Show metadata</A>
';
END IF;
IF NOT t_showCABLint THEN
t_output := t_output ||
' <BR><BR><A href="?id=' || t_certificateID::text || '&opt=' || t_opt || 'cablint">Run cablint</A>
';
END IF;
IF NOT t_showX509Lint THEN
t_output := t_output ||
' <BR><BR><A href="?id=' || t_certificateID::text || '&opt=' || t_opt || 'x509lint">Run x509lint</A>
';
END IF;
IF NOT t_showZLint THEN
t_output := t_output ||
' <BR><BR><A href="?id=' || t_certificateID::text || '&opt=' || t_opt || 'zlint">Run zlint</A>
';
END IF;
t_output := t_output ||
' <BR><BR><BR>Download Certificate: <A href="?d=' || t_certificateID::text || '">PEM</A>
</SPAN>
</TH>
<TD class="text">' || coalesce(t_text, '<I>Not found</I>');
END IF;
t_output := t_output ||
' </TD>
</TR>
</TABLE>
';
ELSIF t_type IN ('CA ID', 'CA Name') THEN
t_output := t_output || ' <SPAN class="whiteongrey">CA Search</SPAN>
<BR><BR>
';
-- Determine whether to use a reverse index (if available).
IF position('%' IN t_value) != 0 THEN
t_matchType := 'LIKE';
t_useReverseIndex := (
position('%' IN t_value) < position('%' IN reverse(t_value))
);
END IF;
t_output := t_output ||
'<TABLE>
<TR>
<TH class="outer">Criteria</TH>
<TD class="outer">' || html_escape(t_type)
|| ' ' || html_escape(t_matchType)
|| ' ''' || html_escape(t_value) || '''</TD>
</TR>
</TABLE>
<BR>
';
-- Search for a specific CA.
IF t_type = 'CA ID' THEN
SELECT ca.ID, ca.NAME, ca.PUBLIC_KEY
INTO t_caID, t_caName, t_caPublicKey
FROM ca
WHERE ca.ID = t_value::integer;
IF t_caName IS NULL THEN
RAISE no_data_found USING MESSAGE = 'CA not found';
ELSE
t_text := html_escape(t_caName);
END IF;
SELECT min(cac.CERTIFICATE_ID)
INTO t_certificateID
FROM ca_certificate cac
WHERE cac.CA_ID = t_caID;
IF t_certificateID IS NOT NULL THEN
SELECT html_escape(x509_print(c.CERTIFICATE, NULL, 7999))
INTO t_text
FROM certificate c
WHERE c.ID = t_certificateID;
t_text := replace(t_text, ' Subject:', 'Subject:');
t_text := replace(t_text, chr(10) || ' ', '<BR>');
t_text := replace(t_text, ' ', ' ');
END IF;
t_showMozillaDisclosure := (',' || t_opt || ',') LIKE '%,mozilladisclosure,%';
t_temp := '';
IF t_opt != '' THEN
t_temp := '&opt=' || RTRIM(t_opt, ',');
END IF;
t_output := t_output ||
'<TABLE>
<TR>
<TH class="outer">crt.sh CA ID</TH>
<TD class="outer">' || t_caID::text || '</TD>
</TR>
<TR>
<TH class="outer">CA Name/Key</TH>
<TD class="text">' || t_text || '</TD>
</TR>
<TR>
<TH class="outer">Certificates</TH>
<TD class="outer">
<TABLE class="options" style="margin-left:0px">
<TR>
';
IF t_showMozillaDisclosure THEN
t_output := t_output ||
' <TH style="white-space:nowrap">Mozilla Disclosure<BR><SPAN class="small">(id-kp-serverAuth)</SPAN></TH>
';
END IF;
t_output := t_output ||
' <TH style="white-space:nowrap">crt.sh ID</TH>
<TH style="white-space:nowrap">Not Before</TH>
<TH style="white-space:nowrap">Not After</TH>
<TH>Issuer Name</TH>
</TR>
';
FOR l_record IN (
SELECT x509_issuerName(c.CERTIFICATE) ISSUER_NAME,
c.ID,
c.ISSUER_CA_ID,
c.CERTIFICATE,
x509_notBefore(c.CERTIFICATE) NOT_BEFORE,
x509_notAfter(c.CERTIFICATE) NOT_AFTER
FROM ca_certificate cac, certificate c
LEFT OUTER JOIN ca ON (c.ISSUER_CA_ID = ca.ID)
WHERE cac.CA_ID = t_caID
AND cac.CERTIFICATE_ID = c.ID
ORDER BY ISSUER_NAME, NOT_BEFORE
) LOOP
t_output := t_output ||
' <TR>
';
IF t_showMozillaDisclosure THEN
t_temp3 := '<FONT color=#';
SELECT ctp.*
INTO t_ctp
FROM ca_trust_purpose ctp
WHERE ctp.CA_ID = l_record.ISSUER_CA_ID
AND ctp.TRUST_CONTEXT_ID = 5
AND ctp.TRUST_PURPOSE_ID = 1;
IF NOT FOUND THEN
t_temp3 := t_temp3 || '888888>Not Trusted';
t_ctp.SHORTEST_CHAIN := NULL;
ELSIF NOT t_ctp.IS_TIME_VALID THEN
t_temp3 := t_temp3 || '888888>Expired';
ELSE
SELECT cc.MOZILLA_DISCLOSURE_STATUS
INTO t_temp2
FROM ccadb_certificate cc
WHERE cc.CERTIFICATE_ID = l_record.ID;
IF FOUND AND (t_temp2 LIKE 'Revoked%') THEN
t_temp3 := t_temp3 || 'CC0000>Revoked';
ELSIF is_technically_constrained(l_record.CERTIFICATE) THEN
t_temp3 := t_temp3 || '00CC00>Constrained';
ELSIF t_ctp.ALL_CHAINS_REVOKED_IN_SALESFORCE OR t_ctp.ALL_CHAINS_REVOKED_VIA_ONECRL THEN
t_temp3 := t_temp3 || 'CC0000>All Paths Revoked';
ELSIF t_ctp.ALL_CHAINS_TECHNICALLY_CONSTRAINED THEN
t_temp3 := t_temp3 || '00CC00>All Paths Constrained';
ELSE
t_temp3 := t_temp3 || '00CC00>Valid';
END IF;
END IF;
IF t_ctp.SHORTEST_CHAIN IS NOT NULL THEN
t_temp3 := t_temp3 || ' <SPAN style="vertical-align:super;font-size:70%;color:#33A8FF">' || (t_ctp.SHORTEST_CHAIN + 1)::text || '</SPAN>';
END IF;
t_output := t_output ||
' <TD style="white-space:nowrap">' || t_temp3 || '</FONT></TD>
';
END IF;
t_output := t_output ||
' <TD><A href="?id=' || l_record.ID::text || t_temp || '">' || l_record.ID::text || '</A></TD>
<TD style="white-space:nowrap">' || to_char(l_record.NOT_BEFORE, 'YYYY-MM-DD') || '</TD>
<TD style="white-space:nowrap">' || to_char(l_record.NOT_AFTER, 'YYYY-MM-DD') || '</TD>
<TD><A href="?caid=' || l_record.ISSUER_CA_ID::text || t_temp || '">' || html_escape(l_record.ISSUER_NAME) || '</A></TD>
</TR>
';
END LOOP;
t_output := t_output ||
'</TABLE>
</TD>
</TR>
<TR><TD colspan=2> </TD></TR>
';
t_showCABLint := (',' || coalesce(get_parameter('opt', paramNames, paramValues), '') || ',') LIKE '%,cablint,%';
IF t_showCABLint THEN
t_output := t_output ||
' <TR>
<TH class="outer">CA/B Forum lint</TH>
<TD class="outer">
<TABLE class="options">
<TR><TH colspan=3>For Issued Certificates with notBefore >= ' || to_char(t_minNotBefore, 'YYYY-MM-DD') || ':</TH><TR>
<TR>
<TH>Issue</TH>
<TH># Affected Certs</TH>
</TR>
';
FOR l_record IN (
SELECT sum(ls.NO_OF_CERTS) NUM_CERTS, li.ID, li.SEVERITY, li.ISSUE_TEXT,
CASE li.SEVERITY
WHEN 'F' THEN 1
WHEN 'E' THEN 2
WHEN 'W' THEN 3
ELSE 4
END ISSUE_TYPE,
CASE li.SEVERITY
WHEN 'F' THEN '<SPAN class="fatal"> FATAL:'
WHEN 'E' THEN '<SPAN class="error"> ERROR:'
WHEN 'W' THEN '<SPAN class="warning"> WARNING:'
ELSE '<SPAN> ' || li.SEVERITY || ':'
END ISSUE_HEADING
FROM lint_summary ls, lint_issue li
WHERE ls.NOT_BEFORE_DATE >= t_minNotBefore
AND ls.ISSUER_CA_ID = t_value::integer
AND ls.LINT_ISSUE_ID = li.ID
AND li.LINTER = 'cablint'
GROUP BY li.ID, li.SEVERITY, li.ISSUE_TEXT
ORDER BY ISSUE_TYPE, NUM_CERTS DESC
) LOOP
t_output := t_output ||
' <TR>
<TD class="text">' || l_record.ISSUE_HEADING || ' ' || l_record.ISSUE_TEXT || ' </SPAN></TD>
<TD><A href="?cablint=' || l_record.ID::text || '&iCAID=' || t_caID::text || t_minNotBeforeString || '">' || l_record.NUM_CERTS::text || '</A></TD>
</TR>
';
END LOOP;
t_output := t_output ||
' </TABLE>
</TD>
</TR>
';
END IF;
t_showX509Lint := (',' || coalesce(get_parameter('opt', paramNames, paramValues), '') || ',') LIKE '%,x509lint,%';
IF t_showX509Lint THEN
t_output := t_output ||
' <TR>
<TH class="outer">X.509 lint</TH>
<TD class="outer">
<TABLE class="options">
<TR><TH colspan=3>For Issued Certificates with notBefore >= ' || to_char(t_minNotBefore, 'YYYY-MM-DD') || ':</TH><TR>
<TR>
<TH>Issue</TH>
<TH># Affected Certs</TH>
</TR>
';
FOR l_record IN (
SELECT sum(ls.NO_OF_CERTS) NUM_CERTS, li.ID, li.SEVERITY, li.ISSUE_TEXT,
CASE li.SEVERITY
WHEN 'F' THEN 1
WHEN 'E' THEN 2
WHEN 'W' THEN 3
ELSE 4
END ISSUE_TYPE,
CASE li.SEVERITY
WHEN 'F' THEN '<SPAN class="fatal"> FATAL:'
WHEN 'E' THEN '<SPAN class="error"> ERROR:'
WHEN 'W' THEN '<SPAN class="warning"> WARNING:'
ELSE '<SPAN> ' || li.SEVERITY || ':'
END ISSUE_HEADING
FROM lint_summary ls, lint_issue li
WHERE ls.NOT_BEFORE_DATE >= t_minNotBefore
AND ls.ISSUER_CA_ID = t_value::integer
AND ls.LINT_ISSUE_ID = li.ID
AND li.LINTER = 'x509lint'
GROUP BY li.ID, li.SEVERITY, li.ISSUE_TEXT
ORDER BY ISSUE_TYPE, NUM_CERTS DESC
) LOOP
t_output := t_output ||
' <TR>
<TD class="text">' || l_record.ISSUE_HEADING || ' ' || l_record.ISSUE_TEXT || ' </SPAN></TD>
<TD><A href="?x509lint=' || l_record.ID::text || '&iCAID=' || t_caID::text || t_minNotBeforeString || '">' || l_record.NUM_CERTS::text || '</A></TD>
</TR>
';
END LOOP;
t_output := t_output ||
' </TABLE>
</TD>
</TR>
';
END IF;
t_showZLint := (',' || coalesce(get_parameter('opt', paramNames, paramValues), '') || ',') LIKE '%,zlint,%';
IF t_showZLint THEN
t_output := t_output ||
' <TR>
<TH class="outer">ZLint</TH>
<TD class="outer">
<TABLE class="options">
<TR><TH colspan=3>For Issued Certificates with notBefore >= ' || to_char(t_minNotBefore, 'YYYY-MM-DD') || ':</TH><TR>
<TR>
<TH>Issue</TH>
<TH># Affected Certs</TH>
</TR>
';
FOR l_record IN (
SELECT sum(ls.NO_OF_CERTS) NUM_CERTS, li.ID, li.SEVERITY, li.ISSUE_TEXT,
CASE li.SEVERITY
WHEN 'F' THEN 1
WHEN 'E' THEN 2
WHEN 'W' THEN 3
ELSE 4
END ISSUE_TYPE,
CASE li.SEVERITY
WHEN 'F' THEN '<SPAN class="fatal"> FATAL:'
WHEN 'E' THEN '<SPAN class="error"> ERROR:'
WHEN 'W' THEN '<SPAN class="warning"> WARNING:'
ELSE '<SPAN> ' || li.SEVERITY || ':'
END ISSUE_HEADING
FROM lint_summary ls, lint_issue li
WHERE ls.NOT_BEFORE_DATE >= t_minNotBefore
AND ls.ISSUER_CA_ID = t_value::integer
AND ls.LINT_ISSUE_ID = li.ID
AND li.LINTER = 'zlint'
GROUP BY li.ID, li.SEVERITY, li.ISSUE_TEXT
ORDER BY ISSUE_TYPE, NUM_CERTS DESC
) LOOP
t_output := t_output ||
' <TR>
<TD class="text">' || l_record.ISSUE_HEADING || ' ' || l_record.ISSUE_TEXT || ' </SPAN></TD>
<TD><A href="?zlint=' || l_record.ID::text || '&iCAID=' || t_caID::text || t_minNotBeforeString || '">' || l_record.NUM_CERTS::text || '</A></TD>
</TR>
';
END LOOP;
t_output := t_output ||
' </TABLE>
</TD>
</TR>
';
END IF;
t_output := t_output ||
' <TR>
<TH class="outer">Issued Certificates</TH>
<TD class="outer">
<SCRIPT type="text/javascript">
function identitySearch(
type,
value
)
{
if ((!type) || (!value))
return;
var t_url;
if (document.search_form.searchCensys.checked) {
t_url = "//www.censys.io/certificates?q="
+ encodeURIComponent("parsed.issuer_dn=\"' || replace(t_caName, '"', '') || '\"");
var t_field = "";
if (value != "%") {
if (type == "Identity") {
t_url += " AND (parsed.subject_dn:" + encodeURIComponent("\"" + value + "\"")
+ " OR parsed.extensions.subject_alt_name.dns_names:" + encodeURIComponent("\"" + value + "\"")
+ " OR parsed.extensions.subject_alt_name.email_addresses:" + encodeURIComponent("\"" + value + "\"")
+ " OR parsed.extensions.subject_alt_name.ip_addresses:" + encodeURIComponent("\"" + value + "\"")
+ ")";
}
else if (type == "CN")
t_field = "parsed.subject.common_name";
else if (type == "E") {
alert("Sorry, Censys doesn''t support ''emailAddress (Subject)'' searches");
return false;
}
else if (type == "OU")
t_field = "parsed.subject.organizational_unit";
else if (type == "O")
t_field = "parsed.subject.organization";
else if (type == "dNSName")
t_field = "parsed.extensions.subject_alt_name.dns_names";
else if (type == "rfc822Name")
t_field = "parsed.extensions.subject_alt_name.email_addresses";
else if (type == "iPAddress")
t_field = "parsed.extensions.subject_alt_name.ip_addresses";
}
if (t_field != "")
t_url += " AND " + t_field + ":" + encodeURIComponent("\"" + value + "\"");
}
else {
t_url = "?" + encodeURIComponent(type) + "=" + encodeURIComponent(value);
if (document.search_form.caID.value != "")
t_url += "&iCAID=" + document.search_form.caID.value;
if (document.search_form.excludeExpired.checked)
t_url += "&exclude=expired";
}
window.location = t_url;
}
</SCRIPT>
<FORM name="search_form" method="GET" onSubmit="return false">
<INPUT type="hidden" name="caID" value="' || t_caID::text || '">
<TABLE class="options" style="margin-left:0px">
<TR>
<TD class="options">
<SPAN class="text">Select search type:</SPAN>
<BR><SELECT name="idtype" size="8">
<OPTION value="Identity" selected>IDENTITY</OPTION>
<OPTION value="CN"> commonName (Subject)</OPTION>
<OPTION value="E"> emailAddress (Subject)</OPTION>
<OPTION value="OU"> organizationalUnitName (Subject)</OPTION>
<OPTION value="O"> organizationName (Subject)</OPTION>
<OPTION value="dNSName"> dNSName (SAN)</OPTION>
<OPTION value="rfc822Name"> rfc822Name (SAN)</OPTION>
<OPTION value="iPAddress"> iPAddress (SAN)</OPTION>
</SELECT>
</TD>
<TD class="options" style="padding-left:20px;vertical-align:top">
<SPAN class="text">Enter search term:</SPAN><BR><SPAN class="small">(% = wildcard)</SPAN>
<BR><BR>
<INPUT type="text" name="idvalue" class="input" size="25" style="margin-top:2px">
<BR><BR><BR>
<INPUT type="submit" class="button" value="Search"
onClick="identitySearch(document.search_form.idtype.value,document.search_form.idvalue.value)">
</TD>
<TD class="options" style="padding-left:20px;vertical-align:top">
<SPAN class="text">Search options:</SPAN>
<BR><BR><DIV style="border:1px solid #AAAAAA;margin-bottom:8px;padding:5px 0px;text-align:left">
<INPUT type="checkbox" name="excludeExpired"';
IF t_excludeExpired IS NOT NULL THEN
t_output := t_output || ' checked';
END IF;
t_output := t_output || '> Exclude expired certificates?
<BR><INPUT type="checkbox" name="searchCensys"';
IF coalesce(t_searchProvider, '') = '&search=censys' THEN
t_output := t_output || ' checked';
END IF;
t_output := t_output || '> Search on <SPAN style="vertical-align:-30%"><IMG src="/censys.png"></SPAN>?
</DIV>
</TD>
</TR>
</TABLE>
</FORM>
<SCRIPT type="text/javascript">
document.search_form.idvalue.focus();
</SCRIPT>
</TD>
</TR>
<TR><TD colspan=2> </TD></TR>
<TR>
<TH class="outer">Trust</TH>
<TD class="outer">
<TABLE class="options" style="margin-left:0px">
<TR>
<TH rowspan="2" style="vertical-align:middle">Purpose</TH>
';
t_text := '';
t_count := 0;
FOR l_record IN (
SELECT *
FROM trust_context tc
ORDER BY tc.DISPLAY_ORDER
) LOOP
t_text := t_text ||
' <TH><A href="' || l_record.URL || '" target="_blank">' || l_record.CTX || '</A>';
IF l_record.VERSION IS NOT NULL THEN
t_text := t_text || '<BR>';
IF l_record.VERSION_URL IS NOT NULL THEN
t_text := t_text || '<A href="' || l_record.VERSION_URL || '" target="_blank">';
END IF;
t_text := t_text || '<SPAN class="small">(' || l_record.VERSION || ')</SPAN>';
IF l_record.VERSION_URL IS NOT NULL THEN
t_text := t_text || '</A>';
END IF;
END IF;
t_text := t_text || '</TH>
';
t_count := t_count + 1;
END LOOP;
t_output := t_output ||
' <TH colspan="' || t_count::text || '">Context <SPAN class="small">(Version)</SPAN> <SPAN style="vertical-align:super;font-size:70%;color:#33A8FF">Shortest Path</SPAN></TH>
</TR>
<TR>
';
t_purposeOID := '';
FOR l_record IN (
SELECT trustsrc.TRUST_CONTEXT_ID,
trustsrc.PURPOSE,
trustsrc.PURPOSE_OID,
(ctp.CA_ID IS NOT NULL) HAS_TRUST,
(ap.PURPOSE IS NOT NULL) IS_APPLICABLE,
ctp.IS_TIME_VALID,
ctp.SHORTEST_CHAIN,
ctp.ALL_CHAINS_REVOKED_VIA_ONECRL,
ctp.ALL_CHAINS_REVOKED_VIA_CRLSET,
ctp.ALL_CHAINS_REVOKED_VIA_DISALLOWEDSTL,
ctp.ALL_CHAINS_TECHNICALLY_CONSTRAINED
FROM (SELECT tc.DISPLAY_ORDER CTX_DISPLAY_ORDER,
tc.ID TRUST_CONTEXT_ID,
tp.ID TRUST_PURPOSE_ID,
tp.DISPLAY_ORDER,
tp.PURPOSE,
tp.PURPOSE_OID
FROM trust_purpose tp, trust_context tc
WHERE tp.PURPOSE != 'EV Server Authentication'
UNION
SELECT tc.DISPLAY_ORDER CTX_DISPLAY_ORDER,
tc.ID TRUST_CONTEXT_ID,
tp.ID TRUST_PURPOSE_ID,
tp.DISPLAY_ORDER,
tp.PURPOSE,
tp.PURPOSE_OID
FROM ca_trust_purpose ctp_ev, trust_purpose tp, trust_context tc
WHERE ctp_ev.CA_ID = t_caID
AND ctp_ev.TRUST_PURPOSE_ID = tp.ID
AND tp.PURPOSE = 'EV Server Authentication'
GROUP BY tc.CTX, tc.ID, tp.ID, tp.DISPLAY_ORDER, tp.PURPOSE, tp.PURPOSE_OID
) trustsrc
LEFT OUTER JOIN ca_trust_purpose ctp ON (
ctp.CA_ID = t_caID
AND trustsrc.TRUST_CONTEXT_ID = ctp.TRUST_CONTEXT_ID
AND trustsrc.TRUST_PURPOSE_ID = ctp.TRUST_PURPOSE_ID
)
LEFT OUTER JOIN applicable_purpose ap ON (
trustsrc.TRUST_CONTEXT_ID = ap.TRUST_CONTEXT_ID
AND trustsrc.PURPOSE = ap.PURPOSE
)
ORDER BY trustsrc.DISPLAY_ORDER, trustsrc.PURPOSE_OID, trustsrc.CTX_DISPLAY_ORDER
) LOOP
IF (t_purposeOID != l_record.PURPOSE_OID) OR (t_purpose != l_record.PURPOSE) THEN
t_purposeOID := l_record.PURPOSE_OID;
t_purpose := l_record.PURPOSE;
t_text := t_text ||
' </TR>
<TR>
<TD>' || l_record.PURPOSE;
IF l_record.PURPOSE = 'EV Server Authentication' THEN
t_text := t_text || ' (' || l_record.PURPOSE_OID || ')';
END IF;
t_text := t_text || '</TD>
';
END IF;
IF (l_record.TRUST_CONTEXT_ID = 6) AND (l_record.IS_APPLICABLE) THEN
SELECT true
INTO l_record.ALL_CHAINS_REVOKED_VIA_CRLSET
FROM ca_trust_purpose ctp
WHERE ctp.CA_ID = t_caID
AND ctp.ALL_CHAINS_REVOKED_VIA_CRLSET
LIMIT 1;
END IF;
t_text := t_text ||
' <TD style="text-align:center"><FONT color=#';
IF NOT l_record.IS_APPLICABLE THEN
t_text := t_text || 'CCCCCC>n/a';
l_record.SHORTEST_CHAIN := NULL;
ELSIF l_record.ALL_CHAINS_REVOKED_VIA_ONECRL AND (l_record.TRUST_CONTEXT_ID = 5) THEN
t_text := t_text || 'CC0000 style="font-weight:bold">Revoked</FONT><BR><FONT style="font-size:8pt;color:#CC0000">via OneCRL';
ELSIF l_record.ALL_CHAINS_REVOKED_VIA_CRLSET AND (l_record.TRUST_CONTEXT_ID = 6) THEN
t_text := t_text || 'CC0000 style="font-weight:bold">Revoked</FONT> <FONT style="font-size:8pt;color:#CC0000">via<BR>CRLSet / Blacklist';
ELSIF l_record.ALL_CHAINS_REVOKED_VIA_DISALLOWEDSTL AND (l_record.TRUST_CONTEXT_ID = 1) THEN
t_text := t_text || 'CC0000 style="font-weight:bold">Revoked</FONT> <FONT style="font-size:8pt;color:#CC0000">via<BR>disallowedcert.stl';
ELSIF (l_record.PURPOSE = 'Server Authentication') AND (l_record.TRUST_CONTEXT_ID = 6) THEN
t_text := t_text || '888888>Defer to OS';
ELSIF NOT l_record.HAS_TRUST THEN
t_text := t_text || '888888>No';
l_record.SHORTEST_CHAIN := NULL;
ELSIF NOT l_record.IS_TIME_VALID THEN
t_text := t_text || '888888>Expired';
ELSIF l_record.ALL_CHAINS_TECHNICALLY_CONSTRAINED THEN
t_text := t_text || '00CC00>Constrained';
ELSE
t_text := t_text || '00CC00>Valid';
END IF;
IF l_record.SHORTEST_CHAIN IS NOT NULL THEN
t_text := t_text || ' <SPAN style="vertical-align:super;font-size:70%;color:#33A8FF">' || l_record.SHORTEST_CHAIN || '</SPAN>';
END IF;
t_text := t_text || '</FONT></TD>
';
END LOOP;
t_output := t_output || t_text ||
' </TR>
</TABLE>
</TD>
</TR>
<TR><TD colspan=2> </TD></TR>
<TR>
<TH class="outer">Parent CAs</TH>
<TD class="outer">
';
t_text := NULL;
FOR l_record IN (
SELECT x509_issuerName(c.CERTIFICATE) ISSUER_NAME,
c.ISSUER_CA_ID
FROM ca_certificate cac, certificate c
LEFT OUTER JOIN ca ON (c.ISSUER_CA_ID = ca.ID)
WHERE cac.CA_ID = t_caID
AND cac.CERTIFICATE_ID = c.ID
AND c.ISSUER_CA_ID != t_caID
GROUP BY x509_issuerName(c.CERTIFICATE),
c.ISSUER_CA_ID
ORDER BY x509_issuerName(c.CERTIFICATE)
) LOOP
IF t_text IS NULL THEN
t_text := '
<TABLE class="options" style="margin-left:0px">
';
END IF;
t_text := t_text ||
' <TR>
<TD>';
IF l_record.ISSUER_CA_ID IS NULL THEN
t_text := t_text || html_escape(l_record.ISSUER_NAME);
ELSE
t_text := t_text || '<A href="?caid=' || l_record.ISSUER_CA_ID::text || t_temp || '">'
|| html_escape(l_record.ISSUER_NAME) || '</A>';
END IF;
t_text := t_text || '</TD>
</TR>
';
END LOOP;
IF t_text IS NOT NULL THEN
t_text := t_text ||
'</TABLE>
';
END IF;
t_output := t_output || coalesce(t_text, '<I>None found</I>') ||
' </TD>
</TR>
<TR>
<TH class="outer">Child CAs</TH>
<TD class="outer">
';
t_text := NULL;
FOR l_record IN (
SELECT x509_subjectName(c.CERTIFICATE) SUBJECT_NAME,
cac.CA_ID
FROM certificate c, ca_certificate cac
LEFT OUTER JOIN ca ON (cac.CA_ID = ca.ID)
WHERE x509_canIssueCerts(c.CERTIFICATE)
AND c.ISSUER_CA_ID = t_caID
AND c.ID = cac.CERTIFICATE_ID
AND cac.CA_ID != t_caID
GROUP BY x509_subjectName(c.CERTIFICATE),
cac.CA_ID
ORDER BY x509_subjectName(c.CERTIFICATE)
) LOOP
IF t_text IS NULL THEN
t_text := '
<TABLE class="options" style="margin-left:0px">
';
END IF;
t_text := t_text ||
' <TR>
<TD>';
IF l_record.CA_ID IS NULL THEN
t_text := t_text || html_escape(l_record.SUBJECT_NAME);
ELSE
t_text := t_text || '<A href="?caid=' || l_record.CA_ID::text || t_temp || '">'
|| html_escape(l_record.SUBJECT_NAME) || '</A>';
END IF;
t_text := t_text || '</TD>
</TR>
';
END LOOP;
IF t_text IS NOT NULL THEN
t_text := t_text ||
'</TABLE>
';
END IF;
t_output := t_output || coalesce(t_text, '<I>None found</I>') ||
' </TD>
</TR>
';
t_output := t_output ||
'</TABLE>
';
-- Search for (potentially) multiple CAs.
ELSE /* CA Name */
t_query := 'SELECT ca.ID, ca.NAME' || chr(10) ||
' FROM ca' || chr(10);
IF t_useReverseIndex THEN
t_query := t_query ||
' WHERE reverse(lower(ca.NAME)) LIKE reverse(lower($1))' || chr(10);
ELSE
t_query := t_query ||
' WHERE lower(ca.NAME) LIKE lower($1)' || chr(10);
END IF;
t_query := t_query ||
' ORDER BY ca.NAME';
FOR l_record IN EXECUTE t_query
USING t_value LOOP
IF t_text IS NULL THEN
t_text := '
<TABLE class="options" style="margin-left:0px">
';
END IF;
t_text := t_text ||
' <TR>
<TD>' || '<A href="?caid=' || l_record.ID::text || coalesce(t_excludeExpired, '') || '">'
|| html_escape(l_record.NAME) || '</A></TD>
</TR>
';
END LOOP;
IF t_text IS NOT NULL THEN
t_text := t_text ||
'</TABLE>
';
END IF;
t_output := t_output ||
'<TABLE>
<TR>
<TH class="outer">CAs</TH>
<TD class="outer">' || coalesce(t_text, '<I>None found</I>') || '</TD>
</TR>
</TABLE>
';
END IF;
ELSIF t_type IN (
'CT Entry ID',
'Serial Number',
'Subject Key Identifier',
'SHA-1(SubjectPublicKeyInfo)',
'SHA-256(SubjectPublicKeyInfo)',
'SHA-1(Subject)',
'Identity',
'Common Name',
'Email Address',
'Organizational Unit Name',
'Organization Name',
'Domain Name',
'Email Address (SAN)',
'IP Address',
'CA/B Forum lint',
'X.509 lint',
'ZLint',
'Lint'
) THEN
-- Determine whether to use a reverse index (if available).
IF position('%' IN t_value) != 0 THEN
t_matchType := 'LIKE';
t_useReverseIndex := (
position('%' IN t_value) < position('%' IN reverse(t_value))
);
END IF;
t_caID := get_parameter('icaid', paramNames, paramValues)::integer;
t_temp := coalesce(get_parameter('p', paramNames, paramValues), '');
IF t_temp = '' THEN
IF t_caID IS NOT NULL THEN
t_pageNo := 1;
END IF;
ELSIF lower(t_temp) = 'off' THEN
NULL;
ELSIF t_temp IS NOT NULL THEN
t_pageNo := t_temp::integer;
IF t_pageNo < 1 THEN
t_pageNo := 1;
END IF;
END IF;
t_resultsPerPage := coalesce(get_parameter('n', paramNames, paramValues)::integer, 100);
IF t_outputType = 'html' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">Identity Search</SPAN>
';
IF t_caID IS NULL THEN
t_temp := urlEncode(t_cmd) || '=' || urlEncode(t_value) || coalesce(t_excludeExpired, '')
|| coalesce(t_excludeCAsString, '') || t_minNotBeforeString;
t_output := t_output ||
' <SPAN style="position:absolute">
<A href="atom?' || t_temp || '"><IMG src="/feed-icon-28x28.png"></A>
<A style="font-size:8pt" href="?' || t_temp || '&dir=' || t_direction || '&sort=' || t_sort::text;
IF t_groupBy = 'none' THEN
t_output := t_output || '&group=icaid">Group';
ELSE
t_output := t_output || '&group=none">Ungroup';
END IF;
t_output := t_output || ' by Issuer</A>
</SPAN>
';
END IF;
t_output := t_output ||
'<BR><BR>
<TABLE>
<TR>
<TH class="outer">Criteria</TH>
<TD class="outer">' || html_escape(t_type)
|| ' ' || html_escape(t_matchType)
|| ' ''';
IF lower(t_type) LIKE '%lint' THEN
SELECT CASE li.SEVERITY
WHEN 'F' THEN '<SPAN class="fatal"> FATAL:'
WHEN 'E' THEN '<SPAN class="error"> ERROR:'
WHEN 'W' THEN '<SPAN class="warning"> WARNING:'
ELSE '<SPAN> ' || li.SEVERITY || ':'
END || ' ' || li.ISSUE_TEXT || ' </SPAN>'
INTO t_temp
FROM lint_issue li
WHERE li.ID = t_value::integer
AND li.LINTER = coalesce(t_linter, li.LINTER);
t_output := t_output || t_temp;
ELSE
t_output := t_output || html_escape(t_value);
END IF;
t_output := t_output || '''';
IF t_caID IS NOT NULL THEN
t_output := t_output || '; Issuer CA ID = ' || t_caID::text;
END IF;
IF t_excludeExpired IS NOT NULL THEN
t_output := t_output || '; Exclude expired certificates';
END IF;
t_output := t_output || '</TD>
</TR>
</TABLE>
<BR>
';
IF lower(t_type) LIKE '%lint' THEN
t_output := t_output ||
'For certificates with <B>notBefore >= ' || to_char(t_minNotBefore, 'YYYY-MM-DD') || '</B>:
<BR><BR>
';
t_opt := '&opt=' || t_linters;
ELSE
t_opt := '';
END IF;
END IF;
-- Search for (potentially) multiple certificates.
IF t_caID IS NOT NULL THEN
-- Show all of the certs for 1 identity issued by 1 CA.
t_query := 'SELECT c.ID, x509_subjectName(c.CERTIFICATE) SUBJECT_NAME,' || chr(10) ||
' x509_notBefore(c.CERTIFICATE) NOT_BEFORE,' || chr(10) ||
' x509_notAfter(c.CERTIFICATE) NOT_AFTER,' || chr(10) ||
' c.ISSUER_CA_ID' || chr(10) ||
' FROM certificate c' || chr(10);
IF t_type IN (
'Serial Number', 'Subject Key Identifier',
'SHA-1(SubjectPublicKeyInfo)', 'SHA-256(SubjectPublicKeyInfo)', 'SHA-1(Subject)'
) THEN
IF t_type = 'Serial Number' THEN
t_query := t_query ||
' WHERE x509_serialNumber(c.CERTIFICATE) = decode($2, ''hex'')' || chr(10);
ELSIF t_type = 'Subject Key Identifier' THEN
t_query := t_query ||
' WHERE x509_subjectKeyIdentifier(c.CERTIFICATE) = decode($2, ''hex'')' || chr(10);
ELSIF t_type = 'SHA-1(SubjectPublicKeyInfo)' THEN
t_query := t_query ||
' WHERE digest(x509_publickey(c.CERTIFICATE), ''sha1'') = decode($2, ''hex'')' || chr(10);
ELSIF t_type = 'SHA-256(SubjectPublicKeyInfo)' THEN
t_query := t_query ||
' WHERE digest(x509_publickey(c.CERTIFICATE), ''sha256'') = decode($2, ''hex'')' || chr(10);
ELSIF t_type = 'SHA-1(Subject)' THEN
t_query := t_query ||
' WHERE digest(x509_name(c.CERTIFICATE), ''sha1'') = decode($2, ''hex'')' || chr(10);
END IF;
t_query := t_query ||
' AND c.ISSUER_CA_ID = $1' || chr(10);
ELSIF (t_type = 'Identity') AND (t_value = '%') THEN
t_query := t_query ||
' WHERE c.ISSUER_CA_ID = $1' || chr(10);
ELSIF lower(t_type) LIKE '%lint' THEN
t_query := t_query ||
' , lint_cert_issue lci, lint_issue li' || chr(10) ||
' WHERE c.ISSUER_CA_ID = $1::integer' || chr(10) ||
' AND c.ID = lci.CERTIFICATE_ID' || chr(10) ||
' AND lci.ISSUER_CA_ID = $1::integer' || chr(10) ||
' AND lci.NOT_BEFORE_DATE >= $3' || chr(10) ||
' AND lci.LINT_ISSUE_ID = $2::integer' || chr(10) ||
' AND lci.LINT_ISSUE_ID = li.ID' || chr(10);
IF t_linter IS NOT NULL THEN
t_query := t_query ||
' AND li.LINTER = ''' || t_linter || '''' || chr(10);
END IF;
ELSE
t_query := t_query ||
' WHERE c.ID IN (' || chr(10) ||
' SELECT DISTINCT ci.CERTIFICATE_ID' || chr(10) ||
' FROM certificate_identity ci' || chr(10) ||
' WHERE ci.ISSUER_CA_ID = $1' || chr(10);
IF t_useReverseIndex THEN
t_query := t_query ||
' AND reverse(lower(ci.NAME_VALUE)) LIKE reverse(lower($2))' || chr(10);
ELSE
t_query := t_query ||
' AND lower(ci.NAME_VALUE) LIKE lower($2)' || chr(10);
END IF;
IF t_type != 'Identity' THEN
t_query := t_query || ' ' ||
' AND ci.NAME_TYPE = ' || quote_literal(t_nameType) || chr(10);
END IF;
t_query := t_query ||
' )' || chr(10);
END IF;
IF t_excludeExpired IS NOT NULL THEN
t_query := t_query ||
' AND x509_notAfter(c.CERTIFICATE) > statement_timestamp()' || chr(10);
END IF;
IF lower(t_type) LIKE '%lint' THEN
t_query := t_query ||
' GROUP BY c.ID, c.ISSUER_CA_ID, SUBJECT_NAME, NOT_BEFORE, NOT_AFTER' || chr(10);
END IF;
t_query := t_query ||
' ORDER BY NOT_BEFORE DESC';
IF t_pageNo IS NOT NULL THEN
t_query := t_query || chr(10) ||
' OFFSET ' || ((t_pageNo - 1) * t_resultsPerPage)::text || chr(10) ||
' LIMIT ' || t_resultsPerPage::text;
END IF;
t_text := '';
t_count := 0;
FOR l_record IN EXECUTE t_query
USING t_caID, t_value, t_minNotBefore LOOP
t_count := t_count + 1;
t_text := t_text ||
' <TR>
<TD style="text-align:center"><A href="?id=' || l_record.ID::text;
IF lower(t_type) LIKE '%lint' THEN
t_text := t_text || '&opt=' || t_linters;
END IF;
t_text := t_text || '">' || l_record.ID::text || '</A></TD>
<TD style="white-space:nowrap">' || to_char(l_record.NOT_BEFORE, 'YYYY-MM-DD') || '</TD>
<TD style="white-space:nowrap">' || to_char(l_record.NOT_AFTER, 'YYYY-MM-DD') || '</TD>
<TD>' || html_escape(l_record.SUBJECT_NAME) || '</TD>
</TR>
';
END LOOP;
IF t_pageNo IS NOT NULL THEN
IF (t_value = '%') AND (t_excludeExpired IS NULL) THEN
SELECT ca.NO_OF_CERTS_ISSUED
INTO t_count
FROM ca
WHERE ca.ID = t_caID;
ELSE
t_temp := 'SELECT count(*) FROM (' || chr(10) || substring(t_query from '^.* ORDER BY');
t_temp := substr(t_temp, 1, length(t_temp) - length(' ORDER BY')) || ') sub';
EXECUTE t_temp INTO t_count USING t_caID, t_value, t_minNotBefore;
END IF;
END IF;
SELECT ca.NAME
INTO t_temp
FROM ca
WHERE ca.ID = t_caID;
t_output := t_output ||
'<TABLE>
<TR>
<TH class="outer">Issuer Name</TH>
<TD class="outer"><A href="?caid=' || t_caID::text || coalesce(t_excludeExpired, '') || t_opt || '">'
|| coalesce(html_escape(t_temp), ' ') || '</A></TD>
</TR>
<TR>
<TH class="outer">Certificates<BR>(' || trim(to_char(t_count, '999G999G999G999G999')) || ')</TH>
<TD class="outer">';
IF t_text != '' THEN
t_output := t_output || '
<TABLE>
';
IF (t_pageNo IS NOT NULL) AND (t_count > t_resultsPerPage) THEN
t_output := t_output ||
' <TR><TD colspan="3" style="text-align:center;padding:4px">';
IF t_pageNo > 1 THEN
t_output := t_output || '<A style="font-size:8pt" href="?' ||
urlEncode(t_cmd) || '=' || urlEncode(t_value) ||
'&iCAID=' || t_caID::text || coalesce(t_excludeExpired, '') ||
'&p=' || (t_pageNo - 1)::text ||
'&n=' || t_resultsPerPage::text || '">Previous</A> ';
END IF;
t_output := t_output || '<B>' ||
(((t_pageNo - 1) * t_resultsPerPage) + 1)::integer || '</B> to <B>' ||
least(t_pageNo * t_resultsPerPage, t_count)::integer || '</B>';
IF (t_pageNo * t_resultsPerPage) < t_count THEN
t_output := t_output || ' <A style="font-size:8pt" href="?' ||
urlEncode(t_cmd) || '=' || urlEncode(t_value) ||
'&iCAID=' || t_caID::text || coalesce(t_excludeExpired, '') ||
'&p=' || (t_pageNo + 1)::text ||
'&n=' || t_resultsPerPage::text || '">Next</A>';
END IF;
t_output := t_output || '</TD></TR>
';
END IF;
t_output := t_output ||
' <TR>
<TH style="white-space:nowrap">crt.sh ID</TH>
<TH style="white-space:nowrap">Not Before</TH>
<TH style="white-space:nowrap">Not After</TH>
<TH>Subject Name</TH>
</TR>
' || t_text ||
'</TABLE>
';
ELSE
t_output := t_output ||
'<I>None found</I>';
END IF;
t_output := t_output || '</TD>
</TR>
</TABLE>
';
ELSE
IF trim(t_value, '%') = '' THEN
RAISE no_data_found
USING MESSAGE = '</SPAN>
<BR><BR>Value not permitted: ''%''';
END IF;
t_select := 'SELECT __issuer_ca_id_table__.ISSUER_CA_ID,' || chr(10) ||
' ca.NAME ISSUER_NAME,' || chr(10) ||
' __name_value__ NAME_VALUE,' || chr(10) ||
' min(__cert_id_field__) MIN_CERT_ID,' || chr(10);
t_from := ' FROM ca';
t_where := ' WHERE __issuer_ca_id_table__.ISSUER_CA_ID = ca.ID';
IF coalesce(t_groupBy, '') = 'none' THEN
t_select := t_select ||
' min(ctle.ENTRY_TIMESTAMP) MIN_ENTRY_TIMESTAMP,' || chr(10) ||
' x509_notBefore(c.CERTIFICATE) NOT_BEFORE';
t_from := t_from || ',' || chr(10) ||
' ct_log_entry ctle';
t_where := t_where || chr(10) ||
' AND __ctle_cert_id__ = ctle.CERTIFICATE_ID';
t_joinToCTLogEntry := 'c.ID';
t_query := ' GROUP BY c.ID, __issuer_ca_id_table__.ISSUER_CA_ID, ISSUER_NAME, NAME_VALUE' || chr(10) ||
' ORDER BY ';
IF t_sort = 0 THEN
t_query := t_query || 'MIN_CERT_ID ' || t_orderBy;
ELSIF t_sort = 1 THEN
t_query := t_query || 'MIN_ENTRY_TIMESTAMP ' || t_orderBy || ', NAME_VALUE, ISSUER_NAME';
ELSIF t_sort = 2 THEN
t_query := t_query || 'NOT_BEFORE ' || t_orderBy || ', NAME_VALUE, ISSUER_NAME';
ELSE
t_query := t_query || 'ISSUER_NAME ' || t_orderBy || ', NOT_BEFORE ' || t_orderBy || ', NAME_VALUE';
END IF;
ELSE
-- Group certs for the same identity issued by the same CA.
t_select := t_select ||
' count(DISTINCT __cert_id_field__) NUM_CERTS';
t_query := ' GROUP BY __issuer_ca_id_table__.ISSUER_CA_ID, ISSUER_NAME, NAME_VALUE' || chr(10) ||
' ORDER BY ';
IF t_sort = 3 THEN
t_query := t_query || 'ISSUER_NAME ' || t_orderBy || ', NAME_VALUE, NUM_CERTS';
ELSE
t_query := t_query || 'NUM_CERTS ' || t_orderBy || ', NAME_VALUE, ISSUER_NAME';
END IF;
END IF;
IF t_type = 'CT Entry ID' THEN
IF coalesce(t_groupBy, '') != 'none' THEN
t_from := t_from || ',' || chr(10) ||
' ct_log_entry ctle';
END IF;
t_from := t_from || ',' || chr(10) ||
' ct_log ctl';
t_issuerCAID_table := 'c';
t_nameValue := 'ctl.NAME';
t_certID_field := 'c.ID';
t_joinToCertificate_table := 'ctle';
t_where := t_where || chr(10) ||
' AND ctle.ENTRY_ID = $1::integer' || chr(10) ||
' AND ctle.CT_LOG_ID = ctl.ID';
ELSIF t_type = 'Serial Number' THEN
t_from := t_from || ',' || chr(10) ||
' certificate c';
t_issuerCAID_table := 'c';
t_nameValue := 'encode(x509_serialNumber(c.CERTIFICATE), ''hex'')';
t_certID_field := 'c.ID';
t_where := t_where || chr(10) ||
' AND x509_serialNumber(c.CERTIFICATE) = decode($1, ''hex'')';
ELSIF t_type = 'Subject Key Identifier' THEN
t_from := t_from || ',' || chr(10) ||
' certificate c';
t_issuerCAID_table := 'c';
t_nameValue := 'encode(x509_subjectKeyIdentifier(c.CERTIFICATE), ''hex'')';
t_certID_field := 'c.ID';
t_where := t_where || chr(10) ||
' AND x509_subjectKeyIdentifier(c.CERTIFICATE) = decode($1, ''hex'')';
ELSIF t_type = 'SHA-1(SubjectPublicKeyInfo)' THEN
t_from := t_from || ',' || chr(10) ||
' certificate c';
t_issuerCAID_table := 'c';
t_nameValue := 'encode(digest(x509_publickey(c.CERTIFICATE), ''sha1''), ''hex'')';
t_certID_field := 'c.ID';
t_where := t_where || chr(10) ||
' AND digest(x509_publickey(c.CERTIFICATE), ''sha1'') = decode($1, ''hex'')';
ELSIF t_type = 'SHA-256(SubjectPublicKeyInfo)' THEN
t_from := t_from || ',' || chr(10) ||
' certificate c';
t_issuerCAID_table := 'c';
t_nameValue := 'encode(digest(x509_publickey(c.CERTIFICATE), ''sha256''), ''hex'')';
t_certID_field := 'c.ID';
t_where := t_where || chr(10) ||
' AND digest(x509_publickey(c.CERTIFICATE), ''sha256'') = decode($1, ''hex'')';
ELSIF t_type = 'SHA-1(Subject)' THEN
t_from := t_from || ',' || chr(10) ||
' certificate c';
t_issuerCAID_table := 'c';
t_nameValue := 'encode(digest(x509_name(c.CERTIFICATE), ''sha1''), ''hex'')';
t_certID_field := 'c.ID';
t_where := t_where || chr(10) ||
' AND digest(x509_name(c.CERTIFICATE), ''sha1'') = decode($1, ''hex'')';
ELSIF lower(t_type) LIKE '%lint' THEN
t_from := t_from || ',' || chr(10) ||
' lint_issue li,' || chr(10) ||
' lint_cert_issue lci';
t_issuerCAID_table := 'lci';
t_nameValue := 'lci.LINT_ISSUE_ID::text';
IF coalesce(t_groupBy, '') = 'none' THEN
t_certID_field := 'c.ID';
t_joinToCertificate_table := 'lci';
ELSE
t_certID_field := 'lci.CERTIFICATE_ID';
IF t_excludeExpired IS NOT NULL THEN
t_joinToCertificate_table := 'lci';
END IF;
END IF;
t_where := t_where || chr(10) ||
' AND lci.LINT_ISSUE_ID = $1::integer' || chr(10) ||
' AND lci.NOT_BEFORE_DATE >= $2' || chr(10) ||
' AND lci.LINT_ISSUE_ID = li.ID' || chr(10) ||
' AND ca.LINTING_APPLIES';
IF t_linter IS NOT NULL THEN
t_where := t_where || chr(10) ||
' AND li.LINTER = ''' || t_linter || '''';
END IF;
ELSE
t_from := t_from || ',' || chr(10) ||
' certificate_identity ci';
t_issuerCAID_table := 'ci';
t_nameValue := 'ci.NAME_VALUE';
IF coalesce(t_groupBy, '') = 'none' THEN
t_certID_field := 'c.ID';
t_joinToCertificate_table := 'ci';
ELSE
t_certID_field := 'ci.CERTIFICATE_ID';
IF t_excludeExpired IS NOT NULL THEN
t_joinToCertificate_table := 'ci';
END IF;
END IF;
IF t_useReverseIndex THEN
t_where := t_where || chr(10) ||
' AND reverse(lower(ci.NAME_VALUE)) LIKE reverse(lower($1))';
ELSE
t_where := t_where || chr(10) ||
' AND lower(ci.NAME_VALUE) LIKE lower($1)';
END IF;
IF t_type != 'Identity' THEN
t_where := t_where || chr(10) ||
' AND ci.NAME_TYPE = ' || quote_literal(t_nameType);
END IF;
END IF;
IF t_joinToCertificate_table IS NOT NULL THEN
t_from := t_from || ',' || chr(10) ||
' certificate c';
t_where := t_where || chr(10) ||
' AND ' || t_joinToCertificate_table || '.CERTIFICATE_ID = c.ID';
END IF;
IF t_excludeExpired IS NOT NULL THEN
t_where := t_where || chr(10) ||
' AND x509_notAfter(c.CERTIFICATE) > statement_timestamp()';
END IF;
IF t_excludeCAsString IS NOT NULL THEN
t_where := t_where || chr(10) ||
' AND ' || t_issuerCAID_table || '.ISSUER_CA_ID NOT IN (' || array_to_string(t_excludeCAs, ',') || ')';
END IF;
t_query := t_select || chr(10)
|| t_from || chr(10)
|| t_where || chr(10)
|| t_query;
t_query := replace(t_query, '__issuer_ca_id_table__', t_issuerCAID_table);
t_query := replace(t_query, '__name_value__', t_nameValue);
t_query := replace(t_query, '__cert_id_field__', t_certID_field);
IF t_joinToCTLogEntry IS NOT NULL THEN
t_query := replace(t_query, '__ctle_cert_id__', t_joinToCTLogEntry);
END IF;
t_showIdentity := (position('%' IN t_value) > 0) OR (t_type = 'CT Entry ID');
t_text := '';
t_summary := '';
FOR l_record IN EXECUTE t_query
USING t_value, t_minNotBefore LOOP
t_temp2 := '';
IF t_outputType = 'atom' THEN
IF coalesce(t_certificateID, -l_record.MIN_CERT_ID) != l_record.MIN_CERT_ID THEN
IF lower(t_type) NOT LIKE '%lint' THEN
t_text := replace(t_text, '__entry_summary__', t_summary);
END IF;
t_summary := l_record.NAME_VALUE;
t_certificateID := l_record.MIN_CERT_ID;
ELSE
t_summary := t_summary || ' &nbsp; ' || l_record.NAME_VALUE;
CONTINUE;
END IF;
SELECT to_char(x509_notAfter(c.CERTIFICATE), 'YYYY-MM-DD')
|| '; Serial number ' || encode(x509_serialNumber(c.CERTIFICATE), 'hex'),
c.CERTIFICATE
INTO t_temp,
t_certificate
FROM certificate c
WHERE c.ID = l_record.MIN_CERT_ID;
t_b64Certificate := replace(encode(t_certificate, 'base64'), chr(10), '');
t_feedUpdated := greatest(t_feedUpdated, l_record.MIN_ENTRY_TIMESTAMP);
t_temp2 := t_temp2 ||
' <entry>
<id>https://crt.sh/?id=' || l_record.MIN_CERT_ID || '#' || t_cmd || ';' || t_value || '</id>
<link rel="alternate" type="text/html" href="https://crt.sh/?id=' || l_record.MIN_CERT_ID || '"/>
<summary type="html">__entry_summary__<br><br><div style="font:8pt monospace">-----BEGIN CERTIFICATE-----';
WHILE length(t_b64Certificate) > 0 LOOP
t_temp2 := t_temp2 || '<br>' || substring(
t_b64Certificate from 1 for 64
);
t_b64Certificate := substring(t_b64Certificate from 65);
END LOOP;
t_temp2 := t_temp2 ||
'<br>-----END CERTIFICATE-----</div>
</summary>
<title>[';
IF x509_print(t_certificate) LIKE '%CT Precertificate Poison%' THEN
t_temp2 := t_temp2 || 'Precertificate';
ELSE
t_temp2 := t_temp2 || 'Certificate';
END IF;
t_temp2 := t_temp2 ||
'] Issued by ' || get_ca_name_attribute(l_record.ISSUER_CA_ID)
|| '; Valid from ' || to_char(l_record.NOT_BEFORE, 'YYYY-MM-DD') || ' to '
|| t_temp || '</title>
<published>' || to_char(l_record.NOT_BEFORE, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') || '</published>
<updated>' || to_char(l_record.MIN_ENTRY_TIMESTAMP, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') || '</updated>
</entry>
';
ELSIF t_outputType = 'json' THEN
t_output := t_output || row_to_json(l_record, FALSE);
ELSIF t_outputType = 'html' THEN
t_temp2 := t_temp2 ||
' <TR>
<TD style="text-align:center">';
IF coalesce(t_groupBy, '') = 'none' THEN
t_temp2 := t_temp2 || '<A href="?id=' || l_record.MIN_CERT_ID::text || t_opt || '">' || l_record.MIN_CERT_ID::text || '</A></TD>
<TD style="text-align:center">' || to_char(l_record.MIN_ENTRY_TIMESTAMP, 'YYYY-MM-DD') || '</TD>
<TD style="text-align:center">' || to_char(l_record.NOT_BEFORE, 'YYYY-MM-DD');
ELSIF (l_record.NUM_CERTS = 1)
AND (l_record.MIN_CERT_ID IS NOT NULL) THEN
t_temp2 := t_temp2 || '<A href="?id=' || l_record.MIN_CERT_ID::text || t_opt || '">'
|| l_record.NUM_CERTS::text || '</A>';
ELSIF (l_record.ISSUER_CA_ID IS NOT NULL)
AND (l_record.MIN_CERT_ID IS NOT NULL) THEN
t_temp2 := t_temp2 || '<A href="?' || urlEncode(t_cmd) || '=' || urlEncode(l_record.NAME_VALUE)
|| '&iCAID=' || l_record.ISSUER_CA_ID::text || t_minNotBeforeString
|| coalesce(t_excludeExpired, '') || t_opt || '">'
|| l_record.NUM_CERTS::text || '</A>';
ELSE
t_temp2 := t_temp2 || l_record.NUM_CERTS::text;
END IF;
t_temp2 := t_temp2 || '</TD>
<TD>';
IF t_showIdentity THEN
t_temp2 := t_temp2 || html_escape(l_record.NAME_VALUE) || '</TD>
<TD>';
END IF;
IF l_record.ISSUER_CA_ID IS NOT NULL THEN
t_temp2 := t_temp2 || '<A style="white-space:normal" href="?caid=' || l_record.ISSUER_CA_ID::text || t_opt || '">'
|| coalesce(html_escape(l_record.ISSUER_NAME), ' ')
|| '</A>';
ELSE
t_temp2 := t_temp2 || coalesce(html_escape(l_record.ISSUER_NAME), '?');
END IF;
IF lower(t_type) LIKE '%lint' THEN
SELECT cc.INCLUDED_CERTIFICATE_OWNER
INTO t_temp
FROM ca_certificate cac, ccadb_certificate cc
WHERE cac.CA_ID = l_record.ISSUER_CA_ID
AND cac.CERTIFICATE_ID = cc.CERTIFICATE_ID
GROUP BY cc.INCLUDED_CERTIFICATE_OWNER
ORDER BY count(*) DESC
LIMIT 1;
t_temp2 := t_temp2 || '</TD>
<TD>' || coalesce(t_temp, ' ');
END IF;
t_temp2 := t_temp2 || '</TD>
</TR>
';
END IF;
t_text := t_text || t_temp2;
END LOOP;
t_temp := replace(
urlEncode(t_cmd) || '=' || urlEncode(t_value) || coalesce(t_excludeExpired, '') || coalesce(t_excludeCAsString, ''),
'&', '&'
);
IF t_outputType = 'atom' THEN
t_output :=
'[BEGIN_HEADERS]
Cache-Control: max-age=' || t_cacheControlMaxAge::integer || '
Content-Type: application/atom+xml
[END_HEADERS]
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<author>
<name>crt.sh</name>
<uri>https://crt.sh/</uri>
</author>
<icon>https://crt.sh/favicon.ico</icon>
<id>https://crt.sh/?' || t_temp || '</id>
<link rel="self" type="application/atom+xml" href="https://crt.sh/atom?' || t_temp || '"/>
<link rel="via" type="text/html" href="https://crt.sh/"/>
<title>';
IF lower(t_type) LIKE '%lint' THEN
SELECT '[' || li.LINTER || '] ' || li.ISSUE_TEXT
INTO t_summary
FROM lint_issue li
WHERE li.ID = t_value::integer;
t_output := t_output || t_summary;
ELSE
t_output := t_output || html_escape(t_cmd) || '=' || html_escape(t_value);
END IF;
IF t_excludeExpired IS NOT NULL THEN
t_output := t_output || '; ' || substring(t_excludeExpired from 2);
END IF;
IF t_excludeCAsString IS NOT NULL THEN
t_output := t_output || '; ' || substring(t_excludeCAsString from 2);
END IF;
IF coalesce(t_minNotBeforeString, '') != '' THEN
t_output := t_output || '; ' || substring(t_minNotBeforeString from 2);
END IF;
t_output := t_output || '</title>
<updated>' || to_char(coalesce(t_feedUpdated, statement_timestamp()), 'YYYY-MM-DD"T"HH24:MI:SS"Z"') || '</updated>
' || replace(t_text, '__entry_summary__', t_summary) ||
'</feed>';
ELSIF t_outputType = 'html' THEN
t_output := t_output ||
'<TABLE>
<TR>
<TH class="outer">Certificates</TH>
<TD class="outer">';
IF t_text != '' THEN
t_output := t_output || '
<TABLE>
<TR>
';
IF coalesce(t_groupBy, '') = 'none' THEN
t_output := t_output ||
' <TH>
<A href="?' || t_temp || '&dir=' || t_oppositeDirection || '&sort=0' || t_minNotBeforeString || coalesce(t_excludeExpired, '') || coalesce(t_excludeCAsString, '') || t_groupByParameter || '">crt.sh ID</A>
';
IF t_sort = 0 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output ||
' </TH>
<TH style="white-space:nowrap">
<A href="?' || t_temp || '&dir=' || t_oppositeDirection || '&sort=1' || t_minNotBeforeString || coalesce(t_excludeExpired, '') || coalesce(t_excludeCAsString, '') || t_groupByParameter || '">Logged At</A>
';
IF t_sort = 1 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output ||
' </TH>
<TH style="white-space:nowrap"><A href="?' || t_temp || '&dir=' || t_oppositeDirection || '&sort=2' || t_minNotBeforeString || coalesce(t_excludeExpired, '') || coalesce(t_excludeCAsString, '') || t_groupByParameter || '">Not Before</A>
';
IF t_sort = 2 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output ||
' </TH>
';
ELSE
t_output := t_output ||
' <TH>
<A href="?' || t_temp || '&dir=' || t_oppositeDirection || '&sort=1' || t_minNotBeforeString || coalesce(t_excludeExpired, '') || coalesce(t_excludeCAsString, '') || t_groupByParameter || '">#</A>
';
IF t_sort = 1 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output ||
' </TH>
';
END IF;
IF t_showIdentity THEN
IF t_type = 'CT Entry ID' THEN
t_output := t_output ||
' <TH>CT Log</TH>
';
ELSE
t_output := t_output ||
' <TH>Identity</TH>
';
END IF;
END IF;
t_output := t_output ||
' <TH>
<A href="?' || t_temp || '&dir=' || t_oppositeDirection || '&sort=3' || t_minNotBeforeString || coalesce(t_excludeExpired, '') || coalesce(t_excludeCAsString, '') || t_groupByParameter || '">Issuer Name</A>
';
IF t_sort = 3 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output ||
' </TH>
';
IF lower(t_type) LIKE '%lint' THEN
t_output := t_output ||
' <TH>Root Owner (Mozilla)</TH>
';
END IF;
t_output := t_output ||
' </TR>
' || t_text ||
'</TABLE>
';
ELSE
t_output := t_output ||
'<I>None found</I>';
END IF;
t_output := t_output || '</TD>
</TR>
</TABLE>
';
END IF;
END IF;
ELSIF lower(t_type) LIKE '%lint: summary' THEN
IF t_sort NOT BETWEEN 1 AND 18 THEN
t_sort := 1;
END IF;
t_issuerO := get_parameter('issuerO', paramNames, paramValues);
t_issuerOParameter := coalesce(t_issuerO, '');
IF t_issuerOParameter != '' THEN
t_issuerOParameter := '&issuerO=' || t_issuerOParameter;
END IF;
IF t_outputType = 'html' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">' || t_type || '</SPAN>
';
END IF;
IF t_value != '1 week' THEN
t_output := t_output ||
' <BR><BR>Sorry, only "1 week" statistics are currently supported.
';
ELSIF t_groupBy NOT IN ('', 'IssuerO') THEN
t_output := t_output ||
' <BR><BR>Sorry, "IssuerO" is the only currently supported value for "group".
';
ELSE
IF t_outputType = 'html' THEN
t_output := t_output ||
' <SPAN style="position:absolute">
<A style="font-size:8pt;vertical-align:sub" href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_direction || '&sort=' || t_sort::text || t_issuerOParameter;
IF t_groupBy != 'IssuerO' THEN
t_output := t_output || '&group=IssuerO">Group';
ELSE
t_output := t_output || '">Ungroup';
END IF;
t_output := t_output || ' by "Issuer O"</A>
';
IF t_issuerO IS NOT NULL THEN
t_output := t_output || ' <A style="font-size:8pt;vertical-align:sub" href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value)
|| '&dir=' || t_direction || '&sort=' || t_sort::text || t_groupByParameter || '">Show all "Issuer O"s</A>
';
END IF;
t_output := t_output ||
' </SPAN>
<BR><BR>
For certificates with <B>notBefore >= ' || to_char(date_trunc('day', statement_timestamp() - interval '1 week'), 'YYYY-MM-DD') || '</B>';
IF t_issuerO IS NOT NULL THEN
t_output := t_output || ' and <B>"Issuer O" LIKE ''' || t_issuerO || '''</B>';
END IF;
t_output := t_output || ':
<BR><BR>
<TABLE class="lint">
<TR>
<TH rowspan="2"><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=1' || t_groupByParameter || t_issuerOParameter || '">Issuer O</A>';
IF t_sort = 1 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
IF t_groupBy != 'IssuerO' THEN
t_output := t_output || '</TH>
<TH rowspan="2"><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=2' || t_groupByParameter || t_issuerOParameter || '">Issuer CN, OU or O</A>';
IF t_sort = 2 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
END IF;
t_output := t_output || '</TH>
<TH rowspan="2"><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=3' || t_groupByParameter || t_issuerOParameter || '"># Certs<BR>Issued</A>';
IF t_sort = 3 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH colspan="2"><A title="These errors are fatal to the checks and prevent most further checks from being executed. These are extremely bad errors."><SPAN class="fatal"> FATAL </SPAN></A></TH>
<TH colspan="2"><A title="These are issues where the certificate is not compliant with the standard."><SPAN class="error"> ERROR </SPAN></A></TH>
<TH colspan="2"><A title="These are issues where a standard recommends differently but the standard uses terms such as ''SHOULD'' or ''MAY''."><SPAN class="warning"> WARNING </SPAN></A></TH>
<TH colspan="2"><A title="FATAL + ERROR + WARNING">ALL</A></TH>
</TR>
<TR>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=4' || t_groupByParameter || t_issuerOParameter || '"># Certs</A>';
IF t_sort = 4 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=5' || t_groupByParameter || t_issuerOParameter || '">%</A>';
IF t_sort = 5 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=7' || t_groupByParameter || t_issuerOParameter || '"># Certs</A>';
IF t_sort = 7 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=8' || t_groupByParameter || t_issuerOParameter || '">%</A>';
IF t_sort = 8 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=10' || t_groupByParameter || t_issuerOParameter || '"># Certs</A>';
IF t_sort = 10 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=11' || t_groupByParameter || t_issuerOParameter || '">%</A>';
IF t_sort = 11 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=16' || t_groupByParameter || t_issuerOParameter || '"># Certs</A>';
IF t_sort = 16 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=17' || t_groupByParameter || t_issuerOParameter || '">%</A>';
IF t_sort = 17 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
</TR>
';
END IF;
IF t_groupBy = 'IssuerO' THEN
t_query := 'SELECT NULL::integer ISSUER_CA_ID,' || chr(10) ||
' (sum(l1s.CERTS_ISSUED))::bigint CERTS_ISSUED,' || chr(10) ||
' (sum(l1s.ALL_CERTS))::bigint ALL_CERTS,' || chr(10) ||
' ((sum(l1s.ALL_CERTS) * 100) / sum(l1s.CERTS_ISSUED))::numeric ALL_PERC,' || chr(10) ||
' (sum(l1s.FATAL_CERTS))::bigint FATAL_CERTS,' || chr(10) ||
' ((sum(l1s.FATAL_CERTS) * 100) / sum(l1s.CERTS_ISSUED))::numeric FATAL_PERC,' || chr(10) ||
' (sum(l1s.ERROR_CERTS))::bigint ERROR_CERTS,' || chr(10) ||
' ((sum(l1s.ERROR_CERTS) * 100) / sum(l1s.CERTS_ISSUED))::numeric ERROR_PERC,' || chr(10) ||
' (sum(l1s.WARNING_CERTS))::bigint WARNING_CERTS,' || chr(10) ||
' ((sum(l1s.WARNING_CERTS) * 100) / sum(l1s.CERTS_ISSUED))::numeric WARNING_PERC,' || chr(10) ||
' get_ca_name_attribute(l1s.ISSUER_CA_ID, ''organizationName'') ISSUER_ORGANIZATION_NAME,' || chr(10) ||
' NULL ISSUER_FRIENDLY_NAME' || chr(10);
ELSE
t_query := 'SELECT l1s.ISSUER_CA_ID::integer,' || chr(10) ||
' l1s.CERTS_ISSUED::bigint,' || chr(10) ||
' l1s.ALL_CERTS::bigint,' || chr(10) ||
' ((l1s.ALL_CERTS * 100) / l1s.CERTS_ISSUED::numeric) ALL_PERC,' || chr(10) ||
' l1s.FATAL_CERTS::bigint,' || chr(10) ||
' ((l1s.FATAL_CERTS * 100) / l1s.CERTS_ISSUED::numeric) FATAL_PERC,' || chr(10) ||
' l1s.ERROR_CERTS::bigint,' || chr(10) ||
' ((l1s.ERROR_CERTS * 100) / l1s.CERTS_ISSUED::numeric) ERROR_PERC,' || chr(10) ||
' l1s.WARNING_CERTS::bigint,' || chr(10) ||
' ((l1s.WARNING_CERTS * 100) / l1s.CERTS_ISSUED::numeric) WARNING_PERC,' || chr(10) ||
' get_ca_name_attribute(l1s.ISSUER_CA_ID, ''organizationName'') ISSUER_ORGANIZATION_NAME,' || chr(10) ||
' get_ca_name_attribute(l1s.ISSUER_CA_ID, ''_friendlyName_'') ISSUER_FRIENDLY_NAME' || chr(10);
END IF;
t_query := t_query ||
' FROM lint_1week_summary l1s' || chr(10) ||
' WHERE l1s.LINTER ';
IF t_linter IS NOT NULL THEN
t_query := t_query || '= ''' || t_linter || '''' || chr(10);
ELSE
t_query := t_query || 'IS NULL' || chr(10);
END IF;
IF t_issuerO IS NOT NULL THEN
t_query := t_query ||
' AND get_ca_name_attribute(l1s.ISSUER_CA_ID, ''organizationName'') LIKE $1' || chr(10);
END IF;
t_query := t_query || ' ';
IF t_groupBy = 'IssuerO' THEN
t_query := t_query || ' GROUP BY ISSUER_ORGANIZATION_NAME' || chr(10) ||
' ';
END IF;
IF t_sort = 1 THEN
t_query := t_query || 'ORDER BY ISSUER_ORGANIZATION_NAME ' || t_orderBy || ', ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 2 THEN
t_query := t_query || 'ORDER BY ISSUER_FRIENDLY_NAME ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME';
ELSIF t_sort = 3 THEN
t_query := t_query || 'ORDER BY CERTS_ISSUED ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 4 THEN
t_query := t_query || 'ORDER BY FATAL_CERTS ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 5 THEN
t_query := t_query || 'ORDER BY FATAL_PERC ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 7 THEN
t_query := t_query || 'ORDER BY ERROR_CERTS ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 8 THEN
t_query := t_query || 'ORDER BY ERROR_PERC ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 10 THEN
t_query := t_query || 'ORDER BY WARNING_CERTS ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 11 THEN
t_query := t_query || 'ORDER BY WARNING_PERC ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 16 THEN
t_query := t_query || 'ORDER BY ALL_CERTS ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
ELSIF t_sort = 17 THEN
t_query := t_query || 'ORDER BY ALL_PERC ' || t_orderBy || ', ISSUER_ORGANIZATION_NAME, ISSUER_FRIENDLY_NAME';
END IF;
FOR l_record IN EXECUTE t_query USING t_issuerO LOOP
IF t_outputType = 'json' THEN
t_output := t_output || row_to_json(l_record, FALSE);
ELSIF t_outputType = 'html' THEN
t_output := t_output || '
<TR>
<TD>';
IF l_record.ISSUER_ORGANIZATION_NAME IS NOT NULL THEN
t_output := t_output || '<A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_direction
|| '&sort=' || t_sort::text || t_groupByParameter
|| '&issuerO=' || urlEncode(l_record.ISSUER_ORGANIZATION_NAME) || '">'
|| l_record.ISSUER_ORGANIZATION_NAME || '</A>';
ELSE
t_output := t_output || ' ';
END IF;
t_output := t_output || '</TD>
';
IF t_groupBy != 'IssuerO' THEN
t_output := t_output ||
' <TD><A href="?caid=' || l_record.ISSUER_CA_ID::text || '&opt=' || t_linters || '">' || coalesce(l_record.ISSUER_FRIENDLY_NAME, ' ') || '</A></TD>
';
END IF;
t_output := t_output ||
' <TD>' || l_record.CERTS_ISSUED::text || '</TD>
<TD>' || l_record.FATAL_CERTS::text || '</TD>
<TD>' || replace(round(l_record.FATAL_PERC, 2)::text, '.00', '') || '</TD>
<TD>' || l_record.ERROR_CERTS::text || '</TD>
<TD>' || replace(round(l_record.ERROR_PERC, 2)::text, '.00', '') || '</TD>
<TD>' || l_record.WARNING_CERTS::text || '</TD>
<TD>' || replace(round(l_record.WARNING_PERC, 2)::text, '.00', '') || '</TD>
<TD>' || l_record.ALL_CERTS::text || '</TD>
<TD>' || replace(round(l_record.ALL_PERC, 2)::text, '.00', '') || '</TD>
</TR>
';
END IF;
END LOOP;
IF t_outputType = 'html' THEN
t_output := t_output ||
' </TABLE>
';
END IF;
END IF;
ELSIF lower(t_type) LIKE '%lint: issues' THEN
IF t_sort NOT BETWEEN 1 AND 3 THEN
t_sort := 1;
END IF;
t_temp := get_parameter('exclude', paramNames, paramValues);
IF lower(coalesce(',' || t_temp || ',', 'nothing')) LIKE ',affected_certs,' THEN
t_excludeAffectedCerts := '&exclude=affected_certs';
END IF;
IF t_outputType = 'html' THEN
t_output := t_output ||
' <SPAN class="whiteongrey">' || t_type || '</SPAN>
<BR><BR>
For certificates with <B>notBefore >= ' || to_char(t_minNotBefore, 'YYYY-MM-DD') || '</B>:
<BR><BR>
<TABLE class="lint">
<TR>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=1' || t_groupByParameter || coalesce(t_excludeAffectedCerts, '') || '">Severity</A>';
IF t_sort = 1 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=2' || t_groupByParameter || coalesce(t_excludeAffectedCerts, '') || '">Issue</A>';
IF t_sort = 2 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
<TH><A href="?' || urlEncode(t_cmd) || '=' || urlEncode(t_value) || '&dir=' || t_oppositeDirection || '&sort=3' || t_groupByParameter || coalesce(t_excludeAffectedCerts, '') || '"># Affected Certs</A>';
IF t_sort = 3 THEN
t_output := t_output || ' ' || t_dirSymbol;
END IF;
t_output := t_output || '</TH>
</TR>
';
END IF;
t_query := 'SELECT li.ID, li.ISSUE_TEXT,';
IF t_excludeAffectedCerts IS NULL THEN
t_query := t_query || ' sum(ls.NO_OF_CERTS) NUM_CERTS,';
ELSE
t_query := t_query || ' -1::bigint NUM_CERTS,';
END IF;
t_query := t_query || chr(10) ||
' CASE li.SEVERITY' || chr(10) ||
' WHEN ''F'' THEN 1' || chr(10) ||
' WHEN ''E'' THEN 2' || chr(10) ||
' WHEN ''W'' THEN 3' || chr(10) ||
' ELSE 4' || chr(10) ||
' END ISSUE_TYPE,' || chr(10) ||
' CASE li.SEVERITY' || chr(10) ||
' WHEN ''F'' THEN ''FATAL''' || chr(10) ||
' WHEN ''E'' THEN ''ERROR''' || chr(10) ||
' WHEN ''W'' THEN ''WARNING''' || chr(10) ||
' ELSE li.SEVERITY ' || chr(10) ||
' END ISSUE_HEADING,' || chr(10) ||
' CASE li.SEVERITY' || chr(10) ||
' WHEN ''F'' THEN ''class="fatal"''' || chr(10) ||
' WHEN ''E'' THEN ''class="error"''' || chr(10) ||
' WHEN ''W'' THEN ''class="warning"''' || chr(10) ||
' ELSE ''''' || chr(10) ||
' END ISSUE_CLASS' || chr(10);
IF t_excludeAffectedCerts IS NULL THEN
t_query := t_query ||
' FROM lint_summary ls, lint_issue li, ca' || chr(10) ||
' WHERE ls.LINT_ISSUE_ID = li.ID' || chr(10) ||
' AND ls.ISSUER_CA_ID = ca.ID' || chr(10) ||
' AND ca.LINTING_APPLIES' || chr(10);
IF t_linter IS NOT NULL THEN
t_query := t_query ||
' AND li.LINTER = ''' || t_linter || '''' || chr(10);
END IF;
t_query := t_query ||
' AND ls.NOT_BEFORE_DATE >= $1' || chr(10) ||
' GROUP BY li.ID, li.SEVERITY, li.ISSUE_TEXT' || chr(10);
ELSE
t_query := t_query ||
' FROM lint_issue li' || chr(10);
IF t_linter IS NOT NULL THEN
t_query := t_query ||
' AND li.LINTER = ''' || t_linter || '''' || chr(10);
END IF;
END IF;
IF t_sort = 1 THEN
t_query := t_query ||
' ORDER BY ISSUE_TYPE, li.ISSUE_TEXT ' || t_orderBy;
ELSIF t_sort = 2 THEN
t_query := t_query ||
' ORDER BY li.ISSUE_TEXT ' || t_orderBy;
ELSIF t_sort = 3 THEN
t_query := t_query ||
' ORDER BY NUM_CERTS ' || t_orderBy;
END IF;
FOR l_record IN EXECUTE t_query USING t_minNotBefore LOOP
IF t_outputType = 'json' THEN
t_output := t_output || row_to_json(l_record, FALSE);
ELSIF t_outputType = 'html' THEN
t_output := t_output ||
' <TR>
<TD ' || l_record.ISSUE_CLASS || '>' || l_record.ISSUE_HEADING || '</TD>
<TD ' || l_record.ISSUE_CLASS || '>' || l_record.ISSUE_TEXT || '</TD>
<TD><A href="?' || urlEncode(t_cmd) || '=' || l_record.ID::text || t_minNotBeforeString || '">';
IF l_record.NUM_CERTS = -1 THEN
t_output := t_output || '?';
ELSE
t_output := t_output || l_record.NUM_CERTS;
END IF;
t_output := t_output || '</A></TD>
</TR>
';
END IF;
END LOOP;
IF t_outputType = 'html' THEN
t_output := t_output ||
' </TABLE>
';
END IF;
ELSE
t_output := t_output || ' <SPAN class="whiteongrey">Error</SPAN>
<BR><BR>''' || name || ''' is an unsupported action!
';
END IF;
IF t_outputType = 'html' THEN
t_output :=
'[BEGIN_HEADERS]
Cache-Control: max-age=' || t_cacheControlMaxAge::integer || '
Content-Type: text/html; charset=UTF-8
[END_HEADERS]
' || t_output || '
<BR><BR><BR>
';
IF upper(coalesce(get_parameter('showSQL', paramNames, paramValues), 'N')) = 'Y' THEN
IF t_query IS NOT NULL THEN
t_output := t_output || '<BR><BR><TEXTAREA cols="80" rows="25">' || t_query || ';</TEXTAREA>';
END IF;
END IF;
t_output := t_output || '
<P class="copyright">© COMODO CA Limited 2015-2017. All rights reserved.</P>
<DIV>
<A href="https://github.com/crtsh"><IMG src="/GitHub-Mark-32px.png"></A>
</DIV>
</BODY>
</HTML>';
END IF;
IF t_cacheResponse THEN
INSERT INTO cached_response (
PAGE_NAME, GENERATED_AT, RESPONSE_BODY
)
VALUES (
t_type, statement_timestamp(), t_output
)
ON CONFLICT (PAGE_NAME) DO UPDATE
SET GENERATED_AT = statement_timestamp(),
RESPONSE_BODY = t_output;
RETURN 'Cached';
ELSE
RETURN t_output;
END IF;
EXCEPTION
WHEN no_data_found THEN
RETURN
'[BEGIN_HEADERS]
Cache-Control: max-age=' || t_cacheControlMaxAge::integer || '
Content-Type: text/html; charset=UTF-8
[END_HEADERS]
' || coalesce(t_output, '') || '<BR><BR>' || SQLERRM ||
'</BODY>
</HTML>
';
WHEN others THEN
GET STACKED DIAGNOSTICS t_temp = PG_EXCEPTION_CONTEXT;
RETURN
'[BEGIN_HEADERS]
Cache-Control: max-age=' || t_cacheControlMaxAge::integer || '
Content-Type: text/html; charset=UTF-8
[END_HEADERS]
' || coalesce(t_output, '') || '<BR><BR>' || html_escape(SQLERRM) || '<BR><BR>' || html_escape(coalesce(t_temp, '')) || '<BR><BR>' || html_escape(coalesce(t_query, ''));
END;
$$ LANGUAGE plpgsql;
|