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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Module: keystore.c
* Description: This module contains the structure definitions for processing
* package keystore files.
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#include <libintl.h>
#include <time.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/pkcs12.h>
#include <openssl/asn1.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/safestack.h>
#include <openssl/stack.h>
#include "p12lib.h"
#include "pkgerr.h"
#include "keystore.h"
#include "pkglib.h"
#include "pkglibmsgs.h"
typedef struct keystore_t {
boolean_t dirty;
boolean_t new;
char *path;
char *passphrase;
/* truststore handles */
int cafd;
STACK_OF(X509) *cacerts;
char *capath;
/* user certificate handles */
STACK_OF(X509) *clcerts;
char *clpath;
/* private key handles */
STACK_OF(EVP_PKEY) *pkeys;
char *keypath;
} keystore_t;
/* local routines */
static keystore_t *new_keystore(void);
static void free_keystore(keystore_t *);
static boolean_t verify_keystore_integrity(PKG_ERR *, keystore_t *);
static boolean_t check_password(PKCS12 *, char *);
static boolean_t resolve_paths(PKG_ERR *, char *, char *,
long, keystore_t *);
static boolean_t lock_keystore(PKG_ERR *, long, keystore_t *);
static boolean_t unlock_keystore(PKG_ERR *, keystore_t *);
static boolean_t read_keystore(PKG_ERR *, keystore_t *,
keystore_passphrase_cb);
static boolean_t write_keystore(PKG_ERR *, keystore_t *,
keystore_passphrase_cb);
static boolean_t write_keystore_file(PKG_ERR *, char *, PKCS12 *);
static boolean_t clear_keystore_file(PKG_ERR *, char *);
static PKCS12 *read_keystore_file(PKG_ERR *, char *);
static char *get_time_string(ASN1_TIME *);
/* locking routines */
static boolean_t restore_keystore_file(PKG_ERR *, char *);
static int file_lock(int, int, int);
static int file_unlock(int);
static boolean_t file_lock_test(int, int);
static boolean_t file_empty(char *);
static boolean_t get_keystore_passwd(PKG_ERR *err, PKCS12 *p12,
keystore_passphrase_cb cb, keystore_t *keystore);
static boolean_t wait_restore(int, char *, char *, char *);
#define KEYSTORE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/* wait on other keystore access for 1 minute before giving up */
#define LOCK_TIMEOUT 60
/*
* print_certs - prints certificates out of a keystore, to a file.
*
* Arguments:
* err - Error object to append errors to
* keystore - Keystore on which to operate
* alias - Name of certificate to print, NULL means print all
* format - Format in which to print certificates
* outfile - Where to print certificates
*
* Returns:
* 0 - Success
* non-zero - Failure, errors added to err
*/
int
print_certs(PKG_ERR *err, keystore_handle_t keystore_h, char *alias,
keystore_encoding_format_t format, FILE *outfile)
{
int i;
X509 *cert;
char *fname = NULL;
boolean_t found = B_FALSE;
keystore_t *keystore = keystore_h;
if (keystore->clcerts != NULL) {
/* print out each client cert */
for (i = 0; i < sk_X509_num(keystore->clcerts); i++) {
cert = sk_X509_value(keystore->clcerts, i);
(void) sunw_get_cert_fname(GETDO_COPY, cert,
&fname);
if (fname == NULL) {
/* no name recorded, keystore is corrupt */
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_NO_ALIAS),
get_subject_display_name(cert));
return (1);
}
if ((alias != NULL) && (!streq(alias, fname))) {
/* name does not match, skip it */
(void) OPENSSL_free(fname);
fname = NULL;
continue;
} else {
found = B_TRUE;
(void) print_cert(err, cert, format,
fname, B_FALSE, outfile);
(void) OPENSSL_free(fname);
fname = NULL;
}
}
}
if (fname != NULL) {
(void) OPENSSL_free(fname);
fname = NULL;
}
if (keystore->cacerts != NULL) {
/* print out each trusted cert */
for (i = 0; i < sk_X509_num(keystore->cacerts); i++) {
cert = sk_X509_value(keystore->cacerts, i);
(void) sunw_get_cert_fname(GETDO_COPY,
cert, &fname);
if (fname == NULL) {
/* no name recorded, keystore is corrupt */
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_NO_ALIAS),
get_subject_display_name(cert));
return (1);
}
if ((alias != NULL) && (!streq(alias, fname))) {
/* name does not match, skip it */
(void) OPENSSL_free(fname);
fname = NULL;
continue;
} else {
found = B_TRUE;
(void) print_cert(err, cert, format,
fname, B_TRUE, outfile);
(void) OPENSSL_free(fname);
fname = NULL;
}
}
}
if (fname != NULL) {
(void) OPENSSL_free(fname);
fname = NULL;
}
if (found) {
return (0);
} else {
/* no certs printed */
if (alias != NULL) {
pkgerr_add(err, PKGERR_NOALIASMATCH,
gettext(ERR_KEYSTORE_NOCERT),
alias, keystore->path);
} else {
pkgerr_add(err, PKGERR_NOPUBKEY,
gettext(ERR_KEYSTORE_NOPUBCERTS),
keystore->path);
pkgerr_add(err, PKGERR_NOCACERT,
gettext(ERR_KEYSTORE_NOCACERTS),
keystore->path);
}
return (1);
}
}
/*
* print_cert - prints a single certificate, to a file
*
* Arguments:
* err - Error object to append errors to
* x - The certificate to print
* alias - Name of certificate to print
* format - Format in which to print certificate
* outfile - Where to print certificate
*
* Returns:
* 0 - Success
* non-zero - Failure, errors added to err
*/
int print_cert(PKG_ERR *err, X509 *x,
keystore_encoding_format_t format, char *alias, boolean_t is_trusted,
FILE *outfile)
{
char *vdb_str;
char *vda_str;
char vd_str[ATTR_MAX];
int ret = 0;
char *cn_str, *icn_str, *typ_str;
char *tmp;
char *md5_fp;
char *sha1_fp;
int len;
/* need to localize the word "Fingerprint", hence these pointers */
char md5_label[ATTR_MAX];
char sha1_label[ATTR_MAX];
if (is_trusted) {
typ_str = gettext(MSG_KEYSTORE_TRUSTED);
} else {
typ_str = gettext(MSG_KEYSTORE_UNTRUSTED);
}
if ((cn_str = get_subject_display_name(x)) == NULL) {
cn_str = gettext(MSG_KEYSTORE_UNKNOWN);
}
if ((icn_str = get_issuer_display_name(x)) == NULL) {
icn_str = gettext(MSG_KEYSTORE_UNKNOWN);
}
vdb_str = xstrdup(get_time_string(X509_get_notBefore(x)));
vda_str = xstrdup(get_time_string(X509_get_notAfter(x)));
if (((len = snprintf(vd_str, ATTR_MAX, "<%s> - <%s>",
vdb_str, vda_str)) < 0) || (len >= ATTR_MAX)) {
pkgerr_add(err, PKGERR_WEB, gettext(ERR_LEN), vdb_str);
ret = 1;
goto cleanup;
}
if ((tmp = get_fingerprint(x, EVP_md5())) == NULL) {
md5_fp = gettext(MSG_KEYSTORE_UNKNOWN);
} else {
/*
* make a copy, otherwise the next call to get_fingerprint
* will overwrite this one
*/
md5_fp = xstrdup(tmp);
}
if ((tmp = get_fingerprint(x, EVP_sha1())) == NULL) {
sha1_fp = gettext(MSG_KEYSTORE_UNKNOWN);
} else {
sha1_fp = xstrdup(tmp);
}
(void) snprintf(md5_label, ATTR_MAX, "%s %s",
OBJ_nid2sn(EVP_MD_type(EVP_md5())),
/* i18n: 14 characters max */
gettext(MSG_KEYSTORE_FP));
(void) snprintf(sha1_label, ATTR_MAX, "%s %s",
OBJ_nid2sn(EVP_MD_type(EVP_sha1())),
/* i18n: 14 characters max */
gettext(MSG_KEYSTORE_FP));
switch (format) {
case KEYSTORE_FORMAT_PEM:
(void) PEM_write_X509(outfile, x);
break;
case KEYSTORE_FORMAT_DER:
(void) i2d_X509_fp(outfile, x);
break;
case KEYSTORE_FORMAT_TEXT:
(void) fprintf(outfile, "%18s: %s\n",
/* i18n: 18 characters max */
gettext(MSG_KEYSTORE_AL), alias);
(void) fprintf(outfile, "%18s: %s\n",
/* i18n: 18 characters max */
gettext(MSG_KEYSTORE_CN), cn_str);
(void) fprintf(outfile, "%18s: %s\n",
/* i18n: 18 characters max */
gettext(MSG_KEYSTORE_TY), typ_str);
(void) fprintf(outfile, "%18s: %s\n",
/* i18n: 18 characters max */
gettext(MSG_KEYSTORE_IN), icn_str);
(void) fprintf(outfile, "%18s: %s\n",
/* i18n: 18 characters max */
gettext(MSG_KEYSTORE_VD), vd_str);
(void) fprintf(outfile, "%18s: %s\n", md5_label, md5_fp);
(void) fprintf(outfile, "%18s: %s\n", sha1_label, sha1_fp);
(void) fprintf(outfile, "\n");
break;
default:
pkgerr_add(err, PKGERR_INTERNAL,
gettext(ERR_KEYSTORE_INTERNAL),
__FILE__, __LINE__);
ret = 1;
goto cleanup;
}
cleanup:
if (md5_fp != NULL)
free(md5_fp);
if (sha1_fp != NULL)
free(sha1_fp);
if (vda_str != NULL)
free(vda_str);
if (vdb_str != NULL)
free(vdb_str);
return (ret);
}
/*
* open_keystore - Initialize new keystore object for
* impending access.
*
* Arguments:
* err - Error object to append errors to
* keystore_file - Base filename or directory of keystore
* app - Application making request
* passwd - Password used to decrypt keystore
* flags - Control flags used to control access mode and behavior
* result - Resulting keystore object stored here on success
*
* Returns:
* 0 - Success - result contains a pointer to the opened keystore
* non-zero - Failure, errors added to err
*/
int
open_keystore(PKG_ERR *err, char *keystore_file, char *app,
keystore_passphrase_cb cb, long flags, keystore_handle_t *result)
{
int ret = 0;
keystore_t *tmpstore;
tmpstore = new_keystore();
tmpstore->dirty = B_FALSE;
tmpstore->new = B_FALSE;
tmpstore->path = xstrdup(keystore_file);
if (!resolve_paths(err, keystore_file, app, flags, tmpstore)) {
/* unable to determine keystore paths */
pkgerr_add(err, PKGERR_CORRUPT, gettext(ERR_KEYSTORE_REPAIR),
keystore_file);
ret = 1;
goto cleanup;
}
if (!verify_keystore_integrity(err, tmpstore)) {
/* unable to repair keystore */
pkgerr_add(err, PKGERR_CORRUPT, gettext(ERR_KEYSTORE_REPAIR),
keystore_file);
ret = 1;
goto cleanup;
}
if (!lock_keystore(err, flags, tmpstore)) {
pkgerr_add(err, PKGERR_LOCKED, gettext(ERR_KEYSTORE_LOCKED),
keystore_file);
ret = 1;
goto cleanup;
}
/* now that we have locked the keystore, go ahead and read it */
if (!read_keystore(err, tmpstore, cb)) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_PARSE),
keystore_file);
ret = 1;
goto cleanup;
}
*result = tmpstore;
tmpstore = NULL;
cleanup:
if (tmpstore != NULL)
free_keystore(tmpstore);
return (ret);
}
/*
* new_keystore - Allocates and initializes a Keystore object
*
* Arguments:
* NONE
*
* Returns:
* NULL - out of memory
* otherwise, returns a pointer to the newly allocated object,
* which should be freed with free_keystore() when no longer
* needed.
*/
static keystore_t
*new_keystore(void)
{
keystore_t *tmpstore;
if ((tmpstore = (keystore_t *)malloc(sizeof (keystore_t))) == NULL) {
return (NULL);
}
tmpstore->dirty = B_FALSE;
tmpstore->new = B_FALSE;
tmpstore->path = NULL;
tmpstore->passphrase = NULL;
tmpstore->cafd = -1;
tmpstore->cacerts = NULL;
tmpstore->capath = NULL;
tmpstore->clcerts = NULL;
tmpstore->clpath = NULL;
tmpstore->pkeys = NULL;
tmpstore->keypath = NULL;
return (tmpstore);
}
/*
* free_keystore - Deallocates a Keystore object
*
* Arguments:
* keystore - The keystore to deallocate
*
* Returns:
* NONE
*/
static void
free_keystore(keystore_t *keystore)
{
if (keystore->path != NULL)
free(keystore->path);
if (keystore->capath != NULL)
free(keystore->capath);
if (keystore->passphrase != NULL)
free(keystore->passphrase);
if (keystore->clpath != NULL)
free(keystore->clpath);
if (keystore->keypath != NULL)
free(keystore->keypath);
if (keystore->pkeys != NULL) {
sk_EVP_PKEY_pop_free(keystore->pkeys,
sunw_evp_pkey_free);
}
if (keystore->clcerts != NULL)
sk_X509_free(keystore->clcerts);
if (keystore->cacerts != NULL)
sk_X509_free(keystore->cacerts);
free(keystore);
}
/*
* close_keystore - Writes keystore to disk if needed, then
* unlocks and closes keystore.
*
* Arguments:
* err - Error object to append errors to
* keystore - Keystore which should be closed
* passwd - Password used to encrypt keystore
*
* Returns:
* 0 - Success - keystore is committed to disk, and unlocked
* non-zero - Failure, errors added to err
*/
int
close_keystore(PKG_ERR *err, keystore_handle_t keystore_h,
keystore_passphrase_cb cb)
{
int ret = 0;
keystore_t *keystore = keystore_h;
if (keystore->dirty) {
/* write out the keystore first */
if (!write_keystore(err, keystore, cb)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_WRITE),
keystore->path);
ret = 1;
goto cleanup;
}
}
if (!unlock_keystore(err, keystore)) {
pkgerr_add(err, PKGERR_UNLOCK, gettext(ERR_KEYSTORE_UNLOCK),
keystore->path);
ret = 1;
goto cleanup;
}
free_keystore(keystore);
cleanup:
return (ret);
}
/*
* merge_ca_cert - Adds a trusted certificate (trust anchor) to a keystore.
* certificate checked for validity dates and non-duplicity.
*
* Arguments:
* err - Error object to add errors to
* cacert - Certificate which to merge into keystore
* keystore - The keystore into which the certificate is merged
*
* Returns:
* 0 - Success - Certificate passes validity, and
* is merged into keystore
* non-zero - Failure, errors recorded in err
*/
int
merge_ca_cert(PKG_ERR *err, X509 *cacert, keystore_handle_t keystore_h)
{
int ret = 0;
X509 *existing = NULL;
char *fname;
keystore_t *keystore = keystore_h;
/* check validity dates */
if (check_cert(err, cacert) != 0) {
ret = 1;
goto cleanup;
}
/* create the certificate's friendlyName */
fname = get_subject_display_name(cacert);
if (sunw_set_fname(fname, NULL, cacert) != 0) {
pkgerr_add(err, PKGERR_NOMEM, gettext(ERR_MEM));
ret = 1;
goto cleanup;
}
/* merge certificate into the keystore */
if (keystore->cacerts == NULL) {
/* no existing truststore, so make a new one */
if ((keystore->cacerts = sk_X509_new_null()) == NULL) {
pkgerr_add(err, PKGERR_NOMEM, gettext(ERR_MEM));
ret = 1;
goto cleanup;
}
} else {
/* existing truststore, make sure there's no duplicate */
if (sunw_find_fname(fname, NULL, keystore->cacerts,
NULL, &existing) < 0) {
pkgerr_add(err, PKGERR_INTERNAL,
gettext(ERR_KEYSTORE_INTERNAL),
__FILE__, __LINE__);
ERR_print_errors_fp(stderr);
ret = 1;
goto cleanup;
/* could not search properly! */
}
if (existing != NULL) {
/* whoops, found one already */
pkgerr_add(err, PKGERR_DUPLICATE,
gettext(ERR_KEYSTORE_DUPLICATECERT), fname);
ret = 1;
goto cleanup;
}
}
(void) sk_X509_push(keystore->cacerts, cacert);
keystore->dirty = B_TRUE;
cleanup:
if (existing != NULL)
X509_free(existing);
return (ret);
}
/*
* find_key_cert_pair - Searches a keystore for a matching
* public key certificate and private key, given an alias.
*
* Arguments:
* err - Error object to add errors to
* ks - Keystore to search
* alias - Name to used to match certificate's alias
* key - Resulting key is placed here
* cert - Resulting cert is placed here
*
* Returns:
* 0 - Success - Matching cert/key pair placed in key and cert.
* non-zero - Failure, errors recorded in err
*/
int
find_key_cert_pair(PKG_ERR *err, keystore_handle_t ks_h, char *alias,
EVP_PKEY **key, X509 **cert)
{
X509 *tmpcert = NULL;
EVP_PKEY *tmpkey = NULL;
int ret = 0;
int items_found;
keystore_t *ks = ks_h;
if (key == NULL || cert == NULL) {
pkgerr_add(err, PKGERR_NOPUBKEY,
gettext(ERR_KEYSTORE_NOPUBCERTS), ks->path);
ret = 1;
goto cleanup;
}
if (ks->clcerts == NULL) {
/* no public certs */
pkgerr_add(err, PKGERR_NOPUBKEY,
gettext(ERR_KEYSTORE_NOCERTS), ks->path);
ret = 1;
goto cleanup;
}
if (ks->pkeys == NULL) {
/* no private keys */
pkgerr_add(err, PKGERR_NOPRIVKEY,
gettext(ERR_KEYSTORE_NOKEYS), ks->path);
ret = 1;
goto cleanup;
}
/* try the easy case first */
if ((sk_EVP_PKEY_num(ks->pkeys) == 1) &&
(sk_X509_num(ks->clcerts) == 1)) {
tmpkey = sk_EVP_PKEY_value(ks->pkeys, 0);
tmpcert = sk_X509_value(ks->clcerts, 0);
if (sunw_check_keys(tmpcert, tmpkey)) {
/*
* only one private key and public key cert, and they
* match, so use them
*/
*key = tmpkey;
tmpkey = NULL;
*cert = tmpcert;
tmpcert = NULL;
goto cleanup;
}
}
/* Attempt to find the right pair given the alias */
items_found = sunw_find_fname(alias, ks->pkeys, ks->clcerts,
&tmpkey, &tmpcert);
if ((items_found < 0) ||
(items_found & (FOUND_PKEY | FOUND_CERT)) == 0) {
/* no key/cert pair found. bail. */
pkgerr_add(err, PKGERR_BADALIAS,
gettext(ERR_KEYSTORE_NOMATCH), alias);
ret = 1;
goto cleanup;
}
/* success */
*key = tmpkey;
tmpkey = NULL;
*cert = tmpcert;
tmpcert = NULL;
cleanup:
if (tmpcert != NULL)
(void) X509_free(tmpcert);
if (tmpkey != NULL)
sunw_evp_pkey_free(tmpkey);
return (ret);
}
/*
* find_ca_certs - Searches a keystore for trusted certificates
*
* Arguments:
* err - Error object to add errors to
* ks - Keystore to search
* cacerts - resulting set of trusted certs are placed here
*
* Returns:
* 0 - Success - trusted cert list returned in cacerts
* non-zero - Failure, errors recorded in err
*/
int
find_ca_certs(PKG_ERR *err, keystore_handle_t ks_h, STACK_OF(X509) **cacerts)
{
keystore_t *ks = ks_h;
/* easy */
if (cacerts == NULL) {
pkgerr_add(err, PKGERR_INTERNAL,
gettext(ERR_KEYSTORE_INTERNAL), __FILE__, __LINE__);
return (1);
}
*cacerts = ks->cacerts;
return (0);
}
/*
* find_cl_certs - Searches a keystore for user certificates
*
* Arguments:
* err - Error object to add errors to
* ks - Keystore to search
* cacerts - resulting set of user certs are placed here
*
* No matching of any kind is performed.
* Returns:
* 0 - Success - trusted cert list returned in cacerts
* non-zero - Failure, errors recorded in err
*/
/* ARGSUSED */
int
find_cl_certs(PKG_ERR *err, keystore_handle_t ks_h, STACK_OF(X509) **clcerts)
{
keystore_t *ks = ks_h;
/* easy */
*clcerts = ks->clcerts;
return (0);
}
/*
* merge_cert_and_key - Adds a user certificate and matching
* private key to a keystore.
* certificate checked for validity dates and non-duplicity.
*
* Arguments:
* err - Error object to add errors to
* cert - Certificate which to merge into keystore
* key - matching private key to 'cert'
* alias - Name which to store the cert and key under
* keystore - The keystore into which the certificate is merged
*
* Returns:
* 0 - Success - Certificate passes validity, and
* is merged into keystore, along with key
* non-zero - Failure, errors recorded in err
*/
int
merge_cert_and_key(PKG_ERR *err, X509 *cert, EVP_PKEY *key, char *alias,
keystore_handle_t keystore_h)
{
X509 *existingcert = NULL;
EVP_PKEY *existingkey = NULL;
int ret = 0;
keystore_t *keystore = keystore_h;
/* check validity dates */
if (check_cert(err, cert) != 0) {
ret = 1;
goto cleanup;
}
/* set the friendlyName of the key and cert to the supplied alias */
if (sunw_set_fname(alias, key, cert) != 0) {
pkgerr_add(err, PKGERR_NOMEM, gettext(ERR_MEM));
ret = 1;
goto cleanup;
}
/* merge certificate and key into the keystore */
if (keystore->clcerts == NULL) {
/* no existing truststore, so make a new one */
if ((keystore->clcerts = sk_X509_new_null()) == NULL) {
pkgerr_add(err, PKGERR_NOMEM, gettext(ERR_MEM));
ret = 1;
goto cleanup;
}
} else {
/* existing certstore, make sure there's no duplicate */
if (sunw_find_fname(alias, NULL, keystore->clcerts,
NULL, &existingcert) < 0) {
pkgerr_add(err, PKGERR_INTERNAL,
gettext(ERR_KEYSTORE_INTERNAL),
__FILE__, __LINE__);
ERR_print_errors_fp(stderr);
ret = 1;
goto cleanup;
/* could not search properly! */
}
if (existingcert != NULL) {
/* whoops, found one already */
pkgerr_add(err, PKGERR_DUPLICATE,
gettext(ERR_KEYSTORE_DUPLICATECERT), alias);
ret = 1;
goto cleanup;
}
}
if (keystore->pkeys == NULL) {
/* no existing keystore, so make a new one */
if ((keystore->pkeys = sk_EVP_PKEY_new_null()) == NULL) {
pkgerr_add(err, PKGERR_NOMEM, gettext(ERR_MEM));
ret = 1;
goto cleanup;
}
} else {
/* existing keystore, so make sure there's no duplicate entry */
if (sunw_find_fname(alias, keystore->pkeys, NULL,
&existingkey, NULL) < 0) {
pkgerr_add(err, PKGERR_INTERNAL,
gettext(ERR_KEYSTORE_INTERNAL),
__FILE__, __LINE__);
ERR_print_errors_fp(stderr);
ret = 1;
goto cleanup;
/* could not search properly! */
}
if (existingkey != NULL) {
/* whoops, found one already */
pkgerr_add(err, PKGERR_DUPLICATE,
gettext(ERR_KEYSTORE_DUPLICATEKEY), alias);
ret = 1;
goto cleanup;
}
}
(void) sk_X509_push(keystore->clcerts, cert);
(void) sk_EVP_PKEY_push(keystore->pkeys, key);
keystore->dirty = B_TRUE;
cleanup:
if (existingcert != NULL)
(void) X509_free(existingcert);
if (existingkey != NULL)
(void) sunw_evp_pkey_free(existingkey);
return (ret);
}
/*
* delete_cert_and_keys - Deletes one or more certificates
* and matching private keys from a keystore.
*
* Arguments:
* err - Error object to add errors to
* ks - The keystore from which certs and keys are deleted
* alias - Name which to search for certificates and keys
* to delete
*
* Returns:
* 0 - Success - All trusted certs which match 'alias'
* are deleted. All user certificates
* which match 'alias' are deleted, along
* with the matching private key.
* non-zero - Failure, errors recorded in err
*/
int
delete_cert_and_keys(PKG_ERR *err, keystore_handle_t ks_h, char *alias)
{
X509 *existingcert;
EVP_PKEY *existingkey;
int i;
char *fname = NULL;
boolean_t found = B_FALSE;
keystore_t *ks = ks_h;
/* delete any and all client certs with the supplied name */
if (ks->clcerts != NULL) {
for (i = 0; i < sk_X509_num(ks->clcerts); i++) {
existingcert = sk_X509_value(ks->clcerts, i);
if (sunw_get_cert_fname(GETDO_COPY,
existingcert, &fname) >= 0) {
if (streq(fname, alias)) {
/* match, so nuke it */
existingcert =
sk_X509_delete(ks->clcerts, i);
X509_free(existingcert);
existingcert = NULL;
found = B_TRUE;
}
(void) OPENSSL_free(fname);
fname = NULL;
}
}
if (sk_X509_num(ks->clcerts) <= 0) {
/* we deleted all the client certs */
sk_X509_free(ks->clcerts);
ks->clcerts = NULL;
}
}
/* and now the private keys */
if (ks->pkeys != NULL) {
for (i = 0; i < sk_EVP_PKEY_num(ks->pkeys); i++) {
existingkey = sk_EVP_PKEY_value(ks->pkeys, i);
if (sunw_get_pkey_fname(GETDO_COPY,
existingkey, &fname) >= 0) {
if (streq(fname, alias)) {
/* match, so nuke it */
existingkey =
sk_EVP_PKEY_delete(ks->pkeys, i);
sunw_evp_pkey_free(existingkey);
existingkey = NULL;
found = B_TRUE;
}
(void) OPENSSL_free(fname);
fname = NULL;
}
}
if (sk_EVP_PKEY_num(ks->pkeys) <= 0) {
/* we deleted all the private keys */
sk_EVP_PKEY_free(ks->pkeys);
ks->pkeys = NULL;
}
}
/* finally, remove any trust anchors that match */
if (ks->cacerts != NULL) {
for (i = 0; i < sk_X509_num(ks->cacerts); i++) {
existingcert = sk_X509_value(ks->cacerts, i);
if (sunw_get_cert_fname(GETDO_COPY,
existingcert, &fname) >= 0) {
if (streq(fname, alias)) {
/* match, so nuke it */
existingcert =
sk_X509_delete(ks->cacerts, i);
X509_free(existingcert);
existingcert = NULL;
found = B_TRUE;
}
(void) OPENSSL_free(fname);
fname = NULL;
}
}
if (sk_X509_num(ks->cacerts) <= 0) {
/* we deleted all the CA certs */
sk_X509_free(ks->cacerts);
ks->cacerts = NULL;
}
}
if (found) {
ks->dirty = B_TRUE;
return (0);
} else {
/* no certs or keys deleted */
pkgerr_add(err, PKGERR_NOALIASMATCH,
gettext(ERR_KEYSTORE_NOCERTKEY),
alias, ks->path);
return (1);
}
}
/*
* check_cert - Checks certificate validity. This routine
* checks that the current time falls within the period
* of validity for the cert.
*
* Arguments:
* err - Error object to add errors to
* cert - The certificate to check
*
* Returns:
* 0 - Success - Certificate checks out
* non-zero - Failure, errors and reasons recorded in err
*/
int
check_cert(PKG_ERR *err, X509 *cert)
{
char currtimestr[ATTR_MAX];
time_t currtime;
char *r, *before_str, *after_str;
/* get current time */
if ((currtime = time(NULL)) == (time_t)-1) {
pkgerr_add(err, PKGERR_TIME, gettext(ERR_CURR_TIME));
return (1);
}
(void) strlcpy(currtimestr, ctime(&currtime), ATTR_MAX);
/* trim whitespace from end of time string */
for (r = (currtimestr + strlen(currtimestr) - 1); isspace(*r); r--) {
*r = '\0';
}
/* check validity of cert */
switch (sunw_check_cert_times(CHK_BOTH, cert)) {
case CHKERR_TIME_OK:
/* Current time meets requested checks */
break;
case CHKERR_TIME_BEFORE_BAD:
/* 'not before' field is invalid */
case CHKERR_TIME_AFTER_BAD:
/* 'not after' field is invalid */
pkgerr_add(err, PKGERR_TIME, gettext(ERR_CERT_TIME_BAD));
return (1);
case CHKERR_TIME_IS_BEFORE:
/* Current time is before 'not before' */
case CHKERR_TIME_HAS_EXPIRED:
/*
* Ignore expiration time since the trust cert used to
* verify the certs used to sign Sun patches is already
* expired. Once the patches get resigned with the new
* cert we will check expiration against the time the
* patch was signed and not the time it is installed.
*/
return (0);
default:
pkgerr_add(err, PKGERR_INTERNAL,
gettext(ERR_KEYSTORE_INTERNAL),
__FILE__, __LINE__);
return (1);
}
/* all checks ok */
return (0);
}
/*
* check_cert - Checks certificate validity. This routine
* checks everything that check_cert checks, and additionally
* verifies that the private key and corresponding public
* key are indeed a pair.
*
* Arguments:
* err - Error object to add errors to
* cert - The certificate to check
* key - the key to check
* Returns:
* 0 - Success - Certificate checks out
* non-zero - Failure, errors and reasons recorded in err
*/
int
check_cert_and_key(PKG_ERR *err, X509 *cert, EVP_PKEY *key)
{
/* check validity dates */
if (check_cert(err, cert) != 0) {
return (1);
}
/* check key pair match */
if (sunw_check_keys(cert, key) == 0) {
pkgerr_add(err, PKGERR_VERIFY, gettext(ERR_MISMATCHED_KEYS),
get_subject_display_name(cert));
return (1);
}
/* all checks OK */
return (0);
}
/* ------------------ private functions ---------------------- */
/*
* verify_keystore_integrity - Searches for the remnants
* of a failed or aborted keystore modification, and
* cleans up the files, retstores the keystore to a known
* state.
*
* Arguments:
* err - Error object to add errors to
* keystore_file - Base directory or filename of keystore
* app - Application making request
*
* Returns:
* 0 - Success - Keystore is restored, or untouched in the
* case that cleanup was unnecessary
* non-zero - Failure, errors and reasons recorded in err
*/
static boolean_t
verify_keystore_integrity(PKG_ERR *err, keystore_t *keystore)
{
if (keystore->capath != NULL) {
if (!restore_keystore_file(err, keystore->capath)) {
return (B_FALSE);
}
}
if (keystore->clpath != NULL) {
if (!restore_keystore_file(err, keystore->clpath)) {
return (B_FALSE);
}
}
if (keystore->keypath != NULL) {
if (!restore_keystore_file(err, keystore->keypath)) {
return (B_FALSE);
}
}
return (B_TRUE);
}
/*
* restore_keystore_file - restores a keystore file to
* a known state.
*
* Keystore files can possibly be corrupted by a variety
* of error conditions during reading/writing. This
* routine, along with write_keystore_file, tries to
* maintain keystore integrity by writing the files
* out in a particular order, minimizing the time period
* that the keystore is in an indeterminate state.
*
* With the current implementation, there are some failures
* that are wholly unrecoverable, such as disk corruption.
* These routines attempt to minimize the risk, but not
* eliminate it. When better, atomic operations are available
* (such as a trued atabase with commit, rollback, and
* guaranteed atomicity), this implementation should use that.
*
* Arguments:
* err - Error object to add errors to
* keystore_file - keystore file path to restore.
*
* Returns:
* 0 - Success - Keystore file is restored, or untouched in the
* case that cleanup was unnecessary
* non-zero - Failure, errors and reasons recorded in err
*/
/* ARGSUSED */
static boolean_t
restore_keystore_file(PKG_ERR *err, char *keystore_file)
{
char newpath[MAXPATHLEN];
char backuppath[MAXPATHLEN];
int newfd;
struct stat buf;
int len;
if (((len = snprintf(newpath, MAXPATHLEN, "%s.new",
keystore_file)) < 0) ||
(len >= ATTR_MAX)) {
pkgerr_add(err, PKGERR_WEB, gettext(ERR_LEN), keystore_file);
return (B_FALSE);
}
if (((len = snprintf(backuppath, MAXPATHLEN, "%s.bak",
keystore_file)) < 0) ||
(len >= ATTR_MAX)) {
pkgerr_add(err, PKGERR_WEB, gettext(ERR_LEN), keystore_file);
return (B_FALSE);
}
if ((newfd = open(newpath, O_RDWR|O_NONBLOCK, 0)) != -1) {
if (fstat(newfd, &buf) != -1) {
if (S_ISREG(buf.st_mode)) {
/*
* restore the file, waiting on it
* to be free for locking, or for
* it to disappear
*/
if (!wait_restore(newfd, keystore_file,
newpath, backuppath)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_WRITE),
newpath, strerror(errno));
(void) close(newfd);
return (B_FALSE);
} else {
return (B_TRUE);
}
} else {
/* "new" file is not a regular file */
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_NOT_REG), newpath);
(void) close(newfd);
return (B_FALSE);
}
} else {
/* couldn't stat "new" file */
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_WRITE), newpath,
strerror(errno));
(void) close(newfd);
return (B_FALSE);
}
} else {
/* "new" file doesn't exist */
return (B_TRUE);
}
}
static boolean_t
wait_restore(int newfd, char *keystore_file,
char *origpath, char *backuppath)
{
struct stat buf;
FILE *newstream;
PKCS12 *p12;
(void) alarm(LOCK_TIMEOUT);
if (file_lock(newfd, F_WRLCK, 1) == -1) {
/* could not lock file */
(void) alarm(0);
return (B_FALSE);
}
(void) alarm(0);
if (fstat(newfd, &buf) != -1) {
if (S_ISREG(buf.st_mode)) {
/*
* The new file still
* exists, with no
* owner. It must be
* the result of an
* aborted update.
*/
newstream = fdopen(newfd, "r");
if ((p12 =
d2i_PKCS12_fp(newstream,
NULL)) != NULL) {
/*
* The file
* appears
* complete.
* Replace the
* exsisting
* keystore
* file with
* this one
*/
(void) rename(keystore_file, backuppath);
(void) rename(origpath, keystore_file);
PKCS12_free(p12);
} else {
/* The file is not complete. Remove it */
(void) remove(origpath);
}
/* remove backup file */
(void) remove(backuppath);
(void) fclose(newstream);
(void) close(newfd);
return (B_TRUE);
} else {
/*
* new file exists, but is not a
* regular file
*/
(void) close(newfd);
return (B_FALSE);
}
} else {
/*
* could not stat file. Unless
* the reason was that the file
* is now gone, this is an error
*/
if (errno != ENOENT) {
(void) close(newfd);
return (B_FALSE);
}
/*
* otherwise, file is gone. The process
* that held the lock must have
* successfully cleaned up and
* exited with a valid keystore
* state
*/
(void) close(newfd);
return (B_TRUE);
}
}
/*
* resolve_paths - figure out if we are dealing with a single-file
* or multi-file keystore
*
* The flags tell resolve_paths how to behave:
*
* KEYSTORE_PATH_SOFT
* If the keystore file does not exist at <base>/<app> then
* use <base> as the path to the keystore. This can be used,
* for example, to access an app-specific keystore iff it
* exists, otherwise revert back to an app-generic keystore.
*
* KEYSTORE_PATH_HARD
* Always use the keystore located at <keystore_path>/<app>.
* In read/write mode, if the files do not exist, then
* they will be created. This is used to avoid falling
* back to an app-generic keystore path when the app-specific
* one does not exist.
*
* Arguments:
* err - Error object to add errors to
* keystore_file - base keystore file path to lock
* app - Application making requests
* flags - Control flags (see above description)
* keystore - object which is being locked
*
* Returns:
* B_TRUE - Success - Keystore file is locked, paths to
* appropriate files placed in keystore.
* B_FALSE - Failure, errors and reasons recorded in err
*/
static boolean_t
resolve_paths(PKG_ERR *err, char *keystore_file, char *app,
long flags, keystore_t *keystore)
{
char storepath[PATH_MAX];
struct stat buf;
boolean_t multi = B_FALSE;
int fd1, fd2, len;
/*
* figure out whether we are dealing with a single-file keystore
* or a multi-file keystore
*/
if (app != NULL) {
if (((len = snprintf(storepath, PATH_MAX, "%s/%s",
keystore_file, app)) < 0) ||
(len >= ATTR_MAX)) {
pkgerr_add(err, PKGERR_WEB, gettext(ERR_LEN),
keystore_file);
return (B_FALSE);
}
if (((fd1 = open(storepath, O_NONBLOCK|O_RDONLY)) == -1) ||
(fstat(fd1, &buf) == -1) ||
!S_ISDIR(buf.st_mode)) {
/*
* app-specific does not exist
* fallback to app-generic, if flags say we can
*/
if ((flags & KEYSTORE_PATH_MASK) ==
KEYSTORE_PATH_SOFT) {
if (((fd2 = open(keystore_file,
O_NONBLOCK|O_RDONLY)) != -1) &&
(fstat(fd2, &buf) != -1)) {
if (S_ISDIR(buf.st_mode)) {
/*
* app-generic dir
* exists, so use it
* as a multi-file
* keystore
*/
multi = B_TRUE;
app = NULL;
} else if (S_ISREG(buf.st_mode)) {
/*
* app-generic file exists, so
* use it as a single file ks
*/
multi = B_FALSE;
app = NULL;
}
}
}
}
if (fd1 != -1)
(void) close(fd1);
if (fd2 != -1)
(void) close(fd2);
} else {
if (((fd1 = open(keystore_file,
O_NONBLOCK|O_RDONLY)) != -1) &&
(fstat(fd1, &buf) != -1) &&
S_ISDIR(buf.st_mode)) {
/*
* app-generic dir exists, so use
* it as a multi-file keystore
*/
multi = B_TRUE;
}
if (fd1 != -1)
(void) close(fd1);
}
if (app != NULL) {
/* app-specific keystore */
(void) snprintf(storepath, PATH_MAX, "%s/%s/%s",
keystore_file, app, TRUSTSTORE);
keystore->capath = xstrdup(storepath);
(void) snprintf(storepath, PATH_MAX, "%s/%s/%s",
keystore_file, app, CERTSTORE);
keystore->clpath = xstrdup(storepath);
(void) snprintf(storepath, PATH_MAX, "%s/%s/%s",
keystore_file, app, KEYSTORE);
keystore->keypath = xstrdup(storepath);
} else {
/* app-generic keystore */
if (!multi) {
/* single-file app-generic keystore */
keystore->capath = xstrdup(keystore_file);
keystore->keypath = NULL;
keystore->clpath = NULL;
} else {
/* multi-file app-generic keystore */
(void) snprintf(storepath, PATH_MAX, "%s/%s",
keystore_file, TRUSTSTORE);
keystore->capath = xstrdup(storepath);
(void) snprintf(storepath, PATH_MAX, "%s/%s",
keystore_file, CERTSTORE);
keystore->clpath = xstrdup(storepath);
(void) snprintf(storepath, PATH_MAX, "%s/%s",
keystore_file, KEYSTORE);
keystore->keypath = xstrdup(storepath);
}
}
return (B_TRUE);
}
/*
* lock_keystore - Locks a keystore for shared (read-only)
* or exclusive (read-write) access.
*
* The flags tell lock_keystore how to behave:
*
* KEYSTORE_ACCESS_READONLY
* opens keystore read-only. Attempts to modify results in an error
*
* KEYSTORE_ACCESS_READWRITE
* opens keystore read-write
*
* KEYSTORE_PATH_SOFT
* If the keystore file does not exist at <base>/<app> then
* use <base> as the path to the keystore. This can be used,
* for example, to access an app-specific keystore iff it
* exists, otherwise revert back to an app-generic keystore.
*
* KEYSTORE_PATH_HARD
* Always use the keystore located at <keystore_path>/<app>.
* In read/write mode, if the files do not exist, then
* they will be created. This is used to avoid falling
* back to an app-generic keystore path when the app-specific
* one does not exist.
*
* Arguments:
* err - Error object to add errors to
* flags - Control flags (see above description)
* keystore - object which is being locked
*
* Returns:
* 0 - Success - Keystore file is locked, paths to
* appropriate files placed in keystore.
* non-zero - Failure, errors and reasons recorded in err
*/
static boolean_t
lock_keystore(PKG_ERR *err, long flags, keystore_t *keystore)
{
boolean_t ret = B_TRUE;
struct stat buf;
switch (flags & KEYSTORE_ACCESS_MASK) {
case KEYSTORE_ACCESS_READONLY:
if ((keystore->cafd =
open(keystore->capath, O_NONBLOCK|O_RDONLY)) == -1) {
if (errno == ENOENT) {
/*
* no keystore. try to create an
* empty one so we can lock on it and
* prevent others from gaining
* exclusive access. It will be
* deleted when the keystore is closed.
*/
if ((keystore->cafd =
open(keystore->capath,
O_NONBLOCK|O_RDWR|O_CREAT|O_EXCL,
S_IRUSR|S_IWUSR)) == -1) {
pkgerr_add(err, PKGERR_READ,
gettext(ERR_NO_KEYSTORE),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
} else {
pkgerr_add(err, PKGERR_READ,
gettext(ERR_KEYSTORE_OPEN),
keystore->capath, strerror(errno));
ret = B_FALSE;
goto cleanup;
}
}
if (fstat(keystore->cafd, &buf) != -1) {
if (S_ISREG(buf.st_mode)) {
if (file_lock(keystore->cafd, F_RDLCK,
0) == -1) {
pkgerr_add(err, PKGERR_LOCKED,
gettext(ERR_KEYSTORE_LOCKED_READ),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
} else {
/* ca file not a regular file! */
pkgerr_add(err, PKGERR_READ,
gettext(ERR_NOT_REG),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
} else {
pkgerr_add(err, PKGERR_READ,
gettext(ERR_KEYSTORE_OPEN),
keystore->capath, strerror(errno));
ret = B_FALSE;
goto cleanup;
}
break;
case KEYSTORE_ACCESS_READWRITE:
if ((keystore->cafd = open(keystore->capath,
O_RDWR|O_NONBLOCK)) == -1) {
/* does not exist. try to create an empty one */
if (errno == ENOENT) {
if ((keystore->cafd =
open(keystore->capath,
O_NONBLOCK|O_RDWR|O_CREAT|O_EXCL,
S_IRUSR|S_IWUSR)) == -1) {
pkgerr_add(err, PKGERR_READ,
gettext(ERR_KEYSTORE_WRITE),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
} else {
pkgerr_add(err, PKGERR_READ,
gettext(ERR_KEYSTORE_OPEN),
keystore->capath, strerror(errno));
ret = B_FALSE;
goto cleanup;
}
}
if (fstat(keystore->cafd, &buf) != -1) {
if (S_ISREG(buf.st_mode)) {
if (file_lock(keystore->cafd, F_WRLCK,
0) == -1) {
pkgerr_add(err, PKGERR_LOCKED,
gettext(ERR_KEYSTORE_LOCKED),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
} else {
/* ca file not a regular file! */
pkgerr_add(err, PKGERR_READ,
gettext(ERR_NOT_REG),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
} else {
pkgerr_add(err, PKGERR_READ,
gettext(ERR_KEYSTORE_OPEN),
keystore->capath, strerror(errno));
ret = B_FALSE;
goto cleanup;
}
break;
default:
pkgerr_add(err, PKGERR_INTERNAL,
gettext(ERR_KEYSTORE_INTERNAL),
__FILE__, __LINE__);
ret = B_FALSE;
goto cleanup;
}
cleanup:
if (!ret) {
if (keystore->cafd > 0) {
(void) file_unlock(keystore->cafd);
(void) close(keystore->cafd);
keystore->cafd = -1;
}
if (keystore->capath != NULL)
free(keystore->capath);
if (keystore->clpath != NULL)
free(keystore->clpath);
if (keystore->keypath != NULL)
free(keystore->keypath);
keystore->capath = NULL;
keystore->clpath = NULL;
keystore->keypath = NULL;
}
return (ret);
}
/*
* unlock_keystore - Unocks a keystore
*
* Arguments:
* err - Error object to add errors to
* keystore - keystore object to unlock
* Returns:
* 0 - Success - Keystore files are unlocked, files are closed,
* non-zero - Failure, errors and reasons recorded in err
*/
/* ARGSUSED */
static boolean_t
unlock_keystore(PKG_ERR *err, keystore_t *keystore)
{
/*
* Release lock on the CA file.
* Delete file if it is empty
*/
if (file_empty(keystore->capath)) {
(void) remove(keystore->capath);
}
(void) file_unlock(keystore->cafd);
(void) close(keystore->cafd);
return (B_TRUE);
}
/*
* read_keystore - Reads keystore files of disk, parses
* into internal structures.
*
* Arguments:
* err - Error object to add errors to
* keystore - keystore object to read into
* cb - callback to get password, if required
* Returns:
* 0 - Success - Keystore files are read, and placed
* into keystore structure.
* non-zero - Failure, errors and reasons recorded in err
*/
static boolean_t
read_keystore(PKG_ERR *err, keystore_t *keystore, keystore_passphrase_cb cb)
{
boolean_t ret = B_TRUE;
PKCS12 *p12 = NULL;
boolean_t ca_empty;
boolean_t have_passwd = B_FALSE;
boolean_t cl_empty = B_TRUE;
boolean_t key_empty = B_TRUE;
ca_empty = file_empty(keystore->capath);
if (keystore->clpath != NULL)
cl_empty = file_empty(keystore->clpath);
if (keystore->keypath != NULL)
key_empty = file_empty(keystore->keypath);
if (ca_empty && cl_empty && key_empty) {
keystore->new = B_TRUE;
}
if (!ca_empty) {
/* first read the ca file */
if ((p12 = read_keystore_file(err,
keystore->capath)) == NULL) {
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_CORRUPT), keystore->capath);
ret = B_FALSE;
goto cleanup;
}
/* Get password, using callback if necessary */
if (!have_passwd) {
if (!get_keystore_passwd(err, p12, cb, keystore)) {
ret = B_FALSE;
goto cleanup;
}
have_passwd = B_TRUE;
}
/* decrypt and parse keystore file */
if (sunw_PKCS12_contents(p12, keystore->passphrase,
&keystore->pkeys, &keystore->cacerts) < 0) {
/* could not parse the contents */
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_CORRUPT), keystore->capath);
ret = B_FALSE;
goto cleanup;
}
PKCS12_free(p12);
p12 = NULL;
} else {
/*
* truststore is empty, so we don't have any trusted
* certs
*/
keystore->cacerts = NULL;
}
/*
* if there is no cl file or key file, use the cl's and key's found
* in the ca file
*/
if (keystore->clpath == NULL && !ca_empty) {
if (sunw_split_certs(keystore->pkeys, keystore->cacerts,
&keystore->clcerts, NULL) < 0) {
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_CORRUPT), keystore->capath);
ret = B_FALSE;
goto cleanup;
}
} else {
/*
* files are in separate files. read keys out of the keystore
* certs out of the certstore, if they are not empty
*/
if (!cl_empty) {
if ((p12 = read_keystore_file(err,
keystore->clpath)) == NULL) {
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_CORRUPT),
keystore->clpath);
ret = B_FALSE;
goto cleanup;
}
/* Get password, using callback if necessary */
if (!have_passwd) {
if (!get_keystore_passwd(err, p12, cb,
keystore)) {
ret = B_FALSE;
goto cleanup;
}
have_passwd = B_TRUE;
}
if (check_password(p12,
keystore->passphrase) == B_FALSE) {
/*
* password in client cert file
* is different than
* the one in the other files!
*/
pkgerr_add(err, PKGERR_BADPASS,
gettext(ERR_MISMATCHPASS),
keystore->clpath,
keystore->capath, keystore->path);
ret = B_FALSE;
goto cleanup;
}
if (sunw_PKCS12_contents(p12, keystore->passphrase,
NULL, &keystore->clcerts) < 0) {
/* could not parse the contents */
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_CORRUPT),
keystore->clpath);
ret = B_FALSE;
goto cleanup;
}
PKCS12_free(p12);
p12 = NULL;
} else {
keystore->clcerts = NULL;
}
if (!key_empty) {
if ((p12 = read_keystore_file(err,
keystore->keypath)) == NULL) {
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_CORRUPT),
keystore->keypath);
ret = B_FALSE;
goto cleanup;
}
/* Get password, using callback if necessary */
if (!have_passwd) {
if (!get_keystore_passwd(err, p12, cb,
keystore)) {
ret = B_FALSE;
goto cleanup;
}
have_passwd = B_TRUE;
}
if (check_password(p12,
keystore->passphrase) == B_FALSE) {
pkgerr_add(err, PKGERR_BADPASS,
gettext(ERR_MISMATCHPASS),
keystore->keypath,
keystore->capath, keystore->path);
ret = B_FALSE;
goto cleanup;
}
if (sunw_PKCS12_contents(p12, keystore->passphrase,
&keystore->pkeys, NULL) < 0) {
/* could not parse the contents */
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_CORRUPT),
keystore->keypath);
ret = B_FALSE;
goto cleanup;
}
PKCS12_free(p12);
p12 = NULL;
} else {
keystore->pkeys = NULL;
}
}
cleanup:
if (p12 != NULL)
PKCS12_free(p12);
return (ret);
}
/*
* get_keystore_password - retrieves pasword used to
* decrypt PKCS12 structure.
*
* Arguments:
* err - Error object to add errors to
* p12 - PKCS12 structure which returned password should
* decrypt
* cb - callback to collect password.
* keystore - The keystore in which the PKCS12 structure
* will eventually populate.
* Returns:
* B_TRUE - success.
* keystore password is set in keystore->passphrase.
* B_FALSE - failure, errors logged
*/
static boolean_t
get_keystore_passwd(PKG_ERR *err, PKCS12 *p12, keystore_passphrase_cb cb,
keystore_t *keystore)
{
char *passwd;
char passbuf[KEYSTORE_PASS_MAX + 1];
keystore_passphrase_data data;
/* see if no password is the right password */
if (check_password(p12, "") == B_TRUE) {
passwd = "";
} else if (check_password(p12, NULL) == B_TRUE) {
passwd = NULL;
} else {
/* oops, it's encrypted. get password */
data.err = err;
if (cb(passbuf, KEYSTORE_PASS_MAX, 0,
&data) == -1) {
/* could not get password */
return (B_FALSE);
}
if (check_password(p12, passbuf) == B_FALSE) {
/* wrong password */
pkgerr_add(err, PKGERR_BADPASS,
gettext(ERR_BADPASS));
return (B_FALSE);
}
/*
* make copy of password buffer, since it
* goes away upon return
*/
passwd = xstrdup(passbuf);
}
keystore->passphrase = passwd;
return (B_TRUE);
}
/*
* write_keystore - Writes keystore files to disk
*
* Arguments:
* err - Error object to add errors to
* keystore - keystore object to write from
* passwd - password used to encrypt keystore
* Returns:
* 0 - Success - Keystore contents are written out to
* the same locations as read from
* non-zero - Failure, errors and reasons recorded in err
*/
static boolean_t
write_keystore(PKG_ERR *err, keystore_t *keystore,
keystore_passphrase_cb cb)
{
PKCS12 *p12 = NULL;
boolean_t ret = B_TRUE;
keystore_passphrase_data data;
char passbuf[KEYSTORE_PASS_MAX + 1];
if (keystore->capath != NULL && keystore->clpath == NULL &&
keystore->keypath == NULL) {
/*
* keystore is a file.
* just write out a single file
*/
if ((keystore->pkeys == NULL) &&
(keystore->clcerts == NULL) &&
(keystore->cacerts == NULL)) {
if (!clear_keystore_file(err, keystore->capath)) {
/*
* no keys or certs to write out, so
* blank the ca file. we do not
* delete it since it is used as a
* lock by lock_keystore() in
* subsequent invocations
*/
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_WRITE),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
} else {
/*
* if the keystore is being created for the first time,
* prompt for a passphrase for encryption
*/
if (keystore->new) {
data.err = err;
if (cb(passbuf, KEYSTORE_PASS_MAX,
1, &data) == -1) {
ret = B_FALSE;
goto cleanup;
}
} else {
/*
* use the one used when the keystore
* was read
*/
strlcpy(passbuf, keystore->passphrase,
KEYSTORE_PASS_MAX);
}
p12 = sunw_PKCS12_create(passbuf, keystore->pkeys,
keystore->clcerts, keystore->cacerts);
if (p12 == NULL) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_FORM),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
if (!write_keystore_file(err, keystore->capath, p12)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_WRITE),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
}
} else {
/* files are seprate. Do one at a time */
/*
* if the keystore is being created for the first time,
* prompt for a passphrase for encryption
*/
if (keystore->new && ((keystore->pkeys != NULL) ||
(keystore->clcerts != NULL) ||
(keystore->cacerts != NULL))) {
data.err = err;
if (cb(passbuf, KEYSTORE_PASS_MAX,
1, &data) == -1) {
ret = B_FALSE;
goto cleanup;
}
} else {
/* use the one used when the keystore was read */
strlcpy(passbuf, keystore->passphrase,
KEYSTORE_PASS_MAX);
}
/* do private keys first */
if (keystore->pkeys != NULL) {
p12 = sunw_PKCS12_create(passbuf, keystore->pkeys,
NULL, NULL);
if (p12 == NULL) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_FORM),
keystore->keypath);
ret = B_FALSE;
goto cleanup;
}
if (!write_keystore_file(err, keystore->keypath,
p12)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_WRITE),
keystore->keypath);
ret = B_FALSE;
goto cleanup;
}
PKCS12_free(p12);
} else {
if ((remove(keystore->keypath) != 0) &&
(errno != ENOENT)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_REMOVE),
keystore->keypath);
ret = B_FALSE;
goto cleanup;
}
}
/* do user certs next */
if (keystore->clcerts != NULL) {
p12 = sunw_PKCS12_create(passbuf, NULL,
keystore->clcerts, NULL);
if (p12 == NULL) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_FORM),
keystore->clpath);
ret = B_FALSE;
goto cleanup;
}
if (!write_keystore_file(err, keystore->clpath, p12)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_WRITE),
keystore->clpath);
ret = B_FALSE;
goto cleanup;
}
PKCS12_free(p12);
} else {
if ((remove(keystore->clpath) != 0) &&
(errno != ENOENT)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_REMOVE),
keystore->clpath);
ret = B_FALSE;
goto cleanup;
}
}
/* finally do CA cert file */
if (keystore->cacerts != NULL) {
p12 = sunw_PKCS12_create(passbuf, NULL,
NULL, keystore->cacerts);
if (p12 == NULL) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_FORM),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
if (!write_keystore_file(err, keystore->capath, p12)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_WRITE),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
PKCS12_free(p12);
p12 = NULL;
} else {
/*
* nothing to write out, so truncate the file
* (it will be deleted during close_keystore)
*/
if (!clear_keystore_file(err, keystore->capath)) {
pkgerr_add(err, PKGERR_WRITE,
gettext(ERR_KEYSTORE_WRITE),
keystore->capath);
ret = B_FALSE;
goto cleanup;
}
}
}
cleanup:
if (p12 != NULL)
PKCS12_free(p12);
return (ret);
}
/*
* clear_keystore_file - Clears (zeros out) a keystore file.
*
* Arguments:
* err - Error object to add errors to
* dest - Path of keystore file to zero out.
* Returns:
* 0 - Success - Keystore file is truncated to zero length
* non-zero - Failure, errors and reasons recorded in err
*/
static boolean_t
clear_keystore_file(PKG_ERR *err, char *dest)
{
int fd;
struct stat buf;
fd = open(dest, O_RDWR|O_NONBLOCK);
if (fd == -1) {
/* can't open for writing */
pkgerr_add(err, PKGERR_WRITE, gettext(MSG_OPEN),
errno);
return (B_FALSE);
}
if ((fstat(fd, &buf) == -1) || !S_ISREG(buf.st_mode)) {
/* not a regular file */
(void) close(fd);
pkgerr_add(err, PKGERR_WRITE, gettext(ERR_NOT_REG),
dest);
return (B_FALSE);
}
if (ftruncate(fd, 0) == -1) {
(void) close(fd);
pkgerr_add(err, PKGERR_WRITE, gettext(ERR_WRITE),
dest, strerror(errno));
return (B_FALSE);
}
(void) close(fd);
return (B_TRUE);
}
/*
* write_keystore_file - Writes keystore file to disk.
*
* Keystore files can possibly be corrupted by a variety
* of error conditions during reading/writing. This
* routine, along with restore_keystore_file, tries to
* maintain keystore integity by writing the files
* out in a particular order, minimizing the time period
* that the keystore is in an indeterminate state.
*
* With the current implementation, there are some failures
* that are wholly unrecoverable, such as disk corruption.
* These routines attempt to minimize the risk, but not
* eliminate it. When better, atomic operations are available
* (such as a true database with commit, rollback, and
* guaranteed atomicity), this implementation should use that.
*
*
* Arguments:
* err - Error object to add errors to
* dest - Destination filename
* contents - Contents to write to the file
* Returns:
* 0 - Success - Keystore contents are written out to
* the destination.
* non-zero - Failure, errors and reasons recorded in err
*/
static boolean_t
write_keystore_file(PKG_ERR *err, char *dest, PKCS12 *contents)
{
FILE *newfile = NULL;
boolean_t ret = B_TRUE;
char newpath[MAXPATHLEN];
char backuppath[MAXPATHLEN];
struct stat buf;
int fd;
(void) snprintf(newpath, MAXPATHLEN, "%s.new", dest);
(void) snprintf(backuppath, MAXPATHLEN, "%s.bak", dest);
if ((fd = open(newpath, O_CREAT|O_EXCL|O_WRONLY|O_NONBLOCK,
S_IRUSR|S_IWUSR)) == -1) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_KEYSTORE_OPEN),
newpath, strerror(errno));
ret = B_FALSE;
goto cleanup;
}
if (fstat(fd, &buf) == -1) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_KEYSTORE_OPEN),
newpath, strerror(errno));
ret = B_FALSE;
goto cleanup;
}
if (!S_ISREG(buf.st_mode)) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_NOT_REG),
newpath);
ret = B_FALSE;
goto cleanup;
}
if ((newfile = fdopen(fd, "w")) == NULL) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_KEYSTORE_OPEN),
newpath, strerror(errno));
ret = B_FALSE;
goto cleanup;
}
if (i2d_PKCS12_fp(newfile, contents) == 0) {
pkgerr_add(err, PKGERR_WRITE, gettext(ERR_KEYSTORE_WRITE),
newpath);
ret = B_FALSE;
goto cleanup;
}
/* flush, then close */
(void) fflush(newfile);
(void) fclose(newfile);
newfile = NULL;
/* now back up the original file */
(void) rename(dest, backuppath);
/* put new one in its place */
(void) rename(newpath, dest);
/* remove backup */
(void) remove(backuppath);
cleanup:
if (newfile != NULL)
(void) fclose(newfile);
if (fd != -1)
(void) close(fd);
return (ret);
}
/*
* read_keystore_file - Reads single keystore file
* off disk in PKCS12 format.
*
* Arguments:
* err - Error object to add errors to
* file - File path to read
* Returns:
* PKCS12 contents of file, or NULL if an error occurred.
* errors recorded in 'err'.
*/
static PKCS12
*read_keystore_file(PKG_ERR *err, char *file)
{
int fd;
struct stat buf;
FILE *newfile;
PKCS12 *p12 = NULL;
if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_KEYSTORE_OPEN),
file, strerror(errno));
goto cleanup;
}
if (fstat(fd, &buf) == -1) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_KEYSTORE_OPEN),
file, strerror(errno));
goto cleanup;
}
if (!S_ISREG(buf.st_mode)) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_NOT_REG),
file);
goto cleanup;
}
if ((newfile = fdopen(fd, "r")) == NULL) {
pkgerr_add(err, PKGERR_READ, gettext(ERR_KEYSTORE_OPEN),
file, strerror(errno));
goto cleanup;
}
if ((p12 = d2i_PKCS12_fp(newfile, NULL)) == NULL) {
pkgerr_add(err, PKGERR_CORRUPT,
gettext(ERR_KEYSTORE_CORRUPT), file);
goto cleanup;
}
cleanup:
if (newfile != NULL)
(void) fclose(newfile);
if (fd != -1)
(void) close(fd);
return (p12);
}
/*
* Locks the specified file.
*/
static int
file_lock(int fd, int type, int wait)
{
struct flock lock;
lock.l_type = type;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
if (!wait) {
if (file_lock_test(fd, type)) {
/*
* The caller would have to wait to get the
* lock on this file.
*/
return (-1);
}
}
return (fcntl(fd, F_SETLKW, &lock));
}
/*
* Returns FALSE if the file is not locked; TRUE
* otherwise.
*/
static boolean_t
file_lock_test(int fd, int type)
{
struct flock lock;
lock.l_type = type;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
if (fcntl(fd, F_GETLK, &lock) != -1) {
if (lock.l_type != F_UNLCK) {
/*
* The caller would have to wait to get the
* lock on this file.
*/
return (B_TRUE);
}
}
/*
* The file is not locked.
*/
return (B_FALSE);
}
/*
* Unlocks the specified file.
*/
static int
file_unlock(int fd)
{
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
return (fcntl(fd, F_SETLK, &lock));
}
/*
* Determines if file has a length of 0 or not
*/
static boolean_t
file_empty(char *path)
{
struct stat buf;
/* file is empty if size = 0 or it doesn't exist */
if (lstat(path, &buf) == 0) {
if (buf.st_size == 0) {
return (B_TRUE);
}
} else {
if (errno == ENOENT) {
return (B_TRUE);
}
}
return (B_FALSE);
}
/*
* Name: get_time_string
* Description: Generates a human-readable string from an ASN1_TIME
*
* Arguments: intime - The time to convert
*
* Returns : A pointer to a static string representing the passed-in time.
*/
static char
*get_time_string(ASN1_TIME *intime)
{
static char time[ATTR_MAX];
BIO *mem;
char *p;
if (intime == NULL) {
return (NULL);
}
if ((mem = BIO_new(BIO_s_mem())) == NULL) {
return (NULL);
}
if (ASN1_TIME_print(mem, intime) == 0) {
(void) BIO_free(mem);
return (NULL);
}
if (BIO_gets(mem, time, ATTR_MAX) <= 0) {
(void) BIO_free(mem);
return (NULL);
}
(void) BIO_free(mem);
/* trim the end of the string */
for (p = time + strlen(time) - 1; isspace(*p); p--) {
*p = '\0';
}
return (time);
}
/*
* check_password - do various password checks to see if the current password
* will work or we need to prompt for a new one.
*
* Arguments:
* pass - password to check
*
* Returns:
* B_TRUE - Password is OK.
* B_FALSE - Password not valid.
*/
static boolean_t
check_password(PKCS12 *p12, char *pass)
{
boolean_t ret = B_TRUE;
/*
* If password is zero length or NULL then try verifying both cases
* to determine which password is correct. The reason for this is that
* under PKCS#12 password based encryption no password and a zero
* length password are two different things...
*/
/* Check the mac */
if (pass == NULL || *pass == '\0') {
if (PKCS12_verify_mac(p12, NULL, 0) == 0 &&
PKCS12_verify_mac(p12, "", 0) == 0)
ret = B_FALSE;
} else if (PKCS12_verify_mac(p12, pass, -1) == 0) {
ret = B_FALSE;
}
return (ret);
}
|