/src/mozilla-central/security/nss/lib/ssl/sslspec.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * Handling of cipher specs. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
8 | | |
9 | | #include "ssl.h" |
10 | | #include "sslproto.h" |
11 | | #include "pk11func.h" |
12 | | #include "secitem.h" |
13 | | |
14 | | #include "sslimpl.h" |
15 | | |
16 | | /* Record protection algorithms, indexed by SSL3BulkCipher. |
17 | | * |
18 | | * The |max_records| field (|mr| below) is set to a number that is higher than |
19 | | * recommended in some literature (esp. TLS 1.3) because we currently abort the |
20 | | * connection when this limit is reached and we want to ensure that we only |
21 | | * rarely hit this limit. See bug 1268745 for details. |
22 | | */ |
23 | | #define MR_MAX RECORD_SEQ_MAX /* 2^48-1 */ |
24 | | #define MR_128 (0x5aULL << 28) /* For AES and similar. */ |
25 | | #define MR_LOW (1ULL << 20) /* For weak ciphers. */ |
26 | | /* clang-format off */ |
27 | | static const ssl3BulkCipherDef ssl_bulk_cipher_defs[] = { |
28 | | /* |--------- Lengths ---------| */ |
29 | | /* cipher calg : s : */ |
30 | | /* : e b n */ |
31 | | /* oid short_name mr : c l o */ |
32 | | /* k r o t n */ |
33 | | /* e e i c a c */ |
34 | | /* y t type v k g e */ |
35 | | {cipher_null, ssl_calg_null, 0, 0, type_stream, 0, 0, 0, 0, |
36 | | SEC_OID_NULL_CIPHER, "NULL", MR_MAX}, |
37 | | {cipher_rc4, ssl_calg_rc4, 16,16, type_stream, 0, 0, 0, 0, |
38 | | SEC_OID_RC4, "RC4", MR_LOW}, |
39 | | {cipher_des, ssl_calg_des, 8, 8, type_block, 8, 8, 0, 0, |
40 | | SEC_OID_DES_CBC, "DES-CBC", MR_LOW}, |
41 | | {cipher_3des, ssl_calg_3des, 24,24, type_block, 8, 8, 0, 0, |
42 | | SEC_OID_DES_EDE3_CBC, "3DES-EDE-CBC", MR_LOW}, |
43 | | {cipher_aes_128, ssl_calg_aes, 16,16, type_block, 16,16, 0, 0, |
44 | | SEC_OID_AES_128_CBC, "AES-128", MR_128}, |
45 | | {cipher_aes_256, ssl_calg_aes, 32,32, type_block, 16,16, 0, 0, |
46 | | SEC_OID_AES_256_CBC, "AES-256", MR_128}, |
47 | | {cipher_camellia_128, ssl_calg_camellia, 16,16, type_block, 16,16, 0, 0, |
48 | | SEC_OID_CAMELLIA_128_CBC, "Camellia-128", MR_128}, |
49 | | {cipher_camellia_256, ssl_calg_camellia, 32,32, type_block, 16,16, 0, 0, |
50 | | SEC_OID_CAMELLIA_256_CBC, "Camellia-256", MR_128}, |
51 | | {cipher_seed, ssl_calg_seed, 16,16, type_block, 16,16, 0, 0, |
52 | | SEC_OID_SEED_CBC, "SEED-CBC", MR_128}, |
53 | | {cipher_aes_128_gcm, ssl_calg_aes_gcm, 16,16, type_aead, 4, 0,16, 8, |
54 | | SEC_OID_AES_128_GCM, "AES-128-GCM", MR_128}, |
55 | | {cipher_aes_256_gcm, ssl_calg_aes_gcm, 32,32, type_aead, 4, 0,16, 8, |
56 | | SEC_OID_AES_256_GCM, "AES-256-GCM", MR_128}, |
57 | | {cipher_chacha20, ssl_calg_chacha20, 32,32, type_aead, 12, 0,16, 0, |
58 | | SEC_OID_CHACHA20_POLY1305, "ChaCha20-Poly1305", MR_MAX}, |
59 | | {cipher_missing, ssl_calg_null, 0, 0, type_stream, 0, 0, 0, 0, |
60 | | SEC_OID_UNKNOWN, "missing", 0U}, |
61 | | }; |
62 | | /* clang-format on */ |
63 | | |
64 | | const ssl3BulkCipherDef * |
65 | | ssl_GetBulkCipherDef(const ssl3CipherSuiteDef *suiteDef) |
66 | 0 | { |
67 | 0 | SSL3BulkCipher bulkCipher = suiteDef->bulk_cipher_alg; |
68 | 0 | PORT_Assert(bulkCipher < PR_ARRAY_SIZE(ssl_bulk_cipher_defs)); |
69 | 0 | PORT_Assert(ssl_bulk_cipher_defs[bulkCipher].cipher == bulkCipher); |
70 | 0 | return &ssl_bulk_cipher_defs[bulkCipher]; |
71 | 0 | } |
72 | | |
73 | | /* indexed by SSL3MACAlgorithm */ |
74 | | static const ssl3MACDef ssl_mac_defs[] = { |
75 | | /* pad_size is only used for SSL 3.0 MAC. See RFC 6101 Sec. 5.2.3.1. */ |
76 | | /* mac mmech pad_size mac_size */ |
77 | | { ssl_mac_null, CKM_INVALID_MECHANISM, 0, 0, 0 }, |
78 | | { ssl_mac_md5, CKM_SSL3_MD5_MAC, 48, MD5_LENGTH, SEC_OID_HMAC_MD5 }, |
79 | | { ssl_mac_sha, CKM_SSL3_SHA1_MAC, 40, SHA1_LENGTH, SEC_OID_HMAC_SHA1 }, |
80 | | { ssl_hmac_md5, CKM_MD5_HMAC, 0, MD5_LENGTH, SEC_OID_HMAC_MD5 }, |
81 | | { ssl_hmac_sha, CKM_SHA_1_HMAC, 0, SHA1_LENGTH, SEC_OID_HMAC_SHA1 }, |
82 | | { ssl_hmac_sha256, CKM_SHA256_HMAC, 0, SHA256_LENGTH, SEC_OID_HMAC_SHA256 }, |
83 | | { ssl_mac_aead, CKM_INVALID_MECHANISM, 0, 0, 0 }, |
84 | | { ssl_hmac_sha384, CKM_SHA384_HMAC, 0, SHA384_LENGTH, SEC_OID_HMAC_SHA384 } |
85 | | }; |
86 | | |
87 | | const ssl3MACDef * |
88 | | ssl_GetMacDefByAlg(SSL3MACAlgorithm mac) |
89 | 0 | { |
90 | 0 | /* Cast here for clang: https://bugs.llvm.org/show_bug.cgi?id=16154 */ |
91 | 0 | PORT_Assert((size_t)mac < PR_ARRAY_SIZE(ssl_mac_defs)); |
92 | 0 | PORT_Assert(ssl_mac_defs[mac].mac == mac); |
93 | 0 | return &ssl_mac_defs[mac]; |
94 | 0 | } |
95 | | |
96 | | const ssl3MACDef * |
97 | | ssl_GetMacDef(const sslSocket *ss, const ssl3CipherSuiteDef *suiteDef) |
98 | 0 | { |
99 | 0 | SSL3MACAlgorithm mac = suiteDef->mac_alg; |
100 | 0 | if (ss->version > SSL_LIBRARY_VERSION_3_0) { |
101 | 0 | switch (mac) { |
102 | 0 | case ssl_mac_md5: |
103 | 0 | mac = ssl_hmac_md5; |
104 | 0 | break; |
105 | 0 | case ssl_mac_sha: |
106 | 0 | mac = ssl_hmac_sha; |
107 | 0 | break; |
108 | 0 | default: |
109 | 0 | break; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | return ssl_GetMacDefByAlg(mac); |
113 | 0 | } |
114 | | |
115 | | ssl3CipherSpec * |
116 | | ssl_FindCipherSpecByEpoch(sslSocket *ss, CipherSpecDirection direction, |
117 | | DTLSEpoch epoch) |
118 | 0 | { |
119 | 0 | PRCList *cur_p; |
120 | 0 | for (cur_p = PR_LIST_HEAD(&ss->ssl3.hs.cipherSpecs); |
121 | 0 | cur_p != &ss->ssl3.hs.cipherSpecs; |
122 | 0 | cur_p = PR_NEXT_LINK(cur_p)) { |
123 | 0 | ssl3CipherSpec *spec = (ssl3CipherSpec *)cur_p; |
124 | 0 |
|
125 | 0 | if (spec->epoch != epoch) { |
126 | 0 | continue; |
127 | 0 | } |
128 | 0 | if (direction != spec->direction) { |
129 | 0 | continue; |
130 | 0 | } |
131 | 0 | return spec; |
132 | 0 | } |
133 | 0 | return NULL; |
134 | 0 | } |
135 | | |
136 | | ssl3CipherSpec * |
137 | | ssl_CreateCipherSpec(sslSocket *ss, CipherSpecDirection direction) |
138 | 0 | { |
139 | 0 | ssl3CipherSpec *spec = PORT_ZNew(ssl3CipherSpec); |
140 | 0 | if (!spec) { |
141 | 0 | return NULL; |
142 | 0 | } |
143 | 0 | spec->refCt = 1; |
144 | 0 | spec->version = ss->version; |
145 | 0 | spec->direction = direction; |
146 | 0 | spec->recordSizeLimit = MAX_FRAGMENT_LENGTH; |
147 | 0 | SSL_TRC(10, ("%d: SSL[%d]: new %s spec %d ct=%d", |
148 | 0 | SSL_GETPID(), ss->fd, SPEC_DIR(spec), spec, |
149 | 0 | spec->refCt)); |
150 | 0 | return spec; |
151 | 0 | } |
152 | | |
153 | | void |
154 | | ssl_SaveCipherSpec(sslSocket *ss, ssl3CipherSpec *spec) |
155 | 0 | { |
156 | 0 | PR_APPEND_LINK(&spec->link, &ss->ssl3.hs.cipherSpecs); |
157 | 0 | } |
158 | | |
159 | | /* Called from ssl3_InitState. */ |
160 | | /* Caller must hold the SpecWriteLock. */ |
161 | | SECStatus |
162 | | ssl_SetupNullCipherSpec(sslSocket *ss, CipherSpecDirection dir) |
163 | 0 | { |
164 | 0 | ssl3CipherSpec *spec; |
165 | 0 |
|
166 | 0 | PORT_Assert(ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); |
167 | 0 |
|
168 | 0 | spec = ssl_CreateCipherSpec(ss, dir); |
169 | 0 | if (!spec) { |
170 | 0 | return SECFailure; |
171 | 0 | } |
172 | 0 | |
173 | 0 | /* Set default versions. This value will be used to generate and send |
174 | 0 | * alerts if a version is not negotiated. These values are overridden when |
175 | 0 | * sending a ClientHello and when a version is negotiated. */ |
176 | 0 | spec->version = SSL_LIBRARY_VERSION_TLS_1_0; |
177 | 0 | spec->recordVersion = IS_DTLS(ss) |
178 | 0 | ? SSL_LIBRARY_VERSION_DTLS_1_0_WIRE |
179 | 0 | : SSL_LIBRARY_VERSION_TLS_1_0; |
180 | 0 | spec->cipherDef = &ssl_bulk_cipher_defs[cipher_null]; |
181 | 0 | PORT_Assert(spec->cipherDef->cipher == cipher_null); |
182 | 0 | spec->macDef = &ssl_mac_defs[ssl_mac_null]; |
183 | 0 | PORT_Assert(spec->macDef->mac == ssl_mac_null); |
184 | 0 | spec->cipher = Null_Cipher; |
185 | 0 |
|
186 | 0 | spec->phase = "cleartext"; |
187 | 0 | dtls_InitRecvdRecords(&spec->recvdRecords); |
188 | 0 |
|
189 | 0 | ssl_SaveCipherSpec(ss, spec); |
190 | 0 | if (dir == CipherSpecRead) { |
191 | 0 | ss->ssl3.crSpec = spec; |
192 | 0 | } else { |
193 | 0 | ss->ssl3.cwSpec = spec; |
194 | 0 | } |
195 | 0 | return SECSuccess; |
196 | 0 | } |
197 | | |
198 | | void |
199 | | ssl_CipherSpecAddRef(ssl3CipherSpec *spec) |
200 | 0 | { |
201 | 0 | ++spec->refCt; |
202 | 0 | SSL_TRC(10, ("%d: SSL[-]: Increment ref ct for %s spec %d. new ct = %d", |
203 | 0 | SSL_GETPID(), SPEC_DIR(spec), spec, spec->refCt)); |
204 | 0 | } |
205 | | |
206 | | static void |
207 | | ssl_DestroyKeyMaterial(ssl3KeyMaterial *keyMaterial) |
208 | 0 | { |
209 | 0 | PK11_FreeSymKey(keyMaterial->key); |
210 | 0 | PK11_FreeSymKey(keyMaterial->macKey); |
211 | 0 | if (keyMaterial->macContext != NULL) { |
212 | 0 | PK11_DestroyContext(keyMaterial->macContext, PR_TRUE); |
213 | 0 | } |
214 | 0 | } |
215 | | |
216 | | static void |
217 | | ssl_FreeCipherSpec(ssl3CipherSpec *spec) |
218 | 0 | { |
219 | 0 | SSL_TRC(10, ("%d: SSL[-]: Freeing %s spec %d. epoch=%d", |
220 | 0 | SSL_GETPID(), SPEC_DIR(spec), spec, spec->epoch)); |
221 | 0 |
|
222 | 0 | PR_REMOVE_LINK(&spec->link); |
223 | 0 |
|
224 | 0 | /* PORT_Assert( ss->opt.noLocks || ssl_HaveSpecWriteLock(ss)); Don't have ss! */ |
225 | 0 | if (spec->cipherContext) { |
226 | 0 | PK11_DestroyContext(spec->cipherContext, PR_TRUE); |
227 | 0 | } |
228 | 0 | PK11_FreeSymKey(spec->masterSecret); |
229 | 0 | ssl_DestroyKeyMaterial(&spec->keyMaterial); |
230 | 0 |
|
231 | 0 | PORT_ZFree(spec, sizeof(*spec)); |
232 | 0 | } |
233 | | |
234 | | /* This function is never called on a spec which is on the |
235 | | * cipherSpecs list. */ |
236 | | void |
237 | | ssl_CipherSpecRelease(ssl3CipherSpec *spec) |
238 | 0 | { |
239 | 0 | if (!spec) { |
240 | 0 | return; |
241 | 0 | } |
242 | 0 | |
243 | 0 | PORT_Assert(spec->refCt > 0); |
244 | 0 | --spec->refCt; |
245 | 0 | SSL_TRC(10, ("%d: SSL[-]: decrement refct for %s spec %d. epoch=%d new ct = %d", |
246 | 0 | SSL_GETPID(), SPEC_DIR(spec), spec, spec->epoch, spec->refCt)); |
247 | 0 | if (!spec->refCt) { |
248 | 0 | ssl_FreeCipherSpec(spec); |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | | void |
253 | | ssl_DestroyCipherSpecs(PRCList *list) |
254 | 0 | { |
255 | 0 | while (!PR_CLIST_IS_EMPTY(list)) { |
256 | 0 | ssl3CipherSpec *spec = (ssl3CipherSpec *)PR_LIST_TAIL(list); |
257 | 0 | ssl_FreeCipherSpec(spec); |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | | void |
262 | | ssl_CipherSpecReleaseByEpoch(sslSocket *ss, CipherSpecDirection dir, |
263 | | DTLSEpoch epoch) |
264 | 0 | { |
265 | 0 | ssl3CipherSpec *spec; |
266 | 0 | SSL_TRC(10, ("%d: SSL[%d]: releasing %s cipher spec for epoch %d", |
267 | 0 | SSL_GETPID(), ss->fd, |
268 | 0 | (dir == CipherSpecRead) ? "read" : "write", epoch)); |
269 | 0 |
|
270 | 0 | spec = ssl_FindCipherSpecByEpoch(ss, dir, epoch); |
271 | 0 | if (spec) { |
272 | 0 | ssl_CipherSpecRelease(spec); |
273 | 0 | } |
274 | 0 | } |