Skip to content

Commit aa8fad4

Browse files
sip/transp: add client certificate to all TLS transports
Currently, when a client certificate is added to a SIP transport, it is only added to the first matching transport in the transport list. Then, if multiple SIP transports exist (e.g if there are multiple network interfaces), the certificate might not be present in the transport when it is needed. Now, the certificate is added to all matching transports.
1 parent 7d8ac60 commit aa8fad4

File tree

1 file changed

+66
-23
lines changed

1 file changed

+66
-23
lines changed

src/sip/transp.c

+66-23
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ struct sip_ccert {
4747
};
4848

4949

50+
struct ccert_data {
51+
uint32_t hsup;
52+
struct sip_ccert *ccert;
53+
};
54+
55+
5056
struct sip_transport {
5157
struct le le;
5258
struct sa laddr;
@@ -191,6 +197,33 @@ static const struct sip_transport *transp_find(struct sip *sip,
191197
}
192198

193199

200+
static struct le *transp_apply_all(struct sip *sip, enum sip_transp tp, int af,
201+
list_apply_h ah, void *arg)
202+
{
203+
struct le *le;
204+
205+
if (!ah)
206+
return NULL;
207+
208+
for (le = sip->transpl.head; le; le = le->next) {
209+
210+
const struct sip_transport *transp = le->data;
211+
const struct sa *laddr = &transp->laddr;
212+
213+
if (transp->tp != tp)
214+
continue;
215+
216+
if (af != AF_UNSPEC && sa_af(laddr) != af)
217+
continue;
218+
219+
if (ah(le, arg))
220+
return le;
221+
}
222+
223+
return NULL;
224+
}
225+
226+
194227
static struct sip_conn *conn_find(struct sip *sip, const struct sa *paddr,
195228
bool secure)
196229
{
@@ -1401,6 +1434,27 @@ int sip_transp_add_websock(struct sip *sip, enum sip_transp tp,
14011434
}
14021435

14031436

1437+
static bool add_ccert(struct le *le, void *arg)
1438+
{
1439+
const struct sip_transport *transp = le->data;
1440+
struct ccert_data *cc = arg;
1441+
1442+
if (!cc->ccert->he.list)
1443+
hash_append(transp->ht_ccert, cc->hsup, &cc->ccert->he,
1444+
cc->ccert);
1445+
else {
1446+
struct sip_ccert *ccert = mem_zalloc(sizeof(*ccert), NULL);
1447+
if (!ccert)
1448+
return false;
1449+
1450+
ccert->file = cc->ccert->file;
1451+
hash_append(transp->ht_ccert, cc->hsup, &ccert->he, ccert);
1452+
}
1453+
1454+
return false;
1455+
}
1456+
1457+
14041458
/**
14051459
* Add a client certificate to the TLS transport object
14061460
* Client certificates are saved as hash-table.
@@ -1416,10 +1470,9 @@ int sip_transp_add_ccert(struct sip *sip, const struct uri *uri,
14161470
const char *cert)
14171471
{
14181472
int err = 0;
1419-
const struct sip_transport *transp = NULL;
14201473
struct sip_ccert *ccert = NULL;
1474+
struct ccert_data cc_data;
14211475
struct mbuf *sup = NULL;
1422-
uint32_t hsup = 0;
14231476

14241477
if (!sip || !uri || !cert)
14251478
return EINVAL;
@@ -1435,30 +1488,20 @@ int sip_transp_add_ccert(struct sip *sip, const struct uri *uri,
14351488

14361489
mbuf_set_pos(sup, 0);
14371490

1438-
hsup = hash_joaat(mbuf_buf(sup), mbuf_get_left(sup));
1439-
transp = transp_find(sip, SIP_TRANSP_TLS, AF_INET, NULL);
1440-
if (transp) {
1441-
ccert = mem_zalloc(sizeof(*ccert), NULL);
1442-
if (!ccert) {
1443-
err = ENOMEM;
1444-
goto out;
1445-
}
1446-
1447-
pl_set_str(&ccert->file, cert);
1448-
hash_append(transp->ht_ccert, hsup, &ccert->he, ccert);
1491+
ccert = mem_zalloc(sizeof(*ccert), NULL);
1492+
if (!ccert) {
1493+
err = ENOMEM;
1494+
goto out;
14491495
}
1496+
pl_set_str(&ccert->file, cert);
14501497

1451-
transp = transp_find(sip, SIP_TRANSP_TLS, AF_INET6, NULL);
1452-
if (transp) {
1453-
ccert = mem_zalloc(sizeof(*ccert), NULL);
1454-
if (!ccert) {
1455-
err = ENOMEM;
1456-
goto out;
1457-
}
1498+
cc_data.hsup = hash_joaat(mbuf_buf(sup), mbuf_get_left(sup));
1499+
cc_data.ccert = ccert;
14581500

1459-
pl_set_str(&ccert->file, cert);
1460-
hash_append(transp->ht_ccert, hsup, &ccert->he, ccert);
1461-
}
1501+
(void)transp_apply_all(sip, SIP_TRANSP_TLS, AF_INET, add_ccert,
1502+
&cc_data);
1503+
(void)transp_apply_all(sip, SIP_TRANSP_TLS, AF_INET6, add_ccert,
1504+
&cc_data);
14621505

14631506
out:
14641507
mem_deref(sup);

0 commit comments

Comments
 (0)