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.

130 lines
4.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2008, Google Inc.
  4. * Copyright 2011, 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. #ifndef TALK_BASE_NSSSTREAMADAPTER_H_
  29. #define TALK_BASE_NSSSTREAMADAPTER_H_
  30. #include <string>
  31. #include <vector>
  32. #include "nspr.h"
  33. #include "nss.h"
  34. #include "secmodt.h"
  35. #include "talk/base/buffer.h"
  36. #include "talk/base/nssidentity.h"
  37. #include "talk/base/ssladapter.h"
  38. #include "talk/base/sslstreamadapter.h"
  39. #include "talk/base/sslstreamadapterhelper.h"
  40. namespace talk_base {
  41. // Singleton
  42. class NSSContext {
  43. public:
  44. NSSContext() {}
  45. ~NSSContext() {
  46. }
  47. static PK11SlotInfo *GetSlot() {
  48. return Instance() ? Instance()->slot_: NULL;
  49. }
  50. static NSSContext *Instance();
  51. static bool InitializeSSL(VerificationCallback callback);
  52. static bool InitializeSSLThread();
  53. static bool CleanupSSL();
  54. private:
  55. PK11SlotInfo *slot_; // The PKCS-11 slot
  56. static bool initialized; // Was this initialized?
  57. static NSSContext *global_nss_context; // The global context
  58. };
  59. class NSSStreamAdapter : public SSLStreamAdapterHelper {
  60. public:
  61. explicit NSSStreamAdapter(StreamInterface* stream);
  62. virtual ~NSSStreamAdapter();
  63. bool Init();
  64. virtual StreamResult Read(void* data, size_t data_len,
  65. size_t* read, int* error);
  66. virtual StreamResult Write(const void* data, size_t data_len,
  67. size_t* written, int* error);
  68. void OnMessage(Message *msg);
  69. // Key Extractor interface
  70. virtual bool ExportKeyingMaterial(const std::string& label,
  71. const uint8* context,
  72. size_t context_len,
  73. bool use_context,
  74. uint8* result,
  75. size_t result_len);
  76. // DTLS-SRTP interface
  77. virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers);
  78. virtual bool GetDtlsSrtpCipher(std::string* cipher);
  79. // Capabilities interfaces
  80. static bool HaveDtls();
  81. static bool HaveDtlsSrtp();
  82. static bool HaveExporter();
  83. protected:
  84. // Override SSLStreamAdapter
  85. virtual void OnEvent(StreamInterface* stream, int events, int err);
  86. // Override SSLStreamAdapterHelper
  87. virtual int BeginSSL();
  88. virtual void Cleanup();
  89. virtual bool GetDigestLength(const std::string &algorithm,
  90. std::size_t *length) {
  91. return NSSCertificate::GetDigestLength(algorithm, length);
  92. }
  93. private:
  94. int ContinueSSL();
  95. static SECStatus AuthCertificateHook(void *arg, PRFileDesc *fd,
  96. PRBool checksig, PRBool isServer);
  97. static SECStatus GetClientAuthDataHook(void *arg, PRFileDesc *fd,
  98. CERTDistNames *caNames,
  99. CERTCertificate **pRetCert,
  100. SECKEYPrivateKey **pRetKey);
  101. PRFileDesc *ssl_fd_; // NSS's SSL file descriptor
  102. static bool initialized; // Was InitializeSSL() called?
  103. bool cert_ok_; // Did we get and check a cert
  104. std::vector<PRUint16> srtp_ciphers_; // SRTP cipher list
  105. static PRDescIdentity nspr_layer_identity; // The NSPR layer identity
  106. };
  107. } // namespace talk_base
  108. #endif // TALK_BASE_NSSSTREAMADAPTER_H_