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
|
diff --git a/mozilla/security/nss/lib/ssl/fnv1a64.c b/mozilla/security/nss/lib/ssl/fnv1a64.c
new file mode 100644
index 0000000..c7c4b08
--- /dev/null
+++ b/mozilla/security/nss/lib/ssl/fnv1a64.c
@@ -0,0 +1,72 @@
+/*
+ * FNV1A64 Hash
+ * http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is the Netscape security libraries.
+ *
+ * The Initial Developer of the Original Code is
+ * Netscape Communications Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 1994-2000
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Adam Langley, Google Inc.
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+/* $Id: fnv1a64.c,v 1.0 2010/08/09 13:00:00 agl%google.com Exp $ */
+
+#include "prtypes.h"
+#include "prnetdb.h"
+
+/* Older versions of Visual C++ don't support the 'ull' suffix. */
+#ifdef _MSC_VER
+static const PRUint64 FNV1A64_OFFSET_BASIS = 14695981039346656037ui64;
+static const PRUint64 FNV1A64_PRIME = 1099511628211ui64;
+#else
+static const PRUint64 FNV1A64_OFFSET_BASIS = 14695981039346656037ull;
+static const PRUint64 FNV1A64_PRIME = 1099511628211ull;
+#endif
+
+void FNV1A64_Init(PRUint64* digest) {
+ *digest = FNV1A64_OFFSET_BASIS;
+}
+
+void FNV1A64_Update(PRUint64* digest, const unsigned char *data,
+ unsigned int length) {
+ unsigned int i;
+
+ for (i = 0; i < length; i++) {
+ *digest ^= data[i];
+ *digest *= FNV1A64_PRIME;
+ }
+}
+
+void FNV1A64_Final(PRUint64 *digest) {
+ *digest = PR_htonll(*digest);
+}
diff --git a/mozilla/security/nss/lib/ssl/snapstart.c b/mozilla/security/nss/lib/ssl/snapstart.c
new file mode 100644
index 0000000..ca2cafa
--- /dev/null
+++ b/mozilla/security/nss/lib/ssl/snapstart.c
@@ -0,0 +1,1062 @@
+/*
+ * TLS Snap Start
+ *
+ * ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is the Netscape security libraries.
+ *
+ * The Initial Developer of the Original Code is
+ * Netscape Communications Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 1994-2000
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Adam Langley, Google Inc.
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+/* $Id: ssl3snap.c,v 1.0 2010/08/09 13:00:00 agl%google.com Exp $ */
+
+
+/* TODO(agl): Refactor ssl3_CompressMACEncryptRecord so that it can write to
+** |sendBuf| directly and fix ssl3_AppendSnapStartHandshakeRecord and
+** ssl3_AppendSnapStartApplicationData.
+*/
+
+/* TODO(agl): Add support for snap starting with compression. */
+
+/* TODO(agl): Free snapStartApplicationData as soon as the handshake has
+** completed.
+*/
+
+#include "pk11pub.h"
+#include "ssl.h"
+#include "sslimpl.h"
+#include "sslproto.h"
+
+static unsigned int GetBE16(const void *in)
+{
+ const unsigned char *p = in;
+ return ((unsigned) p[0]) << 8 |
+ p[1];
+}
+
+static unsigned int GetBE24(const void *in)
+{
+ const unsigned char *p = in;
+ return ((unsigned) p[0]) << 16 |
+ ((unsigned) p[1]) << 8 |
+ p[2];
+}
+
+static void PutBE16(void *out, unsigned int value)
+{
+ unsigned char *p = out;
+ p[0] = value >> 8;
+ p[1] = value;
+}
+
+static void PutBE24(void *out, unsigned int value)
+{
+ unsigned char *p = out;
+ p[0] = value >> 16;
+ p[1] = value >> 8;
+ p[2] = value;
+}
+
+/* ssl3_ForEachExtension calls a callback for each TLS extension in |extensions|
+** extensions: points to a block of extensions which includes the two prefix
+** length bytes
+** is_resuming: if true, certain extensions will be omitted
+** f: a function which is called with the data of each extension, which
+** includes the four type and length bytes at the beginning.
+*/
+static PRBool
+ssl3_ForEachExtension(const SECItem *extensions, PRBool is_resuming,
+ void (*f) (const unsigned char *data, unsigned int length,
+ void *ctx),
+ void *ctx) {
+ unsigned int extensions_len, offset;
+
+ if (extensions->len == 0)
+ return PR_TRUE;
+
+ if (extensions->len < 2)
+ goto loser;
+
+ extensions_len = GetBE16(extensions->data);
+ offset = 2;
+
+ if (extensions->len != 2 + extensions_len)
+ goto loser;
+
+ while (extensions_len) {
+ unsigned int extension_num, extension_len;
+
+ if (extensions->len - offset < 4)
+ goto loser;
+
+ extension_num = GetBE16(extensions->data + offset);
+ extension_len = GetBE16(extensions->data + offset + 2);
+
+ if (extensions->len - offset < 4 + extension_len)
+ goto loser;
+
+ /* When resuming, the server will omit some extensions from the
+ * previous non-resume ServerHello. */
+ if (!is_resuming ||
+ (extension_num != ssl_server_name_xtn &&
+ extension_num != ssl_session_ticket_xtn)) {
+ f(extensions->data + offset, 4 + extension_len, ctx);
+ }
+
+ offset += 4 + extension_len;
+ extensions_len -= 4 + extension_len;
+ }
+
+ return PR_TRUE;
+
+loser:
+ PORT_SetError(SEC_ERROR_INPUT_LEN);
+ return PR_FALSE;
+}
+
+static void
+ssl3_AccumlateLengths(const unsigned char *data, unsigned int length, void *ptr)
+{
+ unsigned int *sum = (unsigned int *) ptr;
+ *sum += length;
+}
+
+/* ssl3_PredictServerResponse predicts the contents of the server's
+** ServerHello...ServerHelloDone (inclusive) and progressively calls a callback
+** with the contents of those messages.
+** previous_server_hello: the contents of a previous ServerHello from the
+** server where the 'random' field has been replaced with our suggested
+** server random.
+** is_resuming: if false, Certificate and ServerHelloDone messages will be
+** predicted
+** hashUpdate: a callback which is called repeated with the contents of the
+** predicted messages.
+*/
+static PRBool
+ssl3_PredictServerResponse(
+ sslSocket *ss, SECItem *previous_server_hello, PRBool is_resuming,
+ void (*hashUpdate) (const unsigned char *data, unsigned int length,
+ void *ctx),
+ void *ctx) {
+ unsigned int old_session_id_length, old_extensions_len;
+ unsigned int extensions_len, server_hello_len;
+ unsigned char session_id_len, header[4];
+ SECItem extensions;
+
+ /* Keep the structure of a ServerHello in mind when reading the following:
+ *
+ * struct ServerHello {
+ * uint16_t version;
+ * uint8_t random[32];
+ * uint8_t session_id_len;
+ * uint8_t session_id[session_id_len];
+ * uint16_t cipher
+ * uint8_t compression
+ *
+ * // Optional:
+ * uint16_t extensions_len;
+ * struct Extension {
+ * uint16_t num;
+ * uint16_t len;
+ * uint8_t payload[len];
+ * }
+ */
+
+ /* 38 bytes is the shortest possible ServerHello with a zero-length
+ * session_id and no extensions. */
+ if (previous_server_hello->len < 38) {
+ PORT_SetError(SEC_ERROR_INPUT_LEN);
+ return PR_FALSE;
+ }
+
+ /* First we need to figure out the length of the predicted ServerHello. Any
+ * session id in |previous_server_hello| needs to be removed
+ * (or replaced). */
+ old_session_id_length = previous_server_hello->data[34];
+
+ extensions.len = 0;
+
+ if (previous_server_hello->len >= 35 + old_session_id_length + 3 + 2) {
+ /* Extensions present */
+ unsigned int offset = 35 + old_session_id_length + 3;
+ extensions.data = previous_server_hello->data + offset;
+ extensions.len = previous_server_hello->len - offset;
+ }
+
+ /* Sum the lengths of all the extensions that we wish to include */
+ extensions_len = 0;
+ if (!ssl3_ForEachExtension(&extensions, is_resuming, ssl3_AccumlateLengths,
+ &extensions_len)) {
+ return PR_FALSE;
+ }
+
+ old_extensions_len =
+ (previous_server_hello->len - 35 - old_session_id_length - 3 - 2);
+
+ session_id_len = 0;
+ if (ss->sec.ci.sid)
+ session_id_len = ss->sec.ci.sid->u.ssl3.sessionIDLength;
+ server_hello_len = previous_server_hello->len +
+ session_id_len - old_session_id_length +
+ extensions_len - old_extensions_len;
+
+ header[0] = server_hello;
+ PutBE24(header + 1, server_hello_len);
+ hashUpdate(header, 4, ctx);
+
+ hashUpdate(previous_server_hello->data, 34, ctx);
+ hashUpdate(&session_id_len, sizeof(session_id_len), ctx);
+ if (session_id_len)
+ hashUpdate(ss->sec.ci.sid->u.ssl3.sessionID, session_id_len, ctx);
+ hashUpdate(previous_server_hello->data + 35 + old_session_id_length, 3,
+ ctx);
+
+ if (extensions.len) {
+ PutBE16(header, extensions_len);
+ hashUpdate(header, 2, ctx);
+
+ if (!ssl3_ForEachExtension(&extensions, is_resuming, hashUpdate, ctx))
+ return PR_FALSE;
+ }
+
+ if (!is_resuming) {
+ unsigned int certificate_message_len = 3, i;
+ for (i = 0; ss->ssl3.predictedCertChain[i]; i++) {
+ certificate_message_len += 3;
+ certificate_message_len +=
+ ss->ssl3.predictedCertChain[i]->derCert.len;
+ }
+
+ header[0] = certificate;
+ PutBE24(header + 1, certificate_message_len);
+ hashUpdate(header, 4, ctx);
+
+ PutBE24(header, certificate_message_len - 3);
+ hashUpdate(header, 3, ctx);
+
+ for (i = 0; ss->ssl3.predictedCertChain[i]; i++) {
+ unsigned int len = ss->ssl3.predictedCertChain[i]->derCert.len;
+ PutBE24(header, len);
+ hashUpdate(header, 3, ctx);
+ hashUpdate(ss->ssl3.predictedCertChain[i]->derCert.data, len, ctx);
+ }
+
+ header[0] = server_hello_done;
+ header[1] = header[2] = header[3] = 0;
+ hashUpdate(header, 4, ctx);
+ }
+
+ return PR_TRUE;
+}
+
+/* ssl3_SnapStartHash is called with the contents of the server's predicted
+ * response and updates both the Finished hash and an FNV641a hash. */
+static void
+ssl3_SnapStartHash(const unsigned char *data, unsigned int len, void *ctx)
+{
+ SECStatus rv;
+ void **ptrs = (void **) ctx;
+ sslSocket *ss = ptrs[0];
+ PRUint64 *fnv = ptrs[1];
+
+ FNV1A64_Update(fnv, data, len);
+ rv = ssl3_UpdateHandshakeHashes(ss, (unsigned char *) data, len);
+ if (rv != SECSuccess)
+ PR_Assert("rv == SECSuccess", __FILE__, __LINE__);
+}
+
+static PRInt32
+ssl3_SendEmptySnapStartXtn(sslSocket *ss, PRBool append, PRUint32 maxBytes)
+{
+ SECStatus rv;
+
+ if (maxBytes < 4)
+ return 0;
+
+ if (append) {
+ rv = ssl3_AppendHandshakeNumber(ss, ssl_snap_start_xtn, 2);
+ if (rv != SECSuccess)
+ return -1;
+ rv = ssl3_AppendHandshakeNumber(ss, 0 /* empty extension */, 2);
+ if (rv != SECSuccess)
+ return -1;
+ if (!ss->sec.isServer) {
+ TLSExtensionData *xtnData = &ss->xtnData;
+ xtnData->advertised[xtnData->numAdvertised++] = ssl_snap_start_xtn;
+ }
+ }
+ return 4;
+}
+
+static SECStatus
+ssl3_BufferEnsure(sslBuffer *buf, unsigned int extra_bytes)
+{
+ if (buf->space < buf->len + extra_bytes)
+ return sslBuffer_Grow(buf, buf->len + extra_bytes);
+ return SECSuccess;
+}
+
+/* ssl3_AppendSnapStartRecordHeader appends a 5 byte TLS record header to the
+ * sendBuf of the given sslSocket. */
+static SECStatus
+ssl3_AppendSnapStartRecordHeader(sslSocket *ss, SSL3ContentType type,
+ unsigned int len)
+{
+ SECStatus rv;
+
+ rv = ssl3_BufferEnsure(&ss->sec.ci.sendBuf, 5);
+ if (rv != SECSuccess)
+ return rv;
+ ss->sec.ci.sendBuf.buf[ss->sec.ci.sendBuf.len + 0] = type;
+ PutBE16(&ss->sec.ci.sendBuf.buf[ss->sec.ci.sendBuf.len + 1], ss->version);
+ PutBE16(&ss->sec.ci.sendBuf.buf[ss->sec.ci.sendBuf.len + 3], len);
+ ss->sec.ci.sendBuf.len += 5;
+ return SECSuccess;
+}
+
+/* ssl3_AppendSnapStartHandshakeRecord appends a (possibly encrypted) record to
+** the sendBuf of the given sslSocket.
+** f: a function which will append the bytes of the record (not including the
+** 5 byte header) to the sendBuf.
+*/
+static SECStatus
+ssl3_AppendSnapStartHandshakeRecord(sslSocket *ss, SECStatus (*f) (sslSocket*),
+ PRBool encrypt)
+{
+ SECStatus rv;
+ unsigned int record_offset, record_len;
+
+ /* ssl3_CompressMACEncryptRecord will deal with the record header if we are
+ * encrypting. */
+ if (!encrypt) {
+ /* The zero length argument here is a dummy value. We write the real
+ * length once we know it, below. */
+ rv = ssl3_AppendSnapStartRecordHeader(ss, content_handshake, 0);
+ if (rv != SECSuccess)
+ return rv;
+ }
+
+ record_offset = ss->sec.ci.sendBuf.len;
+ rv = f(ss);
+ if (rv != SECSuccess)
+ return rv;
+ record_len = ss->sec.ci.sendBuf.len - record_offset;
+ if (!encrypt) {
+ PutBE16(&ss->sec.ci.sendBuf.buf[record_offset - 2], record_len);
+ } else {
+ /* ssl3_CompressMACEncryptRecord writes to |ss->sec.writeBuf|
+ * so we copy it back to |ss->sec.ci.sendBuf| */
+ /* TODO(agl): the buffer copy here is a bodge. See TODO at the top of
+ * the file. */
+ rv = ssl3_CompressMACEncryptRecord(
+ ss, content_handshake,
+ &ss->sec.ci.sendBuf.buf[record_offset], record_len);
+ if (rv != SECSuccess)
+ return rv;
+ ss->sec.ci.sendBuf.len -= record_len;
+ ssl3_BufferEnsure(&ss->sec.ci.sendBuf, ss->sec.writeBuf.len);
+ memcpy(&ss->sec.ci.sendBuf.buf[record_offset], ss->sec.writeBuf.buf,
+ ss->sec.writeBuf.len);
+ ss->sec.ci.sendBuf.len += ss->sec.writeBuf.len;
+ ss->sec.writeBuf.len = 0;
+ }
+
+ return SECSuccess;
+}
+
+/* ssl3_AppendSnapStartApplicationData appends an encrypted Application Data
+** record the sendBuf of the given sslSocket.
+*/
+static SECStatus ssl3_AppendSnapStartApplicationData(
+ sslSocket *ss, const SSL3Opaque *data, unsigned int len)
+{
+ SECStatus rv;
+
+ /* TODO(agl): the buffer copy here is a bodge. See TODO at the top of the
+ * file. */
+ rv = ssl3_CompressMACEncryptRecord(ss, content_application_data, data, len);
+ if (rv != SECSuccess)
+ return rv;
+ rv = ssl3_BufferEnsure(&ss->sec.ci.sendBuf, ss->sec.writeBuf.len);
+ if (rv != SECSuccess)
+ return rv;
+ memcpy(&ss->sec.ci.sendBuf.buf[ss->sec.ci.sendBuf.len],
+ ss->sec.writeBuf.buf, ss->sec.writeBuf.len);
+ ss->sec.ci.sendBuf.len += ss->sec.writeBuf.len;
+ ss->sec.writeBuf.len = 0;
+
+ return SECSuccess;
+}
+
+static SECStatus ssl3_SendSnapStartFinished(sslSocket *ss)
+{
+ /* We use ssl_SEND_FLAG_NO_FLUSH here because this finished message is
+ * going to end up in the middle of the Snap Start extension. So
+ * transmitting |sendBuf| at this point would result in an incomplete
+ * ClientHello. */
+ return ssl3_SendFinished(ss, ssl_SEND_FLAG_NO_FLUSH);
+}
+
+/* ssl3_FindOrbit is called for each extension in a ServerHello message. It
+** tests for a Snap Start extension and records the server's orbit when found.
+** data: the extension data (including the four type and length bytes)
+** length: the length, in bytes, of |data|
+** ptr: a pointer to a uint8_t[9]. The orbit, if found, is copied into the
+** first 8 bytes and then the ninth byte is set to one.
+*/
+static void
+ssl3_FindOrbit(const unsigned char *data, unsigned int length, void *ptr)
+{
+ unsigned char *orbit = (unsigned char *) ptr;
+
+ unsigned int extension_num = GetBE16(data);
+ if (extension_num == ssl_snap_start_xtn &&
+ length == 4 + 8 /* orbit */ + 2 /* snap start cipher suite */) {
+ memcpy(orbit, data + 4, 8);
+ /* A last byte of 1 indicates that the previous eight are valid. */
+ orbit[8] = 1;
+ }
+}
+
+/* ssl3_CanSnapStart returns true if we are able to perform Snap Start on
+** the given socket.
+** extensions: on successful return, this is filled in with the contents of
+** the server's predicted extensions. This points within
+** |ss->ssl3.serverHelloPredictionData|.
+** resuming: PR_TRUE iff we wish to attempt a Snap Start resume
+** out_orbit: if this function returns PR_TRUE, then |out_orbit| is filled
+** with the server's predicted orbit value.
+** The |hs.cipher_suite|, |hs.cipher_def| and |hs.compression| fields of |ss|
+** are set to match the predicted ServerHello on successful exit (and may still
+** be modified on failure).
+*/
+static PRBool
+ssl3_CanSnapStart(sslSocket *ss, SECItem *extensions, PRBool resuming,
+ unsigned char out_orbit[8]) {
+ const unsigned char *server_hello;
+ unsigned int server_hello_len, session_id_len, cipher_suite_offset;
+ unsigned int extensions_offset, cipher_suite, compression_method;
+ unsigned char orbit[9];
+ SECStatus rv;
+ SSL3ProtocolVersion version;
+
+ /* If we don't have the information needed then we can't perform a Snap
+ * Start. */
+ if (!ss->ssl3.predictedCertChain || !ss->ssl3.serverHelloPredictionData.data)
+ return PR_FALSE;
+
+ /* When the sizes of the fields in the ClientHello are calculated, they'll
+ * take the length of the Snap Start extension to be zero, so currently
+ * it's as if this extension didn't exist, which is the state that we
+ * need. */
+
+ server_hello = ss->ssl3.serverHelloPredictionData.data;
+ server_hello_len = ss->ssl3.serverHelloPredictionData.len;
+
+ if (server_hello_len < 2 + 32 + 1)
+ return PR_FALSE;
+ session_id_len = server_hello[2 + 32];
+ cipher_suite_offset = 2 + 32 + 1 + session_id_len;
+ if (server_hello_len < cipher_suite_offset + 3)
+ return PR_FALSE;
+ extensions_offset = cipher_suite_offset + 3;
+
+ version = (SSL3ProtocolVersion) GetBE16(server_hello);
+ if (MSB(version) < MSB(SSL_LIBRARY_VERSION_3_0))
+ return PR_FALSE;
+ rv = ssl3_NegotiateVersion(ss, version);
+ if (rv != SECSuccess)
+ return PR_FALSE;
+
+ cipher_suite = GetBE16(&server_hello[cipher_suite_offset]);
+ ss->ssl3.hs.cipher_suite = (ssl3CipherSuite)cipher_suite;
+ ss->ssl3.hs.suite_def = ssl_LookupCipherSuiteDef(ss->ssl3.hs.cipher_suite);
+ if (!ss->ssl3.hs.suite_def)
+ return PR_FALSE;
+ compression_method = server_hello[cipher_suite_offset + 2];
+ if (compression_method != ssl_compression_null) {
+ /* TODO(agl): support compression. */
+ return PR_FALSE;
+ }
+ ss->ssl3.hs.compression = ssl_compression_null;
+
+ extensions->data = (unsigned char *) server_hello + extensions_offset;
+ extensions->len = server_hello_len - extensions_offset;
+
+ /* The last byte is used to indictate that the previous eight are valid. */
+ orbit[8] = 0;
+ if (!ssl3_ForEachExtension(extensions, resuming, ssl3_FindOrbit, orbit))
+ return PR_FALSE;
+
+ if (!orbit[8])
+ return PR_FALSE;
+
+ memcpy(out_orbit, orbit, 8);
+
+ return PR_TRUE;
+}
+
+/* ssl3_UpdateClientHelloLengths rewrites the handshake header length and
+** extensions length in the ClientHello to reflect the addition of the Snap
+** Start extension.
+** snap_start_extension_len_offset: the number of bytes of the ClientHello to
+** skip in order to find the embedded length of the Snap Start extension.
+*/
+static void
+ssl3_UpdateClientHelloLengths(sslSocket *ss,
+ unsigned int snap_start_extension_len_offset,
+ sslSessionID *sid) {
+ unsigned int extension_length, old_length, new_length, new_session_id_len;
+ unsigned int offset, ciphers_length, compressions_len;
+
+ extension_length =
+ ss->sec.ci.sendBuf.len - snap_start_extension_len_offset - 2;
+ PutBE16(&ss->sec.ci.sendBuf.buf[snap_start_extension_len_offset],
+ extension_length);
+
+ /* The length in the handshake header is short by extension_length + 4
+ * bytes. */
+ old_length = GetBE24(&ss->sec.ci.sendBuf.buf[1]);
+ new_length = old_length + extension_length + 4;
+ PutBE24(&ss->sec.ci.sendBuf.buf[1], new_length);
+
+ /* The length of the extensions block is similarly wrong. */
+ new_session_id_len = 0;
+ if (sid)
+ new_session_id_len = sid->u.ssl3.sessionIDLength;
+ offset = 4 + 2 + 32 + 1 + new_session_id_len;
+ ciphers_length = GetBE16(&ss->sec.ci.sendBuf.buf[offset]);
+ offset += 2 + ciphers_length;
+ compressions_len = ss->sec.ci.sendBuf.buf[offset];
+ offset += 1 + compressions_len;
+ old_length = GetBE16(&ss->sec.ci.sendBuf.buf[offset]);
+ new_length = old_length + extension_length + 4;
+ PutBE16(&ss->sec.ci.sendBuf.buf[offset], new_length);
+}
+
+/* ssl3_FindServerNPNExtension is a callback function for ssl3_ForEachExtension.
+ * It looks for a Next Protocol Negotiation and saves the payload of the
+ * extension in the given SECItem */
+static void
+ssl3_FindServerNPNExtension(const unsigned char* data, unsigned int length,
+ void *ctx)
+{
+ SECItem *server_npn_extension = (SECItem*) ctx;
+
+ unsigned int extension_num = GetBE16(data);
+ if (extension_num == ssl_next_proto_neg_xtn && length >= 4) {
+ server_npn_extension->data = (unsigned char*)data + 4;
+ server_npn_extension->len = length - 4;
+ }
+}
+
+/* ssl3_MaybeWriteNextProtocol deals with the interaction of Next Protocol
+ * Negotiation and Snap Start. It's called just before we serialise the embedded
+ * Finished message in the extension. At this point, if NPN is enabled, we have
+ * to include a NextProtocol message. */
+static SECStatus
+ssl3_MaybeWriteNextProtocol(sslSocket *ss, SECItem *server_hello_extensions)
+{
+ PRUint16 i16;
+ SECItem server_npn_extension;
+
+ for (i16 = 0; i16 < ss->xtnData.numAdvertised; i16++) {
+ if (ss->xtnData.advertised[i16] == ssl_next_proto_neg_xtn)
+ break;
+ }
+
+ if (i16 == ss->xtnData.numAdvertised) {
+ /* We didn't send an NPN extension, so no need to do anything here. */
+ return SECSuccess;
+ }
+
+ memset(&server_npn_extension, 0, sizeof(server_npn_extension));
+
+ ssl3_ForEachExtension(
+ server_hello_extensions, PR_FALSE /* is_resuming: value doesn't matter
+ in this case */, ssl3_FindServerNPNExtension, &server_npn_extension);
+
+ if (server_npn_extension.data == NULL) {
+ /* We predicted that the server doesn't support NPN, so nothing to do
+ * here. */
+ return SECSuccess;
+ }
+
+ ssl3_ClientHandleNextProtoNegoXtn(ss, ssl_next_proto_neg_xtn,
+ &server_npn_extension);
+
+ if (ss->ssl3.nextProtoState == SSL_NEXT_PROTO_NO_SUPPORT) {
+ /* The server's predicted NPN extension was malformed. We're didn't pick
+ * a protocol so we won't send a NextProtocol message. However, this is
+ * probably fatal to the connection. */
+ return SECSuccess;
+ }
+
+ return ssl3_AppendSnapStartHandshakeRecord(ss, ssl3_SendNextProto,
+ PR_TRUE /* encrypt */);
+}
+
+/* ssl3_SendSnapStartXtn appends a Snap Start extension. It assumes that the
+ * inchoate ClientHello is in |ss->sec.ci.sendBuf|. */
+PRInt32
+ssl3_SendSnapStartXtn(sslSocket *ss, PRBool append, PRUint32 maxBytes)
+{
+ unsigned char orbit[8];
+ PRBool resuming = PR_FALSE;
+ unsigned char suggested_server_random[32];
+ SECStatus rv;
+ PRUint64 fnv;
+ /* The context for |ssl3_SnapStartHash|. The first pointer points to |ss|
+ * and the second to the running FNV1A64 hash of the predicted server
+ * response. */
+ void *ctx[2];
+ unsigned int snap_start_extension_len_offset, original_sendbuf_len;
+ ssl3CipherSpec *temp;
+ sslSessionID *sid;
+ SECItem server_extensions;
+
+ if (!ss->opt.enableSnapStart)
+ return 0;
+
+ original_sendbuf_len = ss->sec.ci.sendBuf.len;
+
+ /* This function is called twice for each ClientHello emitted. The first
+ * time around is to calculate the sizes of the extension (|append| is
+ * false). The second time around is to actually write out the bytes
+ * (|append| is true).
+ *
+ * We always return 0 bytes in each case because we want to be able to hash
+ * the inchoate ClientHello as if this extension was missing: that's why
+ * it's important that this always be the last extension serialised. */
+ sid = ss->sec.ci.sid;
+
+ if (!ss->opt.enableSessionTickets || ss->sec.isServer)
+ return 0;
+
+ /* If we are sending a SessionTicket then the first time around
+ * ticketTimestampVerified will be true but it's reset after serialising
+ * the session ticket extension, so we have
+ * |clientSentNonEmptySessionTicket|. */
+ if (ss->xtnData.clientSentNonEmptySessionTicket) {
+ resuming = PR_TRUE;
+ } else if (sid->u.ssl3.sessionTicket.ticket.data &&
+ ss->xtnData.ticketTimestampVerified) {
+ resuming = PR_TRUE;
+ }
+
+ if (!ssl3_CanSnapStart(ss, &server_extensions, resuming, orbit))
+ return ssl3_SendEmptySnapStartXtn(ss, append, maxBytes);
+
+ /* At this point we are happy that we are going to send a non-empty Snap
+ * Start extension. If we are still calculating length then we lie and
+ * return 0 so that everything is set up as if the extension didn't exist
+ * when this function is called again later. */
+
+ if (!append)
+ return 0;
+
+ /* |ss->sec.ci.sendBuf.buf| contains the inchoate ClientHello. This copies
+ * the ClientHello's gmt_unix_time into the suggested server random. */
+ memcpy(suggested_server_random, ss->sec.ci.sendBuf.buf + 6, 4);
+ memcpy(suggested_server_random + 4, orbit, 8);
+ rv = PK11_GenerateRandom(suggested_server_random + 12, 20);
+ if (rv != SECSuccess)
+ goto loser;
+ memcpy(ss->ssl3.serverHelloPredictionData.data + 2, suggested_server_random,
+ 32);
+
+ memcpy(&ss->ssl3.hs.server_random, suggested_server_random, 32);
+
+ PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
+ rv = ssl3_SetupPendingCipherSpec(ss);
+ if (rv != SECSuccess)
+ goto loser;
+ ss->ssl3.hs.isResuming = resuming;
+
+ FNV1A64_Init(&fnv);
+ ctx[0] = ss;
+ ctx[1] = &fnv;
+
+ if (!ssl3_PredictServerResponse(ss, &ss->ssl3.serverHelloPredictionData,
+ resuming, ssl3_SnapStartHash, ctx)) {
+ /* It's not a fatal error if the predicted ServerHello was invalid. */
+ return 0;
+ }
+ FNV1A64_Final(&fnv);
+
+ /* Now we grow the send buffer to accomodate the extension type and length,
+ * orbit, suggested random and predicted server response hash without
+ * calling ssl3_AppendHandshake (which would also update the Finished
+ * hash). */
+ if (ssl3_BufferEnsure(&ss->sec.ci.sendBuf, 4 + 8 + 20 + 8) != SECSuccess)
+ goto loser;
+
+ PutBE16(&ss->sec.ci.sendBuf.buf[ss->sec.ci.sendBuf.len],
+ ssl_snap_start_xtn);
+ ss->sec.ci.sendBuf.len += 2;
+ /* Skip over the length for now. */
+ snap_start_extension_len_offset = ss->sec.ci.sendBuf.len;
+ ss->sec.ci.sendBuf.len += 2;
+ memcpy(ss->sec.ci.sendBuf.buf + ss->sec.ci.sendBuf.len, orbit, 8);
+ ss->sec.ci.sendBuf.len += 8;
+ memcpy(&ss->sec.ci.sendBuf.buf[ss->sec.ci.sendBuf.len],
+ suggested_server_random + 12, 20);
+ ss->sec.ci.sendBuf.len += 20;
+ memcpy(ss->sec.ci.sendBuf.buf + ss->sec.ci.sendBuf.len, &fnv, 8);
+ ss->sec.ci.sendBuf.len += 8;
+
+ if (!resuming) {
+ /* Write ClientKeyExchange */
+ ss->sec.peerCert =
+ CERT_DupCertificate(ss->ssl3.predictedCertChain[0]);
+ rv = ssl3_AppendSnapStartHandshakeRecord(
+ ss, ssl3_SendClientKeyExchange, PR_FALSE /* do not encrypt */);
+ if (rv != SECSuccess)
+ goto loser;
+ } else {
+ SSL3Finished hashes;
+ TLSFinished tlsFinished;
+ unsigned char hdr[4];
+
+ rv = ssl3_SetupMasterSecretFromSessionID(ss);
+ if (rv == SECFailure)
+ goto loser;
+
+ if (sid->peerCert != NULL) {
+ ss->sec.peerCert = CERT_DupCertificate(sid->peerCert);
+ ssl3_CopyPeerCertsFromSID(ss, sid);
+ }
+
+ rv = ssl3_InitPendingCipherSpec(ss, NULL /* re-use master secret */);
+ if (rv != SECSuccess)
+ goto loser;
+
+ /* Need to add the server's predicted Finished message to our handshake
+ * hash in order to be able to produce our own Finished message. */
+ rv = ssl3_ComputeHandshakeHashes(ss, ss->ssl3.pwSpec, &hashes,
+ 0 /* only for SSL3 */);
+ if (rv != SECSuccess)
+ goto loser;
+
+ rv = ssl3_ComputeTLSFinished(ss->ssl3.pwSpec, PR_TRUE /* isServer */,
+ &hashes, &tlsFinished);
+ if (rv != SECSuccess)
+ goto loser;
+
+ hdr[0] = (unsigned char) finished;
+ hdr[1] = hdr[2] = 0;
+ hdr[3] = sizeof(tlsFinished.verify_data);
+ ssl3_UpdateHandshakeHashes(ss, hdr, sizeof(hdr));
+ ssl3_UpdateHandshakeHashes(ss, tlsFinished.verify_data,
+ sizeof(tlsFinished.verify_data));
+
+ /* Store the Finished message so that we can verify it later */
+ memcpy(&ss->ssl3.hs.finishedMsgs.tFinished[1], tlsFinished.verify_data,
+ sizeof(tlsFinished.verify_data));
+ }
+
+ /* Write ChangeCipherSpec */
+ rv = ssl3_AppendSnapStartRecordHeader(ss, content_change_cipher_spec, 1);
+ if (rv != SECSuccess)
+ goto loser;
+ rv = ssl3_BufferEnsure(&ss->sec.ci.sendBuf, 1);
+ if (rv != SECSuccess)
+ goto loser;
+ ss->sec.ci.sendBuf.buf[ss->sec.ci.sendBuf.len] = change_cipher_spec_choice;
+ ss->sec.ci.sendBuf.len++;
+
+ /* We swap |cwSpec| and |pwSpec| temporarily in order to encrypt some
+ * records before switching them back so that the whole ClientHello doesn't
+ * get encrypted. */
+ ssl_GetSpecWriteLock(ss);
+ temp = ss->ssl3.cwSpec;
+ ss->ssl3.cwSpec = ss->ssl3.pwSpec;
+ ss->ssl3.pwSpec = temp;
+ ss->ssl3.cwSpec->write_seq_num.high = 0;
+ ss->ssl3.cwSpec->write_seq_num.low = 0;
+ ssl_ReleaseSpecWriteLock(ss);
+
+ rv = ssl3_MaybeWriteNextProtocol(ss, &server_extensions);
+ if (rv != SECSuccess)
+ goto loser;
+
+ /* Write Finished */
+ rv = ssl3_AppendSnapStartHandshakeRecord(ss, ssl3_SendSnapStartFinished,
+ PR_TRUE /* encrypt */);
+ if (rv != SECSuccess)
+ goto loser;
+
+ /* Write application data */
+ if (ss->ssl3.snapStartApplicationData.data) {
+ rv = ssl3_AppendSnapStartApplicationData(
+ ss, ss->ssl3.snapStartApplicationData.data,
+ ss->ssl3.snapStartApplicationData.len);
+ if (rv != SECSuccess)
+ goto loser;
+ }
+
+ /* Revert the write cipher spec because the ClientHello will get encrypted
+ * with it otherwise. */
+ ssl_GetSpecWriteLock(ss);
+ temp = ss->ssl3.cwSpec;
+ ss->ssl3.cwSpec = ss->ssl3.pwSpec;
+ ss->ssl3.pwSpec = temp;
+ ssl_ReleaseSpecWriteLock(ss);
+
+ /* Update the lengths in the ClientHello to reflect this extension. */
+ ssl3_UpdateClientHelloLengths(ss, snap_start_extension_len_offset, sid);
+
+ /* Keep a copy of the ClientHello around so that we can hash it in the case
+ * the the Snap Start handshake is rejected. */
+
+ if (SECITEM_AllocItem(NULL, &ss->ssl3.hs.origClientHello,
+ ss->sec.ci.sendBuf.len) == NULL) {
+ goto loser;
+ }
+ memcpy(ss->ssl3.hs.origClientHello.data, ss->sec.ci.sendBuf.buf,
+ ss->sec.ci.sendBuf.len);
+ ss->ssl3.hs.origClientHello.len = ss->sec.ci.sendBuf.len;
+
+ ss->xtnData.advertised[ss->xtnData.numAdvertised++] = ssl_snap_start_xtn;
+
+ if (resuming) {
+ ss->ssl3.hs.snapStartType = snap_start_resume;
+ } else {
+ ss->ssl3.hs.snapStartType = snap_start_full;
+ }
+
+ return 0;
+
+loser:
+ /* In the case of an error we revert the length of the sendBuf to remove
+ * any partial data that we may have appended. */
+ ss->sec.ci.sendBuf.len = original_sendbuf_len;
+ return -1;
+}
+
+SECStatus ssl3_ClientHandleSnapStartXtn(sslSocket *ss, PRUint16 ex_type,
+ SECItem *data) {
+ /* The work of saving the ServerHello is done in ssl3_HandleServerHello,
+ * where its contents are available. Here we renognise that the saved
+ * ServerHello message contains a Snap Start extension and mark it as
+ * valid. */
+ ss->ssl3.serverHelloPredictionDataValid = PR_TRUE;
+ return SECSuccess;
+}
+
+SECStatus
+SSL_SetPredictedPeerCertificates(PRFileDesc *fd, CERTCertificate **certs,
+ unsigned int numCerts)
+{
+ sslSocket *ss;
+ unsigned int i;
+
+ ss = ssl_FindSocket(fd);
+ if (!ss) {
+ SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetPredictedPeerCertificates",
+ SSL_GETPID(), fd));
+ return SECFailure;
+ }
+
+ ss->ssl3.predictedCertChain =
+ PORT_NewArray(CERTCertificate*, numCerts + 1);
+ if (!ss->ssl3.predictedCertChain)
+ return SECFailure; /* error code was set */
+ for (i = 0; i < numCerts; i++)
+ ss->ssl3.predictedCertChain[i] = CERT_DupCertificate(certs[i]);
+ ss->ssl3.predictedCertChain[numCerts] = NULL;
+
+ return SECSuccess;
+}
+
+void
+ssl3_CleanupPredictedPeerCertificates(sslSocket *ss) {
+ unsigned int i;
+
+ if (!ss->ssl3.predictedCertChain)
+ return;
+
+ for (i = 0; ss->ssl3.predictedCertChain[i]; i++) {
+ CERT_DestroyCertificate(ss->ssl3.predictedCertChain[i]);
+ }
+
+ PORT_Free(ss->ssl3.predictedCertChain);
+ ss->ssl3.predictedCertChain = NULL;
+}
+
+SECStatus
+SSL_GetPredictedServerHelloData(PRFileDesc *fd, const unsigned char **data,
+ unsigned int *data_len)
+{
+ sslSocket *ss;
+
+ ss = ssl_FindSocket(fd);
+ if (!ss) {
+ SSL_DBG(("%d: SSL[%d]: bad socket in SSL_GetPredictedServerHelloData",
+ SSL_GETPID(), fd));
+ *data = NULL;
+ *data_len = 0;
+ return SECFailure;
+ }
+
+ if (!ss->ssl3.serverHelloPredictionDataValid) {
+ *data = NULL;
+ *data_len = 0;
+ } else {
+ *data = ss->ssl3.serverHelloPredictionData.data;
+ *data_len = ss->ssl3.serverHelloPredictionData.len;
+ }
+ return SECSuccess;
+}
+
+SECStatus
+SSL_SetPredictedServerHelloData(PRFileDesc *fd, const unsigned char *data,
+ unsigned int data_len)
+{
+ sslSocket *ss;
+
+ ss = ssl_FindSocket(fd);
+ if (!ss) {
+ SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetPredictedServerHelloData",
+ SSL_GETPID(), fd));
+ return SECFailure;
+ }
+
+ if (ss->ssl3.serverHelloPredictionData.data)
+ SECITEM_FreeItem(&ss->ssl3.serverHelloPredictionData, PR_FALSE);
+ if (!SECITEM_AllocItem(NULL, &ss->ssl3.serverHelloPredictionData, data_len))
+ return SECFailure;
+ memcpy(ss->ssl3.serverHelloPredictionData.data, data, data_len);
+ return SECSuccess;
+}
+
+SECStatus
+SSL_SetSnapStartApplicationData(PRFileDesc *fd, const unsigned char *data,
+ unsigned int data_len)
+{
+ sslSocket *ss;
+
+ ss = ssl_FindSocket(fd);
+ if (!ss) {
+ SSL_DBG(("%d: SSL[%d]: bad socket in SSL_SetSnapStartApplicationData",
+ SSL_GETPID(), fd));
+ return SECFailure;
+ }
+
+ if (ss->ssl3.snapStartApplicationData.data)
+ SECITEM_FreeItem(&ss->ssl3.snapStartApplicationData, PR_FALSE);
+ if (!SECITEM_AllocItem(NULL, &ss->ssl3.snapStartApplicationData, data_len))
+ return SECFailure;
+ memcpy(ss->ssl3.snapStartApplicationData.data, data, data_len);
+ return SECSuccess;
+}
+
+SECStatus
+SSL_GetSnapStartResult(PRFileDesc *fd, SSLSnapStartResult *result)
+{
+ sslSocket *ss;
+
+ ss = ssl_FindSocket(fd);
+ if (!ss) {
+ SSL_DBG(("%d: SSL[%d]: bad socket in SSL_GetSnapStartResult",
+ SSL_GETPID(), fd));
+ return SECFailure;
+ }
+
+ switch (ss->ssl3.hs.snapStartType) {
+ case snap_start_full:
+ *result = SSL_SNAP_START_FULL;
+ break;
+ case snap_start_recovery:
+ *result = SSL_SNAP_START_RECOVERY;
+ break;
+ case snap_start_resume:
+ *result = SSL_SNAP_START_RESUME;
+ break;
+ case snap_start_resume_recovery:
+ *result = SSL_SNAP_START_RESUME_RECOVERY;
+ break;
+ default:
+ PORT_Assert(ss->ssl3.hs.snapStartType == snap_start_none);
+ *result = SSL_SNAP_START_NONE;
+ break;
+ }
+
+ return SECSuccess;
+}
+
+/* Called form ssl3_HandleServerHello in the case that we sent a Snap Start
+** ClientHello but received a ServerHello in reply.
+*/
+SECStatus
+ssl3_ResetForSnapStartRecovery(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
+{
+ SECStatus rv;
+ PRUint8 hdr[4];
+
+ ss->ssl3.hs.ws = wait_server_hello;
+
+ /* Need to reset the Finished hashes to include the full ClientHello
+ * message. */
+
+ rv = ssl3_RestartHandshakeHashes(ss);
+ if (rv != SECSuccess)
+ return rv;
+ rv = ssl3_UpdateHandshakeHashes(ss, ss->ssl3.hs.origClientHello.data,
+ ss->ssl3.hs.origClientHello.len);
+ SECITEM_FreeItem(&ss->ssl3.hs.origClientHello, PR_FALSE);
+ if (rv != SECSuccess)
+ return rv;
+
+ hdr[0] = (PRUint8)server_hello;
+ hdr[1] = (PRUint8)(length >> 16);
+ hdr[2] = (PRUint8)(length >> 8);
+ hdr[3] = (PRUint8)(length );
+
+ rv = ssl3_UpdateHandshakeHashes(ss, hdr, sizeof(hdr));
+ if (rv != SECSuccess)
+ return rv;
+ rv = ssl3_UpdateHandshakeHashes(ss, b, length);
+ if (rv != SECSuccess)
+ return rv;
+
+ if (ss->ssl3.hs.snapStartType == snap_start_full) {
+ ss->ssl3.hs.snapStartType = snap_start_recovery;
+ } else {
+ ss->ssl3.hs.snapStartType = snap_start_resume_recovery;
+ }
+
+ ssl3_DestroyCipherSpec(ss->ssl3.pwSpec, PR_TRUE/*freeSrvName*/);
+
+ return SECSuccess;
+}
diff --git a/mozilla/security/nss/lib/ssl/ssl.def b/mozilla/security/nss/lib/ssl/ssl.def
index a1f4b51..effc35d 100644
--- a/mozilla/security/nss/lib/ssl/ssl.def
+++ b/mozilla/security/nss/lib/ssl/ssl.def
@@ -159,3 +159,13 @@ SSL_SetNextProtoNego;
;+ local:
;+*;
;+};
+;+NSS_3.13 { # NSS 3.13 release
+;+ global:
+SSL_GetPredictedServerHelloData;
+SSL_GetSnapStartResult;
+SSL_SetPredictedPeerCertificates;
+SSL_SetPredictedServerHelloData;
+SSL_SetSnapStartApplicationData;
+;+ local:
+;+*;
+;+};
diff --git a/mozilla/security/nss/lib/ssl/ssl.h b/mozilla/security/nss/lib/ssl/ssl.h
index d87ae56..8217d2e 100644
--- a/mozilla/security/nss/lib/ssl/ssl.h
+++ b/mozilla/security/nss/lib/ssl/ssl.h
@@ -139,6 +139,15 @@ SSL_IMPORT PRFileDesc *SSL_ImportFD(PRFileDesc *model, PRFileDesc *fd);
/* occur on RSA or DH ciphersuites where the cipher's key length is >= 80 */
/* bits. The advantage of False Start is that it saves a round trip for */
/* client-speaks-first protocols when performing a full handshake. */
+#define SSL_ENABLE_SNAP_START 23 /* Enable SSL snap start (off by */
+ /* default, applies only to */
+ /* clients). Snap start is a way */
+/* of performing TLS handshakes with no round trips. The client's entire */
+/* handshake is included in the first handshake message, along with */
+/* optional application data. In order to do this, information from a */
+/* previous connection to the same server is required. See */
+/* SSL_GetPredictedServerHelloData, SSL_SetPredictedPeerCertificates and */
+/* SSL_SetSnapStartApplicationData. */
#ifdef SSL_DEPRECATED_FUNCTION
/* Old deprecated function names */
@@ -376,6 +385,49 @@ SSL_IMPORT SECStatus SSL_BadCertHook(PRFileDesc *fd, SSLBadCertHandler f,
void *arg);
/*
+** Set the predicted chain of certificates for the peer. This is used for the
+** TLS Snap Start extension. Note that the SSL_ENABLE_SNAP_START option must
+** be set for this to occur.
+**
+** This function takes a reference to each of the given certificates.
+*/
+SSL_IMPORT SECStatus SSL_SetPredictedPeerCertificates(
+ PRFileDesc *fd, CERTCertificate **certs,
+ unsigned int numCerts);
+
+/*
+** Get the data needed to predict the server's hello message in the future. On
+** return, |*data| will either be NULL (in which case no data is available and
+** |*data_len| will be zero) or it will point to a buffer within the internal
+** data of |fd| and |*data_len| will contain the number of bytes available. If
+** non-NULL, |*data| will persist at least until the next handshake on |fd|.
+*/
+SSL_IMPORT SECStatus SSL_GetPredictedServerHelloData(
+ PRFileDesc *fd, const unsigned char **data,
+ unsigned int *data_len);
+
+/*
+** Set the predicted server hello data. This is used for the TLS Snap Start
+** extension. Note that the SSL_ENABLE_SNAP_START option must be set for this
+** to occur.
+*/
+SSL_IMPORT SECStatus SSL_SetPredictedServerHelloData(
+ PRFileDesc *fd, const unsigned char *data, unsigned int data_len);
+
+/* Set the application data which will be transmitted in a Snap Start
+** handshake. If the Snap Start handshake fails, this data will be
+* retransmitted automatically. */
+SSL_IMPORT SECStatus SSL_SetSnapStartApplicationData(
+ PRFileDesc *fd, const unsigned char *data, unsigned int data_len);
+
+/* Get the result of a Snap Start handshake. It's valid to call then even if
+** SSL_ENABLE_SNAP_START hasn't been set, although the result will always be
+** SSL_SNAP_START_NONE.
+*/
+SSL_IMPORT SECStatus SSL_GetSnapStartResult(PRFileDesc* socket,
+ SSLSnapStartResult* result);
+
+/*
** Configure SSL socket for running a secure server. Needs the
** certificate for the server and the servers private key. The arguments
** are copied.
diff --git a/mozilla/security/nss/lib/ssl/ssl3con.c b/mozilla/security/nss/lib/ssl/ssl3con.c
index 64c3452..9ab2a1c 100644
--- a/mozilla/security/nss/lib/ssl/ssl3con.c
+++ b/mozilla/security/nss/lib/ssl/ssl3con.c
@@ -72,7 +72,8 @@
#endif
static void ssl3_CleanupPeerCerts(sslSocket *ss);
-static void ssl3_CopyPeerCertsFromSID(sslSocket *ss, sslSessionID *sid);
+static void ssl3_CopyPeerCertsToSID(ssl3CertNode *certs,
+ sslSessionID *sid);
static PK11SymKey *ssl3_GenerateRSAPMS(sslSocket *ss, ssl3CipherSpec *spec,
PK11SlotInfo * serverKeySlot);
static SECStatus ssl3_DeriveMasterSecret(sslSocket *ss, PK11SymKey *pms);
@@ -82,14 +83,10 @@ static SECStatus ssl3_InitState( sslSocket *ss);
static SECStatus ssl3_SendCertificate( sslSocket *ss);
static SECStatus ssl3_SendEmptyCertificate( sslSocket *ss);
static SECStatus ssl3_SendCertificateRequest(sslSocket *ss);
-static SECStatus ssl3_SendNextProto( sslSocket *ss);
-static SECStatus ssl3_SendFinished( sslSocket *ss, PRInt32 flags);
static SECStatus ssl3_SendServerHello( sslSocket *ss);
static SECStatus ssl3_SendServerHelloDone( sslSocket *ss);
static SECStatus ssl3_SendServerKeyExchange( sslSocket *ss);
static SECStatus ssl3_NewHandshakeHashes( sslSocket *ss);
-static SECStatus ssl3_UpdateHandshakeHashes( sslSocket *ss, unsigned char *b,
- unsigned int l);
static SECStatus Null_Cipher(void *ctx, unsigned char *output, int *outputLen,
int maxOutputLen, const unsigned char *input,
@@ -583,7 +580,7 @@ void SSL_AtomicIncrementLong(long * x)
/* return pointer to ssl3CipherSuiteDef for suite, or NULL */
/* XXX This does a linear search. A binary search would be better. */
-static const ssl3CipherSuiteDef *
+const ssl3CipherSuiteDef *
ssl_LookupCipherSuiteDef(ssl3CipherSuite suite)
{
int cipher_suite_def_len =
@@ -1169,7 +1166,7 @@ ssl3_CleanupKeyMaterial(ssl3KeyMaterial *mat)
** ssl3_DestroySSL3Info
** Caller must hold SpecWriteLock.
*/
-static void
+void
ssl3_DestroyCipherSpec(ssl3CipherSpec *spec, PRBool freeSrvName)
{
PRBool freeit = (PRBool)(!spec->bypassCiphers);
@@ -1211,7 +1208,7 @@ ssl3_DestroyCipherSpec(ssl3CipherSpec *spec, PRBool freeSrvName)
** Caller must hold the ssl3 handshake lock.
** Acquires & releases SpecWriteLock.
*/
-static SECStatus
+SECStatus
ssl3_SetupPendingCipherSpec(sslSocket *ss)
{
ssl3CipherSpec * pwSpec;
@@ -2039,7 +2036,7 @@ ssl3_ClientAuthTokenPresent(sslSessionID *sid) {
return isPresent;
}
-static SECStatus
+SECStatus
ssl3_CompressMACEncryptRecord(sslSocket * ss,
SSL3ContentType type,
const SSL3Opaque * pIn,
@@ -3097,7 +3094,7 @@ loser:
return SECFailure;
}
-static SECStatus
+SECStatus
ssl3_RestartHandshakeHashes(sslSocket *ss)
{
SECStatus rv = SECSuccess;
@@ -3175,7 +3172,7 @@ loser:
** ssl3_HandleHandshakeMessage()
** Caller must hold the ssl3Handshake lock.
*/
-static SECStatus
+SECStatus
ssl3_UpdateHandshakeHashes(sslSocket *ss, unsigned char *b, unsigned int l)
{
SECStatus rv = SECSuccess;
@@ -3434,7 +3431,7 @@ ssl3_ConsumeHandshakeVariable(sslSocket *ss, SECItem *i, PRInt32 bytes,
* Caller must hold a read or write lock on the Spec R/W lock.
* (There is presently no way to assert on a Read lock.)
*/
-static SECStatus
+SECStatus
ssl3_ComputeHandshakeHashes(sslSocket * ss,
ssl3CipherSpec *spec, /* uses ->master_secret */
SSL3Hashes * hashes, /* output goes here. */
@@ -4032,7 +4029,18 @@ ssl3_SendClientHello(sslSocket *ss)
return rv; /* error code set by ssl3_FlushHandshake */
}
- ss->ssl3.hs.ws = wait_server_hello;
+ switch (ss->ssl3.hs.snapStartType) {
+ case snap_start_full:
+ ss->ssl3.hs.ws = wait_new_session_ticket;
+ break;
+ case snap_start_resume:
+ ss->ssl3.hs.ws = wait_change_cipher;
+ break;
+ default:
+ ss->ssl3.hs.ws = wait_server_hello;
+ break;
+ }
+
return rv;
}
@@ -4723,7 +4731,7 @@ loser:
/* Called from ssl3_HandleServerHelloDone(). */
-static SECStatus
+SECStatus
ssl3_SendClientKeyExchange(sslSocket *ss)
{
SECKEYPublicKey * serverKey = NULL;
@@ -4862,6 +4870,94 @@ done:
return rv;
}
+/* Called from ssl3_HandleServerHello to set up the master secret in
+ * ss->ssl3.pwSpec and the auth algorithm and kea type in ss->sec in the case
+ * of a successful session resumption. */
+SECStatus ssl3_SetupMasterSecretFromSessionID(sslSocket* ss) {
+ sslSessionID *sid = ss->sec.ci.sid;
+ ssl3CipherSpec *pwSpec = ss->ssl3.pwSpec;
+ SECItem wrappedMS; /* wrapped master secret. */
+
+ ss->sec.authAlgorithm = sid->authAlgorithm;
+ ss->sec.authKeyBits = sid->authKeyBits;
+ ss->sec.keaType = sid->keaType;
+ ss->sec.keaKeyBits = sid->keaKeyBits;
+
+ /* 3 cases here:
+ * a) key is wrapped (implies using PKCS11)
+ * b) key is unwrapped, but we're still using PKCS11
+ * c) key is unwrapped, and we're bypassing PKCS11.
+ */
+ if (sid->u.ssl3.keys.msIsWrapped) {
+ PK11SlotInfo *slot;
+ PK11SymKey * wrapKey; /* wrapping key */
+ CK_FLAGS keyFlags = 0;
+
+ if (ss->opt.bypassPKCS11) {
+ /* we cannot restart a non-bypass session in a
+ ** bypass socket.
+ */
+ return SECFailure;
+ }
+ /* unwrap master secret with PKCS11 */
+ slot = SECMOD_LookupSlot(sid->u.ssl3.masterModuleID,
+ sid->u.ssl3.masterSlotID);
+ if (slot == NULL) {
+ return SECFailure;
+ }
+ if (!PK11_IsPresent(slot)) {
+ PK11_FreeSlot(slot);
+ return SECFailure;
+ }
+ wrapKey = PK11_GetWrapKey(slot, sid->u.ssl3.masterWrapIndex,
+ sid->u.ssl3.masterWrapMech,
+ sid->u.ssl3.masterWrapSeries,
+ ss->pkcs11PinArg);
+ PK11_FreeSlot(slot);
+ if (wrapKey == NULL) {
+ return SECFailure;
+ }
+
+ if (ss->version > SSL_LIBRARY_VERSION_3_0) { /* isTLS */
+ keyFlags = CKF_SIGN | CKF_VERIFY;
+ }
+
+ wrappedMS.data = sid->u.ssl3.keys.wrapped_master_secret;
+ wrappedMS.len = sid->u.ssl3.keys.wrapped_master_secret_len;
+ pwSpec->master_secret =
+ PK11_UnwrapSymKeyWithFlags(wrapKey, sid->u.ssl3.masterWrapMech,
+ NULL, &wrappedMS, CKM_SSL3_MASTER_KEY_DERIVE,
+ CKA_DERIVE, sizeof(SSL3MasterSecret), keyFlags);
+ PK11_FreeSymKey(wrapKey);
+ if (pwSpec->master_secret == NULL) {
+ return SECFailure;
+ }
+ } else if (ss->opt.bypassPKCS11) {
+ /* MS is not wrapped */
+ wrappedMS.data = sid->u.ssl3.keys.wrapped_master_secret;
+ wrappedMS.len = sid->u.ssl3.keys.wrapped_master_secret_len;
+ memcpy(pwSpec->raw_master_secret, wrappedMS.data, wrappedMS.len);
+ pwSpec->msItem.data = pwSpec->raw_master_secret;
+ pwSpec->msItem.len = wrappedMS.len;
+ } else {
+ /* We CAN restart a bypass session in a non-bypass socket. */
+ /* need to import the raw master secret to session object */
+ PK11SlotInfo *slot = PK11_GetInternalSlot();
+ wrappedMS.data = sid->u.ssl3.keys.wrapped_master_secret;
+ wrappedMS.len = sid->u.ssl3.keys.wrapped_master_secret_len;
+ pwSpec->master_secret =
+ PK11_ImportSymKey(slot, CKM_SSL3_MASTER_KEY_DERIVE,
+ PK11_OriginUnwrap, CKA_ENCRYPT,
+ &wrappedMS, NULL);
+ PK11_FreeSlot(slot);
+ if (pwSpec->master_secret == NULL) {
+ return SECFailure;
+ }
+ }
+
+ return SECSuccess;
+}
+
/* Called from ssl3_HandleHandshakeMessage() when it has deciphered a complete
* ssl3 ServerHello message.
* Caller must hold Handshake and RecvBuf locks.
@@ -4886,6 +4982,14 @@ ssl3_HandleServerHello(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
PORT_Assert( ss->opt.noLocks || ssl_HaveRecvBufLock(ss) );
PORT_Assert( ss->opt.noLocks || ssl_HaveSSL3HandshakeLock(ss) );
+ if (ss->ssl3.hs.snapStartType == snap_start_full ||
+ ss->ssl3.hs.snapStartType == snap_start_resume) {
+ /* Snap Start handshake was rejected. */
+ rv = ssl3_ResetForSnapStartRecovery(ss, b, length);
+ if (rv != SECSuccess)
+ return rv;
+ }
+
rv = ssl3_InitState(ss);
if (rv != SECSuccess) {
errCode = PORT_GetError(); /* ssl3_InitState has set the error code. */
@@ -4897,6 +5001,21 @@ ssl3_HandleServerHello(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
goto alert_loser;
}
+ if (!ss->ssl3.serverHelloPredictionData.data) {
+ /* If this allocation fails it will only stop the application from
+ * recording the ServerHello information and performing future Snap
+ * Starts. */
+ if (SECITEM_AllocItem(NULL, &ss->ssl3.serverHelloPredictionData,
+ length))
+ memcpy(ss->ssl3.serverHelloPredictionData.data, b, length);
+ /* ss->ssl3.serverHelloPredictionDataValid is still false at this
+ * point. We have to record the contents of the ServerHello here
+ * because we don't have a pointer to the whole message when handling
+ * the extensions. However, we wait until the Snap Start extenion
+ * handler to recognise that the server supports Snap Start and to set
+ * serverHelloPredictionDataValid. */
+ }
+
temp = ssl3_ConsumeHandshakeNumber(ss, 2, &b, &length);
if (temp < 0) {
goto loser; /* alert has been sent */
@@ -5037,118 +5156,40 @@ ssl3_HandleServerHello(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
if (sid_match &&
sid->version == ss->version &&
- sid->u.ssl3.cipherSuite == ss->ssl3.hs.cipher_suite) do {
- ssl3CipherSpec *pwSpec = ss->ssl3.pwSpec;
-
- SECItem wrappedMS; /* wrapped master secret. */
+ sid->u.ssl3.cipherSuite == ss->ssl3.hs.cipher_suite) {
+ rv = ssl3_SetupMasterSecretFromSessionID(ss);
+ /* Failure of ssl3_SetupMasterSecretFromSessionID not considered an
+ * error. Continue with a full handshake. */
+ if (rv == SECSuccess) {
+ /* Got a Match */
+ SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_cache_hits );
- ss->sec.authAlgorithm = sid->authAlgorithm;
- ss->sec.authKeyBits = sid->authKeyBits;
- ss->sec.keaType = sid->keaType;
- ss->sec.keaKeyBits = sid->keaKeyBits;
+ /* If we sent a session ticket, then this is a stateless resume. */
+ if (sid->version > SSL_LIBRARY_VERSION_3_0 &&
+ sid->u.ssl3.sessionTicket.ticket.data != NULL)
+ SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_stateless_resumes );
- /* 3 cases here:
- * a) key is wrapped (implies using PKCS11)
- * b) key is unwrapped, but we're still using PKCS11
- * c) key is unwrapped, and we're bypassing PKCS11.
- */
- if (sid->u.ssl3.keys.msIsWrapped) {
- PK11SlotInfo *slot;
- PK11SymKey * wrapKey; /* wrapping key */
- CK_FLAGS keyFlags = 0;
+ if (ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn))
+ ss->ssl3.hs.ws = wait_new_session_ticket;
+ else
+ ss->ssl3.hs.ws = wait_change_cipher;
- if (ss->opt.bypassPKCS11) {
- /* we cannot restart a non-bypass session in a
- ** bypass socket.
- */
- break;
- }
- /* unwrap master secret with PKCS11 */
- slot = SECMOD_LookupSlot(sid->u.ssl3.masterModuleID,
- sid->u.ssl3.masterSlotID);
- if (slot == NULL) {
- break; /* not considered an error. */
- }
- if (!PK11_IsPresent(slot)) {
- PK11_FreeSlot(slot);
- break; /* not considered an error. */
- }
- wrapKey = PK11_GetWrapKey(slot, sid->u.ssl3.masterWrapIndex,
- sid->u.ssl3.masterWrapMech,
- sid->u.ssl3.masterWrapSeries,
- ss->pkcs11PinArg);
- PK11_FreeSlot(slot);
- if (wrapKey == NULL) {
- break; /* not considered an error. */
- }
+ ss->ssl3.hs.isResuming = PR_TRUE;
- if (ss->version > SSL_LIBRARY_VERSION_3_0) { /* isTLS */
- keyFlags = CKF_SIGN | CKF_VERIFY;
+ /* copy the peer cert from the SID */
+ if (sid->peerCert != NULL) {
+ ss->sec.peerCert = CERT_DupCertificate(sid->peerCert);
+ ssl3_CopyPeerCertsFromSID(ss, sid);
}
- wrappedMS.data = sid->u.ssl3.keys.wrapped_master_secret;
- wrappedMS.len = sid->u.ssl3.keys.wrapped_master_secret_len;
- pwSpec->master_secret =
- PK11_UnwrapSymKeyWithFlags(wrapKey, sid->u.ssl3.masterWrapMech,
- NULL, &wrappedMS, CKM_SSL3_MASTER_KEY_DERIVE,
- CKA_DERIVE, sizeof(SSL3MasterSecret), keyFlags);
- errCode = PORT_GetError();
- PK11_FreeSymKey(wrapKey);
- if (pwSpec->master_secret == NULL) {
- break; /* errorCode set just after call to UnwrapSymKey. */
- }
- } else if (ss->opt.bypassPKCS11) {
- /* MS is not wrapped */
- wrappedMS.data = sid->u.ssl3.keys.wrapped_master_secret;
- wrappedMS.len = sid->u.ssl3.keys.wrapped_master_secret_len;
- memcpy(pwSpec->raw_master_secret, wrappedMS.data, wrappedMS.len);
- pwSpec->msItem.data = pwSpec->raw_master_secret;
- pwSpec->msItem.len = wrappedMS.len;
- } else {
- /* We CAN restart a bypass session in a non-bypass socket. */
- /* need to import the raw master secret to session object */
- PK11SlotInfo *slot = PK11_GetInternalSlot();
- wrappedMS.data = sid->u.ssl3.keys.wrapped_master_secret;
- wrappedMS.len = sid->u.ssl3.keys.wrapped_master_secret_len;
- pwSpec->master_secret =
- PK11_ImportSymKey(slot, CKM_SSL3_MASTER_KEY_DERIVE,
- PK11_OriginUnwrap, CKA_ENCRYPT,
- &wrappedMS, NULL);
- PK11_FreeSlot(slot);
- if (pwSpec->master_secret == NULL) {
- break;
+ /* NULL value for PMS signifies re-use of the old MS */
+ rv = ssl3_InitPendingCipherSpec(ss, NULL);
+ if (rv != SECSuccess) {
+ goto alert_loser;
}
+ return SECSuccess;
}
-
- /* Got a Match */
- SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_cache_hits );
-
- /* If we sent a session ticket, then this is a stateless resume. */
- if (sid->version > SSL_LIBRARY_VERSION_3_0 &&
- sid->u.ssl3.sessionTicket.ticket.data != NULL)
- SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_stateless_resumes );
-
- if (ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn))
- ss->ssl3.hs.ws = wait_new_session_ticket;
- else
- ss->ssl3.hs.ws = wait_change_cipher;
-
- ss->ssl3.hs.isResuming = PR_TRUE;
-
- /* copy the peer cert from the SID */
- if (sid->peerCert != NULL) {
- ss->sec.peerCert = CERT_DupCertificate(sid->peerCert);
- ssl3_CopyPeerCertsFromSID(ss, sid);
- }
-
-
- /* NULL value for PMS signifies re-use of the old MS */
- rv = ssl3_InitPendingCipherSpec(ss, NULL);
- if (rv != SECSuccess) {
- goto alert_loser; /* err code was set */
- }
- return SECSuccess;
- } while (0);
+ }
if (sid_match)
SSL_AtomicIncrementLong(& ssl3stats.hsh_sid_cache_not_ok );
@@ -6116,7 +6157,7 @@ ssl3_HandleClientHello(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
* ticket extension, but sent an empty ticket.
*/
if (!ssl3_ExtensionNegotiated(ss, ssl_session_ticket_xtn) ||
- ss->xtnData.emptySessionTicket) {
+ ss->xtnData.serverReceivedEmptySessionTicket) {
if (sidBytes.len > 0 && !ss->opt.noCache) {
SSL_TRC(7, ("%d: SSL3[%d]: server, lookup client session-id for 0x%08x%08x%08x%08x",
SSL_GETPID(), ss->fd, ss->sec.ci.peer.pr_s6_addr32[0],
@@ -7569,6 +7610,12 @@ ssl3_HandleNewSessionTicket(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
return SECFailure; /* malformed */
}
+ if (ss->sec.ci.sid->peerCert == NULL) {
+ ss->sec.ci.sid->peerCert = CERT_DupCertificate(ss->sec.peerCert);
+ ssl3_CopyPeerCertsToSID((ssl3CertNode *)ss->ssl3.peerCertChain,
+ ss->sec.ci.sid);
+ }
+
rv = ssl3_SetSIDSessionTicket(ss->sec.ci.sid, &session_ticket);
if (rv != SECSuccess) {
(void)SSL3_SendAlert(ss, alert_fatal, handshake_failure);
@@ -7760,7 +7807,7 @@ ssl3_CleanupPeerCerts(sslSocket *ss)
ss->ssl3.peerCertChain = NULL;
}
-static void
+void
ssl3_CopyPeerCertsFromSID(sslSocket *ss, sslSessionID *sid)
{
PRArenaPool *arena;
@@ -8163,7 +8210,7 @@ ssl3_RestartHandshakeAfterServerCert(sslSocket *ss)
return rv;
}
-static SECStatus
+SECStatus
ssl3_ComputeTLSFinished(ssl3CipherSpec *spec,
PRBool isServer,
const SSL3Finished * hashes,
@@ -8211,7 +8258,7 @@ ssl3_ComputeTLSFinished(ssl3CipherSpec *spec,
/* called from ssl3_HandleServerHelloDone
*/
-static SECStatus
+SECStatus
ssl3_SendNextProto(sslSocket *ss)
{
SECStatus rv;
@@ -8247,7 +8294,7 @@ ssl3_SendNextProto(sslSocket *ss)
* ssl3_HandleClientHello
* ssl3_HandleFinished
*/
-static SECStatus
+SECStatus
ssl3_SendFinished(sslSocket *ss, PRInt32 flags)
{
ssl3CipherSpec *cwSpec;
@@ -8300,10 +8347,27 @@ ssl3_SendFinished(sslSocket *ss, PRInt32 flags)
if (rv != SECSuccess)
goto fail; /* err set by AppendHandshake. */
}
- rv = ssl3_FlushHandshake(ss, flags);
- if (rv != SECSuccess) {
- goto fail; /* error code set by ssl3_FlushHandshake */
+ if ((flags & ssl_SEND_FLAG_NO_FLUSH) == 0) {
+ rv = ssl3_FlushHandshake(ss, flags);
+ if (rv != SECSuccess) {
+ goto fail; /* error code set by ssl3_FlushHandshake */
+ }
}
+
+ if ((ss->ssl3.hs.snapStartType == snap_start_recovery ||
+ ss->ssl3.hs.snapStartType == snap_start_resume_recovery) &&
+ ss->ssl3.snapStartApplicationData.data) {
+ /* In the event that the server ignored the application data in our
+ * snap start extension, we need to retransmit it now. */
+ PRInt32 sent = ssl3_SendRecord(ss, content_application_data,
+ ss->ssl3.snapStartApplicationData.data,
+ ss->ssl3.snapStartApplicationData.len,
+ flags);
+ SECITEM_FreeItem(&ss->ssl3.snapStartApplicationData, PR_FALSE);
+ if (sent < 0)
+ return (SECStatus)sent; /* error code set by ssl3_SendRecord */
+ }
+
return SECSuccess;
fail:
@@ -8420,6 +8484,16 @@ ssl3_HandleFinished(sslSocket *ss, SSL3Opaque *b, PRUint32 length,
return SECFailure;
}
+ if (ss->ssl3.hs.snapStartType == snap_start_full ||
+ ss->ssl3.hs.snapStartType == snap_start_resume) {
+ /* Snap Start handshake was successful. Switch the cipher spec. */
+ ssl_GetSpecWriteLock(ss);
+ ssl3_DestroyCipherSpec(ss->ssl3.cwSpec, PR_TRUE/*freeSrvName*/);
+ ss->ssl3.cwSpec = ss->ssl3.pwSpec;
+ ss->ssl3.pwSpec = NULL;
+ ssl_ReleaseSpecWriteLock(ss);
+ }
+
isTLS = (PRBool)(ss->ssl3.crSpec->version > SSL_LIBRARY_VERSION_3_0);
if (isTLS) {
TLSFinished tlsFinished;
@@ -8429,12 +8503,21 @@ ssl3_HandleFinished(sslSocket *ss, SSL3Opaque *b, PRUint32 length,
PORT_SetError(SSL_ERROR_RX_MALFORMED_FINISHED);
return SECFailure;
}
- rv = ssl3_ComputeTLSFinished(ss->ssl3.crSpec, !isServer,
- hashes, &tlsFinished);
- if (!isServer)
- ss->ssl3.hs.finishedMsgs.tFinished[1] = tlsFinished;
- else
- ss->ssl3.hs.finishedMsgs.tFinished[0] = tlsFinished;
+
+ if (ss->ssl3.hs.snapStartType == snap_start_resume) {
+ /* In this case we have already advanced the Finished hash past the
+ * server's verify_data because we needed to predict the server's
+ * Finished message in order to compute our own (which includes
+ * it). When we did this, we stored a copy in tFinished[1]. */
+ tlsFinished = ss->ssl3.hs.finishedMsgs.tFinished[1];
+ } else {
+ rv = ssl3_ComputeTLSFinished(ss->ssl3.crSpec, !isServer,
+ hashes, &tlsFinished);
+ if (!isServer)
+ ss->ssl3.hs.finishedMsgs.tFinished[1] = tlsFinished;
+ else
+ ss->ssl3.hs.finishedMsgs.tFinished[0] = tlsFinished;
+ }
ss->ssl3.hs.finishedBytes = sizeof tlsFinished;
if (rv != SECSuccess ||
0 != NSS_SecureMemcmp(&tlsFinished, b, length)) {
@@ -8465,8 +8548,9 @@ ssl3_HandleFinished(sslSocket *ss, SSL3Opaque *b, PRUint32 length,
ssl_GetXmitBufLock(ss); /*************************************/
- if ((isServer && !ss->ssl3.hs.isResuming) ||
- (!isServer && ss->ssl3.hs.isResuming)) {
+ if (ss->ssl3.hs.snapStartType != snap_start_resume &&
+ ((isServer && !ss->ssl3.hs.isResuming) ||
+ (!isServer && ss->ssl3.hs.isResuming))) {
PRInt32 flags = 0;
/* Send a NewSessionTicket message if the client sent us
@@ -8582,7 +8666,10 @@ xmit_loser:
ss->ssl3.hs.ws = idle_handshake;
/* Do the handshake callback for sslv3 here, if we cannot false start. */
- if (ss->handshakeCallback != NULL && !ssl3_CanFalseStart(ss)) {
+ if (ss->handshakeCallback != NULL &&
+ (!ssl3_CanFalseStart(ss) ||
+ ss->ssl3.hs.snapStartType == snap_start_full ||
+ ss->ssl3.hs.snapStartType == snap_start_resume)) {
(ss->handshakeCallback)(ss->fd, ss->handshakeCallbackData);
}
@@ -8643,8 +8730,13 @@ ssl3_HandleHandshakeMessage(sslSocket *ss, SSL3Opaque *b, PRUint32 length)
return rv;
}
}
- /* We should not include hello_request messages in the handshake hashes */
- if (ss->ssl3.hs.msg_type != hello_request) {
+ /* We should not include hello_request messages in the handshake hashes.
+ * Likewise, for Finished messages from the server during a Snap Start
+ * resume, we have already predicted and included the message in our
+ * Finished hash. */
+ if (ss->ssl3.hs.msg_type != hello_request &&
+ !(ss->ssl3.hs.msg_type == finished &&
+ ss->ssl3.hs.snapStartType == snap_start_resume)) {
rv = ssl3_UpdateHandshakeHashes(ss, (unsigned char*) hdr, 4);
if (rv != SECSuccess) return rv; /* err code already set. */
rv = ssl3_UpdateHandshakeHashes(ss, b, length);
@@ -9545,6 +9637,15 @@ ssl3_DestroySSL3Info(sslSocket *ss)
ss->ssl3.clientCertChain = NULL;
}
+ if (ss->ssl3.predictedCertChain != NULL)
+ ssl3_CleanupPredictedPeerCertificates(ss);
+
+ if (ss->ssl3.serverHelloPredictionData.data)
+ SECITEM_FreeItem(&ss->ssl3.serverHelloPredictionData, PR_FALSE);
+
+ if (ss->ssl3.snapStartApplicationData.data)
+ SECITEM_FreeItem(&ss->ssl3.snapStartApplicationData, PR_FALSE);
+
/* clean up handshake */
if (ss->opt.bypassPKCS11) {
SHA1_DestroyContext((SHA1Context *)ss->ssl3.hs.sha_cx, PR_FALSE);
@@ -9562,6 +9663,9 @@ ssl3_DestroySSL3Info(sslSocket *ss)
ss->ssl3.hs.messages.len = 0;
ss->ssl3.hs.messages.space = 0;
}
+ if (ss->ssl3.hs.origClientHello.data) {
+ SECITEM_FreeItem(&ss->ssl3.hs.origClientHello, PR_FALSE);
+ }
/* free the SSL3Buffer (msg_body) */
PORT_Free(ss->ssl3.hs.msg_body.buf);
diff --git a/mozilla/security/nss/lib/ssl/ssl3ext.c b/mozilla/security/nss/lib/ssl/ssl3ext.c
index fbd5a91..a7ae062 100644
--- a/mozilla/security/nss/lib/ssl/ssl3ext.c
+++ b/mozilla/security/nss/lib/ssl/ssl3ext.c
@@ -247,6 +247,7 @@ static const ssl3HelloExtensionHandler serverHelloHandlersTLS[] = {
{ ssl_session_ticket_xtn, &ssl3_ClientHandleSessionTicketXtn },
{ ssl_renegotiation_info_xtn, &ssl3_HandleRenegotiationInfoXtn },
{ ssl_next_proto_neg_xtn, &ssl3_ClientHandleNextProtoNegoXtn },
+ { ssl_snap_start_xtn, &ssl3_ClientHandleSnapStartXtn },
{ -1, NULL }
};
@@ -270,7 +271,9 @@ ssl3HelloExtensionSender clientHelloSendersTLS[SSL_MAX_EXTENSIONS] = {
{ ssl_ec_point_formats_xtn, &ssl3_SendSupportedPointFormatsXtn },
#endif
{ ssl_session_ticket_xtn, &ssl3_SendSessionTicketXtn },
- { ssl_next_proto_neg_xtn, &ssl3_ClientSendNextProtoNegoXtn }
+ { ssl_next_proto_neg_xtn, &ssl3_ClientSendNextProtoNegoXtn },
+ { ssl_snap_start_xtn, &ssl3_SendSnapStartXtn }
+ /* NOTE: The Snap Start sender MUST be the last extension in the list. */
/* any extra entries will appear as { 0, NULL } */
};
@@ -298,7 +301,7 @@ ssl3_ExtensionNegotiated(sslSocket *ss, PRUint16 ex_type) {
xtnData->numNegotiated, ex_type);
}
-static PRBool
+PRBool
ssl3_ClientExtensionAdvertised(sslSocket *ss, PRUint16 ex_type) {
TLSExtensionData *xtnData = &ss->xtnData;
return arrayContainsExtension(xtnData->advertised,
@@ -515,6 +518,8 @@ ssl3_SendSessionTicketXtn(
rv = ssl3_AppendHandshakeVariable(ss, session_ticket->ticket.data,
session_ticket->ticket.len, 2);
ss->xtnData.ticketTimestampVerified = PR_FALSE;
+ if (!ss->sec.isServer)
+ ss->xtnData.clientSentNonEmptySessionTicket = PR_TRUE;
} else {
rv = ssl3_AppendHandshakeNumber(ss, 0, 2);
}
@@ -573,7 +578,7 @@ ssl3_ValidateNextProtoNego(const unsigned char* data, unsigned short length)
SECStatus
ssl3_ClientHandleNextProtoNegoXtn(sslSocket *ss, PRUint16 ex_type,
- SECItem *data)
+ SECItem *data)
{
unsigned int i, j;
SECStatus rv;
@@ -1021,7 +1026,7 @@ ssl3_ServerHandleSessionTicketXtn(sslSocket *ss, PRUint16 ex_type,
* instead of terminating the current connection.
*/
if (data->len == 0) {
- ss->xtnData.emptySessionTicket = PR_TRUE;
+ ss->xtnData.serverReceivedEmptySessionTicket = PR_TRUE;
} else {
int i;
SECItem extension_data;
diff --git a/mozilla/security/nss/lib/ssl/sslimpl.h b/mozilla/security/nss/lib/ssl/sslimpl.h
index fe7ac7a..f708696 100644
--- a/mozilla/security/nss/lib/ssl/sslimpl.h
+++ b/mozilla/security/nss/lib/ssl/sslimpl.h
@@ -278,6 +278,7 @@ struct sslSocketOpsStr {
/* Flags interpreted by ssl send functions. */
#define ssl_SEND_FLAG_FORCE_INTO_BUFFER 0x40000000
#define ssl_SEND_FLAG_NO_BUFFER 0x20000000
+#define ssl_SEND_FLAG_NO_FLUSH 0x10000000
#define ssl_SEND_FLAG_MASK 0x7f000000
/*
@@ -339,6 +340,7 @@ typedef struct sslOptionsStr {
unsigned int enableRenegotiation : 2; /* 20-21 */
unsigned int requireSafeNegotiation : 1; /* 22 */
unsigned int enableFalseStart : 1; /* 23 */
+ unsigned int enableSnapStart : 1; /* 24 */
} sslOptions;
typedef enum { sslHandshakingUndetermined = 0,
@@ -743,7 +745,8 @@ struct TLSExtensionDataStr {
/* SessionTicket Extension related data. */
PRBool ticketTimestampVerified;
- PRBool emptySessionTicket;
+ PRBool serverReceivedEmptySessionTicket;
+ PRBool clientSentNonEmptySessionTicket;
/* SNI Extension related data
* Names data is not coppied from the input buffer. It can not be
@@ -753,6 +756,14 @@ struct TLSExtensionDataStr {
PRUint32 sniNameArrSize;
};
+typedef enum {
+ snap_start_none = 0,
+ snap_start_full,
+ snap_start_recovery,
+ snap_start_resume,
+ snap_start_resume_recovery
+} TLSSnapStartType;
+
/*
** This is the "hs" member of the "ssl3" struct.
** This entire struct is protected by ssl3HandshakeLock
@@ -791,6 +802,14 @@ const ssl3CipherSuiteDef *suite_def;
SSL3Hashes sFinished[2];
SSL3Opaque data[72];
} finishedMsgs;
+
+ TLSSnapStartType snapStartType;
+ /* When we perform a Snap Start handshake, we hash our ClientHello as if
+ * the Snap Start extension wasn't included. However, if the server rejects
+ * our Snap Start attempt, then it will hash the whole ClientHello. Thus we
+ * store the original ClientHello that we sent in case we need to reset our
+ * Finished hash to cover it. */
+ SECItem origClientHello;
#ifdef NSS_ENABLE_ECC
PRUint32 negotiatedECCurves; /* bit mask */
#endif /* NSS_ENABLE_ECC */
@@ -823,6 +842,17 @@ struct ssl3StateStr {
CERTCertificateList *clientCertChain; /* used by client */
PRBool sendEmptyCert; /* used by client */
+ /* TLS Snap Start: */
+ CERTCertificate ** predictedCertChain;
+ /* An array terminated with a NULL. */
+ SECItem serverHelloPredictionData;
+ PRBool serverHelloPredictionDataValid;
+ /* data needed to predict the ServerHello from
+ * this server. */
+ SECItem snapStartApplicationData;
+ /* the application data to include in the Snap
+ * Start extension. */
+
int policy;
/* This says what cipher suites we can do, and should
* be either SSL_ALLOWED or SSL_RESTRICTED
@@ -1258,10 +1288,13 @@ extern sslSessionID *ssl3_NewSessionID(sslSocket *ss, PRBool is_server);
extern sslSessionID *ssl_LookupSID(const PRIPv6Addr *addr, PRUint16 port,
const char *peerID, const char *urlSvrName);
extern void ssl_FreeSID(sslSessionID *sid);
+extern void ssl3_CopyPeerCertsFromSID(sslSocket *ss, sslSessionID *sid);
extern int ssl3_SendApplicationData(sslSocket *ss, const PRUint8 *in,
int len, int flags);
+extern SECStatus ssl3_RestartHandshakeHashes(sslSocket *ss);
+
extern PRBool ssl_FdIsBlocking(PRFileDesc *fd);
extern PRBool ssl_SocketIsBlocking(sslSocket *ss);
@@ -1434,6 +1467,9 @@ ECName ssl3_GetCurveWithECKeyStrength(PRUint32 curvemsk, int requiredECCbits);
#endif /* NSS_ENABLE_ECC */
+extern SECStatus ssl3_UpdateHandshakeHashes(sslSocket* ss, unsigned char *b,
+ unsigned int l);
+
extern SECStatus ssl3_CipherPrefSetDefault(ssl3CipherSuite which, PRBool on);
extern SECStatus ssl3_CipherPrefGetDefault(ssl3CipherSuite which, PRBool *on);
extern SECStatus ssl2_CipherPrefSetDefault(PRInt32 which, PRBool enabled);
@@ -1454,6 +1490,7 @@ extern void ssl3_InitSocketPolicy(sslSocket *ss);
extern SECStatus ssl3_ConstructV2CipherSpecsHack(sslSocket *ss,
unsigned char *cs, int *size);
+extern void ssl3_DestroyCipherSpec(ssl3CipherSpec* spec, PRBool freeSrvName);
extern SECStatus ssl3_RedoHandshake(sslSocket *ss, PRBool flushCache);
@@ -1503,6 +1540,31 @@ extern SECStatus ssl3_VerifySignedHashes(SSL3Hashes *hash,
extern SECStatus ssl3_CacheWrappedMasterSecret(sslSocket *ss,
sslSessionID *sid, ssl3CipherSpec *spec,
SSL3KEAType effectiveExchKeyType);
+extern void ssl3_CleanupPredictedPeerCertificates(sslSocket *ss);
+extern const ssl3CipherSuiteDef* ssl_LookupCipherSuiteDef(ssl3CipherSuite suite);
+extern SECStatus ssl3_SetupPendingCipherSpec(sslSocket *ss);
+extern SECStatus ssl3_SendClientKeyExchange(sslSocket *ss);
+extern SECStatus ssl3_SendNextProto(sslSocket *ss);
+extern SECStatus ssl3_SendFinished(sslSocket *ss, PRInt32 flags);
+extern SECStatus ssl3_CompressMACEncryptRecord
+ (sslSocket * ss,
+ SSL3ContentType type,
+ const SSL3Opaque * pIn,
+ PRUint32 contentLen);
+extern PRBool ssl3_ClientExtensionAdvertised(sslSocket *ss, PRUint16 ex_type);
+extern SECStatus ssl3_SetupMasterSecretFromSessionID(sslSocket* ss);
+extern SECStatus ssl3_ComputeHandshakeHashes(
+ sslSocket * ss,
+ ssl3CipherSpec *spec, /* uses ->master_secret */
+ SSL3Hashes * hashes, /* output goes here. */
+ PRUint32 sender);
+extern SECStatus ssl3_UpdateHandshakeHashes(sslSocket* ss, unsigned char *b,
+ unsigned int l);
+extern SECStatus ssl3_ComputeTLSFinished(
+ ssl3CipherSpec *spec,
+ PRBool isServer,
+ const SSL3Finished * hashes,
+ TLSFinished * tlsFinished);
/* Functions that handle ClientHello and ServerHello extensions. */
extern SECStatus ssl3_HandleServerNameXtn(sslSocket * ss,
@@ -1532,6 +1594,13 @@ extern PRInt32 ssl3_SendSessionTicketXtn(sslSocket *ss, PRBool append,
*/
extern PRInt32 ssl3_SendServerNameXtn(sslSocket *ss, PRBool append,
PRUint32 maxBytes);
+extern PRInt32 ssl3_SendSnapStartXtn(sslSocket *ss, PRBool append,
+ PRUint32 maxBytes);
+extern SECStatus ssl3_ClientHandleSnapStartXtn(sslSocket *ss, PRUint16 ex_type,
+ SECItem *data);
+
+extern SECStatus ssl3_ResetForSnapStartRecovery(sslSocket *ss,
+ SSL3Opaque *b, PRUint32 length);
/* Assigns new cert, cert chain and keys to ss->serverCerts
* struct. If certChain is NULL, tries to find one. Aborts if
@@ -1635,6 +1704,12 @@ SECStatus SSL_DisableDefaultExportCipherSuites(void);
SECStatus SSL_DisableExportCipherSuites(PRFileDesc * fd);
PRBool SSL_IsExportCipherSuite(PRUint16 cipherSuite);
+/********************** FNV hash *********************/
+
+void FNV1A64_Init(PRUint64 *digest);
+void FNV1A64_Update(PRUint64 *digest, const unsigned char *data,
+ unsigned int length);
+void FNV1A64_Final(PRUint64 *digest);
#ifdef TRACE
#define SSL_TRACE(msg) ssl_Trace msg
diff --git a/mozilla/security/nss/lib/ssl/sslsock.c b/mozilla/security/nss/lib/ssl/sslsock.c
index ca0d714..2898b88 100644
--- a/mozilla/security/nss/lib/ssl/sslsock.c
+++ b/mozilla/security/nss/lib/ssl/sslsock.c
@@ -738,6 +738,10 @@ SSL_OptionSet(PRFileDesc *fd, PRInt32 which, PRBool on)
ss->opt.enableFalseStart = on;
break;
+ case SSL_ENABLE_SNAP_START:
+ ss->opt.enableSnapStart = on;
+ break;
+
default:
PORT_SetError(SEC_ERROR_INVALID_ARGS);
rv = SECFailure;
@@ -802,6 +806,7 @@ SSL_OptionGet(PRFileDesc *fd, PRInt32 which, PRBool *pOn)
case SSL_REQUIRE_SAFE_NEGOTIATION:
on = ss->opt.requireSafeNegotiation; break;
case SSL_ENABLE_FALSE_START: on = ss->opt.enableFalseStart; break;
+ case SSL_ENABLE_SNAP_START: on = ss->opt.enableSnapStart; break;
default:
PORT_SetError(SEC_ERROR_INVALID_ARGS);
@@ -853,6 +858,7 @@ SSL_OptionGetDefault(PRInt32 which, PRBool *pOn)
on = ssl_defaults.requireSafeNegotiation;
break;
case SSL_ENABLE_FALSE_START: on = ssl_defaults.enableFalseStart; break;
+ case SSL_ENABLE_SNAP_START: on = ssl_defaults.enableSnapStart; break;
default:
PORT_SetError(SEC_ERROR_INVALID_ARGS);
@@ -1000,6 +1006,10 @@ SSL_OptionSetDefault(PRInt32 which, PRBool on)
ssl_defaults.enableFalseStart = on;
break;
+ case SSL_ENABLE_SNAP_START:
+ ssl_defaults.enableSnapStart = on;
+ break;
+
default:
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
diff --git a/mozilla/security/nss/lib/ssl/sslt.h b/mozilla/security/nss/lib/ssl/sslt.h
index f6e0b62..68cbf87 100644
--- a/mozilla/security/nss/lib/ssl/sslt.h
+++ b/mozilla/security/nss/lib/ssl/sslt.h
@@ -204,9 +204,23 @@ typedef enum {
#endif
ssl_session_ticket_xtn = 35,
ssl_next_proto_neg_xtn = 13172,
+ ssl_snap_start_xtn = 13174,
ssl_renegotiation_info_xtn = 0xff01 /* experimental number */
} SSLExtensionType;
-#define SSL_MAX_EXTENSIONS 6
+#define SSL_MAX_EXTENSIONS 7
+
+typedef enum {
+ /* No Snap Start handshake was attempted. */
+ SSL_SNAP_START_NONE = 0,
+ /* A Snap Start full handshake was completed. */
+ SSL_SNAP_START_FULL = 1,
+ /* A Snap Start full handshake was attempted, but failed. */
+ SSL_SNAP_START_RECOVERY = 2,
+ /* A Snap Start resume handshake was completed. */
+ SSL_SNAP_START_RESUME = 3,
+ /* A Snap Start resume handshake was attempted, but failed. */
+ SSL_SNAP_START_RESUME_RECOVERY = 4
+} SSLSnapStartResult;
#endif /* __sslt_h_ */
|