Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip authentication for silence packets (which are discarded anyway). #279

Merged
merged 2 commits into from Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/kotlin/org/jitsi/nlj/srtp/SrtpConfig.kt
@@ -0,0 +1,27 @@
/*
* Copyright @ 2019 - present 8x8, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.nlj.srtp

import org.jitsi.config.JitsiConfig
import org.jitsi.metaconfig.config

class SrtpConfig {
companion object {
val maxConsecutivePacketsDiscardedEarly: Int by config {
"jmt.srtp.max-consecutive-packets-discarded-early".from(JitsiConfig.newConfig)
bbaldino marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
28 changes: 25 additions & 3 deletions src/main/kotlin/org/jitsi/nlj/srtp/SrtpTransformer.kt
Expand Up @@ -29,6 +29,7 @@ import org.jitsi.utils.logging2.Logger
import org.jitsi.utils.logging2.createChildLogger
import org.jitsi.utils.logging2.cwarn
import java.util.concurrent.ConcurrentHashMap
import org.jitsi.nlj.srtp.SrtpConfig.Companion.maxConsecutivePacketsDiscardedEarly

/**
* Implements the methods common to all 4 transformer implementation (encrypt/decrypt for SRTP/SRTCP)
Expand Down Expand Up @@ -177,13 +178,34 @@ class SrtpDecryptTransformer(
contextFactory: SrtpContextFactory,
parentLogger: Logger
) : SrtpTransformer(contextFactory, parentLogger) {
var earlyDiscardedPacketsSinceLastSuccess = 0
private val disableBypass = maxConsecutivePacketsDiscardedEarly <= 0
bbaldino marked this conversation as resolved.
Show resolved Hide resolved

override fun transform(packetInfo: PacketInfo, context: SrtpCryptoContext): SrtpErrorStatus {
// For silence packets we update the ROC (if authentication passes), but don't decrypt
return context.reverseTransformPacket(packetInfo.packetAs(), packetInfo.shouldDiscard).apply {
packetInfo.resetPayloadVerification()
// We want to avoid authenticating and decrypting packets that we are going to discarded (e.g. silence). We
// can not just discard them without passing them to the SRTP stack, because this will eventually break the ROC.
// Here we bypass the SRTP stack for packets marked to be discarded, but make sure that we haven't dropped too
// many consecutive packets.
if (packetInfo.shouldDiscard) {
return if (disableBypass ||
earlyDiscardedPacketsSinceLastSuccess++ > maxConsecutivePacketsDiscardedEarly) {
doTransform(packetInfo, context)
} else {
// Bypass the SRTP stack. The packet is already marked to be discarded, so there's no error condition.
SrtpErrorStatus.OK
}
}

return doTransform(packetInfo, context)
}

private fun doTransform(packetInfo: PacketInfo, context: SrtpCryptoContext): SrtpErrorStatus =
context.reverseTransformPacket(packetInfo.packetAs(), packetInfo.shouldDiscard).also {
packetInfo.resetPayloadVerification()
if (it == SrtpErrorStatus.OK) {
earlyDiscardedPacketsSinceLastSuccess = 0
}
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/reference.conf
Expand Up @@ -17,6 +17,12 @@ jmt {
}
}
}
srtp {
// The maximum number of packets that can be discarded early (without going through the SRTP stack for
// authentication), or -1 to authenticate all packets.
// This is useful to avoid unnecessary computation for audio silence.
max-consecutive-packets-discarded-early=-1
}

transceiver {
recv {
Expand Down