blob: 5f0c7162eb8305a694c3ec999383763dc5d47929 [file] [log] [blame]
Yingdi Yub263f152015-07-12 16:50:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
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
22#include "verifier-filter.hpp"
23#include "../detail/openssl.hpp"
24
25namespace ndn {
26namespace security {
27namespace transform {
28
29class VerifierFilter::Impl
30{
31public:
32 Impl(const PublicKey& key, const uint8_t* sig, size_t sigLen)
33 : m_key(key)
34 , m_md(BIO_new(BIO_f_md()))
35 , m_sink(BIO_new(BIO_s_null()))
36 , m_sig(sig)
37 , m_sigLen(sigLen)
38 {
39 BIO_push(m_md, m_sink);
40 }
41
42 ~Impl()
43 {
Junxiao Shi1b0f4862016-09-10 17:45:04 +000044 BIO_free_all(m_md);
Yingdi Yub263f152015-07-12 16:50:13 -070045 }
46
47public:
48 const PublicKey& m_key;
49
50 BIO* m_md;
51 BIO* m_sink;
52
53 const uint8_t* m_sig;
54 size_t m_sigLen;
55};
56
57VerifierFilter::VerifierFilter(DigestAlgorithm algo, const PublicKey& key,
58 const uint8_t* sig, size_t sigLen)
59 : m_impl(new Impl(key, sig, sigLen))
60{
61 switch (algo) {
62 case DigestAlgorithm::SHA256: {
63 if (!BIO_set_md(m_impl->m_md, EVP_sha256()))
64 BOOST_THROW_EXCEPTION(Error(getIndex(), "Cannot set digest"));
65 break;
66 }
67
68 default:
69 BOOST_THROW_EXCEPTION(Error(getIndex(), "Digest algorithm is not supported"));
70 }
71}
72
73size_t
74VerifierFilter::convert(const uint8_t* buf, size_t size)
75{
76 int wLen = BIO_write(m_impl->m_md, buf, size);
77
78 if (wLen <= 0) { // fail to write data
79 if (!BIO_should_retry(m_impl->m_md)) {
80 // we haven't written everything but some error happens, and we cannot retry
81 BOOST_THROW_EXCEPTION(Error(getIndex(), "Failed to accept more input"));
82 }
83 return 0;
84 }
85 else { // update number of bytes written
86 return wLen;
87 }
88}
89
90void
91VerifierFilter::finalize()
92{
93 EVP_PKEY* key = reinterpret_cast<EVP_PKEY*>(m_impl->m_key.getEvpPkey());
94 auto buffer = make_unique<OBuffer>(1);
95
96 EVP_MD_CTX* ctx = nullptr;
97 BIO_get_md_ctx(m_impl->m_md, &ctx);
98 int res = EVP_VerifyFinal(ctx, m_impl->m_sig, m_impl->m_sigLen, key);
99
100 if (res < 0)
101 BOOST_THROW_EXCEPTION(Error(getIndex(), "Verification error"));
102
103 (*buffer)[0] = (res != 0) ? 1 : 0;
104 setOutputBuffer(std::move(buffer));
105
106 flushAllOutput();
107}
108
109unique_ptr<Transform>
110verifierFilter(DigestAlgorithm algo, const PublicKey& key,
111 const uint8_t* sig, size_t sigLen)
112{
113 return make_unique<VerifierFilter>(algo, key, sig, sigLen);
114}
115
116} // namespace transform
117} // namespace security
118} // namespace ndn