blob: 0cf0e1c10d727ebde7c7336723301710f5a6d6de [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 Hong1deca4b2021-09-10 16:16:44 -070019#include <binder/RpcCertificateUtils.h>
20
Yifan Hong13c90062021-09-09 14:59:53 -070021#include "RpcCertificateVerifierSimple.h"
22
23namespace android {
24
Yifan Hong1deca4b2021-09-10 16:16:44 -070025status_t RpcCertificateVerifierSimple::verify(const X509* peerCert, uint8_t* outAlert) {
26 std::lock_guard<std::mutex> lock(mMutex);
27 for (const auto& trustedCert : mTrustedPeerCertificates) {
28 if (0 == X509_cmp(trustedCert.get(), peerCert)) {
29 return OK;
30 }
31 }
32 *outAlert = SSL_AD_CERTIFICATE_UNKNOWN;
33 return PERMISSION_DENIED;
34}
35
36status_t RpcCertificateVerifierSimple::addTrustedPeerCertificate(CertificateFormat format,
37 const std::vector<uint8_t>& cert) {
38 bssl::UniquePtr<X509> x509 = deserializeCertificate(cert, format);
39 if (x509 == nullptr) {
40 ALOGE("Certificate is not in the proper format %s", PrintToString(format).c_str());
41 return BAD_VALUE;
42 }
43 std::lock_guard<std::mutex> lock(mMutex);
44 mTrustedPeerCertificates.push_back(std::move(x509));
Yifan Hong13c90062021-09-09 14:59:53 -070045 return OK;
46}
47
48} // namespace android