| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 32 |
static pj_caching_pool caching_pool; |
| 33 |
static pjsip_endpoint *endpt; |
| 34 |
static pjsip_auth_srv auth_srv; |
| 35 |
|
| 36 |
|
| 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 |
|
| 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 |
|
| 64 |
msg_buf = (char*)pj_pool_alloc(pool, size + 1); |
| 65 |
pj_memcpy(msg_buf, data, size); |
| 66 |
msg_buf[size] = '\0'; |
| 67 |
|
| 68 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 105 |
pj_bzero(&rdata, sizeof(rdata)); |
| 106 |
rdata.msg_info.msg = msg; |
| 107 |
rdata.tp_info.pool = pool; |
| 108 |
|
| 109 |
|
| 110 |
int status_code; |
| 111 |
pjsip_auth_srv_verify(&auth_srv, &rdata, &status_code); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 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 |
|
| 122 |
pj_bzero(&rdata, sizeof(rdata)); |
| 123 |
rdata.msg_info.msg = msg; |
| 124 |
rdata.tp_info.pool = pool; |
| 125 |
|
| 126 |
|
| 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 |
|
| 140 |
status = pjsip_endpt_create_response(endpt, &rdata, 401, NULL, &tdata); |
| 141 |
if (status != PJ_SUCCESS || !tdata) |
| 142 |
return; |
| 143 |
|
| 144 |
|
| 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 |
|
| 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 |
|
| 167 |
status = pjsip_endpt_create(&caching_pool.factory, "fuzz", &endpt); |
| 168 |
if (status != PJ_SUCCESS) |
| 169 |
return 0; |
| 170 |
|
| 171 |
|
| 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 |
|
| 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 |
|
| 195 |
pjsip_endpt_release_pool(endpt, pool); |
| 196 |
pjsip_endpt_handle_events(endpt, &timeout); |
| 197 |
|
| 198 |
return 0; |
| 199 |
} |