RTSP认证方式
用户通过rtsp url地址获取设备的视频流时,大多数是需要认证的。RTSP的认证方式有2种:1.rtsp url直接携带明文的用户名和密码(rtsp://用户名:密码@IP地址/Stream/Channels/101) 2.HTTP 认证,http 认证又分为两种:基本认证和摘要认证。
rtsp url直接携带明文的用户名和密码的方式安全性差,现在一般不使用。下面主要讲解HTTP认证方式。
HTTP 基本认证
基本认证是http 1.0提出的认证方案,其消息传输不经过加密转换因此存在严重的安全隐患。
1.1 客户端发送DESCRIBE请求到前端设备,URL中携带请求的URL地址
DESCRIBE rtsp://192.168.1.55:554/live/1/video.sdp?token=A00453FR805a54C8
RTSP/1.0\r\n
CSeq: 1\r\n
Accept: application/sdp\r\n
User-agent: Realplayer\r\n\r\n
2、设备认证通过,发送 WWW-Authenticate认证请求
RTSP/1.0 401 Unauthorized\r\n
CSeq: 1\r\n
WWW-Authenticate: Basic realm="RTSPD"\r\n\r\n
此时客户端程序会弹出用户名名密码认证窗口,输入认证信息后,
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\n
其中“YWRtaW46YWRtaW4=”为前端设备访问username:password明文的base64编码
HTTP 摘要认证
摘要认证是http 1.1提出的基本认证的替代方案,其消息经过MD5哈希转换因此具有更高的安全性。
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 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
此时设备返回相应401报文,提示用户需要认证
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
3.通过上一步Rtsp server 返回的 realm 和 nonce 计算出 Authorization,然后重新获取请求,在请求中加上 Authorization
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
服务端校验是否通过认证,通过的话,回应媒体初始化描述信息,主要是sdp
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
4.客户端发送PLAY request请求
PLAY rtsp://10.67.193.76:554/Streaming/Channels/1/?transportmode=unicast&profile=Profile_1 RTSP/1.0
CSeq: 10
Authorization: Digest username="admin", realm="IP Camera(23435)", nonce="18de8ae133a1703bae94cb37ce8bf08d", uri="rtsp://10.67.193.76:554/Streaming/Channels/1/", response="fe32b85aeae647879fb384ad60f6a711"
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Session: 427807408
Range: npt=0.000-
5.服务端相应客户端的播放请求
RTSP/1.0 200 OK
CSeq: 10
Session: 427807408
RTP-Info: url=rtsp://10.67.193.76:554/Streaming/Channels/1/trackID=1?transportmode=unicast&profile=Profile_1;seq=40721;rtptime=4036421714,url=rtsp://10.67.193.76:554/Streaming/Channels/1/trackID=2?transportmode=unicast&profile=Profile_1;seq=35759;rtptime=740568584
Date: Wed, Jun 03 2020 10:54:37 GMT
参考
1.https://www.jianshu.com/p/df107df10196
2.https://blog.csdn.net/u011244446/article/details/46764065