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.

129 lines
4.1 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_NSSIDENTITY_H_
  28. #define TALK_BASE_NSSIDENTITY_H_
  29. #include <string>
  30. #include "cert.h"
  31. #include "nspr.h"
  32. #include "hasht.h"
  33. #include "keythi.h"
  34. #include "talk/base/common.h"
  35. #include "talk/base/logging.h"
  36. #include "talk/base/scoped_ptr.h"
  37. #include "talk/base/sslidentity.h"
  38. namespace talk_base {
  39. class NSSKeyPair {
  40. public:
  41. ~NSSKeyPair();
  42. // Generate a 1024-bit RSA key pair.
  43. static NSSKeyPair* Generate();
  44. NSSKeyPair* GetReference();
  45. SECKEYPrivateKey* privkey() const { return privkey_; }
  46. SECKEYPublicKey * pubkey() const { return pubkey_; }
  47. private:
  48. NSSKeyPair(SECKEYPrivateKey* privkey, SECKEYPublicKey* pubkey) :
  49. privkey_(privkey), pubkey_(pubkey) {}
  50. SECKEYPrivateKey* privkey_;
  51. SECKEYPublicKey* pubkey_;
  52. DISALLOW_EVIL_CONSTRUCTORS(NSSKeyPair);
  53. };
  54. class NSSCertificate : public SSLCertificate {
  55. public:
  56. static NSSCertificate* FromPEMString(const std::string& pem_string,
  57. int* pem_length);
  58. explicit NSSCertificate(CERTCertificate* cert) : certificate_(cert) {}
  59. virtual ~NSSCertificate() {
  60. if (certificate_)
  61. CERT_DestroyCertificate(certificate_);
  62. }
  63. virtual NSSCertificate* GetReference() const;
  64. virtual std::string ToPEMString() const;
  65. virtual bool ComputeDigest(const std::string& algorithm,
  66. unsigned char* digest, std::size_t size,
  67. std::size_t* length) const;
  68. CERTCertificate* certificate() { return certificate_; }
  69. // Helper function to get the length of a digest
  70. static bool GetDigestLength(const std::string& algorithm,
  71. std::size_t* length);
  72. // Comparison
  73. bool Equals(const NSSCertificate* tocompare) const;
  74. private:
  75. static bool GetDigestObject(const std::string& algorithm,
  76. const SECHashObject** hash_object);
  77. CERTCertificate* certificate_;
  78. DISALLOW_EVIL_CONSTRUCTORS(NSSCertificate);
  79. };
  80. // Represents a SSL key pair and certificate for NSS.
  81. class NSSIdentity : public SSLIdentity {
  82. public:
  83. static NSSIdentity* Generate(const std::string& common_name);
  84. virtual ~NSSIdentity() {
  85. LOG(LS_INFO) << "Destroying NSS identity";
  86. }
  87. virtual NSSIdentity* GetReference() const;
  88. virtual NSSCertificate& certificate() const;
  89. NSSKeyPair* keypair() const { return keypair_.get(); }
  90. private:
  91. NSSIdentity(NSSKeyPair* keypair, NSSCertificate* cert) :
  92. keypair_(keypair), certificate_(cert) {}
  93. talk_base::scoped_ptr<NSSKeyPair> keypair_;
  94. talk_base::scoped_ptr<NSSCertificate> certificate_;
  95. DISALLOW_EVIL_CONSTRUCTORS(NSSIdentity);
  96. };
  97. } // namespace talk_base
  98. #endif // TALK_BASE_NSSIDENTITY_H_