blob: 1f2e531b5b93874c59a71e6f5950960ec1127e89 [file] [log] [blame]
Yifan Hong13c90062021-09-09 14:59:53 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Yifan Hong1deca4b2021-09-10 16:16:44 -070019#include <mutex>
20#include <string_view>
21#include <vector>
22
23#include <openssl/ssl.h>
24
Yifan Hong9734cfc2021-09-13 16:14:09 -070025#include <binder/RpcCertificateFormat.h>
Yifan Hong13c90062021-09-09 14:59:53 -070026#include <binder/RpcCertificateVerifier.h>
27
28namespace android {
29
30// A simple certificate verifier for testing.
Yifan Hong1deca4b2021-09-10 16:16:44 -070031// Keep a list of leaf certificates as trusted. No certificate chain support.
32//
33// All APIs are thread-safe. However, if verify() and addTrustedPeerCertificate() are called
34// simultaneously in different threads, it is not deterministic whether verify() will use the
35// certificate being added.
Yifan Hong13c90062021-09-09 14:59:53 -070036class RpcCertificateVerifierSimple : public RpcCertificateVerifier {
37public:
38 status_t verify(const X509*, uint8_t*) override;
Yifan Hong1deca4b2021-09-10 16:16:44 -070039
40 // Add a trusted peer certificate. Peers presenting this certificate are accepted.
41 //
42 // Caller must ensure that RpcTransportCtx::newTransport() are called after all trusted peer
43 // certificates are added. Otherwise, RpcTransport-s created before may not trust peer
44 // certificates added later.
Yifan Hong9734cfc2021-09-13 16:14:09 -070045 [[nodiscard]] status_t addTrustedPeerCertificate(RpcCertificateFormat format,
Yifan Hong1deca4b2021-09-10 16:16:44 -070046 const std::vector<uint8_t>& cert);
47
48private:
49 std::mutex mMutex; // for below
50 std::vector<bssl::UniquePtr<X509>> mTrustedPeerCertificates;
Yifan Hong13c90062021-09-09 14:59:53 -070051};
52
53} // namespace android