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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* egg-asn1x.c - ASN.1/DER parse and coding routines
Copyright (C) 2009 Stefan Walter
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <stef@memberwebs.com>
*/
/*
* Some portions are:
*
* Copyright (C) 2004, 2006, 2008, 2009 Free Software Foundation
* Copyright (C) 2002 Fabio Fiorina
*
* This file is part of LIBTASN1.
*
* The LIBTASN1 library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*/
#include "config.h"
#include "egg-asn1x.h"
#include <libtasn1.h>
#include <stdlib.h>
#include <string.h>
/* From libtasn1's int.h */
enum {
TYPE_CONSTANT = 1,
TYPE_IDENTIFIER = 2,
TYPE_INTEGER = 3,
TYPE_BOOLEAN = 4,
TYPE_SEQUENCE = 5,
TYPE_BIT_STRING = 6,
TYPE_OCTET_STRING = 7,
TYPE_TAG = 8,
TYPE_DEFAULT = 9,
TYPE_SIZE = 10,
TYPE_SEQUENCE_OF = 11,
TYPE_OBJECT_ID = 12,
TYPE_ANY = 13,
TYPE_SET = 14,
TYPE_SET_OF = 15,
TYPE_DEFINITIONS = 16,
TYPE_TIME = 17,
TYPE_CHOICE = 18,
TYPE_IMPORTS = 19,
TYPE_NULL = 20,
TYPE_ENUMERATED = 21,
TYPE_GENERALSTRING = 27
};
enum {
FLAG_UNIVERSAL = (1<<8),
FLAG_PRIVATE = (1<<9),
FLAG_APPLICATION = (1<<10),
FLAG_EXPLICIT = (1<<11),
FLAG_IMPLICIT = (1<<12),
FLAG_TAG = (1<<13),
FLAG_OPTION = (1<<14),
FLAG_DEFAULT = (1<<15),
FLAG_TRUE = (1<<16),
FLAG_FALSE = (1<<17),
FLAG_LIST = (1<<18),
FLAG_MIN_MAX = (1<<19),
FLAG_1_PARAM = (1<<20),
FLAG_SIZE = (1<<21),
FLAG_DEFINED_BY = (1<<22),
FLAG_GENERALIZED = (1<<23),
FLAG_UTC = (1<<24),
FLAG_IMPORTS = (1<<25),
FLAG_NOT_USED = (1<<26),
FLAG_SET = (1<<27),
FLAG_ASSIGN = (1<<28),
FLAG_DOWN = (1<<29),
FLAG_RIGHT = (1<<30),
};
typedef struct _Aenc Aenc;
typedef struct _Atlv Atlv;
typedef struct _Anode Anode;
typedef struct _Abuf Abuf;
typedef struct _Abits Abits;
struct _Aenc {
EggAsn1xEncoder encoder;
gpointer data;
};
struct _Atlv {
guchar cls;
gulong tag;
gint off;
gint oft;
gint len;
const guchar *buf;
const guchar *end;
};
struct _Anode {
const ASN1_ARRAY_TYPE *def;
const ASN1_ARRAY_TYPE *join;
GList *opts;
Atlv *tlv;
Aenc *enc;
gpointer user_data;
GDestroyNotify destroy;
gchar* failure;
gint chosen : 1;
};
struct _Abuf {
guchar* data;
gsize n_data;
gpointer user_data;
};
struct _Abits {
guint n_bits;
guchar *bits;
GDestroyNotify destroy;
};
/* Forward Declarations */
static gboolean anode_decode_anything (GNode*, Atlv*);
static gboolean anode_decode_anything_for_flags (GNode *, Atlv*, gint);
static gboolean anode_validate_anything (GNode*);
static gboolean anode_encode_prepare (GNode*, gboolean want);
static gint
atoin (const char *p, gint digits)
{
gint ret = 0, base = 1;
while(--digits >= 0) {
if (p[digits] < '0' || p[digits] > '9')
return -1;
ret += (p[digits] - '0') * base;
base *= 10;
}
return ret;
}
static GNode*
anode_new (const ASN1_ARRAY_TYPE *def)
{
Anode *an = g_slice_new0 (Anode);
an->def = def;
return g_node_new (an);
}
static gpointer
anode_copy_func (gconstpointer src, gpointer unused)
{
const Anode *san = src;
Anode *an = g_slice_new0 (Anode);
an->def = san->def;
an->join = san->join;
an->opts = g_list_copy (san->opts);
return an;
}
static GNode*
anode_clone (GNode *node)
{
return g_node_copy_deep (node, anode_copy_func, NULL);
}
static int
anode_def_type (GNode *node)
{
Anode *an = node->data;
gint type = an->join ? an->join->type : an->def->type;
return type & 0xFF;
}
static gboolean
anode_def_type_is_real (GNode *node)
{
switch (anode_def_type (node)) {
case TYPE_INTEGER:
case TYPE_BOOLEAN:
case TYPE_BIT_STRING:
case TYPE_OCTET_STRING:
case TYPE_OBJECT_ID:
case TYPE_TIME:
case TYPE_NULL:
case TYPE_ENUMERATED:
case TYPE_GENERALSTRING:
return TRUE;
case TYPE_SEQUENCE:
case TYPE_SEQUENCE_OF:
case TYPE_ANY:
case TYPE_SET:
case TYPE_SET_OF:
case TYPE_CHOICE:
return TRUE;
case TYPE_CONSTANT:
case TYPE_IDENTIFIER:
case TYPE_TAG:
case TYPE_DEFAULT:
case TYPE_SIZE:
case TYPE_DEFINITIONS:
case TYPE_IMPORTS:
return FALSE;
default:
g_return_val_if_reached (FALSE);
}
}
static int
anode_def_flags (GNode *node)
{
Anode *an = node->data;
gint type = an->def->type;
if (an->join)
type |= an->join->type;
return type & 0xFFFFFF00;
}
static const gchar*
anode_def_name (GNode *node)
{
Anode *an = node->data;
return an->def->name;
}
static const gchar*
anode_def_value (GNode *node)
{
Anode *an = node->data;
return an->def->value;
}
static gulong
anode_def_value_as_ulong (const ASN1_ARRAY_TYPE *def)
{
gchar *end = NULL;
gulong lval;
g_return_val_if_fail (def->value, G_MAXULONG);
lval = strtoul (def->value, &end, 10);
g_return_val_if_fail (end && !end[0], G_MAXULONG);
return lval;
}
static GNode*
anode_child_with_name (GNode *node, const gchar *name)
{
GNode *child;
for (child = node->children; child; child = child->next) {
if (g_str_equal (name, anode_def_name (child)))
return child;
}
return NULL;
}
static void
anode_opt_add (GNode *node, const ASN1_ARRAY_TYPE *def)
{
Anode *an = node->data;
an->opts = g_list_append (an->opts, (gpointer)def);
}
static ASN1_ARRAY_TYPE*
anode_opt_lookup (GNode *node, gint type, const gchar *name)
{
Anode *an = node->data;
ASN1_ARRAY_TYPE* def;
GList *l;
for (l = an->opts; l; l = g_list_next (l)) {
def = l->data;
if (name && def->name && !g_str_equal (name, def->name))
continue;
if ((def->type & 0xFF) == type)
return def;
}
return NULL;
}
static ASN1_ARRAY_TYPE*
anode_opt_lookup_value (GNode *node, gint type, const gchar *value)
{
Anode *an = node->data;
ASN1_ARRAY_TYPE* def;
GList *l;
for (l = an->opts; l; l = g_list_next (l)) {
def = l->data;
if (value && def->value && !g_str_equal (value, def->value))
continue;
if ((def->type & 0xFF) == type)
return def;
}
return NULL;
}
static GList*
anode_opts_lookup (GNode *node, gint type, const gchar *name)
{
Anode *an = node->data;
ASN1_ARRAY_TYPE* def;
GList *l, *res = NULL;
for (l = an->opts; l; l = g_list_next (l)) {
def = l->data;
if (name && def->name && !g_str_equal (name, def->name))
continue;
if ((def->type & 0xFF) == type)
res = g_list_prepend (res, def);
}
return g_list_reverse (res);
}
static gint
compare_tlvs (Atlv *tlva, Atlv *tlvb)
{
gint la = tlva->off + tlva->len;
gint lb = tlvb->off + tlvb->len;
gint res;
g_assert (tlva->buf);
g_assert (tlvb->buf);
res = memcmp (tlva->buf, tlvb->buf, MIN (la, lb));
if (la == lb || res != 0)
return res;
return la < lb ? -1 : 1;
}
static void
anode_set_tlv_data (GNode *node, Atlv *tlv)
{
Anode *an = node->data;
g_assert (!an->tlv);
g_assert (tlv->len >= 0);
an->tlv = g_slice_new0 (Atlv);
memcpy (an->tlv, tlv, sizeof (Atlv));
}
static Atlv*
anode_get_tlv_data (GNode *node)
{
Anode *an = node->data;
return an->tlv;
}
static void
anode_clr_tlv_data (GNode *node)
{
Anode *an = node->data;
if (an->tlv);
g_slice_free (Atlv, an->tlv);
an->tlv = NULL;
}
static void
anode_clr_enc_data (GNode *node)
{
Anode *an = node->data;
if (an->enc) {
g_slice_free (Aenc, an->enc);
an->enc = NULL;
}
}
static void
anode_set_enc_data (GNode *node, EggAsn1xEncoder encoder, gpointer enc_data)
{
Anode *an = node->data;
g_assert (!an->enc);
an->enc = g_slice_new0 (Aenc);
an->enc->encoder = encoder;
an->enc->data = enc_data;
}
static void
anode_set_user_data (GNode *node, gpointer user_data, GDestroyNotify destroy)
{
Anode *an;
g_assert (node && node->data);
an = node->data;
if (an->destroy)
(an->destroy) (an->user_data);
an->user_data = user_data;
an->destroy = destroy;
}
static Aenc*
anode_get_enc_data (GNode *node)
{
Anode *an = node->data;
return an->enc;
}
static gboolean
anode_failure (GNode *node, const gchar *failure)
{
Anode *an = node->data;
const gchar *prefix = an->def->name;
if (!prefix && an->join)
prefix = an->join->name;
if (!prefix)
prefix = an->def->value;
if (!prefix && an->join)
prefix = an->join->value;
if (!prefix)
prefix = "unknown";
g_free (an->failure);
an->failure = g_strdup_printf ("%s: %s", prefix, failure);
return FALSE; /* So this can be chained */
}
static const gchar*
anode_failure_get (GNode *node)
{
Anode *an = node->data;
return an->failure;
}
static void
anode_clear (GNode *node)
{
Anode *an = node->data;
anode_clr_tlv_data (node);
anode_clr_enc_data (node);
anode_set_user_data (node, NULL, NULL);
g_free (an->failure);
an->failure = NULL;
}
static gboolean
anode_free_func (GNode *node, gpointer unused)
{
Anode *an = node->data;
anode_clear (node);
g_list_free (an->opts);
g_slice_free (Anode, an);
return FALSE;
}
static void
abits_destroy (gpointer data)
{
Abits *ab = data;
g_assert (ab);
if (ab->destroy)
(ab->destroy) (ab->bits);
g_slice_free (Abits, ab);
}
static void
anode_destroy (GNode *node)
{
if (!G_NODE_IS_ROOT (node))
g_node_unlink (node);
g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, anode_free_func, NULL);
g_node_destroy (node);
}
static gulong
anode_calc_tag_for_flags (GNode *node, gint flags)
{
ASN1_ARRAY_TYPE *def;
/* A context specific tag */
if (flags & FLAG_TAG) {
def = anode_opt_lookup (node, TYPE_TAG, NULL);
g_return_val_if_fail (def, G_MAXULONG);
return anode_def_value_as_ulong (def);
}
/* A tag from the universal set */
switch (anode_def_type (node)) {
case TYPE_INTEGER:
return ASN1_TAG_INTEGER;
case TYPE_ENUMERATED:
return ASN1_TAG_ENUMERATED;
case TYPE_BOOLEAN:
return ASN1_TAG_BOOLEAN;
case TYPE_BIT_STRING:
return ASN1_TAG_BIT_STRING;
case TYPE_OCTET_STRING:
return ASN1_TAG_OCTET_STRING;
case TYPE_OBJECT_ID:
return ASN1_TAG_OBJECT_ID;
case TYPE_NULL:
return ASN1_TAG_NULL;
case TYPE_GENERALSTRING:
return ASN1_TAG_GENERALSTRING;
case TYPE_TIME:
if (flags & FLAG_GENERALIZED)
return ASN1_TAG_GENERALIZEDTime;
else if (flags & FLAG_UTC)
return ASN1_TAG_UTCTime;
else
g_return_val_if_reached (G_MAXULONG);
case TYPE_SEQUENCE:
case TYPE_SEQUENCE_OF:
return ASN1_TAG_SEQUENCE;
case TYPE_SET:
case TYPE_SET_OF:
return ASN1_TAG_SET;
/* These should be handled specially */
case TYPE_ANY:
case TYPE_CHOICE:
return G_MAXULONG;
/* These are not real nodes */
case TYPE_CONSTANT:
case TYPE_IDENTIFIER:
case TYPE_TAG:
case TYPE_DEFAULT:
case TYPE_SIZE:
case TYPE_DEFINITIONS:
case TYPE_IMPORTS:
g_return_val_if_reached (G_MAXULONG);
/* Unknown value */
default:
g_return_val_if_reached (G_MAXULONG);
}
}
static gulong
anode_calc_tag (GNode *node)
{
return anode_calc_tag_for_flags (node, anode_def_flags (node));
}
static gboolean
anode_calc_explicit_for_flags (GNode *node, gint flags)
{
const ASN1_ARRAY_TYPE *opt;
if ((flags & FLAG_TAG) != FLAG_TAG)
return FALSE;
opt = anode_opt_lookup (node, TYPE_TAG, NULL);
g_return_val_if_fail (opt, FALSE);
if ((opt->type & FLAG_IMPLICIT) == FLAG_IMPLICIT)
return FALSE;
return TRUE;
}
static gboolean
anode_calc_explicit (GNode *node)
{
return anode_calc_explicit_for_flags (node, anode_def_flags (node));
}
/* -------------------------------------------------------------------------
* DECODE
*/
static gboolean
anode_decode_cls_tag (const guchar *data, const guchar *end,
guchar *cls, gulong *tag, gint *cb)
{
gint punt, ris, last;
gint n_data;
g_assert (end >= data);
g_assert (cls);
g_assert (cb);
n_data = end - data;
if (n_data < 2)
return FALSE;
*cls = data[0] & 0xE0;
/* short form */
if ((data[0] & 0x1F) != 0x1F) {
*cb = 1;
ris = data[0] & 0x1F;
/* Long form */
} else {
punt = 1;
ris = 0;
while (punt <= n_data && data[punt] & 128) {
int last = ris;
ris = ris * 128 + (data[punt++] & 0x7F);
/* wrapper around, and no bignums... */
if (ris < last)
return FALSE;
}
if (punt >= n_data)
return FALSE;
last = ris;
ris = ris * 128 + (data[punt++] & 0x7F);
/* wrapper around, and no bignums... */
if (ris < last)
return FALSE;
*cb = punt;
}
if (tag)
*tag = ris;
return TRUE;
}
static gint
anode_decode_length (const guchar *data, const guchar *end, gint *cb)
{
gint ans, last;
gint k, punt;
gint n_data;
g_assert (data);
g_assert (end);
g_assert (end >= data);
g_assert (cb);
*cb = 0;
n_data = end - data;
if (n_data == 0)
return 0;
/* short form */
if (!(data[0] & 128)) {
*cb = 1;
return data[0];
/* Long form */
} else {
k = data[0] & 0x7F;
punt = 1;
/* definite length method */
if (k) {
ans = 0;
while (punt <= k && punt < n_data) {
last = ans;
ans = ans * 256 + data[punt++];
/* we wrapped around, no bignum support... */
if (ans < last)
return -2;
}
/* indefinite length method */
} else {
ans = -1;
}
*cb = punt;
return ans;
}
}
static gboolean
anode_decode_cls_tag_len (const guchar *data, const guchar *end,
guchar *cls, gulong *tag, gint *off, gint *len)
{
gint cb1, cb2;
g_assert (data);
g_assert (end);
g_assert (end >= data);
g_assert (off);
g_assert (len);
if (!anode_decode_cls_tag (data, end, cls, tag, &cb1))
return FALSE;
*len = anode_decode_length (data + cb1, end, &cb2);
if (*len < -1)
return FALSE;
*off = cb1 + cb2;
if (*len >= 0 && data + *off + *len > end)
return FALSE;
return TRUE;
}
static gboolean
anode_check_indefinite_end (guchar cls, gulong tag, gint len)
{
return (cls == ASN1_CLASS_UNIVERSAL && tag == 0 && len == 0);
}
static gboolean
anode_decode_indefinite_len (const guchar *data, const guchar *end, gint *rlen)
{
gint result = 0;
gint der_len;
gint len;
guchar cls;
gulong tag;
gint off;
g_assert (data <= end);
der_len = end - data;
while (result < der_len) {
if (!anode_decode_cls_tag_len (data + result, end, &cls, &tag, &off, &len))
return FALSE;
/* The indefinite end */
if (anode_check_indefinite_end (cls, tag, len))
break;
result += off;
/* Mid way check */
if (result > der_len)
break;
if (len < 0) {
if (!anode_decode_indefinite_len (data + result, end, &len))
return FALSE;
g_assert (len >= 0);
}
if (result + len > der_len)
return FALSE;
result += len;
}
if (result > der_len)
return FALSE;
*rlen = result;
return TRUE;
}
static gboolean
anode_decode_tlv_for_data (const guchar *data, const guchar *end, Atlv *tlv)
{
g_assert (data <= end);
if (!anode_decode_cls_tag_len (data, end, &tlv->cls,
&tlv->tag, &tlv->off, &tlv->len))
return FALSE;
tlv->buf = data;
if (tlv->len < 0)
tlv->end = end;
else
tlv->end = tlv->buf + tlv->len + tlv->off;
g_assert (tlv->end <= end);
return TRUE;
}
static gboolean
anode_decode_tlv_for_contents (Atlv *outer, gboolean first, Atlv *tlv)
{
const guchar *data;
const guchar *end;
if (first) {
data = outer->buf + outer->off;
end = outer->end;
} else {
data = tlv->end;
end = outer->end;
}
/* The end */
if (end == data) {
tlv->cls = ASN1_CLASS_UNIVERSAL;
tlv->tag = 0;
tlv->len = 0;
tlv->off = 0;
tlv->buf = data;
tlv->end = end;
return TRUE;
}
g_return_val_if_fail (end > data, FALSE);
if (!anode_decode_tlv_for_data (data, end, tlv))
return FALSE;
/* Caller should stop before indefinite end, and not consume */
if (anode_check_indefinite_end (tlv->cls, tlv->tag, tlv->len)) {
tlv->buf = data;
tlv->end = data;
tlv->off = 0;
}
return TRUE;
}
static gboolean
anode_decode_choice (GNode *node, Atlv *tlv)
{
gboolean have = FALSE;
GNode *child;
Anode *an;
for (child = node->children; child; child = child->next) {
an = (Anode*)child->data;
if (!have && anode_decode_anything (child, tlv)) {
an->chosen = 1;
have = TRUE;
} else {
an->chosen = 0;
}
}
if (!have)
return anode_failure (node, "no choice is present");
return TRUE;
}
static gboolean
anode_decode_struct_string (GNode *node, Atlv *outer)
{
gint i = 0;
Atlv tlv;
/* Recalculated below */
outer->len = 0;
for (i = 0; TRUE; ++i) {
if (!anode_decode_tlv_for_contents (outer, i == 0, &tlv))
return anode_failure (node, "invalid encoding of child");
if (tlv.tag != outer->tag)
return anode_failure (node, "contents have an invalid tag");
outer->len = (tlv.end - outer->buf) - outer->off;
}
g_assert (outer->len >= 0);
return TRUE;
}
static gboolean
anode_decode_struct_any (GNode *node, Atlv *tlv)
{
if (tlv->len < 0) {
if (!anode_decode_indefinite_len (tlv->buf + tlv->off, tlv->end, &tlv->len))
return anode_failure (node, "could not find end of encoding");
tlv->end = tlv->buf + tlv->off + tlv->len;
}
return TRUE;
}
static gboolean
anode_decode_sequence_or_set (GNode *node, Atlv *outer)
{
GNode *child;
Atlv tlv;
gint i;
/* Recalculated below */
outer->len = 0;
/*
* The reason we can parse a set just like a sequence, is because in DER,
* the order of the SET is predefined by the tags. In addition the definitions
* we have are sorted.
*/
for (child = node->children, i = 0; child; child = child->next, ++i) {
if (!anode_decode_tlv_for_contents (outer, i == 0, &tlv))
return anode_failure (node, "invalid encoding of child");
if (!anode_decode_anything (child, &tlv))
return FALSE;
outer->len = (tlv.end - outer->buf) - outer->off;
}
g_assert (outer->len >= 0);
return TRUE;
}
static gboolean
anode_decode_sequence_or_set_of (GNode *node, Atlv *outer)
{
GNode *child, *other;
Atlv tlv;
gint i;
outer->len = 0;
/* The first child */
child = node->children;
g_return_val_if_fail (child, FALSE);
/* Remove all the other children */
while (child->next)
anode_destroy (child->next);
/* Try to dig out as many of them as possible */
for (i = 0; TRUE; ++i) {
if (!anode_decode_tlv_for_contents (outer, i == 0, &tlv))
return anode_failure (node, "invalid encoding of child");
/* The end of the road for us */
if (tlv.off == 0)
break;
if (i == 0) {
other = child;
} else {
other = anode_clone (child);
g_node_append (node, other);
}
if (!anode_decode_anything (other, &tlv))
return FALSE;
outer->len = (tlv.end - outer->buf) - outer->off;
}
g_assert (outer->len >= 0);
return TRUE;
}
static gboolean
anode_decode_primitive (GNode *node, Atlv *tlv, gint flags)
{
gint type;
/* Must have a definite length */
if (tlv->len < 0)
return anode_failure (node, "primitive value with an indefinite length");
type = anode_def_type (node);
switch (type) {
/* The primitive value types */
case TYPE_INTEGER:
case TYPE_ENUMERATED:
case TYPE_BOOLEAN:
case TYPE_BIT_STRING:
case TYPE_OCTET_STRING:
case TYPE_OBJECT_ID:
case TYPE_NULL:
case TYPE_GENERALSTRING:
case TYPE_TIME:
anode_set_tlv_data (node, tlv);
return TRUE;
/* Transparent types */
case TYPE_ANY:
anode_set_tlv_data (node, tlv);
return TRUE;
case TYPE_CHOICE:
if (!anode_decode_choice (node, tlv))
return FALSE;
anode_set_tlv_data (node, tlv);
return TRUE;
default:
return anode_failure (node, "primitive value of an unexpected type");
}
g_assert_not_reached ();
}
static gboolean
anode_decode_structured (GNode *node, Atlv *tlv, gint flags)
{
gboolean definite;
const guchar *end;
Atlv ctlv;
gint len;
gulong tag;
guchar cls;
gint off = 0;
definite = (tlv->len >= 0);
end = tlv->end;
/* An explicit, wrapped tag */
if (anode_calc_explicit_for_flags (node, flags)) {
if ((tlv->cls & ASN1_CLASS_CONTEXT_SPECIFIC) == 0)
return anode_failure (node, "missing context specific tag");
if (!anode_decode_tlv_for_contents (tlv, TRUE, &ctlv))
return anode_failure (node, "invalid encoding of child");
flags &= ~FLAG_TAG;
if (!anode_decode_anything_for_flags (node, &ctlv, flags))
return FALSE;
/* Use most of the child's tlv */
tlv->cls = ctlv.cls;
tlv->tag = ctlv.tag;
tlv->off += ctlv.off;
tlv->oft = ctlv.off;
tlv->len = ctlv.len;
anode_clr_tlv_data (node);
/* Other structured types */
} else {
switch (anode_def_type (node)) {
case TYPE_ANY:
if (!anode_decode_struct_any (node, tlv))
return FALSE;
break;
case TYPE_CHOICE:
if (!anode_decode_choice (node, tlv))
return FALSE;
break;
case TYPE_GENERALSTRING:
case TYPE_OCTET_STRING:
if (!anode_decode_struct_string (node, tlv))
return FALSE;
break;
case TYPE_SEQUENCE:
case TYPE_SET:
if (!anode_decode_sequence_or_set (node, tlv))
return FALSE;
break;
case TYPE_SEQUENCE_OF:
case TYPE_SET_OF:
if (!anode_decode_sequence_or_set_of (node, tlv))
return FALSE;
break;
default:
return FALSE;
}
}
g_return_val_if_fail (tlv->len >= 0, FALSE);
/* Indefinite, needs to be terminated with zeros */
if (!definite) {
if (!anode_decode_cls_tag_len (tlv->buf + (tlv->off + tlv->len), end,
&cls, &tag, &off, &len))
return anode_failure (node, "end of indefinite content is missing");
if (!anode_check_indefinite_end (cls, tag, len))
return anode_failure (node, "end of indefinite content is invalid");
end = tlv->buf + tlv->off + tlv->len + off;
}
/* A structure must be filled up, no stuff ignored */
if (tlv->buf + tlv->off + tlv->len + off < end)
return anode_failure (node, "extra data at the end of the content");
g_return_val_if_fail (tlv->buf + tlv->off + tlv->len + off == end, FALSE);
tlv->end = end;
anode_set_tlv_data (node, tlv);
return TRUE;
}
static gboolean
anode_decode_option_or_default (GNode *node, Atlv *tlv, gint flags)
{
if (flags & FLAG_OPTION || flags & FLAG_DEFAULT) {
tlv->len = 0;
tlv->end = tlv->buf;
tlv->off = 0;
anode_clr_tlv_data (node);
return TRUE;
}
return FALSE;
}
static gboolean
anode_decode_anything_for_flags (GNode *node, Atlv *tlv, gint flags)
{
gboolean ret;
gulong tag;
tag = anode_calc_tag_for_flags (node, flags);
/* We don't know what the tag is supposed to be */
if (tag == G_MAXULONG)
tag = tlv->tag;
/* Tag does not match, what do we do? */
if (tlv->off == 0 || tag != tlv->tag) {
if (anode_decode_option_or_default (node, tlv, flags))
return TRUE;
return anode_failure (node, "decoded tag did not match expected");
}
/* Structured value */
if (tlv->cls & ASN1_CLASS_STRUCTURED)
ret = anode_decode_structured (node, tlv, flags);
/* A primitive simple value */
else
ret = anode_decode_primitive (node, tlv, flags);
return ret;
}
static gboolean
anode_decode_anything (GNode *node, Atlv *tlv)
{
gint flags = anode_def_flags (node);
if (!anode_decode_anything_for_flags (node, tlv, flags))
return anode_decode_option_or_default (node, tlv, flags);
return TRUE;
}
gboolean
egg_asn1x_decode (GNode *asn, gconstpointer data, gsize n_data)
{
Atlv tlv;
g_return_val_if_fail (asn, FALSE);
g_return_val_if_fail (data, FALSE);
g_return_val_if_fail (n_data, FALSE);
egg_asn1x_clear (asn);
if (!anode_decode_tlv_for_data (data, (const guchar*)data + n_data, &tlv))
return anode_failure (asn, "content is not encoded properly");
if (!anode_decode_anything (asn, &tlv))
return FALSE;
if (tlv.end - tlv.buf != n_data)
return FALSE;
return egg_asn1x_validate (asn);
}
/* -----------------------------------------------------------------------------------
* ENCODING
*/
static void
anode_encode_length (gulong len, guchar *ans, gint *cb)
{
guchar temp[sizeof (gulong)];
gint k;
g_assert (cb);
/* short form */
if (len < 128) {
if (ans != NULL)
ans[0] = (unsigned char)len;
*cb = 1;
/* Long form */
} else {
k = 0;
while (len) {
temp[k++] = len & 0xFF;
len = len >> 8;
}
*cb = k + 1;
if (ans != NULL) {
ans[0] = ((unsigned char) k & 0x7F) + 128;
while (k--)
ans[*cb - 1 - k] = temp[k];
}
}
}
static gint
anode_encode_cls_tag_len (guchar *data, gsize n_data, guchar cls,
gulong tag, gint len)
{
guchar temp[sizeof(gulong)];
gint cb;
gint off = 0;
gint k;
/* Short form */
if (tag < 31) {
off += 1;
if (data) {
g_assert (n_data >= off);
data[0] = (cls & 0xE0) + ((guchar) (tag & 0x1F));
}
/* Long form */
} else {
k = 0;
while (tag) {
temp[k++] = tag & 0x7F;
tag = tag >> 7;
}
off = k + 1;
if (data) {
g_assert (n_data >= off);
data[0] = (cls & 0xE0) + 31;
while (data && k--)
data[off - 1 - k] = temp[k] + 128;
data[off - 1] -= 128;
}
}
/* And now the length */
cb = n_data - off;
anode_encode_length (len, data ? data + off : NULL, &cb);
off += cb;
g_assert (!data || n_data >= off);
return off;
}
static void
anode_encode_tlv_and_enc (GNode *node, gsize n_data, EggAsn1xEncoder encoder,
gpointer user_data, GDestroyNotify destroy)
{
gboolean explicit = FALSE;
gulong tag;
gint flags;
Atlv tlv;
g_assert (node);
g_assert (encoder);
/* The data length */
memset (&tlv, 0, sizeof (tlv));
tlv.len = n_data;
/* Figure out the basis if the class */
switch (anode_def_type (node)) {
case TYPE_INTEGER:
case TYPE_BOOLEAN:
case TYPE_BIT_STRING:
case TYPE_OCTET_STRING:
case TYPE_OBJECT_ID:
case TYPE_TIME:
case TYPE_ENUMERATED:
case TYPE_GENERALSTRING:
case TYPE_NULL:
tlv.cls = ASN1_CLASS_UNIVERSAL;
break;
/* Container types */
case TYPE_SEQUENCE:
case TYPE_SET:
case TYPE_SEQUENCE_OF:
case TYPE_SET_OF:
tlv.cls = (ASN1_CLASS_STRUCTURED | ASN1_CLASS_UNIVERSAL);
break;
/* Transparent types shouldn't get here */
case TYPE_ANY:
case TYPE_CHOICE:
g_return_if_reached ();
default:
g_return_if_reached ();
};
/* Build up the class */
flags = anode_def_flags (node);
if (flags & FLAG_TAG) {
explicit = anode_calc_explicit_for_flags (node, flags);
if (explicit)
flags &= ~FLAG_TAG;
else
tlv.cls |= ASN1_CLASS_CONTEXT_SPECIFIC;
}
/* And now the tag */
tlv.tag = anode_calc_tag_for_flags (node, flags);
/* Calculate the length for the main thingy */
tlv.off = anode_encode_cls_tag_len (NULL, 0, tlv.cls, tlv.tag, tlv.len);
/* Wrap that in another explicit tag if necessary */
if (explicit) {
tag = anode_calc_tag (node);
g_return_if_fail (tag != G_MAXULONG);
tlv.oft = anode_encode_cls_tag_len (NULL, 0, 0, tag, tlv.off + tlv.len);
tlv.off += tlv.oft;
}
/* Not completely filled in */
tlv.buf = tlv.end = NULL;
anode_clear (node);
anode_set_tlv_data (node, &tlv);
anode_set_enc_data (node, encoder, user_data);
anode_set_user_data (node, user_data, destroy);
}
static gboolean
anode_encode_build (GNode *node, guchar *data, gsize n_data)
{
gint type;
guchar cls;
gulong tag;
Aenc *enc;
Atlv *tlv;
gint off = 0;
type = anode_def_type (node);
tlv = anode_get_tlv_data (node);
g_return_val_if_fail (tlv, FALSE);
/* Should have an encoder */
enc = anode_get_enc_data (node);
g_return_val_if_fail (enc, FALSE);
/* If it's a choice node, use the choice for calculations */
if (type == TYPE_CHOICE) {
node = egg_asn1x_get_choice (node);
g_return_val_if_fail (node, FALSE);
}
/* Encode any explicit tag */
if (anode_calc_explicit (node)) {
tag = anode_calc_tag (node);
g_return_val_if_fail (tag != G_MAXULONG, FALSE);
cls = (ASN1_CLASS_STRUCTURED | ASN1_CLASS_CONTEXT_SPECIFIC);
g_assert (tlv->oft > 0 && tlv->oft < tlv->off);
off = anode_encode_cls_tag_len (data, n_data, cls, tag, (tlv->off - tlv->oft) + tlv->len);
g_assert (off == tlv->oft);
}
/* Now encode the main tag */
off += anode_encode_cls_tag_len (data + off, n_data - off, tlv->cls, tlv->tag, tlv->len);
g_assert (off == tlv->off);
/* Setup the remainder of the tlv */
g_assert (tlv->len + tlv->off == n_data);
tlv->buf = data;
tlv->end = data + n_data;
/* Encode in the data */
if (!(enc->encoder) (enc->data, data + tlv->off, tlv->len))
return FALSE;
return TRUE;
}
static void
anode_encode_rollback (GNode *node)
{
GNode *child;
Aenc *enc;
Atlv *tlv;
/* Undo any references to our new buffer */
enc = anode_get_enc_data (node);
if (enc) {
tlv = anode_get_tlv_data (node);
g_return_if_fail (tlv);
tlv->buf = tlv->end = NULL;
}
for (child = node->children; child; child = child->next)
anode_encode_rollback (child);
}
static void
anode_encode_commit (GNode *node)
{
GNode *child;
/* Remove and free all the encoder stuff */
anode_clr_enc_data (node);
for (child = node->children; child; child = child->next)
anode_encode_commit (child);
}
static gint
compare_bufs (gconstpointer a, gconstpointer b)
{
const Abuf *ba = a;
const Abuf *bb = b;
gint res = memcmp (ba->data, bb->data, MIN (ba->n_data, bb->n_data));
if (ba->n_data == bb->n_data || res != 0)
return res;
return ba->n_data < bb->n_data ? -1 : 1;
}
static gboolean
traverse_and_sort_set_of (GNode *node, gpointer user_data)
{
EggAllocator allocator = user_data;
GList *bufs, *l;
Abuf *buf;
guchar *data;
gsize n_data;
Atlv *tlv;
GNode *child;
GNode *next;
g_assert (allocator);
/* We have to sort any SET OF :( */
if (anode_def_type (node) != TYPE_SET_OF)
return FALSE;
bufs = NULL;
for (child = node->children; child; child = next) {
next = child->next;
tlv = anode_get_tlv_data (child);
if (!tlv)
continue;
/* Allocate enough memory */
n_data = tlv->len + tlv->off;
data = (allocator) (NULL, n_data + 1);
if (!data)
break;
if (!anode_encode_build (child, data, n_data)) {
(allocator) (data, 0);
continue;
}
buf = g_slice_new0 (Abuf);
buf->user_data = child;
buf->n_data = n_data;
buf->data = data;
bufs = g_list_prepend (bufs, buf);
g_node_unlink (child);
}
bufs = g_list_sort (bufs, compare_bufs);
for (l = bufs; l; l = g_list_next (l)) {
buf = l->data;
g_node_append (node, buf->user_data);
(allocator) (buf->data, 0);
g_slice_free (Abuf, buf);
}
anode_encode_rollback (node);
g_list_free (bufs);
return FALSE;
}
static gboolean
anode_encoder_simple (gpointer user_data, guchar *data, gsize n_data)
{
memcpy (data, user_data, n_data);
return TRUE;
}
static gboolean
anode_encoder_structured (gpointer user_data, guchar *data, gsize n_data)
{
GNode *node = user_data;
GNode *child;
gsize length;
Atlv *tlv;
for (child = node->children; child; child = child->next) {
tlv = anode_get_tlv_data (child);
if (tlv) {
length = tlv->off + tlv->len;
g_assert (length <= n_data);
if (!anode_encode_build (child, data, length))
return FALSE;
data += length;
n_data -= length;
}
}
return TRUE;
}
static gboolean
anode_encoder_choice (gpointer user_data, guchar *data, gsize n_data)
{
GNode *node = user_data;
Aenc *enc = NULL;
GNode *child;
Atlv *tlv, *ctlv;
tlv = anode_get_tlv_data (node);
g_return_val_if_fail (tlv, FALSE);
child = egg_asn1x_get_choice (node);
g_return_val_if_fail (child, FALSE);
ctlv = anode_get_tlv_data (child);
g_assert (ctlv);
enc = anode_get_enc_data (child);
g_return_val_if_fail (enc, FALSE);
if (!(enc->encoder) (enc->data, data, n_data))
return FALSE;
/* Child's buffer matches ours */
ctlv->buf = tlv->buf;
ctlv->end = tlv->end;
return TRUE;
}
static gboolean
anode_encoder_bit_string (gpointer user_data, guchar *data, gsize n_data)
{
Abits *ab = user_data;
guchar empty, mask;
gsize len;
empty = ab->n_bits % 8;
if (empty > 0)
empty = 8 - empty;
len = (ab->n_bits / 8) + (empty ? 1 : 0);
g_assert (n_data == len + 1);
/* Fill in the amount of empty */
data[0] = empty;
data += 1;
/* Fill in the actual data */
memcpy (data, ab->bits, len);
/* Set the extra bits to zero */
if (len && empty) {
mask = 0xFF >> (8 - empty);
data[len - 1] &= ~mask;
}
return TRUE;
}
static gboolean
anode_encode_prepare_simple (GNode *node, gboolean want)
{
Aenc *enc;
Atlv *tlv;
tlv = anode_get_tlv_data (node);
if (tlv == NULL)
return FALSE;
/* Transfer the tlv data over to enc */
enc = anode_get_enc_data (node);
if (enc == NULL)
anode_set_enc_data (node, anode_encoder_simple, (guchar*)tlv->buf + tlv->off);
tlv->buf = tlv->end = NULL;
return TRUE;
}
static gboolean
anode_encode_prepare_choice (GNode *node, gboolean want)
{
Atlv *tlv;
GNode *child;
g_assert (anode_def_type (node) == TYPE_CHOICE);
child = egg_asn1x_get_choice (node);
if (!child)
return FALSE;
if (!anode_encode_prepare (child, want))
return FALSE;
tlv = anode_get_tlv_data (child);
g_return_val_if_fail (tlv, FALSE);
anode_clr_tlv_data (node);
anode_set_tlv_data (node, tlv);
anode_set_enc_data (node, anode_encoder_choice, node);
return TRUE;
}
static gboolean
anode_encode_prepare_structured (GNode *node, gboolean want)
{
gboolean child_want;
gsize length;
gboolean had;
Atlv *tlv;
GNode *child;
gint type;
type = anode_def_type (node);
child_want = want;
had = FALSE;
length = 0;
if (type == TYPE_SEQUENCE_OF || type == TYPE_SET_OF)
child_want = FALSE;
if (anode_def_flags (node) & FLAG_OPTION)
want = FALSE;
for (child = node->children; child; child = child->next) {
if (anode_encode_prepare (child, child_want)) {
tlv = anode_get_tlv_data (child);
g_return_val_if_fail (tlv, FALSE);
length += tlv->off + tlv->len;
had = TRUE;
}
}
if (had == FALSE) {
/* See if we should encode an empty set or seq of */
if (type == TYPE_SEQUENCE_OF || type == TYPE_SET_OF) {
if (!want)
return FALSE;
} else {
return FALSE;
}
}
anode_encode_tlv_and_enc (node, length, anode_encoder_structured, node, NULL);
return TRUE;
}
static gboolean
anode_encode_prepare (GNode *node, gboolean want)
{
switch (anode_def_type (node)) {
case TYPE_INTEGER:
case TYPE_BOOLEAN:
case TYPE_BIT_STRING:
case TYPE_OCTET_STRING:
case TYPE_OBJECT_ID:
case TYPE_TIME:
case TYPE_ENUMERATED:
case TYPE_GENERALSTRING:
case TYPE_ANY:
case TYPE_NULL:
return anode_encode_prepare_simple (node, want);
break;
case TYPE_SEQUENCE:
case TYPE_SEQUENCE_OF:
case TYPE_SET:
case TYPE_SET_OF:
return anode_encode_prepare_structured (node, want);
break;
case TYPE_CHOICE:
return anode_encode_prepare_choice (node, want);
break;
default:
g_return_val_if_reached (FALSE);
};
}
gpointer
egg_asn1x_encode (GNode *asn, EggAllocator allocator, gsize *n_data)
{
guchar *data;
gsize length;
Atlv *tlv;
g_return_val_if_fail (asn, NULL);
g_return_val_if_fail (n_data, NULL);
g_return_val_if_fail (anode_def_type_is_real (asn), NULL);
if (!allocator)
allocator = g_realloc;
if (!anode_encode_prepare (asn, TRUE)) {
anode_failure (asn, "missing value(s)");
return NULL;
}
/* We must sort all the nasty SET OF nodes */
g_node_traverse (asn, G_POST_ORDER, G_TRAVERSE_ALL, -1,
traverse_and_sort_set_of, allocator);
tlv = anode_get_tlv_data (asn);
g_return_val_if_fail (tlv, NULL);
/* Allocate enough memory for entire thingy */
length = tlv->off + tlv->len;
data = (allocator) (NULL, length + 1);
if (data == NULL)
return NULL;
if (anode_encode_build (asn, data, length) &&
anode_validate_anything (asn)) {
anode_encode_commit (asn);
*n_data = length;
return data;
}
(allocator) (data, 0);
anode_encode_rollback (asn);
return NULL;
}
/* -----------------------------------------------------------------------------------
* READING, WRITING, GETTING, SETTING
*/
static int
two_to_four_digit_year (int year)
{
time_t now;
struct tm tm;
int century, current;
g_return_val_if_fail (year >= 0 && year <= 99, -1);
/* Get the current year */
now = time (NULL);
g_return_val_if_fail (now >= 0, -1);
if (!gmtime_r (&now, &tm))
g_return_val_if_reached (-1);
current = (tm.tm_year % 100);
century = (tm.tm_year + 1900) - current;
/*
* Check if it's within 40 years before the
* current date.
*/
if (current < 40) {
if (year < current)
return century + year;
if (year > 100 - (40 - current))
return (century - 100) + year;
} else {
if (year < current && year > (current - 40))
return century + year;
}
/*
* If it's after then adjust for overflows to
* the next century.
*/
if (year < current)
return century + 100 + year;
else
return century + year;
}
#ifndef HAVE_TIMEGM
time_t timegm(struct tm *t)
{
time_t tl, tb;
struct tm *tg;
tl = mktime (t);
if (tl == -1)
{
t->tm_hour--;
tl = mktime (t);
if (tl == -1)
return -1; /* can't deal with output from strptime */
tl += 3600;
}
tg = gmtime (&tl);
tg->tm_isdst = 0;
tb = mktime (tg);
if (tb == -1)
{
tg->tm_hour--;
tb = mktime (tg);
if (tb == -1)
return -1; /* can't deal with output from gmtime */
tb += 3600;
}
return (tl - (tb - tl));
}
#endif // NOT_HAVE_TIMEGM
static gboolean
parse_utc_time (const gchar *time, gsize n_time,
struct tm* when, gint *offset)
{
const char *p, *e;
int year;
g_assert (when);
g_assert (time);
g_assert (offset);
/* YYMMDDhhmmss.ffff Z | +0000 */
if (n_time < 6 || n_time >= 28)
return FALSE;
/* Reset everything to default legal values */
memset (when, 0, sizeof (*when));
*offset = 0;
when->tm_mday = 1;
/* Select the digits part of it */
p = time;
for (e = p; *e >= '0' && *e <= '9'; ++e);
if (p + 2 <= e) {
year = atoin (p, 2);
p += 2;
/*
* 40 years in the past is our century. 60 years
* in the future is the next century.
*/
when->tm_year = two_to_four_digit_year (year) - 1900;
}
if (p + 2 <= e) {
when->tm_mon = atoin (p, 2) - 1;
p += 2;
}
if (p + 2 <= e) {
when->tm_mday = atoin (p, 2);
p += 2;
}
if (p + 2 <= e) {
when->tm_hour = atoin (p, 2);
p += 2;
}
if (p + 2 <= e) {
when->tm_min = atoin (p, 2);
p += 2;
}
if (p + 2 <= e) {
when->tm_sec = atoin (p, 2);
p += 2;
}
if (when->tm_year < 0 || when->tm_year > 9999 ||
when->tm_mon < 0 || when->tm_mon > 11 ||
when->tm_mday < 1 || when->tm_mday > 31 ||
when->tm_hour < 0 || when->tm_hour > 23 ||
when->tm_min < 0 || when->tm_min > 59 ||
when->tm_sec < 0 || when->tm_sec > 59)
return FALSE;
/* Make sure all that got parsed */
if (p != e)
return FALSE;
/* Now the remaining optional stuff */
e = time + n_time;
/* See if there's a fraction, and discard it if so */
if (p < e && *p == '.' && p + 5 <= e)
p += 5;
/* See if it's UTC */
if (p < e && *p == 'Z') {
p += 1;
/* See if it has a timezone */
} else if ((*p == '-' || *p == '+') && p + 3 <= e) {
int off, neg;
neg = *p == '-';
++p;
off = atoin (p, 2) * 3600;
if (off < 0 || off > 86400)
return -1;
p += 2;
if (p + 2 <= e) {
off += atoin (p, 2) * 60;
p += 2;
}
/* Use TZ offset */
if (neg)
*offset = 0 - off;
else
*offset = off;
}
/* Make sure everything got parsed */
if (p != e)
return FALSE;
return TRUE;
}
static gboolean
parse_general_time (const gchar *time, gsize n_time,
struct tm* when, gint *offset)
{
const char *p, *e;
g_assert (time);
g_assert (when);
g_assert (offset);
/* YYYYMMDDhhmmss.ffff Z | +0000 */
if (n_time < 8 || n_time >= 30)
return FALSE;
/* Reset everything to default legal values */
memset (when, 0, sizeof (*when));
*offset = 0;
when->tm_mday = 1;
/* Select the digits part of it */
p = time;
for (e = p; *e >= '0' && *e <= '9'; ++e);
if (p + 4 <= e) {
when->tm_year = atoin (p, 4) - 1900;
p += 4;
}
if (p + 2 <= e) {
when->tm_mon = atoin (p, 2) - 1;
p += 2;
}
if (p + 2 <= e) {
when->tm_mday = atoin (p, 2);
p += 2;
}
if (p + 2 <= e) {
when->tm_hour = atoin (p, 2);
p += 2;
}
if (p + 2 <= e) {
when->tm_min = atoin (p, 2);
p += 2;
}
if (p + 2 <= e) {
when->tm_sec = atoin (p, 2);
p += 2;
}
if (when->tm_year < 0 || when->tm_year > 9999 ||
when->tm_mon < 0 || when->tm_mon > 11 ||
when->tm_mday < 1 || when->tm_mday > 31 ||
when->tm_hour < 0 || when->tm_hour > 23 ||
when->tm_min < 0 || when->tm_min > 59 ||
when->tm_sec < 0 || when->tm_sec > 59)
return FALSE;
/* Make sure all that got parsed */
if (p != e)
return FALSE;
/* Now the remaining optional stuff */
e = time + n_time;
/* See if there's a fraction, and discard it if so */
if (p < e && *p == '.' && p + 5 <= e)
p += 5;
/* See if it's UTC */
if (p < e && *p == 'Z') {
p += 1;
/* See if it has a timezone */
} else if ((*p == '-' || *p == '+') && p + 3 <= e) {
int off, neg;
neg = *p == '-';
++p;
off = atoin (p, 2) * 3600;
if (off < 0 || off > 86400)
return -1;
p += 2;
if (p + 2 <= e) {
off += atoin (p, 2) * 60;
p += 2;
}
/* Use TZ offset */
if (neg)
*offset = 0 - off;
else
*offset = off;
}
/* Make sure everything got parsed */
if (p != e)
return FALSE;
return TRUE;
}
static gboolean
anode_read_time (GNode *node, Atlv *tlv, struct tm *when, glong *value)
{
const gchar *data;
gboolean ret;
gint offset = 0;
gint flags;
g_assert (when);
g_assert (value);
flags = anode_def_flags (node);
data = (gchar*)(tlv->buf + tlv->off);
if (flags & FLAG_GENERALIZED)
ret = parse_general_time (data, tlv->len, when, &offset);
else if (flags & FLAG_UTC)
ret = parse_utc_time (data, tlv->len, when, &offset);
else
g_return_val_if_reached (FALSE);
if (!ret)
return anode_failure (node, "invalid time content");
/* In order to work with 32 bit time_t. */
if (sizeof (time_t) <= 4 && when->tm_year >= 2038) {
*value = (time_t)2145914603; /* 2037-12-31 23:23:23 */
/* Convert to seconds since epoch */
} else {
*value = timegm (when);
g_return_val_if_fail (*time >= 0, FALSE);
*value += offset;
}
return TRUE;
}
static gboolean
anode_read_integer_as_ulong (GNode *node, Atlv *tlv, gulong *value)
{
const guchar *p;
gsize k;
if (tlv->len < 1 || tlv->len > sizeof (gulong))
return FALSE;
p = tlv->buf + tlv->off;
*value = 0;
for (k = 0; k < tlv->len; ++k)
*value |= p[k] << (8 * ((tlv->len - 1) - k));
return TRUE;
}
static gboolean
anode_write_integer_ulong (gulong value, guchar *data, gsize *n_data)
{
guchar buf[sizeof (gulong)];
gint bytes, i, off;
for (i = 0; i < sizeof (gulong); ++i) {
off = sizeof (gulong) - (i + 1);
buf[i] = (value >> (off * 8)) & 0xFF;
}
for (bytes = sizeof (gulong) - 1; bytes >= 0; --bytes)
if (!buf[bytes])
break;
bytes = sizeof (gulong) - (bytes + 1);
if (bytes == 0)
bytes = 1;
if (data) {
g_assert (*n_data >= bytes);
memcpy (data, buf + (sizeof (gulong) - bytes), bytes);
}
*n_data = bytes;
return TRUE;
}
static gboolean
anode_read_string (GNode *node, Atlv *tlv, gpointer value, gsize *n_value)
{
Atlv ctlv;
guchar *buf;
gint n_buf;
gint i;
g_assert (tlv);
g_assert (n_value);
buf = value;
n_buf = *n_value;
/* Is it constructed ? */
if (tlv->cls & ASN1_CLASS_STRUCTURED) {
*n_value = 0;
for (i = 0; TRUE; ++i) {
if (!anode_decode_tlv_for_contents (tlv, i == 0, &ctlv))
return anode_failure (node, "invalid encoding of child");
if (ctlv.off == 0)
break;
if (ctlv.cls & ASN1_CLASS_STRUCTURED)
return FALSE;
*n_value += ctlv.len;
if (buf) {
if (n_buf >= ctlv.len)
memcpy (buf, ctlv.buf + ctlv.off, ctlv.len);
buf += ctlv.len;
n_buf -= ctlv.len;
}
}
if (n_buf < 0)
return FALSE;
/* Primitive, just return the contents */
} else {
*n_value = tlv->len;
if (buf) {
if (n_buf < tlv->len)
return FALSE;
memcpy (buf, tlv->buf + tlv->off, tlv->len);
}
}
return TRUE;
}
static gboolean
anode_read_boolean (GNode *node, Atlv *tlv, gboolean *value)
{
g_assert (node);
g_assert (tlv);
g_assert (value);
if (tlv->len != 1)
return FALSE;
if (tlv->buf[tlv->off] == 0x00)
*value = FALSE;
else if (tlv->buf[tlv->off] == 0xFF)
*value = TRUE;
else
return FALSE;
return TRUE;
}
static gboolean
anode_write_boolean (gboolean value, guchar *data, gsize *n_data)
{
if (data) {
g_assert (*n_data >= 1);
if (value)
data[0] = 0xFF;
else
data[0] = 0x00;
}
*n_data = 1;
return TRUE;
}
static gboolean
anode_read_object_id (GNode *node, Atlv *tlv, gchar **oid)
{
GString *result = NULL;
const guchar *p;
gboolean lead;
guint val, pval;
gint k;
g_assert (tlv);
if (tlv->len <= 0)
return FALSE;
p = tlv->buf + tlv->off;
if (oid)
result = g_string_sized_new (32);
pval = p[0] / 40;
val = p[0] - pval * 40;
if (result)
g_string_append_printf (result, "%u.%u", pval, val);
/* TODO: Validate first byte? */
for (k = 1, lead = 1, val = 0, pval = 0; k < tlv->len; ++k) {
/* X.690: the leading byte must never be 0x80 */
if (lead && p[k] == 0x80) {
anode_failure (node, "object id encoding is invalid");
break;
}
val = val << 7;
val |= p[k] & 0x7F;
/* Check for wrap around */
if (val < pval) {
anode_failure (node, "object id encoding is invalid");
break;
}
pval = val;
if (!(p[k] & 0x80)) {
if (result)
g_string_append_printf (result, ".%u", val);
pval = val = 0;
lead = 1;
}
}
if (k < tlv->len) {
if (result)
g_string_free (result, TRUE);
return FALSE;
}
if (result)
*oid = g_string_free (result, FALSE);
return TRUE;
}
static gboolean
anode_write_oid (const gchar *oid, guchar *data, gsize *n_data)
{
const gchar *p, *next;
gint num, num1;
guchar bit7;
gboolean had;
gint i, k, at;
p = oid;
at = 0;
num1 = 0;
for (i = 0; oid[0]; ++i, oid = next) {
p = strchr (oid, '.');
if (p == NULL)
next = p = oid + strlen (oid);
else
next = p + 1;
if (p == oid)
return FALSE;
num = atoin (oid, p - oid);
if (num < 0)
return FALSE;
if (i == 0) {
num1 = num;
} else if (i == 1) {
if (data) {
g_assert (*n_data > at);
data[at] = 40 * num1 + num;
}
++at;
} else {
for (had = FALSE, k = 4; k >= 0; k--) {
bit7 = (num >> (k * 7)) & 0x7F;
if (bit7 || had || !k) {
if (k)
bit7 |= 0x80;
if (data) {
g_assert (*n_data > at);
data[at] = bit7;
}
++at;
had = 1;
}
}
}
}
if (at < 2)
return FALSE;
if (data)
g_assert (*n_data >= at);
*n_data = at;
return TRUE;
}
GNode*
egg_asn1x_node (GNode *asn, ...)
{
GNode *node = asn;
const gchar *name;
va_list va;
gint type;
gint index;
g_return_val_if_fail (asn, NULL);
va_start (va, asn);
for (;;) {
type = anode_def_type (node);
/* Use integer indexes for these */
if (type == TYPE_SEQUENCE_OF || type == TYPE_SET_OF) {
index = va_arg (va, gint);
if (index == 0)
return node;
/* Only consider nodes that have data */
node = g_node_nth_child (node, 0);
while (node) {
if (egg_asn1x_have (node)) {
--index;
if (index == 0)
break;
}
node = g_node_next_sibling (node);
}
if (node == NULL)
return NULL;
/* Use strings for these */
} else {
name = va_arg (va, const gchar*);
if (name == NULL)
return node;
/* Warn if they're using indexes here */
if (name <= (const gchar*)4096) {
g_warning ("possible misuse of egg_asn1x_node, expected a string, but got an index");
return NULL;
}
node = anode_child_with_name (node, name);
if (node == NULL)
return NULL;
}
}
}
const gchar*
egg_asn1x_name (GNode *node)
{
g_return_val_if_fail (node, NULL);
return anode_def_name (node);
}
guint
egg_asn1x_count (GNode *node)
{
guint result = 0;
GNode *child;
gint type;
g_return_val_if_fail (node, 0);
type = anode_def_type (node);
if (type != TYPE_SEQUENCE_OF && type != TYPE_SET_OF) {
g_warning ("node passed to egg_asn1x_count was not a sequence of or set of");
return 0;
}
for (child = node->children; child; child = child->next) {
if (egg_asn1x_have (child))
++result;
}
return result;
}
GNode*
egg_asn1x_append (GNode *node)
{
GNode *child;
gint type;
g_return_val_if_fail (node, NULL);
type = anode_def_type (node);
if (type != TYPE_SEQUENCE_OF && type != TYPE_SET_OF) {
g_warning ("node passed to egg_asn1x_append was not a sequence of or set of");
return NULL;
}
/* There must be at least one child */
child = node->children;
g_return_val_if_fail (child, NULL);
child = anode_clone (child);
anode_clear (child);
g_node_append (node, child);
return child;
}
gboolean
egg_asn1x_have (GNode *node)
{
Atlv *tlv;
g_return_val_if_fail (node, FALSE);
/* TODO: Handle default values */
tlv = anode_get_tlv_data (node);
return tlv != NULL && tlv->buf != NULL;
}
gboolean
egg_asn1x_get_boolean (GNode *node, gboolean *value)
{
ASN1_ARRAY_TYPE *opt;
Atlv *tlv;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (value, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_BOOLEAN, FALSE);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL) {
if ((anode_def_flags (node) & FLAG_DEFAULT) == 0)
return FALSE;
/* Try to get a default */
opt = anode_opt_lookup (node, TYPE_DEFAULT, NULL);
g_return_val_if_fail (opt, FALSE);
/* Parse out the default value */
if ((opt->type & FLAG_TRUE) == FLAG_TRUE)
*value = TRUE;
else if ((opt->type & FLAG_FALSE) == FLAG_FALSE)
*value = FALSE;
else
g_return_val_if_reached (FALSE);
return TRUE;
}
return anode_read_boolean (node, tlv, value);
}
gboolean
egg_asn1x_set_boolean (GNode *node, gboolean value)
{
guchar *data;
gsize n_data;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_BOOLEAN, FALSE);
/* TODO: Handle default values */
n_data = 1;
data = g_malloc0 (1);
if (!anode_write_boolean (value, data, &n_data))
return FALSE;
anode_encode_tlv_and_enc (node, n_data, anode_encoder_simple, data, g_free);
return TRUE;
}
gboolean
egg_asn1x_set_null (GNode *node)
{
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_NULL, FALSE);
/* Encode zero characters */
anode_encode_tlv_and_enc (node, 0, anode_encoder_simple, "", NULL);
return TRUE;
}
GQuark
egg_asn1x_get_enumerated (GNode *node)
{
gchar buf[sizeof (gulong) * 3];
ASN1_ARRAY_TYPE *opt;
gulong val;
Atlv *tlv;
g_return_val_if_fail (node, 0);
g_return_val_if_fail (anode_def_type (node) == TYPE_ENUMERATED, 0);
tlv = anode_get_tlv_data (node);
/* TODO: Defaults */
if (tlv == NULL || tlv->buf == NULL)
return 0;
/* TODO: Signed values */
if (!anode_read_integer_as_ulong (node, tlv, &val))
return 0;
/* Format that as a string */
if (g_snprintf (buf, sizeof (buf), "%lu", val) < 0)
g_return_val_if_reached (0);
/* Lookup that value in our table */
opt = anode_opt_lookup_value (node, TYPE_CONSTANT, buf);
if (opt == NULL || opt->name == NULL)
return 0;
return g_quark_from_static_string (opt->name);
}
gboolean
egg_asn1x_set_enumerated (GNode *node, GQuark value)
{
ASN1_ARRAY_TYPE *opt;
const gchar *name;
gpointer data;
gsize n_data;
gulong val;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (value, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_ENUMERATED, FALSE);
/* TODO: Handle default values */
name = g_quark_to_string (value);
g_return_val_if_fail (name, FALSE);
opt = anode_opt_lookup (node, TYPE_CONSTANT, name);
g_return_val_if_fail (opt && opt->value, FALSE);
/* TODO: Signed values */
val = anode_def_value_as_ulong (opt);
g_return_val_if_fail (val != G_MAXULONG, FALSE);
n_data = sizeof (gulong);
data = g_malloc0 (n_data);
if (!anode_write_integer_ulong (val, data, &n_data))
return FALSE;
anode_encode_tlv_and_enc (node, n_data, anode_encoder_simple, data, g_free);
return TRUE;
}
gboolean
egg_asn1x_get_integer_as_ulong (GNode *node, gulong *value)
{
const ASN1_ARRAY_TYPE *opt;
const gchar *defval;
Atlv *tlv;
gchar *end;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (value, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_INTEGER, FALSE);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL) {
if ((anode_def_flags (node) & FLAG_DEFAULT) == 0)
return FALSE;
/* Try to get a default */
opt = anode_opt_lookup (node, TYPE_DEFAULT, NULL);
g_return_val_if_fail (opt, FALSE);
g_return_val_if_fail (opt->value, FALSE);
defval = opt->value;
opt = anode_opt_lookup (node, TYPE_CONSTANT, defval);
if (opt != NULL) {
g_return_val_if_fail (opt->value, FALSE);
defval = opt->value;
}
/* Parse out the default value */
*value = strtoul (defval, &end, 10);
g_return_val_if_fail (end && !end[0], FALSE);
return TRUE;
}
return anode_read_integer_as_ulong (node, tlv, value);
}
gboolean
egg_asn1x_set_integer_as_ulong (GNode *node, gulong value)
{
guchar *data;
gsize n_data;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_INTEGER, FALSE);
/* TODO: Handle default values */
n_data = sizeof (gulong);
data = g_malloc0 (n_data);
if (!anode_write_integer_ulong (value, data, &n_data))
return FALSE;
anode_encode_tlv_and_enc (node, n_data, anode_encoder_simple, data, g_free);
return TRUE;
}
gpointer
egg_asn1x_get_integer_as_raw (GNode *node, EggAllocator allocator, gsize *n_data)
{
Atlv *tlv;
gpointer data;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (n_data, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_INTEGER, FALSE);
if (!allocator)
allocator = g_realloc;
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return NULL;
data = (allocator) (NULL, tlv->len);
if (data == NULL)
return NULL;
memcpy (data, tlv->buf + tlv->off, tlv->len);
*n_data = tlv->len;
return data;
}
gboolean
egg_asn1x_set_integer_as_raw (GNode *node, gconstpointer data, gsize n_data, GDestroyNotify destroy)
{
gboolean sign;
guchar *p;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (data, FALSE);
g_return_val_if_fail (n_data > 0, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_INTEGER, FALSE);
/* Make sure the integer is properly encoded in twos complement*/
p = (guchar*)data;
sign = !!(p[0] & 0x80);
if (sign) {
g_warning ("integer in egg_asn1x_set_integer_as_raw is not two's complement");
return FALSE;
}
anode_encode_tlv_and_enc (node, n_data, anode_encoder_simple, (gpointer)data, destroy);
return TRUE;
}
gconstpointer
egg_asn1x_get_raw_element (GNode *node, gsize *n_element)
{
Atlv *tlv;
g_return_val_if_fail (node, NULL);
g_return_val_if_fail (n_element, NULL);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return NULL;
if (anode_calc_explicit (node)) {
*n_element = (tlv->len + tlv->off) - tlv->oft;
return tlv->buf + tlv->oft;
} else {
*n_element = tlv->len + tlv->off;
return tlv->buf;
}
}
gboolean
egg_asn1x_set_raw_element (GNode *node, gpointer data,
gsize n_data, GDestroyNotify destroy)
{
Atlv dtlv, *tlv;
gint oft, flags;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (data, FALSE);
g_return_val_if_fail (n_data, FALSE);
anode_clear (node);
memset (&dtlv, 0, sizeof (dtlv));
/* Decode the beginning TLV */
if (!anode_decode_tlv_for_data (data, (const guchar*)data + n_data, &dtlv))
return FALSE;
/*
* Decode the data into place properly, to make sure it fits. Note
* we are not decoding any explicit outer tagging, this is just
* the internal value. In addition we do not support optional
* and default values, which would decode successfully in
* unexpected ways.
*/
flags = anode_def_flags (node);
flags &= ~(FLAG_TAG | FLAG_DEFAULT | FLAG_OPTION);
if (!anode_decode_anything_for_flags (node, &dtlv, flags))
return FALSE;
/* There was extra data */
if (dtlv.end - dtlv.buf != n_data)
return FALSE;
/* Clear buffer from TLV so it gets encoded */
tlv = anode_get_tlv_data (node);
g_assert (tlv);
tlv->buf = tlv->end = NULL;
/* Explicit tagging: leave space for the outer tag */
if (anode_calc_explicit (node)) {
oft = anode_encode_cls_tag_len (NULL, 0, (ASN1_CLASS_STRUCTURED | ASN1_CLASS_CONTEXT_SPECIFIC),
anode_calc_tag (node), n_data);
tlv->off += oft;
tlv->oft = oft;
}
/* Setup encoding of the contents */
anode_set_enc_data (node, anode_encoder_simple, (gpointer)(dtlv.buf + dtlv.off));
anode_set_user_data (node, data, destroy);
return TRUE;
}
gconstpointer
egg_asn1x_get_raw_value (GNode *node, gsize *n_content)
{
Atlv *tlv;
g_return_val_if_fail (node, NULL);
g_return_val_if_fail (n_content, NULL);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return NULL;
g_return_val_if_fail (!(tlv->cls & ASN1_CLASS_STRUCTURED), NULL);
*n_content = tlv->len;
return tlv->buf + tlv->off;
}
gboolean
egg_asn1x_set_raw_value (GNode *node, gsize length, EggAsn1xEncoder encoder,
gpointer user_data, GDestroyNotify destroy)
{
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (encoder, FALSE);
anode_encode_tlv_and_enc (node, length, encoder, user_data, destroy);
return TRUE;
}
guchar*
egg_asn1x_get_string_as_raw (GNode *node, EggAllocator allocator, gsize *n_string)
{
gsize length;
guchar *string;
Atlv *tlv;
gint type;
g_return_val_if_fail (node, NULL);
g_return_val_if_fail (n_string, NULL);
if (!allocator)
allocator = g_realloc;
type = anode_def_type (node);
g_return_val_if_fail (type == TYPE_OCTET_STRING || type == TYPE_GENERALSTRING, NULL);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return NULL;
if (!anode_read_string (node, tlv, NULL, &length))
return NULL;
string = (allocator) (NULL, length + 1);
if (string == NULL)
return NULL;
if (!anode_read_string (node, tlv, string, &length)) {
(allocator) (string, 0);
return NULL;
}
/* Courtesy null termination, string must however be validated! */
string[length] = 0;
*n_string = length;
return string;
}
gboolean
egg_asn1x_set_string_as_raw (GNode *node, guchar *data, gsize n_data, GDestroyNotify destroy)
{
gint type;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (data, FALSE);
type = anode_def_type (node);
g_return_val_if_fail (type == TYPE_OCTET_STRING || type == TYPE_GENERALSTRING, FALSE);
anode_encode_tlv_and_enc (node, n_data, anode_encoder_simple, data, destroy);
return TRUE;
}
gchar*
egg_asn1x_get_string_as_utf8 (GNode *node, EggAllocator allocator)
{
gchar *string;
gsize n_string;
g_return_val_if_fail (node, NULL);
if (allocator == NULL)
allocator = g_realloc;
string = (gchar*)egg_asn1x_get_string_as_raw (node, allocator, &n_string);
if (!string)
return NULL;
if (!g_utf8_validate (string, n_string, NULL)) {
(allocator) (string, 0);
return NULL;
}
return string;
}
gboolean
egg_asn1x_set_string_as_utf8 (GNode *node, gchar *data, GDestroyNotify destroy)
{
gsize n_data;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (data, FALSE);
n_data = strlen (data);
if (!g_utf8_validate (data, n_data, NULL))
return FALSE;
return egg_asn1x_set_string_as_raw (node, (guchar*)data, n_data, destroy);
}
guchar*
egg_asn1x_get_bits_as_raw (GNode *node, EggAllocator allocator, guint *n_bits)
{
Atlv *tlv;
gpointer bits;
guchar padded;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (n_bits, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_BIT_STRING, FALSE);
if (!allocator)
allocator = g_realloc;
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return NULL;
padded = *(tlv->buf + tlv->off);
g_return_val_if_fail (padded < 8, NULL);
g_return_val_if_fail (tlv->len > 1, NULL);
bits = (allocator) (NULL, tlv->len);
if (bits == NULL)
return NULL;
memcpy (bits, tlv->buf + tlv->off + 1, tlv->len - 1);
*n_bits = ((tlv->len - 1) * 8) - padded;
return bits;
}
gboolean
egg_asn1x_set_bits_as_raw (GNode *node, guchar *bits, guint n_bits, GDestroyNotify destroy)
{
gint type;
gsize length;
Abits *ab;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (bits, FALSE);
type = anode_def_type (node);
g_return_val_if_fail (type == TYPE_BIT_STRING, FALSE);
length = (n_bits / 8);
if (n_bits % 8)
length += 1;
ab = g_slice_new0 (Abits);
ab->bits = bits;
ab->n_bits = n_bits;
ab->destroy = destroy;
anode_encode_tlv_and_enc (node, length + 1, anode_encoder_bit_string, ab, abits_destroy);
return TRUE;
}
gboolean
egg_asn1x_get_bits_as_ulong (GNode *node, gulong *bits, guint *n_bits)
{
Atlv *tlv;
guint i, length;
guchar empty;
const guchar *p;
gulong value;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (bits, FALSE);
g_return_val_if_fail (n_bits, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_BIT_STRING, FALSE);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return FALSE;
empty = *(tlv->buf + tlv->off);
g_return_val_if_fail (empty < 8, FALSE);
g_return_val_if_fail (tlv->len > 1, FALSE);
length = ((tlv->len - 1) * 8) - empty;
if (length > sizeof (gulong) * 8)
return FALSE;
value = 0;
p = tlv->buf + tlv->off + 1;
for (i = 0; i < tlv->len - 1; ++i)
value = value << 8 | p[i];
*bits = value >> empty;
*n_bits = length;
return TRUE;
}
gboolean
egg_asn1x_set_bits_as_ulong (GNode *node, gulong bits, guint n_bits)
{
guchar *data;
gulong value;
gint type;
gsize i, length;
guchar empty;
Abits *ab;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (bits, FALSE);
g_return_val_if_fail (n_bits <= sizeof (gulong) * 8, FALSE);
type = anode_def_type (node);
g_return_val_if_fail (type == TYPE_BIT_STRING, FALSE);
empty = n_bits % 8;
if (empty > 0)
empty = 8 - empty;
length = (n_bits / 8) + (empty ? 1 : 0);
data = g_malloc0 (sizeof (gulong));
value = bits << empty;
for (i = 0; i < length; ++i)
data[(length - i) - 1] = (value >> i * 8) & 0xFF;
ab = g_slice_new0 (Abits);
ab->bits = data;
ab->n_bits = n_bits;
ab->destroy = g_free;
anode_encode_tlv_and_enc (node, length + 1, anode_encoder_bit_string, ab, abits_destroy);
return TRUE;
}
glong
egg_asn1x_get_time_as_long (GNode *node)
{
struct tm when;
Atlv *tlv;
glong time;
gint type;
g_return_val_if_fail (node, -1);
type = anode_def_type (node);
/* Time is often represented as a choice, so work than in here */
if (type == TYPE_CHOICE) {
node = egg_asn1x_get_choice (node);
if (node == NULL)
return -1;
g_return_val_if_fail (anode_def_type (node) == TYPE_TIME, -1);
return egg_asn1x_get_time_as_long (node);
}
g_return_val_if_fail (type == TYPE_TIME, -1);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return -1;
if (!anode_read_time (node, tlv, &when, &time))
return -1;
return time;
}
gboolean
egg_asn1x_get_time_as_date (GNode *node, GDate *date)
{
struct tm when;
Atlv *tlv;
glong time;
gint type;
g_return_val_if_fail (node, FALSE);
type = anode_def_type (node);
/* Time is often represented as a choice, so work than in here */
if (type == TYPE_CHOICE) {
node = egg_asn1x_get_choice (node);
if (node == NULL)
return FALSE;
g_return_val_if_fail (anode_def_type (node) == TYPE_TIME, FALSE);
return egg_asn1x_get_time_as_date (node, date);
}
g_return_val_if_fail (type == TYPE_TIME, FALSE);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return FALSE;
if (!anode_read_time (node, tlv, &when, &time))
return FALSE;
g_date_set_dmy (date, when.tm_mday, when.tm_mon + 1, when.tm_year + 1900);
return TRUE;
}
gchar*
egg_asn1x_get_oid_as_string (GNode *node)
{
gchar *oid;
Atlv *tlv;
g_return_val_if_fail (node, NULL);
g_return_val_if_fail (anode_def_type (node) == TYPE_OBJECT_ID, NULL);
tlv = anode_get_tlv_data (node);
if (tlv == NULL || tlv->buf == NULL)
return NULL;
if (!anode_read_object_id (node, tlv, &oid))
return NULL;
return oid;
}
gboolean
egg_asn1x_set_oid_as_string (GNode *node, const gchar *oid)
{
guchar *data;
gsize n_data;
g_return_val_if_fail (oid, FALSE);
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_OBJECT_ID, FALSE);
/* Encoding will always be shorter than string */
n_data = strlen (oid);
data = g_malloc0 (n_data);
if (!anode_write_oid (oid, data, &n_data)) {
g_free (data);
return FALSE;
}
anode_encode_tlv_and_enc (node, n_data, anode_encoder_simple, data, g_free);
return TRUE;
}
GQuark
egg_asn1x_get_oid_as_quark (GNode *node)
{
GQuark quark;
gchar *oid;
oid = egg_asn1x_get_oid_as_string (node);
if (!oid)
return 0;
quark = g_quark_from_string (oid);
g_free (oid);
return quark;
}
gboolean
egg_asn1x_set_oid_as_quark (GNode *node, GQuark oid)
{
const gchar *str;
g_return_val_if_fail (oid, FALSE);
str = g_quark_to_string (oid);
g_return_val_if_fail (str, FALSE);
return egg_asn1x_set_oid_as_string (node, str);
}
GNode*
egg_asn1x_get_choice (GNode *node)
{
GNode *child;
Anode *an;
g_return_val_if_fail (node, NULL);
/* One and only one of the children must be set */
for (child = node->children; child; child = child->next) {
an = (Anode*)child->data;
if (an->chosen)
return child;
}
return NULL;
}
gboolean
egg_asn1x_set_choice (GNode *node, GNode *choice)
{
GNode *child;
Anode *an;
g_return_val_if_fail (node, FALSE);
g_return_val_if_fail (anode_def_type (node) == TYPE_CHOICE, FALSE);
/* One and only one of the children must be set */
for (child = node->children; child; child = child->next) {
an = (Anode*)child->data;
if (child == choice) {
an->chosen = 1;
choice = NULL;
} else {
an->chosen = 0;
}
}
/* The choice is not one of the child nodes */
g_return_val_if_fail (!choice, FALSE);
return TRUE;
}
/* -----------------------------------------------------------------------------------
* VALIDATION
*/
static gboolean
anode_parse_size (GNode *node, const gchar *text, gulong *value)
{
ASN1_ARRAY_TYPE *def;
gchar *end = NULL;
if (text == NULL) {
*value = 0;
return FALSE;
} else if (g_str_equal (text, "MAX")) {
*value = G_MAXULONG;
return TRUE;
} else if (g_ascii_isalpha (text[0])) {
def = anode_opt_lookup (node, TYPE_INTEGER, text);
g_return_val_if_fail (def, FALSE);
return anode_parse_size (node, def->value, value);
}
*value = strtoul (text, &end, 10);
g_return_val_if_fail (end && !end[0], FALSE);
return TRUE;
}
static gboolean
anode_validate_size (GNode *node, gulong length)
{
ASN1_ARRAY_TYPE *size;
gulong value1 = 0;
gulong value2 = G_MAXULONG;
if (anode_def_flags (node) & FLAG_SIZE) {
size = anode_opt_lookup (node, TYPE_SIZE, NULL);
g_return_val_if_fail (size, FALSE);
if (!anode_parse_size (node, size->value, &value1))
g_return_val_if_reached (FALSE);
if (size->type & FLAG_MIN_MAX) {
if (!anode_parse_size (node, size->name, &value2))
g_return_val_if_reached (FALSE);
if (length < value1 || length >= value2)
return anode_failure (node, "content size is out of bounds");
} else {
if (length != value1)
return anode_failure (node, "content size is not correct");
}
}
return TRUE;
}
static gboolean
anode_validate_integer (GNode *node, Atlv *tlv)
{
GList *constants, *l;
gulong value, check;
gboolean found;
gint flags;
g_assert (tlv);
/* Integers must be at least one byte long */
if (tlv->len <= 0)
return anode_failure (node, "zero length integer");
flags = anode_def_flags (node);
if (flags & FLAG_LIST) {
/* Parse out the value, we only support small integers*/
if (!anode_read_integer_as_ulong (node, tlv, &value))
return anode_failure (node, "integer not part of list");
/* Look through the list of constants */
found = FALSE;
constants = anode_opts_lookup (node, TYPE_CONSTANT, NULL);
for (l = constants; l; l = g_list_next (l)) {
check = anode_def_value_as_ulong (l->data);
g_return_val_if_fail (check != G_MAXULONG, FALSE);
if (check == value) {
found = TRUE;
break;
}
}
g_list_free (constants);
if (!found)
return anode_failure (node, "integer not part of listed set");
}
return TRUE;
}
static gboolean
anode_validate_enumerated (GNode *node, Atlv *tlv)
{
g_assert (tlv);
if (!anode_validate_integer (node, tlv))
return FALSE;
g_assert (tlv->len); /* Checked above */
/* Enumerated must be positive */
if (tlv->buf[tlv->off] & 0x80)
return anode_failure (node, "enumerated must be positive");
return TRUE;
}
static gboolean
anode_validate_boolean (GNode *node, Atlv *tlv)
{
g_assert (tlv);
/* Must one byte, and zero or all ones */
if (tlv->len != 1)
return anode_failure (node, "invalid length boolean");
if (tlv->buf[tlv->off] != 0x00 && tlv->buf[tlv->off] != 0xFF)
return anode_failure (node, "boolean must be true or false");
return TRUE;
}
static gboolean
anode_validate_bit_string (GNode *node, Atlv *tlv)
{
guchar empty, mask;
g_assert (tlv);
/* At least two bytes in length */
if (tlv->len < 1)
return anode_failure (node, "invalid length bit string");
/* First byte is the number of free bits at end */
empty = tlv->buf[tlv->off];
if (empty > 7)
return anode_failure (node, "bit string has invalid header");
/* Free bits at end must be zero */
mask = 0xFF >> (8 - empty);
if (tlv->len > 1 && tlv->buf[tlv->off + tlv->len - 1] & mask)
return anode_failure (node, "bit string has invalid trailing bits");
return TRUE;
}
static gboolean
anode_validate_string (GNode *node, Atlv *tlv)
{
gsize length;
if (!anode_read_string (node, tlv, NULL, &length))
return anode_failure (node, "string content is invalid");
return anode_validate_size (node, (gulong)length);
}
static gboolean
anode_validate_object_id (GNode *node, Atlv *tlv)
{
return anode_read_object_id (node, tlv, NULL);
}
static gboolean
anode_validate_null (GNode *node, Atlv *tlv)
{
g_assert (tlv);
return (tlv->len == 0);
}
static gboolean
anode_validate_time (GNode *node, Atlv *tlv)
{
glong time;
struct tm when;
return anode_read_time (node, tlv, &when, &time);
}
static gboolean
anode_validate_choice (GNode *node)
{
GNode *child, *choice;
Anode *an;
/* One and only one of the children must be set */
choice = egg_asn1x_get_choice (node);
if (!choice)
return anode_failure (node, "one choice must be set");
if (!anode_validate_anything (choice))
return FALSE;
for (child = node->children; child; child = child->next) {
if (child != choice) {
an = (Anode*)child->data;
if (an->chosen)
return anode_failure (node, "only one choice may be set");
}
}
return TRUE;
}
static gboolean
anode_validate_sequence_or_set (GNode *node)
{
GNode *child;
gulong tag = 0;
gint count = 0;
gint type;
Atlv *tlv;
type = anode_def_type (node);
/* All of the children must validate properly */
for (child = node->children; child; child = child->next) {
if (!anode_validate_anything (child))
return FALSE;
/* Tags must be in ascending order */
tlv = anode_get_tlv_data (child);
if (tlv && type == TYPE_SET) {
if (count > 0 && tag > tlv->tag)
return anode_failure (node, "content must be in ascending order");
tag = tlv->tag;
++count;
}
}
return TRUE;
}
static gboolean
anode_validate_sequence_or_set_of (GNode *node)
{
GNode *child;
Atlv *tlv, *ptlv;
gulong tag;
gulong count;
gint type;
tag = 0;
count = 0;
tlv = ptlv = NULL;
type = anode_def_type (node);
/* All the children must validate properly */
for (child = node->children; child; child = child->next) {
tlv = anode_get_tlv_data (child);
if (tlv) {
if (!anode_validate_anything (child))
return FALSE;
/* Tag must have same tag as top */
if (count == 0)
tag = anode_calc_tag (child);
else if (tag != G_MAXULONG && tlv->tag != tag)
return anode_failure (node, "invalid mismatched content");
/* Set of must be in ascending order */
if (type == TYPE_SET_OF && ptlv && compare_tlvs (ptlv, tlv) > 0)
return anode_failure (node, "content must be in ascending order");
ptlv = tlv;
++count;
}
}
return anode_validate_size (node, count);
}
static gboolean
anode_validate_anything (GNode *node)
{
Atlv *tlv;
gint type;
type = anode_def_type (node);
tlv = anode_get_tlv_data (node);
if (!tlv) {
if (anode_def_flags (node) & FLAG_OPTION)
return TRUE;
if (anode_def_flags (node) & FLAG_DEFAULT)
return TRUE;
return anode_failure (node, "missing value");
}
g_return_val_if_fail (tlv->buf, FALSE);
switch (type) {
/* The primitive value types */
case TYPE_INTEGER:
return anode_validate_integer (node, tlv);
case TYPE_ENUMERATED:
return anode_validate_enumerated (node, tlv);
case TYPE_BOOLEAN:
return anode_validate_boolean (node, tlv);
case TYPE_BIT_STRING:
return anode_validate_bit_string (node, tlv);
case TYPE_OCTET_STRING:
return anode_validate_string (node, tlv);
case TYPE_OBJECT_ID:
return anode_validate_object_id (node, tlv);
case TYPE_NULL:
return anode_validate_null (node, tlv);
case TYPE_GENERALSTRING:
return anode_validate_string (node, tlv);
case TYPE_TIME:
return anode_validate_time (node, tlv);
/* Transparent types */
case TYPE_ANY:
return TRUE;
case TYPE_CHOICE:
return anode_validate_choice (node);
/* Structured types */
case TYPE_SEQUENCE:
case TYPE_SET:
return anode_validate_sequence_or_set (node);
case TYPE_SEQUENCE_OF:
case TYPE_SET_OF:
return anode_validate_sequence_or_set_of (node);
default:
g_return_val_if_reached (FALSE);
}
}
gboolean
egg_asn1x_validate (GNode *asn)
{
g_return_val_if_fail (asn, FALSE);
return anode_validate_anything (asn);
}
/* -----------------------------------------------------------------------------------
* TREE CREATION
*/
static gint
compare_nodes_by_tag (gconstpointer a, gconstpointer b)
{
GNode *na = (gpointer)a;
GNode *nb = (gpointer)b;
gulong taga, tagb;
g_return_val_if_fail (anode_def_flags (na) & FLAG_TAG, 0);
g_return_val_if_fail (anode_def_flags (nb) & FLAG_TAG, 0);
taga = anode_calc_tag (na);
g_return_val_if_fail (taga != G_MAXULONG, 0);
tagb = anode_calc_tag (nb);
g_return_val_if_fail (tagb != G_MAXULONG, 0);
if (taga == tagb)
return 0;
return (taga < tagb) ? -1 : 1;
}
static const ASN1_ARRAY_TYPE*
adef_next_sibling (const ASN1_ARRAY_TYPE *def)
{
int depth = 0;
g_assert (def);
g_assert (def->value || def->type || def->name);
if ((def->type & FLAG_RIGHT) == 0)
return NULL;
/* Skip past any children */
if ((def->type & FLAG_DOWN) == FLAG_DOWN) {
depth += 1;
while (depth > 0) {
++def;
if ((def->type & FLAG_DOWN) == FLAG_DOWN)
depth += 1;
if ((def->type & FLAG_RIGHT) == 0)
depth -= 1;
}
}
++def;
g_return_val_if_fail (def->value || def->type || def->name, NULL);
return def;
}
static const ASN1_ARRAY_TYPE*
adef_first_child (const ASN1_ARRAY_TYPE *def)
{
g_assert (def);
g_assert (def->value || def->type || def->name);
if ((def->type & FLAG_DOWN) == 0)
return NULL;
++def;
g_return_val_if_fail (def->value || def->type || def->name, NULL);
return def;
}
static const ASN1_ARRAY_TYPE*
lookup_def_of_type (const ASN1_ARRAY_TYPE *defs, const gchar *name, gint type)
{
const ASN1_ARRAY_TYPE *def;
g_assert (defs);
g_assert (defs->value || defs->type || defs->name);
for (def = adef_first_child (defs); def; def = adef_next_sibling (def)) {
if ((def->type & 0xFF) == type && def->name && g_str_equal (name, def->name))
return def;
}
return NULL;
}
static gboolean
traverse_and_prepare (GNode *node, gpointer data)
{
const ASN1_ARRAY_TYPE *defs = data;
const ASN1_ARRAY_TYPE *def;
const gchar *identifier;
Anode *an, *anj;
GNode *join = NULL;
GNode *child, *next;
GList *list = NULL, *l;
/* A while, because the stuff we join, could also be an identifier */
while (anode_def_type (node) == TYPE_IDENTIFIER) {
an = node->data;
identifier = an->join ? an->join->value : an->def->value;
g_return_val_if_fail (identifier, TRUE);
egg_asn1x_destroy (join);
join = egg_asn1x_create (defs, identifier);
g_return_val_if_fail (join, TRUE);
anj = join->data;
an->join = anj->def;
}
/* Move all the children of join node into our node */
if (join) {
list = NULL;
for (child = join->children, list = NULL; child; child = child->next)
list = g_list_prepend (list, child);
list = g_list_reverse (list);
for (l = list; l; l = g_list_next (l)) {
child = l->data;
g_node_unlink (child);
g_node_append (node, child);
}
g_list_free (list);
list = NULL;
}
/* Lookup the max set size */
if (anode_def_type (node) == TYPE_SIZE) {
identifier = anode_def_name (node);
if (identifier && !g_str_equal (identifier, "MAX") &&
g_ascii_isalpha (identifier[0])) {
def = lookup_def_of_type (defs, identifier, TYPE_INTEGER);
g_return_val_if_fail (def, TRUE);
anode_opt_add (node, def);
}
}
/* Anything child not a real node, we put into opts */
if (anode_def_type_is_real (node)) {
child = node->children;
while (child) {
next = child->next;
if (!anode_def_type_is_real (child)) {
an = child->data;
anode_opt_add (node, an->def);
for (l = an->opts; l; l = g_list_next (l))
anode_opt_add (node, l->data);
g_node_unlink (child);
anode_destroy (child);
}
child = next;
}
}
if (join) {
an = join->data;
for (l = an->opts; l; l = g_list_next (l))
anode_opt_add (node, l->data);
egg_asn1x_destroy (join);
}
/* Sort the children of any sets */
if (anode_def_type (node) == TYPE_SET) {
for (child = node->children; child; child = child->next)
list = g_list_prepend (list, child);
list = g_list_sort (list, compare_nodes_by_tag);
for (l = list; l; l = g_list_next (l))
g_node_unlink (l->data);
for (l = list; l; l = g_list_next (l))
g_node_append (node, l->data);
g_list_free (list);
list = NULL;
}
/* Continue traversal */
return FALSE;
}
static const ASN1_ARRAY_TYPE*
match_oid_in_definition (const ASN1_ARRAY_TYPE *def, GHashTable *names,
const gchar *match, const gchar **problem)
{
const ASN1_ARRAY_TYPE *result = NULL;
const ASN1_ARRAY_TYPE *odef;
const gchar *value;
GString *oid = NULL;
g_assert (match);
g_assert (problem);
g_assert (names);
for (odef = adef_first_child (def); odef; odef = adef_next_sibling (odef)) {
if ((odef->type & 0xFF) != TYPE_CONSTANT)
continue;
g_return_val_if_fail (odef->value, NULL);
if (strspn (odef->value, "01234567890") == strlen (odef->value)) {
value = odef->value;
} else {
value = g_hash_table_lookup (names, odef->value);
/* A name resolution problem */
if (!value) {
if (oid)
g_string_free (oid, TRUE);
*problem = odef->value;
return NULL;
}
}
if (oid) {
g_string_append_c (oid, '.');
g_string_append (oid, value);
} else {
oid = g_string_new (value);
}
}
if (oid != NULL) {
if (g_str_equal (oid->str, match))
result = adef_next_sibling (def);
g_assert (def->name);
g_hash_table_insert (names, (gchar*)def->name, g_string_free (oid, FALSE));
}
return result;
}
static const ASN1_ARRAY_TYPE*
match_oid_in_definitions (const ASN1_ARRAY_TYPE *defs, const gchar *match)
{
const ASN1_ARRAY_TYPE *def;
const ASN1_ARRAY_TYPE *result;
GHashTable *names;
gboolean progress;
const gchar *problem;
names = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
result = NULL;
for (;;) {
progress = FALSE;
problem = NULL;
for (def = adef_first_child (defs); def; def = adef_next_sibling (def)) {
/* Only work with object ids, and ones with names */
if ((def->type & 0xFF) != TYPE_OBJECT_ID || !def->name)
continue;
/* If we've already seen this one, skip */
if (g_hash_table_lookup (names, def->name))
continue;
progress = TRUE;
result = match_oid_in_definition (def, names, match, &problem);
if (result != NULL)
break;
}
if (!problem || result) {
break;
} else if (problem && !progress) {
g_warning ("couldn't find oid definition in ASN.1 for: %s", problem);
g_return_val_if_reached (NULL);
}
}
g_hash_table_destroy (names);
return result;
}
GNode*
egg_asn1x_create (const ASN1_ARRAY_TYPE *defs, const gchar *type)
{
const ASN1_ARRAY_TYPE *def;
GNode *root, *parent, *node;
int flags;
g_return_val_if_fail (defs, NULL);
g_return_val_if_fail (type, NULL);
/* An OID */
if (strspn (type, "0123456789.") == strlen (type)) {
def = match_oid_in_definitions (defs, type);
/* An Identifier */
} else {
for (def = adef_first_child (defs); def; def = adef_next_sibling (def)) {
if (def->name && g_str_equal (type, def->name))
break;
}
}
if (def == NULL || !def->name || !def->type)
return NULL;
/* The node for this item */
root = anode_new (def);
/* Build up nodes for underlying level */
if (def->type & FLAG_DOWN) {
node = root;
for (;;) {
if (def->type & FLAG_DOWN) {
parent = node;
} else if (def->type & FLAG_RIGHT) {
g_assert (node->parent);
parent = node->parent;
} else {
parent = node->parent;
while (parent) {
flags = anode_def_flags (parent);
parent = parent->parent;
if (flags & FLAG_RIGHT)
break;
}
}
if (!parent)
break;
++def;
node = anode_new (def);
g_node_append (parent, node);
}
}
/* Load up sub identifiers */
g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
traverse_and_prepare, (gpointer)defs);
return root;
}
GNode*
egg_asn1x_create_quark (const ASN1_ARRAY_TYPE *defs, GQuark type)
{
g_return_val_if_fail (type, NULL);
return egg_asn1x_create (defs, g_quark_to_string (type));
}
GNode*
egg_asn1x_create_and_decode (const ASN1_ARRAY_TYPE *defs, const gchar *identifier,
gconstpointer data, gsize n_data)
{
GNode *asn;
g_return_val_if_fail (defs, NULL);
g_return_val_if_fail (identifier, NULL);
asn = egg_asn1x_create (defs, identifier);
g_return_val_if_fail (asn, NULL);
if (!egg_asn1x_decode (asn, data, n_data)) {
egg_asn1x_destroy (asn);
return NULL;
}
return asn;
}
/* -----------------------------------------------------------------------------------
* DUMPING and MESSAGES
*/
static void
dump_append_type (GString *output, gint type)
{
#define XX(x) if (type == TYPE_##x) g_string_append (output, #x " ")
XX(CONSTANT); XX(IDENTIFIER); XX(INTEGER); XX(BOOLEAN); XX(SEQUENCE); XX(BIT_STRING);
XX(OCTET_STRING); XX(TAG); XX(DEFAULT); XX(SIZE); XX(SEQUENCE_OF); XX(OBJECT_ID); XX(ANY);
XX(SET); XX(SET_OF); XX(DEFINITIONS); XX(TIME); XX(CHOICE); XX(IMPORTS); XX(NULL);
XX(ENUMERATED); XX(GENERALSTRING);
if (output->len == 0)
g_string_printf (output, "%d ", (int)type);
#undef XX
}
static void
dump_append_flags (GString *output, gint flags)
{
#define XX(x) if ((FLAG_##x & flags) == FLAG_##x) g_string_append (output, #x " ")
XX(UNIVERSAL); XX(PRIVATE); XX(APPLICATION); XX(EXPLICIT); XX(IMPLICIT); XX(TAG); XX(OPTION);
XX(DEFAULT); XX(TRUE); XX(FALSE); XX(LIST); XX(MIN_MAX); XX(1_PARAM); XX(SIZE); XX(DEFINED_BY);
XX(GENERALIZED); XX(UTC); XX(IMPORTS); XX(NOT_USED); XX(SET); XX(ASSIGN);
/* XX(DOWN); XX(RIGHT); */
#undef XX
}
static gboolean
traverse_and_dump (GNode *node, gpointer unused)
{
ASN1_ARRAY_TYPE *def;
guint i, depth;
GString *output;
gchar *string;
Atlv *tlv;
Anode *an;
GList *l;
depth = g_node_depth (node);
for (i = 0; i < depth - 1; ++i)
g_printerr (" ");
tlv = anode_get_tlv_data (node);
output = g_string_new ("");
dump_append_type (output, anode_def_type (node));
dump_append_flags (output, anode_def_flags (node));
string = g_utf8_casefold (output->str, output->len - 1);
g_string_free (output, TRUE);
g_printerr ("+ %s: %s [%s]%s\n", anode_def_name (node), anode_def_value (node),
string, tlv && tlv->buf ? " *" : "");
g_free (string);
/* Print out all the options */
an = node->data;
for (l = an->opts; l; l = g_list_next (l)) {
for (i = 0; i < depth; ++i)
g_printerr (" ");
def = l->data;
output = g_string_new ("");
dump_append_type (output, def->type & 0xFF);
dump_append_flags (output, def->type);
string = g_utf8_casefold (output->str, output->len - 1);
g_string_free (output, TRUE);
g_printerr ("- %s: %s [%s]\n", def->name, (const gchar*)def->value, string);
g_free (string);
}
return FALSE;
}
void
egg_asn1x_dump (GNode *asn)
{
g_return_if_fail (asn);
g_node_traverse (asn, G_PRE_ORDER, G_TRAVERSE_ALL, -1, traverse_and_dump, NULL);
}
static gboolean
traverse_and_get_failure (GNode *node, gpointer user_data)
{
const gchar **failure = user_data;
g_assert (!*failure);
*failure = anode_failure_get (node);
return (*failure != NULL);
}
const gchar*
egg_asn1x_message (GNode *asn)
{
const gchar *failure = NULL;
g_return_val_if_fail (asn, NULL);
g_node_traverse (asn, G_POST_ORDER, G_TRAVERSE_ALL, -1, traverse_and_get_failure, &failure);
return failure;
}
/* -----------------------------------------------------------------------------------
* CLEARING and DESTROYING
*/
static gboolean
traverse_and_clear (GNode *node, gpointer unused)
{
GNode *child, *next;
gint type;
anode_clear (node);
type = anode_def_type (node);
if (type == TYPE_SET_OF || type == TYPE_SEQUENCE_OF) {
/* The first 'real' child is the template */
child = node->children;
g_return_val_if_fail (child, TRUE);
/* And any others are extras */
child = child->next;
while (child) {
next = child->next;
anode_destroy (child);
child = next;
}
}
/* Don't stop traversal */
return FALSE;
}
void
egg_asn1x_clear (GNode *asn)
{
g_return_if_fail (asn);
g_node_traverse (asn, G_POST_ORDER, G_TRAVERSE_ALL, -1, traverse_and_clear, NULL);
}
void
egg_asn1x_destroy (gpointer data)
{
GNode *node = data;
if (node != NULL) {
g_return_if_fail (G_NODE_IS_ROOT (node));
anode_destroy (node);
}
}
/* --------------------------------------------------------------------------------
* TIME PARSING
*/
glong
egg_asn1x_parse_time_general (const gchar *time, gssize n_time)
{
gboolean ret;
glong value;
struct tm when;
gint offset = 0;
g_return_val_if_fail (time, -1);
if (n_time < 0)
n_time = strlen (time);
ret = parse_general_time (time, n_time, &when, &offset);
if (!ret)
return -1;
/* In order to work with 32 bit time_t. */
if (sizeof (time_t) <= 4 && when.tm_year >= 2038) {
value = (time_t)2145914603; /* 2037-12-31 23:23:23 */
/* Convert to seconds since epoch */
} else {
value = timegm (&when);
g_return_val_if_fail (*time >= 0, FALSE);
value += offset;
}
return value;
}
glong
egg_asn1x_parse_time_utc (const gchar *time, gssize n_time)
{
gboolean ret;
glong value;
struct tm when;
gint offset = 0;
g_return_val_if_fail (time, -1);
if (n_time < 0)
n_time = strlen (time);
ret = parse_utc_time (time, n_time, &when, &offset);
if (!ret)
return -1;
/* In order to work with 32 bit time_t. */
if (sizeof (time_t) <= 4 && when.tm_year >= 2038) {
value = (time_t)2145914603; /* 2037-12-31 23:23:23 */
/* Convert to seconds since epoch */
} else {
value = timegm (&when);
g_return_val_if_fail (*time >= 0, FALSE);
value += offset;
}
return value;
}
/* --------------------------------------------------------------------------------
* BASIC RAW ELEMENT INFO
*/
gssize
egg_asn1x_element_length (gconstpointer data, gsize n_data)
{
guchar cls;
int counter = 0;
int cb, len;
gulong tag;
if (anode_decode_cls_tag (data, (const guchar*)data + n_data, &cls, &tag, &cb)) {
counter += cb;
len = anode_decode_length ((const guchar*)data + cb, (const guchar*)data + n_data, &cb);
counter += cb;
if (len >= 0) {
len += counter;
if (n_data >= len)
return len;
}
}
return -1;
}
gconstpointer
egg_asn1x_element_content (gconstpointer data, gsize n_data, gsize *n_content)
{
int counter = 0;
guchar cls;
gulong tag;
int cb, len;
g_return_val_if_fail (data != NULL, NULL);
g_return_val_if_fail (n_content != NULL, NULL);
/* Now get the data out of this element */
if (!anode_decode_cls_tag (data, (const guchar*)data + n_data, &cls, &tag, &cb))
return NULL;
counter += cb;
len = anode_decode_length ((const guchar*)data + cb, (const guchar*)data + n_data, &cb);
if (len < 0)
return NULL;
counter += cb;
*n_content = len;
return (const guchar*)data + counter;
}
|