1/*
2 * Copyright (C) 2018 Apple Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "config.h"
26#include "LibWebRTCCertificateGenerator.h"
27
28#if USE(LIBWEBRTC)
29
30#include "JSRTCCertificate.h"
31#include "LibWebRTCMacros.h"
32
33ALLOW_UNUSED_PARAMETERS_BEGIN
34
35#include <webrtc/rtc_base/rtccertificategenerator.h>
36
37ALLOW_UNUSED_PARAMETERS_END
38
39namespace WebCore {
40
41namespace LibWebRTCCertificateGenerator {
42
43static inline String fromStdString(const std::string& value)
44{
45 return String::fromUTF8(value.data(), value.length());
46}
47
48class RTCCertificateGeneratorCallback : public ThreadSafeRefCounted<RTCCertificateGeneratorCallback, WTF::DestructionThread::Main>, public rtc::RTCCertificateGeneratorCallback {
49public:
50 RTCCertificateGeneratorCallback(Ref<SecurityOrigin>&& origin, DOMPromiseDeferred<IDLInterface<RTCCertificate>>&& promise)
51 : m_origin(WTFMove(origin))
52 , m_promise(WTFMove(promise))
53 {
54 }
55
56 void AddRef() const { ref(); }
57 rtc::RefCountReleaseStatus Release() const
58 {
59 auto result = refCount() - 1;
60 deref();
61 return result ? rtc::RefCountReleaseStatus::kOtherRefsRemained : rtc::RefCountReleaseStatus::kDroppedLastRef;
62 }
63
64private:
65 void OnSuccess(const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) final
66 {
67 callOnMainThread([origin = m_origin.releaseNonNull(), promise = WTFMove(m_promise.value()), certificate]() mutable {
68 Vector<RTCCertificate::DtlsFingerprint> fingerprints;
69 auto stats = certificate->ssl_certificate().GetStats();
70 auto* info = stats.get();
71 while (info) {
72 StringView fingerprint { reinterpret_cast<const unsigned char*>(info->fingerprint.data()), static_cast<unsigned>(info->fingerprint.length()) };
73 fingerprints.append({ fromStdString(info->fingerprint_algorithm), fingerprint.convertToASCIILowercase() });
74 info = info->issuer.get();
75 };
76
77 auto pem = certificate->ToPEM();
78 promise.resolve(RTCCertificate::create(WTFMove(origin), certificate->Expires(), WTFMove(fingerprints), fromStdString(pem.certificate()), fromStdString(pem.private_key())));
79 });
80 }
81
82 void OnFailure() final
83 {
84 callOnMainThread([promise = WTFMove(m_promise.value())]() mutable {
85 promise.reject(Exception { TypeError, "Unable to create a certificate"_s});
86 });
87 }
88
89 RefPtr<SecurityOrigin> m_origin;
90 Optional<DOMPromiseDeferred<IDLInterface<RTCCertificate>>> m_promise;
91};
92
93static inline rtc::KeyParams keyParamsFromCertificateType(const PeerConnectionBackend::CertificateInformation& info)
94{
95 switch (info.type) {
96 case PeerConnectionBackend::CertificateInformation::Type::ECDSAP256:
97 return rtc::KeyParams::ECDSA();
98 case PeerConnectionBackend::CertificateInformation::Type::RSASSAPKCS1v15:
99 if (info.rsaParameters)
100 return rtc::KeyParams::RSA(info.rsaParameters->modulusLength, info.rsaParameters->publicExponent);
101 return rtc::KeyParams::RSA(2048, 65537);
102 }
103
104 RELEASE_ASSERT_NOT_REACHED();
105}
106
107void generateCertificate(Ref<SecurityOrigin>&& origin, LibWebRTCProvider& provider, const PeerConnectionBackend::CertificateInformation& info, DOMPromiseDeferred<IDLInterface<RTCCertificate>>&& promise)
108{
109 rtc::scoped_refptr<RTCCertificateGeneratorCallback> callback(new rtc::RefCountedObject<RTCCertificateGeneratorCallback>(WTFMove(origin), WTFMove(promise)));
110
111 absl::optional<uint64_t> expiresMs;
112 if (info.expires)
113 expiresMs = static_cast<uint64_t>(*info.expires);
114
115 provider.certificateGenerator().GenerateCertificateAsync(keyParamsFromCertificateType(info), expiresMs, WTFMove(callback));
116}
117
118} // namespace LibWebRTCCertificateGenerator
119
120} // namespace WebCore
121
122#endif // USE(LIBWEBRTC)
123