dtlsidentity.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef dtls_identity_h__
  6. #define dtls_identity_h__
  7. #include <string>
  8. #include "m_cpp_utils.h"
  9. #include "mozilla/RefPtr.h"
  10. #include "nsISupportsImpl.h"
  11. #include "sslt.h"
  12. #include "ScopedNSSTypes.h"
  13. // All code in this module requires NSS to be live.
  14. // Callers must initialize NSS and implement the nsNSSShutdownObject
  15. // protocol.
  16. namespace mozilla {
  17. class DtlsIdentity final {
  18. public:
  19. // This constructor takes ownership of privkey and cert.
  20. DtlsIdentity(SECKEYPrivateKey *privkey,
  21. CERTCertificate *cert,
  22. SSLKEAType authType)
  23. : private_key_(privkey), cert_(cert), auth_type_(authType) {}
  24. // This is only for use in tests, or for external linkage. It makes a (bad)
  25. // instance of this class.
  26. static RefPtr<DtlsIdentity> Generate();
  27. // These don't create copies or transfer ownership. If you want these to live
  28. // on, make a copy.
  29. const UniqueCERTCertificate& cert() const { return cert_; }
  30. SECKEYPrivateKey *privkey() const { return private_key_; }
  31. // Note: this uses SSLKEAType because that is what the libssl API requires.
  32. // This is a giant confusing mess, but libssl indexes certificates based on a
  33. // key exchange type, not authentication type (as you might have reasonably
  34. // expected).
  35. SSLKEAType auth_type() const { return auth_type_; }
  36. nsresult ComputeFingerprint(const std::string algorithm,
  37. uint8_t *digest,
  38. size_t size,
  39. size_t *digest_length) const;
  40. static nsresult ComputeFingerprint(const UniqueCERTCertificate& cert,
  41. const std::string algorithm,
  42. uint8_t *digest,
  43. size_t size,
  44. size_t *digest_length);
  45. static const std::string DEFAULT_HASH_ALGORITHM;
  46. enum {
  47. HASH_ALGORITHM_MAX_LENGTH = 64
  48. };
  49. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DtlsIdentity)
  50. private:
  51. ~DtlsIdentity() {}
  52. DISALLOW_COPY_ASSIGN(DtlsIdentity);
  53. ScopedSECKEYPrivateKey private_key_;
  54. UniqueCERTCertificate cert_;
  55. SSLKEAType auth_type_;
  56. };
  57. } // close namespace
  58. #endif