Loading libs/binder/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -256,6 +256,7 @@ cc_defaults { ], srcs: [ "RpcTransportTls.cpp", "RpcCertificateUtils.cpp", ], } Loading libs/binder/RpcCertificateUtils.cpp 0 → 100644 +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 libs/binder/RpcServer.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -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); } Loading libs/binder/RpcSession.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -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); } Loading libs/binder/RpcTransportRaw.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -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 Loading
libs/binder/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -256,6 +256,7 @@ cc_defaults { ], srcs: [ "RpcTransportTls.cpp", "RpcCertificateUtils.cpp", ], } Loading
libs/binder/RpcCertificateUtils.cpp 0 → 100644 +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
libs/binder/RpcServer.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -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); } Loading
libs/binder/RpcSession.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -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); } Loading
libs/binder/RpcTransportRaw.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -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