2 * Copyright (C) 2018 Apple Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "LibWebRTCCertificateGenerator.h"
30 #include "JSRTCCertificate.h"
31 #include "LibWebRTCMacros.h"
33 ALLOW_UNUSED_PARAMETERS_BEGIN
35 #include <webrtc/rtc_base/rtccertificategenerator.h>
37 ALLOW_UNUSED_PARAMETERS_END
41 namespace LibWebRTCCertificateGenerator {
43 static inline String fromStdString(const std::string& value)
45 return String::fromUTF8(value.data(), value.length());
48 class RTCCertificateGeneratorCallback : public ThreadSafeRefCounted<RTCCertificateGeneratorCallback, WTF::DestructionThread::Main>, public rtc::RTCCertificateGeneratorCallback {
50 explicit RTCCertificateGeneratorCallback(DOMPromiseDeferred<IDLInterface<RTCCertificate>>&& promise)
51 : m_promise(WTFMove(promise))
55 void AddRef() const final { ref(); }
56 rtc::RefCountReleaseStatus Release() const final
58 auto result = refCount() - 1;
60 return result ? rtc::RefCountReleaseStatus::kOtherRefsRemained : rtc::RefCountReleaseStatus::kDroppedLastRef;
64 void OnSuccess(const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) final
66 callOnMainThread([promise = WTFMove(m_promise.value()), certificate]() mutable {
67 Vector<RTCCertificate::DtlsFingerprint> fingerprints;
68 auto stats = certificate->ssl_certificate().GetStats();
69 auto* info = stats.get();
71 StringView fingerprint { reinterpret_cast<const unsigned char*>(info->fingerprint.data()), static_cast<unsigned>(info->fingerprint.length()) };
72 fingerprints.append({ fromStdString(info->fingerprint_algorithm), fingerprint.convertToASCIILowercase() });
73 info = info->issuer.get();
76 auto pem = certificate->ToPEM();
77 promise.resolve(RTCCertificate::create(certificate->Expires(), WTFMove(fingerprints), fromStdString(pem.certificate()), fromStdString(pem.private_key())));
81 void OnFailure() final
83 callOnMainThread([promise = WTFMove(m_promise.value())]() mutable {
84 promise.reject(Exception { TypeError, "Unable to create a certificate"_s});
88 std::optional<DOMPromiseDeferred<IDLInterface<RTCCertificate>>> m_promise;
91 static inline rtc::KeyParams keyParamsFromCertificateType(const PeerConnectionBackend::CertificateInformation& info)
94 case PeerConnectionBackend::CertificateInformation::Type::ECDSAP256:
95 return rtc::KeyParams::ECDSA();
96 case PeerConnectionBackend::CertificateInformation::Type::RSASSAPKCS1v15:
97 if (info.rsaParameters)
98 return rtc::KeyParams::RSA(info.rsaParameters->modulusLength, info.rsaParameters->publicExponent);
99 return rtc::KeyParams::RSA(2048, 65537);
102 RELEASE_ASSERT_NOT_REACHED();
105 void generateCertificate(LibWebRTCProvider& provider, const PeerConnectionBackend::CertificateInformation& info, DOMPromiseDeferred<IDLInterface<RTCCertificate>>&& promise)
107 rtc::scoped_refptr<RTCCertificateGeneratorCallback> callback(new RTCCertificateGeneratorCallback(WTFMove(promise)));
109 absl::optional<uint64_t> expiresMs;
111 expiresMs = static_cast<uint64_t>(*info.expires);
113 provider.certificateGenerator().GenerateCertificateAsync(keyParamsFromCertificateType(info), expiresMs, WTFMove(callback));
116 } // namespace LibWebRTCCertificateGenerator
118 } // namespace WebCore
120 #endif // USE(LIBWEBRTC)