SignedPolicy

Overview

SignedPolicy is a module that limits the user's privileges and time. For example, operators can distribute RTMP URLs that can be accessed for 60 seconds to authorized users, and limit RTMP transmission to 1 hour. The provided URL will be destroyed after 60 seconds, and transmission will automatically stop after 1 hour. Users who are provided with a SignedPolicy URL cannot access resources other than the provided URL. This is because the SignedPolicy URL is authenticated.

SignedPolicy URL consists of the query string of the streaming URL with Policy and Signature as shown below. If SignedPolicy is enabled in the configuration of OvenMediaEngine, access to URLs with no signature or invalid signature is not allowed. Signature uses HMAC-SHA1 to authenticate all URLs except signature.

scheme://domain.com:port/app/stream?policy=<>&signature=<>

Policy

Policy is in json format and provides the following properties.

{
    "url_activate":1399711581,                                    
    "url_expire":1399721581,                                    
    "stream_expire":1399821581,                                    
    "allow_ip":"192.168.100.5/32"
}
Key
Value
Description

url_expire

(Required)

<Number> Milliseconds since unix epoch

The time the URL expires Reject on request after the expiration

url_activate

(Optional)

<Number> Milliseconds since unix epoch

The time the URL activates Reject on request before activation

stream_expire

(Optional)

<Number> Milliseconds since unix epoch

The time the Stream expires Transmission and playback stop when the time expires

allow_ip

(Optional)

<String> IPv4 CIDR

Allowed IP address range, 192.168.0.0/24

url_expire means the time the URL is valid, so if you connect before the URL expires, you can continue to use it, and sessions that have already been connected will not be deleted even if the time expires. However, stream_expire forcibly terminates the session when the time expires even if it is already playing.

Signature

Signature is generated by HMAC-SHA1 encoding all URLs except signature query string. The generated Signature is encoded using Base64URL and included as a query string of the existing URL.

Base64URL.Encode(
    HMAC.Encrypt(
        SHA1, 
        secret_key, 
        "scheme://domain.com:port/app/stream[/file]?policy='encoded policy'>"
    )
)

The URL entered into HMAC to generate the Signature must include :port.

When creating a signature, you cannot omit the default port such as http port 80, https port 443, or rtmp port 1935. This is because when OvenMediaEngine creates a signature for checking the signature, it is created by putting the port value.

When using SignedPolicy with SRT providers, only use the streamid portion of the URL, e.g. srt://myserver:9999?streamid=srt://myserver:9999/app/stream?policy=abc123

Configuration

To enable SignedPolicy, you need to add the following <SignedPolicy> setting in Server.xml under <VirtualHost>.

<VirtualHost>
    <SignedPolicy>
        <PolicyQueryKeyName>policy</PolicyQueryKeyName>
        <SignatureQueryKeyName>signature</SignatureQueryKeyName>
        <SecretKey>aKq#1kj</SecretKey>

        <Enables>
            <Providers>rtmp</Providers>
            <Publishers>webrtc,llhls,thumbnail</Publishers>
        </Enables>
    </SignedPolicy>
</VirtualHost>
Key
Description

PolicyQueryKeyName

The query string key name in the URL pointing to the policy value

SignatureQueryKeyName

The query string key name in the URL pointing to the signature value

SecretKey

The secret key used when encoding with HMAC-SHA1

Enables

List of providers and publishers to enable SignedPolicy. Currently, SignedPolicy supports rtmp among providers, and among publishers, WebRTC, LLHLS, Thumbnail are supported.

Make SignedPolicy URL with a script

We provide a script that can easily generate SignedPolicy URL. The script can be found in the path below.

/misc/signed_policy_url_generator.sh

Here's how to use this script:

./signed_policy_generator.sh [HMAC_KEY] [BASE_URL] [SIGNATURE_QUERY_KEY_NAME] [POLICY_QUERY_KEY_NAME] [POLICY]

For example, you can use it like this:

Make SignedPolicy URL manually

We hope to provide SignedPolicy URL Generator Library in various languages. If you have created the SignedPolicy URL Generator Library in another language, please send a Pull Request to our GITHUB. Thank you for your open source contributions.

Encoding policy

In order to include the policy in the URL, it must be encoded with Base64URL.

Plain {Policy}
{"url_expire":1399721581}
Base64URL Encoded {Policy}
eyJ1cmxfZXhwaXJlIjoxMzk5NzIxNTgxfQ

Policy encoded with Base64URL is added as a query string to the existing streaming URL. (The query string key is set in Server.xml.)

ws://192.168.0.100:3333/app/stream?policy=eyJ1cmxfZXhwaXJlIjoxMzk5NzIxNTgxfQ

Signature

Signature hashes the entire URL including the policy in HMAC (SHA-1) method, encodes it as Base64URL, and includes it in the query string.

URL input to signature generation
ws://192.168.0.100:3333/app/stream?policy=eyJ1cmxfZXhwaXJlIjoxMzk5NzIxNTgxfQ

Create a hash using the secret key (1kU^b6 in the example) and the URL above using HMAC-SHA1.

Base64URL encoded { HMAC-SHA1 <KEY : 1kU^b6> (URL) }
dvVdBpoxAeCPl94Kt5RoiqLI0YE

If you include it as a signature query string (query string key is set in Server.xml), the following SignedPolicy URL is finally generated.

URL with signature
ws://192.168.0.100/app/stream?policy=eyJ1cmxfZXhwaXJlIjoxMzk5NzIxNTgxfQ&signature=dvVdBpoxAeCPl94Kt5RoiqLI0YE

Usage examples

Applying SignedPolicy in OBS

Generate SignedPolicy URL with the script.

Separate the URL based on "app" as shown in the example below and enter all the parts under the stream in the Stream Key.