blob: dfd5551abcdbfd600512ed6f7c1a0354f306a33d [file] [log] [blame]
sergeyu@chromium.orgf32dd312014-01-15 23:15:54 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 * Copyright 2012, RTFM Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
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.
16 *
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.
27 */
28
29#include "talk/base/sslfingerprint.h"
30
31#include <ctype.h>
32#include <string>
33
34#include "talk/base/helpers.h"
35#include "talk/base/messagedigest.h"
36#include "talk/base/stringencode.h"
37
38namespace talk_base {
39
40SSLFingerprint* SSLFingerprint::Create(
41 const std::string& algorithm, const talk_base::SSLIdentity* identity) {
42 if (!identity) {
43 return NULL;
44 }
45
46 return Create(algorithm, &(identity->certificate()));
47}
48
49SSLFingerprint* SSLFingerprint::Create(
50 const std::string& algorithm, const talk_base::SSLCertificate* cert) {
51 uint8 digest_val[64];
52 size_t digest_len;
53 bool ret = cert->ComputeDigest(
54 algorithm, digest_val, sizeof(digest_val), &digest_len);
55 if (!ret) {
56 return NULL;
57 }
58
59 return new SSLFingerprint(algorithm, digest_val, digest_len);
60}
61
62SSLFingerprint* SSLFingerprint::CreateFromRfc4572(
63 const std::string& algorithm, const std::string& fingerprint) {
64 if (algorithm.empty() || !talk_base::IsFips180DigestAlgorithm(algorithm))
65 return NULL;
66
67 if (fingerprint.empty())
68 return NULL;
69
70 size_t value_len;
71 char value[talk_base::MessageDigest::kMaxSize];
72 value_len = talk_base::hex_decode_with_delimiter(value, sizeof(value),
73 fingerprint.c_str(),
74 fingerprint.length(),
75 ':');
76 if (!value_len)
77 return NULL;
78
79 return new SSLFingerprint(algorithm,
80 reinterpret_cast<uint8*>(value),
81 value_len);
82}
83
84SSLFingerprint::SSLFingerprint(
85 const std::string& algorithm, const uint8* digest_in, size_t digest_len)
86 : algorithm(algorithm) {
87 digest.SetData(digest_in, digest_len);
88}
89
90SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
91 : algorithm(from.algorithm), digest(from.digest) {}
92
93bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
94 return algorithm == other.algorithm &&
95 digest == other.digest;
96}
97
98std::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);
104 return fingerprint;
105}
106
107std::string SSLFingerprint::ToString() {
108 std::string fp_str = algorithm;
109 fp_str.append(" ");
110 fp_str.append(GetRfc4572Fingerprint());
111 return fp_str;
112}
113
114} // namespace talk_base