3 * Copyright 2012, Google Inc.
4 * Copyright 2012, RTFM Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "talk/base/sslfingerprint.h"
34 #include "talk/base/helpers.h"
35 #include "talk/base/messagedigest.h"
36 #include "talk/base/stringencode.h"
40 SSLFingerprint* SSLFingerprint::Create(
41 const std::string& algorithm, const talk_base::SSLIdentity* identity) {
46 return Create(algorithm, &(identity->certificate()));
49 SSLFingerprint* SSLFingerprint::Create(
50 const std::string& algorithm, const talk_base::SSLCertificate* cert) {
53 bool ret = cert->ComputeDigest(
54 algorithm, digest_val, sizeof(digest_val), &digest_len);
59 return new SSLFingerprint(algorithm, digest_val, digest_len);
62 SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
63 const std::string& algorithm, const std::string& fingerprint) {
64 if (algorithm.empty() || !talk_base::IsFips180DigestAlgorithm(algorithm))
67 if (fingerprint.empty())
71 char value[talk_base::MessageDigest::kMaxSize];
72 value_len = talk_base::hex_decode_with_delimiter(value, sizeof(value),
79 return new SSLFingerprint(algorithm,
80 reinterpret_cast<uint8*>(value),
84 SSLFingerprint::SSLFingerprint(
85 const std::string& algorithm, const uint8* digest_in, size_t digest_len)
86 : algorithm(algorithm) {
87 digest.SetData(digest_in, digest_len);
90 SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
91 : algorithm(from.algorithm), digest(from.digest) {}
93 bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
94 return algorithm == other.algorithm &&
95 digest == other.digest;
98 std::string SSLFingerprint::GetRfc4572Fingerprint() const {
99 std::string fingerprint =
100 talk_base::hex_encode_with_delimiter(
101 digest.data(), digest.length(), ':');
102 std::transform(fingerprint.begin(), fingerprint.end(),
103 fingerprint.begin(), ::toupper);
107 std::string SSLFingerprint::ToString() {
108 std::string fp_str = algorithm;
110 fp_str.append(GetRfc4572Fingerprint());
114 } // namespace talk_base