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.

182 lines
6.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2011 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/message_digest.h"
  11. #include <string.h>
  12. #include <cstdint>
  13. #include <memory>
  14. #include "rtc_base/openssl_digest.h"
  15. #include "rtc_base/string_encode.h"
  16. namespace rtc {
  17. // From RFC 4572.
  18. const char DIGEST_MD5[] = "md5";
  19. const char DIGEST_SHA_1[] = "sha-1";
  20. const char DIGEST_SHA_224[] = "sha-224";
  21. const char DIGEST_SHA_256[] = "sha-256";
  22. const char DIGEST_SHA_384[] = "sha-384";
  23. const char DIGEST_SHA_512[] = "sha-512";
  24. static const size_t kBlockSize = 64; // valid for SHA-256 and down
  25. MessageDigest* MessageDigestFactory::Create(const std::string& alg) {
  26. MessageDigest* digest = new OpenSSLDigest(alg);
  27. if (digest->Size() == 0) { // invalid algorithm
  28. delete digest;
  29. digest = nullptr;
  30. }
  31. return digest;
  32. }
  33. bool IsFips180DigestAlgorithm(const std::string& alg) {
  34. // These are the FIPS 180 algorithms. According to RFC 4572 Section 5,
  35. // "Self-signed certificates (for which legacy certificates are not a
  36. // consideration) MUST use one of the FIPS 180 algorithms (SHA-1,
  37. // SHA-224, SHA-256, SHA-384, or SHA-512) as their signature algorithm,
  38. // and thus also MUST use it to calculate certificate fingerprints."
  39. return alg == DIGEST_SHA_1 || alg == DIGEST_SHA_224 ||
  40. alg == DIGEST_SHA_256 || alg == DIGEST_SHA_384 ||
  41. alg == DIGEST_SHA_512;
  42. }
  43. size_t ComputeDigest(MessageDigest* digest,
  44. const void* input,
  45. size_t in_len,
  46. void* output,
  47. size_t out_len) {
  48. digest->Update(input, in_len);
  49. return digest->Finish(output, out_len);
  50. }
  51. size_t ComputeDigest(const std::string& alg,
  52. const void* input,
  53. size_t in_len,
  54. void* output,
  55. size_t out_len) {
  56. std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
  57. return (digest) ? ComputeDigest(digest.get(), input, in_len, output, out_len)
  58. : 0;
  59. }
  60. std::string ComputeDigest(MessageDigest* digest, const std::string& input) {
  61. std::unique_ptr<char[]> output(new char[digest->Size()]);
  62. ComputeDigest(digest, input.data(), input.size(), output.get(),
  63. digest->Size());
  64. return hex_encode(output.get(), digest->Size());
  65. }
  66. bool ComputeDigest(const std::string& alg,
  67. const std::string& input,
  68. std::string* output) {
  69. std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
  70. if (!digest) {
  71. return false;
  72. }
  73. *output = ComputeDigest(digest.get(), input);
  74. return true;
  75. }
  76. std::string ComputeDigest(const std::string& alg, const std::string& input) {
  77. std::string output;
  78. ComputeDigest(alg, input, &output);
  79. return output;
  80. }
  81. // Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
  82. size_t ComputeHmac(MessageDigest* digest,
  83. const void* key,
  84. size_t key_len,
  85. const void* input,
  86. size_t in_len,
  87. void* output,
  88. size_t out_len) {
  89. // We only handle algorithms with a 64-byte blocksize.
  90. // TODO: Add BlockSize() method to MessageDigest.
  91. size_t block_len = kBlockSize;
  92. if (digest->Size() > 32) {
  93. return 0;
  94. }
  95. // Copy the key to a block-sized buffer to simplify padding.
  96. // If the key is longer than a block, hash it and use the result instead.
  97. std::unique_ptr<uint8_t[]> new_key(new uint8_t[block_len]);
  98. if (key_len > block_len) {
  99. ComputeDigest(digest, key, key_len, new_key.get(), block_len);
  100. memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
  101. } else {
  102. memcpy(new_key.get(), key, key_len);
  103. memset(new_key.get() + key_len, 0, block_len - key_len);
  104. }
  105. // Set up the padding from the key, salting appropriately for each padding.
  106. std::unique_ptr<uint8_t[]> o_pad(new uint8_t[block_len]);
  107. std::unique_ptr<uint8_t[]> i_pad(new uint8_t[block_len]);
  108. for (size_t i = 0; i < block_len; ++i) {
  109. o_pad[i] = 0x5c ^ new_key[i];
  110. i_pad[i] = 0x36 ^ new_key[i];
  111. }
  112. // Inner hash; hash the inner padding, and then the input buffer.
  113. std::unique_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]);
  114. digest->Update(i_pad.get(), block_len);
  115. digest->Update(input, in_len);
  116. digest->Finish(inner.get(), digest->Size());
  117. // Outer hash; hash the outer padding, and then the result of the inner hash.
  118. digest->Update(o_pad.get(), block_len);
  119. digest->Update(inner.get(), digest->Size());
  120. return digest->Finish(output, out_len);
  121. }
  122. size_t ComputeHmac(const std::string& alg,
  123. const void* key,
  124. size_t key_len,
  125. const void* input,
  126. size_t in_len,
  127. void* output,
  128. size_t out_len) {
  129. std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
  130. if (!digest) {
  131. return 0;
  132. }
  133. return ComputeHmac(digest.get(), key, key_len, input, in_len, output,
  134. out_len);
  135. }
  136. std::string ComputeHmac(MessageDigest* digest,
  137. const std::string& key,
  138. const std::string& input) {
  139. std::unique_ptr<char[]> output(new char[digest->Size()]);
  140. ComputeHmac(digest, key.data(), key.size(), input.data(), input.size(),
  141. output.get(), digest->Size());
  142. return hex_encode(output.get(), digest->Size());
  143. }
  144. bool ComputeHmac(const std::string& alg,
  145. const std::string& key,
  146. const std::string& input,
  147. std::string* output) {
  148. std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
  149. if (!digest) {
  150. return false;
  151. }
  152. *output = ComputeHmac(digest.get(), key, input);
  153. return true;
  154. }
  155. std::string ComputeHmac(const std::string& alg,
  156. const std::string& key,
  157. const std::string& input) {
  158. std::string output;
  159. ComputeHmac(alg, key, input, &output);
  160. return output;
  161. }
  162. } // namespace rtc