A SIP 401 Unauthorized response is a question, not a refusal. The server is asking the client to prove who it is, and the request will succeed once the client answers with valid credentials. RFC 3261 defines it in section 21.4.2 in one line: "The request requires user authentication." Its twin, 407 Proxy Authentication Required (section 21.4.8), means the same thing, except that a proxy is asking rather than the final destination.

That is why the first 401 or 407 in a trace is almost never the problem. Nearly every successful registration and most outbound trunk calls begin with one. The problem is the second challenge: the client sent credentials and the server asked again. This guide shows how to tell the normal challenge from the failing one, what causes the failing one on Asterisk, FreePBX, FreeSWITCH, SIP trunks and browser phones, and how to fix each cause. For every other code, see the SIP response codes lookup.

401 or 407: who is asking

RFC 3261 (section 22.1) splits the job by role. User agents, registrars and redirect servers challenge with 401. Proxies must not use 401; they challenge with 407. Each code has its own pair of headers:

401 Unauthorized 407 Proxy Authentication Required
Sent by The destination: a registrar, a PBX, the called user agent A proxy on the path: a carrier's edge proxy, a gateway
Challenge header WWW-Authenticate Proxy-Authenticate
Client answers with Authorization Proxy-Authorization
Typically seen on REGISTER, and INVITEs to Asterisk INVITEs to SIP trunk providers and to FreeSWITCH

In practice you read them the same way. Asterisk's PJSIP stack always challenges with 401, for calls as well as registrations: its request authenticator (pjsip_distributor.c) builds every challenge as a 401. FreeSWITCH challenges registrations with 401 and calls with 407, as its documentation shows. Many trunk providers answer an unauthenticated INVITE with 407 because the challenge comes from their edge proxy.

What a normal challenge looks like

A healthy registration is four messages:

REGISTER (no credentials) ---> <--- 401 Unauthorized WWW-Authenticate: Digest realm="pbx.example.com", nonce="...", algorithm=MD5, qop="auth" REGISTER (Authorization: Digest username="1001", realm="pbx.example.com", nonce="...", response="...") ---> <--- 200 OK 

The client hashes its username, the realm and its password together with the server's nonce, and sends the result. The server does the same calculation with the password it holds. If the two match, the request goes through, and the password itself never crosses the wire. Two details matter when you read a trace:

  • The CSeq goes up. RFC 3261 (section 22.2) requires the resent request to carry a higher CSeq. Same Call-ID, CSeq one higher, an Authorization header added: that is the retry.
  • The realm names the credentials. A client that holds credentials for one realm will not use them for another.

When a 401 is a real failure

The failure signature is a second challenge in reply to a request that already carried credentials. RFC 3261 says a server rejecting credentials "may repeat its challenge, or it may respond with a 403 Forbidden", and that the client "MUST NOT re-attempt requests with the credentials that have just been rejected." That is why softphones give up with messages such as "authentication loop".

One exception: a stale nonce. If the second challenge carries stale=true, the server accepted the password but the nonce had expired, and the client may retry at once with the fresh nonce. Asterisk's nonces live for 32 seconds by default (nonce_lifetime).

What you see What it means
401/407, request again with credentials, then 200 or 100 Trying Normal. Nothing to fix.
401/407, request with credentials, 401/407 again (no stale=true) Credentials rejected: wrong username, password or realm, or the server doesn't know the user
401/407, request with credentials, 403 Credentials rejected (common at SIP providers), or accepted and then refused by policy
401/407, and the client never resends The client has no credentials for that realm, or doesn't support the offered algorithm
Second challenge with stale=true Expired nonce. The client should retry automatically

Repeated 401 from Asterisk

On Asterisk, a wrong password and an unknown user look exactly the same from the phone: both get another 401. This is deliberate. When no endpoint matches, Asterisk challenges with an internal "artificial" endpoint rather than revealing that the account doesn't exist; its source code says the aim is to avoid enabling "the discovery of SIP accounts". The difference shows only in the log (/var/log/asterisk/full), where each failure produces a NOTICE line ending in the reason:

  • Failed to authenticate: Asterisk found the endpoint and the digest did not match. Check the password, and check that the username in the phone's Authorization header matches the username in the endpoint's auth section exactly.
  • No matching endpoint found: Asterisk couldn't work out which endpoint the request belongs to, so no password was ever checked. By default PJSIP identifies an endpoint by the user part of the From header (identify_by=username). If the phone's From user isn't the endpoint's name, set identify_by=auth_username on the endpoint, or make the From user match. Trunks are normally identified by IP address through an identify section instead.

A real case: a browser phone stuck in a 401 loop

A user on the Asterisk forum spent weeks on a WebRTC client built with SIP.js 0.21 that looped on 401, while UDP phones on the same server worked. The Asterisk log said No matching endpoint found, and the REGISTER came from sip:anonymous.3c5i34@anonymous.invalid. Joshua Colp, the Asterisk project lead, pointed out that the client was putting "anonymous" in the From user.

The password was never the problem. SIP.js creates an anonymous user agent when given no address of record, and the code passed username, password and uri as top-level options, which SIP.js 0.21's SimpleUser doesn't read. It expects aor, plus userAgentOptions.authorizationUsername and authorizationPassword. If a browser phone loops on 401, check the From header of its REGISTER before touching the password.

FreePBX extensions and scanner noise

On FreePBX, the extension number is both the endpoint name and the auth username, and the password is the extension's Secret. A phone set up with a different auth username, or a secret copied with a trailing space, gives Failed to authenticate.

Logs full of 401s you didn't cause are usually scanners. Every probe of an internet-facing SIP port is challenged and logged. Restrict who can reach the port with a firewall or ACL, and use a tool such as fail2ban for repeat offenders.

401 or 407 from your SIP provider

When your PBX registers to a provider, or places a call through it, the provider is asking. Check, in this order:

  1. The authentication username. Many providers use an auth username that differs from your number or account number; VoIP.ms sub-accounts, for example, take the form 123456_name. In Asterisk it is the username in the auth section used by outbound_auth; in a FreeSWITCH gateway it is auth-username when it differs from username.
  2. The password, best retyped rather than pasted, in case of hidden characters.
  3. The realm. When Asterisk is the client, leave the auth section's realm empty or set it to *. The Asterisk documentation says to set a realm only if you know exactly what the server will send. Don't share one auth section between an endpoint's auth and its outbound_auth.
  4. The right auth on the right request. The registration section's outbound_auth answers challenges to REGISTER; the endpoint section's outbound_auth answers challenges to INVITE. A trunk that registers but whose calls fail with 401 or 407 is usually missing outbound_auth on the endpoint.

An Asterisk detail that catches people out: once a provider rejects the credentials, Asterisk stops trying. auth_rejection_permanent defaults to yes, so registration isn't attempted again until the configuration is reloaded. After fixing the credentials, reload (module reload res_pjsip_outbound_registration.so, or pjsip send register for one registration) and check with pjsip show registrations.

MD5, SHA-256 and the algorithm mismatch

Digest authentication was built on MD5. RFC 7616 added SHA-256 and SHA-512/256, and RFC 8760 brought them to SIP. A server that supports several algorithms sends one challenge header per algorithm, most preferred first, and the client answers the first it supports.

Asterisk gained SHA-256 and SHA-512-256 in 20.12.0, 21.7.0 and 22.2.0. Two auth options control it, both defaulting to MD5:

  • supported_algorithms_uas: which challenges Asterisk sends to phones, in order.
  • supported_algorithms_uac: which challenges Asterisk will answer when a provider asks. If a provider challenges only with SHA-256 and this list holds only MD5, the request fails again.

If you enable SHA-256 towards phones, keep MD5 in the list and test every phone model. If you store pre-hashed credentials (password_digest), you need one per listed algorithm. pjproject show buildopts shows which algorithms your build supports.

Capturing the exchange

  • Asterisk: pjsip set logger on (or pjsip set logger host 203.0.113.10), reproduce, pjsip set logger off, then read the NOTICE line in /var/log/asterisk/full.
  • FreeSWITCH: sofia profile internal siptrace on (or external for trunks), reproduce, then turn it off.
  • Any Linux server: sngrep draws the exchange as a ladder diagram, which makes a second challenge obvious.
  • Browser phone: the developer console (F12); most JavaScript SIP libraries can log every SIP message there.

Compare the username and realm in the client's Authorization (or Proxy-Authorization) header with what the server expects. If a proxy sits between you and the provider, the Via and Server headers show which hop sent the challenge.

FAQ

Is a SIP 401 Unauthorized an error? Not on its own. The first 401 or 407 is the normal start of digest authentication. It is a problem only when the server challenges again after credentials were sent.

What is the difference between 401 and 407? Who is asking: the destination (401, WWW-Authenticate/Authorization) or a proxy on the path (407, Proxy-Authenticate/Proxy-Authorization).

Why does my phone keep getting 401 when the password is right? On Asterisk, usually because the server can't identify the endpoint at all (No matching endpoint found). Check that the From user (or, with identify_by=auth_username, the auth username) matches the endpoint's name.

What is the difference between 401 and 403? A 401 asks for credentials and can succeed once they're supplied. A 403 refuses outright; repeating it unchanged won't help.

Originally published on SoftPage. See also SIP 403 Forbidden.