| OLD | NEW |
| (Empty) |
| 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/logging.h" | |
| 18 #include "webrtc/base/messagedigest.h" | |
| 19 #include "webrtc/base/stringencode.h" | |
| 20 | |
| 21 namespace rtc { | |
| 22 | |
| 23 SSLFingerprint* SSLFingerprint::Create( | |
| 24 const std::string& algorithm, const rtc::SSLIdentity* identity) { | |
| 25 if (!identity) { | |
| 26 return nullptr; | |
| 27 } | |
| 28 | |
| 29 return Create(algorithm, &(identity->certificate())); | |
| 30 } | |
| 31 | |
| 32 SSLFingerprint* SSLFingerprint::Create( | |
| 33 const std::string& algorithm, const rtc::SSLCertificate* cert) { | |
| 34 uint8_t digest_val[64]; | |
| 35 size_t digest_len; | |
| 36 bool ret = cert->ComputeDigest( | |
| 37 algorithm, digest_val, sizeof(digest_val), &digest_len); | |
| 38 if (!ret) { | |
| 39 return nullptr; | |
| 40 } | |
| 41 | |
| 42 return new SSLFingerprint(algorithm, digest_val, digest_len); | |
| 43 } | |
| 44 | |
| 45 SSLFingerprint* SSLFingerprint::CreateFromRfc4572( | |
| 46 const std::string& algorithm, const std::string& fingerprint) { | |
| 47 if (algorithm.empty() || !rtc::IsFips180DigestAlgorithm(algorithm)) | |
| 48 return nullptr; | |
| 49 | |
| 50 if (fingerprint.empty()) | |
| 51 return nullptr; | |
| 52 | |
| 53 size_t value_len; | |
| 54 char value[rtc::MessageDigest::kMaxSize]; | |
| 55 value_len = rtc::hex_decode_with_delimiter(value, sizeof(value), | |
| 56 fingerprint.c_str(), | |
| 57 fingerprint.length(), | |
| 58 ':'); | |
| 59 if (!value_len) | |
| 60 return nullptr; | |
| 61 | |
| 62 return new SSLFingerprint(algorithm, reinterpret_cast<uint8_t*>(value), | |
| 63 value_len); | |
| 64 } | |
| 65 | |
| 66 SSLFingerprint* SSLFingerprint::CreateFromCertificate( | |
| 67 const RTCCertificate* cert) { | |
| 68 std::string digest_alg; | |
| 69 if (!cert->ssl_certificate().GetSignatureDigestAlgorithm(&digest_alg)) { | |
| 70 LOG(LS_ERROR) << "Failed to retrieve the certificate's digest algorithm"; | |
| 71 return nullptr; | |
| 72 } | |
| 73 | |
| 74 SSLFingerprint* fingerprint = Create(digest_alg, cert->identity()); | |
| 75 if (!fingerprint) { | |
| 76 LOG(LS_ERROR) << "Failed to create identity fingerprint, alg=" | |
| 77 << digest_alg; | |
| 78 } | |
| 79 return fingerprint; | |
| 80 } | |
| 81 | |
| 82 SSLFingerprint::SSLFingerprint(const std::string& algorithm, | |
| 83 const uint8_t* digest_in, | |
| 84 size_t digest_len) | |
| 85 : algorithm(algorithm) { | |
| 86 digest.SetData(digest_in, digest_len); | |
| 87 } | |
| 88 | |
| 89 SSLFingerprint::SSLFingerprint(const SSLFingerprint& from) | |
| 90 : algorithm(from.algorithm), digest(from.digest) {} | |
| 91 | |
| 92 bool SSLFingerprint::operator==(const SSLFingerprint& other) const { | |
| 93 return algorithm == other.algorithm && | |
| 94 digest == other.digest; | |
| 95 } | |
| 96 | |
| 97 std::string SSLFingerprint::GetRfc4572Fingerprint() const { | |
| 98 std::string fingerprint = | |
| 99 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':'); | |
| 100 std::transform(fingerprint.begin(), fingerprint.end(), | |
| 101 fingerprint.begin(), ::toupper); | |
| 102 return fingerprint; | |
| 103 } | |
| 104 | |
| 105 std::string SSLFingerprint::ToString() const { | |
| 106 std::string fp_str = algorithm; | |
| 107 fp_str.append(" "); | |
| 108 fp_str.append(GetRfc4572Fingerprint()); | |
| 109 return fp_str; | |
| 110 } | |
| 111 | |
| 112 } // namespace rtc | |
| OLD | NEW |