| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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 #if HAVE_OPENSSL_SSL_H | 11 #if HAVE_OPENSSL_SSL_H |
| 12 | 12 |
| 13 #include "webrtc/base/openssldigest.h" | 13 #include "webrtc/base/openssldigest.h" |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/common.h" | 16 #include "webrtc/base/common.h" |
| 16 #include "webrtc/base/openssl.h" | 17 #include "webrtc/base/openssl.h" |
| 17 | 18 |
| 18 namespace rtc { | 19 namespace rtc { |
| 19 | 20 |
| 20 OpenSSLDigest::OpenSSLDigest(const std::string& algorithm) { | 21 OpenSSLDigest::OpenSSLDigest(const std::string& algorithm) { |
| 21 EVP_MD_CTX_init(&ctx_); | 22 EVP_MD_CTX_init(&ctx_); |
| 22 if (GetDigestEVP(algorithm, &md_)) { | 23 if (GetDigestEVP(algorithm, &md_)) { |
| 23 EVP_DigestInit_ex(&ctx_, md_, NULL); | 24 EVP_DigestInit_ex(&ctx_, md_, NULL); |
| 24 } else { | 25 } else { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 44 EVP_DigestUpdate(&ctx_, buf, len); | 45 EVP_DigestUpdate(&ctx_, buf, len); |
| 45 } | 46 } |
| 46 | 47 |
| 47 size_t OpenSSLDigest::Finish(void* buf, size_t len) { | 48 size_t OpenSSLDigest::Finish(void* buf, size_t len) { |
| 48 if (!md_ || len < Size()) { | 49 if (!md_ || len < Size()) { |
| 49 return 0; | 50 return 0; |
| 50 } | 51 } |
| 51 unsigned int md_len; | 52 unsigned int md_len; |
| 52 EVP_DigestFinal_ex(&ctx_, static_cast<unsigned char*>(buf), &md_len); | 53 EVP_DigestFinal_ex(&ctx_, static_cast<unsigned char*>(buf), &md_len); |
| 53 EVP_DigestInit_ex(&ctx_, md_, NULL); // prepare for future Update()s | 54 EVP_DigestInit_ex(&ctx_, md_, NULL); // prepare for future Update()s |
| 54 ASSERT(md_len == Size()); | 55 RTC_DCHECK(md_len == Size()); |
| 55 return md_len; | 56 return md_len; |
| 56 } | 57 } |
| 57 | 58 |
| 58 bool OpenSSLDigest::GetDigestEVP(const std::string& algorithm, | 59 bool OpenSSLDigest::GetDigestEVP(const std::string& algorithm, |
| 59 const EVP_MD** mdp) { | 60 const EVP_MD** mdp) { |
| 60 const EVP_MD* md; | 61 const EVP_MD* md; |
| 61 if (algorithm == DIGEST_MD5) { | 62 if (algorithm == DIGEST_MD5) { |
| 62 md = EVP_md5(); | 63 md = EVP_md5(); |
| 63 } else if (algorithm == DIGEST_SHA_1) { | 64 } else if (algorithm == DIGEST_SHA_1) { |
| 64 md = EVP_sha1(); | 65 md = EVP_sha1(); |
| 65 } else if (algorithm == DIGEST_SHA_224) { | 66 } else if (algorithm == DIGEST_SHA_224) { |
| 66 md = EVP_sha224(); | 67 md = EVP_sha224(); |
| 67 } else if (algorithm == DIGEST_SHA_256) { | 68 } else if (algorithm == DIGEST_SHA_256) { |
| 68 md = EVP_sha256(); | 69 md = EVP_sha256(); |
| 69 } else if (algorithm == DIGEST_SHA_384) { | 70 } else if (algorithm == DIGEST_SHA_384) { |
| 70 md = EVP_sha384(); | 71 md = EVP_sha384(); |
| 71 } else if (algorithm == DIGEST_SHA_512) { | 72 } else if (algorithm == DIGEST_SHA_512) { |
| 72 md = EVP_sha512(); | 73 md = EVP_sha512(); |
| 73 } else { | 74 } else { |
| 74 return false; | 75 return false; |
| 75 } | 76 } |
| 76 | 77 |
| 77 // Can't happen | 78 // Can't happen |
| 78 ASSERT(EVP_MD_size(md) >= 16); | 79 RTC_DCHECK(EVP_MD_size(md) >= 16); |
| 79 *mdp = md; | 80 *mdp = md; |
| 80 return true; | 81 return true; |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool OpenSSLDigest::GetDigestName(const EVP_MD* md, | 84 bool OpenSSLDigest::GetDigestName(const EVP_MD* md, |
| 84 std::string* algorithm) { | 85 std::string* algorithm) { |
| 85 ASSERT(md != NULL); | 86 RTC_DCHECK(md != NULL); |
| 86 ASSERT(algorithm != NULL); | 87 RTC_DCHECK(algorithm != NULL); |
| 87 | 88 |
| 88 int md_type = EVP_MD_type(md); | 89 int md_type = EVP_MD_type(md); |
| 89 if (md_type == NID_md5) { | 90 if (md_type == NID_md5) { |
| 90 *algorithm = DIGEST_MD5; | 91 *algorithm = DIGEST_MD5; |
| 91 } else if (md_type == NID_sha1) { | 92 } else if (md_type == NID_sha1) { |
| 92 *algorithm = DIGEST_SHA_1; | 93 *algorithm = DIGEST_SHA_1; |
| 93 } else if (md_type == NID_sha224) { | 94 } else if (md_type == NID_sha224) { |
| 94 *algorithm = DIGEST_SHA_224; | 95 *algorithm = DIGEST_SHA_224; |
| 95 } else if (md_type == NID_sha256) { | 96 } else if (md_type == NID_sha256) { |
| 96 *algorithm = DIGEST_SHA_256; | 97 *algorithm = DIGEST_SHA_256; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 112 if (!GetDigestEVP(algorithm, &md)) | 113 if (!GetDigestEVP(algorithm, &md)) |
| 113 return false; | 114 return false; |
| 114 | 115 |
| 115 *length = EVP_MD_size(md); | 116 *length = EVP_MD_size(md); |
| 116 return true; | 117 return true; |
| 117 } | 118 } |
| 118 | 119 |
| 119 } // namespace rtc | 120 } // namespace rtc |
| 120 | 121 |
| 121 #endif // HAVE_OPENSSL_SSL_H | 122 #endif // HAVE_OPENSSL_SSL_H |
| 122 | |
| OLD | NEW |