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.

109 lines
3.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, 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. #ifndef TALK_BASE_SSLFINGERPRINT_H_
  29. #define TALK_BASE_SSLFINGERPRINT_H_
  30. #include <ctype.h>
  31. #include <string>
  32. #include "talk/base/buffer.h"
  33. #include "talk/base/helpers.h"
  34. #include "talk/base/messagedigest.h"
  35. #include "talk/base/sslidentity.h"
  36. #include "talk/base/stringencode.h"
  37. namespace talk_base {
  38. struct SSLFingerprint {
  39. static SSLFingerprint* Create(const std::string& algorithm,
  40. const talk_base::SSLIdentity* identity) {
  41. if (!identity) {
  42. return NULL;
  43. }
  44. uint8 digest_val[64];
  45. size_t digest_len;
  46. bool ret = identity->certificate().ComputeDigest(
  47. algorithm, digest_val, sizeof(digest_val), &digest_len);
  48. if (!ret) {
  49. return NULL;
  50. }
  51. return new SSLFingerprint(algorithm, digest_val, digest_len);
  52. }
  53. static SSLFingerprint* CreateFromRfc4572(const std::string& algorithm,
  54. const std::string& fingerprint) {
  55. if (algorithm.empty())
  56. return NULL;
  57. if (fingerprint.empty())
  58. return NULL;
  59. size_t value_len;
  60. char value[talk_base::MessageDigest::kMaxSize];
  61. value_len = talk_base::hex_decode_with_delimiter(value, sizeof(value),
  62. fingerprint.c_str(),
  63. fingerprint.length(),
  64. ':');
  65. if (!value_len)
  66. return NULL;
  67. return new SSLFingerprint(algorithm,
  68. reinterpret_cast<uint8*>(value),
  69. value_len);
  70. }
  71. SSLFingerprint(const std::string& algorithm, const uint8* digest_in,
  72. size_t digest_len) : algorithm(algorithm) {
  73. digest.SetData(digest_in, digest_len);
  74. }
  75. SSLFingerprint(const SSLFingerprint& from)
  76. : algorithm(from.algorithm), digest(from.digest) {}
  77. bool operator==(const SSLFingerprint& other) const {
  78. return algorithm == other.algorithm &&
  79. digest == other.digest;
  80. }
  81. std::string GetRfc4752Fingerprint() const {
  82. std::string fingerprint =
  83. talk_base::hex_encode_with_delimiter(
  84. digest.data(), digest.length(), ':');
  85. std::transform(fingerprint.begin(), fingerprint.end(),
  86. fingerprint.begin(), ::toupper);
  87. return fingerprint;
  88. }
  89. std::string algorithm;
  90. talk_base::Buffer digest;
  91. };
  92. } // namespace talk_base
  93. #endif // TALK_BASE_SSLFINGERPRINT_H_