blob: 1f74adc4655b38ab1d11a5cb853f84cb4f12fbe3 [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#define LOG_TAG "RpcCertificateVerifierSimple"
17#include <log/log.h>
18
Yifan Hongbb24eea2021-09-17 18:21:56 -070019#include <binder/RpcTlsUtils.h>
Yifan Hong1deca4b2021-09-10 16:16:44 -070020
Yifan Hong13c90062021-09-09 14:59:53 -070021#include "RpcCertificateVerifierSimple.h"
22
23namespace android {
24
Yifan Hongb160f8c2021-09-17 22:59:11 -070025status_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 Hong1deca4b2021-09-10 16:16:44 -070031 std::lock_guard<std::mutex> lock(mMutex);
32 for (const auto& trustedCert : mTrustedPeerCertificates) {
Yifan Hongb160f8c2021-09-17 22:59:11 -070033 if (0 == X509_cmp(trustedCert.get(), peerCert.get())) {
Yifan Hong1deca4b2021-09-10 16:16:44 -070034 return OK;
35 }
36 }
37 *outAlert = SSL_AD_CERTIFICATE_UNKNOWN;
38 return PERMISSION_DENIED;
39}
40
Yifan Hong9734cfc2021-09-13 16:14:09 -070041status_t RpcCertificateVerifierSimple::addTrustedPeerCertificate(RpcCertificateFormat format,
Yifan Hong1deca4b2021-09-10 16:16:44 -070042 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 Hong13c90062021-09-09 14:59:53 -070050 return OK;
51}
52
53} // namespace android