SRTPCryptographicContext.hh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved.
  15. // The SRTP 'Cryptographic Context', used in all of our uses of SRTP.
  16. // Definition
  17. #ifndef _SRTP_CRYPTOGRAPHIC_CONTEXT_HH
  18. #define _SRTP_CRYPTOGRAPHIC_CONTEXT_HH
  19. #ifndef _MIKEY_HH
  20. #include "MIKEY.hh"
  21. #endif
  22. class SRTPCryptographicContext {
  23. public:
  24. SRTPCryptographicContext(MIKEYState const& mikeyState);
  25. virtual ~SRTPCryptographicContext();
  26. // Authenticate (if necessary) and decrypt (if necessary) incoming SRTP and SRTCP packets.
  27. // Returns True iff the packet is well-formed and authenticates OK.
  28. // ("outPacketSize" will be <= "inPacketSize".)
  29. Boolean processIncomingSRTPPacket(u_int8_t* buffer, unsigned inPacketSize,
  30. unsigned& outPacketSize);
  31. Boolean processIncomingSRTCPPacket(u_int8_t* buffer, unsigned inPacketSize,
  32. unsigned& outPacketSize);
  33. // Encrypt (if necessary) and add an authentication tag (if necessary) to an outgoing
  34. // RTCP packet.
  35. // Returns True iff the packet is well-formed.
  36. // ("outPacketSize" will be >= "inPacketSize"; there must be enough space at the end of
  37. // "buffer" for the extra SRTCP tags (4+4+10 bytes).)
  38. Boolean processOutgoingSRTCPPacket(u_int8_t* buffer, unsigned inPacketSize,
  39. unsigned& outPacketSize);
  40. #ifndef NO_OPENSSL
  41. private:
  42. // Definitions specific to the "SRTP_AES128_CM_HMAC_SHA1_80" ciphersuite.
  43. // Later generalize to support more SRTP ciphersuites #####
  44. #define SRTP_CIPHER_KEY_LENGTH (128/8) // in bytes
  45. #define SRTP_CIPHER_SALT_LENGTH (112/8) // in bytes
  46. #define SRTP_MKI_LENGTH 4 // in bytes
  47. #define SRTP_AUTH_KEY_LENGTH (160/8) // in bytes
  48. #define SRTP_AUTH_TAG_LENGTH (80/8) // in bytes
  49. struct derivedKeys {
  50. u_int8_t cipherKey[SRTP_CIPHER_KEY_LENGTH];
  51. u_int8_t salt[SRTP_CIPHER_SALT_LENGTH];
  52. u_int8_t authKey[SRTP_AUTH_KEY_LENGTH];
  53. };
  54. struct allDerivedKeys {
  55. derivedKeys srtp;
  56. derivedKeys srtcp;
  57. };
  58. typedef enum {
  59. label_srtp_encryption = 0x00,
  60. label_srtp_msg_auth = 0x01,
  61. label_srtp_salt = 0x02,
  62. label_srtcp_encryption = 0x03,
  63. label_srtcp_msg_auth = 0x04,
  64. label_srtcp_salt = 0x05
  65. } SRTPKeyDerivationLabel;
  66. unsigned generateSRTCPAuthenticationTag(u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
  67. u_int8_t* resultAuthenticationTag);
  68. // returns the size of the resulting authentication tag
  69. Boolean verifySRTPAuthenticationTag(u_int8_t* dataToAuthenticate, unsigned numBytesToAuthenticate,
  70. u_int32_t roc, u_int8_t const* authenticationTag);
  71. Boolean verifySRTCPAuthenticationTag(u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
  72. u_int8_t const* authenticationTag);
  73. void decryptSRTPPacket(u_int64_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
  74. void decryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
  75. void encryptSRTCPPacket(u_int32_t index, u_int32_t ssrc, u_int8_t* data, unsigned numDataBytes);
  76. unsigned generateAuthenticationTag(derivedKeys& keysToUse,
  77. u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
  78. u_int8_t* resultAuthenticationTag);
  79. // returns the size of the resulting authentication tag
  80. // "resultAuthenticationTag" must point to an array of at least SRTP_AUTH_TAG_LENGTH
  81. Boolean verifyAuthenticationTag(derivedKeys& keysToUse,
  82. u_int8_t const* dataToAuthenticate, unsigned numBytesToAuthenticate,
  83. u_int8_t const* authenticationTag);
  84. void cryptData(derivedKeys& keys, u_int64_t index, u_int32_t ssrc,
  85. u_int8_t* data, unsigned numDataBytes);
  86. void performKeyDerivation();
  87. void deriveKeysFromMaster(u_int8_t const* masterKey, u_int8_t const* salt,
  88. allDerivedKeys& allKeysResult);
  89. // used to implement "performKeyDerivation()"
  90. void deriveSingleKey(u_int8_t const* masterKey, u_int8_t const* salt,
  91. SRTPKeyDerivationLabel label,
  92. unsigned resultKeyLength, u_int8_t* resultKey);
  93. // used to implement "deriveKeysFromMaster()".
  94. // ("resultKey" must be an existing buffer, of size >= "resultKeyLength")
  95. private:
  96. MIKEYState const& fMIKEYState;
  97. // Master key + salt:
  98. u_int8_t const* masterKeyPlusSalt() const { return fMIKEYState.keyData(); }
  99. u_int8_t const* masterKey() const { return &masterKeyPlusSalt()[0]; }
  100. u_int8_t const* masterSalt() const { return &masterKeyPlusSalt()[SRTP_CIPHER_KEY_LENGTH]; }
  101. Boolean weEncryptSRTP() const { return fMIKEYState.encryptSRTP(); }
  102. Boolean weEncryptSRTCP() const { return fMIKEYState.encryptSRTCP(); }
  103. Boolean weAuthenticate() const { return fMIKEYState.useAuthentication(); }
  104. u_int32_t MKI() const { return fMIKEYState.MKI(); }
  105. // Derived (i.e., session) keys:
  106. allDerivedKeys fDerivedKeys;
  107. // State used for handling the reception of SRTP packets:
  108. Boolean fHaveReceivedSRTPPackets;
  109. u_int16_t fPreviousHighRTPSeqNum;
  110. u_int32_t fROC; // rollover counter
  111. // State used for handling the sending of SRTCP packets:
  112. u_int32_t fSRTCPIndex;
  113. #endif
  114. };
  115. #endif