Howwise RSS
VoIP & Phone Systems8 min read

Asterisk PJSIP Realm Mismatch 401: Find and Fix the Cause

Short answer

An Asterisk PJSIP realm mismatch 401 means the digest the client returned does not match what Asterisk computed. Read realm= in the WWW-Authenticate header, then compare the username, realm, From user and endpoint identification.

Key takeaways
  • The realm in the 401 WWW-Authenticate header is the only realm that matters, and the client hashes username:realm:password with it.
  • A stored md5_cred hash is bound to one realm and username, so changing the realm or the endpoint name silently breaks it.
  • On a trunk, leave realm blank in the [auth] section unless it exactly equals the provider's challenge realm.
  • 'No matching endpoint found' is an identification problem (From user, auth username or source IP), not a password problem.
  • Always capture the full REGISTER, 401, second REGISTER exchange with pjsip set logger on before changing anything.

How does digest authentication cause an Asterisk PJSIP realm mismatch 401?

SIP digest authentication never sends the password. The server answers the first request with a 401 containing a nonce and a realm. The client computes HA1 = MD5(username:realm:password), HA2 = MD5(method:uri), and a response hash over HA1, the nonce and HA2, then repeats the request with an Authorization header.

Because the realm is part of HA1, a credential stored or typed for a different realm gives a different hash. The server rejects it even though the password is correct. That is why an asterisk pjsip 401 with the right password is usually a mismatch of realm, username or identification, not a typo.

This is the shape of a healthy exchange as seen with pjsip set logger on. The realm to read is in the 401, and the Authorization header must echo it:

<--- Received SIP request from UDP:10.0.0.50:5060 --->
REGISTER sip:pbx.example.com SIP/2.0
From: <sip:101@pbx.example.com>;tag=abc
To: <sip:101@pbx.example.com>
Contact: <sip:101@10.0.0.50:5060>

<--- Transmitting SIP response to UDP:10.0.0.50:5060 --->
SIP/2.0 401 Unauthorized
WWW-Authenticate: Digest realm="asterisk",nonce="1732800000/7f3c...",opaque="5a1e...",algorithm=MD5,qop="auth"

<--- Received SIP request from UDP:10.0.0.50:5060 --->
REGISTER sip:pbx.example.com SIP/2.0
Authorization: Digest username="101", realm="asterisk", nonce="1732800000/7f3c...",
  uri="sip:pbx.example.com", response="9b2c...", algorithm=MD5, cnonce="x1", qop=auth, nc=00000001

<--- Transmitting SIP response --->
SIP/2.0 200 OK
Tip: Compare three things in the second REGISTER: username= against your [auth] username, realm= against the 401, and the From user against your endpoint name. Whichever differs is your bug.

What is the difference between 401, 403 and 'No matching endpoint found'?

A 401 is a challenge. It is normal on the first request and also appears again if the second request carried a wrong response. Asterisk deliberately challenges unknown users too, using the default realm, so an attacker cannot tell a valid endpoint from an invalid one.

A 403 means Asterisk will not proceed: an ACL or contact_acl rejected the source, or authentication failed and the rejection is treated as permanent. On trunks, the registration option auth_rejection_permanent (default yes) makes the registration enter a Rejected state after a 403 instead of retrying forever.

The log lines tell you which layer failed:

Log or trace lineLayerMeaning
No matching endpoint foundIdentificationFrom user, auth username and source IP matched no endpoint or identify section
Failed to authenticateAuthenticationEndpoint found, but the digest response did not match (realm, username, password or hash)
Rejected by ACL / ACL rejectedAccess controlSource IP blocked by acl or contact_acl
401 repeated with the same nonceClientClient is not computing a new response, often a wrong realm or a stuck stale nonce
403 Forbidden after the second REGISTERAuthenticationBad credentials treated as permanent, or the provider refused the account
Tip: Security events are logged as 'Request 'REGISTER' from ... failed for '1.2.3.4:5060' (callid: ...) - Failed to authenticate' or '- No matching endpoint found'. Both are what fail2ban should watch.

Why does the phone get 401 when username and password look right?

For inbound phones, the realm is set in the [auth] section. If it is blank, Asterisk uses the default_realm from the [global] section, which is asterisk. Phones normally just echo whatever realm they receive, so a plain-text password works whatever the realm is.

The failure appears when you use auth_type=md5. Then Asterisk does not store the password, only md5_cred, which is the MD5 of username:realm:password. If you generated it with one realm and later set another, or renamed the username, every attempt fails.

Generate the hash with the exact realm the server will send:

# hash for username 101, realm asterisk, password S3cret
echo -n '101:asterisk:S3cret' | md5sum

# pjsip.conf
[101-auth]
type=auth
auth_type=md5
username=101
realm=asterisk
md5_cred=<hash from above>

# versus plain storage, immune to realm changes
[101-auth]
type=auth
auth_type=userpass
username=101
password=S3cret
Warning: Use echo -n. Without it a newline is hashed and the result never matches. Spaces and special characters in a password also need care: a trailing space copied from a web page is part of the password.

How do I match endpoint, auth username and From user?

Asterisk first identifies the endpoint, then authenticates it with the [auth] object listed in the endpoint's auth= option. By default identify_by is username,ip: the From user must equal the endpoint section name, or the source IP must match an [identify] section. Adding auth_username lets the Authorization username identify the endpoint instead.

A common loop: the phone sends From: 1001 and authenticates as user 101, but the endpoint is named 101. Asterisk logs 'No matching endpoint found' and keeps challenging. Fix it by naming the endpoint to match the From user, or by letting the auth username identify it.

This pairing shows a minimal phone endpoint where the names are deliberately different:

[desk-101]
type=endpoint
context=from-internal
disallow=all
allow=ulaw
auth=desk-101-auth
aors=desk-101
identify_by=username,auth_username

[desk-101-auth]
type=auth
auth_type=userpass
username=101
password=S3cret

[desk-101]
type=aor
max_contacts=1
Tip: With identify_by including auth_username, the auth username 101 can identify the endpoint desk-101 only if the [auth] username matches what the phone sends. Keep the names aligned where possible; it makes traces far easier to read.

Why does my outbound trunk registration fail with 401?

For a trunk, Asterisk is the client. It answers whatever realm the provider sends. The realm option in the [auth] section is optional: if blank, Asterisk accepts any challenge realm. If set, it must equal the provider's realm exactly, otherwise Asterisk finds no usable credentials and the registration fails.

The next suspects are the username fields. The URI identity (client_uri) is the SIP address being registered, while the [auth] username is the digest user. Many providers want a different authentication user than the account number, so set them separately. server_uri is the registrar; client_uri is user@domain.

Compare a failing and a working trunk:

; BEFORE: realm forced, wrong auth user
[prov-auth]
type=auth
auth_type=userpass
username=5551234
realm=sip.example.com
password=Secret

; AFTER: realm left blank, provider's auth user
[prov-auth]
type=auth
auth_type=userpass
username=authuser77
password=Secret

[prov-reg]
type=registration
outbound_auth=prov-auth
server_uri=sip:sip.example.com
client_uri=sip:5551234@sip.example.com
retry_interval=60
forbidden_retry_interval=600
expiration=300
line=yes
endpoint=prov

[prov]
type=endpoint
context=from-trunk
disallow=all
allow=ulaw
outbound_auth=prov-auth
aors=prov
from_user=5551234
from_domain=sip.example.com

[prov]
type=aor
contact=sip:sip.example.com

[prov]
type=identify
endpoint=prov
match=203.0.113.10
Warning: A wrong password sent repeatedly can get your IP blocked by the provider. To halt retries immediately, remove or comment out the [prov-reg] section (or set it so it is not loaded), then run pjsip reload. A reload alone does not stop a registration that still has bad credentials. After the password is corrected, re-add the section and run pjsip reload again.

What do from_domain, from_user and identify mean on a trunk?

Registration alone does not make incoming calls work. Calls from the provider arrive as INVITEs and Asterisk must map them to an endpoint. Providers rarely send your endpoint name as the From user, so an [identify] section with match= the provider IP (or CIDR) is the dependable method. In FreePBX this is the trunk's 'Match (Permit)' field in the PJSIP Settings tab.

from_user and from_domain override what Asterisk puts in the From header of outbound requests. Providers that reject with 403 or 'caller not authorised' often require the account number as from_user and their domain as from_domain. In FreePBX these are 'From Domain' and 'From User' in the trunk's pjsip settings, and the Registration tab holds Server URI and Client URI.

A symptom to cause mapping covers most cases:

Symptom in traceLikely causeLine to change
401, retry has realm="asterisk" but 401 said another realmFixed realm in [auth] differs from the challengeRemove realm= or set it to the provider's value
401 then 401 with md5_credHash built with another realm or userRegenerate md5_cred or use auth_type=userpass
No matching endpoint found on phone REGISTERFrom user is not the endpoint nameRename the endpoint or set identify_by=username,auth_username
No matching endpoint found on trunk INVITENo source IP matchAdd type=identify, match=<provider IP>
Registration 401, correct passwordProvider wants a different auth userSet username= in [auth], keep the number in client_uri
403 on REGISTERACL, banned IP, or provider rejectionCheck acl=, then provider's allowed IP and account state
403 on INVITE from your trunkFrom user or domain not acceptedSet from_user and from_domain on the endpoint

How do I verify the fix and keep it secure?

Change one identifier at a time and re-capture the exchange. If the provider mentions SHA-256, note that the algorithm= value in the challenge comes from the server, and a loop with matching realm and username usually means the password or the user is wrong, not the algorithm.

For security, do not set allow_unauthenticated or an anonymous endpoint unless you need it. Keep endpoint_identifier_order tight, keep default responses generic so unknown users look identical to bad passwords, and let fail2ban act on the Failed to authenticate and No matching endpoint found lines.

  1. Run asterisk -rvvv, then pjsip set logger on (or sngrep) and trigger a REGISTER.
  2. Read realm= in the 401, then username= and realm= in the second REGISTER.
  3. Run pjsip show auths and pjsip show endpoint <name> to confirm the auth object, identify_by and aors.
  4. Edit the setting that differs, then run pjsip reload (or Apply Config in FreePBX).
  5. Run pjsip show registrations and confirm the status is Registered, not Rejected or Unregistered.
  6. Place an inbound and an outbound test call, and confirm rtp set debug on shows audio both ways.

Common questions

Does the realm have to match between the phone and Asterisk?

No. The phone simply uses the realm Asterisk sends in the 401. It only matters when a credential is bound to a realm, such as md5_cred, or when a trunk [auth] section fixes a realm that differs from the provider's challenge.

Should I use auth_type=userpass or md5?

Use userpass unless you must keep plain passwords out of the config. md5 stores only the hash of username:realm:password, so any change of username or realm requires regenerating it.

Why do I see a 401 on the first REGISTER even when everything works?

The first request intentionally has no credentials, so Asterisk replies with a 401 and a nonce. The client repeats with an Authorization header and should get 200 OK. Only a second 401 or a 403 indicates a problem.

Why does 'No matching endpoint found' appear for my trunk?

Asterisk could not match the From user, auth username or source IP to an endpoint. Add a type=identify section with match set to the provider's IP address, or name the endpoint after the From user the provider sends.

Can trailing spaces in a password cause a 401?

Yes. A trailing space or a hidden character copied from a portal becomes part of the hashed secret, so the digest never matches. Retype the password, and quote special characters as needed in the FreePBX field.

Related topics:asteriskpjsipsip authenticationfreepbx