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.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2008, Google Inc.
  4. * Copyright 2012, RTFM, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  20. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  25. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <vector>
  29. #if HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif // HAVE_CONFIG_H
  32. #include "talk/base/sslstreamadapterhelper.h"
  33. #include "talk/base/common.h"
  34. #include "talk/base/logging.h"
  35. #include "talk/base/stream.h"
  36. namespace talk_base {
  37. void SSLStreamAdapterHelper::SetIdentity(SSLIdentity* identity) {
  38. ASSERT(identity_.get() == NULL);
  39. identity_.reset(identity);
  40. }
  41. void SSLStreamAdapterHelper::SetServerRole(SSLRole role) {
  42. role_ = role;
  43. }
  44. int SSLStreamAdapterHelper::StartSSLWithServer(const char* server_name) {
  45. ASSERT(server_name != NULL && server_name[0] != '\0');
  46. ssl_server_name_ = server_name;
  47. return StartSSL();
  48. }
  49. int SSLStreamAdapterHelper::StartSSLWithPeer() {
  50. ASSERT(ssl_server_name_.empty());
  51. // It is permitted to specify peer_certificate_ only later.
  52. return StartSSL();
  53. }
  54. void SSLStreamAdapterHelper::SetMode(SSLMode mode) {
  55. ASSERT(state_ == SSL_NONE);
  56. ssl_mode_ = mode;
  57. }
  58. StreamState SSLStreamAdapterHelper::GetState() const {
  59. switch (state_) {
  60. case SSL_WAIT:
  61. case SSL_CONNECTING:
  62. return SS_OPENING;
  63. case SSL_CONNECTED:
  64. return SS_OPEN;
  65. default:
  66. return SS_CLOSED;
  67. };
  68. // not reached
  69. }
  70. void SSLStreamAdapterHelper::SetPeerCertificate(SSLCertificate* cert) {
  71. ASSERT(peer_certificate_.get() == NULL);
  72. ASSERT(peer_certificate_digest_algorithm_.empty());
  73. ASSERT(ssl_server_name_.empty());
  74. peer_certificate_.reset(cert);
  75. }
  76. bool SSLStreamAdapterHelper::SetPeerCertificateDigest(
  77. const std::string &digest_alg,
  78. const unsigned char* digest_val,
  79. size_t digest_len) {
  80. ASSERT(peer_certificate_.get() == NULL);
  81. ASSERT(peer_certificate_digest_algorithm_.empty());
  82. ASSERT(ssl_server_name_.empty());
  83. size_t expected_len;
  84. if (!GetDigestLength(digest_alg, &expected_len)) {
  85. LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg;
  86. return false;
  87. }
  88. if (expected_len != digest_len)
  89. return false;
  90. peer_certificate_digest_value_.SetData(digest_val, digest_len);
  91. peer_certificate_digest_algorithm_ = digest_alg;
  92. return true;
  93. }
  94. void SSLStreamAdapterHelper::Error(const char* context, int err, bool signal) {
  95. LOG(LS_WARNING) << "SSLStreamAdapterHelper::Error("
  96. << context << ", " << err << "," << signal << ")";
  97. state_ = SSL_ERROR;
  98. ssl_error_code_ = err;
  99. Cleanup();
  100. if (signal)
  101. StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err);
  102. }
  103. void SSLStreamAdapterHelper::Close() {
  104. Cleanup();
  105. ASSERT(state_ == SSL_CLOSED || state_ == SSL_ERROR);
  106. StreamAdapterInterface::Close();
  107. }
  108. int SSLStreamAdapterHelper::StartSSL() {
  109. ASSERT(state_ == SSL_NONE);
  110. if (StreamAdapterInterface::GetState() != SS_OPEN) {
  111. state_ = SSL_WAIT;
  112. return 0;
  113. }
  114. state_ = SSL_CONNECTING;
  115. int err = BeginSSL();
  116. if (err) {
  117. Error("BeginSSL", err, false);
  118. return err;
  119. }
  120. return 0;
  121. }
  122. } // namespace talk_base