pixel-react 1.15.21 → 1.15.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/lib/_virtual/aesCipherSuites.js +4 -0
  2. package/lib/_virtual/aesCipherSuites.js.map +1 -0
  3. package/lib/_virtual/asn1-validator.js +4 -0
  4. package/lib/_virtual/asn1-validator.js.map +1 -0
  5. package/lib/_virtual/asn1.js +4 -0
  6. package/lib/_virtual/asn1.js.map +1 -0
  7. package/lib/_virtual/cipherModes.js +4 -0
  8. package/lib/_virtual/cipherModes.js.map +1 -0
  9. package/lib/_virtual/hmac.js +4 -0
  10. package/lib/_virtual/hmac.js.map +1 -0
  11. package/lib/_virtual/index11.js +2 -2
  12. package/lib/_virtual/index12.js +2 -2
  13. package/lib/_virtual/index13.js +2 -2
  14. package/lib/_virtual/index14.js +4 -0
  15. package/lib/_virtual/index14.js.map +1 -0
  16. package/lib/_virtual/index2.js +5 -3
  17. package/lib/_virtual/index2.js.map +1 -1
  18. package/lib/_virtual/index3.js +4 -2
  19. package/lib/_virtual/index3.js.map +1 -1
  20. package/lib/_virtual/index4.js +2 -4
  21. package/lib/_virtual/index4.js.map +1 -1
  22. package/lib/_virtual/index5.js +4 -2
  23. package/lib/_virtual/index5.js.map +1 -1
  24. package/lib/_virtual/index6.js +2 -2
  25. package/lib/_virtual/index7.js +2 -6
  26. package/lib/_virtual/index7.js.map +1 -1
  27. package/lib/_virtual/index8.js +6 -2
  28. package/lib/_virtual/index8.js.map +1 -1
  29. package/lib/_virtual/index9.js +2 -2
  30. package/lib/_virtual/md5.js +4 -0
  31. package/lib/_virtual/md5.js.map +1 -0
  32. package/lib/_virtual/mgf1.js +4 -0
  33. package/lib/_virtual/mgf1.js.map +1 -0
  34. package/lib/_virtual/oids.js +4 -0
  35. package/lib/_virtual/oids.js.map +1 -0
  36. package/lib/_virtual/pem.js +4 -0
  37. package/lib/_virtual/pem.js.map +1 -0
  38. package/lib/_virtual/pkcs1.js +4 -0
  39. package/lib/_virtual/pkcs1.js.map +1 -0
  40. package/lib/_virtual/pkcs12.js +4 -0
  41. package/lib/_virtual/pkcs12.js.map +1 -0
  42. package/lib/_virtual/pkcs7.js +4 -0
  43. package/lib/_virtual/pkcs7.js.map +1 -0
  44. package/lib/_virtual/pkcs7asn1.js +4 -0
  45. package/lib/_virtual/pkcs7asn1.js.map +1 -0
  46. package/lib/_virtual/pki.js +4 -0
  47. package/lib/_virtual/pki.js.map +1 -0
  48. package/lib/_virtual/prime.js +4 -0
  49. package/lib/_virtual/prime.js.map +1 -0
  50. package/lib/_virtual/prng.js +4 -0
  51. package/lib/_virtual/prng.js.map +1 -0
  52. package/lib/_virtual/pss.js +4 -0
  53. package/lib/_virtual/pss.js.map +1 -0
  54. package/lib/_virtual/random.js +4 -0
  55. package/lib/_virtual/random.js.map +1 -0
  56. package/lib/_virtual/sha1.js +4 -0
  57. package/lib/_virtual/sha1.js.map +1 -0
  58. package/lib/_virtual/sha256.js +4 -0
  59. package/lib/_virtual/sha256.js.map +1 -0
  60. package/lib/_virtual/sha512.js +4 -0
  61. package/lib/_virtual/sha512.js.map +1 -0
  62. package/lib/_virtual/ssh.js +4 -0
  63. package/lib/_virtual/ssh.js.map +1 -0
  64. package/lib/_virtual/util.js +4 -0
  65. package/lib/_virtual/util.js.map +1 -0
  66. package/lib/_virtual/x509.js +4 -0
  67. package/lib/_virtual/x509.js.map +1 -0
  68. package/lib/assets/icons/testify_icon.svg.js +6 -0
  69. package/lib/assets/icons/testify_icon.svg.js.map +1 -0
  70. package/lib/assets/icons/testify_loader.svg.js +6 -0
  71. package/lib/assets/icons/testify_loader.svg.js.map +1 -0
  72. package/lib/assets/icons/testify_logo_name.svg.js +6 -0
  73. package/lib/assets/icons/testify_logo_name.svg.js.map +1 -0
  74. package/lib/components/AllProjectsDropdown/AllProjectsDropdown.js +3 -1
  75. package/lib/components/AllProjectsDropdown/AllProjectsDropdown.js.map +1 -1
  76. package/lib/components/Charts/DashboardDonutChart/DashboardDonutChart.js +2 -9
  77. package/lib/components/Charts/DashboardDonutChart/DashboardDonutChart.js.map +1 -1
  78. package/lib/components/Charts/DashboardDonutChart/types.d.ts +1 -1
  79. package/lib/components/ConditionalDropdown/ConditionalDropdown.js +2 -9
  80. package/lib/components/ConditionalDropdown/ConditionalDropdown.js.map +1 -1
  81. package/lib/components/Editor/Editor.js +37 -17
  82. package/lib/components/Editor/Editor.js.map +1 -1
  83. package/lib/components/Icon/iconList.js +6 -0
  84. package/lib/components/Icon/iconList.js.map +1 -1
  85. package/lib/components/MultiSelect/MultiSelect.js +9 -10
  86. package/lib/components/MultiSelect/MultiSelect.js.map +1 -1
  87. package/lib/components/PhoneInput/PhoneInput.js +1 -0
  88. package/lib/components/PhoneInput/PhoneInput.js.map +1 -1
  89. package/lib/components/SessionDropdown/SessionDropdown.js +4 -2
  90. package/lib/components/SessionDropdown/SessionDropdown.js.map +1 -1
  91. package/lib/index.cjs +27423 -379
  92. package/lib/index.cjs.map +1 -1
  93. package/lib/index.d.ts +1 -1
  94. package/lib/node_modules/classnames/index.js +1 -1
  95. package/lib/node_modules/input-format/modules/react/Input.js +1 -1
  96. package/lib/node_modules/js-beautify/js/index.js +1 -1
  97. package/lib/node_modules/js-beautify/js/src/html/beautifier.js +1 -1
  98. package/lib/node_modules/js-beautify/js/src/html/index.js +1 -1
  99. package/lib/node_modules/js-beautify/js/src/html/options.js +1 -1
  100. package/lib/node_modules/js-beautify/js/src/html/tokenizer.js +1 -1
  101. package/lib/node_modules/js-beautify/js/src/index.js +1 -1
  102. package/lib/node_modules/js-beautify/js/src/javascript/beautifier.js +1 -1
  103. package/lib/node_modules/js-beautify/js/src/javascript/index.js +1 -1
  104. package/lib/node_modules/js-beautify/js/src/javascript/options.js +1 -1
  105. package/lib/node_modules/js-beautify/js/src/javascript/tokenizer.js +1 -1
  106. package/lib/node_modules/node-forge/lib/aes.js +1014 -0
  107. package/lib/node_modules/node-forge/lib/aes.js.map +1 -0
  108. package/lib/node_modules/node-forge/lib/aesCipherSuites.js +286 -0
  109. package/lib/node_modules/node-forge/lib/aesCipherSuites.js.map +1 -0
  110. package/lib/node_modules/node-forge/lib/asn1-validator.js +100 -0
  111. package/lib/node_modules/node-forge/lib/asn1-validator.js.map +1 -0
  112. package/lib/node_modules/node-forge/lib/asn1.js +1379 -0
  113. package/lib/node_modules/node-forge/lib/asn1.js.map +1 -0
  114. package/lib/node_modules/node-forge/lib/baseN.js +181 -0
  115. package/lib/node_modules/node-forge/lib/baseN.js.map +1 -0
  116. package/lib/node_modules/node-forge/lib/cipher.js +236 -0
  117. package/lib/node_modules/node-forge/lib/cipher.js.map +1 -0
  118. package/lib/node_modules/node-forge/lib/cipherModes.js +936 -0
  119. package/lib/node_modules/node-forge/lib/cipherModes.js.map +1 -0
  120. package/lib/node_modules/node-forge/lib/des.js +467 -0
  121. package/lib/node_modules/node-forge/lib/des.js.map +1 -0
  122. package/lib/node_modules/node-forge/lib/ed25519.js +1108 -0
  123. package/lib/node_modules/node-forge/lib/ed25519.js.map +1 -0
  124. package/lib/node_modules/node-forge/lib/forge.js +23 -0
  125. package/lib/node_modules/node-forge/lib/forge.js.map +1 -0
  126. package/lib/node_modules/node-forge/lib/hmac.js +158 -0
  127. package/lib/node_modules/node-forge/lib/hmac.js.map +1 -0
  128. package/lib/node_modules/node-forge/lib/index.js +70 -0
  129. package/lib/node_modules/node-forge/lib/index.js.map +1 -0
  130. package/lib/node_modules/node-forge/lib/jsbn.js +1481 -0
  131. package/lib/node_modules/node-forge/lib/jsbn.js.map +1 -0
  132. package/lib/node_modules/node-forge/lib/kem.js +178 -0
  133. package/lib/node_modules/node-forge/lib/kem.js.map +1 -0
  134. package/lib/node_modules/node-forge/lib/log.js +325 -0
  135. package/lib/node_modules/node-forge/lib/log.js.map +1 -0
  136. package/lib/node_modules/node-forge/lib/md.all.js +28 -0
  137. package/lib/node_modules/node-forge/lib/md.all.js.map +1 -0
  138. package/lib/node_modules/node-forge/lib/md.js +22 -0
  139. package/lib/node_modules/node-forge/lib/md.js.map +1 -0
  140. package/lib/node_modules/node-forge/lib/md5.js +288 -0
  141. package/lib/node_modules/node-forge/lib/md5.js.map +1 -0
  142. package/lib/node_modules/node-forge/lib/mgf.js +24 -0
  143. package/lib/node_modules/node-forge/lib/mgf.js.map +1 -0
  144. package/lib/node_modules/node-forge/lib/mgf1.js +68 -0
  145. package/lib/node_modules/node-forge/lib/mgf1.js.map +1 -0
  146. package/lib/node_modules/node-forge/lib/oids.js +185 -0
  147. package/lib/node_modules/node-forge/lib/oids.js.map +1 -0
  148. package/lib/node_modules/node-forge/lib/pbe.js +966 -0
  149. package/lib/node_modules/node-forge/lib/pbe.js.map +1 -0
  150. package/lib/node_modules/node-forge/lib/pbkdf2.js +209 -0
  151. package/lib/node_modules/node-forge/lib/pbkdf2.js.map +1 -0
  152. package/lib/node_modules/node-forge/lib/pem.js +250 -0
  153. package/lib/node_modules/node-forge/lib/pem.js.map +1 -0
  154. package/lib/node_modules/node-forge/lib/pkcs1.js +273 -0
  155. package/lib/node_modules/node-forge/lib/pkcs1.js.map +1 -0
  156. package/lib/node_modules/node-forge/lib/pkcs12.js +980 -0
  157. package/lib/node_modules/node-forge/lib/pkcs12.js.map +1 -0
  158. package/lib/node_modules/node-forge/lib/pkcs7.js +1073 -0
  159. package/lib/node_modules/node-forge/lib/pkcs7.js.map +1 -0
  160. package/lib/node_modules/node-forge/lib/pkcs7asn1.js +415 -0
  161. package/lib/node_modules/node-forge/lib/pkcs7asn1.js.map +1 -0
  162. package/lib/node_modules/node-forge/lib/pki.js +125 -0
  163. package/lib/node_modules/node-forge/lib/pki.js.map +1 -0
  164. package/lib/node_modules/node-forge/lib/prime.js +297 -0
  165. package/lib/node_modules/node-forge/lib/prime.js.map +1 -0
  166. package/lib/node_modules/node-forge/lib/prng.js +433 -0
  167. package/lib/node_modules/node-forge/lib/prng.js.map +1 -0
  168. package/lib/node_modules/node-forge/lib/pss.js +246 -0
  169. package/lib/node_modules/node-forge/lib/pss.js.map +1 -0
  170. package/lib/node_modules/node-forge/lib/random.js +191 -0
  171. package/lib/node_modules/node-forge/lib/random.js.map +1 -0
  172. package/lib/node_modules/node-forge/lib/rc2.js +382 -0
  173. package/lib/node_modules/node-forge/lib/rc2.js.map +1 -0
  174. package/lib/node_modules/node-forge/lib/rsa.js +1815 -0
  175. package/lib/node_modules/node-forge/lib/rsa.js.map +1 -0
  176. package/lib/node_modules/node-forge/lib/sha1.js +325 -0
  177. package/lib/node_modules/node-forge/lib/sha1.js.map +1 -0
  178. package/lib/node_modules/node-forge/lib/sha256.js +306 -0
  179. package/lib/node_modules/node-forge/lib/sha256.js.map +1 -0
  180. package/lib/node_modules/node-forge/lib/sha512.js +479 -0
  181. package/lib/node_modules/node-forge/lib/sha512.js.map +1 -0
  182. package/lib/node_modules/node-forge/lib/ssh.js +244 -0
  183. package/lib/node_modules/node-forge/lib/ssh.js.map +1 -0
  184. package/lib/node_modules/node-forge/lib/tls.js +4207 -0
  185. package/lib/node_modules/node-forge/lib/tls.js.map +1 -0
  186. package/lib/node_modules/node-forge/lib/util.js +2565 -0
  187. package/lib/node_modules/node-forge/lib/util.js.map +1 -0
  188. package/lib/node_modules/node-forge/lib/x509.js +2986 -0
  189. package/lib/node_modules/node-forge/lib/x509.js.map +1 -0
  190. package/lib/node_modules/prop-types/index.js +1 -1
  191. package/lib/node_modules/react-async-script/lib/esm/async-script-loader.js +1 -1
  192. package/lib/node_modules/react-google-recaptcha/lib/esm/recaptcha.js +1 -1
  193. package/lib/node_modules/react-is/index.js +1 -1
  194. package/lib/node_modules/react-phone-number-input/modules/CountryIcon.js +1 -1
  195. package/lib/node_modules/react-phone-number-input/modules/CountrySelect.js +1 -1
  196. package/lib/node_modules/react-phone-number-input/modules/Flag.js +1 -1
  197. package/lib/node_modules/react-phone-number-input/modules/InputBasic.js +1 -1
  198. package/lib/node_modules/react-phone-number-input/modules/InputSmart.js +1 -1
  199. package/lib/node_modules/react-phone-number-input/modules/InternationalIcon.js +1 -1
  200. package/lib/node_modules/react-phone-number-input/modules/PhoneInputWithCountry.js +1 -1
  201. package/lib/node_modules/react-phone-number-input/modules/PropTypes.js +1 -1
  202. package/lib/node_modules/scheduler/index.js +1 -1
  203. package/lib/node_modules/use-context-selector/dist/index.js +1 -1
  204. package/lib/styles.css +1 -1
  205. package/lib/styles.css.map +1 -1
  206. package/lib/utils/getEncryptedData/getEncryptedData.js +10 -13
  207. package/lib/utils/getEncryptedData/getEncryptedData.js.map +1 -1
  208. package/package.json +3 -1
@@ -0,0 +1,4207 @@
1
+ import { __require as requireForge } from './forge.js';
2
+ import { __require as requireAsn1 } from './asn1.js';
3
+ import { __require as requireHmac } from './hmac.js';
4
+ import { __require as requireMd5 } from './md5.js';
5
+ import { __require as requirePem } from './pem.js';
6
+ import { __require as requirePki } from './pki.js';
7
+ import { __require as requireRandom } from './random.js';
8
+ import { __require as requireSha1 } from './sha1.js';
9
+ import { __require as requireUtil } from './util.js';
10
+
11
+ /**
12
+ * A Javascript implementation of Transport Layer Security (TLS).
13
+ *
14
+ * @author Dave Longley
15
+ *
16
+ * Copyright (c) 2009-2014 Digital Bazaar, Inc.
17
+ *
18
+ * The TLS Handshake Protocol involves the following steps:
19
+ *
20
+ * - Exchange hello messages to agree on algorithms, exchange random values,
21
+ * and check for session resumption.
22
+ *
23
+ * - Exchange the necessary cryptographic parameters to allow the client and
24
+ * server to agree on a premaster secret.
25
+ *
26
+ * - Exchange certificates and cryptographic information to allow the client
27
+ * and server to authenticate themselves.
28
+ *
29
+ * - Generate a master secret from the premaster secret and exchanged random
30
+ * values.
31
+ *
32
+ * - Provide security parameters to the record layer.
33
+ *
34
+ * - Allow the client and server to verify that their peer has calculated the
35
+ * same security parameters and that the handshake occurred without tampering
36
+ * by an attacker.
37
+ *
38
+ * Up to 4 different messages may be sent during a key exchange. The server
39
+ * certificate, the server key exchange, the client certificate, and the
40
+ * client key exchange.
41
+ *
42
+ * A typical handshake (from the client's perspective).
43
+ *
44
+ * 1. Client sends ClientHello.
45
+ * 2. Client receives ServerHello.
46
+ * 3. Client receives optional Certificate.
47
+ * 4. Client receives optional ServerKeyExchange.
48
+ * 5. Client receives ServerHelloDone.
49
+ * 6. Client sends optional Certificate.
50
+ * 7. Client sends ClientKeyExchange.
51
+ * 8. Client sends optional CertificateVerify.
52
+ * 9. Client sends ChangeCipherSpec.
53
+ * 10. Client sends Finished.
54
+ * 11. Client receives ChangeCipherSpec.
55
+ * 12. Client receives Finished.
56
+ * 13. Client sends/receives application data.
57
+ *
58
+ * To reuse an existing session:
59
+ *
60
+ * 1. Client sends ClientHello with session ID for reuse.
61
+ * 2. Client receives ServerHello with same session ID if reusing.
62
+ * 3. Client receives ChangeCipherSpec message if reusing.
63
+ * 4. Client receives Finished.
64
+ * 5. Client sends ChangeCipherSpec.
65
+ * 6. Client sends Finished.
66
+ *
67
+ * Note: Client ignores HelloRequest if in the middle of a handshake.
68
+ *
69
+ * Record Layer:
70
+ *
71
+ * The record layer fragments information blocks into TLSPlaintext records
72
+ * carrying data in chunks of 2^14 bytes or less. Client message boundaries are
73
+ * not preserved in the record layer (i.e., multiple client messages of the
74
+ * same ContentType MAY be coalesced into a single TLSPlaintext record, or a
75
+ * single message MAY be fragmented across several records).
76
+ *
77
+ * struct {
78
+ * uint8 major;
79
+ * uint8 minor;
80
+ * } ProtocolVersion;
81
+ *
82
+ * struct {
83
+ * ContentType type;
84
+ * ProtocolVersion version;
85
+ * uint16 length;
86
+ * opaque fragment[TLSPlaintext.length];
87
+ * } TLSPlaintext;
88
+ *
89
+ * type:
90
+ * The higher-level protocol used to process the enclosed fragment.
91
+ *
92
+ * version:
93
+ * The version of the protocol being employed. TLS Version 1.2 uses version
94
+ * {3, 3}. TLS Version 1.0 uses version {3, 1}. Note that a client that
95
+ * supports multiple versions of TLS may not know what version will be
96
+ * employed before it receives the ServerHello.
97
+ *
98
+ * length:
99
+ * The length (in bytes) of the following TLSPlaintext.fragment. The length
100
+ * MUST NOT exceed 2^14 = 16384 bytes.
101
+ *
102
+ * fragment:
103
+ * The application data. This data is transparent and treated as an
104
+ * independent block to be dealt with by the higher-level protocol specified
105
+ * by the type field.
106
+ *
107
+ * Implementations MUST NOT send zero-length fragments of Handshake, Alert, or
108
+ * ChangeCipherSpec content types. Zero-length fragments of Application data
109
+ * MAY be sent as they are potentially useful as a traffic analysis
110
+ * countermeasure.
111
+ *
112
+ * Note: Data of different TLS record layer content types MAY be interleaved.
113
+ * Application data is generally of lower precedence for transmission than
114
+ * other content types. However, records MUST be delivered to the network in
115
+ * the same order as they are protected by the record layer. Recipients MUST
116
+ * receive and process interleaved application layer traffic during handshakes
117
+ * subsequent to the first one on a connection.
118
+ *
119
+ * struct {
120
+ * ContentType type; // same as TLSPlaintext.type
121
+ * ProtocolVersion version;// same as TLSPlaintext.version
122
+ * uint16 length;
123
+ * opaque fragment[TLSCompressed.length];
124
+ * } TLSCompressed;
125
+ *
126
+ * length:
127
+ * The length (in bytes) of the following TLSCompressed.fragment.
128
+ * The length MUST NOT exceed 2^14 + 1024.
129
+ *
130
+ * fragment:
131
+ * The compressed form of TLSPlaintext.fragment.
132
+ *
133
+ * Note: A CompressionMethod.null operation is an identity operation; no fields
134
+ * are altered. In this implementation, since no compression is supported,
135
+ * uncompressed records are always the same as compressed records.
136
+ *
137
+ * Encryption Information:
138
+ *
139
+ * The encryption and MAC functions translate a TLSCompressed structure into a
140
+ * TLSCiphertext. The decryption functions reverse the process. The MAC of the
141
+ * record also includes a sequence number so that missing, extra, or repeated
142
+ * messages are detectable.
143
+ *
144
+ * struct {
145
+ * ContentType type;
146
+ * ProtocolVersion version;
147
+ * uint16 length;
148
+ * select (SecurityParameters.cipher_type) {
149
+ * case stream: GenericStreamCipher;
150
+ * case block: GenericBlockCipher;
151
+ * case aead: GenericAEADCipher;
152
+ * } fragment;
153
+ * } TLSCiphertext;
154
+ *
155
+ * type:
156
+ * The type field is identical to TLSCompressed.type.
157
+ *
158
+ * version:
159
+ * The version field is identical to TLSCompressed.version.
160
+ *
161
+ * length:
162
+ * The length (in bytes) of the following TLSCiphertext.fragment.
163
+ * The length MUST NOT exceed 2^14 + 2048.
164
+ *
165
+ * fragment:
166
+ * The encrypted form of TLSCompressed.fragment, with the MAC.
167
+ *
168
+ * Note: Only CBC Block Ciphers are supported by this implementation.
169
+ *
170
+ * The TLSCompressed.fragment structures are converted to/from block
171
+ * TLSCiphertext.fragment structures.
172
+ *
173
+ * struct {
174
+ * opaque IV[SecurityParameters.record_iv_length];
175
+ * block-ciphered struct {
176
+ * opaque content[TLSCompressed.length];
177
+ * opaque MAC[SecurityParameters.mac_length];
178
+ * uint8 padding[GenericBlockCipher.padding_length];
179
+ * uint8 padding_length;
180
+ * };
181
+ * } GenericBlockCipher;
182
+ *
183
+ * The MAC is generated as described in Section 6.2.3.1.
184
+ *
185
+ * IV:
186
+ * The Initialization Vector (IV) SHOULD be chosen at random, and MUST be
187
+ * unpredictable. Note that in versions of TLS prior to 1.1, there was no
188
+ * IV field, and the last ciphertext block of the previous record (the "CBC
189
+ * residue") was used as the IV. This was changed to prevent the attacks
190
+ * described in [CBCATT]. For block ciphers, the IV length is of length
191
+ * SecurityParameters.record_iv_length, which is equal to the
192
+ * SecurityParameters.block_size.
193
+ *
194
+ * padding:
195
+ * Padding that is added to force the length of the plaintext to be an
196
+ * integral multiple of the block cipher's block length. The padding MAY be
197
+ * any length up to 255 bytes, as long as it results in the
198
+ * TLSCiphertext.length being an integral multiple of the block length.
199
+ * Lengths longer than necessary might be desirable to frustrate attacks on
200
+ * a protocol that are based on analysis of the lengths of exchanged
201
+ * messages. Each uint8 in the padding data vector MUST be filled with the
202
+ * padding length value. The receiver MUST check this padding and MUST use
203
+ * the bad_record_mac alert to indicate padding errors.
204
+ *
205
+ * padding_length:
206
+ * The padding length MUST be such that the total size of the
207
+ * GenericBlockCipher structure is a multiple of the cipher's block length.
208
+ * Legal values range from zero to 255, inclusive. This length specifies the
209
+ * length of the padding field exclusive of the padding_length field itself.
210
+ *
211
+ * The encrypted data length (TLSCiphertext.length) is one more than the sum of
212
+ * SecurityParameters.block_length, TLSCompressed.length,
213
+ * SecurityParameters.mac_length, and padding_length.
214
+ *
215
+ * Example: If the block length is 8 bytes, the content length
216
+ * (TLSCompressed.length) is 61 bytes, and the MAC length is 20 bytes, then the
217
+ * length before padding is 82 bytes (this does not include the IV. Thus, the
218
+ * padding length modulo 8 must be equal to 6 in order to make the total length
219
+ * an even multiple of 8 bytes (the block length). The padding length can be
220
+ * 6, 14, 22, and so on, through 254. If the padding length were the minimum
221
+ * necessary, 6, the padding would be 6 bytes, each containing the value 6.
222
+ * Thus, the last 8 octets of the GenericBlockCipher before block encryption
223
+ * would be xx 06 06 06 06 06 06 06, where xx is the last octet of the MAC.
224
+ *
225
+ * Note: With block ciphers in CBC mode (Cipher Block Chaining), it is critical
226
+ * that the entire plaintext of the record be known before any ciphertext is
227
+ * transmitted. Otherwise, it is possible for the attacker to mount the attack
228
+ * described in [CBCATT].
229
+ *
230
+ * Implementation note: Canvel et al. [CBCTIME] have demonstrated a timing
231
+ * attack on CBC padding based on the time required to compute the MAC. In
232
+ * order to defend against this attack, implementations MUST ensure that
233
+ * record processing time is essentially the same whether or not the padding
234
+ * is correct. In general, the best way to do this is to compute the MAC even
235
+ * if the padding is incorrect, and only then reject the packet. For instance,
236
+ * if the pad appears to be incorrect, the implementation might assume a
237
+ * zero-length pad and then compute the MAC. This leaves a small timing
238
+ * channel, since MAC performance depends, to some extent, on the size of the
239
+ * data fragment, but it is not believed to be large enough to be exploitable,
240
+ * due to the large block size of existing MACs and the small size of the
241
+ * timing signal.
242
+ */
243
+ var tls_1;
244
+ var hasRequiredTls;
245
+ function requireTls() {
246
+ if (hasRequiredTls) return tls_1;
247
+ hasRequiredTls = 1;
248
+ var forge = requireForge();
249
+ requireAsn1();
250
+ requireHmac();
251
+ requireMd5();
252
+ requirePem();
253
+ requirePki();
254
+ requireRandom();
255
+ requireSha1();
256
+ requireUtil();
257
+
258
+ /**
259
+ * Generates pseudo random bytes by mixing the result of two hash functions,
260
+ * MD5 and SHA-1.
261
+ *
262
+ * prf_TLS1(secret, label, seed) =
263
+ * P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed);
264
+ *
265
+ * Each P_hash function functions as follows:
266
+ *
267
+ * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
268
+ * HMAC_hash(secret, A(2) + seed) +
269
+ * HMAC_hash(secret, A(3) + seed) + ...
270
+ * A() is defined as:
271
+ * A(0) = seed
272
+ * A(i) = HMAC_hash(secret, A(i-1))
273
+ *
274
+ * The '+' operator denotes concatenation.
275
+ *
276
+ * As many iterations A(N) as are needed are performed to generate enough
277
+ * pseudo random byte output. If an iteration creates more data than is
278
+ * necessary, then it is truncated.
279
+ *
280
+ * Therefore:
281
+ * A(1) = HMAC_hash(secret, A(0))
282
+ * = HMAC_hash(secret, seed)
283
+ * A(2) = HMAC_hash(secret, A(1))
284
+ * = HMAC_hash(secret, HMAC_hash(secret, seed))
285
+ *
286
+ * Therefore:
287
+ * P_hash(secret, seed) =
288
+ * HMAC_hash(secret, HMAC_hash(secret, A(0)) + seed) +
289
+ * HMAC_hash(secret, HMAC_hash(secret, A(1)) + seed) +
290
+ * ...
291
+ *
292
+ * Therefore:
293
+ * P_hash(secret, seed) =
294
+ * HMAC_hash(secret, HMAC_hash(secret, seed) + seed) +
295
+ * HMAC_hash(secret, HMAC_hash(secret, HMAC_hash(secret, seed)) + seed) +
296
+ * ...
297
+ *
298
+ * @param secret the secret to use.
299
+ * @param label the label to use.
300
+ * @param seed the seed value to use.
301
+ * @param length the number of bytes to generate.
302
+ *
303
+ * @return the pseudo random bytes in a byte buffer.
304
+ */
305
+ var prf_TLS1 = function (secret, label, seed, length) {
306
+ var rval = forge.util.createBuffer();
307
+
308
+ /* For TLS 1.0, the secret is split in half, into two secrets of equal
309
+ length. If the secret has an odd length then the last byte of the first
310
+ half will be the same as the first byte of the second. The length of the
311
+ two secrets is half of the secret rounded up. */
312
+ var idx = secret.length >> 1;
313
+ var slen = idx + (secret.length & 1);
314
+ var s1 = secret.substr(0, slen);
315
+ var s2 = secret.substr(idx, slen);
316
+ var ai = forge.util.createBuffer();
317
+ var hmac = forge.hmac.create();
318
+ seed = label + seed;
319
+
320
+ // determine the number of iterations that must be performed to generate
321
+ // enough output bytes, md5 creates 16 byte hashes, sha1 creates 20
322
+ var md5itr = Math.ceil(length / 16);
323
+ var sha1itr = Math.ceil(length / 20);
324
+
325
+ // do md5 iterations
326
+ hmac.start('MD5', s1);
327
+ var md5bytes = forge.util.createBuffer();
328
+ ai.putBytes(seed);
329
+ for (var i = 0; i < md5itr; ++i) {
330
+ // HMAC_hash(secret, A(i-1))
331
+ hmac.start(null, null);
332
+ hmac.update(ai.getBytes());
333
+ ai.putBuffer(hmac.digest());
334
+
335
+ // HMAC_hash(secret, A(i) + seed)
336
+ hmac.start(null, null);
337
+ hmac.update(ai.bytes() + seed);
338
+ md5bytes.putBuffer(hmac.digest());
339
+ }
340
+
341
+ // do sha1 iterations
342
+ hmac.start('SHA1', s2);
343
+ var sha1bytes = forge.util.createBuffer();
344
+ ai.clear();
345
+ ai.putBytes(seed);
346
+ for (var i = 0; i < sha1itr; ++i) {
347
+ // HMAC_hash(secret, A(i-1))
348
+ hmac.start(null, null);
349
+ hmac.update(ai.getBytes());
350
+ ai.putBuffer(hmac.digest());
351
+
352
+ // HMAC_hash(secret, A(i) + seed)
353
+ hmac.start(null, null);
354
+ hmac.update(ai.bytes() + seed);
355
+ sha1bytes.putBuffer(hmac.digest());
356
+ }
357
+
358
+ // XOR the md5 bytes with the sha1 bytes
359
+ rval.putBytes(forge.util.xorBytes(md5bytes.getBytes(), sha1bytes.getBytes(), length));
360
+ return rval;
361
+ };
362
+
363
+ /**
364
+ * Gets a MAC for a record using the SHA-1 hash algorithm.
365
+ *
366
+ * @param key the mac key.
367
+ * @param state the sequence number (array of two 32-bit integers).
368
+ * @param record the record.
369
+ *
370
+ * @return the sha-1 hash (20 bytes) for the given record.
371
+ */
372
+ var hmac_sha1 = function (key, seqNum, record) {
373
+ /* MAC is computed like so:
374
+ HMAC_hash(
375
+ key, seqNum +
376
+ TLSCompressed.type +
377
+ TLSCompressed.version +
378
+ TLSCompressed.length +
379
+ TLSCompressed.fragment)
380
+ */
381
+ var hmac = forge.hmac.create();
382
+ hmac.start('SHA1', key);
383
+ var b = forge.util.createBuffer();
384
+ b.putInt32(seqNum[0]);
385
+ b.putInt32(seqNum[1]);
386
+ b.putByte(record.type);
387
+ b.putByte(record.version.major);
388
+ b.putByte(record.version.minor);
389
+ b.putInt16(record.length);
390
+ b.putBytes(record.fragment.bytes());
391
+ hmac.update(b.getBytes());
392
+ return hmac.digest().getBytes();
393
+ };
394
+
395
+ /**
396
+ * Compresses the TLSPlaintext record into a TLSCompressed record using the
397
+ * deflate algorithm.
398
+ *
399
+ * @param c the TLS connection.
400
+ * @param record the TLSPlaintext record to compress.
401
+ * @param s the ConnectionState to use.
402
+ *
403
+ * @return true on success, false on failure.
404
+ */
405
+ var deflate = function (c, record, s) {
406
+ var rval = false;
407
+ try {
408
+ var bytes = c.deflate(record.fragment.getBytes());
409
+ record.fragment = forge.util.createBuffer(bytes);
410
+ record.length = bytes.length;
411
+ rval = true;
412
+ } catch (ex) {
413
+ // deflate error, fail out
414
+ }
415
+ return rval;
416
+ };
417
+
418
+ /**
419
+ * Decompresses the TLSCompressed record into a TLSPlaintext record using the
420
+ * deflate algorithm.
421
+ *
422
+ * @param c the TLS connection.
423
+ * @param record the TLSCompressed record to decompress.
424
+ * @param s the ConnectionState to use.
425
+ *
426
+ * @return true on success, false on failure.
427
+ */
428
+ var inflate = function (c, record, s) {
429
+ var rval = false;
430
+ try {
431
+ var bytes = c.inflate(record.fragment.getBytes());
432
+ record.fragment = forge.util.createBuffer(bytes);
433
+ record.length = bytes.length;
434
+ rval = true;
435
+ } catch (ex) {
436
+ // inflate error, fail out
437
+ }
438
+ return rval;
439
+ };
440
+
441
+ /**
442
+ * Reads a TLS variable-length vector from a byte buffer.
443
+ *
444
+ * Variable-length vectors are defined by specifying a subrange of legal
445
+ * lengths, inclusively, using the notation <floor..ceiling>. When these are
446
+ * encoded, the actual length precedes the vector's contents in the byte
447
+ * stream. The length will be in the form of a number consuming as many bytes
448
+ * as required to hold the vector's specified maximum (ceiling) length. A
449
+ * variable-length vector with an actual length field of zero is referred to
450
+ * as an empty vector.
451
+ *
452
+ * @param b the byte buffer.
453
+ * @param lenBytes the number of bytes required to store the length.
454
+ *
455
+ * @return the resulting byte buffer.
456
+ */
457
+ var readVector = function (b, lenBytes) {
458
+ var len = 0;
459
+ switch (lenBytes) {
460
+ case 1:
461
+ len = b.getByte();
462
+ break;
463
+ case 2:
464
+ len = b.getInt16();
465
+ break;
466
+ case 3:
467
+ len = b.getInt24();
468
+ break;
469
+ case 4:
470
+ len = b.getInt32();
471
+ break;
472
+ }
473
+
474
+ // read vector bytes into a new buffer
475
+ return forge.util.createBuffer(b.getBytes(len));
476
+ };
477
+
478
+ /**
479
+ * Writes a TLS variable-length vector to a byte buffer.
480
+ *
481
+ * @param b the byte buffer.
482
+ * @param lenBytes the number of bytes required to store the length.
483
+ * @param v the byte buffer vector.
484
+ */
485
+ var writeVector = function (b, lenBytes, v) {
486
+ // encode length at the start of the vector, where the number of bytes for
487
+ // the length is the maximum number of bytes it would take to encode the
488
+ // vector's ceiling
489
+ b.putInt(v.length(), lenBytes << 3);
490
+ b.putBuffer(v);
491
+ };
492
+
493
+ /**
494
+ * The tls implementation.
495
+ */
496
+ var tls = {};
497
+
498
+ /**
499
+ * Version: TLS 1.2 = 3.3, TLS 1.1 = 3.2, TLS 1.0 = 3.1. Both TLS 1.1 and
500
+ * TLS 1.2 were still too new (ie: openSSL didn't implement them) at the time
501
+ * of this implementation so TLS 1.0 was implemented instead.
502
+ */
503
+ tls.Versions = {
504
+ TLS_1_0: {
505
+ major: 3,
506
+ minor: 1
507
+ },
508
+ TLS_1_1: {
509
+ major: 3,
510
+ minor: 2
511
+ },
512
+ TLS_1_2: {
513
+ major: 3,
514
+ minor: 3
515
+ }
516
+ };
517
+ tls.SupportedVersions = [tls.Versions.TLS_1_1, tls.Versions.TLS_1_0];
518
+ tls.Version = tls.SupportedVersions[0];
519
+
520
+ /**
521
+ * Maximum fragment size. True maximum is 16384, but we fragment before that
522
+ * to allow for unusual small increases during compression.
523
+ */
524
+ tls.MaxFragment = 16384 - 1024;
525
+
526
+ /**
527
+ * Whether this entity is considered the "client" or "server".
528
+ * enum { server, client } ConnectionEnd;
529
+ */
530
+ tls.ConnectionEnd = {
531
+ server: 0,
532
+ client: 1
533
+ };
534
+
535
+ /**
536
+ * Pseudo-random function algorithm used to generate keys from the master
537
+ * secret.
538
+ * enum { tls_prf_sha256 } PRFAlgorithm;
539
+ */
540
+ tls.PRFAlgorithm = {
541
+ tls_prf_sha256: 0
542
+ };
543
+
544
+ /**
545
+ * Bulk encryption algorithms.
546
+ * enum { null, rc4, des3, aes } BulkCipherAlgorithm;
547
+ */
548
+ tls.BulkCipherAlgorithm = {
549
+ none: null,
550
+ rc4: 0,
551
+ des3: 1,
552
+ aes: 2
553
+ };
554
+
555
+ /**
556
+ * Cipher types.
557
+ * enum { stream, block, aead } CipherType;
558
+ */
559
+ tls.CipherType = {
560
+ stream: 0,
561
+ block: 1,
562
+ aead: 2
563
+ };
564
+
565
+ /**
566
+ * MAC (Message Authentication Code) algorithms.
567
+ * enum { null, hmac_md5, hmac_sha1, hmac_sha256,
568
+ * hmac_sha384, hmac_sha512} MACAlgorithm;
569
+ */
570
+ tls.MACAlgorithm = {
571
+ none: null,
572
+ hmac_md5: 0,
573
+ hmac_sha1: 1,
574
+ hmac_sha256: 2,
575
+ hmac_sha384: 3,
576
+ hmac_sha512: 4
577
+ };
578
+
579
+ /**
580
+ * Compression algorithms.
581
+ * enum { null(0), deflate(1), (255) } CompressionMethod;
582
+ */
583
+ tls.CompressionMethod = {
584
+ none: 0,
585
+ deflate: 1
586
+ };
587
+
588
+ /**
589
+ * TLS record content types.
590
+ * enum {
591
+ * change_cipher_spec(20), alert(21), handshake(22),
592
+ * application_data(23), (255)
593
+ * } ContentType;
594
+ */
595
+ tls.ContentType = {
596
+ change_cipher_spec: 20,
597
+ alert: 21,
598
+ handshake: 22,
599
+ application_data: 23,
600
+ heartbeat: 24
601
+ };
602
+
603
+ /**
604
+ * TLS handshake types.
605
+ * enum {
606
+ * hello_request(0), client_hello(1), server_hello(2),
607
+ * certificate(11), server_key_exchange (12),
608
+ * certificate_request(13), server_hello_done(14),
609
+ * certificate_verify(15), client_key_exchange(16),
610
+ * finished(20), (255)
611
+ * } HandshakeType;
612
+ */
613
+ tls.HandshakeType = {
614
+ hello_request: 0,
615
+ client_hello: 1,
616
+ server_hello: 2,
617
+ certificate: 11,
618
+ server_key_exchange: 12,
619
+ certificate_request: 13,
620
+ server_hello_done: 14,
621
+ certificate_verify: 15,
622
+ client_key_exchange: 16,
623
+ finished: 20
624
+ };
625
+
626
+ /**
627
+ * TLS Alert Protocol.
628
+ *
629
+ * enum { warning(1), fatal(2), (255) } AlertLevel;
630
+ *
631
+ * enum {
632
+ * close_notify(0),
633
+ * unexpected_message(10),
634
+ * bad_record_mac(20),
635
+ * decryption_failed(21),
636
+ * record_overflow(22),
637
+ * decompression_failure(30),
638
+ * handshake_failure(40),
639
+ * bad_certificate(42),
640
+ * unsupported_certificate(43),
641
+ * certificate_revoked(44),
642
+ * certificate_expired(45),
643
+ * certificate_unknown(46),
644
+ * illegal_parameter(47),
645
+ * unknown_ca(48),
646
+ * access_denied(49),
647
+ * decode_error(50),
648
+ * decrypt_error(51),
649
+ * export_restriction(60),
650
+ * protocol_version(70),
651
+ * insufficient_security(71),
652
+ * internal_error(80),
653
+ * user_canceled(90),
654
+ * no_renegotiation(100),
655
+ * (255)
656
+ * } AlertDescription;
657
+ *
658
+ * struct {
659
+ * AlertLevel level;
660
+ * AlertDescription description;
661
+ * } Alert;
662
+ */
663
+ tls.Alert = {};
664
+ tls.Alert.Level = {
665
+ warning: 1,
666
+ fatal: 2
667
+ };
668
+ tls.Alert.Description = {
669
+ close_notify: 0,
670
+ unexpected_message: 10,
671
+ bad_record_mac: 20,
672
+ decryption_failed: 21,
673
+ record_overflow: 22,
674
+ decompression_failure: 30,
675
+ handshake_failure: 40,
676
+ bad_certificate: 42,
677
+ unsupported_certificate: 43,
678
+ certificate_revoked: 44,
679
+ certificate_expired: 45,
680
+ certificate_unknown: 46,
681
+ illegal_parameter: 47,
682
+ unknown_ca: 48,
683
+ access_denied: 49,
684
+ decode_error: 50,
685
+ decrypt_error: 51,
686
+ export_restriction: 60,
687
+ protocol_version: 70,
688
+ insufficient_security: 71,
689
+ internal_error: 80,
690
+ user_canceled: 90,
691
+ no_renegotiation: 100
692
+ };
693
+
694
+ /**
695
+ * TLS Heartbeat Message types.
696
+ * enum {
697
+ * heartbeat_request(1),
698
+ * heartbeat_response(2),
699
+ * (255)
700
+ * } HeartbeatMessageType;
701
+ */
702
+ tls.HeartbeatMessageType = {
703
+ heartbeat_request: 1,
704
+ heartbeat_response: 2
705
+ };
706
+
707
+ /**
708
+ * Supported cipher suites.
709
+ */
710
+ tls.CipherSuites = {};
711
+
712
+ /**
713
+ * Gets a supported cipher suite from its 2 byte ID.
714
+ *
715
+ * @param twoBytes two bytes in a string.
716
+ *
717
+ * @return the matching supported cipher suite or null.
718
+ */
719
+ tls.getCipherSuite = function (twoBytes) {
720
+ var rval = null;
721
+ for (var key in tls.CipherSuites) {
722
+ var cs = tls.CipherSuites[key];
723
+ if (cs.id[0] === twoBytes.charCodeAt(0) && cs.id[1] === twoBytes.charCodeAt(1)) {
724
+ rval = cs;
725
+ break;
726
+ }
727
+ }
728
+ return rval;
729
+ };
730
+
731
+ /**
732
+ * Called when an unexpected record is encountered.
733
+ *
734
+ * @param c the connection.
735
+ * @param record the record.
736
+ */
737
+ tls.handleUnexpected = function (c, record) {
738
+ // if connection is client and closed, ignore unexpected messages
739
+ var ignore = !c.open && c.entity === tls.ConnectionEnd.client;
740
+ if (!ignore) {
741
+ c.error(c, {
742
+ message: 'Unexpected message. Received TLS record out of order.',
743
+ send: true,
744
+ alert: {
745
+ level: tls.Alert.Level.fatal,
746
+ description: tls.Alert.Description.unexpected_message
747
+ }
748
+ });
749
+ }
750
+ };
751
+
752
+ /**
753
+ * Called when a client receives a HelloRequest record.
754
+ *
755
+ * @param c the connection.
756
+ * @param record the record.
757
+ * @param length the length of the handshake message.
758
+ */
759
+ tls.handleHelloRequest = function (c, record, length) {
760
+ // ignore renegotiation requests from the server during a handshake, but
761
+ // if handshaking, send a warning alert that renegotation is denied
762
+ if (!c.handshaking && c.handshakes > 0) {
763
+ // send alert warning
764
+ tls.queue(c, tls.createAlert(c, {
765
+ level: tls.Alert.Level.warning,
766
+ description: tls.Alert.Description.no_renegotiation
767
+ }));
768
+ tls.flush(c);
769
+ }
770
+
771
+ // continue
772
+ c.process();
773
+ };
774
+
775
+ /**
776
+ * Parses a hello message from a ClientHello or ServerHello record.
777
+ *
778
+ * @param record the record to parse.
779
+ *
780
+ * @return the parsed message.
781
+ */
782
+ tls.parseHelloMessage = function (c, record, length) {
783
+ var msg = null;
784
+ var client = c.entity === tls.ConnectionEnd.client;
785
+
786
+ // minimum of 38 bytes in message
787
+ if (length < 38) {
788
+ c.error(c, {
789
+ message: client ? 'Invalid ServerHello message. Message too short.' : 'Invalid ClientHello message. Message too short.',
790
+ send: true,
791
+ alert: {
792
+ level: tls.Alert.Level.fatal,
793
+ description: tls.Alert.Description.illegal_parameter
794
+ }
795
+ });
796
+ } else {
797
+ // use 'remaining' to calculate # of remaining bytes in the message
798
+ var b = record.fragment;
799
+ var remaining = b.length();
800
+ msg = {
801
+ version: {
802
+ major: b.getByte(),
803
+ minor: b.getByte()
804
+ },
805
+ random: forge.util.createBuffer(b.getBytes(32)),
806
+ session_id: readVector(b, 1),
807
+ extensions: []
808
+ };
809
+ if (client) {
810
+ msg.cipher_suite = b.getBytes(2);
811
+ msg.compression_method = b.getByte();
812
+ } else {
813
+ msg.cipher_suites = readVector(b, 2);
814
+ msg.compression_methods = readVector(b, 1);
815
+ }
816
+
817
+ // read extensions if there are any bytes left in the message
818
+ remaining = length - (remaining - b.length());
819
+ if (remaining > 0) {
820
+ // parse extensions
821
+ var exts = readVector(b, 2);
822
+ while (exts.length() > 0) {
823
+ msg.extensions.push({
824
+ type: [exts.getByte(), exts.getByte()],
825
+ data: readVector(exts, 2)
826
+ });
827
+ }
828
+
829
+ // TODO: make extension support modular
830
+ if (!client) {
831
+ for (var i = 0; i < msg.extensions.length; ++i) {
832
+ var ext = msg.extensions[i];
833
+
834
+ // support SNI extension
835
+ if (ext.type[0] === 0x00 && ext.type[1] === 0x00) {
836
+ // get server name list
837
+ var snl = readVector(ext.data, 2);
838
+ while (snl.length() > 0) {
839
+ // read server name type
840
+ var snType = snl.getByte();
841
+
842
+ // only HostName type (0x00) is known, break out if
843
+ // another type is detected
844
+ if (snType !== 0x00) {
845
+ break;
846
+ }
847
+
848
+ // add host name to server name list
849
+ c.session.extensions.server_name.serverNameList.push(readVector(snl, 2).getBytes());
850
+ }
851
+ }
852
+ }
853
+ }
854
+ }
855
+
856
+ // version already set, do not allow version change
857
+ if (c.session.version) {
858
+ if (msg.version.major !== c.session.version.major || msg.version.minor !== c.session.version.minor) {
859
+ return c.error(c, {
860
+ message: 'TLS version change is disallowed during renegotiation.',
861
+ send: true,
862
+ alert: {
863
+ level: tls.Alert.Level.fatal,
864
+ description: tls.Alert.Description.protocol_version
865
+ }
866
+ });
867
+ }
868
+ }
869
+
870
+ // get the chosen (ServerHello) cipher suite
871
+ if (client) {
872
+ // FIXME: should be checking configured acceptable cipher suites
873
+ c.session.cipherSuite = tls.getCipherSuite(msg.cipher_suite);
874
+ } else {
875
+ // get a supported preferred (ClientHello) cipher suite
876
+ // choose the first supported cipher suite
877
+ var tmp = forge.util.createBuffer(msg.cipher_suites.bytes());
878
+ while (tmp.length() > 0) {
879
+ // FIXME: should be checking configured acceptable suites
880
+ // cipher suites take up 2 bytes
881
+ c.session.cipherSuite = tls.getCipherSuite(tmp.getBytes(2));
882
+ if (c.session.cipherSuite !== null) {
883
+ break;
884
+ }
885
+ }
886
+ }
887
+
888
+ // cipher suite not supported
889
+ if (c.session.cipherSuite === null) {
890
+ return c.error(c, {
891
+ message: 'No cipher suites in common.',
892
+ send: true,
893
+ alert: {
894
+ level: tls.Alert.Level.fatal,
895
+ description: tls.Alert.Description.handshake_failure
896
+ },
897
+ cipherSuite: forge.util.bytesToHex(msg.cipher_suite)
898
+ });
899
+ }
900
+
901
+ // TODO: handle compression methods
902
+ if (client) {
903
+ c.session.compressionMethod = msg.compression_method;
904
+ } else {
905
+ // no compression
906
+ c.session.compressionMethod = tls.CompressionMethod.none;
907
+ }
908
+ }
909
+ return msg;
910
+ };
911
+
912
+ /**
913
+ * Creates security parameters for the given connection based on the given
914
+ * hello message.
915
+ *
916
+ * @param c the TLS connection.
917
+ * @param msg the hello message.
918
+ */
919
+ tls.createSecurityParameters = function (c, msg) {
920
+ /* Note: security params are from TLS 1.2, some values like prf_algorithm
921
+ are ignored for TLS 1.0/1.1 and the builtin as specified in the spec is
922
+ used. */
923
+
924
+ // TODO: handle other options from server when more supported
925
+
926
+ // get client and server randoms
927
+ var client = c.entity === tls.ConnectionEnd.client;
928
+ var msgRandom = msg.random.bytes();
929
+ var cRandom = client ? c.session.sp.client_random : msgRandom;
930
+ var sRandom = client ? msgRandom : tls.createRandom().getBytes();
931
+
932
+ // create new security parameters
933
+ c.session.sp = {
934
+ entity: c.entity,
935
+ prf_algorithm: tls.PRFAlgorithm.tls_prf_sha256,
936
+ bulk_cipher_algorithm: null,
937
+ cipher_type: null,
938
+ enc_key_length: null,
939
+ block_length: null,
940
+ fixed_iv_length: null,
941
+ record_iv_length: null,
942
+ mac_algorithm: null,
943
+ mac_length: null,
944
+ mac_key_length: null,
945
+ compression_algorithm: c.session.compressionMethod,
946
+ pre_master_secret: null,
947
+ master_secret: null,
948
+ client_random: cRandom,
949
+ server_random: sRandom
950
+ };
951
+ };
952
+
953
+ /**
954
+ * Called when a client receives a ServerHello record.
955
+ *
956
+ * When a ServerHello message will be sent:
957
+ * The server will send this message in response to a client hello message
958
+ * when it was able to find an acceptable set of algorithms. If it cannot
959
+ * find such a match, it will respond with a handshake failure alert.
960
+ *
961
+ * uint24 length;
962
+ * struct {
963
+ * ProtocolVersion server_version;
964
+ * Random random;
965
+ * SessionID session_id;
966
+ * CipherSuite cipher_suite;
967
+ * CompressionMethod compression_method;
968
+ * select(extensions_present) {
969
+ * case false:
970
+ * struct {};
971
+ * case true:
972
+ * Extension extensions<0..2^16-1>;
973
+ * };
974
+ * } ServerHello;
975
+ *
976
+ * @param c the connection.
977
+ * @param record the record.
978
+ * @param length the length of the handshake message.
979
+ */
980
+ tls.handleServerHello = function (c, record, length) {
981
+ var msg = tls.parseHelloMessage(c, record, length);
982
+ if (c.fail) {
983
+ return;
984
+ }
985
+
986
+ // ensure server version is compatible
987
+ if (msg.version.minor <= c.version.minor) {
988
+ c.version.minor = msg.version.minor;
989
+ } else {
990
+ return c.error(c, {
991
+ message: 'Incompatible TLS version.',
992
+ send: true,
993
+ alert: {
994
+ level: tls.Alert.Level.fatal,
995
+ description: tls.Alert.Description.protocol_version
996
+ }
997
+ });
998
+ }
999
+
1000
+ // indicate session version has been set
1001
+ c.session.version = c.version;
1002
+
1003
+ // get the session ID from the message
1004
+ var sessionId = msg.session_id.bytes();
1005
+
1006
+ // if the session ID is not blank and matches the cached one, resume
1007
+ // the session
1008
+ if (sessionId.length > 0 && sessionId === c.session.id) {
1009
+ // resuming session, expect a ChangeCipherSpec next
1010
+ c.expect = SCC;
1011
+ c.session.resuming = true;
1012
+
1013
+ // get new server random
1014
+ c.session.sp.server_random = msg.random.bytes();
1015
+ } else {
1016
+ // not resuming, expect a server Certificate message next
1017
+ c.expect = SCE;
1018
+ c.session.resuming = false;
1019
+
1020
+ // create new security parameters
1021
+ tls.createSecurityParameters(c, msg);
1022
+ }
1023
+
1024
+ // set new session ID
1025
+ c.session.id = sessionId;
1026
+
1027
+ // continue
1028
+ c.process();
1029
+ };
1030
+
1031
+ /**
1032
+ * Called when a server receives a ClientHello record.
1033
+ *
1034
+ * When a ClientHello message will be sent:
1035
+ * When a client first connects to a server it is required to send the
1036
+ * client hello as its first message. The client can also send a client
1037
+ * hello in response to a hello request or on its own initiative in order
1038
+ * to renegotiate the security parameters in an existing connection.
1039
+ *
1040
+ * @param c the connection.
1041
+ * @param record the record.
1042
+ * @param length the length of the handshake message.
1043
+ */
1044
+ tls.handleClientHello = function (c, record, length) {
1045
+ var msg = tls.parseHelloMessage(c, record, length);
1046
+ if (c.fail) {
1047
+ return;
1048
+ }
1049
+
1050
+ // get the session ID from the message
1051
+ var sessionId = msg.session_id.bytes();
1052
+
1053
+ // see if the given session ID is in the cache
1054
+ var session = null;
1055
+ if (c.sessionCache) {
1056
+ session = c.sessionCache.getSession(sessionId);
1057
+ if (session === null) {
1058
+ // session ID not found
1059
+ sessionId = '';
1060
+ } else if (session.version.major !== msg.version.major || session.version.minor > msg.version.minor) {
1061
+ // if session version is incompatible with client version, do not resume
1062
+ session = null;
1063
+ sessionId = '';
1064
+ }
1065
+ }
1066
+
1067
+ // no session found to resume, generate a new session ID
1068
+ if (sessionId.length === 0) {
1069
+ sessionId = forge.random.getBytes(32);
1070
+ }
1071
+
1072
+ // update session
1073
+ c.session.id = sessionId;
1074
+ c.session.clientHelloVersion = msg.version;
1075
+ c.session.sp = {};
1076
+ if (session) {
1077
+ // use version and security parameters from resumed session
1078
+ c.version = c.session.version = session.version;
1079
+ c.session.sp = session.sp;
1080
+ } else {
1081
+ // use highest compatible minor version
1082
+ var version;
1083
+ for (var i = 1; i < tls.SupportedVersions.length; ++i) {
1084
+ version = tls.SupportedVersions[i];
1085
+ if (version.minor <= msg.version.minor) {
1086
+ break;
1087
+ }
1088
+ }
1089
+ c.version = {
1090
+ major: version.major,
1091
+ minor: version.minor
1092
+ };
1093
+ c.session.version = c.version;
1094
+ }
1095
+
1096
+ // if a session is set, resume it
1097
+ if (session !== null) {
1098
+ // resuming session, expect a ChangeCipherSpec next
1099
+ c.expect = CCC;
1100
+ c.session.resuming = true;
1101
+
1102
+ // get new client random
1103
+ c.session.sp.client_random = msg.random.bytes();
1104
+ } else {
1105
+ // not resuming, expect a Certificate or ClientKeyExchange
1106
+ c.expect = c.verifyClient !== false ? CCE : CKE;
1107
+ c.session.resuming = false;
1108
+
1109
+ // create new security parameters
1110
+ tls.createSecurityParameters(c, msg);
1111
+ }
1112
+
1113
+ // connection now open
1114
+ c.open = true;
1115
+
1116
+ // queue server hello
1117
+ tls.queue(c, tls.createRecord(c, {
1118
+ type: tls.ContentType.handshake,
1119
+ data: tls.createServerHello(c)
1120
+ }));
1121
+ if (c.session.resuming) {
1122
+ // queue change cipher spec message
1123
+ tls.queue(c, tls.createRecord(c, {
1124
+ type: tls.ContentType.change_cipher_spec,
1125
+ data: tls.createChangeCipherSpec()
1126
+ }));
1127
+
1128
+ // create pending state
1129
+ c.state.pending = tls.createConnectionState(c);
1130
+
1131
+ // change current write state to pending write state
1132
+ c.state.current.write = c.state.pending.write;
1133
+
1134
+ // queue finished
1135
+ tls.queue(c, tls.createRecord(c, {
1136
+ type: tls.ContentType.handshake,
1137
+ data: tls.createFinished(c)
1138
+ }));
1139
+ } else {
1140
+ // queue server certificate
1141
+ tls.queue(c, tls.createRecord(c, {
1142
+ type: tls.ContentType.handshake,
1143
+ data: tls.createCertificate(c)
1144
+ }));
1145
+ if (!c.fail) {
1146
+ // queue server key exchange
1147
+ tls.queue(c, tls.createRecord(c, {
1148
+ type: tls.ContentType.handshake,
1149
+ data: tls.createServerKeyExchange(c)
1150
+ }));
1151
+
1152
+ // request client certificate if set
1153
+ if (c.verifyClient !== false) {
1154
+ // queue certificate request
1155
+ tls.queue(c, tls.createRecord(c, {
1156
+ type: tls.ContentType.handshake,
1157
+ data: tls.createCertificateRequest(c)
1158
+ }));
1159
+ }
1160
+
1161
+ // queue server hello done
1162
+ tls.queue(c, tls.createRecord(c, {
1163
+ type: tls.ContentType.handshake,
1164
+ data: tls.createServerHelloDone(c)
1165
+ }));
1166
+ }
1167
+ }
1168
+
1169
+ // send records
1170
+ tls.flush(c);
1171
+
1172
+ // continue
1173
+ c.process();
1174
+ };
1175
+
1176
+ /**
1177
+ * Called when a client receives a Certificate record.
1178
+ *
1179
+ * When this message will be sent:
1180
+ * The server must send a certificate whenever the agreed-upon key exchange
1181
+ * method is not an anonymous one. This message will always immediately
1182
+ * follow the server hello message.
1183
+ *
1184
+ * Meaning of this message:
1185
+ * The certificate type must be appropriate for the selected cipher suite's
1186
+ * key exchange algorithm, and is generally an X.509v3 certificate. It must
1187
+ * contain a key which matches the key exchange method, as follows. Unless
1188
+ * otherwise specified, the signing algorithm for the certificate must be
1189
+ * the same as the algorithm for the certificate key. Unless otherwise
1190
+ * specified, the public key may be of any length.
1191
+ *
1192
+ * opaque ASN.1Cert<1..2^24-1>;
1193
+ * struct {
1194
+ * ASN.1Cert certificate_list<1..2^24-1>;
1195
+ * } Certificate;
1196
+ *
1197
+ * @param c the connection.
1198
+ * @param record the record.
1199
+ * @param length the length of the handshake message.
1200
+ */
1201
+ tls.handleCertificate = function (c, record, length) {
1202
+ // minimum of 3 bytes in message
1203
+ if (length < 3) {
1204
+ return c.error(c, {
1205
+ message: 'Invalid Certificate message. Message too short.',
1206
+ send: true,
1207
+ alert: {
1208
+ level: tls.Alert.Level.fatal,
1209
+ description: tls.Alert.Description.illegal_parameter
1210
+ }
1211
+ });
1212
+ }
1213
+ var b = record.fragment;
1214
+ var msg = {
1215
+ certificate_list: readVector(b, 3)
1216
+ };
1217
+
1218
+ /* The sender's certificate will be first in the list (chain), each
1219
+ subsequent one that follows will certify the previous one, but root
1220
+ certificates (self-signed) that specify the certificate authority may
1221
+ be omitted under the assumption that clients must already possess it. */
1222
+ var cert, asn1;
1223
+ var certs = [];
1224
+ try {
1225
+ while (msg.certificate_list.length() > 0) {
1226
+ // each entry in msg.certificate_list is a vector with 3 len bytes
1227
+ cert = readVector(msg.certificate_list, 3);
1228
+ asn1 = forge.asn1.fromDer(cert);
1229
+ cert = forge.pki.certificateFromAsn1(asn1, true);
1230
+ certs.push(cert);
1231
+ }
1232
+ } catch (ex) {
1233
+ return c.error(c, {
1234
+ message: 'Could not parse certificate list.',
1235
+ cause: ex,
1236
+ send: true,
1237
+ alert: {
1238
+ level: tls.Alert.Level.fatal,
1239
+ description: tls.Alert.Description.bad_certificate
1240
+ }
1241
+ });
1242
+ }
1243
+
1244
+ // ensure at least 1 certificate was provided if in client-mode
1245
+ // or if verifyClient was set to true to require a certificate
1246
+ // (as opposed to 'optional')
1247
+ var client = c.entity === tls.ConnectionEnd.client;
1248
+ if ((client || c.verifyClient === true) && certs.length === 0) {
1249
+ // error, no certificate
1250
+ c.error(c, {
1251
+ message: client ? 'No server certificate provided.' : 'No client certificate provided.',
1252
+ send: true,
1253
+ alert: {
1254
+ level: tls.Alert.Level.fatal,
1255
+ description: tls.Alert.Description.illegal_parameter
1256
+ }
1257
+ });
1258
+ } else if (certs.length === 0) {
1259
+ // no certs to verify
1260
+ // expect a ServerKeyExchange or ClientKeyExchange message next
1261
+ c.expect = client ? SKE : CKE;
1262
+ } else {
1263
+ // save certificate in session
1264
+ if (client) {
1265
+ c.session.serverCertificate = certs[0];
1266
+ } else {
1267
+ c.session.clientCertificate = certs[0];
1268
+ }
1269
+ if (tls.verifyCertificateChain(c, certs)) {
1270
+ // expect a ServerKeyExchange or ClientKeyExchange message next
1271
+ c.expect = client ? SKE : CKE;
1272
+ }
1273
+ }
1274
+
1275
+ // continue
1276
+ c.process();
1277
+ };
1278
+
1279
+ /**
1280
+ * Called when a client receives a ServerKeyExchange record.
1281
+ *
1282
+ * When this message will be sent:
1283
+ * This message will be sent immediately after the server certificate
1284
+ * message (or the server hello message, if this is an anonymous
1285
+ * negotiation).
1286
+ *
1287
+ * The server key exchange message is sent by the server only when the
1288
+ * server certificate message (if sent) does not contain enough data to
1289
+ * allow the client to exchange a premaster secret.
1290
+ *
1291
+ * Meaning of this message:
1292
+ * This message conveys cryptographic information to allow the client to
1293
+ * communicate the premaster secret: either an RSA public key to encrypt
1294
+ * the premaster secret with, or a Diffie-Hellman public key with which the
1295
+ * client can complete a key exchange (with the result being the premaster
1296
+ * secret.)
1297
+ *
1298
+ * enum {
1299
+ * dhe_dss, dhe_rsa, dh_anon, rsa, dh_dss, dh_rsa
1300
+ * } KeyExchangeAlgorithm;
1301
+ *
1302
+ * struct {
1303
+ * opaque dh_p<1..2^16-1>;
1304
+ * opaque dh_g<1..2^16-1>;
1305
+ * opaque dh_Ys<1..2^16-1>;
1306
+ * } ServerDHParams;
1307
+ *
1308
+ * struct {
1309
+ * select(KeyExchangeAlgorithm) {
1310
+ * case dh_anon:
1311
+ * ServerDHParams params;
1312
+ * case dhe_dss:
1313
+ * case dhe_rsa:
1314
+ * ServerDHParams params;
1315
+ * digitally-signed struct {
1316
+ * opaque client_random[32];
1317
+ * opaque server_random[32];
1318
+ * ServerDHParams params;
1319
+ * } signed_params;
1320
+ * case rsa:
1321
+ * case dh_dss:
1322
+ * case dh_rsa:
1323
+ * struct {};
1324
+ * };
1325
+ * } ServerKeyExchange;
1326
+ *
1327
+ * @param c the connection.
1328
+ * @param record the record.
1329
+ * @param length the length of the handshake message.
1330
+ */
1331
+ tls.handleServerKeyExchange = function (c, record, length) {
1332
+ // this implementation only supports RSA, no Diffie-Hellman support
1333
+ // so any length > 0 is invalid
1334
+ if (length > 0) {
1335
+ return c.error(c, {
1336
+ message: 'Invalid key parameters. Only RSA is supported.',
1337
+ send: true,
1338
+ alert: {
1339
+ level: tls.Alert.Level.fatal,
1340
+ description: tls.Alert.Description.unsupported_certificate
1341
+ }
1342
+ });
1343
+ }
1344
+
1345
+ // expect an optional CertificateRequest message next
1346
+ c.expect = SCR;
1347
+
1348
+ // continue
1349
+ c.process();
1350
+ };
1351
+
1352
+ /**
1353
+ * Called when a client receives a ClientKeyExchange record.
1354
+ *
1355
+ * @param c the connection.
1356
+ * @param record the record.
1357
+ * @param length the length of the handshake message.
1358
+ */
1359
+ tls.handleClientKeyExchange = function (c, record, length) {
1360
+ // this implementation only supports RSA, no Diffie-Hellman support
1361
+ // so any length < 48 is invalid
1362
+ if (length < 48) {
1363
+ return c.error(c, {
1364
+ message: 'Invalid key parameters. Only RSA is supported.',
1365
+ send: true,
1366
+ alert: {
1367
+ level: tls.Alert.Level.fatal,
1368
+ description: tls.Alert.Description.unsupported_certificate
1369
+ }
1370
+ });
1371
+ }
1372
+ var b = record.fragment;
1373
+ var msg = {
1374
+ enc_pre_master_secret: readVector(b, 2).getBytes()
1375
+ };
1376
+
1377
+ // do rsa decryption
1378
+ var privateKey = null;
1379
+ if (c.getPrivateKey) {
1380
+ try {
1381
+ privateKey = c.getPrivateKey(c, c.session.serverCertificate);
1382
+ privateKey = forge.pki.privateKeyFromPem(privateKey);
1383
+ } catch (ex) {
1384
+ c.error(c, {
1385
+ message: 'Could not get private key.',
1386
+ cause: ex,
1387
+ send: true,
1388
+ alert: {
1389
+ level: tls.Alert.Level.fatal,
1390
+ description: tls.Alert.Description.internal_error
1391
+ }
1392
+ });
1393
+ }
1394
+ }
1395
+ if (privateKey === null) {
1396
+ return c.error(c, {
1397
+ message: 'No private key set.',
1398
+ send: true,
1399
+ alert: {
1400
+ level: tls.Alert.Level.fatal,
1401
+ description: tls.Alert.Description.internal_error
1402
+ }
1403
+ });
1404
+ }
1405
+ try {
1406
+ // decrypt 48-byte pre-master secret
1407
+ var sp = c.session.sp;
1408
+ sp.pre_master_secret = privateKey.decrypt(msg.enc_pre_master_secret);
1409
+
1410
+ // ensure client hello version matches first 2 bytes
1411
+ var version = c.session.clientHelloVersion;
1412
+ if (version.major !== sp.pre_master_secret.charCodeAt(0) || version.minor !== sp.pre_master_secret.charCodeAt(1)) {
1413
+ // error, do not send alert (see BLEI attack below)
1414
+ throw new Error('TLS version rollback attack detected.');
1415
+ }
1416
+ } catch (ex) {
1417
+ /* Note: Daniel Bleichenbacher [BLEI] can be used to attack a
1418
+ TLS server which is using PKCS#1 encoded RSA, so instead of
1419
+ failing here, we generate 48 random bytes and use that as
1420
+ the pre-master secret. */
1421
+ sp.pre_master_secret = forge.random.getBytes(48);
1422
+ }
1423
+
1424
+ // expect a CertificateVerify message if a Certificate was received that
1425
+ // does not have fixed Diffie-Hellman params, otherwise expect
1426
+ // ChangeCipherSpec
1427
+ c.expect = CCC;
1428
+ if (c.session.clientCertificate !== null) {
1429
+ // only RSA support, so expect CertificateVerify
1430
+ // TODO: support Diffie-Hellman
1431
+ c.expect = CCV;
1432
+ }
1433
+
1434
+ // continue
1435
+ c.process();
1436
+ };
1437
+
1438
+ /**
1439
+ * Called when a client receives a CertificateRequest record.
1440
+ *
1441
+ * When this message will be sent:
1442
+ * A non-anonymous server can optionally request a certificate from the
1443
+ * client, if appropriate for the selected cipher suite. This message, if
1444
+ * sent, will immediately follow the Server Key Exchange message (if it is
1445
+ * sent; otherwise, the Server Certificate message).
1446
+ *
1447
+ * enum {
1448
+ * rsa_sign(1), dss_sign(2), rsa_fixed_dh(3), dss_fixed_dh(4),
1449
+ * rsa_ephemeral_dh_RESERVED(5), dss_ephemeral_dh_RESERVED(6),
1450
+ * fortezza_dms_RESERVED(20), (255)
1451
+ * } ClientCertificateType;
1452
+ *
1453
+ * opaque DistinguishedName<1..2^16-1>;
1454
+ *
1455
+ * struct {
1456
+ * ClientCertificateType certificate_types<1..2^8-1>;
1457
+ * SignatureAndHashAlgorithm supported_signature_algorithms<2^16-1>;
1458
+ * DistinguishedName certificate_authorities<0..2^16-1>;
1459
+ * } CertificateRequest;
1460
+ *
1461
+ * @param c the connection.
1462
+ * @param record the record.
1463
+ * @param length the length of the handshake message.
1464
+ */
1465
+ tls.handleCertificateRequest = function (c, record, length) {
1466
+ // minimum of 3 bytes in message
1467
+ if (length < 3) {
1468
+ return c.error(c, {
1469
+ message: 'Invalid CertificateRequest. Message too short.',
1470
+ send: true,
1471
+ alert: {
1472
+ level: tls.Alert.Level.fatal,
1473
+ description: tls.Alert.Description.illegal_parameter
1474
+ }
1475
+ });
1476
+ }
1477
+
1478
+ // TODO: TLS 1.2+ has different format including
1479
+ // SignatureAndHashAlgorithm after cert types
1480
+ var b = record.fragment;
1481
+ var msg = {
1482
+ certificate_types: readVector(b, 1),
1483
+ certificate_authorities: readVector(b, 2)
1484
+ };
1485
+
1486
+ // save certificate request in session
1487
+ c.session.certificateRequest = msg;
1488
+
1489
+ // expect a ServerHelloDone message next
1490
+ c.expect = SHD;
1491
+
1492
+ // continue
1493
+ c.process();
1494
+ };
1495
+
1496
+ /**
1497
+ * Called when a server receives a CertificateVerify record.
1498
+ *
1499
+ * @param c the connection.
1500
+ * @param record the record.
1501
+ * @param length the length of the handshake message.
1502
+ */
1503
+ tls.handleCertificateVerify = function (c, record, length) {
1504
+ if (length < 2) {
1505
+ return c.error(c, {
1506
+ message: 'Invalid CertificateVerify. Message too short.',
1507
+ send: true,
1508
+ alert: {
1509
+ level: tls.Alert.Level.fatal,
1510
+ description: tls.Alert.Description.illegal_parameter
1511
+ }
1512
+ });
1513
+ }
1514
+
1515
+ // rewind to get full bytes for message so it can be manually
1516
+ // digested below (special case for CertificateVerify messages because
1517
+ // they must be digested *after* handling as opposed to all others)
1518
+ var b = record.fragment;
1519
+ b.read -= 4;
1520
+ var msgBytes = b.bytes();
1521
+ b.read += 4;
1522
+ var msg = {
1523
+ signature: readVector(b, 2).getBytes()
1524
+ };
1525
+
1526
+ // TODO: add support for DSA
1527
+
1528
+ // generate data to verify
1529
+ var verify = forge.util.createBuffer();
1530
+ verify.putBuffer(c.session.md5.digest());
1531
+ verify.putBuffer(c.session.sha1.digest());
1532
+ verify = verify.getBytes();
1533
+ try {
1534
+ var cert = c.session.clientCertificate;
1535
+ /*b = forge.pki.rsa.decrypt(
1536
+ msg.signature, cert.publicKey, true, verify.length);
1537
+ if(b !== verify) {*/
1538
+ if (!cert.publicKey.verify(verify, msg.signature, 'NONE')) {
1539
+ throw new Error('CertificateVerify signature does not match.');
1540
+ }
1541
+
1542
+ // digest message now that it has been handled
1543
+ c.session.md5.update(msgBytes);
1544
+ c.session.sha1.update(msgBytes);
1545
+ } catch (ex) {
1546
+ return c.error(c, {
1547
+ message: 'Bad signature in CertificateVerify.',
1548
+ send: true,
1549
+ alert: {
1550
+ level: tls.Alert.Level.fatal,
1551
+ description: tls.Alert.Description.handshake_failure
1552
+ }
1553
+ });
1554
+ }
1555
+
1556
+ // expect ChangeCipherSpec
1557
+ c.expect = CCC;
1558
+
1559
+ // continue
1560
+ c.process();
1561
+ };
1562
+
1563
+ /**
1564
+ * Called when a client receives a ServerHelloDone record.
1565
+ *
1566
+ * When this message will be sent:
1567
+ * The server hello done message is sent by the server to indicate the end
1568
+ * of the server hello and associated messages. After sending this message
1569
+ * the server will wait for a client response.
1570
+ *
1571
+ * Meaning of this message:
1572
+ * This message means that the server is done sending messages to support
1573
+ * the key exchange, and the client can proceed with its phase of the key
1574
+ * exchange.
1575
+ *
1576
+ * Upon receipt of the server hello done message the client should verify
1577
+ * that the server provided a valid certificate if required and check that
1578
+ * the server hello parameters are acceptable.
1579
+ *
1580
+ * struct {} ServerHelloDone;
1581
+ *
1582
+ * @param c the connection.
1583
+ * @param record the record.
1584
+ * @param length the length of the handshake message.
1585
+ */
1586
+ tls.handleServerHelloDone = function (c, record, length) {
1587
+ // len must be 0 bytes
1588
+ if (length > 0) {
1589
+ return c.error(c, {
1590
+ message: 'Invalid ServerHelloDone message. Invalid length.',
1591
+ send: true,
1592
+ alert: {
1593
+ level: tls.Alert.Level.fatal,
1594
+ description: tls.Alert.Description.record_overflow
1595
+ }
1596
+ });
1597
+ }
1598
+ if (c.serverCertificate === null) {
1599
+ // no server certificate was provided
1600
+ var error = {
1601
+ message: 'No server certificate provided. Not enough security.',
1602
+ send: true,
1603
+ alert: {
1604
+ level: tls.Alert.Level.fatal,
1605
+ description: tls.Alert.Description.insufficient_security
1606
+ }
1607
+ };
1608
+
1609
+ // call application callback
1610
+ var depth = 0;
1611
+ var ret = c.verify(c, error.alert.description, depth, []);
1612
+ if (ret !== true) {
1613
+ // check for custom alert info
1614
+ if (ret || ret === 0) {
1615
+ // set custom message and alert description
1616
+ if (typeof ret === 'object' && !forge.util.isArray(ret)) {
1617
+ if (ret.message) {
1618
+ error.message = ret.message;
1619
+ }
1620
+ if (ret.alert) {
1621
+ error.alert.description = ret.alert;
1622
+ }
1623
+ } else if (typeof ret === 'number') {
1624
+ // set custom alert description
1625
+ error.alert.description = ret;
1626
+ }
1627
+ }
1628
+
1629
+ // send error
1630
+ return c.error(c, error);
1631
+ }
1632
+ }
1633
+
1634
+ // create client certificate message if requested
1635
+ if (c.session.certificateRequest !== null) {
1636
+ record = tls.createRecord(c, {
1637
+ type: tls.ContentType.handshake,
1638
+ data: tls.createCertificate(c)
1639
+ });
1640
+ tls.queue(c, record);
1641
+ }
1642
+
1643
+ // create client key exchange message
1644
+ record = tls.createRecord(c, {
1645
+ type: tls.ContentType.handshake,
1646
+ data: tls.createClientKeyExchange(c)
1647
+ });
1648
+ tls.queue(c, record);
1649
+
1650
+ // expect no messages until the following callback has been called
1651
+ c.expect = SER;
1652
+
1653
+ // create callback to handle client signature (for client-certs)
1654
+ var callback = function (c, signature) {
1655
+ if (c.session.certificateRequest !== null && c.session.clientCertificate !== null) {
1656
+ // create certificate verify message
1657
+ tls.queue(c, tls.createRecord(c, {
1658
+ type: tls.ContentType.handshake,
1659
+ data: tls.createCertificateVerify(c, signature)
1660
+ }));
1661
+ }
1662
+
1663
+ // create change cipher spec message
1664
+ tls.queue(c, tls.createRecord(c, {
1665
+ type: tls.ContentType.change_cipher_spec,
1666
+ data: tls.createChangeCipherSpec()
1667
+ }));
1668
+
1669
+ // create pending state
1670
+ c.state.pending = tls.createConnectionState(c);
1671
+
1672
+ // change current write state to pending write state
1673
+ c.state.current.write = c.state.pending.write;
1674
+
1675
+ // create finished message
1676
+ tls.queue(c, tls.createRecord(c, {
1677
+ type: tls.ContentType.handshake,
1678
+ data: tls.createFinished(c)
1679
+ }));
1680
+
1681
+ // expect a server ChangeCipherSpec message next
1682
+ c.expect = SCC;
1683
+
1684
+ // send records
1685
+ tls.flush(c);
1686
+
1687
+ // continue
1688
+ c.process();
1689
+ };
1690
+
1691
+ // if there is no certificate request or no client certificate, do
1692
+ // callback immediately
1693
+ if (c.session.certificateRequest === null || c.session.clientCertificate === null) {
1694
+ return callback(c, null);
1695
+ }
1696
+
1697
+ // otherwise get the client signature
1698
+ tls.getClientSignature(c, callback);
1699
+ };
1700
+
1701
+ /**
1702
+ * Called when a ChangeCipherSpec record is received.
1703
+ *
1704
+ * @param c the connection.
1705
+ * @param record the record.
1706
+ */
1707
+ tls.handleChangeCipherSpec = function (c, record) {
1708
+ if (record.fragment.getByte() !== 0x01) {
1709
+ return c.error(c, {
1710
+ message: 'Invalid ChangeCipherSpec message received.',
1711
+ send: true,
1712
+ alert: {
1713
+ level: tls.Alert.Level.fatal,
1714
+ description: tls.Alert.Description.illegal_parameter
1715
+ }
1716
+ });
1717
+ }
1718
+
1719
+ // create pending state if:
1720
+ // 1. Resuming session in client mode OR
1721
+ // 2. NOT resuming session in server mode
1722
+ var client = c.entity === tls.ConnectionEnd.client;
1723
+ if (c.session.resuming && client || !c.session.resuming && !client) {
1724
+ c.state.pending = tls.createConnectionState(c);
1725
+ }
1726
+
1727
+ // change current read state to pending read state
1728
+ c.state.current.read = c.state.pending.read;
1729
+
1730
+ // clear pending state if:
1731
+ // 1. NOT resuming session in client mode OR
1732
+ // 2. resuming a session in server mode
1733
+ if (!c.session.resuming && client || c.session.resuming && !client) {
1734
+ c.state.pending = null;
1735
+ }
1736
+
1737
+ // expect a Finished record next
1738
+ c.expect = client ? SFI : CFI;
1739
+
1740
+ // continue
1741
+ c.process();
1742
+ };
1743
+
1744
+ /**
1745
+ * Called when a Finished record is received.
1746
+ *
1747
+ * When this message will be sent:
1748
+ * A finished message is always sent immediately after a change
1749
+ * cipher spec message to verify that the key exchange and
1750
+ * authentication processes were successful. It is essential that a
1751
+ * change cipher spec message be received between the other
1752
+ * handshake messages and the Finished message.
1753
+ *
1754
+ * Meaning of this message:
1755
+ * The finished message is the first protected with the just-
1756
+ * negotiated algorithms, keys, and secrets. Recipients of finished
1757
+ * messages must verify that the contents are correct. Once a side
1758
+ * has sent its Finished message and received and validated the
1759
+ * Finished message from its peer, it may begin to send and receive
1760
+ * application data over the connection.
1761
+ *
1762
+ * struct {
1763
+ * opaque verify_data[verify_data_length];
1764
+ * } Finished;
1765
+ *
1766
+ * verify_data
1767
+ * PRF(master_secret, finished_label, Hash(handshake_messages))
1768
+ * [0..verify_data_length-1];
1769
+ *
1770
+ * finished_label
1771
+ * For Finished messages sent by the client, the string
1772
+ * "client finished". For Finished messages sent by the server, the
1773
+ * string "server finished".
1774
+ *
1775
+ * verify_data_length depends on the cipher suite. If it is not specified
1776
+ * by the cipher suite, then it is 12. Versions of TLS < 1.2 always used
1777
+ * 12 bytes.
1778
+ *
1779
+ * @param c the connection.
1780
+ * @param record the record.
1781
+ * @param length the length of the handshake message.
1782
+ */
1783
+ tls.handleFinished = function (c, record, length) {
1784
+ // rewind to get full bytes for message so it can be manually
1785
+ // digested below (special case for Finished messages because they
1786
+ // must be digested *after* handling as opposed to all others)
1787
+ var b = record.fragment;
1788
+ b.read -= 4;
1789
+ var msgBytes = b.bytes();
1790
+ b.read += 4;
1791
+
1792
+ // message contains only verify_data
1793
+ var vd = record.fragment.getBytes();
1794
+
1795
+ // ensure verify data is correct
1796
+ b = forge.util.createBuffer();
1797
+ b.putBuffer(c.session.md5.digest());
1798
+ b.putBuffer(c.session.sha1.digest());
1799
+
1800
+ // set label based on entity type
1801
+ var client = c.entity === tls.ConnectionEnd.client;
1802
+ var label = client ? 'server finished' : 'client finished';
1803
+
1804
+ // TODO: determine prf function and verify length for TLS 1.2
1805
+ var sp = c.session.sp;
1806
+ var vdl = 12;
1807
+ var prf = prf_TLS1;
1808
+ b = prf(sp.master_secret, label, b.getBytes(), vdl);
1809
+ if (b.getBytes() !== vd) {
1810
+ return c.error(c, {
1811
+ message: 'Invalid verify_data in Finished message.',
1812
+ send: true,
1813
+ alert: {
1814
+ level: tls.Alert.Level.fatal,
1815
+ description: tls.Alert.Description.decrypt_error
1816
+ }
1817
+ });
1818
+ }
1819
+
1820
+ // digest finished message now that it has been handled
1821
+ c.session.md5.update(msgBytes);
1822
+ c.session.sha1.update(msgBytes);
1823
+
1824
+ // resuming session as client or NOT resuming session as server
1825
+ if (c.session.resuming && client || !c.session.resuming && !client) {
1826
+ // create change cipher spec message
1827
+ tls.queue(c, tls.createRecord(c, {
1828
+ type: tls.ContentType.change_cipher_spec,
1829
+ data: tls.createChangeCipherSpec()
1830
+ }));
1831
+
1832
+ // change current write state to pending write state, clear pending
1833
+ c.state.current.write = c.state.pending.write;
1834
+ c.state.pending = null;
1835
+
1836
+ // create finished message
1837
+ tls.queue(c, tls.createRecord(c, {
1838
+ type: tls.ContentType.handshake,
1839
+ data: tls.createFinished(c)
1840
+ }));
1841
+ }
1842
+
1843
+ // expect application data next
1844
+ c.expect = client ? SAD : CAD;
1845
+
1846
+ // handshake complete
1847
+ c.handshaking = false;
1848
+ ++c.handshakes;
1849
+
1850
+ // save access to peer certificate
1851
+ c.peerCertificate = client ? c.session.serverCertificate : c.session.clientCertificate;
1852
+
1853
+ // send records
1854
+ tls.flush(c);
1855
+
1856
+ // now connected
1857
+ c.isConnected = true;
1858
+ c.connected(c);
1859
+
1860
+ // continue
1861
+ c.process();
1862
+ };
1863
+
1864
+ /**
1865
+ * Called when an Alert record is received.
1866
+ *
1867
+ * @param c the connection.
1868
+ * @param record the record.
1869
+ */
1870
+ tls.handleAlert = function (c, record) {
1871
+ // read alert
1872
+ var b = record.fragment;
1873
+ var alert = {
1874
+ level: b.getByte(),
1875
+ description: b.getByte()
1876
+ };
1877
+
1878
+ // TODO: consider using a table?
1879
+ // get appropriate message
1880
+ var msg;
1881
+ switch (alert.description) {
1882
+ case tls.Alert.Description.close_notify:
1883
+ msg = 'Connection closed.';
1884
+ break;
1885
+ case tls.Alert.Description.unexpected_message:
1886
+ msg = 'Unexpected message.';
1887
+ break;
1888
+ case tls.Alert.Description.bad_record_mac:
1889
+ msg = 'Bad record MAC.';
1890
+ break;
1891
+ case tls.Alert.Description.decryption_failed:
1892
+ msg = 'Decryption failed.';
1893
+ break;
1894
+ case tls.Alert.Description.record_overflow:
1895
+ msg = 'Record overflow.';
1896
+ break;
1897
+ case tls.Alert.Description.decompression_failure:
1898
+ msg = 'Decompression failed.';
1899
+ break;
1900
+ case tls.Alert.Description.handshake_failure:
1901
+ msg = 'Handshake failure.';
1902
+ break;
1903
+ case tls.Alert.Description.bad_certificate:
1904
+ msg = 'Bad certificate.';
1905
+ break;
1906
+ case tls.Alert.Description.unsupported_certificate:
1907
+ msg = 'Unsupported certificate.';
1908
+ break;
1909
+ case tls.Alert.Description.certificate_revoked:
1910
+ msg = 'Certificate revoked.';
1911
+ break;
1912
+ case tls.Alert.Description.certificate_expired:
1913
+ msg = 'Certificate expired.';
1914
+ break;
1915
+ case tls.Alert.Description.certificate_unknown:
1916
+ msg = 'Certificate unknown.';
1917
+ break;
1918
+ case tls.Alert.Description.illegal_parameter:
1919
+ msg = 'Illegal parameter.';
1920
+ break;
1921
+ case tls.Alert.Description.unknown_ca:
1922
+ msg = 'Unknown certificate authority.';
1923
+ break;
1924
+ case tls.Alert.Description.access_denied:
1925
+ msg = 'Access denied.';
1926
+ break;
1927
+ case tls.Alert.Description.decode_error:
1928
+ msg = 'Decode error.';
1929
+ break;
1930
+ case tls.Alert.Description.decrypt_error:
1931
+ msg = 'Decrypt error.';
1932
+ break;
1933
+ case tls.Alert.Description.export_restriction:
1934
+ msg = 'Export restriction.';
1935
+ break;
1936
+ case tls.Alert.Description.protocol_version:
1937
+ msg = 'Unsupported protocol version.';
1938
+ break;
1939
+ case tls.Alert.Description.insufficient_security:
1940
+ msg = 'Insufficient security.';
1941
+ break;
1942
+ case tls.Alert.Description.internal_error:
1943
+ msg = 'Internal error.';
1944
+ break;
1945
+ case tls.Alert.Description.user_canceled:
1946
+ msg = 'User canceled.';
1947
+ break;
1948
+ case tls.Alert.Description.no_renegotiation:
1949
+ msg = 'Renegotiation not supported.';
1950
+ break;
1951
+ default:
1952
+ msg = 'Unknown error.';
1953
+ break;
1954
+ }
1955
+
1956
+ // close connection on close_notify, not an error
1957
+ if (alert.description === tls.Alert.Description.close_notify) {
1958
+ return c.close();
1959
+ }
1960
+
1961
+ // call error handler
1962
+ c.error(c, {
1963
+ message: msg,
1964
+ send: false,
1965
+ // origin is the opposite end
1966
+ origin: c.entity === tls.ConnectionEnd.client ? 'server' : 'client',
1967
+ alert: alert
1968
+ });
1969
+
1970
+ // continue
1971
+ c.process();
1972
+ };
1973
+
1974
+ /**
1975
+ * Called when a Handshake record is received.
1976
+ *
1977
+ * @param c the connection.
1978
+ * @param record the record.
1979
+ */
1980
+ tls.handleHandshake = function (c, record) {
1981
+ // get the handshake type and message length
1982
+ var b = record.fragment;
1983
+ var type = b.getByte();
1984
+ var length = b.getInt24();
1985
+
1986
+ // see if the record fragment doesn't yet contain the full message
1987
+ if (length > b.length()) {
1988
+ // cache the record, clear its fragment, and reset the buffer read
1989
+ // pointer before the type and length were read
1990
+ c.fragmented = record;
1991
+ record.fragment = forge.util.createBuffer();
1992
+ b.read -= 4;
1993
+
1994
+ // continue
1995
+ return c.process();
1996
+ }
1997
+
1998
+ // full message now available, clear cache, reset read pointer to
1999
+ // before type and length
2000
+ c.fragmented = null;
2001
+ b.read -= 4;
2002
+
2003
+ // save the handshake bytes for digestion after handler is found
2004
+ // (include type and length of handshake msg)
2005
+ var bytes = b.bytes(length + 4);
2006
+
2007
+ // restore read pointer
2008
+ b.read += 4;
2009
+
2010
+ // handle expected message
2011
+ if (type in hsTable[c.entity][c.expect]) {
2012
+ // initialize server session
2013
+ if (c.entity === tls.ConnectionEnd.server && !c.open && !c.fail) {
2014
+ c.handshaking = true;
2015
+ c.session = {
2016
+ version: null,
2017
+ extensions: {
2018
+ server_name: {
2019
+ serverNameList: []
2020
+ }
2021
+ },
2022
+ cipherSuite: null,
2023
+ compressionMethod: null,
2024
+ serverCertificate: null,
2025
+ clientCertificate: null,
2026
+ md5: forge.md.md5.create(),
2027
+ sha1: forge.md.sha1.create()
2028
+ };
2029
+ }
2030
+
2031
+ /* Update handshake messages digest. Finished and CertificateVerify
2032
+ messages are not digested here. They can't be digested as part of
2033
+ the verify_data that they contain. These messages are manually
2034
+ digested in their handlers. HelloRequest messages are simply never
2035
+ included in the handshake message digest according to spec. */
2036
+ if (type !== tls.HandshakeType.hello_request && type !== tls.HandshakeType.certificate_verify && type !== tls.HandshakeType.finished) {
2037
+ c.session.md5.update(bytes);
2038
+ c.session.sha1.update(bytes);
2039
+ }
2040
+
2041
+ // handle specific handshake type record
2042
+ hsTable[c.entity][c.expect][type](c, record, length);
2043
+ } else {
2044
+ // unexpected record
2045
+ tls.handleUnexpected(c, record);
2046
+ }
2047
+ };
2048
+
2049
+ /**
2050
+ * Called when an ApplicationData record is received.
2051
+ *
2052
+ * @param c the connection.
2053
+ * @param record the record.
2054
+ */
2055
+ tls.handleApplicationData = function (c, record) {
2056
+ // buffer data, notify that its ready
2057
+ c.data.putBuffer(record.fragment);
2058
+ c.dataReady(c);
2059
+
2060
+ // continue
2061
+ c.process();
2062
+ };
2063
+
2064
+ /**
2065
+ * Called when a Heartbeat record is received.
2066
+ *
2067
+ * @param c the connection.
2068
+ * @param record the record.
2069
+ */
2070
+ tls.handleHeartbeat = function (c, record) {
2071
+ // get the heartbeat type and payload
2072
+ var b = record.fragment;
2073
+ var type = b.getByte();
2074
+ var length = b.getInt16();
2075
+ var payload = b.getBytes(length);
2076
+ if (type === tls.HeartbeatMessageType.heartbeat_request) {
2077
+ // discard request during handshake or if length is too large
2078
+ if (c.handshaking || length > payload.length) {
2079
+ // continue
2080
+ return c.process();
2081
+ }
2082
+ // retransmit payload
2083
+ tls.queue(c, tls.createRecord(c, {
2084
+ type: tls.ContentType.heartbeat,
2085
+ data: tls.createHeartbeat(tls.HeartbeatMessageType.heartbeat_response, payload)
2086
+ }));
2087
+ tls.flush(c);
2088
+ } else if (type === tls.HeartbeatMessageType.heartbeat_response) {
2089
+ // check payload against expected payload, discard heartbeat if no match
2090
+ if (payload !== c.expectedHeartbeatPayload) {
2091
+ // continue
2092
+ return c.process();
2093
+ }
2094
+
2095
+ // notify that a valid heartbeat was received
2096
+ if (c.heartbeatReceived) {
2097
+ c.heartbeatReceived(c, forge.util.createBuffer(payload));
2098
+ }
2099
+ }
2100
+
2101
+ // continue
2102
+ c.process();
2103
+ };
2104
+
2105
+ /**
2106
+ * The transistional state tables for receiving TLS records. It maps the
2107
+ * current TLS engine state and a received record to a function to handle the
2108
+ * record and update the state.
2109
+ *
2110
+ * For instance, if the current state is SHE, then the TLS engine is expecting
2111
+ * a ServerHello record. Once a record is received, the handler function is
2112
+ * looked up using the state SHE and the record's content type.
2113
+ *
2114
+ * The resulting function will either be an error handler or a record handler.
2115
+ * The function will take whatever action is appropriate and update the state
2116
+ * for the next record.
2117
+ *
2118
+ * The states are all based on possible server record types. Note that the
2119
+ * client will never specifically expect to receive a HelloRequest or an alert
2120
+ * from the server so there is no state that reflects this. These messages may
2121
+ * occur at any time.
2122
+ *
2123
+ * There are two tables for mapping states because there is a second tier of
2124
+ * types for handshake messages. Once a record with a content type of handshake
2125
+ * is received, the handshake record handler will look up the handshake type in
2126
+ * the secondary map to get its appropriate handler.
2127
+ *
2128
+ * Valid message orders are as follows:
2129
+ *
2130
+ * =======================FULL HANDSHAKE======================
2131
+ * Client Server
2132
+ *
2133
+ * ClientHello -------->
2134
+ * ServerHello
2135
+ * Certificate*
2136
+ * ServerKeyExchange*
2137
+ * CertificateRequest*
2138
+ * <-------- ServerHelloDone
2139
+ * Certificate*
2140
+ * ClientKeyExchange
2141
+ * CertificateVerify*
2142
+ * [ChangeCipherSpec]
2143
+ * Finished -------->
2144
+ * [ChangeCipherSpec]
2145
+ * <-------- Finished
2146
+ * Application Data <-------> Application Data
2147
+ *
2148
+ * =====================SESSION RESUMPTION=====================
2149
+ * Client Server
2150
+ *
2151
+ * ClientHello -------->
2152
+ * ServerHello
2153
+ * [ChangeCipherSpec]
2154
+ * <-------- Finished
2155
+ * [ChangeCipherSpec]
2156
+ * Finished -------->
2157
+ * Application Data <-------> Application Data
2158
+ */
2159
+ // client expect states (indicate which records are expected to be received)
2160
+ var SHE = 0; // rcv server hello
2161
+ var SCE = 1; // rcv server certificate
2162
+ var SKE = 2; // rcv server key exchange
2163
+ var SCR = 3; // rcv certificate request
2164
+ var SHD = 4; // rcv server hello done
2165
+ var SCC = 5; // rcv change cipher spec
2166
+ var SFI = 6; // rcv finished
2167
+ var SAD = 7; // rcv application data
2168
+ var SER = 8; // not expecting any messages at this point
2169
+
2170
+ // server expect states
2171
+ var CHE = 0; // rcv client hello
2172
+ var CCE = 1; // rcv client certificate
2173
+ var CKE = 2; // rcv client key exchange
2174
+ var CCV = 3; // rcv certificate verify
2175
+ var CCC = 4; // rcv change cipher spec
2176
+ var CFI = 5; // rcv finished
2177
+ var CAD = 6; // rcv application data
2178
+
2179
+ // map client current expect state and content type to function
2180
+ var __ = tls.handleUnexpected;
2181
+ var R0 = tls.handleChangeCipherSpec;
2182
+ var R1 = tls.handleAlert;
2183
+ var R2 = tls.handleHandshake;
2184
+ var R3 = tls.handleApplicationData;
2185
+ var R4 = tls.handleHeartbeat;
2186
+ var ctTable = [];
2187
+ ctTable[tls.ConnectionEnd.client] = [
2188
+ // CC,AL,HS,AD,HB
2189
+ /*SHE*/
2190
+ [__, R1, R2, __, R4], /*SCE*/[__, R1, R2, __, R4], /*SKE*/[__, R1, R2, __, R4], /*SCR*/[__, R1, R2, __, R4], /*SHD*/[__, R1, R2, __, R4], /*SCC*/[R0, R1, __, __, R4], /*SFI*/[__, R1, R2, __, R4], /*SAD*/[__, R1, R2, R3, R4], /*SER*/[__, R1, R2, __, R4]];
2191
+
2192
+ // map server current expect state and content type to function
2193
+ ctTable[tls.ConnectionEnd.server] = [
2194
+ // CC,AL,HS,AD
2195
+ /*CHE*/
2196
+ [__, R1, R2, __, R4], /*CCE*/[__, R1, R2, __, R4], /*CKE*/[__, R1, R2, __, R4], /*CCV*/[__, R1, R2, __, R4], /*CCC*/[R0, R1, __, __, R4], /*CFI*/[__, R1, R2, __, R4], /*CAD*/[__, R1, R2, R3, R4], /*CER*/[__, R1, R2, __, R4]];
2197
+
2198
+ // map client current expect state and handshake type to function
2199
+ var H0 = tls.handleHelloRequest;
2200
+ var H1 = tls.handleServerHello;
2201
+ var H2 = tls.handleCertificate;
2202
+ var H3 = tls.handleServerKeyExchange;
2203
+ var H4 = tls.handleCertificateRequest;
2204
+ var H5 = tls.handleServerHelloDone;
2205
+ var H6 = tls.handleFinished;
2206
+ var hsTable = [];
2207
+ hsTable[tls.ConnectionEnd.client] = [
2208
+ // HR,01,SH,03,04,05,06,07,08,09,10,SC,SK,CR,HD,15,CK,17,18,19,FI
2209
+ /*SHE*/
2210
+ [__, __, H1, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __], /*SCE*/[H0, __, __, __, __, __, __, __, __, __, __, H2, H3, H4, H5, __, __, __, __, __, __], /*SKE*/[H0, __, __, __, __, __, __, __, __, __, __, __, H3, H4, H5, __, __, __, __, __, __], /*SCR*/[H0, __, __, __, __, __, __, __, __, __, __, __, __, H4, H5, __, __, __, __, __, __], /*SHD*/[H0, __, __, __, __, __, __, __, __, __, __, __, __, __, H5, __, __, __, __, __, __], /*SCC*/[H0, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __], /*SFI*/[H0, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, H6], /*SAD*/[H0, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __], /*SER*/[H0, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __]];
2211
+
2212
+ // map server current expect state and handshake type to function
2213
+ // Note: CAD[CH] does not map to FB because renegotation is prohibited
2214
+ var H7 = tls.handleClientHello;
2215
+ var H8 = tls.handleClientKeyExchange;
2216
+ var H9 = tls.handleCertificateVerify;
2217
+ hsTable[tls.ConnectionEnd.server] = [
2218
+ // 01,CH,02,03,04,05,06,07,08,09,10,CC,12,13,14,CV,CK,17,18,19,FI
2219
+ /*CHE*/
2220
+ [__, H7, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __], /*CCE*/[__, __, __, __, __, __, __, __, __, __, __, H2, __, __, __, __, __, __, __, __, __], /*CKE*/[__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, H8, __, __, __, __], /*CCV*/[__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, H9, __, __, __, __, __], /*CCC*/[__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __], /*CFI*/[__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, H6], /*CAD*/[__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __], /*CER*/[__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __]];
2221
+
2222
+ /**
2223
+ * Generates the master_secret and keys using the given security parameters.
2224
+ *
2225
+ * The security parameters for a TLS connection state are defined as such:
2226
+ *
2227
+ * struct {
2228
+ * ConnectionEnd entity;
2229
+ * PRFAlgorithm prf_algorithm;
2230
+ * BulkCipherAlgorithm bulk_cipher_algorithm;
2231
+ * CipherType cipher_type;
2232
+ * uint8 enc_key_length;
2233
+ * uint8 block_length;
2234
+ * uint8 fixed_iv_length;
2235
+ * uint8 record_iv_length;
2236
+ * MACAlgorithm mac_algorithm;
2237
+ * uint8 mac_length;
2238
+ * uint8 mac_key_length;
2239
+ * CompressionMethod compression_algorithm;
2240
+ * opaque master_secret[48];
2241
+ * opaque client_random[32];
2242
+ * opaque server_random[32];
2243
+ * } SecurityParameters;
2244
+ *
2245
+ * Note that this definition is from TLS 1.2. In TLS 1.0 some of these
2246
+ * parameters are ignored because, for instance, the PRFAlgorithm is a
2247
+ * builtin-fixed algorithm combining iterations of MD5 and SHA-1 in TLS 1.0.
2248
+ *
2249
+ * The Record Protocol requires an algorithm to generate keys required by the
2250
+ * current connection state.
2251
+ *
2252
+ * The master secret is expanded into a sequence of secure bytes, which is then
2253
+ * split to a client write MAC key, a server write MAC key, a client write
2254
+ * encryption key, and a server write encryption key. In TLS 1.0 a client write
2255
+ * IV and server write IV are also generated. Each of these is generated from
2256
+ * the byte sequence in that order. Unused values are empty. In TLS 1.2, some
2257
+ * AEAD ciphers may additionally require a client write IV and a server write
2258
+ * IV (see Section 6.2.3.3).
2259
+ *
2260
+ * When keys, MAC keys, and IVs are generated, the master secret is used as an
2261
+ * entropy source.
2262
+ *
2263
+ * To generate the key material, compute:
2264
+ *
2265
+ * master_secret = PRF(pre_master_secret, "master secret",
2266
+ * ClientHello.random + ServerHello.random)
2267
+ *
2268
+ * key_block = PRF(SecurityParameters.master_secret,
2269
+ * "key expansion",
2270
+ * SecurityParameters.server_random +
2271
+ * SecurityParameters.client_random);
2272
+ *
2273
+ * until enough output has been generated. Then, the key_block is
2274
+ * partitioned as follows:
2275
+ *
2276
+ * client_write_MAC_key[SecurityParameters.mac_key_length]
2277
+ * server_write_MAC_key[SecurityParameters.mac_key_length]
2278
+ * client_write_key[SecurityParameters.enc_key_length]
2279
+ * server_write_key[SecurityParameters.enc_key_length]
2280
+ * client_write_IV[SecurityParameters.fixed_iv_length]
2281
+ * server_write_IV[SecurityParameters.fixed_iv_length]
2282
+ *
2283
+ * In TLS 1.2, the client_write_IV and server_write_IV are only generated for
2284
+ * implicit nonce techniques as described in Section 3.2.1 of [AEAD]. This
2285
+ * implementation uses TLS 1.0 so IVs are generated.
2286
+ *
2287
+ * Implementation note: The currently defined cipher suite which requires the
2288
+ * most material is AES_256_CBC_SHA256. It requires 2 x 32 byte keys and 2 x 32
2289
+ * byte MAC keys, for a total 128 bytes of key material. In TLS 1.0 it also
2290
+ * requires 2 x 16 byte IVs, so it actually takes 160 bytes of key material.
2291
+ *
2292
+ * @param c the connection.
2293
+ * @param sp the security parameters to use.
2294
+ *
2295
+ * @return the security keys.
2296
+ */
2297
+ tls.generateKeys = function (c, sp) {
2298
+ // TLS_RSA_WITH_AES_128_CBC_SHA (required to be compliant with TLS 1.2) &
2299
+ // TLS_RSA_WITH_AES_256_CBC_SHA are the only cipher suites implemented
2300
+ // at present
2301
+
2302
+ // TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA is required to be compliant with
2303
+ // TLS 1.0 but we don't care right now because AES is better and we have
2304
+ // an implementation for it
2305
+
2306
+ // TODO: TLS 1.2 implementation
2307
+ /*
2308
+ // determine the PRF
2309
+ var prf;
2310
+ switch(sp.prf_algorithm) {
2311
+ case tls.PRFAlgorithm.tls_prf_sha256:
2312
+ prf = prf_sha256;
2313
+ break;
2314
+ default:
2315
+ // should never happen
2316
+ throw new Error('Invalid PRF');
2317
+ }
2318
+ */
2319
+
2320
+ // TLS 1.0/1.1 implementation
2321
+ var prf = prf_TLS1;
2322
+
2323
+ // concatenate server and client random
2324
+ var random = sp.client_random + sp.server_random;
2325
+
2326
+ // only create master secret if session is new
2327
+ if (!c.session.resuming) {
2328
+ // create master secret, clean up pre-master secret
2329
+ sp.master_secret = prf(sp.pre_master_secret, 'master secret', random, 48).bytes();
2330
+ sp.pre_master_secret = null;
2331
+ }
2332
+
2333
+ // generate the amount of key material needed
2334
+ random = sp.server_random + sp.client_random;
2335
+ var length = 2 * sp.mac_key_length + 2 * sp.enc_key_length;
2336
+
2337
+ // include IV for TLS/1.0
2338
+ var tls10 = c.version.major === tls.Versions.TLS_1_0.major && c.version.minor === tls.Versions.TLS_1_0.minor;
2339
+ if (tls10) {
2340
+ length += 2 * sp.fixed_iv_length;
2341
+ }
2342
+ var km = prf(sp.master_secret, 'key expansion', random, length);
2343
+
2344
+ // split the key material into the MAC and encryption keys
2345
+ var rval = {
2346
+ client_write_MAC_key: km.getBytes(sp.mac_key_length),
2347
+ server_write_MAC_key: km.getBytes(sp.mac_key_length),
2348
+ client_write_key: km.getBytes(sp.enc_key_length),
2349
+ server_write_key: km.getBytes(sp.enc_key_length)
2350
+ };
2351
+
2352
+ // include TLS 1.0 IVs
2353
+ if (tls10) {
2354
+ rval.client_write_IV = km.getBytes(sp.fixed_iv_length);
2355
+ rval.server_write_IV = km.getBytes(sp.fixed_iv_length);
2356
+ }
2357
+ return rval;
2358
+ };
2359
+
2360
+ /**
2361
+ * Creates a new initialized TLS connection state. A connection state has
2362
+ * a read mode and a write mode.
2363
+ *
2364
+ * compression state:
2365
+ * The current state of the compression algorithm.
2366
+ *
2367
+ * cipher state:
2368
+ * The current state of the encryption algorithm. This will consist of the
2369
+ * scheduled key for that connection. For stream ciphers, this will also
2370
+ * contain whatever state information is necessary to allow the stream to
2371
+ * continue to encrypt or decrypt data.
2372
+ *
2373
+ * MAC key:
2374
+ * The MAC key for the connection.
2375
+ *
2376
+ * sequence number:
2377
+ * Each connection state contains a sequence number, which is maintained
2378
+ * separately for read and write states. The sequence number MUST be set to
2379
+ * zero whenever a connection state is made the active state. Sequence
2380
+ * numbers are of type uint64 and may not exceed 2^64-1. Sequence numbers do
2381
+ * not wrap. If a TLS implementation would need to wrap a sequence number,
2382
+ * it must renegotiate instead. A sequence number is incremented after each
2383
+ * record: specifically, the first record transmitted under a particular
2384
+ * connection state MUST use sequence number 0.
2385
+ *
2386
+ * @param c the connection.
2387
+ *
2388
+ * @return the new initialized TLS connection state.
2389
+ */
2390
+ tls.createConnectionState = function (c) {
2391
+ var client = c.entity === tls.ConnectionEnd.client;
2392
+ var createMode = function () {
2393
+ var mode = {
2394
+ // two 32-bit numbers, first is most significant
2395
+ sequenceNumber: [0, 0],
2396
+ macKey: null,
2397
+ macLength: 0,
2398
+ macFunction: null,
2399
+ cipherState: null,
2400
+ cipherFunction: function (record) {
2401
+ return true;
2402
+ },
2403
+ compressionState: null,
2404
+ compressFunction: function (record) {
2405
+ return true;
2406
+ },
2407
+ updateSequenceNumber: function () {
2408
+ if (mode.sequenceNumber[1] === 0xFFFFFFFF) {
2409
+ mode.sequenceNumber[1] = 0;
2410
+ ++mode.sequenceNumber[0];
2411
+ } else {
2412
+ ++mode.sequenceNumber[1];
2413
+ }
2414
+ }
2415
+ };
2416
+ return mode;
2417
+ };
2418
+ var state = {
2419
+ read: createMode(),
2420
+ write: createMode()
2421
+ };
2422
+
2423
+ // update function in read mode will decrypt then decompress a record
2424
+ state.read.update = function (c, record) {
2425
+ if (!state.read.cipherFunction(record, state.read)) {
2426
+ c.error(c, {
2427
+ message: 'Could not decrypt record or bad MAC.',
2428
+ send: true,
2429
+ alert: {
2430
+ level: tls.Alert.Level.fatal,
2431
+ // doesn't matter if decryption failed or MAC was
2432
+ // invalid, return the same error so as not to reveal
2433
+ // which one occurred
2434
+ description: tls.Alert.Description.bad_record_mac
2435
+ }
2436
+ });
2437
+ } else if (!state.read.compressFunction(c, record, state.read)) {
2438
+ c.error(c, {
2439
+ message: 'Could not decompress record.',
2440
+ send: true,
2441
+ alert: {
2442
+ level: tls.Alert.Level.fatal,
2443
+ description: tls.Alert.Description.decompression_failure
2444
+ }
2445
+ });
2446
+ }
2447
+ return !c.fail;
2448
+ };
2449
+
2450
+ // update function in write mode will compress then encrypt a record
2451
+ state.write.update = function (c, record) {
2452
+ if (!state.write.compressFunction(c, record, state.write)) {
2453
+ // error, but do not send alert since it would require
2454
+ // compression as well
2455
+ c.error(c, {
2456
+ message: 'Could not compress record.',
2457
+ send: false,
2458
+ alert: {
2459
+ level: tls.Alert.Level.fatal,
2460
+ description: tls.Alert.Description.internal_error
2461
+ }
2462
+ });
2463
+ } else if (!state.write.cipherFunction(record, state.write)) {
2464
+ // error, but do not send alert since it would require
2465
+ // encryption as well
2466
+ c.error(c, {
2467
+ message: 'Could not encrypt record.',
2468
+ send: false,
2469
+ alert: {
2470
+ level: tls.Alert.Level.fatal,
2471
+ description: tls.Alert.Description.internal_error
2472
+ }
2473
+ });
2474
+ }
2475
+ return !c.fail;
2476
+ };
2477
+
2478
+ // handle security parameters
2479
+ if (c.session) {
2480
+ var sp = c.session.sp;
2481
+ c.session.cipherSuite.initSecurityParameters(sp);
2482
+
2483
+ // generate keys
2484
+ sp.keys = tls.generateKeys(c, sp);
2485
+ state.read.macKey = client ? sp.keys.server_write_MAC_key : sp.keys.client_write_MAC_key;
2486
+ state.write.macKey = client ? sp.keys.client_write_MAC_key : sp.keys.server_write_MAC_key;
2487
+
2488
+ // cipher suite setup
2489
+ c.session.cipherSuite.initConnectionState(state, c, sp);
2490
+
2491
+ // compression setup
2492
+ switch (sp.compression_algorithm) {
2493
+ case tls.CompressionMethod.none:
2494
+ break;
2495
+ case tls.CompressionMethod.deflate:
2496
+ state.read.compressFunction = inflate;
2497
+ state.write.compressFunction = deflate;
2498
+ break;
2499
+ default:
2500
+ throw new Error('Unsupported compression algorithm.');
2501
+ }
2502
+ }
2503
+ return state;
2504
+ };
2505
+
2506
+ /**
2507
+ * Creates a Random structure.
2508
+ *
2509
+ * struct {
2510
+ * uint32 gmt_unix_time;
2511
+ * opaque random_bytes[28];
2512
+ * } Random;
2513
+ *
2514
+ * gmt_unix_time:
2515
+ * The current time and date in standard UNIX 32-bit format (seconds since
2516
+ * the midnight starting Jan 1, 1970, UTC, ignoring leap seconds) according
2517
+ * to the sender's internal clock. Clocks are not required to be set
2518
+ * correctly by the basic TLS protocol; higher-level or application
2519
+ * protocols may define additional requirements. Note that, for historical
2520
+ * reasons, the data element is named using GMT, the predecessor of the
2521
+ * current worldwide time base, UTC.
2522
+ * random_bytes:
2523
+ * 28 bytes generated by a secure random number generator.
2524
+ *
2525
+ * @return the Random structure as a byte array.
2526
+ */
2527
+ tls.createRandom = function () {
2528
+ // get UTC milliseconds
2529
+ var d = new Date();
2530
+ var utc = +d + d.getTimezoneOffset() * 60000;
2531
+ var rval = forge.util.createBuffer();
2532
+ rval.putInt32(utc);
2533
+ rval.putBytes(forge.random.getBytes(28));
2534
+ return rval;
2535
+ };
2536
+
2537
+ /**
2538
+ * Creates a TLS record with the given type and data.
2539
+ *
2540
+ * @param c the connection.
2541
+ * @param options:
2542
+ * type: the record type.
2543
+ * data: the plain text data in a byte buffer.
2544
+ *
2545
+ * @return the created record.
2546
+ */
2547
+ tls.createRecord = function (c, options) {
2548
+ if (!options.data) {
2549
+ return null;
2550
+ }
2551
+ var record = {
2552
+ type: options.type,
2553
+ version: {
2554
+ major: c.version.major,
2555
+ minor: c.version.minor
2556
+ },
2557
+ length: options.data.length(),
2558
+ fragment: options.data
2559
+ };
2560
+ return record;
2561
+ };
2562
+
2563
+ /**
2564
+ * Creates a TLS alert record.
2565
+ *
2566
+ * @param c the connection.
2567
+ * @param alert:
2568
+ * level: the TLS alert level.
2569
+ * description: the TLS alert description.
2570
+ *
2571
+ * @return the created alert record.
2572
+ */
2573
+ tls.createAlert = function (c, alert) {
2574
+ var b = forge.util.createBuffer();
2575
+ b.putByte(alert.level);
2576
+ b.putByte(alert.description);
2577
+ return tls.createRecord(c, {
2578
+ type: tls.ContentType.alert,
2579
+ data: b
2580
+ });
2581
+ };
2582
+
2583
+ /* The structure of a TLS handshake message.
2584
+ *
2585
+ * struct {
2586
+ * HandshakeType msg_type; // handshake type
2587
+ * uint24 length; // bytes in message
2588
+ * select(HandshakeType) {
2589
+ * case hello_request: HelloRequest;
2590
+ * case client_hello: ClientHello;
2591
+ * case server_hello: ServerHello;
2592
+ * case certificate: Certificate;
2593
+ * case server_key_exchange: ServerKeyExchange;
2594
+ * case certificate_request: CertificateRequest;
2595
+ * case server_hello_done: ServerHelloDone;
2596
+ * case certificate_verify: CertificateVerify;
2597
+ * case client_key_exchange: ClientKeyExchange;
2598
+ * case finished: Finished;
2599
+ * } body;
2600
+ * } Handshake;
2601
+ */
2602
+
2603
+ /**
2604
+ * Creates a ClientHello message.
2605
+ *
2606
+ * opaque SessionID<0..32>;
2607
+ * enum { null(0), deflate(1), (255) } CompressionMethod;
2608
+ * uint8 CipherSuite[2];
2609
+ *
2610
+ * struct {
2611
+ * ProtocolVersion client_version;
2612
+ * Random random;
2613
+ * SessionID session_id;
2614
+ * CipherSuite cipher_suites<2..2^16-2>;
2615
+ * CompressionMethod compression_methods<1..2^8-1>;
2616
+ * select(extensions_present) {
2617
+ * case false:
2618
+ * struct {};
2619
+ * case true:
2620
+ * Extension extensions<0..2^16-1>;
2621
+ * };
2622
+ * } ClientHello;
2623
+ *
2624
+ * The extension format for extended client hellos and server hellos is:
2625
+ *
2626
+ * struct {
2627
+ * ExtensionType extension_type;
2628
+ * opaque extension_data<0..2^16-1>;
2629
+ * } Extension;
2630
+ *
2631
+ * Here:
2632
+ *
2633
+ * - "extension_type" identifies the particular extension type.
2634
+ * - "extension_data" contains information specific to the particular
2635
+ * extension type.
2636
+ *
2637
+ * The extension types defined in this document are:
2638
+ *
2639
+ * enum {
2640
+ * server_name(0), max_fragment_length(1),
2641
+ * client_certificate_url(2), trusted_ca_keys(3),
2642
+ * truncated_hmac(4), status_request(5), (65535)
2643
+ * } ExtensionType;
2644
+ *
2645
+ * @param c the connection.
2646
+ *
2647
+ * @return the ClientHello byte buffer.
2648
+ */
2649
+ tls.createClientHello = function (c) {
2650
+ // save hello version
2651
+ c.session.clientHelloVersion = {
2652
+ major: c.version.major,
2653
+ minor: c.version.minor
2654
+ };
2655
+
2656
+ // create supported cipher suites
2657
+ var cipherSuites = forge.util.createBuffer();
2658
+ for (var i = 0; i < c.cipherSuites.length; ++i) {
2659
+ var cs = c.cipherSuites[i];
2660
+ cipherSuites.putByte(cs.id[0]);
2661
+ cipherSuites.putByte(cs.id[1]);
2662
+ }
2663
+ var cSuites = cipherSuites.length();
2664
+
2665
+ // create supported compression methods, null always supported, but
2666
+ // also support deflate if connection has inflate and deflate methods
2667
+ var compressionMethods = forge.util.createBuffer();
2668
+ compressionMethods.putByte(tls.CompressionMethod.none);
2669
+ // FIXME: deflate support disabled until issues with raw deflate data
2670
+ // without zlib headers are resolved
2671
+ /*
2672
+ if(c.inflate !== null && c.deflate !== null) {
2673
+ compressionMethods.putByte(tls.CompressionMethod.deflate);
2674
+ }
2675
+ */
2676
+ var cMethods = compressionMethods.length();
2677
+
2678
+ // create TLS SNI (server name indication) extension if virtual host
2679
+ // has been specified, see RFC 3546
2680
+ var extensions = forge.util.createBuffer();
2681
+ if (c.virtualHost) {
2682
+ // create extension struct
2683
+ var ext = forge.util.createBuffer();
2684
+ ext.putByte(0x00); // type server_name (ExtensionType is 2 bytes)
2685
+ ext.putByte(0x00);
2686
+
2687
+ /* In order to provide the server name, clients MAY include an
2688
+ * extension of type "server_name" in the (extended) client hello.
2689
+ * The "extension_data" field of this extension SHALL contain
2690
+ * "ServerNameList" where:
2691
+ *
2692
+ * struct {
2693
+ * NameType name_type;
2694
+ * select(name_type) {
2695
+ * case host_name: HostName;
2696
+ * } name;
2697
+ * } ServerName;
2698
+ *
2699
+ * enum {
2700
+ * host_name(0), (255)
2701
+ * } NameType;
2702
+ *
2703
+ * opaque HostName<1..2^16-1>;
2704
+ *
2705
+ * struct {
2706
+ * ServerName server_name_list<1..2^16-1>
2707
+ * } ServerNameList;
2708
+ */
2709
+ var serverName = forge.util.createBuffer();
2710
+ serverName.putByte(0x00); // type host_name
2711
+ writeVector(serverName, 2, forge.util.createBuffer(c.virtualHost));
2712
+
2713
+ // ServerNameList is in extension_data
2714
+ var snList = forge.util.createBuffer();
2715
+ writeVector(snList, 2, serverName);
2716
+ writeVector(ext, 2, snList);
2717
+ extensions.putBuffer(ext);
2718
+ }
2719
+ var extLength = extensions.length();
2720
+ if (extLength > 0) {
2721
+ // add extension vector length
2722
+ extLength += 2;
2723
+ }
2724
+
2725
+ // determine length of the handshake message
2726
+ // cipher suites and compression methods size will need to be
2727
+ // updated if more get added to the list
2728
+ var sessionId = c.session.id;
2729
+ var length = sessionId.length + 1 +
2730
+ // session ID vector
2731
+ 2 +
2732
+ // version (major + minor)
2733
+ 4 + 28 +
2734
+ // random time and random bytes
2735
+ 2 + cSuites +
2736
+ // cipher suites vector
2737
+ 1 + cMethods +
2738
+ // compression methods vector
2739
+ extLength; // extensions vector
2740
+
2741
+ // build record fragment
2742
+ var rval = forge.util.createBuffer();
2743
+ rval.putByte(tls.HandshakeType.client_hello);
2744
+ rval.putInt24(length); // handshake length
2745
+ rval.putByte(c.version.major); // major version
2746
+ rval.putByte(c.version.minor); // minor version
2747
+ rval.putBytes(c.session.sp.client_random); // random time + bytes
2748
+ writeVector(rval, 1, forge.util.createBuffer(sessionId));
2749
+ writeVector(rval, 2, cipherSuites);
2750
+ writeVector(rval, 1, compressionMethods);
2751
+ if (extLength > 0) {
2752
+ writeVector(rval, 2, extensions);
2753
+ }
2754
+ return rval;
2755
+ };
2756
+
2757
+ /**
2758
+ * Creates a ServerHello message.
2759
+ *
2760
+ * @param c the connection.
2761
+ *
2762
+ * @return the ServerHello byte buffer.
2763
+ */
2764
+ tls.createServerHello = function (c) {
2765
+ // determine length of the handshake message
2766
+ var sessionId = c.session.id;
2767
+ var length = sessionId.length + 1 +
2768
+ // session ID vector
2769
+ 2 +
2770
+ // version (major + minor)
2771
+ 4 + 28 +
2772
+ // random time and random bytes
2773
+ 2 +
2774
+ // chosen cipher suite
2775
+ 1; // chosen compression method
2776
+
2777
+ // build record fragment
2778
+ var rval = forge.util.createBuffer();
2779
+ rval.putByte(tls.HandshakeType.server_hello);
2780
+ rval.putInt24(length); // handshake length
2781
+ rval.putByte(c.version.major); // major version
2782
+ rval.putByte(c.version.minor); // minor version
2783
+ rval.putBytes(c.session.sp.server_random); // random time + bytes
2784
+ writeVector(rval, 1, forge.util.createBuffer(sessionId));
2785
+ rval.putByte(c.session.cipherSuite.id[0]);
2786
+ rval.putByte(c.session.cipherSuite.id[1]);
2787
+ rval.putByte(c.session.compressionMethod);
2788
+ return rval;
2789
+ };
2790
+
2791
+ /**
2792
+ * Creates a Certificate message.
2793
+ *
2794
+ * When this message will be sent:
2795
+ * This is the first message the client can send after receiving a server
2796
+ * hello done message and the first message the server can send after
2797
+ * sending a ServerHello. This client message is only sent if the server
2798
+ * requests a certificate. If no suitable certificate is available, the
2799
+ * client should send a certificate message containing no certificates. If
2800
+ * client authentication is required by the server for the handshake to
2801
+ * continue, it may respond with a fatal handshake failure alert.
2802
+ *
2803
+ * opaque ASN.1Cert<1..2^24-1>;
2804
+ *
2805
+ * struct {
2806
+ * ASN.1Cert certificate_list<0..2^24-1>;
2807
+ * } Certificate;
2808
+ *
2809
+ * @param c the connection.
2810
+ *
2811
+ * @return the Certificate byte buffer.
2812
+ */
2813
+ tls.createCertificate = function (c) {
2814
+ // TODO: check certificate request to ensure types are supported
2815
+
2816
+ // get a certificate (a certificate as a PEM string)
2817
+ var client = c.entity === tls.ConnectionEnd.client;
2818
+ var cert = null;
2819
+ if (c.getCertificate) {
2820
+ var hint;
2821
+ if (client) {
2822
+ hint = c.session.certificateRequest;
2823
+ } else {
2824
+ hint = c.session.extensions.server_name.serverNameList;
2825
+ }
2826
+ cert = c.getCertificate(c, hint);
2827
+ }
2828
+
2829
+ // buffer to hold certificate list
2830
+ var certList = forge.util.createBuffer();
2831
+ if (cert !== null) {
2832
+ try {
2833
+ // normalize cert to a chain of certificates
2834
+ if (!forge.util.isArray(cert)) {
2835
+ cert = [cert];
2836
+ }
2837
+ var asn1 = null;
2838
+ for (var i = 0; i < cert.length; ++i) {
2839
+ var msg = forge.pem.decode(cert[i])[0];
2840
+ if (msg.type !== 'CERTIFICATE' && msg.type !== 'X509 CERTIFICATE' && msg.type !== 'TRUSTED CERTIFICATE') {
2841
+ var error = new Error('Could not convert certificate from PEM; PEM ' + 'header type is not "CERTIFICATE", "X509 CERTIFICATE", or ' + '"TRUSTED CERTIFICATE".');
2842
+ error.headerType = msg.type;
2843
+ throw error;
2844
+ }
2845
+ if (msg.procType && msg.procType.type === 'ENCRYPTED') {
2846
+ throw new Error('Could not convert certificate from PEM; PEM is encrypted.');
2847
+ }
2848
+ var der = forge.util.createBuffer(msg.body);
2849
+ if (asn1 === null) {
2850
+ asn1 = forge.asn1.fromDer(der.bytes(), false);
2851
+ }
2852
+
2853
+ // certificate entry is itself a vector with 3 length bytes
2854
+ var certBuffer = forge.util.createBuffer();
2855
+ writeVector(certBuffer, 3, der);
2856
+
2857
+ // add cert vector to cert list vector
2858
+ certList.putBuffer(certBuffer);
2859
+ }
2860
+
2861
+ // save certificate
2862
+ cert = forge.pki.certificateFromAsn1(asn1);
2863
+ if (client) {
2864
+ c.session.clientCertificate = cert;
2865
+ } else {
2866
+ c.session.serverCertificate = cert;
2867
+ }
2868
+ } catch (ex) {
2869
+ return c.error(c, {
2870
+ message: 'Could not send certificate list.',
2871
+ cause: ex,
2872
+ send: true,
2873
+ alert: {
2874
+ level: tls.Alert.Level.fatal,
2875
+ description: tls.Alert.Description.bad_certificate
2876
+ }
2877
+ });
2878
+ }
2879
+ }
2880
+
2881
+ // determine length of the handshake message
2882
+ var length = 3 + certList.length(); // cert list vector
2883
+
2884
+ // build record fragment
2885
+ var rval = forge.util.createBuffer();
2886
+ rval.putByte(tls.HandshakeType.certificate);
2887
+ rval.putInt24(length);
2888
+ writeVector(rval, 3, certList);
2889
+ return rval;
2890
+ };
2891
+
2892
+ /**
2893
+ * Creates a ClientKeyExchange message.
2894
+ *
2895
+ * When this message will be sent:
2896
+ * This message is always sent by the client. It will immediately follow the
2897
+ * client certificate message, if it is sent. Otherwise it will be the first
2898
+ * message sent by the client after it receives the server hello done
2899
+ * message.
2900
+ *
2901
+ * Meaning of this message:
2902
+ * With this message, the premaster secret is set, either though direct
2903
+ * transmission of the RSA-encrypted secret, or by the transmission of
2904
+ * Diffie-Hellman parameters which will allow each side to agree upon the
2905
+ * same premaster secret. When the key exchange method is DH_RSA or DH_DSS,
2906
+ * client certification has been requested, and the client was able to
2907
+ * respond with a certificate which contained a Diffie-Hellman public key
2908
+ * whose parameters (group and generator) matched those specified by the
2909
+ * server in its certificate, this message will not contain any data.
2910
+ *
2911
+ * Meaning of this message:
2912
+ * If RSA is being used for key agreement and authentication, the client
2913
+ * generates a 48-byte premaster secret, encrypts it using the public key
2914
+ * from the server's certificate or the temporary RSA key provided in a
2915
+ * server key exchange message, and sends the result in an encrypted
2916
+ * premaster secret message. This structure is a variant of the client
2917
+ * key exchange message, not a message in itself.
2918
+ *
2919
+ * struct {
2920
+ * select(KeyExchangeAlgorithm) {
2921
+ * case rsa: EncryptedPreMasterSecret;
2922
+ * case diffie_hellman: ClientDiffieHellmanPublic;
2923
+ * } exchange_keys;
2924
+ * } ClientKeyExchange;
2925
+ *
2926
+ * struct {
2927
+ * ProtocolVersion client_version;
2928
+ * opaque random[46];
2929
+ * } PreMasterSecret;
2930
+ *
2931
+ * struct {
2932
+ * public-key-encrypted PreMasterSecret pre_master_secret;
2933
+ * } EncryptedPreMasterSecret;
2934
+ *
2935
+ * A public-key-encrypted element is encoded as a vector <0..2^16-1>.
2936
+ *
2937
+ * @param c the connection.
2938
+ *
2939
+ * @return the ClientKeyExchange byte buffer.
2940
+ */
2941
+ tls.createClientKeyExchange = function (c) {
2942
+ // create buffer to encrypt
2943
+ var b = forge.util.createBuffer();
2944
+
2945
+ // add highest client-supported protocol to help server avoid version
2946
+ // rollback attacks
2947
+ b.putByte(c.session.clientHelloVersion.major);
2948
+ b.putByte(c.session.clientHelloVersion.minor);
2949
+
2950
+ // generate and add 46 random bytes
2951
+ b.putBytes(forge.random.getBytes(46));
2952
+
2953
+ // save pre-master secret
2954
+ var sp = c.session.sp;
2955
+ sp.pre_master_secret = b.getBytes();
2956
+
2957
+ // RSA-encrypt the pre-master secret
2958
+ var key = c.session.serverCertificate.publicKey;
2959
+ b = key.encrypt(sp.pre_master_secret);
2960
+
2961
+ /* Note: The encrypted pre-master secret will be stored in a
2962
+ public-key-encrypted opaque vector that has the length prefixed using
2963
+ 2 bytes, so include those 2 bytes in the handshake message length. This
2964
+ is done as a minor optimization instead of calling writeVector(). */
2965
+
2966
+ // determine length of the handshake message
2967
+ var length = b.length + 2;
2968
+
2969
+ // build record fragment
2970
+ var rval = forge.util.createBuffer();
2971
+ rval.putByte(tls.HandshakeType.client_key_exchange);
2972
+ rval.putInt24(length);
2973
+ // add vector length bytes
2974
+ rval.putInt16(b.length);
2975
+ rval.putBytes(b);
2976
+ return rval;
2977
+ };
2978
+
2979
+ /**
2980
+ * Creates a ServerKeyExchange message.
2981
+ *
2982
+ * @param c the connection.
2983
+ *
2984
+ * @return the ServerKeyExchange byte buffer.
2985
+ */
2986
+ tls.createServerKeyExchange = function (c) {
2987
+
2988
+ // build record fragment
2989
+ var rval = forge.util.createBuffer();
2990
+ return rval;
2991
+ };
2992
+
2993
+ /**
2994
+ * Gets the signed data used to verify a client-side certificate. See
2995
+ * tls.createCertificateVerify() for details.
2996
+ *
2997
+ * @param c the connection.
2998
+ * @param callback the callback to call once the signed data is ready.
2999
+ */
3000
+ tls.getClientSignature = function (c, callback) {
3001
+ // generate data to RSA encrypt
3002
+ var b = forge.util.createBuffer();
3003
+ b.putBuffer(c.session.md5.digest());
3004
+ b.putBuffer(c.session.sha1.digest());
3005
+ b = b.getBytes();
3006
+
3007
+ // create default signing function as necessary
3008
+ c.getSignature = c.getSignature || function (c, b, callback) {
3009
+ // do rsa encryption, call callback
3010
+ var privateKey = null;
3011
+ if (c.getPrivateKey) {
3012
+ try {
3013
+ privateKey = c.getPrivateKey(c, c.session.clientCertificate);
3014
+ privateKey = forge.pki.privateKeyFromPem(privateKey);
3015
+ } catch (ex) {
3016
+ c.error(c, {
3017
+ message: 'Could not get private key.',
3018
+ cause: ex,
3019
+ send: true,
3020
+ alert: {
3021
+ level: tls.Alert.Level.fatal,
3022
+ description: tls.Alert.Description.internal_error
3023
+ }
3024
+ });
3025
+ }
3026
+ }
3027
+ if (privateKey === null) {
3028
+ c.error(c, {
3029
+ message: 'No private key set.',
3030
+ send: true,
3031
+ alert: {
3032
+ level: tls.Alert.Level.fatal,
3033
+ description: tls.Alert.Description.internal_error
3034
+ }
3035
+ });
3036
+ } else {
3037
+ b = privateKey.sign(b, null);
3038
+ }
3039
+ callback(c, b);
3040
+ };
3041
+
3042
+ // get client signature
3043
+ c.getSignature(c, b, callback);
3044
+ };
3045
+
3046
+ /**
3047
+ * Creates a CertificateVerify message.
3048
+ *
3049
+ * Meaning of this message:
3050
+ * This structure conveys the client's Diffie-Hellman public value
3051
+ * (Yc) if it was not already included in the client's certificate.
3052
+ * The encoding used for Yc is determined by the enumerated
3053
+ * PublicValueEncoding. This structure is a variant of the client
3054
+ * key exchange message, not a message in itself.
3055
+ *
3056
+ * When this message will be sent:
3057
+ * This message is used to provide explicit verification of a client
3058
+ * certificate. This message is only sent following a client
3059
+ * certificate that has signing capability (i.e. all certificates
3060
+ * except those containing fixed Diffie-Hellman parameters). When
3061
+ * sent, it will immediately follow the client key exchange message.
3062
+ *
3063
+ * struct {
3064
+ * Signature signature;
3065
+ * } CertificateVerify;
3066
+ *
3067
+ * CertificateVerify.signature.md5_hash
3068
+ * MD5(handshake_messages);
3069
+ *
3070
+ * Certificate.signature.sha_hash
3071
+ * SHA(handshake_messages);
3072
+ *
3073
+ * Here handshake_messages refers to all handshake messages sent or
3074
+ * received starting at client hello up to but not including this
3075
+ * message, including the type and length fields of the handshake
3076
+ * messages.
3077
+ *
3078
+ * select(SignatureAlgorithm) {
3079
+ * case anonymous: struct { };
3080
+ * case rsa:
3081
+ * digitally-signed struct {
3082
+ * opaque md5_hash[16];
3083
+ * opaque sha_hash[20];
3084
+ * };
3085
+ * case dsa:
3086
+ * digitally-signed struct {
3087
+ * opaque sha_hash[20];
3088
+ * };
3089
+ * } Signature;
3090
+ *
3091
+ * In digital signing, one-way hash functions are used as input for a
3092
+ * signing algorithm. A digitally-signed element is encoded as an opaque
3093
+ * vector <0..2^16-1>, where the length is specified by the signing
3094
+ * algorithm and key.
3095
+ *
3096
+ * In RSA signing, a 36-byte structure of two hashes (one SHA and one
3097
+ * MD5) is signed (encrypted with the private key). It is encoded with
3098
+ * PKCS #1 block type 0 or type 1 as described in [PKCS1].
3099
+ *
3100
+ * In DSS, the 20 bytes of the SHA hash are run directly through the
3101
+ * Digital Signing Algorithm with no additional hashing.
3102
+ *
3103
+ * @param c the connection.
3104
+ * @param signature the signature to include in the message.
3105
+ *
3106
+ * @return the CertificateVerify byte buffer.
3107
+ */
3108
+ tls.createCertificateVerify = function (c, signature) {
3109
+ /* Note: The signature will be stored in a "digitally-signed" opaque
3110
+ vector that has the length prefixed using 2 bytes, so include those
3111
+ 2 bytes in the handshake message length. This is done as a minor
3112
+ optimization instead of calling writeVector(). */
3113
+
3114
+ // determine length of the handshake message
3115
+ var length = signature.length + 2;
3116
+
3117
+ // build record fragment
3118
+ var rval = forge.util.createBuffer();
3119
+ rval.putByte(tls.HandshakeType.certificate_verify);
3120
+ rval.putInt24(length);
3121
+ // add vector length bytes
3122
+ rval.putInt16(signature.length);
3123
+ rval.putBytes(signature);
3124
+ return rval;
3125
+ };
3126
+
3127
+ /**
3128
+ * Creates a CertificateRequest message.
3129
+ *
3130
+ * @param c the connection.
3131
+ *
3132
+ * @return the CertificateRequest byte buffer.
3133
+ */
3134
+ tls.createCertificateRequest = function (c) {
3135
+ // TODO: support other certificate types
3136
+ var certTypes = forge.util.createBuffer();
3137
+
3138
+ // common RSA certificate type
3139
+ certTypes.putByte(0x01);
3140
+
3141
+ // add distinguished names from CA store
3142
+ var cAs = forge.util.createBuffer();
3143
+ for (var key in c.caStore.certs) {
3144
+ var cert = c.caStore.certs[key];
3145
+ var dn = forge.pki.distinguishedNameToAsn1(cert.subject);
3146
+ var byteBuffer = forge.asn1.toDer(dn);
3147
+ cAs.putInt16(byteBuffer.length());
3148
+ cAs.putBuffer(byteBuffer);
3149
+ }
3150
+
3151
+ // TODO: TLS 1.2+ has a different format
3152
+
3153
+ // determine length of the handshake message
3154
+ var length = 1 + certTypes.length() + 2 + cAs.length();
3155
+
3156
+ // build record fragment
3157
+ var rval = forge.util.createBuffer();
3158
+ rval.putByte(tls.HandshakeType.certificate_request);
3159
+ rval.putInt24(length);
3160
+ writeVector(rval, 1, certTypes);
3161
+ writeVector(rval, 2, cAs);
3162
+ return rval;
3163
+ };
3164
+
3165
+ /**
3166
+ * Creates a ServerHelloDone message.
3167
+ *
3168
+ * @param c the connection.
3169
+ *
3170
+ * @return the ServerHelloDone byte buffer.
3171
+ */
3172
+ tls.createServerHelloDone = function (c) {
3173
+ // build record fragment
3174
+ var rval = forge.util.createBuffer();
3175
+ rval.putByte(tls.HandshakeType.server_hello_done);
3176
+ rval.putInt24(0);
3177
+ return rval;
3178
+ };
3179
+
3180
+ /**
3181
+ * Creates a ChangeCipherSpec message.
3182
+ *
3183
+ * The change cipher spec protocol exists to signal transitions in
3184
+ * ciphering strategies. The protocol consists of a single message,
3185
+ * which is encrypted and compressed under the current (not the pending)
3186
+ * connection state. The message consists of a single byte of value 1.
3187
+ *
3188
+ * struct {
3189
+ * enum { change_cipher_spec(1), (255) } type;
3190
+ * } ChangeCipherSpec;
3191
+ *
3192
+ * @return the ChangeCipherSpec byte buffer.
3193
+ */
3194
+ tls.createChangeCipherSpec = function () {
3195
+ var rval = forge.util.createBuffer();
3196
+ rval.putByte(0x01);
3197
+ return rval;
3198
+ };
3199
+
3200
+ /**
3201
+ * Creates a Finished message.
3202
+ *
3203
+ * struct {
3204
+ * opaque verify_data[12];
3205
+ * } Finished;
3206
+ *
3207
+ * verify_data
3208
+ * PRF(master_secret, finished_label, MD5(handshake_messages) +
3209
+ * SHA-1(handshake_messages)) [0..11];
3210
+ *
3211
+ * finished_label
3212
+ * For Finished messages sent by the client, the string "client
3213
+ * finished". For Finished messages sent by the server, the
3214
+ * string "server finished".
3215
+ *
3216
+ * handshake_messages
3217
+ * All of the data from all handshake messages up to but not
3218
+ * including this message. This is only data visible at the
3219
+ * handshake layer and does not include record layer headers.
3220
+ * This is the concatenation of all the Handshake structures as
3221
+ * defined in 7.4 exchanged thus far.
3222
+ *
3223
+ * @param c the connection.
3224
+ *
3225
+ * @return the Finished byte buffer.
3226
+ */
3227
+ tls.createFinished = function (c) {
3228
+ // generate verify_data
3229
+ var b = forge.util.createBuffer();
3230
+ b.putBuffer(c.session.md5.digest());
3231
+ b.putBuffer(c.session.sha1.digest());
3232
+
3233
+ // TODO: determine prf function and verify length for TLS 1.2
3234
+ var client = c.entity === tls.ConnectionEnd.client;
3235
+ var sp = c.session.sp;
3236
+ var vdl = 12;
3237
+ var prf = prf_TLS1;
3238
+ var label = client ? 'client finished' : 'server finished';
3239
+ b = prf(sp.master_secret, label, b.getBytes(), vdl);
3240
+
3241
+ // build record fragment
3242
+ var rval = forge.util.createBuffer();
3243
+ rval.putByte(tls.HandshakeType.finished);
3244
+ rval.putInt24(b.length());
3245
+ rval.putBuffer(b);
3246
+ return rval;
3247
+ };
3248
+
3249
+ /**
3250
+ * Creates a HeartbeatMessage (See RFC 6520).
3251
+ *
3252
+ * struct {
3253
+ * HeartbeatMessageType type;
3254
+ * uint16 payload_length;
3255
+ * opaque payload[HeartbeatMessage.payload_length];
3256
+ * opaque padding[padding_length];
3257
+ * } HeartbeatMessage;
3258
+ *
3259
+ * The total length of a HeartbeatMessage MUST NOT exceed 2^14 or
3260
+ * max_fragment_length when negotiated as defined in [RFC6066].
3261
+ *
3262
+ * type: The message type, either heartbeat_request or heartbeat_response.
3263
+ *
3264
+ * payload_length: The length of the payload.
3265
+ *
3266
+ * payload: The payload consists of arbitrary content.
3267
+ *
3268
+ * padding: The padding is random content that MUST be ignored by the
3269
+ * receiver. The length of a HeartbeatMessage is TLSPlaintext.length
3270
+ * for TLS and DTLSPlaintext.length for DTLS. Furthermore, the
3271
+ * length of the type field is 1 byte, and the length of the
3272
+ * payload_length is 2. Therefore, the padding_length is
3273
+ * TLSPlaintext.length - payload_length - 3 for TLS and
3274
+ * DTLSPlaintext.length - payload_length - 3 for DTLS. The
3275
+ * padding_length MUST be at least 16.
3276
+ *
3277
+ * The sender of a HeartbeatMessage MUST use a random padding of at
3278
+ * least 16 bytes. The padding of a received HeartbeatMessage message
3279
+ * MUST be ignored.
3280
+ *
3281
+ * If the payload_length of a received HeartbeatMessage is too large,
3282
+ * the received HeartbeatMessage MUST be discarded silently.
3283
+ *
3284
+ * @param c the connection.
3285
+ * @param type the tls.HeartbeatMessageType.
3286
+ * @param payload the heartbeat data to send as the payload.
3287
+ * @param [payloadLength] the payload length to use, defaults to the
3288
+ * actual payload length.
3289
+ *
3290
+ * @return the HeartbeatRequest byte buffer.
3291
+ */
3292
+ tls.createHeartbeat = function (type, payload, payloadLength) {
3293
+ if (typeof payloadLength === 'undefined') {
3294
+ payloadLength = payload.length;
3295
+ }
3296
+ // build record fragment
3297
+ var rval = forge.util.createBuffer();
3298
+ rval.putByte(type); // heartbeat message type
3299
+ rval.putInt16(payloadLength); // payload length
3300
+ rval.putBytes(payload); // payload
3301
+ // padding
3302
+ var plaintextLength = rval.length();
3303
+ var paddingLength = Math.max(16, plaintextLength - payloadLength - 3);
3304
+ rval.putBytes(forge.random.getBytes(paddingLength));
3305
+ return rval;
3306
+ };
3307
+
3308
+ /**
3309
+ * Fragments, compresses, encrypts, and queues a record for delivery.
3310
+ *
3311
+ * @param c the connection.
3312
+ * @param record the record to queue.
3313
+ */
3314
+ tls.queue = function (c, record) {
3315
+ // error during record creation
3316
+ if (!record) {
3317
+ return;
3318
+ }
3319
+ if (record.fragment.length() === 0) {
3320
+ if (record.type === tls.ContentType.handshake || record.type === tls.ContentType.alert || record.type === tls.ContentType.change_cipher_spec) {
3321
+ // Empty handshake, alert of change cipher spec messages are not allowed per the TLS specification and should not be sent.
3322
+ return;
3323
+ }
3324
+ }
3325
+
3326
+ // if the record is a handshake record, update handshake hashes
3327
+ if (record.type === tls.ContentType.handshake) {
3328
+ var bytes = record.fragment.bytes();
3329
+ c.session.md5.update(bytes);
3330
+ c.session.sha1.update(bytes);
3331
+ bytes = null;
3332
+ }
3333
+
3334
+ // handle record fragmentation
3335
+ var records;
3336
+ if (record.fragment.length() <= tls.MaxFragment) {
3337
+ records = [record];
3338
+ } else {
3339
+ // fragment data as long as it is too long
3340
+ records = [];
3341
+ var data = record.fragment.bytes();
3342
+ while (data.length > tls.MaxFragment) {
3343
+ records.push(tls.createRecord(c, {
3344
+ type: record.type,
3345
+ data: forge.util.createBuffer(data.slice(0, tls.MaxFragment))
3346
+ }));
3347
+ data = data.slice(tls.MaxFragment);
3348
+ }
3349
+ // add last record
3350
+ if (data.length > 0) {
3351
+ records.push(tls.createRecord(c, {
3352
+ type: record.type,
3353
+ data: forge.util.createBuffer(data)
3354
+ }));
3355
+ }
3356
+ }
3357
+
3358
+ // compress and encrypt all fragmented records
3359
+ for (var i = 0; i < records.length && !c.fail; ++i) {
3360
+ // update the record using current write state
3361
+ var rec = records[i];
3362
+ var s = c.state.current.write;
3363
+ if (s.update(c, rec)) {
3364
+ // store record
3365
+ c.records.push(rec);
3366
+ }
3367
+ }
3368
+ };
3369
+
3370
+ /**
3371
+ * Flushes all queued records to the output buffer and calls the
3372
+ * tlsDataReady() handler on the given connection.
3373
+ *
3374
+ * @param c the connection.
3375
+ *
3376
+ * @return true on success, false on failure.
3377
+ */
3378
+ tls.flush = function (c) {
3379
+ for (var i = 0; i < c.records.length; ++i) {
3380
+ var record = c.records[i];
3381
+
3382
+ // add record header and fragment
3383
+ c.tlsData.putByte(record.type);
3384
+ c.tlsData.putByte(record.version.major);
3385
+ c.tlsData.putByte(record.version.minor);
3386
+ c.tlsData.putInt16(record.fragment.length());
3387
+ c.tlsData.putBuffer(c.records[i].fragment);
3388
+ }
3389
+ c.records = [];
3390
+ return c.tlsDataReady(c);
3391
+ };
3392
+
3393
+ /**
3394
+ * Maps a pki.certificateError to a tls.Alert.Description.
3395
+ *
3396
+ * @param error the error to map.
3397
+ *
3398
+ * @return the alert description.
3399
+ */
3400
+ var _certErrorToAlertDesc = function (error) {
3401
+ switch (error) {
3402
+ case true:
3403
+ return true;
3404
+ case forge.pki.certificateError.bad_certificate:
3405
+ return tls.Alert.Description.bad_certificate;
3406
+ case forge.pki.certificateError.unsupported_certificate:
3407
+ return tls.Alert.Description.unsupported_certificate;
3408
+ case forge.pki.certificateError.certificate_revoked:
3409
+ return tls.Alert.Description.certificate_revoked;
3410
+ case forge.pki.certificateError.certificate_expired:
3411
+ return tls.Alert.Description.certificate_expired;
3412
+ case forge.pki.certificateError.certificate_unknown:
3413
+ return tls.Alert.Description.certificate_unknown;
3414
+ case forge.pki.certificateError.unknown_ca:
3415
+ return tls.Alert.Description.unknown_ca;
3416
+ default:
3417
+ return tls.Alert.Description.bad_certificate;
3418
+ }
3419
+ };
3420
+
3421
+ /**
3422
+ * Maps a tls.Alert.Description to a pki.certificateError.
3423
+ *
3424
+ * @param desc the alert description.
3425
+ *
3426
+ * @return the certificate error.
3427
+ */
3428
+ var _alertDescToCertError = function (desc) {
3429
+ switch (desc) {
3430
+ case true:
3431
+ return true;
3432
+ case tls.Alert.Description.bad_certificate:
3433
+ return forge.pki.certificateError.bad_certificate;
3434
+ case tls.Alert.Description.unsupported_certificate:
3435
+ return forge.pki.certificateError.unsupported_certificate;
3436
+ case tls.Alert.Description.certificate_revoked:
3437
+ return forge.pki.certificateError.certificate_revoked;
3438
+ case tls.Alert.Description.certificate_expired:
3439
+ return forge.pki.certificateError.certificate_expired;
3440
+ case tls.Alert.Description.certificate_unknown:
3441
+ return forge.pki.certificateError.certificate_unknown;
3442
+ case tls.Alert.Description.unknown_ca:
3443
+ return forge.pki.certificateError.unknown_ca;
3444
+ default:
3445
+ return forge.pki.certificateError.bad_certificate;
3446
+ }
3447
+ };
3448
+
3449
+ /**
3450
+ * Verifies a certificate chain against the given connection's
3451
+ * Certificate Authority store.
3452
+ *
3453
+ * @param c the TLS connection.
3454
+ * @param chain the certificate chain to verify, with the root or highest
3455
+ * authority at the end.
3456
+ *
3457
+ * @return true if successful, false if not.
3458
+ */
3459
+ tls.verifyCertificateChain = function (c, chain) {
3460
+ try {
3461
+ // Make a copy of c.verifyOptions so that we can modify options.verify
3462
+ // without modifying c.verifyOptions.
3463
+ var options = {};
3464
+ for (var key in c.verifyOptions) {
3465
+ options[key] = c.verifyOptions[key];
3466
+ }
3467
+ options.verify = function (vfd, depth, chain) {
3468
+ // convert pki.certificateError to tls alert description
3469
+ var desc = _certErrorToAlertDesc(vfd);
3470
+
3471
+ // call application callback
3472
+ var ret = c.verify(c, vfd, depth, chain);
3473
+ if (ret !== true) {
3474
+ if (typeof ret === 'object' && !forge.util.isArray(ret)) {
3475
+ // throw custom error
3476
+ var error = new Error('The application rejected the certificate.');
3477
+ error.send = true;
3478
+ error.alert = {
3479
+ level: tls.Alert.Level.fatal,
3480
+ description: tls.Alert.Description.bad_certificate
3481
+ };
3482
+ if (ret.message) {
3483
+ error.message = ret.message;
3484
+ }
3485
+ if (ret.alert) {
3486
+ error.alert.description = ret.alert;
3487
+ }
3488
+ throw error;
3489
+ }
3490
+
3491
+ // convert tls alert description to pki.certificateError
3492
+ if (ret !== vfd) {
3493
+ ret = _alertDescToCertError(ret);
3494
+ }
3495
+ }
3496
+ return ret;
3497
+ };
3498
+
3499
+ // verify chain
3500
+ forge.pki.verifyCertificateChain(c.caStore, chain, options);
3501
+ } catch (ex) {
3502
+ // build tls error if not already customized
3503
+ var err = ex;
3504
+ if (typeof err !== 'object' || forge.util.isArray(err)) {
3505
+ err = {
3506
+ send: true,
3507
+ alert: {
3508
+ level: tls.Alert.Level.fatal,
3509
+ description: _certErrorToAlertDesc(ex)
3510
+ }
3511
+ };
3512
+ }
3513
+ if (!('send' in err)) {
3514
+ err.send = true;
3515
+ }
3516
+ if (!('alert' in err)) {
3517
+ err.alert = {
3518
+ level: tls.Alert.Level.fatal,
3519
+ description: _certErrorToAlertDesc(err.error)
3520
+ };
3521
+ }
3522
+
3523
+ // send error
3524
+ c.error(c, err);
3525
+ }
3526
+ return !c.fail;
3527
+ };
3528
+
3529
+ /**
3530
+ * Creates a new TLS session cache.
3531
+ *
3532
+ * @param cache optional map of session ID to cached session.
3533
+ * @param capacity the maximum size for the cache (default: 100).
3534
+ *
3535
+ * @return the new TLS session cache.
3536
+ */
3537
+ tls.createSessionCache = function (cache, capacity) {
3538
+ var rval = null;
3539
+
3540
+ // assume input is already a session cache object
3541
+ if (cache && cache.getSession && cache.setSession && cache.order) {
3542
+ rval = cache;
3543
+ } else {
3544
+ // create cache
3545
+ rval = {};
3546
+ rval.cache = cache || {};
3547
+ rval.capacity = Math.max(capacity || 100, 1);
3548
+ rval.order = [];
3549
+
3550
+ // store order for sessions, delete session overflow
3551
+ for (var key in cache) {
3552
+ if (rval.order.length <= capacity) {
3553
+ rval.order.push(key);
3554
+ } else {
3555
+ delete cache[key];
3556
+ }
3557
+ }
3558
+
3559
+ // get a session from a session ID (or get any session)
3560
+ rval.getSession = function (sessionId) {
3561
+ var session = null;
3562
+ var key = null;
3563
+
3564
+ // if session ID provided, use it
3565
+ if (sessionId) {
3566
+ key = forge.util.bytesToHex(sessionId);
3567
+ } else if (rval.order.length > 0) {
3568
+ // get first session from cache
3569
+ key = rval.order[0];
3570
+ }
3571
+ if (key !== null && key in rval.cache) {
3572
+ // get cached session and remove from cache
3573
+ session = rval.cache[key];
3574
+ delete rval.cache[key];
3575
+ for (var i in rval.order) {
3576
+ if (rval.order[i] === key) {
3577
+ rval.order.splice(i, 1);
3578
+ break;
3579
+ }
3580
+ }
3581
+ }
3582
+ return session;
3583
+ };
3584
+
3585
+ // set a session in the cache
3586
+ rval.setSession = function (sessionId, session) {
3587
+ // remove session from cache if at capacity
3588
+ if (rval.order.length === rval.capacity) {
3589
+ var key = rval.order.shift();
3590
+ delete rval.cache[key];
3591
+ }
3592
+ // add session to cache
3593
+ var key = forge.util.bytesToHex(sessionId);
3594
+ rval.order.push(key);
3595
+ rval.cache[key] = session;
3596
+ };
3597
+ }
3598
+ return rval;
3599
+ };
3600
+
3601
+ /**
3602
+ * Creates a new TLS connection.
3603
+ *
3604
+ * See public createConnection() docs for more details.
3605
+ *
3606
+ * @param options the options for this connection.
3607
+ *
3608
+ * @return the new TLS connection.
3609
+ */
3610
+ tls.createConnection = function (options) {
3611
+ var caStore = null;
3612
+ if (options.caStore) {
3613
+ // if CA store is an array, convert it to a CA store object
3614
+ if (forge.util.isArray(options.caStore)) {
3615
+ caStore = forge.pki.createCaStore(options.caStore);
3616
+ } else {
3617
+ caStore = options.caStore;
3618
+ }
3619
+ } else {
3620
+ // create empty CA store
3621
+ caStore = forge.pki.createCaStore();
3622
+ }
3623
+
3624
+ // setup default cipher suites
3625
+ var cipherSuites = options.cipherSuites || null;
3626
+ if (cipherSuites === null) {
3627
+ cipherSuites = [];
3628
+ for (var key in tls.CipherSuites) {
3629
+ cipherSuites.push(tls.CipherSuites[key]);
3630
+ }
3631
+ }
3632
+
3633
+ // set default entity
3634
+ var entity = options.server || false ? tls.ConnectionEnd.server : tls.ConnectionEnd.client;
3635
+
3636
+ // create session cache if requested
3637
+ var sessionCache = options.sessionCache ? tls.createSessionCache(options.sessionCache) : null;
3638
+
3639
+ // create TLS connection
3640
+ var c = {
3641
+ version: {
3642
+ major: tls.Version.major,
3643
+ minor: tls.Version.minor
3644
+ },
3645
+ entity: entity,
3646
+ sessionId: options.sessionId,
3647
+ caStore: caStore,
3648
+ sessionCache: sessionCache,
3649
+ cipherSuites: cipherSuites,
3650
+ connected: options.connected,
3651
+ virtualHost: options.virtualHost || null,
3652
+ verifyClient: options.verifyClient || false,
3653
+ verify: options.verify || function (cn, vfd, dpth, cts) {
3654
+ return vfd;
3655
+ },
3656
+ verifyOptions: options.verifyOptions || {},
3657
+ getCertificate: options.getCertificate || null,
3658
+ getPrivateKey: options.getPrivateKey || null,
3659
+ getSignature: options.getSignature || null,
3660
+ input: forge.util.createBuffer(),
3661
+ tlsData: forge.util.createBuffer(),
3662
+ data: forge.util.createBuffer(),
3663
+ tlsDataReady: options.tlsDataReady,
3664
+ dataReady: options.dataReady,
3665
+ heartbeatReceived: options.heartbeatReceived,
3666
+ closed: options.closed,
3667
+ error: function (c, ex) {
3668
+ // set origin if not set
3669
+ ex.origin = ex.origin || (c.entity === tls.ConnectionEnd.client ? 'client' : 'server');
3670
+
3671
+ // send TLS alert
3672
+ if (ex.send) {
3673
+ tls.queue(c, tls.createAlert(c, ex.alert));
3674
+ tls.flush(c);
3675
+ }
3676
+
3677
+ // error is fatal by default
3678
+ var fatal = ex.fatal !== false;
3679
+ if (fatal) {
3680
+ // set fail flag
3681
+ c.fail = true;
3682
+ }
3683
+
3684
+ // call error handler first
3685
+ options.error(c, ex);
3686
+ if (fatal) {
3687
+ // fatal error, close connection, do not clear fail
3688
+ c.close(false);
3689
+ }
3690
+ },
3691
+ deflate: options.deflate || null,
3692
+ inflate: options.inflate || null
3693
+ };
3694
+
3695
+ /**
3696
+ * Resets a closed TLS connection for reuse. Called in c.close().
3697
+ *
3698
+ * @param clearFail true to clear the fail flag (default: true).
3699
+ */
3700
+ c.reset = function (clearFail) {
3701
+ c.version = {
3702
+ major: tls.Version.major,
3703
+ minor: tls.Version.minor
3704
+ };
3705
+ c.record = null;
3706
+ c.session = null;
3707
+ c.peerCertificate = null;
3708
+ c.state = {
3709
+ pending: null,
3710
+ current: null
3711
+ };
3712
+ c.expect = c.entity === tls.ConnectionEnd.client ? SHE : CHE;
3713
+ c.fragmented = null;
3714
+ c.records = [];
3715
+ c.open = false;
3716
+ c.handshakes = 0;
3717
+ c.handshaking = false;
3718
+ c.isConnected = false;
3719
+ c.fail = !(clearFail || typeof clearFail === 'undefined');
3720
+ c.input.clear();
3721
+ c.tlsData.clear();
3722
+ c.data.clear();
3723
+ c.state.current = tls.createConnectionState(c);
3724
+ };
3725
+
3726
+ // do initial reset of connection
3727
+ c.reset();
3728
+
3729
+ /**
3730
+ * Updates the current TLS engine state based on the given record.
3731
+ *
3732
+ * @param c the TLS connection.
3733
+ * @param record the TLS record to act on.
3734
+ */
3735
+ var _update = function (c, record) {
3736
+ // get record handler (align type in table by subtracting lowest)
3737
+ var aligned = record.type - tls.ContentType.change_cipher_spec;
3738
+ var handlers = ctTable[c.entity][c.expect];
3739
+ if (aligned in handlers) {
3740
+ handlers[aligned](c, record);
3741
+ } else {
3742
+ // unexpected record
3743
+ tls.handleUnexpected(c, record);
3744
+ }
3745
+ };
3746
+
3747
+ /**
3748
+ * Reads the record header and initializes the next record on the given
3749
+ * connection.
3750
+ *
3751
+ * @param c the TLS connection with the next record.
3752
+ *
3753
+ * @return 0 if the input data could be processed, otherwise the
3754
+ * number of bytes required for data to be processed.
3755
+ */
3756
+ var _readRecordHeader = function (c) {
3757
+ var rval = 0;
3758
+
3759
+ // get input buffer and its length
3760
+ var b = c.input;
3761
+ var len = b.length();
3762
+
3763
+ // need at least 5 bytes to initialize a record
3764
+ if (len < 5) {
3765
+ rval = 5 - len;
3766
+ } else {
3767
+ // enough bytes for header
3768
+ // initialize record
3769
+ c.record = {
3770
+ type: b.getByte(),
3771
+ version: {
3772
+ major: b.getByte(),
3773
+ minor: b.getByte()
3774
+ },
3775
+ length: b.getInt16(),
3776
+ fragment: forge.util.createBuffer(),
3777
+ ready: false
3778
+ };
3779
+
3780
+ // check record version
3781
+ var compatibleVersion = c.record.version.major === c.version.major;
3782
+ if (compatibleVersion && c.session && c.session.version) {
3783
+ // session version already set, require same minor version
3784
+ compatibleVersion = c.record.version.minor === c.version.minor;
3785
+ }
3786
+ if (!compatibleVersion) {
3787
+ c.error(c, {
3788
+ message: 'Incompatible TLS version.',
3789
+ send: true,
3790
+ alert: {
3791
+ level: tls.Alert.Level.fatal,
3792
+ description: tls.Alert.Description.protocol_version
3793
+ }
3794
+ });
3795
+ }
3796
+ }
3797
+ return rval;
3798
+ };
3799
+
3800
+ /**
3801
+ * Reads the next record's contents and appends its message to any
3802
+ * previously fragmented message.
3803
+ *
3804
+ * @param c the TLS connection with the next record.
3805
+ *
3806
+ * @return 0 if the input data could be processed, otherwise the
3807
+ * number of bytes required for data to be processed.
3808
+ */
3809
+ var _readRecord = function (c) {
3810
+ var rval = 0;
3811
+
3812
+ // ensure there is enough input data to get the entire record
3813
+ var b = c.input;
3814
+ var len = b.length();
3815
+ if (len < c.record.length) {
3816
+ // not enough data yet, return how much is required
3817
+ rval = c.record.length - len;
3818
+ } else {
3819
+ // there is enough data to parse the pending record
3820
+ // fill record fragment and compact input buffer
3821
+ c.record.fragment.putBytes(b.getBytes(c.record.length));
3822
+ b.compact();
3823
+
3824
+ // update record using current read state
3825
+ var s = c.state.current.read;
3826
+ if (s.update(c, c.record)) {
3827
+ // see if there is a previously fragmented message that the
3828
+ // new record's message fragment should be appended to
3829
+ if (c.fragmented !== null) {
3830
+ // if the record type matches a previously fragmented
3831
+ // record, append the record fragment to it
3832
+ if (c.fragmented.type === c.record.type) {
3833
+ // concatenate record fragments
3834
+ c.fragmented.fragment.putBuffer(c.record.fragment);
3835
+ c.record = c.fragmented;
3836
+ } else {
3837
+ // error, invalid fragmented record
3838
+ c.error(c, {
3839
+ message: 'Invalid fragmented record.',
3840
+ send: true,
3841
+ alert: {
3842
+ level: tls.Alert.Level.fatal,
3843
+ description: tls.Alert.Description.unexpected_message
3844
+ }
3845
+ });
3846
+ }
3847
+ }
3848
+
3849
+ // record is now ready
3850
+ c.record.ready = true;
3851
+ }
3852
+ }
3853
+ return rval;
3854
+ };
3855
+
3856
+ /**
3857
+ * Performs a handshake using the TLS Handshake Protocol, as a client.
3858
+ *
3859
+ * This method should only be called if the connection is in client mode.
3860
+ *
3861
+ * @param sessionId the session ID to use, null to start a new one.
3862
+ */
3863
+ c.handshake = function (sessionId) {
3864
+ // error to call this in non-client mode
3865
+ if (c.entity !== tls.ConnectionEnd.client) {
3866
+ // not fatal error
3867
+ c.error(c, {
3868
+ message: 'Cannot initiate handshake as a server.',
3869
+ fatal: false
3870
+ });
3871
+ } else if (c.handshaking) {
3872
+ // handshake is already in progress, fail but not fatal error
3873
+ c.error(c, {
3874
+ message: 'Handshake already in progress.',
3875
+ fatal: false
3876
+ });
3877
+ } else {
3878
+ // clear fail flag on reuse
3879
+ if (c.fail && !c.open && c.handshakes === 0) {
3880
+ c.fail = false;
3881
+ }
3882
+
3883
+ // now handshaking
3884
+ c.handshaking = true;
3885
+
3886
+ // default to blank (new session)
3887
+ sessionId = sessionId || '';
3888
+
3889
+ // if a session ID was specified, try to find it in the cache
3890
+ var session = null;
3891
+ if (sessionId.length > 0) {
3892
+ if (c.sessionCache) {
3893
+ session = c.sessionCache.getSession(sessionId);
3894
+ }
3895
+
3896
+ // matching session not found in cache, clear session ID
3897
+ if (session === null) {
3898
+ sessionId = '';
3899
+ }
3900
+ }
3901
+
3902
+ // no session given, grab a session from the cache, if available
3903
+ if (sessionId.length === 0 && c.sessionCache) {
3904
+ session = c.sessionCache.getSession();
3905
+ if (session !== null) {
3906
+ sessionId = session.id;
3907
+ }
3908
+ }
3909
+
3910
+ // set up session
3911
+ c.session = {
3912
+ id: sessionId,
3913
+ version: null,
3914
+ cipherSuite: null,
3915
+ compressionMethod: null,
3916
+ serverCertificate: null,
3917
+ certificateRequest: null,
3918
+ clientCertificate: null,
3919
+ sp: {},
3920
+ md5: forge.md.md5.create(),
3921
+ sha1: forge.md.sha1.create()
3922
+ };
3923
+
3924
+ // use existing session information
3925
+ if (session) {
3926
+ // only update version on connection, session version not yet set
3927
+ c.version = session.version;
3928
+ c.session.sp = session.sp;
3929
+ }
3930
+
3931
+ // generate new client random
3932
+ c.session.sp.client_random = tls.createRandom().getBytes();
3933
+
3934
+ // connection now open
3935
+ c.open = true;
3936
+
3937
+ // send hello
3938
+ tls.queue(c, tls.createRecord(c, {
3939
+ type: tls.ContentType.handshake,
3940
+ data: tls.createClientHello(c)
3941
+ }));
3942
+ tls.flush(c);
3943
+ }
3944
+ };
3945
+
3946
+ /**
3947
+ * Called when TLS protocol data has been received from somewhere and should
3948
+ * be processed by the TLS engine.
3949
+ *
3950
+ * @param data the TLS protocol data, as a string, to process.
3951
+ *
3952
+ * @return 0 if the data could be processed, otherwise the number of bytes
3953
+ * required for data to be processed.
3954
+ */
3955
+ c.process = function (data) {
3956
+ var rval = 0;
3957
+
3958
+ // buffer input data
3959
+ if (data) {
3960
+ c.input.putBytes(data);
3961
+ }
3962
+
3963
+ // process next record if no failure, process will be called after
3964
+ // each record is handled (since handling can be asynchronous)
3965
+ if (!c.fail) {
3966
+ // reset record if ready and now empty
3967
+ if (c.record !== null && c.record.ready && c.record.fragment.isEmpty()) {
3968
+ c.record = null;
3969
+ }
3970
+
3971
+ // if there is no pending record, try to read record header
3972
+ if (c.record === null) {
3973
+ rval = _readRecordHeader(c);
3974
+ }
3975
+
3976
+ // read the next record (if record not yet ready)
3977
+ if (!c.fail && c.record !== null && !c.record.ready) {
3978
+ rval = _readRecord(c);
3979
+ }
3980
+
3981
+ // record ready to be handled, update engine state
3982
+ if (!c.fail && c.record !== null && c.record.ready) {
3983
+ _update(c, c.record);
3984
+ }
3985
+ }
3986
+ return rval;
3987
+ };
3988
+
3989
+ /**
3990
+ * Requests that application data be packaged into a TLS record. The
3991
+ * tlsDataReady handler will be called when the TLS record(s) have been
3992
+ * prepared.
3993
+ *
3994
+ * @param data the application data, as a raw 'binary' encoded string, to
3995
+ * be sent; to send utf-16/utf-8 string data, use the return value
3996
+ * of util.encodeUtf8(str).
3997
+ *
3998
+ * @return true on success, false on failure.
3999
+ */
4000
+ c.prepare = function (data) {
4001
+ tls.queue(c, tls.createRecord(c, {
4002
+ type: tls.ContentType.application_data,
4003
+ data: forge.util.createBuffer(data)
4004
+ }));
4005
+ return tls.flush(c);
4006
+ };
4007
+
4008
+ /**
4009
+ * Requests that a heartbeat request be packaged into a TLS record for
4010
+ * transmission. The tlsDataReady handler will be called when TLS record(s)
4011
+ * have been prepared.
4012
+ *
4013
+ * When a heartbeat response has been received, the heartbeatReceived
4014
+ * handler will be called with the matching payload. This handler can
4015
+ * be used to clear a retransmission timer, etc.
4016
+ *
4017
+ * @param payload the heartbeat data to send as the payload in the message.
4018
+ * @param [payloadLength] the payload length to use, defaults to the
4019
+ * actual payload length.
4020
+ *
4021
+ * @return true on success, false on failure.
4022
+ */
4023
+ c.prepareHeartbeatRequest = function (payload, payloadLength) {
4024
+ if (payload instanceof forge.util.ByteBuffer) {
4025
+ payload = payload.bytes();
4026
+ }
4027
+ if (typeof payloadLength === 'undefined') {
4028
+ payloadLength = payload.length;
4029
+ }
4030
+ c.expectedHeartbeatPayload = payload;
4031
+ tls.queue(c, tls.createRecord(c, {
4032
+ type: tls.ContentType.heartbeat,
4033
+ data: tls.createHeartbeat(tls.HeartbeatMessageType.heartbeat_request, payload, payloadLength)
4034
+ }));
4035
+ return tls.flush(c);
4036
+ };
4037
+
4038
+ /**
4039
+ * Closes the connection (sends a close_notify alert).
4040
+ *
4041
+ * @param clearFail true to clear the fail flag (default: true).
4042
+ */
4043
+ c.close = function (clearFail) {
4044
+ // save session if connection didn't fail
4045
+ if (!c.fail && c.sessionCache && c.session) {
4046
+ // only need to preserve session ID, version, and security params
4047
+ var session = {
4048
+ id: c.session.id,
4049
+ version: c.session.version,
4050
+ sp: c.session.sp
4051
+ };
4052
+ session.sp.keys = null;
4053
+ c.sessionCache.setSession(session.id, session);
4054
+ }
4055
+ if (c.open) {
4056
+ // connection no longer open, clear input
4057
+ c.open = false;
4058
+ c.input.clear();
4059
+
4060
+ // if connected or handshaking, send an alert
4061
+ if (c.isConnected || c.handshaking) {
4062
+ c.isConnected = c.handshaking = false;
4063
+
4064
+ // send close_notify alert
4065
+ tls.queue(c, tls.createAlert(c, {
4066
+ level: tls.Alert.Level.warning,
4067
+ description: tls.Alert.Description.close_notify
4068
+ }));
4069
+ tls.flush(c);
4070
+ }
4071
+
4072
+ // call handler
4073
+ c.closed(c);
4074
+ }
4075
+
4076
+ // reset TLS connection, do not clear fail flag
4077
+ c.reset(clearFail);
4078
+ };
4079
+ return c;
4080
+ };
4081
+
4082
+ /* TLS API */
4083
+ tls_1 = forge.tls = forge.tls || {};
4084
+
4085
+ // expose non-functions
4086
+ for (var key in tls) {
4087
+ if (typeof tls[key] !== 'function') {
4088
+ forge.tls[key] = tls[key];
4089
+ }
4090
+ }
4091
+
4092
+ // expose prf_tls1 for testing
4093
+ forge.tls.prf_tls1 = prf_TLS1;
4094
+
4095
+ // expose sha1 hmac method
4096
+ forge.tls.hmac_sha1 = hmac_sha1;
4097
+
4098
+ // expose session cache creation
4099
+ forge.tls.createSessionCache = tls.createSessionCache;
4100
+
4101
+ /**
4102
+ * Creates a new TLS connection. This does not make any assumptions about the
4103
+ * transport layer that TLS is working on top of, ie: it does not assume there
4104
+ * is a TCP/IP connection or establish one. A TLS connection is totally
4105
+ * abstracted away from the layer is runs on top of, it merely establishes a
4106
+ * secure channel between a client" and a "server".
4107
+ *
4108
+ * A TLS connection contains 4 connection states: pending read and write, and
4109
+ * current read and write.
4110
+ *
4111
+ * At initialization, the current read and write states will be null. Only once
4112
+ * the security parameters have been set and the keys have been generated can
4113
+ * the pending states be converted into current states. Current states will be
4114
+ * updated for each record processed.
4115
+ *
4116
+ * A custom certificate verify callback may be provided to check information
4117
+ * like the common name on the server's certificate. It will be called for
4118
+ * every certificate in the chain. It has the following signature:
4119
+ *
4120
+ * variable func(c, certs, index, preVerify)
4121
+ * Where:
4122
+ * c The TLS connection
4123
+ * verified Set to true if certificate was verified, otherwise the alert
4124
+ * tls.Alert.Description for why the certificate failed.
4125
+ * depth The current index in the chain, where 0 is the server's cert.
4126
+ * certs The certificate chain, *NOTE* if the server was anonymous then
4127
+ * the chain will be empty.
4128
+ *
4129
+ * The function returns true on success and on failure either the appropriate
4130
+ * tls.Alert.Description or an object with 'alert' set to the appropriate
4131
+ * tls.Alert.Description and 'message' set to a custom error message. If true
4132
+ * is not returned then the connection will abort using, in order of
4133
+ * availability, first the returned alert description, second the preVerify
4134
+ * alert description, and lastly the default 'bad_certificate'.
4135
+ *
4136
+ * There are three callbacks that can be used to make use of client-side
4137
+ * certificates where each takes the TLS connection as the first parameter:
4138
+ *
4139
+ * getCertificate(conn, hint)
4140
+ * The second parameter is a hint as to which certificate should be
4141
+ * returned. If the connection entity is a client, then the hint will be
4142
+ * the CertificateRequest message from the server that is part of the
4143
+ * TLS protocol. If the connection entity is a server, then it will be
4144
+ * the servername list provided via an SNI extension the ClientHello, if
4145
+ * one was provided (empty array if not). The hint can be examined to
4146
+ * determine which certificate to use (advanced). Most implementations
4147
+ * will just return a certificate. The return value must be a
4148
+ * PEM-formatted certificate or an array of PEM-formatted certificates
4149
+ * that constitute a certificate chain, with the first in the array/chain
4150
+ * being the client's certificate.
4151
+ * getPrivateKey(conn, certificate)
4152
+ * The second parameter is an forge.pki X.509 certificate object that
4153
+ * is associated with the requested private key. The return value must
4154
+ * be a PEM-formatted private key.
4155
+ * getSignature(conn, bytes, callback)
4156
+ * This callback can be used instead of getPrivateKey if the private key
4157
+ * is not directly accessible in javascript or should not be. For
4158
+ * instance, a secure external web service could provide the signature
4159
+ * in exchange for appropriate credentials. The second parameter is a
4160
+ * string of bytes to be signed that are part of the TLS protocol. These
4161
+ * bytes are used to verify that the private key for the previously
4162
+ * provided client-side certificate is accessible to the client. The
4163
+ * callback is a function that takes 2 parameters, the TLS connection
4164
+ * and the RSA encrypted (signed) bytes as a string. This callback must
4165
+ * be called once the signature is ready.
4166
+ *
4167
+ * @param options the options for this connection:
4168
+ * server: true if the connection is server-side, false for client.
4169
+ * sessionId: a session ID to reuse, null for a new connection.
4170
+ * caStore: an array of certificates to trust.
4171
+ * sessionCache: a session cache to use.
4172
+ * cipherSuites: an optional array of cipher suites to use,
4173
+ * see tls.CipherSuites.
4174
+ * connected: function(conn) called when the first handshake completes.
4175
+ * virtualHost: the virtual server name to use in a TLS SNI extension.
4176
+ * verifyClient: true to require a client certificate in server mode,
4177
+ * 'optional' to request one, false not to (default: false).
4178
+ * verify: a handler used to custom verify certificates in the chain.
4179
+ * verifyOptions: an object with options for the certificate chain validation.
4180
+ * See documentation of pki.verifyCertificateChain for possible options.
4181
+ * verifyOptions.verify is ignored. If you wish to specify a verify handler
4182
+ * use the verify key.
4183
+ * getCertificate: an optional callback used to get a certificate or
4184
+ * a chain of certificates (as an array).
4185
+ * getPrivateKey: an optional callback used to get a private key.
4186
+ * getSignature: an optional callback used to get a signature.
4187
+ * tlsDataReady: function(conn) called when TLS protocol data has been
4188
+ * prepared and is ready to be used (typically sent over a socket
4189
+ * connection to its destination), read from conn.tlsData buffer.
4190
+ * dataReady: function(conn) called when application data has
4191
+ * been parsed from a TLS record and should be consumed by the
4192
+ * application, read from conn.data buffer.
4193
+ * closed: function(conn) called when the connection has been closed.
4194
+ * error: function(conn, error) called when there was an error.
4195
+ * deflate: function(inBytes) if provided, will deflate TLS records using
4196
+ * the deflate algorithm if the server supports it.
4197
+ * inflate: function(inBytes) if provided, will inflate TLS records using
4198
+ * the deflate algorithm if the server supports it.
4199
+ *
4200
+ * @return the new TLS connection.
4201
+ */
4202
+ forge.tls.createConnection = tls.createConnection;
4203
+ return tls_1;
4204
+ }
4205
+
4206
+ export { requireTls as __require };
4207
+ //# sourceMappingURL=tls.js.map