blob: 1e78b9c3c3f3b8fa635816b78dccb99d1c4081a2 [file] [log] [blame]
jiayl@webrtc.org25484062015-02-18 23:58:16 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
jiayl@webrtc.org25484062015-02-18 23:58:16 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
jiayl@webrtc.org25484062015-02-18 23:58:16 +00009 */
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000010
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#ifndef WEBRTC_API_DTLSIDENTITYSTORE_H_
12#define WEBRTC_API_DTLSIDENTITYSTORE_H_
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000013
14#include <queue>
15#include <string>
kwiberg0eb15ed2015-12-17 03:04:15 -080016#include <utility>
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000017
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000018#include "webrtc/base/messagehandler.h"
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +000019#include "webrtc/base/messagequeue.h"
Henrik Boström5b4ce332015-08-05 16:55:22 +020020#include "webrtc/base/refcount.h"
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000021#include "webrtc/base/scoped_ptr.h"
22#include "webrtc/base/scoped_ref_ptr.h"
Henrik Boström5b4ce332015-08-05 16:55:22 +020023#include "webrtc/base/sslidentity.h"
24#include "webrtc/base/thread.h"
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000025
26namespace webrtc {
glaznev97579a42015-09-01 11:31:27 -070027
28// Passed to SSLIdentity::Generate.
29extern const char kIdentityName[];
30
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000031class SSLIdentity;
32class Thread;
33
Henrik Boström5b4ce332015-08-05 16:55:22 +020034// Used to receive callbacks of DTLS identity requests.
Henrik Boström5b4ce332015-08-05 16:55:22 +020035class DtlsIdentityRequestObserver : public rtc::RefCountInterface {
36 public:
37 virtual void OnFailure(int error) = 0;
38 // TODO(hbos): Unify the OnSuccess method once Chrome code is updated.
39 virtual void OnSuccess(const std::string& der_cert,
40 const std::string& der_private_key) = 0;
41 // |identity| is a scoped_ptr because rtc::SSLIdentity is not copyable and the
42 // client has to get the ownership of the object to make use of it.
43 virtual void OnSuccess(rtc::scoped_ptr<rtc::SSLIdentity> identity) = 0;
44
45 protected:
46 virtual ~DtlsIdentityRequestObserver() {}
47};
48
Henrik Boström5b4ce332015-08-05 16:55:22 +020049// This interface defines an in-memory DTLS identity store, which generates DTLS
50// identities.
51// APIs calls must be made on the signaling thread and the callbacks are also
52// called on the signaling thread.
53class DtlsIdentityStoreInterface {
54 public:
55 virtual ~DtlsIdentityStoreInterface() { }
56
Henrik Boström5e56c592015-08-11 10:33:13 +020057 // The |observer| will be called when the requested identity is ready, or when
58 // identity generation fails.
hbos3b7c7932015-10-21 01:44:21 -070059 // TODO(torbjorng,hbos): The following RequestIdentity is about to be removed,
60 // see below todo.
Henrik Boström5b4ce332015-08-05 16:55:22 +020061 virtual void RequestIdentity(
62 rtc::KeyType key_type,
hbos3b7c7932015-10-21 01:44:21 -070063 const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) {
64 // Add default parameterization.
65 RequestIdentity(rtc::KeyParams(key_type), observer);
66 }
67 // TODO(torbjorng,hbos): Parameterized key types! The following
68 // RequestIdentity should replace the old one that takes rtc::KeyType. When
69 // the new one is implemented by Chromium and WebRTC the old one should be
70 // removed. crbug.com/544902, webrtc:5092.
71 virtual void RequestIdentity(
72 rtc::KeyParams key_params,
73 const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) {
74 // Drop parameterization.
75 RequestIdentity(key_params.type(), observer);
76 }
Henrik Boström5b4ce332015-08-05 16:55:22 +020077};
78
Henrik Boström5e56c592015-08-11 10:33:13 +020079// The WebRTC default implementation of DtlsIdentityStoreInterface.
80// Identity generation is performed on the worker thread.
81class DtlsIdentityStoreImpl : public DtlsIdentityStoreInterface,
82 public rtc::MessageHandler {
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000083 public:
Henrik Boström5e56c592015-08-11 10:33:13 +020084 // This will start to preemptively generating an RSA identity in the
85 // background if the worker thread is not the same as the signaling thread.
86 DtlsIdentityStoreImpl(rtc::Thread* signaling_thread,
87 rtc::Thread* worker_thread);
88 ~DtlsIdentityStoreImpl() override;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000089
Henrik Boström5e56c592015-08-11 10:33:13 +020090 // DtlsIdentityStoreInterface override;
91 void RequestIdentity(
92 rtc::KeyType key_type,
93 const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) override;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +000094
95 // rtc::MessageHandler override;
96 void OnMessage(rtc::Message* msg) override;
97
Henrik Boström5e56c592015-08-11 10:33:13 +020098 // Returns true if there is a free RSA identity, used for unit tests.
99 bool HasFreeIdentityForTesting(rtc::KeyType key_type) const;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000100
101 private:
Henrik Boström5e56c592015-08-11 10:33:13 +0200102 void GenerateIdentity(
103 rtc::KeyType key_type,
104 const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer);
105 void OnIdentityGenerated(rtc::KeyType key_type,
106 rtc::scoped_ptr<rtc::SSLIdentity> identity);
107
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000108 class WorkerTask;
Henrik Boström5e56c592015-08-11 10:33:13 +0200109 typedef rtc::ScopedMessageData<DtlsIdentityStoreImpl::WorkerTask>
110 WorkerTaskMessageData;
jiayl@webrtc.orgd83f4ef2015-03-13 21:26:12 +0000111
Henrik Boström5e56c592015-08-11 10:33:13 +0200112 // A key type-identity pair.
113 struct IdentityResult {
114 IdentityResult(rtc::KeyType key_type,
115 rtc::scoped_ptr<rtc::SSLIdentity> identity)
kwiberg0eb15ed2015-12-17 03:04:15 -0800116 : key_type_(key_type), identity_(std::move(identity)) {}
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000117
Henrik Boström5e56c592015-08-11 10:33:13 +0200118 rtc::KeyType key_type_;
119 rtc::scoped_ptr<rtc::SSLIdentity> identity_;
120 };
121
122 typedef rtc::ScopedMessageData<IdentityResult> IdentityResultMessageData;
123
124 sigslot::signal0<> SignalDestroyed;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000125
Tommi532caea2015-06-09 17:33:06 +0200126 rtc::Thread* const signaling_thread_;
Henrik Boström5e56c592015-08-11 10:33:13 +0200127 // TODO(hbos): RSA generation is slow and would be VERY slow if we switch over
128 // to 2048, DtlsIdentityStore should use a new thread and not the "general
129 // purpose" worker thread.
Tommi532caea2015-06-09 17:33:06 +0200130 rtc::Thread* const worker_thread_;
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000131
Henrik Boström5e56c592015-08-11 10:33:13 +0200132 struct RequestInfo {
133 RequestInfo()
134 : request_observers_(), gen_in_progress_counts_(0), free_identity_() {}
135
136 std::queue<rtc::scoped_refptr<DtlsIdentityRequestObserver>>
137 request_observers_;
138 size_t gen_in_progress_counts_;
139 rtc::scoped_ptr<rtc::SSLIdentity> free_identity_;
140 };
141
142 // One RequestInfo per KeyType. Only touch on the |signaling_thread_|.
143 RequestInfo request_info_[rtc::KT_LAST];
jiayl@webrtc.org61e00b02015-03-04 22:17:38 +0000144};
145
146} // namespace webrtc
147
Henrik Kjellander15583c12016-02-10 10:53:12 +0100148#endif // WEBRTC_API_DTLSIDENTITYSTORE_H_