henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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/sslfingerprint.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <string> |
| 15 | |
tzik | d0be002 | 2018-10-18 15:10:02 +0900 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/logging.h" |
| 18 | #include "rtc_base/messagedigest.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 | |
Henrik Grunell | 2b15626 | 2018-10-11 11:15:48 +0000 | [diff] [blame] | 23 | SSLFingerprint* SSLFingerprint::Create(const std::string& algorithm, |
Mirko Bonadei | 6932fb2 | 2018-10-15 14:18:03 +0000 | [diff] [blame] | 24 | const rtc::SSLIdentity* identity) { |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 25 | return CreateUnique(algorithm, *identity).release(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 28 | std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUnique( |
| 29 | const std::string& algorithm, |
| 30 | const rtc::SSLIdentity& identity) { |
| 31 | return Create(algorithm, identity.certificate()); |
| 32 | } |
| 33 | |
| 34 | std::unique_ptr<SSLFingerprint> SSLFingerprint::Create( |
| 35 | const std::string& algorithm, |
| 36 | const rtc::SSLCertificate& cert) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 37 | uint8_t digest_val[64]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | size_t digest_len; |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 39 | bool ret = cert.ComputeDigest(algorithm, digest_val, sizeof(digest_val), |
| 40 | &digest_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | if (!ret) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 42 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | } |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 44 | return absl::make_unique<SSLFingerprint>( |
| 45 | algorithm, ArrayView<const uint8_t>(digest_val, digest_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | SSLFingerprint* SSLFingerprint::CreateFromRfc4572( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | const std::string& algorithm, |
| 50 | const std::string& fingerprint) { |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 51 | return CreateUniqueFromRfc4572(algorithm, fingerprint).release(); |
| 52 | } |
| 53 | |
| 54 | std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateUniqueFromRfc4572( |
| 55 | const std::string& algorithm, |
| 56 | const std::string& fingerprint) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 57 | if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm)) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 58 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | |
| 60 | if (fingerprint.empty()) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 61 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | char value[rtc::MessageDigest::kMaxSize]; |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 64 | size_t value_len = rtc::hex_decode_with_delimiter( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 65 | value, sizeof(value), fingerprint.c_str(), fingerprint.length(), ':'); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | if (!value_len) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 67 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 69 | return absl::make_unique<SSLFingerprint>( |
| 70 | algorithm, |
| 71 | ArrayView<const uint8_t>(reinterpret_cast<uint8_t*>(value), value_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 74 | std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateFromCertificate( |
| 75 | const RTCCertificate& cert) { |
deadbeef | 8662f94 | 2017-01-20 21:20:51 -0800 | [diff] [blame] | 76 | std::string digest_alg; |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 77 | if (!cert.ssl_certificate().GetSignatureDigestAlgorithm(&digest_alg)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 78 | RTC_LOG(LS_ERROR) |
| 79 | << "Failed to retrieve the certificate's digest algorithm"; |
deadbeef | 8662f94 | 2017-01-20 21:20:51 -0800 | [diff] [blame] | 80 | return nullptr; |
| 81 | } |
| 82 | |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 83 | std::unique_ptr<SSLFingerprint> fingerprint = |
| 84 | CreateUnique(digest_alg, *cert.identity()); |
deadbeef | 8662f94 | 2017-01-20 21:20:51 -0800 | [diff] [blame] | 85 | if (!fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 86 | RTC_LOG(LS_ERROR) << "Failed to create identity fingerprint, alg=" |
| 87 | << digest_alg; |
deadbeef | 8662f94 | 2017-01-20 21:20:51 -0800 | [diff] [blame] | 88 | } |
| 89 | return fingerprint; |
| 90 | } |
| 91 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 92 | SSLFingerprint::SSLFingerprint(const std::string& algorithm, |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 93 | ArrayView<const uint8_t> digest_view) |
| 94 | : algorithm(algorithm), digest(digest_view.data(), digest_view.size()) {} |
| 95 | |
| 96 | SSLFingerprint::SSLFingerprint(const std::string& algorithm, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 97 | const uint8_t* digest_in, |
| 98 | size_t digest_len) |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 99 | : SSLFingerprint(algorithm, MakeArrayView(digest_in, digest_len)) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | |
| 101 | SSLFingerprint::SSLFingerprint(const SSLFingerprint& from) |
| 102 | : algorithm(from.algorithm), digest(from.digest) {} |
| 103 | |
| 104 | bool SSLFingerprint::operator==(const SSLFingerprint& other) const { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 105 | return algorithm == other.algorithm && digest == other.digest; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | std::string SSLFingerprint::GetRfc4572Fingerprint() const { |
| 109 | std::string fingerprint = |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 110 | rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':'); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 111 | std::transform(fingerprint.begin(), fingerprint.end(), fingerprint.begin(), |
| 112 | ::toupper); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | return fingerprint; |
| 114 | } |
| 115 | |
mikescarlett | e774867 | 2016-04-29 20:20:54 -0700 | [diff] [blame] | 116 | std::string SSLFingerprint::ToString() const { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 117 | std::string fp_str = algorithm; |
| 118 | fp_str.append(" "); |
| 119 | fp_str.append(GetRfc4572Fingerprint()); |
| 120 | return fp_str; |
| 121 | } |
| 122 | |
| 123 | } // namespace rtc |