Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 19 | #include <mutex> |
| 20 | #include <string_view> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <openssl/ssl.h> |
| 24 | |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 25 | #include <binder/RpcCertificateFormat.h> |
Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 26 | #include <binder/RpcCertificateVerifier.h> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | // A simple certificate verifier for testing. |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 31 | // 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 Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 36 | class RpcCertificateVerifierSimple : public RpcCertificateVerifier { |
| 37 | public: |
| 38 | status_t verify(const X509*, uint8_t*) override; |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 39 | |
| 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 Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 45 | [[nodiscard]] status_t addTrustedPeerCertificate(RpcCertificateFormat format, |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 46 | const std::vector<uint8_t>& cert); |
| 47 | |
| 48 | private: |
| 49 | std::mutex mMutex; // for below |
| 50 | std::vector<bssl::UniquePtr<X509>> mTrustedPeerCertificates; |
Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | } // namespace android |