diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c
index 0825d2e8891c769d4c462344805a27fe42f2ae67..df37fa57898730031276fb16bbbd11c9e85db3b9 100644
--- a/res/res_pjsip_outbound_registration.c
+++ b/res/res_pjsip_outbound_registration.c
@@ -1155,6 +1155,39 @@ static int handle_registration_response(void *data)
 	ast_debug(1, "Processing REGISTER response %d from server '%s' for client '%s'\n",
 			response->code, server_uri, client_uri);
 
+	if (response->code == 401 || response->code == 407 || response->code == 494) {
+		/* Store MEDIASEC headers */
+		if (response->client_state->mediasec) {
+			struct ast_sip_endpoint *endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
+					response->client_state->registration_name);
+			static const pj_str_t headerName = { "Security-Server", 15 };
+			pjsip_generic_string_hdr *secSrv = NULL;
+
+			if (!endpoint) {
+				ast_log(LOG_ERROR, "No endpoint found to store mediasec headers\n");
+				return -1;
+			}
+			if(AST_LIST_EMPTY(&endpoint->security_mechanisms)) {
+				secSrv = pjsip_msg_find_hdr_by_name(response->rdata->msg_info.msg, &headerName, NULL);
+				struct security_mechanism *sec_mechanism;
+				while (secSrv) {
+					response->client_state->is494=0;
+
+					sec_mechanism = ast_calloc(1, sizeof(*sec_mechanism) + pj_strlen(&secSrv->hvalue));
+					if (!sec_mechanism) {
+						ast_log(LOG_ERROR, "Unable to store server security mechanisms\n");
+						clear_endpoint_security_mechanisms(endpoint);
+						return;
+					}
+					ast_copy_pj_str(&sec_mechanism->value, &secSrv->hvalue, pj_strlen(&secSrv->hvalue) + 1);
+					AST_LIST_INSERT_TAIL(&endpoint->security_mechanisms, sec_mechanism, entry);
+					ast_debug(1, "Store \"Security-Verify\" header: %s\n", sec_mechanism->value);
+					secSrv = pjsip_msg_find_hdr_by_name(response->rdata->msg_info.msg, &headerName, secSrv->next);
+				}
+			}
+		}
+	}
+
 	if (response->code == 408 || response->code == 503) {
 		if ((ast_sip_failover_request(response->old_request))) {
 			int res = registration_client_send(response->client_state, response->old_request);
@@ -1178,36 +1211,20 @@ static int handle_registration_response(void *data)
 			ast_debug(1, "Sending authenticated REGISTER to server '%s' from client '%s'\n",
 					server_uri, client_uri);
 
-			/* Store & Add MEDIASEC headers */
+			/* Add MEDIASEC headers */
 			if (response->client_state->mediasec) {
 				struct ast_sip_endpoint *endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
 						response->client_state->registration_name);
-				static const pj_str_t headerName = { "Security-Server", 15 };
-				pjsip_generic_string_hdr *secSrv = NULL;
 
 				if (!endpoint) {
 					ast_log(LOG_ERROR, "No endpoint found to store/add mediasec headers\n");
 					return -1;
 				}
-				if(AST_LIST_EMPTY(&endpoint->security_mechanisms)) {
-					secSrv = pjsip_msg_find_hdr_by_name(response->rdata->msg_info.msg, &headerName, NULL);
+				if(!AST_LIST_EMPTY(&endpoint->security_mechanisms)) {
 					struct security_mechanism *sec_mechanism;
-					while (secSrv) {
-						response->client_state->is494=0;
-						char local_info[pj_strlen(&secSrv->hvalue) + 1];
-
-						sec_mechanism = ast_calloc(1, sizeof(*sec_mechanism) + pj_strlen(&secSrv->hvalue));
-						if (!sec_mechanism) {
-							ast_log(LOG_ERROR, "Unable to store server security mechanisms\n");
-							clear_endpoint_security_mechanisms(endpoint);
-							return;
-						}
-						pj_strdup_with_null(tdata->pool, &local_info, &secSrv->hvalue);
-						ast_copy_pj_str(&sec_mechanism->value, local_info, sizeof(local_info));
-						AST_LIST_INSERT_TAIL(&endpoint->security_mechanisms, sec_mechanism, entry);
-						ast_debug(1, "Store & Add \"Security-Verify\" header: %s\n", sec_mechanism->value);
+					AST_LIST_TRAVERSE(&endpoint->security_mechanisms, sec_mechanism, entry) {
+						ast_debug(1, "Adding security header: %s\n", sec_mechanism->value);
 						ast_sip_add_header(tdata,"Security-Verify",sec_mechanism->value);
-						secSrv = pjsip_msg_find_hdr_by_name(response->rdata->msg_info.msg, &headerName, secSrv->next);
 					}
 				}
 			}