Files
platform-external-webrtc/webrtc/api/dtlsidentitystore.h
Henrik Boström 6c96314b42 RTCCertificateGeneratorInterface and RTCCertificateGeneratorStoreWrapper added.
This CL adds these classes but does not change any functonality or interface
yet. This is in preparation for future CLs. To be used for this:
https://codereview.webrtc.org/2000163002/

RTCCertificateGenerator is meant to replace DtlsIdentityStoreInterface and
implementations. In order to continue to support mocking and to help with the
transition, RTCCertificateGenerator gets an interface that it implements (just
like the store has both interface and impl).

PeerConnectionFactoryInterface::CreatePeerConnection will take an
RTCCertificateGeneratorInterface instead of DtlsIdentityStoreInterface. As to
not break Chromium, both versions of CreatePeerConnection need to exist for a
transition period. This will be done by wrapping a store into a generator
wrapper - RTCCertificateGeneratorStoreWrapper.

BUG=webrtc:5707, webrtc:5708
R=hta@webrtc.org, tommi@chromium.org, tommi@webrtc.org

Review URL: https://codereview.webrtc.org/2001103002 .

Cr-Commit-Position: refs/heads/master@{#12879}
2016-05-24 18:46:42 +00:00

159 lines
5.6 KiB
C++

/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_API_DTLSIDENTITYSTORE_H_
#define WEBRTC_API_DTLSIDENTITYSTORE_H_
#include <memory>
#include <queue>
#include <string>
#include <utility>
#include "webrtc/base/messagehandler.h"
#include "webrtc/base/messagequeue.h"
#include "webrtc/base/optional.h"
#include "webrtc/base/refcount.h"
#include "webrtc/base/rtccertificategenerator.h"
#include "webrtc/base/scoped_ref_ptr.h"
#include "webrtc/base/sslidentity.h"
#include "webrtc/base/thread.h"
namespace webrtc {
// Passed to SSLIdentity::Generate.
extern const char kIdentityName[];
class SSLIdentity;
class Thread;
// Used to receive callbacks of DTLS identity requests.
class DtlsIdentityRequestObserver : public rtc::RefCountInterface {
public:
virtual void OnFailure(int error) = 0;
// TODO(hbos): Unify the OnSuccess method once Chrome code is updated.
virtual void OnSuccess(const std::string& der_cert,
const std::string& der_private_key) = 0;
// |identity| is a unique_ptr because rtc::SSLIdentity is not copyable and the
// client has to get the ownership of the object to make use of it.
virtual void OnSuccess(std::unique_ptr<rtc::SSLIdentity> identity) = 0;
protected:
virtual ~DtlsIdentityRequestObserver() {}
};
// This interface defines an in-memory DTLS identity store, which generates DTLS
// identities.
// APIs calls must be made on the signaling thread and the callbacks are also
// called on the signaling thread.
class DtlsIdentityStoreInterface {
public:
virtual ~DtlsIdentityStoreInterface() { }
// The |observer| will be called when the requested identity is ready, or when
// identity generation fails.
virtual void RequestIdentity(
const rtc::KeyParams& key_params,
const rtc::Optional<uint64_t>& expires_ms,
const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) = 0;
};
// The WebRTC default implementation of DtlsIdentityStoreInterface.
// Identity generation is performed on the worker thread.
class DtlsIdentityStoreImpl : public DtlsIdentityStoreInterface,
public rtc::MessageHandler {
public:
// This will start to preemptively generating an RSA identity in the
// background if the worker thread is not the same as the signaling thread.
DtlsIdentityStoreImpl(rtc::Thread* signaling_thread,
rtc::Thread* worker_thread);
~DtlsIdentityStoreImpl() override;
// DtlsIdentityStoreInterface override;
void RequestIdentity(
const rtc::KeyParams& key_params,
const rtc::Optional<uint64_t>& expires_ms,
const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) override;
// rtc::MessageHandler override;
void OnMessage(rtc::Message* msg) override;
// Returns true if there is a free RSA identity, used for unit tests.
bool HasFreeIdentityForTesting(rtc::KeyType key_type) const;
private:
void GenerateIdentity(
rtc::KeyType key_type,
const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer);
void OnIdentityGenerated(rtc::KeyType key_type,
std::unique_ptr<rtc::SSLIdentity> identity);
class WorkerTask;
typedef rtc::ScopedMessageData<DtlsIdentityStoreImpl::WorkerTask>
WorkerTaskMessageData;
// A key type-identity pair.
struct IdentityResult {
IdentityResult(rtc::KeyType key_type,
std::unique_ptr<rtc::SSLIdentity> identity)
: key_type_(key_type), identity_(std::move(identity)) {}
rtc::KeyType key_type_;
std::unique_ptr<rtc::SSLIdentity> identity_;
};
typedef rtc::ScopedMessageData<IdentityResult> IdentityResultMessageData;
sigslot::signal0<> SignalDestroyed;
rtc::Thread* const signaling_thread_;
// TODO(hbos): RSA generation is slow and would be VERY slow if we switch over
// to 2048, DtlsIdentityStore should use a new thread and not the "general
// purpose" worker thread.
rtc::Thread* const worker_thread_;
struct RequestInfo {
RequestInfo()
: request_observers_(), gen_in_progress_counts_(0), free_identity_() {}
std::queue<rtc::scoped_refptr<DtlsIdentityRequestObserver>>
request_observers_;
size_t gen_in_progress_counts_;
std::unique_ptr<rtc::SSLIdentity> free_identity_;
};
// One RequestInfo per KeyType. Only touch on the |signaling_thread_|.
RequestInfo request_info_[rtc::KT_LAST];
};
// Implements the |RTCCertificateGeneratorInterface| using the old |SSLIdentity|
// generator API, |DtlsIdentityStoreInterface|. This will be used while
// transitioning from store to generator, see bugs.webrtc.org/5707,
// bugs.webrtc.org/5708. Once those bugs have been fixed, this will be removed.
class RTCCertificateGeneratorStoreWrapper
: public rtc::RTCCertificateGeneratorInterface {
public:
RTCCertificateGeneratorStoreWrapper(
std::unique_ptr<DtlsIdentityStoreInterface> store);
// |RTCCertificateGeneratorInterface| overrides.
void GenerateCertificateAsync(
const rtc::KeyParams& key_params,
const rtc::Optional<uint64_t>& expires_ms,
const rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback>& callback)
override;
private:
const std::unique_ptr<DtlsIdentityStoreInterface> store_;
};
} // namespace webrtc
#endif // WEBRTC_API_DTLSIDENTITYSTORE_H_