Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-auth.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 #include <string.h>
22
23 #include <pjlib.h>
24 #include <pjlib-util.h>
25 #include <pjsip.h>
26 #include <pjsip/sip_auth.h>
27
28 #define POOL_SIZE 4000
29 #define MAX_FUZZ_MSG_SIZE (POOL_SIZE - 1)
30
31 /* Global resources (one-time initialization) */
32 static pj_caching_pool caching_pool;
33 static pjsip_endpoint *endpt;
34 static pjsip_auth_srv auth_srv;
35
36 /* Credential lookup for server authentication */
37 static pj_status_t lookup_cred(pj_pool_t *pool, const pj_str_t *realm,
38 const pj_str_t *acc_name, pjsip_cred_info *cred)
39 {
40 PJ_UNUSED_ARG(pool);
41 pj_bzero(cred, sizeof(*cred));
42 cred->realm = *realm;
43 cred->username = *acc_name;
44 cred->data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
45 cred->data = pj_str("secret");
46 return PJ_SUCCESS;
47 }
48
49 /* Helper: Parse SIP message from fuzzer input */
50 static pjsip_msg* parse_sip_message(pj_pool_t *pool, const uint8_t *data,
51 size_t size, int require_full_headers)
52 {
53 char *msg_buf;
54 pjsip_parser_err_report err_list;
55 pjsip_msg *msg;
56
57 if (size < 10)
58 return NULL;
59
60 if (size > MAX_FUZZ_MSG_SIZE)
61 size = MAX_FUZZ_MSG_SIZE;
62
63 /* Copy to null-terminated buffer */
64 msg_buf = (char*)pj_pool_alloc(pool, size + 1);
65 pj_memcpy(msg_buf, data, size);
66 msg_buf[size] = '\0';
67
68 /* Parse SIP message */
69 pj_list_init(&err_list);
70 msg = pjsip_parse_msg(pool, msg_buf, (pj_size_t)size, &err_list);
71
72 if (!msg || msg->type != PJSIP_REQUEST_MSG || !msg->line.req.uri)
73 return NULL;
74
75 /* Check required headers for response creation */
76 if (require_full_headers) {
77 if (!pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL) ||
78 !pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL) ||
79 !pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL) ||
80 !pjsip_msg_find_hdr(msg, PJSIP_H_CALL_ID, NULL) ||
81 !pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL))
82 return NULL;
83 }
84
85 return msg;
86 }
87
88 /* Server authentication verification */
89 static void test_auth_server_verify(pj_pool_t *pool, pjsip_msg *msg)
90 {
91 pjsip_rx_data rdata;
92 pjsip_authorization_hdr *auth_hdr;
93
94 /* Look for Authorization or Proxy-Authorization header */
95 auth_hdr = (pjsip_authorization_hdr*)
96 pjsip_msg_find_hdr(msg, PJSIP_H_AUTHORIZATION, NULL);
97 if (!auth_hdr) {
98 auth_hdr = (pjsip_authorization_hdr*)
99 pjsip_msg_find_hdr(msg, PJSIP_H_PROXY_AUTHORIZATION, NULL);
100 }
101 if (!auth_hdr)
102 return;
103
104 /* Setup minimal rdata */
105 pj_bzero(&rdata, sizeof(rdata));
106 rdata.msg_info.msg = msg;
107 rdata.tp_info.pool = pool;
108
109 /* Test verification */
110 int status_code;
111 pjsip_auth_srv_verify(&auth_srv, &rdata, &status_code);
112 }
113
114 /* Server challenge generation */
115 static void test_auth_server_challenge(pj_pool_t *pool, pjsip_msg *msg)
116 {
117 pjsip_rx_data rdata;
118 pjsip_tx_data *tdata;
119 pj_status_t status;
120
121 /* Setup minimal rdata */
122 pj_bzero(&rdata, sizeof(rdata));
123 rdata.msg_info.msg = msg;
124 rdata.tp_info.pool = pool;
125
126 /* Populate required msg_info shortcuts; bail if any are missing */
127 rdata.msg_info.via = (pjsip_via_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_VIA, NULL);
128 rdata.msg_info.from = (pjsip_fromto_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_FROM, NULL);
129 rdata.msg_info.to = (pjsip_fromto_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_TO, NULL);
130 rdata.msg_info.cseq = (pjsip_cseq_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CSEQ, NULL);
131 rdata.msg_info.cid = (pjsip_cid_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CALL_ID, NULL);
132 if (!rdata.msg_info.via || !rdata.msg_info.from || !rdata.msg_info.to ||
133 !rdata.msg_info.cseq || !rdata.msg_info.cid)
134 return;
135
136 if (msg->line.req.method.id == PJSIP_ACK_METHOD)
137 return;
138
139 /* Create 401 response */
140 status = pjsip_endpt_create_response(endpt, &rdata, 401, NULL, &tdata);
141 if (status != PJ_SUCCESS || !tdata)
142 return;
143
144 /* Add authentication challenge */
145 pjsip_auth_srv_challenge(&auth_srv, NULL, NULL, NULL, PJ_FALSE, tdata);
146 pjsip_tx_data_dec_ref(tdata);
147 }
148
149 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
150 {
151 static int initialized = 0;
152 pj_pool_t *pool;
153 pjsip_msg *msg;
154 pj_time_val timeout = {0, 0};
155
156 /* === One-time initialization === */
157 if (!initialized) {
158 pj_status_t status;
159 pj_pool_t *init_pool;
160 pj_str_t realm;
161
162 pj_init();
163 pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
164 pj_log_set_level(0);
165
166 /* Create SIP endpoint */
167 status = pjsip_endpt_create(&caching_pool.factory, "fuzz", &endpt);
168 if (status != PJ_SUCCESS)
169 return 0;
170
171 /* Initialize auth server */
172 init_pool = pj_pool_create(&caching_pool.factory, "init", 1000, 1000, NULL);
173 realm = pj_str("example.com");
174 pjsip_auth_srv_init(init_pool, &auth_srv, &realm, &lookup_cred, 0);
175
176 initialized = 1;
177 }
178
179 pool = pjsip_endpt_create_pool(endpt, "fuzz", POOL_SIZE, POOL_SIZE);
180 if (!pool)
181 return 0;
182
183 /* Parse message and test auth server functions */
184 msg = parse_sip_message(pool, Data, Size, 0);
185 if (msg) {
186 test_auth_server_verify(pool, msg);
187 }
188
189 msg = parse_sip_message(pool, Data, Size, 1);
190 if (msg) {
191 test_auth_server_challenge(pool, msg);
192 }
193
194 /* Cleanup */
195 pjsip_endpt_release_pool(endpt, pool);
196 pjsip_endpt_handle_events(endpt, &timeout);
197
198 return 0;
199 }