blob: f3ca02a3bd77d4cec5866104ab809bb4459d15a3 [file] [log] [blame]
Yifan Hong1deca4b2021-09-10 16:16:44 -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
Yifan Hongbb24eea2021-09-17 18:21:56 -070017#define LOG_TAG "RpcTlsUtils"
Yifan Hong1deca4b2021-09-10 16:16:44 -070018#include <log/log.h>
19
Yifan Hongbb24eea2021-09-17 18:21:56 -070020#include <binder/RpcTlsUtils.h>
Yifan Hong1deca4b2021-09-10 16:16:44 -070021
22#include "Utils.h"
23
24namespace android {
25
26namespace {
27
Yifan Hongff73aa92021-09-17 21:28:01 -070028static_assert(sizeof(unsigned char) == sizeof(uint8_t));
29
30template <typename PemReadBioFn,
31 typename T = std::remove_pointer_t<std::invoke_result_t<
32 PemReadBioFn, BIO*, std::nullptr_t, std::nullptr_t, std::nullptr_t>>>
33bssl::UniquePtr<T> fromPem(const std::vector<uint8_t>& data, PemReadBioFn fn) {
34 if (data.size() > std::numeric_limits<int>::max()) return nullptr;
35 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(data.data(), static_cast<int>(data.size())));
36 return bssl::UniquePtr<T>(fn(bio.get(), nullptr, nullptr, nullptr));
Yifan Hong1deca4b2021-09-10 16:16:44 -070037}
38
Yifan Hongff73aa92021-09-17 21:28:01 -070039template <typename D2iFn,
40 typename T = std::remove_pointer_t<
41 std::invoke_result_t<D2iFn, std::nullptr_t, const unsigned char**, long>>>
42bssl::UniquePtr<T> fromDer(const std::vector<uint8_t>& data, D2iFn fn) {
43 if (data.size() > std::numeric_limits<long>::max()) return nullptr;
44 const unsigned char* dataPtr = data.data();
45 auto expectedEnd = dataPtr + data.size();
46 bssl::UniquePtr<T> ret(fn(nullptr, &dataPtr, static_cast<long>(data.size())));
47 if (dataPtr != expectedEnd) {
48 ALOGE("%s: %td bytes remaining!", __PRETTY_FUNCTION__, expectedEnd - dataPtr);
Yifan Hong2c23db12021-08-16 16:59:37 -070049 return nullptr;
50 }
51 return ret;
52}
53
Yifan Hongff73aa92021-09-17 21:28:01 -070054template <typename T, typename WriteBioFn = int (*)(BIO*, T*)>
55std::vector<uint8_t> serialize(T* object, WriteBioFn writeBio) {
56 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
57 TEST_AND_RETURN({}, writeBio(bio.get(), object));
58 const uint8_t* data;
59 size_t len;
60 TEST_AND_RETURN({}, BIO_mem_contents(bio.get(), &data, &len));
61 return std::vector<uint8_t>(data, data + len);
62}
63
Yifan Hong1deca4b2021-09-10 16:16:44 -070064} // namespace
65
Yifan Hongff73aa92021-09-17 21:28:01 -070066bssl::UniquePtr<X509> deserializeCertificate(const std::vector<uint8_t>& data,
Yifan Hong9734cfc2021-09-13 16:14:09 -070067 RpcCertificateFormat format) {
Yifan Hong1deca4b2021-09-10 16:16:44 -070068 switch (format) {
Yifan Hong9734cfc2021-09-13 16:14:09 -070069 case RpcCertificateFormat::PEM:
Yifan Hongff73aa92021-09-17 21:28:01 -070070 return fromPem(data, PEM_read_bio_X509);
Yifan Hong9734cfc2021-09-13 16:14:09 -070071 case RpcCertificateFormat::DER:
Yifan Hongff73aa92021-09-17 21:28:01 -070072 return fromDer(data, d2i_X509);
Yifan Hong1deca4b2021-09-10 16:16:44 -070073 }
74 LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format));
75}
76
Yifan Hong9734cfc2021-09-13 16:14:09 -070077std::vector<uint8_t> serializeCertificate(X509* x509, RpcCertificateFormat format) {
Yifan Hong1deca4b2021-09-10 16:16:44 -070078 switch (format) {
Yifan Hongff73aa92021-09-17 21:28:01 -070079 case RpcCertificateFormat::PEM:
80 return serialize(x509, PEM_write_bio_X509);
81 case RpcCertificateFormat::DER:
82 return serialize(x509, i2d_X509_bio);
Yifan Hong1deca4b2021-09-10 16:16:44 -070083 }
Yifan Hongff73aa92021-09-17 21:28:01 -070084 LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format));
85}
86
87bssl::UniquePtr<EVP_PKEY> deserializeUnencryptedPrivatekey(const std::vector<uint8_t>& data,
88 RpcKeyFormat format) {
89 switch (format) {
90 case RpcKeyFormat::PEM:
91 return fromPem(data, PEM_read_bio_PrivateKey);
92 case RpcKeyFormat::DER:
93 return fromDer(data, d2i_AutoPrivateKey);
94 }
95 LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format));
96}
97
98std::vector<uint8_t> serializeUnencryptedPrivatekey(EVP_PKEY* pkey, RpcKeyFormat format) {
99 switch (format) {
100 case RpcKeyFormat::PEM:
101 return serialize(pkey, [](BIO* bio, EVP_PKEY* pkey) {
102 return PEM_write_bio_PrivateKey(bio, pkey, nullptr /* enc */, nullptr /* kstr */,
103 0 /* klen */, nullptr, nullptr);
104 });
105 case RpcKeyFormat::DER:
106 return serialize(pkey, i2d_PrivateKey_bio);
107 }
108 LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format));
Yifan Hong1deca4b2021-09-10 16:16:44 -0700109}
110
111} // namespace android