Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 1 | /* |
| 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 Hong | bb24eea | 2021-09-17 18:21:56 -0700 | [diff] [blame] | 17 | #define LOG_TAG "RpcTlsUtils" |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 18 | #include <log/log.h> |
| 19 | |
Yifan Hong | bb24eea | 2021-09-17 18:21:56 -0700 | [diff] [blame] | 20 | #include <binder/RpcTlsUtils.h> |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 21 | |
| 22 | #include "Utils.h" |
| 23 | |
Hao Chen | f1c4777 | 2023-05-31 14:12:33 -0700 | [diff] [blame] | 24 | #include <limits> |
| 25 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | |
| 28 | namespace { |
| 29 | |
Yifan Hong | ff73aa9 | 2021-09-17 21:28:01 -0700 | [diff] [blame] | 30 | static_assert(sizeof(unsigned char) == sizeof(uint8_t)); |
| 31 | |
| 32 | template <typename PemReadBioFn, |
| 33 | typename T = std::remove_pointer_t<std::invoke_result_t< |
| 34 | PemReadBioFn, BIO*, std::nullptr_t, std::nullptr_t, std::nullptr_t>>> |
| 35 | bssl::UniquePtr<T> fromPem(const std::vector<uint8_t>& data, PemReadBioFn fn) { |
| 36 | if (data.size() > std::numeric_limits<int>::max()) return nullptr; |
| 37 | bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(data.data(), static_cast<int>(data.size()))); |
| 38 | return bssl::UniquePtr<T>(fn(bio.get(), nullptr, nullptr, nullptr)); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Yifan Hong | ff73aa9 | 2021-09-17 21:28:01 -0700 | [diff] [blame] | 41 | template <typename D2iFn, |
| 42 | typename T = std::remove_pointer_t< |
| 43 | std::invoke_result_t<D2iFn, std::nullptr_t, const unsigned char**, long>>> |
| 44 | bssl::UniquePtr<T> fromDer(const std::vector<uint8_t>& data, D2iFn fn) { |
| 45 | if (data.size() > std::numeric_limits<long>::max()) return nullptr; |
| 46 | const unsigned char* dataPtr = data.data(); |
| 47 | auto expectedEnd = dataPtr + data.size(); |
| 48 | bssl::UniquePtr<T> ret(fn(nullptr, &dataPtr, static_cast<long>(data.size()))); |
| 49 | if (dataPtr != expectedEnd) { |
| 50 | ALOGE("%s: %td bytes remaining!", __PRETTY_FUNCTION__, expectedEnd - dataPtr); |
Yifan Hong | 2c23db1 | 2021-08-16 16:59:37 -0700 | [diff] [blame] | 51 | return nullptr; |
| 52 | } |
| 53 | return ret; |
| 54 | } |
| 55 | |
Yifan Hong | ff73aa9 | 2021-09-17 21:28:01 -0700 | [diff] [blame] | 56 | template <typename T, typename WriteBioFn = int (*)(BIO*, T*)> |
| 57 | std::vector<uint8_t> serialize(T* object, WriteBioFn writeBio) { |
| 58 | bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem())); |
| 59 | TEST_AND_RETURN({}, writeBio(bio.get(), object)); |
| 60 | const uint8_t* data; |
| 61 | size_t len; |
| 62 | TEST_AND_RETURN({}, BIO_mem_contents(bio.get(), &data, &len)); |
| 63 | return std::vector<uint8_t>(data, data + len); |
| 64 | } |
| 65 | |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 66 | } // namespace |
| 67 | |
Yifan Hong | ff73aa9 | 2021-09-17 21:28:01 -0700 | [diff] [blame] | 68 | bssl::UniquePtr<X509> deserializeCertificate(const std::vector<uint8_t>& data, |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 69 | RpcCertificateFormat format) { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 70 | switch (format) { |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 71 | case RpcCertificateFormat::PEM: |
Yifan Hong | ff73aa9 | 2021-09-17 21:28:01 -0700 | [diff] [blame] | 72 | return fromPem(data, PEM_read_bio_X509); |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 73 | case RpcCertificateFormat::DER: |
Yifan Hong | ff73aa9 | 2021-09-17 21:28:01 -0700 | [diff] [blame] | 74 | return fromDer(data, d2i_X509); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 75 | } |
| 76 | LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format)); |
| 77 | } |
| 78 | |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 79 | std::vector<uint8_t> serializeCertificate(X509* x509, RpcCertificateFormat format) { |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 80 | switch (format) { |
Yifan Hong | ff73aa9 | 2021-09-17 21:28:01 -0700 | [diff] [blame] | 81 | case RpcCertificateFormat::PEM: |
| 82 | return serialize(x509, PEM_write_bio_X509); |
| 83 | case RpcCertificateFormat::DER: |
| 84 | return serialize(x509, i2d_X509_bio); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 85 | } |
Yifan Hong | ff73aa9 | 2021-09-17 21:28:01 -0700 | [diff] [blame] | 86 | LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format)); |
| 87 | } |
| 88 | |
| 89 | bssl::UniquePtr<EVP_PKEY> deserializeUnencryptedPrivatekey(const std::vector<uint8_t>& data, |
| 90 | RpcKeyFormat format) { |
| 91 | switch (format) { |
| 92 | case RpcKeyFormat::PEM: |
| 93 | return fromPem(data, PEM_read_bio_PrivateKey); |
| 94 | case RpcKeyFormat::DER: |
| 95 | return fromDer(data, d2i_AutoPrivateKey); |
| 96 | } |
| 97 | LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format)); |
| 98 | } |
| 99 | |
| 100 | std::vector<uint8_t> serializeUnencryptedPrivatekey(EVP_PKEY* pkey, RpcKeyFormat format) { |
| 101 | switch (format) { |
| 102 | case RpcKeyFormat::PEM: |
| 103 | return serialize(pkey, [](BIO* bio, EVP_PKEY* pkey) { |
| 104 | return PEM_write_bio_PrivateKey(bio, pkey, nullptr /* enc */, nullptr /* kstr */, |
| 105 | 0 /* klen */, nullptr, nullptr); |
| 106 | }); |
| 107 | case RpcKeyFormat::DER: |
| 108 | return serialize(pkey, i2d_PrivateKey_bio); |
| 109 | } |
| 110 | LOG_ALWAYS_FATAL("Unsupported format %d", static_cast<int>(format)); |
Yifan Hong | 1deca4b | 2021-09-10 16:16:44 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } // namespace android |