Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11125b58 authored by Yifan Hong's avatar Yifan Hong Committed by Automerger Merge Worker
Browse files

Merge changes Ifaea4598,I78b4d2b1,I6c1f0f88,I6b4d0ebc,I5c17f464 am: a3612f32 am: 1e8a77e6

Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/1825920

Change-Id: I00f43869122ec7f62939eeb00538d0e0e5549246
parents 2824e91b 1e8a77e6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -259,6 +259,7 @@ cc_defaults {
    ],
    srcs: [
        "RpcTransportTls.cpp",
        "RpcCertificateUtils.cpp",
    ],
}

+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "RpcCertificateUtils"
#include <log/log.h>

#include <binder/RpcCertificateUtils.h>

#include "Utils.h"

namespace android {

namespace {

bssl::UniquePtr<X509> fromPem(const std::vector<uint8_t>& cert) {
    if (cert.size() > std::numeric_limits<int>::max()) return nullptr;
    bssl::UniquePtr<BIO> certBio(BIO_new_mem_buf(cert.data(), static_cast<int>(cert.size())));
    return bssl::UniquePtr<X509>(PEM_read_bio_X509(certBio.get(), nullptr, nullptr, nullptr));
}

bssl::UniquePtr<X509> fromDer(const std::vector<uint8_t>& cert) {
    if (cert.size() > std::numeric_limits<long>::max()) return nullptr;
    const unsigned char* data = cert.data();
    auto expectedEnd = data + cert.size();
    bssl::UniquePtr<X509> ret(d2i_X509(nullptr, &data, static_cast<long>(cert.size())));
    if (data != expectedEnd) {
        ALOGE("%s: %td bytes remaining!", __PRETTY_FUNCTION__, expectedEnd - data);
        return nullptr;
    }
    return ret;
}

} // namespace

bssl::UniquePtr<X509> deserializeCertificate(const std::vector<uint8_t>& cert,
                                             RpcCertificateFormat format) {
    switch (format) {
        case RpcCertificateFormat::PEM:
            return fromPem(cert);
        case RpcCertificateFormat::DER:
            return fromDer(cert);
    }
    LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format));
}

std::vector<uint8_t> serializeCertificate(X509* x509, RpcCertificateFormat format) {
    bssl::UniquePtr<BIO> certBio(BIO_new(BIO_s_mem()));
    switch (format) {
        case RpcCertificateFormat::PEM: {
            TEST_AND_RETURN({}, PEM_write_bio_X509(certBio.get(), x509));
        } break;
        case RpcCertificateFormat::DER: {
            TEST_AND_RETURN({}, i2d_X509_bio(certBio.get(), x509));
        } break;
        default: {
            LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format));
        }
    }
    const uint8_t* data;
    size_t len;
    TEST_AND_RETURN({}, BIO_mem_contents(certBio.get(), &data, &len));
    return std::vector<uint8_t>(data, data + len);
}

} // namespace android
+1 −1
Original line number Diff line number Diff line
@@ -141,7 +141,7 @@ sp<IBinder> RpcServer::getRootObject() {
    return ret;
}

std::string RpcServer::getCertificate(CertificateFormat format) {
std::vector<uint8_t> RpcServer::getCertificate(RpcCertificateFormat format) {
    std::lock_guard<std::mutex> _l(mLock);
    return mCtx->getCertificate(format);
}
+1 −1
Original line number Diff line number Diff line
@@ -703,7 +703,7 @@ bool RpcSession::removeIncomingConnection(const sp<RpcConnection>& connection) {
    return false;
}

std::string RpcSession::getCertificate(CertificateFormat format) {
std::vector<uint8_t> RpcSession::getCertificate(RpcCertificateFormat format) {
    return mCtx->getCertificate(format);
}

+1 −1
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ public:
    std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const {
        return std::make_unique<RpcTransportRaw>(std::move(fd));
    }
    std::string getCertificate(CertificateFormat) const override { return {}; }
    std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
};

} // namespace
Loading