Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2021 Regents of the University of California. |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/transform/base64-decode.hpp" |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 23 | #include "ndn-cxx/security/impl/openssl.hpp" |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace security { |
| 27 | namespace transform { |
| 28 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame^] | 29 | class Base64Decode::Impl : boost::noncopyable |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 30 | { |
| 31 | public: |
| 32 | Impl() |
| 33 | : m_base64(BIO_new(BIO_f_base64())) |
| 34 | , m_source(BIO_new(BIO_s_mem())) |
| 35 | { |
| 36 | // Input may not be written in a single time. |
| 37 | // Do not return EOF when source is empty unless explicitly requested |
| 38 | BIO_set_mem_eof_return(m_source, -1); |
| 39 | |
| 40 | // connect base64 transform to the data source. |
| 41 | BIO_push(m_base64, m_source); |
| 42 | } |
| 43 | |
| 44 | ~Impl() |
| 45 | { |
Junxiao Shi | 1b0f486 | 2016-09-10 17:45:04 +0000 | [diff] [blame] | 46 | BIO_free_all(m_base64); |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | public: |
| 50 | BIO* m_base64; |
| 51 | BIO* m_source; // BIO_f_base64 alone does not work without a source |
| 52 | }; |
| 53 | |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 54 | |
| 55 | Base64Decode::Base64Decode(bool expectNewlineEvery64Bytes) |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 56 | : m_impl(make_unique<Impl>()) |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 57 | { |
| 58 | if (!expectNewlineEvery64Bytes) |
| 59 | BIO_set_flags(m_impl->m_base64, BIO_FLAGS_BASE64_NO_NL); |
| 60 | } |
| 61 | |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 62 | Base64Decode::~Base64Decode() = default; |
| 63 | |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 64 | void |
| 65 | Base64Decode::preTransform() |
| 66 | { |
| 67 | while (isOutputBufferEmpty()) { |
| 68 | fillOutputBuffer(); |
| 69 | if (isOutputBufferEmpty()) // nothing to read from BIO, return |
| 70 | return; |
| 71 | |
| 72 | flushOutputBuffer(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | size_t |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame^] | 77 | Base64Decode::convert(span<const uint8_t> buf) |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 78 | { |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame^] | 79 | int wLen = BIO_write(m_impl->m_source, buf.data(), buf.size()); |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 80 | |
| 81 | if (wLen <= 0) { // fail to write data |
| 82 | if (!BIO_should_retry(m_impl->m_source)) { |
| 83 | // we haven't written everything but some error happens, and we cannot retry |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 84 | NDN_THROW(Error(getIndex(), "Failed to accept more input")); |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 85 | } |
| 86 | return 0; |
| 87 | } |
| 88 | else { // update number of bytes written |
| 89 | return wLen; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | Base64Decode::finalize() |
| 95 | { |
| 96 | BIO_set_mem_eof_return(m_impl->m_source, 0); |
| 97 | |
| 98 | fillOutputBuffer(); |
| 99 | |
| 100 | while (!isOutputBufferEmpty()) { |
| 101 | flushOutputBuffer(); |
| 102 | if (isOutputBufferEmpty()) |
| 103 | fillOutputBuffer(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | Base64Decode::fillOutputBuffer() |
| 109 | { |
| 110 | // OpenSSL base64 BIO cannot give us the number bytes of partial decoded result, |
| 111 | // so we just try to read a chunk. |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame^] | 112 | auto buffer = make_unique<OBuffer>(1024); |
Davide Pesavento | 8aad372 | 2017-09-16 20:57:28 -0400 | [diff] [blame] | 113 | int rLen = BIO_read(m_impl->m_base64, buffer->data(), buffer->size()); |
Yingdi Yu | 38317e5 | 2015-07-22 13:58:02 -0700 | [diff] [blame] | 114 | if (rLen <= 0) |
| 115 | return; |
| 116 | |
| 117 | if (static_cast<size_t>(rLen) < buffer->size()) |
| 118 | buffer->erase(buffer->begin() + rLen, buffer->end()); |
| 119 | |
| 120 | setOutputBuffer(std::move(buffer)); |
| 121 | } |
| 122 | |
| 123 | unique_ptr<Transform> |
| 124 | base64Decode(bool expectNewlineEvery64Bytes) |
| 125 | { |
| 126 | return make_unique<Base64Decode>(expectNewlineEvery64Bytes); |
| 127 | } |
| 128 | |
| 129 | } // namespace transform |
| 130 | } // namespace security |
| 131 | } // namespace ndn |