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 | #define LOG_TAG "RpcCertificateVerifierSimple" |
| 17 | #include <log/log.h> |
| 18 | |
Yifan Hong | bb24eea | 2021-09-17 18:21:56 -0700 | [diff] [blame] | 19 | #include <binder/RpcTlsUtils.h> |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 20 | |
Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 21 | #include "RpcCertificateVerifierSimple.h" |
| 22 | |
| 23 | namespace android { |
| 24 | |
Yifan Hong | b160f8c | 2021-09-17 22:59:11 -0700 | [diff] [blame] | 25 | status_t RpcCertificateVerifierSimple::verify(const SSL* ssl, uint8_t* outAlert) { |
| 26 | const char* logPrefix = SSL_is_server(ssl) ? "Server" : "Client"; |
| 27 | bssl::UniquePtr<X509> peerCert(SSL_get_peer_certificate(ssl)); // Does not set error queue |
| 28 | LOG_ALWAYS_FATAL_IF(peerCert == nullptr, |
| 29 | "%s: libssl should not ask to verify non-existing cert", logPrefix); |
| 30 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 31 | std::lock_guard<std::mutex> lock(mMutex); |
| 32 | for (const auto& trustedCert : mTrustedPeerCertificates) { |
Yifan Hong | b160f8c | 2021-09-17 22:59:11 -0700 | [diff] [blame] | 33 | if (0 == X509_cmp(trustedCert.get(), peerCert.get())) { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 34 | return OK; |
| 35 | } |
| 36 | } |
| 37 | *outAlert = SSL_AD_CERTIFICATE_UNKNOWN; |
| 38 | return PERMISSION_DENIED; |
| 39 | } |
| 40 | |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 41 | status_t RpcCertificateVerifierSimple::addTrustedPeerCertificate(RpcCertificateFormat format, |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 42 | const std::vector<uint8_t>& cert) { |
| 43 | bssl::UniquePtr<X509> x509 = deserializeCertificate(cert, format); |
| 44 | if (x509 == nullptr) { |
| 45 | ALOGE("Certificate is not in the proper format %s", PrintToString(format).c_str()); |
| 46 | return BAD_VALUE; |
| 47 | } |
| 48 | std::lock_guard<std::mutex> lock(mMutex); |
| 49 | mTrustedPeerCertificates.push_back(std::move(x509)); |
Yifan Hong | 13c9006 | 2021-09-09 14:59:53 -0700 | [diff] [blame] | 50 | return OK; |
| 51 | } |
| 52 | |
| 53 | } // namespace android |