一、RTSP 认证方式
1、RTSP URL认证
直接携带明文的用户名和密码(rtsp://用户名:密码@IP地址/Stream/Channels/101),安全性低。
2、基本认证(basic authentication)
基本认证是HTTP 1.0 提出的认证方案,其消息传输不经过加密转换因此存在严重的安全隐患。
3、摘要认证( digest authentication)
摘要认证是 HTTP 1.1 提出的基本认证的替代方案,其消息经过 MD5 哈希转换因此具有更高的安全性。
二、基本认证流程
1) 客户端发送 DESCRIBE 请求
DESCRIBE rtsp://192.168.1.55:554/11 RTSP/1.0\\r\\n CSeq: 1\\r\\n
Accept: application/sdp\\r\\n
User-agent: Realplayer\\r\\n\\r\\n
2)服务端发出 WWW-Authenticate 认证响应
服务端返回401错误码,发出 WWW-Authenticate 认证响应告诉客户端需要进行认证。
RTSP/1.0 401 Unauthorized\r\n
CSeq: 1\r\n WWW-Authenticate: Basic realm="RTSPD"\r\n\r\n
3)客户端再次发出 DESCRIBE 请求
此时客户端程序弹出密码认证窗口 ,提示输入用户名,密码等认证信息,并根据服务端返回的响应消息中进处理,如果发现是 Basic认证则携带认证信息发送如下报文:
DESCRIBE rtsp://192.168.1.55:554/live/1/video.sdp?token=A00453FR805a54C8 RTSP/1.0\r\n CSeq: 2\r\n
Accept: application/sdp\r\n
User-Agent: RealMedia Player HelixDNAClient/12.0.1.647 (win32)\r\n Authorization: Basic YWRtaW46YWRtaW4=\r\n\r\
其中 “YWRtaW46YWRtaW4=” 是通过 username:password 进行 base64 编码所得。因为其具有唯一性等价于账号和密码,明文发送泄漏后存在安全风险。
三、摘要认证流程
(1)协商支持的方法
客户端第一次建立连接,发送options request报文,询问服务端有哪些方法可用。
OPTIONS rtsp://10.67.193.76:554/Streaming/Channels/1?transportmode=unicast&profile=Profile_1 RTSP/1.0
CSeq: 2
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
此时服务端(即设备)在回应信息中提供所有的可用方法
RTSP/1.0 200 OK
CSeq: 2
Public: OPTIONS, DESCRIBE, PLAY, PAUSE, SETUP, TEARDOWN, SET_PARAMETER, GET_PARAMETER
Date: Wed, Jun 03 2020 10:54:30 GMT
(2)客户端发送DESCRIBE
客户端发送DESCRIBE request报文,得到服务端的媒体初始化描述信息
DESCRIBE rtsp://10.67.193.76:554/Streaming/Channels/1?transportmode=unicast&profile=Profile_1 RTSP/1.0
CSeq: 3
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Accept: application/sdp
(3)服务端发出 WWW-Authenticate 认证响应
此时设备返回相应401报文,提示用户需要认证,报文中携带了realm和nonce信息。
RTSP/1.0 401 Unauthorized
CSeq: 3
WWW-Authenticate: Digest realm="IP Camera(23435)", nonce="8fd7c44874480bd643d970149224da11", stale="FALSE"
Date: Wed, Jun 03 2020 10:54:30 GMT
(4)客户端发送携带认证信息的DESCRIBE
客户端通过服务端返回的 realm 和 nonce 计算出 response,在报文Authorization字段中携带response信息。
DESCRIBE rtsp://10.67.193.76:554/Streaming/Channels/1?transportmode=unicast&profile=Profile_1 RTSP/1.0
CSeq: 4 Authorization: Digest username="", realm="IP Camera(23435)", nonce="8fd7c44874480bd643d970149224da11", uri="rtsp://10.67.193.76:554/Streaming/Channels/1?transportmode=unicast&profile=Profile_1", response="ca29ba3297f50b32425e46e23723ef7b"
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Accept: application/sdp
response 计算方法如下:
response= md5(md5(username:realm:password):nonce:md5(public_method:url) );
客户端在每次发起不同的请求方法时都需要计算 response 字段,同样在服务器端校验时也默认采取同样的计算方法。由于nonce不同每次计算结果不同,保证了安全性。
(5)服务端认证校验
服务端进行身份校验,认证通过则回应媒体初始化描述信息,到这里就认证结束了,后面客户端可通过PLAY、PAUSE等命令控制媒体数据传输。
RTSP/1.0 200 OK CSeq: 7 Content-Type: application/sdp Content-Base: rtsp://10.67.193.76:554/Streaming/Channels/1/
Content-Length: 895
v=0o=StreamingServer 3331435948 1116907222000 IN IP4192.168.123.158s=\\11c=IN IP4 0.0.0.0b=AS:1032t=0 0a=control:\*
m=video 0 RTP/AVP 96b=AS:1024a=control:trackID=0a=rtpmap:96 H264/90000a=fmtp:96 packetization-mode=1;sprop-parameter-sets=Z0LgHtoCgPRA,aM4wpIA=
a=framesize:96 640-480