How to configure TLS for self-hosted SIP server?
# ask-ai
b
How to configure TLS for self-hosted SIP server?
t
Thanks for reaching out, Delivery. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Let's walk through how to configure TLS for your self-hosted SIP server together. Here’s a simple step-by-step guide to help you get started: 1. Define TLS Port and Certificates: First, update your
config.yaml
file to specify the TLS port and the paths to your certificates. For example:
Copy code
tls:
  port: 5061
  port_listen: 5061
  certs:
    - cert_file: /path/to/cert.pem
      key_file: /path/to/key.pem
1. Start the SIP Server: Make sure your server is started with a valid
tls.Config
object and that it listens on the TLS port you set. 2. Network Setup: Don’t forget to open port 5061 (or whichever TLS port you chose) on your firewall to allow SIP/TLS signaling through. 3. Client Configuration: When configuring your client, use the SIP URI with
;transport=tls
appended, like this:
sip:yourserver.sip.livekit.cloud;transport=tls
. If you need to generate TLS certificates, OpenSSL is a great tool for that. Here’s a handy script to create a self-signed certificate:
Copy code
#!/usr/bin/env bash
mkdir -p certs && cd certs

# Create self-signed root CA certificate
openssl genrsa -out rootca-key.pem 2048

openssl req -new -x509 -nodes -days 3650 \
    -subj "/C=US/ST=California/CN=localhost" \
    -key rootca-key.pem \
    -out rootca-cert.pem

# Generate server private key and certificate signing request (CSR)
openssl req -newkey rsa:2048 -nodes \
    -subj "/C=US/ST=California/CN=localhost" \
    -keyout server.key \
    -out server.csr

# Sign the server certificate with the root CA
openssl x509 -req -days 3650 -set_serial 01 \
    -in server.csr \
    -out server.crt \
    -CA rootca-cert.pem \
    -CAkey rootca-key.pem \
    -extensions SAN \
    -extfile <(printf "\n[SAN]\nsubjectAltName=IP:127.1.1.100\nextendedKeyUsage=serverAuth")
Just a quick note: replace
127.1.1.100
with your server’s actual public IP address. And when you’re moving to production, it’s best to use certificates from a trusted Certificate Authority to keep everything secure. Sources: Deploying LiveKit | LiveKit Docs | pkg/sip/server.go | Secure trunking | LiveKit Docs | testdata/generate_certs_rsa.sh
b
Why do I need a certificate?
t
I'm glad you're looking into securing your self-hosted SIP server! A TLS certificate plays a really important role here by enabling secure communication. It helps make sure that your SIP signaling traffic is encrypted, which protects it from eavesdropping, tampering, and other security risks when it's traveling over networks that might not be fully trusted. Without TLS, the SIP signaling would be sent in plaintext, which could leave it open to attacks like session hijacking or interception of sensitive details such as caller IDs and routing information. Using TLS encryption ensures that your communication stays confidential and intact. Also, it’s worth mentioning that many SIP trunking providers actually require TLS for secure setups. They often expect you to use port 5061 along with valid certificates. Without a certificate, your SIP server won’t be able to create those encrypted connections, and some providers might even reject signaling that isn’t secure. So, to sum it up, having a TLS certificate is really key—not just for following security best practices, but also to meet provider requirements and keep your SIP server’s signaling traffic safe and sound. Sources: pkg/sip/server.go | Ports and firewall | LiveKit Docs | Secure trunking | LiveKit Docs
b
sip_error: TwirpError(code=unavailable, message=twirp error unknown: no response from servers, status=503)
t
It looks like you're encountering the error
sip_error: TwirpError(code=unavailable, message=twirp error unknown: no response from servers, status=503)
, which usually means the SIP server is temporarily unavailable or can’t process the request right now. This can happen sometimes due to misconfigurations or network hiccups. Let’s walk through some steps that might help resolve this: 1. Check Outbound Trunk Configuration: Double-check that the
address
field in your outbound trunk exactly matches the SIP endpoint your provider gave you (for example,
sip.telnyx.com
) and doesn’t include any unsupported subdomains. 2. Verify SIP Trunk and Dispatch Rules: Make sure the trunk ID you’re using in any
CreateSIPParticipant
request actually exists and is set up correctly. 3. Assess Network and Intermediary Services: Take a look at any load balancers, proxies, or gateways between LiveKit and your SIP provider to spot any potential issues. 4. Review Logs: Checking the LiveKit service logs can be really helpful to see if there are any errors or overload situations causing the temporary unavailability. If you’ve gone through these steps and things are still not working, it’s a good idea to reach out to your SIP provider. They can confirm if their endpoint is up and running and help ensure they’re not sending back the 503 error. Sources: livekit-protocol/livekit/protocol/sip.py | livekit-api/src/services/twirp_client.rs | livekit/livekit_sip.twirp.go | protocol/unity_proto/UnityAPI.twirp.go | SIP troubleshooting guide | LiveKit Docs
b
Example CreateSIPParticipant for TLS enabled trunk outbound call
t
To help you make a TLS-enabled outbound SIP call, here’s a simple step-by-step guide you can follow: 1. Create a TLS-Enabled Outbound Trunk: Start by configuring the trunk with
transport: SIP_TRANSPORT_TLS
. Here’s an example to get you started:
Copy code
{
  "trunk": {
    "name": "tls-outbound-trunk",
    "address": "sip.provider.com",
    "numbers": [
      "+1234567890"
    ],
    "transport": "SIP_TRANSPORT_TLS",
    "auth_username": "your_username",
    "auth_password": "your_password",
    "media_encryption": "SIP_MEDIA_ENCRYPT_REQUIRE"
  }
}
1. Use the Trunk in a CreateSIPParticipant Request: When you make your request, just reference the trunk ID like this:
Copy code
{
  "sip_trunk_id": "ST_XXXX",
  "sip_call_to": "+14085551234",
  "room_name": "my-secure-call-room",
  "participant_identity": "caller-secure-01",
  "participant_name": "Secure Caller",
  "participant_metadata": "Outbound TLS call",
  "dtmf": "1234#",
  "play_dialtone": true,
  "ringing_timeout": 30,
  "max_call_duration": 3600,
  "media_encryption": "SIP_MEDIA_ENCRYPT_REQUIRE"
}
Just a quick reminder: please make sure your SIP provider supports TLS and is set up to accept secure signaling on port 5061. If it helps, you can also define the trunk directly inline in your request using the
trunk
field. If you have any questions or need further assistance, I’m here to help! Sources: rpc/sip_test.go | Secure trunking | LiveKit Docs | Making outbound calls | LiveKit Docs
b
SIP_MEDIA_ENCRYPT_REQUIRE make it optional
t
To help you make media encryption optional, you can simply set the
media_encryption
parameter to
SIP_MEDIA_ENCRYPT_ALLOW
instead of
SIP_MEDIA_ENCRYPT_REQUIRE
. This way, the call can continue even if the other party doesn’t support media encryption (SRTP). Here’s a friendly example of the updated
CreateSIPParticipant
request for you to try:
Copy code
{
  "sip_trunk_id": "ST_XXXX",
  "sip_call_to": "+14085551234",
  "room_name": "my-secure-call-room",
  "participant_identity": "caller-secure-01",
  "participant_name": "Secure Caller",
  "participant_metadata": "Outbound TLS call",
  "dtmf": "1234#",
  "play_dialtone": true,
  "ringing_timeout": 30,
  "max_call_duration": 3600,
  "media_encryption": "SIP_MEDIA_ENCRYPT_ALLOW"
}
This setup makes sure media encryption is used when available, but it won’t block the call if it’s not supported. If you have any questions or need more help, I’m here for you!
b
disable rtp exnryption
t
If you'd like to disable RTP encryption completely, you can do so by setting the
media_encryption
parameter to
SIP_MEDIA_ENCRYPT_NONE
. This way, media encryption (SRTP) won’t be applied to your call. Here’s a friendly example of how your
CreateSIPParticipant
request would look with this setting:
Copy code
{
  "sip_trunk_id": "ST_XXXX",
  "sip_call_to": "+14085551234",
  "room_name": "my-secure-call-room",
  "participant_identity": "caller-secure-01",
  "participant_name": "Secure Caller",
  "participant_metadata": "Outbound call without encryption",
  "dtmf": "1234#",
  "play_dialtone": true,
  "ringing_timeout": 30,
  "max_call_duration": 3600,
  "media_encryption": "SIP_MEDIA_ENCRYPT_NONE"
}
With this setup, SRTP is turned off, allowing your call to proceed without media encryption. If you have any questions or need further help, feel free to ask—we’re here to support you!