| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/messagedigest.h" | 11 #include "webrtc/base/messagedigest.h" |
| 12 | 12 |
| 13 #include <memory> |
| 14 |
| 13 #include <string.h> | 15 #include <string.h> |
| 14 | 16 |
| 15 #include "webrtc/base/basictypes.h" | 17 #include "webrtc/base/basictypes.h" |
| 16 #include "webrtc/base/sslconfig.h" | 18 #include "webrtc/base/sslconfig.h" |
| 17 #if SSL_USE_OPENSSL | 19 #if SSL_USE_OPENSSL |
| 18 #include "webrtc/base/openssldigest.h" | 20 #include "webrtc/base/openssldigest.h" |
| 19 #else | 21 #else |
| 20 #include "webrtc/base/md5digest.h" | 22 #include "webrtc/base/md5digest.h" |
| 21 #include "webrtc/base/sha1digest.h" | 23 #include "webrtc/base/sha1digest.h" |
| 22 #endif | 24 #endif |
| 23 #include "webrtc/base/scoped_ptr.h" | |
| 24 #include "webrtc/base/stringencode.h" | 25 #include "webrtc/base/stringencode.h" |
| 25 | 26 |
| 26 namespace rtc { | 27 namespace rtc { |
| 27 | 28 |
| 28 // From RFC 4572. | 29 // From RFC 4572. |
| 29 const char DIGEST_MD5[] = "md5"; | 30 const char DIGEST_MD5[] = "md5"; |
| 30 const char DIGEST_SHA_1[] = "sha-1"; | 31 const char DIGEST_SHA_1[] = "sha-1"; |
| 31 const char DIGEST_SHA_224[] = "sha-224"; | 32 const char DIGEST_SHA_224[] = "sha-224"; |
| 32 const char DIGEST_SHA_256[] = "sha-256"; | 33 const char DIGEST_SHA_256[] = "sha-256"; |
| 33 const char DIGEST_SHA_384[] = "sha-384"; | 34 const char DIGEST_SHA_384[] = "sha-384"; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 } | 69 } |
| 69 | 70 |
| 70 size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len, | 71 size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len, |
| 71 void* output, size_t out_len) { | 72 void* output, size_t out_len) { |
| 72 digest->Update(input, in_len); | 73 digest->Update(input, in_len); |
| 73 return digest->Finish(output, out_len); | 74 return digest->Finish(output, out_len); |
| 74 } | 75 } |
| 75 | 76 |
| 76 size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len, | 77 size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len, |
| 77 void* output, size_t out_len) { | 78 void* output, size_t out_len) { |
| 78 scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); | 79 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); |
| 79 return (digest) ? | 80 return (digest) ? |
| 80 ComputeDigest(digest.get(), input, in_len, output, out_len) : | 81 ComputeDigest(digest.get(), input, in_len, output, out_len) : |
| 81 0; | 82 0; |
| 82 } | 83 } |
| 83 | 84 |
| 84 std::string ComputeDigest(MessageDigest* digest, const std::string& input) { | 85 std::string ComputeDigest(MessageDigest* digest, const std::string& input) { |
| 85 scoped_ptr<char[]> output(new char[digest->Size()]); | 86 std::unique_ptr<char[]> output(new char[digest->Size()]); |
| 86 ComputeDigest(digest, input.data(), input.size(), | 87 ComputeDigest(digest, input.data(), input.size(), |
| 87 output.get(), digest->Size()); | 88 output.get(), digest->Size()); |
| 88 return hex_encode(output.get(), digest->Size()); | 89 return hex_encode(output.get(), digest->Size()); |
| 89 } | 90 } |
| 90 | 91 |
| 91 bool ComputeDigest(const std::string& alg, const std::string& input, | 92 bool ComputeDigest(const std::string& alg, const std::string& input, |
| 92 std::string* output) { | 93 std::string* output) { |
| 93 scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); | 94 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); |
| 94 if (!digest) { | 95 if (!digest) { |
| 95 return false; | 96 return false; |
| 96 } | 97 } |
| 97 *output = ComputeDigest(digest.get(), input); | 98 *output = ComputeDigest(digest.get(), input); |
| 98 return true; | 99 return true; |
| 99 } | 100 } |
| 100 | 101 |
| 101 std::string ComputeDigest(const std::string& alg, const std::string& input) { | 102 std::string ComputeDigest(const std::string& alg, const std::string& input) { |
| 102 std::string output; | 103 std::string output; |
| 103 ComputeDigest(alg, input, &output); | 104 ComputeDigest(alg, input, &output); |
| 104 return output; | 105 return output; |
| 105 } | 106 } |
| 106 | 107 |
| 107 // Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text)) | 108 // Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text)) |
| 108 size_t ComputeHmac(MessageDigest* digest, | 109 size_t ComputeHmac(MessageDigest* digest, |
| 109 const void* key, size_t key_len, | 110 const void* key, size_t key_len, |
| 110 const void* input, size_t in_len, | 111 const void* input, size_t in_len, |
| 111 void* output, size_t out_len) { | 112 void* output, size_t out_len) { |
| 112 // We only handle algorithms with a 64-byte blocksize. | 113 // We only handle algorithms with a 64-byte blocksize. |
| 113 // TODO: Add BlockSize() method to MessageDigest. | 114 // TODO: Add BlockSize() method to MessageDigest. |
| 114 size_t block_len = kBlockSize; | 115 size_t block_len = kBlockSize; |
| 115 if (digest->Size() > 32) { | 116 if (digest->Size() > 32) { |
| 116 return 0; | 117 return 0; |
| 117 } | 118 } |
| 118 // Copy the key to a block-sized buffer to simplify padding. | 119 // Copy the key to a block-sized buffer to simplify padding. |
| 119 // If the key is longer than a block, hash it and use the result instead. | 120 // If the key is longer than a block, hash it and use the result instead. |
| 120 scoped_ptr<uint8_t[]> new_key(new uint8_t[block_len]); | 121 std::unique_ptr<uint8_t[]> new_key(new uint8_t[block_len]); |
| 121 if (key_len > block_len) { | 122 if (key_len > block_len) { |
| 122 ComputeDigest(digest, key, key_len, new_key.get(), block_len); | 123 ComputeDigest(digest, key, key_len, new_key.get(), block_len); |
| 123 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size()); | 124 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size()); |
| 124 } else { | 125 } else { |
| 125 memcpy(new_key.get(), key, key_len); | 126 memcpy(new_key.get(), key, key_len); |
| 126 memset(new_key.get() + key_len, 0, block_len - key_len); | 127 memset(new_key.get() + key_len, 0, block_len - key_len); |
| 127 } | 128 } |
| 128 // Set up the padding from the key, salting appropriately for each padding. | 129 // Set up the padding from the key, salting appropriately for each padding. |
| 129 scoped_ptr<uint8_t[]> o_pad(new uint8_t[block_len]); | 130 std::unique_ptr<uint8_t[]> o_pad(new uint8_t[block_len]); |
| 130 scoped_ptr<uint8_t[]> i_pad(new uint8_t[block_len]); | 131 std::unique_ptr<uint8_t[]> i_pad(new uint8_t[block_len]); |
| 131 for (size_t i = 0; i < block_len; ++i) { | 132 for (size_t i = 0; i < block_len; ++i) { |
| 132 o_pad[i] = 0x5c ^ new_key[i]; | 133 o_pad[i] = 0x5c ^ new_key[i]; |
| 133 i_pad[i] = 0x36 ^ new_key[i]; | 134 i_pad[i] = 0x36 ^ new_key[i]; |
| 134 } | 135 } |
| 135 // Inner hash; hash the inner padding, and then the input buffer. | 136 // Inner hash; hash the inner padding, and then the input buffer. |
| 136 scoped_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]); | 137 std::unique_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]); |
| 137 digest->Update(i_pad.get(), block_len); | 138 digest->Update(i_pad.get(), block_len); |
| 138 digest->Update(input, in_len); | 139 digest->Update(input, in_len); |
| 139 digest->Finish(inner.get(), digest->Size()); | 140 digest->Finish(inner.get(), digest->Size()); |
| 140 // Outer hash; hash the outer padding, and then the result of the inner hash. | 141 // Outer hash; hash the outer padding, and then the result of the inner hash. |
| 141 digest->Update(o_pad.get(), block_len); | 142 digest->Update(o_pad.get(), block_len); |
| 142 digest->Update(inner.get(), digest->Size()); | 143 digest->Update(inner.get(), digest->Size()); |
| 143 return digest->Finish(output, out_len); | 144 return digest->Finish(output, out_len); |
| 144 } | 145 } |
| 145 | 146 |
| 146 size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len, | 147 size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len, |
| 147 const void* input, size_t in_len, | 148 const void* input, size_t in_len, |
| 148 void* output, size_t out_len) { | 149 void* output, size_t out_len) { |
| 149 scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); | 150 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); |
| 150 if (!digest) { | 151 if (!digest) { |
| 151 return 0; | 152 return 0; |
| 152 } | 153 } |
| 153 return ComputeHmac(digest.get(), key, key_len, | 154 return ComputeHmac(digest.get(), key, key_len, |
| 154 input, in_len, output, out_len); | 155 input, in_len, output, out_len); |
| 155 } | 156 } |
| 156 | 157 |
| 157 std::string ComputeHmac(MessageDigest* digest, const std::string& key, | 158 std::string ComputeHmac(MessageDigest* digest, const std::string& key, |
| 158 const std::string& input) { | 159 const std::string& input) { |
| 159 scoped_ptr<char[]> output(new char[digest->Size()]); | 160 std::unique_ptr<char[]> output(new char[digest->Size()]); |
| 160 ComputeHmac(digest, key.data(), key.size(), | 161 ComputeHmac(digest, key.data(), key.size(), |
| 161 input.data(), input.size(), output.get(), digest->Size()); | 162 input.data(), input.size(), output.get(), digest->Size()); |
| 162 return hex_encode(output.get(), digest->Size()); | 163 return hex_encode(output.get(), digest->Size()); |
| 163 } | 164 } |
| 164 | 165 |
| 165 bool ComputeHmac(const std::string& alg, const std::string& key, | 166 bool ComputeHmac(const std::string& alg, const std::string& key, |
| 166 const std::string& input, std::string* output) { | 167 const std::string& input, std::string* output) { |
| 167 scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); | 168 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); |
| 168 if (!digest) { | 169 if (!digest) { |
| 169 return false; | 170 return false; |
| 170 } | 171 } |
| 171 *output = ComputeHmac(digest.get(), key, input); | 172 *output = ComputeHmac(digest.get(), key, input); |
| 172 return true; | 173 return true; |
| 173 } | 174 } |
| 174 | 175 |
| 175 std::string ComputeHmac(const std::string& alg, const std::string& key, | 176 std::string ComputeHmac(const std::string& alg, const std::string& key, |
| 176 const std::string& input) { | 177 const std::string& input) { |
| 177 std::string output; | 178 std::string output; |
| 178 ComputeHmac(alg, key, input, &output); | 179 ComputeHmac(alg, key, input, &output); |
| 179 return output; | 180 return output; |
| 180 } | 181 } |
| 181 | 182 |
| 182 } // namespace rtc | 183 } // namespace rtc |
| OLD | NEW |