henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/messagedigest.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/basictypes.h" |
| 18 | #include "rtc_base/openssldigest.h" |
| 19 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | // From RFC 4572. |
| 24 | const char DIGEST_MD5[] = "md5"; |
| 25 | const char DIGEST_SHA_1[] = "sha-1"; |
| 26 | const char DIGEST_SHA_224[] = "sha-224"; |
| 27 | const char DIGEST_SHA_256[] = "sha-256"; |
| 28 | const char DIGEST_SHA_384[] = "sha-384"; |
| 29 | const char DIGEST_SHA_512[] = "sha-512"; |
| 30 | |
| 31 | static const size_t kBlockSize = 64; // valid for SHA-256 and down |
| 32 | |
| 33 | MessageDigest* MessageDigestFactory::Create(const std::string& alg) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | MessageDigest* digest = new OpenSSLDigest(alg); |
| 35 | if (digest->Size() == 0) { // invalid algorithm |
| 36 | delete digest; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 37 | digest = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | } |
| 39 | return digest; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool IsFips180DigestAlgorithm(const std::string& alg) { |
| 43 | // These are the FIPS 180 algorithms. According to RFC 4572 Section 5, |
| 44 | // "Self-signed certificates (for which legacy certificates are not a |
| 45 | // consideration) MUST use one of the FIPS 180 algorithms (SHA-1, |
| 46 | // SHA-224, SHA-256, SHA-384, or SHA-512) as their signature algorithm, |
| 47 | // and thus also MUST use it to calculate certificate fingerprints." |
| 48 | return alg == DIGEST_SHA_1 || |
| 49 | alg == DIGEST_SHA_224 || |
| 50 | alg == DIGEST_SHA_256 || |
| 51 | alg == DIGEST_SHA_384 || |
| 52 | alg == DIGEST_SHA_512; |
| 53 | } |
| 54 | |
| 55 | size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len, |
| 56 | void* output, size_t out_len) { |
| 57 | digest->Update(input, in_len); |
| 58 | return digest->Finish(output, out_len); |
| 59 | } |
| 60 | |
| 61 | size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len, |
| 62 | void* output, size_t out_len) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 63 | std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 64 | return (digest) ? |
| 65 | ComputeDigest(digest.get(), input, in_len, output, out_len) : |
| 66 | 0; |
| 67 | } |
| 68 | |
| 69 | std::string ComputeDigest(MessageDigest* digest, const std::string& input) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 70 | std::unique_ptr<char[]> output(new char[digest->Size()]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | ComputeDigest(digest, input.data(), input.size(), |
| 72 | output.get(), digest->Size()); |
| 73 | return hex_encode(output.get(), digest->Size()); |
| 74 | } |
| 75 | |
| 76 | bool ComputeDigest(const std::string& alg, const std::string& input, |
| 77 | std::string* output) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 78 | std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | if (!digest) { |
| 80 | return false; |
| 81 | } |
| 82 | *output = ComputeDigest(digest.get(), input); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | std::string ComputeDigest(const std::string& alg, const std::string& input) { |
| 87 | std::string output; |
| 88 | ComputeDigest(alg, input, &output); |
| 89 | return output; |
| 90 | } |
| 91 | |
| 92 | // Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text)) |
| 93 | size_t ComputeHmac(MessageDigest* digest, |
| 94 | const void* key, size_t key_len, |
| 95 | const void* input, size_t in_len, |
| 96 | void* output, size_t out_len) { |
| 97 | // We only handle algorithms with a 64-byte blocksize. |
| 98 | // TODO: Add BlockSize() method to MessageDigest. |
| 99 | size_t block_len = kBlockSize; |
| 100 | if (digest->Size() > 32) { |
| 101 | return 0; |
| 102 | } |
| 103 | // Copy the key to a block-sized buffer to simplify padding. |
| 104 | // If the key is longer than a block, hash it and use the result instead. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 105 | std::unique_ptr<uint8_t[]> new_key(new uint8_t[block_len]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | if (key_len > block_len) { |
| 107 | ComputeDigest(digest, key, key_len, new_key.get(), block_len); |
| 108 | memset(new_key.get() + digest->Size(), 0, block_len - digest->Size()); |
| 109 | } else { |
| 110 | memcpy(new_key.get(), key, key_len); |
| 111 | memset(new_key.get() + key_len, 0, block_len - key_len); |
| 112 | } |
| 113 | // Set up the padding from the key, salting appropriately for each padding. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 114 | std::unique_ptr<uint8_t[]> o_pad(new uint8_t[block_len]); |
| 115 | std::unique_ptr<uint8_t[]> i_pad(new uint8_t[block_len]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | for (size_t i = 0; i < block_len; ++i) { |
| 117 | o_pad[i] = 0x5c ^ new_key[i]; |
| 118 | i_pad[i] = 0x36 ^ new_key[i]; |
| 119 | } |
| 120 | // Inner hash; hash the inner padding, and then the input buffer. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 121 | std::unique_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | digest->Update(i_pad.get(), block_len); |
| 123 | digest->Update(input, in_len); |
| 124 | digest->Finish(inner.get(), digest->Size()); |
| 125 | // Outer hash; hash the outer padding, and then the result of the inner hash. |
| 126 | digest->Update(o_pad.get(), block_len); |
| 127 | digest->Update(inner.get(), digest->Size()); |
| 128 | return digest->Finish(output, out_len); |
| 129 | } |
| 130 | |
| 131 | size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len, |
| 132 | const void* input, size_t in_len, |
| 133 | void* output, size_t out_len) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 134 | std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | if (!digest) { |
| 136 | return 0; |
| 137 | } |
| 138 | return ComputeHmac(digest.get(), key, key_len, |
| 139 | input, in_len, output, out_len); |
| 140 | } |
| 141 | |
| 142 | std::string ComputeHmac(MessageDigest* digest, const std::string& key, |
| 143 | const std::string& input) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 144 | std::unique_ptr<char[]> output(new char[digest->Size()]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 145 | ComputeHmac(digest, key.data(), key.size(), |
| 146 | input.data(), input.size(), output.get(), digest->Size()); |
| 147 | return hex_encode(output.get(), digest->Size()); |
| 148 | } |
| 149 | |
| 150 | bool ComputeHmac(const std::string& alg, const std::string& key, |
| 151 | const std::string& input, std::string* output) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 152 | std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 153 | if (!digest) { |
| 154 | return false; |
| 155 | } |
| 156 | *output = ComputeHmac(digest.get(), key, input); |
| 157 | return true; |
| 158 | } |
| 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 | |
| 167 | } // namespace rtc |