blob: c08cab4ea9db8c4120b89d6b0cdf9caca422c285 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
11#include "webrtc/base/messagedigest.h"
12
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
14
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <string.h>
16
kwiberg@webrtc.org11426dc2015-02-11 14:30:34 +000017#include "webrtc/base/basictypes.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/sslconfig.h"
19#if SSL_USE_OPENSSL
20#include "webrtc/base/openssldigest.h"
21#else
22#include "webrtc/base/md5digest.h"
23#include "webrtc/base/sha1digest.h"
24#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025#include "webrtc/base/stringencode.h"
26
27namespace rtc {
28
29// From RFC 4572.
30const char DIGEST_MD5[] = "md5";
31const char DIGEST_SHA_1[] = "sha-1";
32const char DIGEST_SHA_224[] = "sha-224";
33const char DIGEST_SHA_256[] = "sha-256";
34const char DIGEST_SHA_384[] = "sha-384";
35const char DIGEST_SHA_512[] = "sha-512";
36
37static const size_t kBlockSize = 64; // valid for SHA-256 and down
38
39MessageDigest* MessageDigestFactory::Create(const std::string& alg) {
40#if SSL_USE_OPENSSL
41 MessageDigest* digest = new OpenSSLDigest(alg);
42 if (digest->Size() == 0) { // invalid algorithm
43 delete digest;
44 digest = NULL;
45 }
46 return digest;
47#else
48 MessageDigest* digest = NULL;
49 if (alg == DIGEST_MD5) {
50 digest = new Md5Digest();
51 } else if (alg == DIGEST_SHA_1) {
52 digest = new Sha1Digest();
53 }
54 return digest;
55#endif
56}
57
58bool IsFips180DigestAlgorithm(const std::string& alg) {
59 // These are the FIPS 180 algorithms. According to RFC 4572 Section 5,
60 // "Self-signed certificates (for which legacy certificates are not a
61 // consideration) MUST use one of the FIPS 180 algorithms (SHA-1,
62 // SHA-224, SHA-256, SHA-384, or SHA-512) as their signature algorithm,
63 // and thus also MUST use it to calculate certificate fingerprints."
64 return alg == DIGEST_SHA_1 ||
65 alg == DIGEST_SHA_224 ||
66 alg == DIGEST_SHA_256 ||
67 alg == DIGEST_SHA_384 ||
68 alg == DIGEST_SHA_512;
69}
70
71size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len,
72 void* output, size_t out_len) {
73 digest->Update(input, in_len);
74 return digest->Finish(output, out_len);
75}
76
77size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len,
78 void* output, size_t out_len) {
jbauch555604a2016-04-26 03:13:22 -070079 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 return (digest) ?
81 ComputeDigest(digest.get(), input, in_len, output, out_len) :
82 0;
83}
84
85std::string ComputeDigest(MessageDigest* digest, const std::string& input) {
jbauch555604a2016-04-26 03:13:22 -070086 std::unique_ptr<char[]> output(new char[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 ComputeDigest(digest, input.data(), input.size(),
88 output.get(), digest->Size());
89 return hex_encode(output.get(), digest->Size());
90}
91
92bool ComputeDigest(const std::string& alg, const std::string& input,
93 std::string* output) {
jbauch555604a2016-04-26 03:13:22 -070094 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 if (!digest) {
96 return false;
97 }
98 *output = ComputeDigest(digest.get(), input);
99 return true;
100}
101
102std::string ComputeDigest(const std::string& alg, const std::string& input) {
103 std::string output;
104 ComputeDigest(alg, input, &output);
105 return output;
106}
107
108// Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
109size_t ComputeHmac(MessageDigest* digest,
110 const void* key, size_t key_len,
111 const void* input, size_t in_len,
112 void* output, size_t out_len) {
113 // We only handle algorithms with a 64-byte blocksize.
114 // TODO: Add BlockSize() method to MessageDigest.
115 size_t block_len = kBlockSize;
116 if (digest->Size() > 32) {
117 return 0;
118 }
119 // Copy the key to a block-sized buffer to simplify padding.
120 // If the key is longer than a block, hash it and use the result instead.
jbauch555604a2016-04-26 03:13:22 -0700121 std::unique_ptr<uint8_t[]> new_key(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 if (key_len > block_len) {
123 ComputeDigest(digest, key, key_len, new_key.get(), block_len);
124 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
125 } else {
126 memcpy(new_key.get(), key, key_len);
127 memset(new_key.get() + key_len, 0, block_len - key_len);
128 }
129 // Set up the padding from the key, salting appropriately for each padding.
jbauch555604a2016-04-26 03:13:22 -0700130 std::unique_ptr<uint8_t[]> o_pad(new uint8_t[block_len]);
131 std::unique_ptr<uint8_t[]> i_pad(new uint8_t[block_len]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000132 for (size_t i = 0; i < block_len; ++i) {
133 o_pad[i] = 0x5c ^ new_key[i];
134 i_pad[i] = 0x36 ^ new_key[i];
135 }
136 // Inner hash; hash the inner padding, and then the input buffer.
jbauch555604a2016-04-26 03:13:22 -0700137 std::unique_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000138 digest->Update(i_pad.get(), block_len);
139 digest->Update(input, in_len);
140 digest->Finish(inner.get(), digest->Size());
141 // Outer hash; hash the outer padding, and then the result of the inner hash.
142 digest->Update(o_pad.get(), block_len);
143 digest->Update(inner.get(), digest->Size());
144 return digest->Finish(output, out_len);
145}
146
147size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len,
148 const void* input, size_t in_len,
149 void* output, size_t out_len) {
jbauch555604a2016-04-26 03:13:22 -0700150 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 if (!digest) {
152 return 0;
153 }
154 return ComputeHmac(digest.get(), key, key_len,
155 input, in_len, output, out_len);
156}
157
158std::string ComputeHmac(MessageDigest* digest, const std::string& key,
159 const std::string& input) {
jbauch555604a2016-04-26 03:13:22 -0700160 std::unique_ptr<char[]> output(new char[digest->Size()]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000161 ComputeHmac(digest, key.data(), key.size(),
162 input.data(), input.size(), output.get(), digest->Size());
163 return hex_encode(output.get(), digest->Size());
164}
165
166bool ComputeHmac(const std::string& alg, const std::string& key,
167 const std::string& input, std::string* output) {
jbauch555604a2016-04-26 03:13:22 -0700168 std::unique_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169 if (!digest) {
170 return false;
171 }
172 *output = ComputeHmac(digest.get(), key, input);
173 return true;
174}
175
176std::string ComputeHmac(const std::string& alg, const std::string& key,
177 const std::string& input) {
178 std::string output;
179 ComputeHmac(alg, key, input, &output);
180 return output;
181}
182
183} // namespace rtc