Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-sdes.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2026 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21
22 #include <pjlib.h>
23 #include <pjlib-util.h>
24 #include <pjmedia.h>
25 #include <pjmedia/transport_srtp.h>
26 #include <pjmedia/transport_loop.h>
27 #include <pjmedia/sdp.h>
28
29 #define kMinInputLength 100
30 #define kMaxInputLength 5120
31
32 static pj_pool_factory *mem;
33
34 static void fuzz_sdes(const uint8_t *data, size_t size)
35 {
36 pjmedia_endpt *endpt = NULL;
37 pj_pool_t *pool = NULL, *sdp_pool = NULL;
38 pj_status_t status;
39 pjmedia_transport *loop_tp = NULL, *srtp_tp = NULL;
40 pjmedia_transport *loop_tp2 = NULL, *srtp_tp2 = NULL;
41 pjmedia_srtp_setting srtp_opt;
42 pjmedia_sdp_session *local_sdp = NULL, *remote_sdp = NULL;
43 char *sdp_str = NULL;
44
45 status = pjmedia_endpt_create(mem, NULL, 1, &endpt);
46 if (status != PJ_SUCCESS)
47 return;
48
49 status = pjmedia_srtp_init_lib(endpt);
50 if (status != PJ_SUCCESS)
51 goto cleanup;
52
53 pool = pj_pool_create(mem, "sdes", 2000, 1000, NULL);
54 if (!pool)
55 goto cleanup;
56
57 sdp_pool = pj_pool_create(mem, "sdp", 2000, 1000, NULL);
58 if (!sdp_pool)
59 goto cleanup;
60
61 status = pjmedia_transport_loop_create(endpt, &loop_tp);
62 if (status != PJ_SUCCESS)
63 goto cleanup;
64
65 pjmedia_srtp_setting_default(&srtp_opt);
66 srtp_opt.use = PJMEDIA_SRTP_OPTIONAL;
67
68 status = pjmedia_transport_srtp_create(endpt, loop_tp, &srtp_opt, &srtp_tp);
69 if (status != PJ_SUCCESS)
70 goto cleanup;
71
72 sdp_str = (char*)pj_pool_alloc(pool, size + 1);
73 if (!sdp_str)
74 goto cleanup;
75
76 pj_memcpy(sdp_str, data, size);
77 sdp_str[size] = '\0';
78
79 if (size < 3 || sdp_str[0] != 'v' || sdp_str[1] != '=')
80 goto cleanup;
81
82 status = pjmedia_sdp_parse(sdp_pool, sdp_str, size, &remote_sdp);
83 if (status != PJ_SUCCESS)
84 goto cleanup;
85
86 status = pjmedia_sdp_validate(remote_sdp);
87 if (status != PJ_SUCCESS)
88 goto cleanup;
89
90 /* Create minimal local SDP */
91 local_sdp = PJ_POOL_ZALLOC_T(sdp_pool, pjmedia_sdp_session);
92 local_sdp->origin.user = pj_str("pjsip");
93 local_sdp->origin.version = local_sdp->origin.id = 1;
94 local_sdp->origin.net_type = pj_str("IN");
95 local_sdp->origin.addr_type = pj_str("IP4");
96 local_sdp->origin.addr = pj_str("127.0.0.1");
97 local_sdp->name = pj_str("pjmedia");
98 local_sdp->time.start = 0;
99 local_sdp->time.stop = 0;
100
101 if (remote_sdp->media_count > 0) {
102 pjmedia_sdp_media *m;
103 pj_uint32_t rem_proto = 0;
104
105 m = PJ_POOL_ZALLOC_T(sdp_pool, pjmedia_sdp_media);
106 m->desc.media = pj_str("audio");
107 m->desc.port = 4000;
108 m->desc.port_count = 1;
109
110 /* Match remote transport protocol to properly test SDES negotiation */
111 rem_proto = pjmedia_sdp_transport_get_proto(&remote_sdp->media[0]->desc.transport);
112 if (rem_proto == PJMEDIA_TP_PROTO_RTP_SAVP) {
113 m->desc.transport = pj_str("RTP/SAVP");
114 } else if (rem_proto == (PJMEDIA_TP_PROTO_RTP_SAVP | PJMEDIA_TP_PROFILE_RTCP_FB)) {
115 m->desc.transport = pj_str("RTP/SAVPF");
116 } else {
117 m->desc.transport = pj_str("RTP/AVP");
118 }
119
120 m->desc.fmt_count = 1;
121 m->desc.fmt[0] = pj_str("0");
122
123 local_sdp->media[local_sdp->media_count++] = m;
124
125 /* Test as answerer: process remote offer with crypto attributes */
126 status = pjmedia_transport_media_create(srtp_tp, sdp_pool, 0,
127 remote_sdp, 0);
128 if (status == PJ_SUCCESS) {
129 /* This triggers parse_attr_crypto() for answerer role */
130 status = pjmedia_transport_encode_sdp(srtp_tp, sdp_pool,
131 local_sdp, remote_sdp, 0);
132
133 /* Also test media_start to complete the negotiation */
134 if (status == PJ_SUCCESS) {
135 pjmedia_transport_media_start(srtp_tp, sdp_pool,
136 local_sdp, remote_sdp, 0);
137 }
138 }
139
140 /* Test as offerer: generate crypto offer, then process answer */
141 status = pjmedia_transport_loop_create(endpt, &loop_tp2);
142 if (status != PJ_SUCCESS)
143 goto cleanup;
144
145 status = pjmedia_transport_srtp_create(endpt, loop_tp2, &srtp_opt,
146 &srtp_tp2);
147 if (status != PJ_SUCCESS)
148 goto cleanup;
149
150 status = pjmedia_transport_media_create(srtp_tp2, sdp_pool, 0,
151 NULL, 0);
152 if (status == PJ_SUCCESS) {
153 pjmedia_sdp_session *offer_sdp;
154
155 /* Create a local offer */
156 offer_sdp = pjmedia_sdp_session_clone(sdp_pool, local_sdp);
157 if (offer_sdp && offer_sdp->media_count > 0) {
158 offer_sdp->media[0]->desc.transport = pj_str("RTP/SAVP");
159
160 /* Generate offer with crypto (offerer role) */
161 status = pjmedia_transport_encode_sdp(srtp_tp2, sdp_pool,
162 offer_sdp, NULL, 0);
163
164 if (status == PJ_SUCCESS) {
165 /* Process remote answer */
166 pjmedia_transport_media_start(srtp_tp2, sdp_pool,
167 offer_sdp, remote_sdp, 0);
168 }
169 }
170 }
171 }
172
173 cleanup:
174 if (srtp_tp2)
175 pjmedia_transport_close(srtp_tp2);
176 else if (loop_tp2)
177 pjmedia_transport_close(loop_tp2);
178 if (srtp_tp)
179 pjmedia_transport_close(srtp_tp);
180 else if (loop_tp)
181 pjmedia_transport_close(loop_tp);
182 if (sdp_pool)
183 pj_pool_release(sdp_pool);
184 if (pool)
185 pj_pool_release(pool);
186 if (endpt)
187 pjmedia_endpt_destroy(endpt);
188 }
189
190 extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
191 {
192 pj_caching_pool caching_pool;
193 pj_status_t status;
194
195 if (Size < kMinInputLength || Size > kMaxInputLength) {
196 return 1;
197 }
198
199 status = pj_init();
200 if (status != PJ_SUCCESS)
201 return 0;
202
203 pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
204 pj_log_set_level(0);
205
206 mem = &caching_pool.factory;
207
208 /* Fuzz SRTP SDES */
209 fuzz_sdes(Data, Size);
210
211 pj_caching_pool_destroy(&caching_pool);
212
213 return 0;
214 }