Advertisement
mondain

DTLS notifyHandshakeComplete

Apr 24th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1.     SinglePacketTransformer initializeSRTPTransformer(int srtpProtectionProfile, TlsContext tlsContext) {
  2.         logger.debug("initializeSRTPTransformer");
  3.         int cipher, cipher_key_length, cipher_salt_length;
  4.         int auth_function, auth_key_length;
  5.         int RTCP_auth_tag_length, RTP_auth_tag_length;
  6.         // https://groups.google.com/forum/#!searchin/discuss-webrtc/dtls|sort:date/discuss-webrtc/DQnIWm5x5e0/CcRUadIyCAAJ
  7.         switch (srtpProtectionProfile) {
  8.             case SRTPProtectionProfile.SRTP_AES128_CM_HMAC_SHA1_32:
  9.                 cipher_key_length = 128 / 8;
  10.                 cipher_salt_length = 112 / 8;
  11.                 cipher = SRTPPolicy.AESCM_ENCRYPTION;
  12.                 auth_function = SRTPPolicy.HMACSHA1_AUTHENTICATION;
  13.                 auth_key_length = 160 / 8;
  14.                 RTCP_auth_tag_length = 80 / 8;
  15.                 RTP_auth_tag_length = 32 / 8;
  16.                 break;
  17.             case SRTPProtectionProfile.SRTP_AES128_CM_HMAC_SHA1_80:
  18.                 cipher_key_length = 128 / 8;
  19.                 cipher_salt_length = 112 / 8;
  20.                 cipher = SRTPPolicy.AESCM_ENCRYPTION;
  21.                 auth_function = SRTPPolicy.HMACSHA1_AUTHENTICATION;
  22.                 auth_key_length = 160 / 8;
  23.                 RTCP_auth_tag_length = RTP_auth_tag_length = 80 / 8;
  24.                 break;
  25.             case SRTPProtectionProfile.SRTP_NULL_HMAC_SHA1_32:
  26.                 cipher_key_length = 0;
  27.                 cipher_salt_length = 0;
  28.                 cipher = SRTPPolicy.NULL_ENCRYPTION;
  29.                 auth_function = SRTPPolicy.HMACSHA1_AUTHENTICATION;
  30.                 auth_key_length = 160 / 8;
  31.                 RTCP_auth_tag_length = 80 / 8;
  32.                 RTP_auth_tag_length = 32 / 8;
  33.                 break;
  34.             case SRTPProtectionProfile.SRTP_NULL_HMAC_SHA1_80:
  35.                 cipher_key_length = 0;
  36.                 cipher_salt_length = 0;
  37.                 cipher = SRTPPolicy.NULL_ENCRYPTION;
  38.                 auth_function = SRTPPolicy.HMACSHA1_AUTHENTICATION;
  39.                 auth_key_length = 160 / 8;
  40.                 RTCP_auth_tag_length = RTP_auth_tag_length = 80 / 8;
  41.                 break;
  42.             default:
  43.                 throw new IllegalArgumentException("srtpProtectionProfile");
  44.         }
  45.         // https://www.bouncycastle.org/docs/tlsdocs1.5on/org/bouncycastle/tls/package-summary.html
  46.         // this may only be called inside notifyHandshakeComplete with BC > 1.55
  47.         byte[] keyingMaterial = tlsContext.exportKeyingMaterial(ExporterLabel.dtls_srtp, null, 2 * (cipher_key_length + cipher_salt_length));
  48.         byte[] client_write_SRTP_master_key = new byte[cipher_key_length];
  49.         byte[] server_write_SRTP_master_key = new byte[cipher_key_length];
  50.         byte[] client_write_SRTP_master_salt = new byte[cipher_salt_length];
  51.         byte[] server_write_SRTP_master_salt = new byte[cipher_salt_length];
  52.         byte[][] keyingMaterialValues = { client_write_SRTP_master_key, server_write_SRTP_master_key, client_write_SRTP_master_salt, server_write_SRTP_master_salt };
  53.         for (int i = 0, keyingMaterialOffset = 0; i < keyingMaterialValues.length; i++) {
  54.             byte[] keyingMaterialValue = keyingMaterialValues[i];
  55.             System.arraycopy(keyingMaterial, keyingMaterialOffset, keyingMaterialValue, 0, keyingMaterialValue.length);
  56.             keyingMaterialOffset += keyingMaterialValue.length;
  57.         }
  58.         SRTPPolicy srtpPolicy = new SRTPPolicy(cipher, cipher_key_length, auth_function, auth_key_length, RTP_auth_tag_length, cipher_salt_length);
  59.         SRTPPolicy srtcpPolicy = new SRTPPolicy(cipher, cipher_key_length, auth_function, auth_key_length, RTCP_auth_tag_length, cipher_salt_length);
  60.         SRTPContextFactory clientSRTPContextFactory = new SRTPContextFactory(tlsContext instanceof TlsClientContext, client_write_SRTP_master_key, client_write_SRTP_master_salt, srtpPolicy, srtcpPolicy);
  61.         SRTPContextFactory serverSRTPContextFactory = new SRTPContextFactory(tlsContext instanceof TlsServerContext, server_write_SRTP_master_key, server_write_SRTP_master_salt, srtpPolicy, srtcpPolicy);
  62.         SRTPContextFactory forwardSRTPContextFactory;
  63.         SRTPContextFactory reverseSRTPContextFactory;
  64.         if (tlsContext instanceof TlsClientContext) {
  65.             forwardSRTPContextFactory = clientSRTPContextFactory;
  66.             reverseSRTPContextFactory = serverSRTPContextFactory;
  67.         } else if (tlsContext instanceof TlsServerContext) {
  68.             forwardSRTPContextFactory = serverSRTPContextFactory;
  69.             reverseSRTPContextFactory = clientSRTPContextFactory;
  70.         } else {
  71.             throw new IllegalArgumentException("tlsContext");
  72.         }
  73.         // handles rtp and rtcp
  74.         return new SRTPTransformer(forwardSRTPContextFactory, reverseSRTPContextFactory);
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement