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.

150 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2008, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_OPENSSLIDENTITY_H__
  28. #define TALK_BASE_OPENSSLIDENTITY_H__
  29. #include <openssl/evp.h>
  30. #include <openssl/x509.h>
  31. #include <string>
  32. #include "talk/base/common.h"
  33. #include "talk/base/scoped_ptr.h"
  34. #include "talk/base/sslidentity.h"
  35. typedef struct ssl_ctx_st SSL_CTX;
  36. namespace talk_base {
  37. // OpenSSLKeyPair encapsulates an OpenSSL EVP_PKEY* keypair object,
  38. // which is reference counted inside the OpenSSL library.
  39. class OpenSSLKeyPair {
  40. public:
  41. static OpenSSLKeyPair* Generate();
  42. virtual ~OpenSSLKeyPair();
  43. virtual OpenSSLKeyPair* GetReference() {
  44. AddReference();
  45. return new OpenSSLKeyPair(pkey_);
  46. }
  47. EVP_PKEY* pkey() const { return pkey_; }
  48. private:
  49. explicit OpenSSLKeyPair(EVP_PKEY* pkey) : pkey_(pkey) {
  50. ASSERT(pkey_ != NULL);
  51. }
  52. void AddReference();
  53. EVP_PKEY* pkey_;
  54. DISALLOW_EVIL_CONSTRUCTORS(OpenSSLKeyPair);
  55. };
  56. // OpenSSLCertificate encapsulates an OpenSSL X509* certificate object,
  57. // which is also reference counted inside the OpenSSL library.
  58. class OpenSSLCertificate : public SSLCertificate {
  59. public:
  60. static OpenSSLCertificate* Generate(OpenSSLKeyPair* key_pair,
  61. const std::string& common_name);
  62. static OpenSSLCertificate* FromPEMString(const std::string& pem_string,
  63. int* pem_length);
  64. virtual ~OpenSSLCertificate();
  65. virtual OpenSSLCertificate* GetReference() const {
  66. AddReference();
  67. return new OpenSSLCertificate(x509_);
  68. }
  69. X509* x509() const { return x509_; }
  70. virtual std::string ToPEMString() const;
  71. // Compute the digest of the certificate given algorithm
  72. virtual bool ComputeDigest(const std::string &algorithm,
  73. unsigned char *digest, std::size_t size,
  74. std::size_t *length) const;
  75. // Compute the digest of a certificate as an X509 *
  76. static bool ComputeDigest(const X509 *x509,
  77. const std::string &algorithm,
  78. unsigned char *digest,
  79. std::size_t size,
  80. std::size_t *length);
  81. private:
  82. explicit OpenSSLCertificate(X509* x509) : x509_(x509) {
  83. ASSERT(x509_ != NULL);
  84. }
  85. void AddReference() const;
  86. X509* x509_;
  87. DISALLOW_EVIL_CONSTRUCTORS(OpenSSLCertificate);
  88. };
  89. // Holds a keypair and certificate together, and a method to generate
  90. // them consistently.
  91. class OpenSSLIdentity : public SSLIdentity {
  92. public:
  93. static OpenSSLIdentity* Generate(const std::string& common_name);
  94. virtual ~OpenSSLIdentity() { }
  95. virtual const OpenSSLCertificate& certificate() const {
  96. return *certificate_;
  97. }
  98. virtual OpenSSLIdentity* GetReference() const {
  99. return new OpenSSLIdentity(key_pair_->GetReference(),
  100. certificate_->GetReference());
  101. }
  102. // Configure an SSL context object to use our key and certificate.
  103. bool ConfigureIdentity(SSL_CTX* ctx);
  104. private:
  105. OpenSSLIdentity(OpenSSLKeyPair* key_pair,
  106. OpenSSLCertificate* certificate)
  107. : key_pair_(key_pair), certificate_(certificate) {
  108. ASSERT(key_pair != NULL);
  109. ASSERT(certificate != NULL);
  110. }
  111. scoped_ptr<OpenSSLKeyPair> key_pair_;
  112. scoped_ptr<OpenSSLCertificate> certificate_;
  113. DISALLOW_EVIL_CONSTRUCTORS(OpenSSLIdentity);
  114. };
  115. } // namespace talk_base
  116. #endif // TALK_BASE_OPENSSLIDENTITY_H__