You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/ssl_stream_adapter.h"
  11. #include "rtc_base/openssl_stream_adapter.h"
  12. ///////////////////////////////////////////////////////////////////////////////
  13. namespace rtc {
  14. // TODO(guoweis): Move this to SDP layer and use int form internally.
  15. // webrtc:5043.
  16. const char CS_AES_CM_128_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80";
  17. const char CS_AES_CM_128_HMAC_SHA1_32[] = "AES_CM_128_HMAC_SHA1_32";
  18. const char CS_AEAD_AES_128_GCM[] = "AEAD_AES_128_GCM";
  19. const char CS_AEAD_AES_256_GCM[] = "AEAD_AES_256_GCM";
  20. std::string SrtpCryptoSuiteToName(int crypto_suite) {
  21. switch (crypto_suite) {
  22. case SRTP_AES128_CM_SHA1_32:
  23. return CS_AES_CM_128_HMAC_SHA1_32;
  24. case SRTP_AES128_CM_SHA1_80:
  25. return CS_AES_CM_128_HMAC_SHA1_80;
  26. case SRTP_AEAD_AES_128_GCM:
  27. return CS_AEAD_AES_128_GCM;
  28. case SRTP_AEAD_AES_256_GCM:
  29. return CS_AEAD_AES_256_GCM;
  30. default:
  31. return std::string();
  32. }
  33. }
  34. int SrtpCryptoSuiteFromName(const std::string& crypto_suite) {
  35. if (crypto_suite == CS_AES_CM_128_HMAC_SHA1_32)
  36. return SRTP_AES128_CM_SHA1_32;
  37. if (crypto_suite == CS_AES_CM_128_HMAC_SHA1_80)
  38. return SRTP_AES128_CM_SHA1_80;
  39. if (crypto_suite == CS_AEAD_AES_128_GCM)
  40. return SRTP_AEAD_AES_128_GCM;
  41. if (crypto_suite == CS_AEAD_AES_256_GCM)
  42. return SRTP_AEAD_AES_256_GCM;
  43. return SRTP_INVALID_CRYPTO_SUITE;
  44. }
  45. bool GetSrtpKeyAndSaltLengths(int crypto_suite,
  46. int* key_length,
  47. int* salt_length) {
  48. switch (crypto_suite) {
  49. case SRTP_AES128_CM_SHA1_32:
  50. case SRTP_AES128_CM_SHA1_80:
  51. // SRTP_AES128_CM_HMAC_SHA1_32 and SRTP_AES128_CM_HMAC_SHA1_80 are defined
  52. // in RFC 5764 to use a 128 bits key and 112 bits salt for the cipher.
  53. *key_length = 16;
  54. *salt_length = 14;
  55. break;
  56. case SRTP_AEAD_AES_128_GCM:
  57. // SRTP_AEAD_AES_128_GCM is defined in RFC 7714 to use a 128 bits key and
  58. // a 96 bits salt for the cipher.
  59. *key_length = 16;
  60. *salt_length = 12;
  61. break;
  62. case SRTP_AEAD_AES_256_GCM:
  63. // SRTP_AEAD_AES_256_GCM is defined in RFC 7714 to use a 256 bits key and
  64. // a 96 bits salt for the cipher.
  65. *key_length = 32;
  66. *salt_length = 12;
  67. break;
  68. default:
  69. return false;
  70. }
  71. return true;
  72. }
  73. bool IsGcmCryptoSuite(int crypto_suite) {
  74. return (crypto_suite == SRTP_AEAD_AES_256_GCM ||
  75. crypto_suite == SRTP_AEAD_AES_128_GCM);
  76. }
  77. bool IsGcmCryptoSuiteName(const std::string& crypto_suite) {
  78. return (crypto_suite == CS_AEAD_AES_256_GCM ||
  79. crypto_suite == CS_AEAD_AES_128_GCM);
  80. }
  81. SSLStreamAdapter* SSLStreamAdapter::Create(StreamInterface* stream) {
  82. return new OpenSSLStreamAdapter(stream);
  83. }
  84. SSLStreamAdapter::SSLStreamAdapter(StreamInterface* stream)
  85. : StreamAdapterInterface(stream) {}
  86. SSLStreamAdapter::~SSLStreamAdapter() {}
  87. bool SSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
  88. return false;
  89. }
  90. bool SSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
  91. const uint8_t* context,
  92. size_t context_len,
  93. bool use_context,
  94. uint8_t* result,
  95. size_t result_len) {
  96. return false; // Default is unsupported
  97. }
  98. bool SSLStreamAdapter::SetDtlsSrtpCryptoSuites(
  99. const std::vector<int>& crypto_suites) {
  100. return false;
  101. }
  102. bool SSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
  103. return false;
  104. }
  105. bool SSLStreamAdapter::IsBoringSsl() {
  106. return OpenSSLStreamAdapter::IsBoringSsl();
  107. }
  108. bool SSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
  109. return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
  110. }
  111. bool SSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
  112. KeyType key_type) {
  113. return OpenSSLStreamAdapter::IsAcceptableCipher(cipher, key_type);
  114. }
  115. std::string SSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
  116. return OpenSSLStreamAdapter::SslCipherSuiteToName(cipher_suite);
  117. }
  118. ///////////////////////////////////////////////////////////////////////////////
  119. // Test only settings
  120. ///////////////////////////////////////////////////////////////////////////////
  121. void SSLStreamAdapter::EnableTimeCallbackForTesting() {
  122. OpenSSLStreamAdapter::EnableTimeCallbackForTesting();
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////
  125. } // namespace rtc