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.

184 lines
6.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, 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. #include "talk/base/messagedigest.h"
  28. #include <string.h>
  29. #include "talk/base/sslconfig.h"
  30. #if SSL_USE_OPENSSL
  31. #include "talk/base/openssldigest.h"
  32. #else
  33. #include "talk/base/md5digest.h"
  34. #include "talk/base/sha1digest.h"
  35. #endif
  36. #include "talk/base/scoped_ptr.h"
  37. #include "talk/base/stringencode.h"
  38. namespace talk_base {
  39. // From RFC 4572.
  40. const char DIGEST_MD5[] = "md5";
  41. const char DIGEST_SHA_1[] = "sha-1";
  42. const char DIGEST_SHA_224[] = "sha-224";
  43. const char DIGEST_SHA_256[] = "sha-256";
  44. const char DIGEST_SHA_384[] = "sha-384";
  45. const char DIGEST_SHA_512[] = "sha-512";
  46. static const size_t kBlockSize = 64; // valid for SHA-256 and down
  47. MessageDigest* MessageDigestFactory::Create(const std::string& alg) {
  48. #if SSL_USE_OPENSSL
  49. MessageDigest* digest = new OpenSSLDigest(alg);
  50. if (digest->Size() == 0) { // invalid algorithm
  51. delete digest;
  52. digest = NULL;
  53. }
  54. return digest;
  55. #else
  56. MessageDigest* digest = NULL;
  57. if (alg == DIGEST_MD5) {
  58. digest = new Md5Digest();
  59. } else if (alg == DIGEST_SHA_1) {
  60. digest = new Sha1Digest();
  61. }
  62. return digest;
  63. #endif
  64. }
  65. size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len,
  66. void* output, size_t out_len) {
  67. digest->Update(input, in_len);
  68. return digest->Finish(output, out_len);
  69. }
  70. size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len,
  71. void* output, size_t out_len) {
  72. scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
  73. return (digest) ?
  74. ComputeDigest(digest.get(), input, in_len, output, out_len) :
  75. 0;
  76. }
  77. std::string ComputeDigest(MessageDigest* digest, const std::string& input) {
  78. scoped_array<char> output(new char[digest->Size()]);
  79. ComputeDigest(digest, input.data(), input.size(),
  80. output.get(), digest->Size());
  81. return hex_encode(output.get(), digest->Size());
  82. }
  83. bool ComputeDigest(const std::string& alg, const std::string& input,
  84. std::string* output) {
  85. scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
  86. if (!digest) {
  87. return false;
  88. }
  89. *output = ComputeDigest(digest.get(), input);
  90. return true;
  91. }
  92. std::string ComputeDigest(const std::string& alg, const std::string& input) {
  93. std::string output;
  94. ComputeDigest(alg, input, &output);
  95. return output;
  96. }
  97. // Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
  98. size_t ComputeHmac(MessageDigest* digest,
  99. const void* key, size_t key_len,
  100. const void* input, size_t in_len,
  101. void* output, size_t out_len) {
  102. // We only handle algorithms with a 64-byte blocksize.
  103. // TODO: Add BlockSize() method to MessageDigest.
  104. size_t block_len = kBlockSize;
  105. if (digest->Size() > 32) {
  106. return 0;
  107. }
  108. // Copy the key to a block-sized buffer to simplify padding.
  109. // If the key is longer than a block, hash it and use the result instead.
  110. scoped_array<uint8> new_key(new uint8[block_len]);
  111. if (key_len > block_len) {
  112. ComputeDigest(digest, key, key_len, new_key.get(), block_len);
  113. memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
  114. } else {
  115. memcpy(new_key.get(), key, key_len);
  116. memset(new_key.get() + key_len, 0, block_len - key_len);
  117. }
  118. // Set up the padding from the key, salting appropriately for each padding.
  119. scoped_array<uint8> o_pad(new uint8[block_len]), i_pad(new uint8[block_len]);
  120. for (size_t i = 0; i < block_len; ++i) {
  121. o_pad[i] = 0x5c ^ new_key[i];
  122. i_pad[i] = 0x36 ^ new_key[i];
  123. }
  124. // Inner hash; hash the inner padding, and then the input buffer.
  125. scoped_array<uint8> inner(new uint8[digest->Size()]);
  126. digest->Update(i_pad.get(), block_len);
  127. digest->Update(input, in_len);
  128. digest->Finish(inner.get(), digest->Size());
  129. // Outer hash; hash the outer padding, and then the result of the inner hash.
  130. digest->Update(o_pad.get(), block_len);
  131. digest->Update(inner.get(), digest->Size());
  132. return digest->Finish(output, out_len);
  133. }
  134. size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len,
  135. const void* input, size_t in_len,
  136. void* output, size_t out_len) {
  137. scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
  138. if (!digest) {
  139. return 0;
  140. }
  141. return ComputeHmac(digest.get(), key, key_len,
  142. input, in_len, output, out_len);
  143. }
  144. std::string ComputeHmac(MessageDigest* digest, const std::string& key,
  145. const std::string& input) {
  146. scoped_array<char> output(new char[digest->Size()]);
  147. ComputeHmac(digest, key.data(), key.size(),
  148. input.data(), input.size(), output.get(), digest->Size());
  149. return hex_encode(output.get(), digest->Size());
  150. }
  151. bool ComputeHmac(const std::string& alg, const std::string& key,
  152. const std::string& input, std::string* output) {
  153. scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
  154. if (!digest) {
  155. return false;
  156. }
  157. *output = ComputeHmac(digest.get(), key, input);
  158. return true;
  159. }
  160. std::string ComputeHmac(const std::string& alg, const std::string& key,
  161. const std::string& input) {
  162. std::string output;
  163. ComputeHmac(alg, key, input, &output);
  164. return output;
  165. }
  166. } // namespace talk_base