blob: 4d41156f862e49bc4f21df31e5199391789a8145 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +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#ifndef TALK_BASE_SSLFINGERPRINT_H_
30#define TALK_BASE_SSLFINGERPRINT_H_
31
32#include <ctype.h>
33#include <string>
34
35#include "talk/base/buffer.h"
36#include "talk/base/helpers.h"
37#include "talk/base/messagedigest.h"
38#include "talk/base/sslidentity.h"
39#include "talk/base/stringencode.h"
40
41namespace talk_base {
42
43struct SSLFingerprint {
44 static SSLFingerprint* Create(const std::string& algorithm,
45 const talk_base::SSLIdentity* identity) {
46 if (!identity) {
47 return NULL;
48 }
49
50 uint8 digest_val[64];
51 size_t digest_len;
52 bool ret = identity->certificate().ComputeDigest(
53 algorithm, digest_val, sizeof(digest_val), &digest_len);
54 if (!ret) {
55 return NULL;
56 }
57
58 return new SSLFingerprint(algorithm, digest_val, digest_len);
59 }
60
61 static SSLFingerprint* CreateFromRfc4572(const std::string& algorithm,
62 const std::string& fingerprint) {
63 if (algorithm.empty())
64 return NULL;
65
66 if (fingerprint.empty())
67 return NULL;
68
69 size_t value_len;
70 char value[talk_base::MessageDigest::kMaxSize];
71 value_len = talk_base::hex_decode_with_delimiter(value, sizeof(value),
72 fingerprint.c_str(),
73 fingerprint.length(),
74 ':');
75 if (!value_len)
76 return NULL;
77
78 return new SSLFingerprint(algorithm,
79 reinterpret_cast<uint8*>(value),
80 value_len);
81 }
82
83 SSLFingerprint(const std::string& algorithm, const uint8* digest_in,
84 size_t digest_len) : algorithm(algorithm) {
85 digest.SetData(digest_in, digest_len);
86 }
87 SSLFingerprint(const SSLFingerprint& from)
88 : algorithm(from.algorithm), digest(from.digest) {}
89 bool operator==(const SSLFingerprint& other) const {
90 return algorithm == other.algorithm &&
91 digest == other.digest;
92 }
93
94 std::string GetRfc4572Fingerprint() const {
95 std::string fingerprint =
96 talk_base::hex_encode_with_delimiter(
97 digest.data(), digest.length(), ':');
98 std::transform(fingerprint.begin(), fingerprint.end(),
99 fingerprint.begin(), ::toupper);
100 return fingerprint;
101 }
102
103 std::string algorithm;
104 talk_base::Buffer digest;
105};
106
107} // namespace talk_base
108
109#endif // TALK_BASE_SSLFINGERPRINT_H_