VoIPCallSdpPatcher.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CocoaLumberjackSwift
  22. public class VoIPCallSdpPatcher: NSObject {
  23. static let SDP_MEDIA_AUDIO_ANY_RE = "m=audio ([^ ]+) ([^ ]+) (.+)"
  24. static let SDP_RTPMAP_OPUS_RE = "a=rtpmap:([^ ]+) opus.*"
  25. static let SDP_RTPMAP_ANY_RE = "a=rtpmap:([^ ]+) .*"
  26. static let SDP_FMTP_ANY_RE = "a=fmtp:([^ ]+) ([^ ]+)"
  27. static let SDP_EXTMAP_ANY_RE = "a=extmap:[^ ]+ (.*)"
  28. convenience init(_ config: RtpHeaderExtensionConfig) {
  29. self.init()
  30. rtpHeaderExtensionConfig = config
  31. }
  32. /// Whether this SDP is created locally and it is the offer, a local answer or a remote SDP.
  33. public enum SdpType {
  34. case LOCAL_OFFER
  35. case LOCAL_ANSWER_OR_REMOTE_SDP
  36. }
  37. /// RTP header extension configuration.
  38. public enum RtpHeaderExtensionConfig {
  39. case DISABLE
  40. case ENABLE_WITH_LEGACY_ONE_BYTE_HEADER_ONLY
  41. case ENABLE_WITH_ONE_AND_TWO_BYTE_HEADER
  42. }
  43. enum SdpErrorType: Error, Equatable {
  44. case invalidSdp
  45. case illegalArgument
  46. case matchesError
  47. case unknownSection
  48. }
  49. internal enum SdpSection: String {
  50. case GLOBAL
  51. case MEDIA_AUDIO
  52. case MEDIA_VIDEO
  53. case MEDIA_DATA_CHANNEL
  54. case MEDIA_UNKNOWN
  55. func isRtpSection() -> Bool {
  56. switch self {
  57. case .GLOBAL, .MEDIA_DATA_CHANNEL, .MEDIA_UNKNOWN:
  58. return false
  59. case .MEDIA_AUDIO, .MEDIA_VIDEO:
  60. return true
  61. }
  62. }
  63. }
  64. internal enum LineAction {
  65. case ACCEPT
  66. case REJECT
  67. case REWRITE
  68. }
  69. class SdpError: Error {
  70. var type: SdpErrorType
  71. var description: String
  72. init(type: SdpErrorType, description: String) {
  73. self.type = type
  74. self.description = description
  75. }
  76. var errorDescription: String? {
  77. get {
  78. return self.description
  79. }
  80. }
  81. }
  82. private var rtpHeaderExtensionConfig: RtpHeaderExtensionConfig = .DISABLE
  83. internal struct SdpPatcherContext {
  84. internal var type: SdpType
  85. internal var config: VoIPCallSdpPatcher
  86. internal var payloadTypeOpus: String
  87. internal var rtpExtensionIdRemapper: RtpExtensionIdRemapper
  88. internal var section: SdpSection
  89. init(type: SdpType, config: VoIPCallSdpPatcher, payloadTypeOpus: String) {
  90. self.type = type
  91. self.config = config
  92. self.payloadTypeOpus = payloadTypeOpus
  93. self.rtpExtensionIdRemapper = RtpExtensionIdRemapper(config: config)
  94. self.section = SdpSection.GLOBAL
  95. }
  96. }
  97. internal struct Line {
  98. private(set) var line: String
  99. private var action: LineAction?
  100. init(line: String) {
  101. self.line = line
  102. }
  103. mutating func accept() throws -> LineAction {
  104. if action != nil {
  105. throw SdpError(type: .illegalArgument, description: "LineAction.action already set")
  106. }
  107. self.action = .ACCEPT
  108. return self.action!
  109. }
  110. mutating func reject() throws -> LineAction {
  111. if action != nil {
  112. throw SdpError(type: .illegalArgument, description: "LineAction.action already set")
  113. }
  114. self.action = .REJECT
  115. return self.action!
  116. }
  117. mutating func rewrite(line: String) throws -> LineAction {
  118. if action != nil {
  119. throw SdpError(type: .illegalArgument, description: "LineAction.action already set")
  120. }
  121. self.action = .REWRITE
  122. self.line = line
  123. return self.action!
  124. }
  125. }
  126. internal struct RtpExtensionIdRemapper {
  127. private var currentId: Int?
  128. private var maxId: Int?
  129. private var extensionIdMap = [String: Int]()
  130. init(config: VoIPCallSdpPatcher) {
  131. currentId = 0
  132. switch config.rtpHeaderExtensionConfig {
  133. case .ENABLE_WITH_LEGACY_ONE_BYTE_HEADER_ONLY:
  134. maxId = 14
  135. case .ENABLE_WITH_ONE_AND_TWO_BYTE_HEADER:
  136. maxId = 255
  137. default:
  138. maxId = 0
  139. }
  140. }
  141. mutating func assignId(uriAndAttributes: String) throws -> Int {
  142. // It is extremely important that we give extensions with the same URI the same ID
  143. // across different media sections, otherwise the bundling mechanism will fail and we
  144. // get all sorts of weird behaviour from the WebRTC stack.
  145. var id = extensionIdMap[uriAndAttributes]
  146. if id == nil {
  147. // Check if exhausted
  148. currentId! += 1
  149. if currentId! > maxId! {
  150. throw SdpError(type: .invalidSdp, description: "RTP extension IDs exhausted")
  151. }
  152. id = currentId
  153. if currentId == 15 {
  154. currentId! += 1
  155. id! += 1
  156. }
  157. extensionIdMap[uriAndAttributes] = id
  158. }
  159. return id!
  160. }
  161. }
  162. /// Patch an SDP offer / answer with a few things that we want to enforce in Threema:
  163. /// For all media lines:
  164. /// - Remove audio level and frame marking header extensions
  165. /// - Remap extmap IDs (when offering)
  166. ///
  167. /// For audio in specific:
  168. /// - Only support Opus, remove all other codecs
  169. /// - Force CBR
  170. ///
  171. /// The use of CBR (constant bit rate) will also suppress VAD (voice activity detection). For
  172. /// more security considerations regarding codec configuration, see RFC 6562:
  173. /// https://tools.ietf.org/html/rfc6562
  174. ///
  175. /// - Parameters:
  176. /// - type: Type
  177. /// - sdp: String
  178. /// - Throws: SdpError
  179. /// - Returns: Updated sdp
  180. func patch(type: SdpType, sdp: String) throws -> String {
  181. var payloadTypeOpus: String?
  182. do {
  183. let sdpRtpmapOpusRegex = try NSRegularExpression.init(pattern: VoIPCallSdpPatcher.SDP_RTPMAP_OPUS_RE, options: [])
  184. let sdpRange = NSRange(sdp.startIndex..<sdp.endIndex, in: sdp)
  185. if let match = sdpRtpmapOpusRegex.firstMatch(in: sdp, options: [], range: sdpRange) {
  186. if let sub = sdp.substring(with: match.range(at: 1)) {
  187. payloadTypeOpus = String(sub)
  188. }
  189. } else {
  190. throw SdpError(type: .invalidSdp, description: "a=rtpmap: [...] opus not found")
  191. }
  192. var lines = String()
  193. var context = SdpPatcherContext(type: type, config: self, payloadTypeOpus: payloadTypeOpus!)
  194. let linesArray = sdp.linesArray
  195. for (var index, line) in linesArray.enumerated() {
  196. do {
  197. try handleLine(context: &context, lines: &lines, lineString: line, sdpLineArray: linesArray, index: &index)
  198. } catch let error {
  199. let sdpError = error as! SdpError
  200. switch sdpError.type {
  201. case .unknownSection:
  202. break
  203. default:
  204. throw error
  205. }
  206. }
  207. }
  208. return lines
  209. }
  210. catch {
  211. throw SdpError(type: .invalidSdp, description: "a=rtpmap: [...] opus not found")
  212. }
  213. }
  214. /// Handle an SDP line.
  215. /// - Parameters:
  216. /// - context: SdpPatcherContext
  217. /// - lines: String
  218. /// - lineString: String
  219. /// - sdpLineArray: [String]
  220. /// - index: Int
  221. /// - Throws: SdpError
  222. private func handleLine(context: inout SdpPatcherContext, lines: inout String, lineString: String, sdpLineArray: [String], index: inout Int) throws {
  223. let current: SdpSection = context.section
  224. var line: Line = Line(line: lineString)
  225. var action: LineAction
  226. if lineString.starts(with: "m=") {
  227. action = try handleSectionLine(context: &context, line: &line)
  228. } else {
  229. switch context.section {
  230. case .GLOBAL:
  231. action = try handleGlobalLine(context, &line)
  232. case .MEDIA_AUDIO:
  233. action = try handleAudioLine(&context, &line)
  234. case .MEDIA_VIDEO:
  235. action = try handleVideoLine(&context, &line)
  236. case .MEDIA_DATA_CHANNEL:
  237. action = try handleDataChannelLine(context, &line)
  238. default:
  239. // Note: This also swallows `MEDIA_UNKNOWN`. Since we reject these lines completely,
  240. // a line within that section should never be parsed.
  241. throw SdpError(type: .unknownSection, description: String(format: "Unknown section %@", current.rawValue))
  242. }
  243. }
  244. // Execute line action
  245. switch action {
  246. case .ACCEPT, .REWRITE:
  247. lines.append(line.line)
  248. lines.append("\r\n")
  249. case .REJECT:
  250. DDLogError(String(format: "Rejected line: %@", line.line))
  251. }
  252. // If we have switched to another section and the line has been rejected,
  253. // we need to reject the remainder of the section.
  254. if current != context.section && action == .REJECT {
  255. //noinspection StatementWithEmptyBody
  256. var debug = String()
  257. for (i, newLine) in sdpLineArray.enumerated() {
  258. if i > index {
  259. if !newLine.starts(with: "m=") {
  260. debug.append(newLine)
  261. index = i
  262. } else {
  263. break
  264. }
  265. }
  266. }
  267. DDLogError(String(format: "Rejected section: %@", debug))
  268. }
  269. }
  270. /// Handle a section line.
  271. /// - Parameters:
  272. /// - context: SdpPatcherContext
  273. /// - line: Line
  274. /// - Throws: SdpError
  275. /// - Returns: LineAction
  276. private func handleSectionLine(context: inout SdpPatcherContext, line: inout Line) throws -> LineAction {
  277. let lineString = line.line
  278. // Audio section
  279. do {
  280. let sectionRegex = try NSRegularExpression(pattern: VoIPCallSdpPatcher.SDP_MEDIA_AUDIO_ANY_RE, options: [])
  281. let lineRange = NSRange(lineString.startIndex..<lineString.endIndex, in: lineString)
  282. if let match = sectionRegex.firstMatch(in: lineString, options: [], range: lineRange) {
  283. context.section = .MEDIA_AUDIO
  284. // Parse media description line
  285. if let port = lineString.substring(with: match.range(at: 1)),
  286. let proto = lineString.substring(with: match.range(at: 2)),
  287. let payloadTypes = lineString.substring(with: match.range(at: 3)) {
  288. // Make sure that the Opus payload type is contained here
  289. if !String(payloadTypes).split(separator: " ").map(String.init).contains(context.payloadTypeOpus) {
  290. throw SdpError(type: .invalidSdp, description: String.localizedStringWithFormat("Opus payload type (%@) not found in audio media description", context.payloadTypeOpus))
  291. }
  292. let newString = String(format: "m=audio %@ %@ %@", String(port), String(proto), context.payloadTypeOpus)
  293. return try line.rewrite(line: newString)
  294. }
  295. }
  296. // Video section
  297. if lineString.starts(with: "m=video") {
  298. // Accept
  299. context.section = SdpSection.MEDIA_VIDEO
  300. return try line.accept()
  301. }
  302. // Data channel section
  303. if lineString.starts(with: "m=application") && lineString.contains("DTLS/SCTP") {
  304. // Accept
  305. context.section = SdpSection.MEDIA_DATA_CHANNEL
  306. return try line.accept()
  307. }
  308. // unknown section (reject)
  309. context.section = SdpSection.MEDIA_UNKNOWN
  310. return try line.reject()
  311. }
  312. catch {
  313. throw SdpError(type: .matchesError, description: "SDP_MEDIA_AUDIO_ANY_RE error")
  314. }
  315. }
  316. /// Handle global (non-media) section line.
  317. /// - Returns: LineAction
  318. /// - Parameters:
  319. /// - context: SdpPatcherContext
  320. /// - line: Line
  321. /// - Throws: SdpError
  322. private func handleGlobalLine(_ context: SdpPatcherContext, _ line: inout Line) throws -> LineAction {
  323. return try handleRtpAttributes(context, &line)
  324. }
  325. // Handle RTP attributes shared across global (non-media) and media sections.
  326. /// - Returns: LineAction
  327. /// - Parameters:
  328. /// - context: SdpPatcherContext
  329. /// - line: Line
  330. /// - Throws: SdpError
  331. private func handleRtpAttributes(_ context: SdpPatcherContext, _ line: inout Line) throws -> LineAction {
  332. let lineString = line.line
  333. // Reject one-/two-byte RTP header mixed mode, if requested
  334. if context.config.rtpHeaderExtensionConfig != .ENABLE_WITH_ONE_AND_TWO_BYTE_HEADER && lineString.starts(with: "a=extmap-allow-mixed") {
  335. return try line.reject()
  336. }
  337. // Accept the rest
  338. return try line.accept()
  339. }
  340. /// Handle audio section line.
  341. /// - Parameters:
  342. /// - context: SdpPatcherContext
  343. /// - line: Line
  344. /// - Throws: SdpError
  345. /// - Returns: LineAction
  346. private func handleAudioLine(_ context: inout SdpPatcherContext, _ line: inout Line) throws -> LineAction {
  347. let lineString = line.line
  348. let lineRange = NSRange(lineString.startIndex..<lineString.endIndex, in: lineString)
  349. // RTP mappings
  350. let rtpMappingRegex = try NSRegularExpression(pattern: VoIPCallSdpPatcher.SDP_RTPMAP_ANY_RE, options: [])
  351. if let match = rtpMappingRegex.firstMatch(in: lineString, options: [], range: lineRange) {
  352. if let payloadType = lineString.substring(with: match.range(at: 1)) {
  353. if payloadType == context.payloadTypeOpus {
  354. return try line.accept()
  355. } else {
  356. return try line.reject()
  357. }
  358. }
  359. }
  360. // RTP format parameters
  361. let rtpFormatParametersRegex = try NSRegularExpression(pattern: VoIPCallSdpPatcher.SDP_FMTP_ANY_RE, options: [])
  362. if let match = rtpFormatParametersRegex.firstMatch(in: lineString, options: [], range: lineRange) {
  363. guard let payloadType = lineString.substring(with: match.range(at: 1)) else {
  364. return try line.reject()
  365. }
  366. let paramString = lineString.substring(with: match.range(at: 2))
  367. if payloadType != context.payloadTypeOpus {
  368. return try line.reject()
  369. }
  370. // Split parameters
  371. let params = paramString?.split(separator: ";")
  372. // Specify what params we want to change
  373. let paramUpdates = ["stereo", "sprop-stereo", "cbr"]
  374. // Write unchanged params
  375. var builder = String()
  376. builder.append("a=fmtp:")
  377. builder.append(context.payloadTypeOpus)
  378. builder.append(" ")
  379. for param in params! {
  380. if let key = param.split(separator: "=").first, !param.isEmpty && !paramUpdates.contains(String(key)) {
  381. builder.append(contentsOf: param)
  382. builder.append(";")
  383. }
  384. }
  385. // Write our custom params
  386. builder.append("stereo=0;sprop-stereo=0;cbr=1")
  387. return try line.rewrite(line: builder)
  388. }
  389. // Handle RTP header extensions
  390. let rtpHeaderExtensionsRegex = try NSRegularExpression(pattern: VoIPCallSdpPatcher.SDP_EXTMAP_ANY_RE, options: [])
  391. if let match = rtpHeaderExtensionsRegex.firstMatch(in: lineString, options: [], range: lineRange) {
  392. let uriAndAttributes = lineString.substring(with: match.range(at: 1))
  393. return try handleRtpHeaderExtensionLine(&context, &line, String(uriAndAttributes!));
  394. }
  395. // Handle further common cases
  396. return try handleRtpAttributes(context, &line);
  397. }
  398. /// Handle video section line.
  399. /// - Parameters:
  400. /// - context: SdpPatcherContext
  401. /// - line: Line
  402. /// - Throws: SdpError
  403. /// - Returns: LineAction
  404. private func handleVideoLine(_ context: inout SdpPatcherContext, _ line: inout Line) throws -> LineAction {
  405. let lineString = line.line
  406. let lineRange = NSRange(lineString.startIndex..<lineString.endIndex, in: lineString)
  407. // Handle RTP header extensions
  408. let rtpHeaderExtensionRegex = try NSRegularExpression(pattern: VoIPCallSdpPatcher.SDP_EXTMAP_ANY_RE, options: [])
  409. if let match = rtpHeaderExtensionRegex.firstMatch(in: lineString, options: [], range: lineRange) {
  410. let uriAndAttributes = lineString.substring(with: match.range(at: 1))
  411. return try handleRtpHeaderExtensionLine(&context, &line, String(uriAndAttributes!));
  412. }
  413. // Handle further common cases
  414. return try handleRtpAttributes(context, &line)
  415. }
  416. /// Handle data channel section line.
  417. /// - Parameters:
  418. /// - context: SdpPatcherContext
  419. /// - line: Line
  420. /// - Throws: SdpError
  421. /// - Returns: LineAction
  422. private func handleDataChannelLine(_ context: SdpPatcherContext, _ line: inout Line) throws -> LineAction {
  423. return try line.accept()
  424. }
  425. /// Handle Rtp header extensions.
  426. /// - Parameters:
  427. /// - context: SdpPatcherContext
  428. /// - line: Line
  429. /// - uriAndAttributes: String
  430. /// - Throws: SdpError
  431. /// - Returns: LineAction
  432. private func handleRtpHeaderExtensionLine(_ context: inout SdpPatcherContext, _ line: inout Line, _ uriAndAttributes: String) throws -> LineAction {
  433. // Always reject if disabled
  434. if context.config.rtpHeaderExtensionConfig == .DISABLE {
  435. return try line.reject()
  436. }
  437. // Always reject some of the header extensions
  438. if uriAndAttributes.contains("urn:ietf:params:rtp-hdrext:ssrc-audio-level") || // Audio level, only useful for SFU use cases, remove
  439. uriAndAttributes.contains("urn:ietf:params:rtp-hdrext:csrc-audio-level") ||
  440. uriAndAttributes.contains("http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07") { // Frame marking, only useful for SFU use cases, remove
  441. return try line.reject()
  442. }
  443. // Require encryption for the remainder of headers
  444. if uriAndAttributes.starts(with: "urn:ietf:params:rtp-hdrext:encrypt") {
  445. return try remapRtpHeaderExtensionIfOutbound(&context, &line, uriAndAttributes)
  446. }
  447. // Reject the rest
  448. return try line.reject()
  449. }
  450. /// Handle remap Rtp header extension if outbound.
  451. /// - Parameters:
  452. /// - context: SdpPatcherContext
  453. /// - line: Line
  454. /// - uriAndAttributes: String
  455. /// - Throws: SdpError
  456. /// - Returns: LineAction
  457. private func remapRtpHeaderExtensionIfOutbound(_ context: inout SdpPatcherContext, _ line: inout Line, _ uriAndAttributes: String) throws -> LineAction {
  458. // Rewrite if local offer, otherwise accept
  459. if context.type == .LOCAL_OFFER {
  460. return try line.rewrite(line: String(format: "a=extmap:%i %@", context.rtpExtensionIdRemapper.assignId(uriAndAttributes: uriAndAttributes), uriAndAttributes))
  461. } else {
  462. return try line.accept()
  463. }
  464. }
  465. }
  466. extension String {
  467. var linesArray:[String] {
  468. var result:[String] = []
  469. enumerateLines { (line, _) -> () in
  470. result.append(line)
  471. }
  472. return result
  473. }
  474. }