2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
22 * \brief Generate security events in the PJSIP channel
24 * \author Joshua Colp <jcolp@digium.com>
29 ASTERISK_REGISTER_FILE()
33 #include "asterisk/res_pjsip.h"
34 #include "asterisk/security_events.h"
36 static enum ast_transport security_event_get_transport(pjsip_rx_data *rdata)
38 if (rdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP ||
39 rdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP6) {
40 return AST_TRANSPORT_UDP;
41 } else if (rdata->tp_info.transport->key.type == PJSIP_TRANSPORT_TCP ||
42 rdata->tp_info.transport->key.type == PJSIP_TRANSPORT_TCP6) {
43 return AST_TRANSPORT_TCP;
44 } else if (rdata->tp_info.transport->key.type == PJSIP_TRANSPORT_TLS ||
45 rdata->tp_info.transport->key.type == PJSIP_TRANSPORT_TLS6) {
46 return AST_TRANSPORT_TLS;
47 } else if (!strcmp(rdata->tp_info.transport->type_name, "WS")) {
48 return AST_TRANSPORT_WS;
49 } else if (!strcmp(rdata->tp_info.transport->type_name, "WSS")) {
50 return AST_TRANSPORT_WSS;
56 static void security_event_populate(pjsip_rx_data *rdata, char *call_id, size_t call_id_size, struct ast_sockaddr *local, struct ast_sockaddr *remote)
58 char host[NI_MAXHOST];
60 ast_copy_pj_str(call_id, &rdata->msg_info.cid->id, call_id_size);
62 ast_copy_pj_str(host, &rdata->tp_info.transport->local_name.host, sizeof(host));
63 ast_sockaddr_parse(local, host, PARSE_PORT_FORBID);
64 ast_sockaddr_set_port(local, rdata->tp_info.transport->local_name.port);
66 ast_sockaddr_parse(remote, rdata->pkt_info.src_name, PARSE_PORT_FORBID);
67 ast_sockaddr_set_port(remote, rdata->pkt_info.src_port);
70 static const char *get_account_id(struct ast_sip_endpoint *endpoint)
72 RAII_VAR(struct ast_sip_endpoint *, artificial, ast_sip_get_artificial_endpoint(), ao2_cleanup);
74 return endpoint == artificial ? "<unknown>" : ast_sorcery_object_get_id(endpoint);
77 void ast_sip_report_invalid_endpoint(const char *name, pjsip_rx_data *rdata)
79 enum ast_transport transport = security_event_get_transport(rdata);
80 char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1];
81 struct ast_sockaddr local, remote;
83 struct ast_security_event_inval_acct_id inval_acct_id = {
84 .common.event_type = AST_SECURITY_EVENT_INVAL_ACCT_ID,
85 .common.version = AST_SECURITY_EVENT_INVAL_ACCT_ID_VERSION,
86 .common.service = "PJSIP",
87 .common.account_id = name,
88 .common.local_addr = {
90 .transport = transport,
92 .common.remote_addr = {
94 .transport = transport,
96 .common.session_id = call_id,
99 security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote);
101 ast_security_event_report(AST_SEC_EVT(&inval_acct_id));
104 void ast_sip_report_failed_acl(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *name)
106 enum ast_transport transport = security_event_get_transport(rdata);
107 char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1];
108 struct ast_sockaddr local, remote;
110 struct ast_security_event_failed_acl failed_acl_event = {
111 .common.event_type = AST_SECURITY_EVENT_FAILED_ACL,
112 .common.version = AST_SECURITY_EVENT_FAILED_ACL_VERSION,
113 .common.service = "PJSIP",
114 .common.account_id = get_account_id(endpoint),
115 .common.local_addr = {
117 .transport = transport,
119 .common.remote_addr = {
121 .transport = transport,
123 .common.session_id = call_id,
127 security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote);
129 ast_security_event_report(AST_SEC_EVT(&failed_acl_event));
132 void ast_sip_report_auth_failed_challenge_response(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
134 pjsip_authorization_hdr *auth = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_AUTHORIZATION, NULL);
135 enum ast_transport transport = security_event_get_transport(rdata);
136 char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1];
137 char nonce[64] = "", response[256] = "";
138 struct ast_sockaddr local, remote;
140 struct ast_security_event_chal_resp_failed chal_resp_failed = {
141 .common.event_type = AST_SECURITY_EVENT_CHAL_RESP_FAILED,
142 .common.version = AST_SECURITY_EVENT_CHAL_RESP_FAILED_VERSION,
143 .common.service = "PJSIP",
144 .common.account_id = get_account_id(endpoint),
145 .common.local_addr = {
147 .transport = transport,
149 .common.remote_addr = {
151 .transport = transport,
153 .common.session_id = call_id,
156 .response = response,
157 .expected_response = "",
160 if (auth && !pj_strcmp2(&auth->scheme, "Digest")) {
161 ast_copy_pj_str(nonce, &auth->credential.digest.nonce, sizeof(nonce));
162 ast_copy_pj_str(response, &auth->credential.digest.response, sizeof(response));
165 security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote);
167 ast_security_event_report(AST_SEC_EVT(&chal_resp_failed));
170 void ast_sip_report_auth_success(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
172 pjsip_authorization_hdr *auth = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_AUTHORIZATION, NULL);
173 enum ast_transport transport = security_event_get_transport(rdata);
174 char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1];
175 struct ast_sockaddr local, remote;
177 struct ast_security_event_successful_auth successful_auth = {
178 .common.event_type = AST_SECURITY_EVENT_SUCCESSFUL_AUTH,
179 .common.version = AST_SECURITY_EVENT_SUCCESSFUL_AUTH_VERSION,
180 .common.service = "PJSIP",
181 .common.account_id = get_account_id(endpoint),
182 .common.local_addr = {
184 .transport = transport,
186 .common.remote_addr = {
188 .transport = transport,
190 .common.session_id = call_id,
191 .using_password = auth ? (uint32_t *)1 : (uint32_t *)0,
194 security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote);
196 ast_security_event_report(AST_SEC_EVT(&successful_auth));
199 void ast_sip_report_auth_challenge_sent(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_tx_data *tdata)
201 pjsip_www_authenticate_hdr *auth = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_WWW_AUTHENTICATE, NULL);
202 enum ast_transport transport = security_event_get_transport(rdata);
203 char nonce[64] = "", call_id[pj_strlen(&rdata->msg_info.cid->id) + 1];
204 struct ast_sockaddr local, remote;
206 struct ast_security_event_chal_sent chal_sent = {
207 .common.event_type = AST_SECURITY_EVENT_CHAL_SENT,
208 .common.version = AST_SECURITY_EVENT_CHAL_SENT_VERSION,
209 .common.service = "PJSIP",
210 .common.account_id = get_account_id(endpoint),
211 .common.local_addr = {
213 .transport = transport,
215 .common.remote_addr = {
217 .transport = transport,
219 .common.session_id = call_id,
223 if (auth && !pj_strcmp2(&auth->scheme, "digest")) {
224 ast_copy_pj_str(nonce, &auth->challenge.digest.nonce, sizeof(nonce));
227 security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote);
229 ast_security_event_report(AST_SEC_EVT(&chal_sent));
232 void ast_sip_report_req_no_support(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata,
233 const char* req_type)
235 enum ast_transport transport = security_event_get_transport(rdata);
236 char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1];
237 struct ast_sockaddr local, remote;
239 struct ast_security_event_req_no_support req_no_support_event = {
240 .common.event_type = AST_SECURITY_EVENT_REQ_NO_SUPPORT,
241 .common.version = AST_SECURITY_EVENT_REQ_NO_SUPPORT_VERSION,
242 .common.service = "PJSIP",
243 .common.account_id = get_account_id(endpoint),
244 .common.local_addr = {
246 .transport = transport,
248 .common.remote_addr = {
250 .transport = transport,
252 .common.session_id = call_id,
253 .request_type = req_type
256 security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote);
258 ast_security_event_report(AST_SEC_EVT(&req_no_support_event));
261 void ast_sip_report_mem_limit(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
263 enum ast_transport transport = security_event_get_transport(rdata);
264 char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1];
265 struct ast_sockaddr local, remote;
267 struct ast_security_event_mem_limit mem_limit_event = {
268 .common.event_type = AST_SECURITY_EVENT_MEM_LIMIT,
269 .common.version = AST_SECURITY_EVENT_MEM_LIMIT_VERSION,
270 .common.service = "PJSIP",
271 .common.account_id = get_account_id(endpoint),
272 .common.local_addr = {
274 .transport = transport,
276 .common.remote_addr = {
278 .transport = transport,
280 .common.session_id = call_id
283 security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote);
285 ast_security_event_report(AST_SEC_EVT(&mem_limit_event));