| 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> | 13 #include <memory> |
| 14 | 14 |
| 15 #include <string.h> | 15 #include <string.h> |
| 16 | 16 |
| 17 #include "webrtc/base/basictypes.h" | 17 #include "webrtc/base/basictypes.h" |
| 18 #include "webrtc/base/sslconfig.h" | |
| 19 #if SSL_USE_OPENSSL | |
| 20 #include "webrtc/base/openssldigest.h" | 18 #include "webrtc/base/openssldigest.h" |
| 21 #else | |
| 22 #include "webrtc/base/md5digest.h" | |
| 23 #include "webrtc/base/sha1digest.h" | |
| 24 #endif | |
| 25 #include "webrtc/base/stringencode.h" | 19 #include "webrtc/base/stringencode.h" |
| 26 | 20 |
| 27 namespace rtc { | 21 namespace rtc { |
| 28 | 22 |
| 29 // From RFC 4572. | 23 // From RFC 4572. |
| 30 const char DIGEST_MD5[] = "md5"; | 24 const char DIGEST_MD5[] = "md5"; |
| 31 const char DIGEST_SHA_1[] = "sha-1"; | 25 const char DIGEST_SHA_1[] = "sha-1"; |
| 32 const char DIGEST_SHA_224[] = "sha-224"; | 26 const char DIGEST_SHA_224[] = "sha-224"; |
| 33 const char DIGEST_SHA_256[] = "sha-256"; | 27 const char DIGEST_SHA_256[] = "sha-256"; |
| 34 const char DIGEST_SHA_384[] = "sha-384"; | 28 const char DIGEST_SHA_384[] = "sha-384"; |
| 35 const char DIGEST_SHA_512[] = "sha-512"; | 29 const char DIGEST_SHA_512[] = "sha-512"; |
| 36 | 30 |
| 37 static const size_t kBlockSize = 64; // valid for SHA-256 and down | 31 static const size_t kBlockSize = 64; // valid for SHA-256 and down |
| 38 | 32 |
| 39 MessageDigest* MessageDigestFactory::Create(const std::string& alg) { | 33 MessageDigest* MessageDigestFactory::Create(const std::string& alg) { |
| 40 #if SSL_USE_OPENSSL | |
| 41 MessageDigest* digest = new OpenSSLDigest(alg); | 34 MessageDigest* digest = new OpenSSLDigest(alg); |
| 42 if (digest->Size() == 0) { // invalid algorithm | 35 if (digest->Size() == 0) { // invalid algorithm |
| 43 delete digest; | 36 delete digest; |
| 44 digest = NULL; | 37 digest = NULL; |
| 45 } | 38 } |
| 46 return digest; | 39 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 } | 40 } |
| 57 | 41 |
| 58 bool IsFips180DigestAlgorithm(const std::string& alg) { | 42 bool IsFips180DigestAlgorithm(const std::string& alg) { |
| 59 // These are the FIPS 180 algorithms. According to RFC 4572 Section 5, | 43 // These are the FIPS 180 algorithms. According to RFC 4572 Section 5, |
| 60 // "Self-signed certificates (for which legacy certificates are not a | 44 // "Self-signed certificates (for which legacy certificates are not a |
| 61 // consideration) MUST use one of the FIPS 180 algorithms (SHA-1, | 45 // 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, | 46 // SHA-224, SHA-256, SHA-384, or SHA-512) as their signature algorithm, |
| 63 // and thus also MUST use it to calculate certificate fingerprints." | 47 // and thus also MUST use it to calculate certificate fingerprints." |
| 64 return alg == DIGEST_SHA_1 || | 48 return alg == DIGEST_SHA_1 || |
| 65 alg == DIGEST_SHA_224 || | 49 alg == DIGEST_SHA_224 || |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 } | 158 } |
| 175 | 159 |
| 176 std::string ComputeHmac(const std::string& alg, const std::string& key, | 160 std::string ComputeHmac(const std::string& alg, const std::string& key, |
| 177 const std::string& input) { | 161 const std::string& input) { |
| 178 std::string output; | 162 std::string output; |
| 179 ComputeHmac(alg, key, input, &output); | 163 ComputeHmac(alg, key, input, &output); |
| 180 return output; | 164 return output; |
| 181 } | 165 } |
| 182 | 166 |
| 183 } // namespace rtc | 167 } // namespace rtc |
| OLD | NEW |