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 | |
| 11 | #include "webrtc/base/sslfingerprint.h" |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <string> |
| 15 | |
| 16 | #include "webrtc/base/helpers.h" |
| 17 | #include "webrtc/base/messagedigest.h" |
| 18 | #include "webrtc/base/stringencode.h" |
| 19 | |
| 20 | namespace rtc { |
| 21 | |
| 22 | SSLFingerprint* SSLFingerprint::Create( |
| 23 | const std::string& algorithm, const rtc::SSLIdentity* identity) { |
| 24 | if (!identity) { |
| 25 | return NULL; |
| 26 | } |
| 27 | |
| 28 | return Create(algorithm, &(identity->certificate())); |
| 29 | } |
| 30 | |
| 31 | SSLFingerprint* SSLFingerprint::Create( |
| 32 | const std::string& algorithm, const rtc::SSLCertificate* cert) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 33 | uint8_t digest_val[64]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | size_t digest_len; |
| 35 | bool ret = cert->ComputeDigest( |
| 36 | algorithm, digest_val, sizeof(digest_val), &digest_len); |
| 37 | if (!ret) { |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | return new SSLFingerprint(algorithm, digest_val, digest_len); |
| 42 | } |
| 43 | |
| 44 | SSLFingerprint* SSLFingerprint::CreateFromRfc4572( |
| 45 | const std::string& algorithm, const std::string& fingerprint) { |
| 46 | if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm)) |
| 47 | return NULL; |
| 48 | |
| 49 | if (fingerprint.empty()) |
| 50 | return NULL; |
| 51 | |
| 52 | size_t value_len; |
| 53 | char value[rtc::MessageDigest::kMaxSize]; |
| 54 | value_len = rtc::hex_decode_with_delimiter(value, sizeof(value), |
| 55 | fingerprint.c_str(), |
| 56 | fingerprint.length(), |
| 57 | ':'); |
| 58 | if (!value_len) |
| 59 | return NULL; |
| 60 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 61 | return new SSLFingerprint(algorithm, reinterpret_cast<uint8_t*>(value), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 62 | value_len); |
| 63 | } |
| 64 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame^] | 65 | SSLFingerprint::SSLFingerprint(const std::string& algorithm, |
| 66 | const uint8_t* digest_in, |
| 67 | size_t digest_len) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | : algorithm(algorithm) { |
| 69 | digest.SetData(digest_in, digest_len); |
| 70 | } |
| 71 | |
| 72 | SSLFingerprint::SSLFingerprint(const SSLFingerprint& from) |
| 73 | : algorithm(from.algorithm), digest(from.digest) {} |
| 74 | |
| 75 | bool SSLFingerprint::operator==(const SSLFingerprint& other) const { |
| 76 | return algorithm == other.algorithm && |
| 77 | digest == other.digest; |
| 78 | } |
| 79 | |
| 80 | std::string SSLFingerprint::GetRfc4572Fingerprint() const { |
| 81 | std::string fingerprint = |
Karl Wiberg | 9478437 | 2015-04-20 14:03:07 +0200 | [diff] [blame] | 82 | rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':'); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | std::transform(fingerprint.begin(), fingerprint.end(), |
| 84 | fingerprint.begin(), ::toupper); |
| 85 | return fingerprint; |
| 86 | } |
| 87 | |
| 88 | std::string SSLFingerprint::ToString() { |
| 89 | std::string fp_str = algorithm; |
| 90 | fp_str.append(" "); |
| 91 | fp_str.append(GetRfc4572Fingerprint()); |
| 92 | return fp_str; |
| 93 | } |
| 94 | |
| 95 | } // namespace rtc |