Coverage Report

Created: 2024-05-20 06:23

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