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
Create(const std::string & algorithm,const rtc::SSLIdentity * identity)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
Create(const std::string & algorithm,const rtc::SSLCertificate * cert)31 SSLFingerprint* SSLFingerprint::Create(
32 const std::string& algorithm, const rtc::SSLCertificate* cert) {
33 uint8 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
CreateFromRfc4572(const std::string & algorithm,const std::string & fingerprint)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,
62 reinterpret_cast<uint8*>(value),
63 value_len);
64 }
65
SSLFingerprint(const std::string & algorithm,const uint8 * digest_in,size_t digest_len)66 SSLFingerprint::SSLFingerprint(
67 const std::string& algorithm, const uint8* digest_in, size_t digest_len)
68 : algorithm(algorithm) {
69 digest.SetData(digest_in, digest_len);
70 }
71
SSLFingerprint(const SSLFingerprint & from)72 SSLFingerprint::SSLFingerprint(const SSLFingerprint& from)
73 : algorithm(from.algorithm), digest(from.digest) {}
74
operator ==(const SSLFingerprint & other) const75 bool SSLFingerprint::operator==(const SSLFingerprint& other) const {
76 return algorithm == other.algorithm &&
77 digest == other.digest;
78 }
79
GetRfc4572Fingerprint() const80 std::string SSLFingerprint::GetRfc4572Fingerprint() const {
81 std::string fingerprint =
82 rtc::hex_encode_with_delimiter(
83 digest.data(), digest.length(), ':');
84 std::transform(fingerprint.begin(), fingerprint.end(),
85 fingerprint.begin(), ::toupper);
86 return fingerprint;
87 }
88
ToString()89 std::string SSLFingerprint::ToString() {
90 std::string fp_str = algorithm;
91 fp_str.append(" ");
92 fp_str.append(GetRfc4572Fingerprint());
93 return fp_str;
94 }
95
96 } // namespace rtc
97