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) { 33 uint8_t digest_val[64]; 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 61 return new SSLFingerprint(algorithm, reinterpret_cast<uint8_t*>(value), 62 value_len); 63 } 64 65 SSLFingerprint::SSLFingerprint(const std::string& algorithm, 66 const uint8_t* digest_in, 67 size_t digest_len) 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 = 82 rtc::hex_encode_with_delimiter(digest.data<char>(), digest.size(), ':'); 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 96