Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Jun Cai | 9409ded | 2018-01-30 00:19:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/ssl_private_key_impl.h" |
| 6 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
| 8 | #include "base/functional/callback.h" |
| 9 | #include "base/functional/callback_helpers.h" |
David Benjamin | 168681a | 2025-05-14 21:13:32 | [diff] [blame] | 10 | #include "mojo/public/cpp/bindings/callback_helpers.h" |
| 11 | #include "net/base/net_errors.h" |
Jun Cai | 9409ded | 2018-01-30 00:19:46 | [diff] [blame] | 12 | |
| 13 | namespace content { |
| 14 | |
| 15 | SSLPrivateKeyImpl::SSLPrivateKeyImpl( |
| 16 | scoped_refptr<net::SSLPrivateKey> ssl_private_key) |
| 17 | : ssl_private_key_(std::move(ssl_private_key)) {} |
| 18 | |
| 19 | SSLPrivateKeyImpl::~SSLPrivateKeyImpl() = default; |
| 20 | |
| 21 | void SSLPrivateKeyImpl::Sign( |
| 22 | uint16_t algorithm, |
| 23 | const std::vector<uint8_t>& input, |
| 24 | network::mojom::SSLPrivateKey::SignCallback callback) { |
David Benjamin | 168681a | 2025-05-14 21:13:32 | [diff] [blame] | 25 | // In some cases, `ssl_private_key_` may abandon the callback. See |
| 26 | // https://crbug.com/40651689. |
| 27 | callback = mojo::WrapCallbackWithDefaultInvokeIfNotRun( |
| 28 | std::move(callback), int32_t{net::ERR_FAILED}, std::vector<uint8_t>()); |
Jun Cai | 9409ded | 2018-01-30 00:19:46 | [diff] [blame] | 29 | ssl_private_key_->Sign( |
David Benjamin | 168681a | 2025-05-14 21:13:32 | [diff] [blame] | 30 | algorithm, input, |
| 31 | base::BindOnce(&SSLPrivateKeyImpl::Callback, std::move(callback))); |
Jun Cai | 9409ded | 2018-01-30 00:19:46 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void SSLPrivateKeyImpl::Callback( |
| 35 | network::mojom::SSLPrivateKey::SignCallback callback, |
| 36 | net::Error net_error, |
| 37 | const std::vector<uint8_t>& signature) { |
| 38 | std::move(callback).Run(static_cast<int32_t>(net_error), signature); |
| 39 | } |
| 40 | |
| 41 | } // namespace content |